From 2cb9c0273f8ae4db998411e3ff290899e1eb042a Mon Sep 17 00:00:00 2001 From: Shylie Date: Mon, 25 Dec 2023 17:38:44 -0500 Subject: [PATCH] Added resize callback, made main callback optional --- include/terml.h | 13 ++++++++++--- source/terml.cpp | 26 ++++++++++++++++++++++++-- source/terml_private.h | 4 +++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/include/terml.h b/include/terml.h index 7e15d79..4f74252 100644 --- a/include/terml.h +++ b/include/terml.h @@ -6,9 +6,15 @@ extern "C" { #endif -typedef void (*terml_main_callback)(); -typedef int (*terml_quit_callback)(); -typedef void (*terml_key_callback) (char code); +typedef void (*terml_main_callback) (); +typedef int (*terml_quit_callback) (); +typedef void (*terml_key_callback) (char code); +typedef void (*terml_resize_callback)( + unsigned int previous_width, + unsigned int previous_height, + unsigned int new_width, + unsigned int new_height +); int terml_init(); int terml_deinit(); @@ -23,6 +29,7 @@ void terml_flush(); void terml_set_main_callback(terml_main_callback); void terml_set_quit_callback(terml_quit_callback); void terml_set_key_callback(terml_key_callback); +void terml_set_resize_callback(terml_resize_callback); void terml_start(); void terml_stop(); diff --git a/source/terml.cpp b/source/terml.cpp index 44e8e20..1ab1f34 100644 --- a/source/terml.cpp +++ b/source/terml.cpp @@ -13,6 +13,7 @@ terml::terml() : main(nullptr), quit(nullptr), key(nullptr), + resize(nullptr), buffer(nullptr), width(0), height(0) @@ -136,7 +137,12 @@ void terml::set_key_callback(terml_key_callback callback) key = callback; } -void terml::key_event(char code) +void terml::set_resize_callback(terml_resize_callback callback) +{ + resize = callback; +} + +void terml::key_event(char code) const { if (key) { @@ -158,7 +164,10 @@ void terml::mainloop() while (current_time >= last_time + wait_time) { - main(); + if (main) + { + main(); + } process_events(); @@ -215,6 +224,9 @@ void terml::setup_buffer() if (width != new_width || height != new_height) { + const unsigned int old_width = width; + const unsigned int old_height = height; + width = new_width; height = new_height; @@ -229,6 +241,11 @@ void terml::setup_buffer() } buffer[CELL_SIZE * width * height] = '\0'; + + if (resize) + { + resize(old_width, old_height, new_width, new_height); + } } } @@ -321,6 +338,11 @@ extern "C" TERML_G->set_key_callback(callback); } + void terml_set_resize_callback(terml_resize_callback callback) + { + TERML_G->set_resize_callback(callback); + } + void terml_start() { try diff --git a/source/terml_private.h b/source/terml_private.h index 18ba3da..a3be10f 100644 --- a/source/terml_private.h +++ b/source/terml_private.h @@ -44,6 +44,7 @@ public: void set_main_callback(terml_main_callback); void set_quit_callback(terml_quit_callback); void set_key_callback(terml_key_callback); + void set_resize_callback(terml_resize_callback); void mainloop(); void stop(); @@ -62,7 +63,7 @@ protected: virtual unsigned long long timer_frequency() = 0; virtual void process_events() = 0; - void key_event(char code); + void key_event(char code) const; private: char* buffer; @@ -71,6 +72,7 @@ private: terml_main_callback main; terml_quit_callback quit; terml_key_callback key; + terml_resize_callback resize; bool should_quit; bool really_should_quit;