diff options
author | cflip <cflip@cflip.net> | 2023-01-08 21:50:51 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-01-08 21:50:51 -0700 |
commit | 54d8fdd290e21ae8c773b4228827ee7539637ce8 (patch) | |
tree | 064eb597ddf55a790f2beb4f44af1e58b206d296 | |
parent | a43cf9420a8ca8f84b6ba92cffc4835173ce1c35 (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.
-rw-r--r-- | font.c | 3 | ||||
-rw-r--r-- | window.c | 44 |
2 files changed, 40 insertions, 7 deletions
@@ -49,6 +49,9 @@ BDFFontInfo font_load(const char *filename) result.bounds.h = 0; result.chars = NULL; result.num_chars = 0; + for (int i = 0; i < 128; i++) { + result.char_index_for_code_point[i] = 0; + } fp = fopen(filename, "r"); if (fp == NULL) { @@ -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); } |