Add mouse callbacks

This commit is contained in:
Shylie 2024-06-11 00:40:43 -04:00
parent 996d14c93c
commit 45ec13db45
10 changed files with 212 additions and 59 deletions

4
.gitignore vendored
View File

@ -1 +1,3 @@
build/ build/
cmake-build-*/
.idea/

View File

@ -9,9 +9,9 @@ namespace
glerminal_load_sprites_file("resources/atlas.png"); glerminal_load_sprites_file("resources/atlas.png");
} }
void mainloop(float dt) void mainloop(double dt)
{ {
static float time = 1; static double time = 1;
time += dt; time += dt;
@ -42,5 +42,5 @@ namespace
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
glerminal_run(init, mainloop, nullptr, nullptr); glerminal_run({init, mainloop});
} }

View File

@ -18,9 +18,9 @@ namespace
} }
} }
void mainloop(float dt) void mainloop(double dt)
{ {
static float time = 0; static double time = 0;
time += dt; time += dt;
time = fmodf(time, 3.1415926f * 2); time = fmodf(time, 3.1415926f * 2);
@ -39,5 +39,5 @@ namespace
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
glerminal_run(init, mainloop, nullptr, nullptr); glerminal_run({init, mainloop});
} }

View File

@ -43,9 +43,9 @@ namespace
} }
} }
void mainloop(float dt) void mainloop(double dt)
{ {
static float time = 0; static double time = 0;
time += dt; time += dt;
const float cx = GRID_WIDTH / 2.0f * cosf(time / 2) + GRID_WIDTH / 2.0f; const float cx = GRID_WIDTH / 2.0f * cosf(time / 2) + GRID_WIDTH / 2.0f;
@ -80,5 +80,5 @@ namespace
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
glerminal_run(init, mainloop, nullptr, nullptr); glerminal_run({init, mainloop});
} }

View File

@ -34,9 +34,9 @@ namespace
} }
} }
void mainloop(float dt) void mainloop(double dt)
{ {
static float time = 0; static double time = 0;
time += dt; time += dt;
@ -64,5 +64,5 @@ namespace
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
glerminal_run(init, mainloop, nullptr, nullptr); glerminal_run({init, mainloop});
} }

View File

@ -9,17 +9,24 @@ extern "C"
#endif #endif
typedef void (*glerminal_init_cb)(); typedef void (*glerminal_init_cb)();
typedef void (*glerminal_main_cb)(float dt); typedef void (*glerminal_main_cb)(double dt);
typedef void (*glerminal_keys_cb)(int key); typedef void (*glerminal_keys_cb)(int key);
typedef void (*glerminal_mousemoved_cb)(double x, double y);
typedef void (*glerminal_mousepress_cb)(int button, double x, double y);
typedef struct {
glerminal_init_cb init;
glerminal_main_cb main;
glerminal_keys_cb keypress, keyrelease;
glerminal_mousemoved_cb moved;
glerminal_mousepress_cb mousepress, mouserelease;
} glerminal_init_params;
/** /**
* @brief Call init once, then run the application's mainloop * @brief Call init once, then run the application's mainloop
* @param init initialization callback * @param params Initialization parameters
* @param main main calllback
* @param pressed key pressed callback (can be null)
* @param released key released callback (can be null)
*/ */
void glerminal_run(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released); void glerminal_run(glerminal_init_params params);
void glerminal_quit(); void glerminal_quit();
@ -30,39 +37,43 @@ void glerminal_flush();
/** /**
* @brief Set a cell's sprite * @brief Set a cell's sprite
* @param x position of the cell in the range [0, 40) * @param x position of the cell in the range [0, GRID_WIDTH)
* @param y position of the cell in the range [0, 25) * @param y position of the cell in the range [0, GRID_HEIGHT)
* @param layer layer of the cell in the range [0, 8) * @param layer layer of the cell in the range [0, LAYER_COUNT)
* @param sprite sprite's index in the range [0, 256) * @param sprite sprite's index in the range [0, 4096)
*/ */
void glerminal_set(unsigned char x, unsigned char y, unsigned char layer, unsigned short sprite); void glerminal_set(unsigned char x, unsigned char y, unsigned char layer, unsigned short sprite);
/** /**
* @brief Get a cell's sprite * @brief Get a cell's sprite
* @param x position of the cell in the range [0, 40) * @param x position of the cell in the range [0, GRID_WIDTH)
* @param y position of the cell in the range [0, 25) * @param y position of the cell in the range [0, GRID_HEIGHT)
* @param layer layer of the cell in the range [0, 8) * @param layer layer of the cell in the range [0, LAYER_COUNT)
* @return sprite index currently assigned to the cell * @return sprite index currently assigned to the cell
*/ */
unsigned char glerminal_get(unsigned char x, unsigned char y, unsigned short layer); unsigned char glerminal_get(unsigned char x, unsigned char y, unsigned short layer);
/** /**
* @brief Set a cell's offset * @brief Set a cell's offset
* @param x position of the cell in the range [0, 40) * @param x position of the cell in the range [0, GRID_WIDTH)
* @param y position of the cell in the range [0, 25) * @param y position of the cell in the range [0, GRID_HEIGHT)
* @param layer layer of the cell in the range [0, 8) * @param layer layer of the cell in the range [0, LAYER_COUNT)
* @param x_offset offset of the cell on the x axis in the range [-128, 127], where 0 is no offset * @param x_offset offset of the cell on the x axis in cells
* @param y_offset offset of the cell on the y axis in the range [-128, 127], where 0 is no offset * @param y_offset offset of the cell on the y axis in cells
*/ */
void glerminal_offset(unsigned char x, unsigned char y, unsigned char layer, float x_offset, float y_offset); void glerminal_offset(unsigned char x, unsigned char y, unsigned char layer, float x_offset, float y_offset);
/** /**
* @brief Set a layer's color * @brief Set a cell's color
* @param layer The layer to modify * @param x position of the cell in the range [0, GRID_WIDTH)
* @param y position of the cell in the range [0, GRID_HEIGHT)
* @param layer layer of the cell in the range [0, LAYER_COUNT)
* @param color The new color * @param color The new color
*/ */
void glerminal_color(unsigned char x, unsigned char y, unsigned char layer, unsigned int color); void glerminal_color(unsigned char x, unsigned char y, unsigned char layer, unsigned int color);
/** /**
* @brief Set a layer's scale * @brief Set a cell's scale
* @param layer The layer to modify * @param x position of the cell in the range [0, GRID_WIDTH)
* @param y position of the cell in the range [0, GRID_HEIGHT)
* @param layer layer of the cell in the range [0, LAYER_COUNT)
* @param scale The new scale * @param scale The new scale
*/ */
void glerminal_scale(unsigned char x, unsigned char y, unsigned char layer, float scale); void glerminal_scale(unsigned char x, unsigned char y, unsigned char layer, float scale);

View File

@ -26,4 +26,10 @@ function glerminal.main(dt)
end end
glerminal.flush() glerminal.flush()
end end
function glerminal.keypresse(key) end
function glerminal.keyrelease(key) end
function glerminal.mousemove(x, y) end
function glerminal.mousepress(button, x, y) end
function glerminal.mouserelease(button, x, y) end

View File

@ -139,7 +139,7 @@ namespace
lua_remove(L, handler); lua_remove(L, handler);
} }
void mainloop(float dt) void mainloop(double dt)
{ {
lua_pushcfunction(L, message_handler); lua_pushcfunction(L, message_handler);
const int handler = lua_gettop(L); const int handler = lua_gettop(L);
@ -155,7 +155,7 @@ namespace
lua_remove(L, handler); lua_remove(L, handler);
} }
void pressed(int key) void keypressed(int key)
{ {
const char* const name = glfwGetKeyName(key, 0); const char* const name = glfwGetKeyName(key, 0);
if (name) if (name)
@ -163,7 +163,7 @@ namespace
lua_pushcfunction(L, message_handler); lua_pushcfunction(L, message_handler);
const int handler = lua_gettop(L); const int handler = lua_gettop(L);
lua_getglobal(L, "glerminal"); lua_getglobal(L, "glerminal");
lua_getfield(L, -1, "pressed"); lua_getfield(L, -1, "keypress");
lua_remove(L, -2); lua_remove(L, -2);
lua_pushstring(L, name); lua_pushstring(L, name);
if (lua_pcall(L, 1, 0, handler) != LUA_OK) if (lua_pcall(L, 1, 0, handler) != LUA_OK)
@ -175,7 +175,7 @@ namespace
} }
} }
void released(int key) void keyrelease(int key)
{ {
const char* const name = glfwGetKeyName(key, 0); const char* const name = glfwGetKeyName(key, 0);
if (name) if (name)
@ -183,7 +183,7 @@ namespace
lua_pushcfunction(L, message_handler); lua_pushcfunction(L, message_handler);
const int handler = lua_gettop(L); const int handler = lua_gettop(L);
lua_getglobal(L, "glerminal"); lua_getglobal(L, "glerminal");
lua_getfield(L, -1, "released"); lua_getfield(L, -1, "keyrelease");
lua_remove(L, -2); lua_remove(L, -2);
lua_pushstring(L, name); lua_pushstring(L, name);
if (lua_pcall(L, 1, 0, handler) != LUA_OK) if (lua_pcall(L, 1, 0, handler) != LUA_OK)
@ -194,6 +194,97 @@ namespace
lua_remove(L, handler); lua_remove(L, handler);
} }
} }
void mousemove(double x, double y)
{
lua_pushcfunction(L, message_handler);
const int handler = lua_gettop(L);
lua_getglobal(L, "glerminal");
lua_getfield(L, -1, "mousemove");
lua_remove(L, -2);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
if (lua_pcall(L, 2, 0, handler) != LUA_OK)
{
std::cout << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
lua_remove(L, handler);
}
void mousepress(int button, double x, double y)
{
const char* button_name = nullptr;
switch (button)
{
case GLFW_MOUSE_BUTTON_LEFT:
button_name = "left";
break;
case GLFW_MOUSE_BUTTON_RIGHT:
button_name = "right";
break;
case GLFW_MOUSE_BUTTON_MIDDLE:
button_name = "middle";
break;
}
if (button_name)
{
lua_pushcfunction(L, message_handler);
const int handler = lua_gettop(L);
lua_getglobal(L, "glerminal");
lua_getfield(L, -1, "mousepress");
lua_remove(L, -2);
lua_pushstring(L, button_name);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
if (lua_pcall(L, 3, 0, handler) != LUA_OK)
{
std::cout << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
lua_remove(L, handler);
}
}
void mouserelease(int button, double x, double y)
{
const char* button_name = nullptr;
switch (button)
{
case GLFW_MOUSE_BUTTON_LEFT:
button_name = "left";
break;
case GLFW_MOUSE_BUTTON_RIGHT:
button_name = "right";
break;
case GLFW_MOUSE_BUTTON_MIDDLE:
button_name = "middle";
break;
}
if (button_name)
{
lua_pushcfunction(L, message_handler);
const int handler = lua_gettop(L);
lua_getglobal(L, "glerminal");
lua_getfield(L, -1, "mouserelease");
lua_remove(L, -2);
lua_pushstring(L, button_name);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
if (lua_pcall(L, 3, 0, handler) != LUA_OK)
{
std::cout << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
lua_remove(L, handler);
}
}
} }
int main(int argc, char** argv) int main(int argc, char** argv)
@ -232,7 +323,15 @@ int main(int argc, char** argv)
} }
else else
{ {
glerminal_run(init, mainloop, pressed, released); glerminal_init_params params;
params.init = init;
params.main = mainloop;
params.keypress = keypressed;
params.keyrelease = keyrelease;
params.moved = mousemove;
params.mousepress = mousepress;
params.mouserelease = mouserelease;
glerminal_run(params);
} }
lua_close(L); lua_close(L);

View File

@ -27,7 +27,7 @@ namespace glerminal
class glerminal class glerminal
{ {
public: public:
glerminal(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released); explicit glerminal(glerminal_init_params params);
~glerminal(); ~glerminal();
glerminal(const glerminal&) = delete; glerminal(const glerminal&) = delete;
@ -83,7 +83,9 @@ namespace glerminal
unsigned int m_sprites[(CELL_SIZE + 2) * (CELL_SIZE + 2) * MAX_SPRITES]; unsigned int m_sprites[(CELL_SIZE + 2) * (CELL_SIZE + 2) * MAX_SPRITES];
glerminal_main_cb m_main; glerminal_main_cb m_main;
glerminal_keys_cb m_pressed, m_released; glerminal_keys_cb m_keypressed, m_keyreleased;
glerminal_mousemoved_cb m_mousemoved;
glerminal_mousepress_cb m_mousepressed, m_mousereleased;
#ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT #ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT
mutable std::ofstream m_log; mutable std::ofstream m_log;
@ -101,6 +103,8 @@ namespace glerminal
void update_scales(); void update_scales();
static void glfw_key_handler(GLFWwindow* window, int key, int scancode, int action, int mods); static void glfw_key_handler(GLFWwindow* window, int key, int scancode, int action, int mods);
static void glfw_mousemoved_handler(GLFWwindow* window, double x, double y);
static void glfw_mousepress_handler(GLFWwindow* window, int button, int action, int mods);
}; };
} }

View File

@ -189,10 +189,13 @@ namespace
namespace glerminal namespace glerminal
{ {
glerminal::glerminal(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released) : glerminal::glerminal(glerminal_init_params params) :
m_main(main), m_main(params.main),
m_pressed(pressed), m_keypressed(params.keypress),
m_released(released), m_keyreleased(params.keyrelease),
m_mousemoved(params.moved),
m_mousepressed(params.mousepress),
m_mousereleased(params.mouserelease),
m_cells{ }, m_cells{ },
m_offsets{ }, m_offsets{ },
m_sprites{ }, m_sprites{ },
@ -208,7 +211,7 @@ namespace glerminal
} }
// unsure if this should be an error // unsure if this should be an error
if (!init) if (!params.init)
{ {
throw std::runtime_error("No init callback provided."); throw std::runtime_error("No init callback provided.");
} }
@ -229,7 +232,7 @@ namespace glerminal
GLERMINAL_G = this; GLERMINAL_G = this;
init(); params.init();
} }
glerminal::~glerminal() glerminal::~glerminal()
@ -242,12 +245,12 @@ namespace glerminal
void glerminal::run() void glerminal::run()
{ {
float last = glfwGetTime(); double last = glfwGetTime();
while (!glfwWindowShouldClose(m_window)) while (!glfwWindowShouldClose(m_window))
{ {
glfwPollEvents(); glfwPollEvents();
const float current = glfwGetTime(); const double current = glfwGetTime();
m_main(current - last); m_main(current - last);
@ -382,7 +385,9 @@ namespace glerminal
} }
glfwSetWindowUserPointer(m_window, this); glfwSetWindowUserPointer(m_window, this);
glfwSetKeyCallback(m_window, glerminal::glfw_key_handler); glfwSetKeyCallback(m_window, glfw_key_handler);
glfwSetCursorPosCallback(m_window, glfw_mousemoved_handler);
glfwSetMouseButtonCallback(m_window, glfw_mousepress_handler);
} }
void glerminal::log(GLenum type, GLuint id, GLenum severity, const char* message) const void glerminal::log(GLenum type, GLuint id, GLenum severity, const char* message) const
@ -780,23 +785,49 @@ namespace glerminal
{ {
glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window)); glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window));
if (self->m_pressed && action == GLFW_PRESS) if (self->m_keypressed && action == GLFW_PRESS)
{ {
self->m_pressed(key); self->m_keypressed(key);
} }
if (self->m_released && action == GLFW_RELEASE) if (self->m_keyreleased && action == GLFW_RELEASE)
{ {
self->m_released(key); self->m_keyreleased(key);
} }
} }
void glerminal::glfw_mousemoved_handler(GLFWwindow *window, double x, double y)
{
glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window));
if (self->m_mousemoved) { self->m_mousemoved(x / CELL_SIZE, y / CELL_SIZE); }
}
void glerminal::glfw_mousepress_handler(GLFWwindow *window, int button, int action, int mods)
{
glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window));
double x, y;
glfwGetCursorPos(window, &x, &y);
if (self->m_mousepressed && action == GLFW_PRESS)
{
self->m_mousepressed(button, x / CELL_SIZE, y / CELL_SIZE);
}
if (self->m_mousereleased && action == GLFW_RELEASE)
{
self->m_mousereleased(button, x / CELL_SIZE, y / CELL_SIZE);
}
}
} }
void glerminal_run(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released) void glerminal_run(glerminal_init_params params)
{ {
try try
{ {
glerminal::glerminal* g = new glerminal::glerminal(init, main, pressed, released); glerminal::glerminal* g = new glerminal::glerminal(params);
g->run(); g->run();
delete g; delete g;
} }