Added resize callback, made main callback optional

This commit is contained in:
Shylie 2023-12-25 17:38:44 -05:00
parent 226adeaf72
commit 2cb9c0273f
3 changed files with 37 additions and 6 deletions

View File

@ -6,9 +6,15 @@ extern "C"
{ {
#endif #endif
typedef void (*terml_main_callback)(); typedef void (*terml_main_callback) ();
typedef int (*terml_quit_callback)(); typedef int (*terml_quit_callback) ();
typedef void (*terml_key_callback) (char code); 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_init();
int terml_deinit(); int terml_deinit();
@ -23,6 +29,7 @@ void terml_flush();
void terml_set_main_callback(terml_main_callback); void terml_set_main_callback(terml_main_callback);
void terml_set_quit_callback(terml_quit_callback); void terml_set_quit_callback(terml_quit_callback);
void terml_set_key_callback(terml_key_callback); void terml_set_key_callback(terml_key_callback);
void terml_set_resize_callback(terml_resize_callback);
void terml_start(); void terml_start();
void terml_stop(); void terml_stop();

View File

@ -13,6 +13,7 @@ terml::terml() :
main(nullptr), main(nullptr),
quit(nullptr), quit(nullptr),
key(nullptr), key(nullptr),
resize(nullptr),
buffer(nullptr), buffer(nullptr),
width(0), width(0),
height(0) height(0)
@ -136,7 +137,12 @@ void terml::set_key_callback(terml_key_callback callback)
key = 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) if (key)
{ {
@ -158,7 +164,10 @@ void terml::mainloop()
while (current_time >= last_time + wait_time) while (current_time >= last_time + wait_time)
{ {
main(); if (main)
{
main();
}
process_events(); process_events();
@ -215,6 +224,9 @@ void terml::setup_buffer()
if (width != new_width || height != new_height) if (width != new_width || height != new_height)
{ {
const unsigned int old_width = width;
const unsigned int old_height = height;
width = new_width; width = new_width;
height = new_height; height = new_height;
@ -229,6 +241,11 @@ void terml::setup_buffer()
} }
buffer[CELL_SIZE * width * height] = '\0'; 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); TERML_G->set_key_callback(callback);
} }
void terml_set_resize_callback(terml_resize_callback callback)
{
TERML_G->set_resize_callback(callback);
}
void terml_start() void terml_start()
{ {
try try

View File

@ -44,6 +44,7 @@ public:
void set_main_callback(terml_main_callback); void set_main_callback(terml_main_callback);
void set_quit_callback(terml_quit_callback); void set_quit_callback(terml_quit_callback);
void set_key_callback(terml_key_callback); void set_key_callback(terml_key_callback);
void set_resize_callback(terml_resize_callback);
void mainloop(); void mainloop();
void stop(); void stop();
@ -62,7 +63,7 @@ protected:
virtual unsigned long long timer_frequency() = 0; virtual unsigned long long timer_frequency() = 0;
virtual void process_events() = 0; virtual void process_events() = 0;
void key_event(char code); void key_event(char code) const;
private: private:
char* buffer; char* buffer;
@ -71,6 +72,7 @@ private:
terml_main_callback main; terml_main_callback main;
terml_quit_callback quit; terml_quit_callback quit;
terml_key_callback key; terml_key_callback key;
terml_resize_callback resize;
bool should_quit; bool should_quit;
bool really_should_quit; bool really_should_quit;