From a40638b63deeb27301d56db7426fba56850927ca Mon Sep 17 00:00:00 2001 From: cflip Date: Tue, 17 Jan 2023 20:52:18 -0700 Subject: Allow window to be resizable --- editor.c | 10 +++++++--- editor.h | 1 + window.c | 6 +++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/editor.c b/editor.c index 963a4e4..f7fc684 100644 --- a/editor.c +++ b/editor.c @@ -27,9 +27,7 @@ void init_editor(struct editor_state* editor) editor->syntax = NULL; editor->mode = EDITOR_MODE_NORMAL; - window_get_size(&editor->screen_rows, &editor->screen_cols); - - editor->screen_rows -= 2; + editor_update_screen_size(editor); } void editor_set_status_message(struct editor_state* editor, const char* format, ...) @@ -255,6 +253,12 @@ void editor_scroll(struct editor_state* editor) editor->col_offset = editor->cursor_display_x - editor->screen_cols + 1; } +void editor_update_screen_size(struct editor_state *editor) +{ + window_get_size(&editor->screen_rows, &editor->screen_cols); + editor->screen_rows -= 2; +} + void editor_draw_rows(struct editor_state* editor, struct append_buffer* buffer) { int y; diff --git a/editor.h b/editor.h index a46c37f..aaed086 100644 --- a/editor.h +++ b/editor.h @@ -54,6 +54,7 @@ void editor_add_line_below(struct editor_state* editor); void editor_find(struct editor_state* editor); void editor_scroll(struct editor_state* editor); +void editor_update_screen_size(struct editor_state *); void editor_draw_rows(struct editor_state* editor, struct append_buffer* buffer); void editor_draw_status_bar(struct editor_state* editor, struct append_buffer* buffer); void editor_draw_message_bar(struct editor_state* editor, struct append_buffer* buffer); diff --git a/window.c b/window.c index ea3f9f4..74e2e10 100644 --- a/window.c +++ b/window.c @@ -23,7 +23,7 @@ void window_init(const char *title, int rows, int cols) int window_width = cols * font.width; int window_height = rows * font.height; - window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, 0); + window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, SDL_WINDOW_RESIZABLE); if (window == NULL) fatal_error("Failed to create window: %s\n", SDL_GetError()); @@ -59,6 +59,10 @@ int window_handle_event(struct editor_state *editor) } editor_insert_char(editor, *e.text.text); break; + case SDL_WINDOWEVENT: + if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) + editor_update_screen_size(editor); + break; } return 1; } -- cgit v1.2.3