summaryrefslogtreecommitdiff
path: root/window.c
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-01-08 21:50:51 -0700
committercflip <cflip@cflip.net>2023-01-08 21:50:51 -0700
commit54d8fdd290e21ae8c773b4228827ee7539637ce8 (patch)
tree064eb597ddf55a790f2beb4f44af1e58b206d296 /window.c
parenta43cf9420a8ca8f84b6ba92cffc4835173ce1c35 (diff)
Hook up the editor output to the window
Now the text editor is visible in the window! There are still escape codes for terminal output all over the place, but this is a good start.
Diffstat (limited to 'window.c')
-rw-r--r--window.c44
1 files changed, 37 insertions, 7 deletions
diff --git a/window.c b/window.c
index b3b54f0..81bf634 100644
--- a/window.c
+++ b/window.c
@@ -1,6 +1,9 @@
#include "window.h"
#include <SDL2/SDL.h>
+
+#include "buffer.h"
+#include "editor.h"
#include "font.h"
static SDL_Window *window = NULL;
@@ -47,23 +50,50 @@ int window_handle_event()
return 1;
}
-void window_redraw(struct editor_state *editor)
+static void draw_font_text(struct append_buffer *buffer)
{
- const char *testmsg = "Hello, welcome to the bitmap font test!";
- SDL_RenderClear(renderer);
SDL_Rect dstrect = { 0, 0, 0, 0 };
- for (int i = 0; i < strlen(testmsg); i++) {
- const char letter = testmsg[i];
+ for (int i = 0; i < buffer->length; i++) {
+ const char letter = buffer->buffer[i];
+ if (letter > 128) {
+ printf("TODO: Non-ASCII characters are not currently supported.\n");
+ continue;
+ }
+
const int char_index = font.char_index_for_code_point[letter];
BDFFontChar *glyph = &font.chars[char_index];
+ if (glyph == NULL) {
+ fprintf(stderr, "Font doesn't have character %c", letter);
+ continue;
+ }
dstrect.w = glyph->bounds.w;
dstrect.h = glyph->bounds.h;
SDL_RenderCopy(renderer, font_texture, &glyph->bounds, &dstrect);
- dstrect.x += glyph->next_glyph_offset.x;
- dstrect.y += glyph->next_glyph_offset.y;
+ if (letter == '\n') {
+ dstrect.x = 0;
+ dstrect.y += font.bounds.h;
+ } else {
+ dstrect.x += glyph->next_glyph_offset.x;
+ dstrect.y += glyph->next_glyph_offset.y;
+ }
}
+}
+
+void window_redraw(struct editor_state *editor)
+{
+ SDL_RenderClear(renderer);
+
+ editor_scroll(editor);
+ struct append_buffer buffer = ABUF_INIT;
+
+ editor_draw_rows(editor, &buffer);
+ editor_draw_status_bar(editor, &buffer);
+ editor_draw_message_bar(editor, &buffer);
+
+ draw_font_text(&buffer);
+
SDL_RenderPresent(renderer);
SDL_UpdateWindowSurface(window);
}