Refactor terml_get and terml_set

This commit is contained in:
Shylie 2024-05-03 10:15:35 -04:00
parent 98097d7540
commit 1712c1b1e8
3 changed files with 18 additions and 23 deletions

View File

@ -14,12 +14,12 @@ typedef void (*terml_resize_callback)(
unsigned int previous_height unsigned int previous_height
); );
struct tcell typedef struct
{ {
unsigned int codepoint; unsigned int codepoint;
int foreground; int foreground;
int background; int background;
}; } tcell;
int terml_init(); int terml_init();
int terml_deinit(); int terml_deinit();
@ -27,9 +27,9 @@ int terml_deinit();
unsigned int terml_get_width(); unsigned int terml_get_width();
unsigned int terml_get_height(); unsigned int terml_get_height();
int terml_get(unsigned int x, unsigned int y, tcell* cell); tcell terml_get(unsigned int x, unsigned int y);
int terml_set(unsigned int x, unsigned int y, tcell cell); void terml_set(unsigned int x, unsigned int y, tcell cell);
void terml_flush(); 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);

View File

@ -118,7 +118,7 @@ terml::~terml()
if (cells) { delete[] cells; } if (cells) { delete[] cells; }
} }
const tcell& terml::get(unsigned int x, unsigned int y) const tcell terml::get(unsigned int x, unsigned int y) const
{ {
return cells[x + y * width].cell; return cells[x + y * width].cell;
} }
@ -295,8 +295,11 @@ void terml::reset_console_settings()
reset_console_settings_impl(); reset_console_settings_impl();
} }
static terml* TERML_G; namespace
static const char* LAST_ERROR = nullptr; {
terml* TERML_G;
const char* LAST_ERROR = nullptr;
}
extern "C" extern "C"
{ {
@ -358,36 +361,28 @@ extern "C"
return TERML_G->get_height(); return TERML_G->get_height();
} }
int terml_get(unsigned int x, unsigned int y, tcell* cell) tcell terml_get(unsigned int x, unsigned int y)
{ {
if (x >= TERML_G->get_width() || y >= TERML_G->get_height()) if (x >= TERML_G->get_width() || y >= TERML_G->get_height())
{ {
LAST_ERROR = "Coordinates out of bounds."; LAST_ERROR = "Coordinates out of bounds.";
return 1; return { 0, 0, 0 };
}
else if (!cell)
{
LAST_ERROR = "Null output pointer.";
return 1;
} }
else else
{ {
*cell = TERML_G->get(x, y); return TERML_G->get(x, y);
return 0;
} }
} }
int terml_set(unsigned int x, unsigned int y, tcell cell) void terml_set(unsigned int x, unsigned int y, tcell cell)
{ {
if (x >= TERML_G->get_width() || y >= TERML_G->get_height()) if (x >= TERML_G->get_width() || y >= TERML_G->get_height())
{ {
LAST_ERROR = "Coordinates out of bounds."; LAST_ERROR = "Coordinates out of bounds.";
return 1;
} }
else else
{ {
TERML_G->set(x, y, cell); TERML_G->set(x, y, cell);
return 0;
} }
} }

View File

@ -44,9 +44,9 @@ public:
terml& operator=(const terml&) = delete; terml& operator=(const terml&) = delete;
terml& operator=(terml&&) = delete; terml& operator=(terml&&) = delete;
const tcell& get(unsigned int x, unsigned int y) const; tcell get(unsigned int x, unsigned int y) const;
void set(unsigned int x, unsigned int y, tcell cell); void set(unsigned int x, unsigned int y, tcell cell);
void flush() const; void flush() const;
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);