From 54d8fdd290e21ae8c773b4228827ee7539637ce8 Mon Sep 17 00:00:00 2001 From: cflip Date: Sun, 8 Jan 2023 21:50:51 -0700 Subject: 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. --- window.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'window.c') 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 + +#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); } -- cgit v1.2.3