Add key callbacks, fix buffering issue

This commit is contained in:
Shylie 2024-05-30 10:57:36 -04:00
parent 4b740e3edd
commit 35902c2803
9 changed files with 77 additions and 13 deletions

View File

@ -42,5 +42,5 @@ namespace
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
glerminal_run(init, mainloop); glerminal_run(init, mainloop, nullptr, nullptr);
} }

View File

@ -39,5 +39,5 @@ namespace
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
glerminal_run(init, mainloop); glerminal_run(init, mainloop, nullptr, nullptr);
} }

View File

@ -83,5 +83,5 @@ namespace
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
glerminal_run(init, mainloop); glerminal_run(init, mainloop, nullptr, nullptr);
} }

View File

@ -63,5 +63,5 @@ namespace
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
glerminal_run(init, mainloop); glerminal_run(init, mainloop, nullptr, nullptr);
} }

View File

@ -10,13 +10,16 @@ extern "C"
typedef void (*glerminal_init_cb)(); typedef void (*glerminal_init_cb)();
typedef void (*glerminal_main_cb)(float dt); typedef void (*glerminal_main_cb)(float dt);
typedef void (*glerminal_keys_cb)(int key);
/** /**
* @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 init initialization callback
* @param main main calllback * @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); void glerminal_run(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released);
void glerminal_quit(); void glerminal_quit();

View File

@ -1,4 +1,5 @@
#include <glerminal.h> #include <glerminal.h>
#include <glfw/glfw3.h>
extern "C" extern "C"
{ {
@ -131,6 +132,40 @@ namespace
lua_remove(L, -1); lua_remove(L, -1);
} }
} }
void pressed(int key)
{
const char* const name = glfwGetKeyName(key, 0);
if (name)
{
lua_getglobal(L, "glerminal");
lua_getfield(L, -1, "pressed");
lua_remove(L, -2);
lua_pushstring(L, name);
if (lua_pcall(L, 1, 0, 0) != LUA_OK)
{
// ignore error for now
lua_remove(L, -1);
}
}
}
void released(int key)
{
const char* const name = glfwGetKeyName(key, 0);
if (name)
{
lua_getglobal(L, "glerminal");
lua_getfield(L, -1, "released");
lua_remove(L, -2);
lua_pushstring(L, name);
if (lua_pcall(L, 1, 0, 0) != LUA_OK)
{
// ignore error for now
lua_remove(L, -1);
}
}
}
} }
int main(int argc, char** argv) int main(int argc, char** argv)
@ -155,7 +190,7 @@ int main(int argc, char** argv)
luaL_dofile(L, "main.lua"); luaL_dofile(L, "main.lua");
glerminal_run(init, mainloop); glerminal_run(init, mainloop, pressed, released);
lua_close(L); lua_close(L);

View File

@ -25,7 +25,7 @@ namespace glerminal
class glerminal class glerminal
{ {
public: public:
glerminal(glerminal_init_cb init, glerminal_main_cb main); glerminal(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released);
~glerminal(); ~glerminal();
glerminal(const glerminal&) = delete; glerminal(const glerminal&) = delete;
@ -84,6 +84,7 @@ namespace glerminal
unsigned int m_sprites[CELL_SIZE * CELL_SIZE * (1 << (8 * sizeof(*m_cells)))]; unsigned int m_sprites[CELL_SIZE * CELL_SIZE * (1 << (8 * sizeof(*m_cells)))];
glerminal_main_cb m_main; glerminal_main_cb m_main;
glerminal_keys_cb m_pressed, m_released;
#ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT #ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT
mutable std::ofstream m_log; mutable std::ofstream m_log;
@ -99,6 +100,8 @@ namespace glerminal
void update_sprites(); void update_sprites();
void update_layer_colors(); void update_layer_colors();
void update_layer_scales(); void update_layer_scales();
static void glfw_key_handler(GLFWwindow* window, int key, int scancode, int action, int mods);
}; };
} }

View File

@ -198,8 +198,10 @@ namespace
namespace glerminal namespace glerminal
{ {
glerminal::glerminal(glerminal_init_cb init, glerminal_main_cb main) : glerminal::glerminal(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released) :
m_main(main), m_main(main),
m_pressed(pressed),
m_released(released),
m_cells{ }, m_cells{ },
m_offsets{ }, m_offsets{ },
m_sprites{ }, m_sprites{ },
@ -237,8 +239,6 @@ namespace glerminal
GLERMINAL_G = this; GLERMINAL_G = this;
init(); init();
glerminal_flush();
} }
glerminal::~glerminal() glerminal::~glerminal()
@ -261,6 +261,8 @@ namespace glerminal
m_main(current - last); m_main(current - last);
last = current; last = current;
glfwSwapBuffers(m_window);
} }
} }
@ -292,6 +294,8 @@ namespace glerminal
glBlitNamedFramebuffer(m_screen_framebuffer, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_COLOR_BUFFER_BIT, GL_NEAREST); glBlitNamedFramebuffer(m_screen_framebuffer, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glfwSwapBuffers(m_window); glfwSwapBuffers(m_window);
glBlitNamedFramebuffer(m_screen_framebuffer, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_COLOR_BUFFER_BIT, GL_NEAREST);
} }
void glerminal::set(unsigned char x, unsigned char y, unsigned char layer, unsigned char sprite) void glerminal::set(unsigned char x, unsigned char y, unsigned char layer, unsigned char sprite)
@ -362,6 +366,7 @@ namespace glerminal
{ {
glfwInit(); glfwInit();
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
@ -381,6 +386,9 @@ namespace glerminal
{ {
throw std::runtime_error("Failed to create glerminal window."); throw std::runtime_error("Failed to create glerminal window.");
} }
glfwSetWindowUserPointer(m_window, this);
glfwSetKeyCallback(m_window, glerminal::glfw_key_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
@ -767,13 +775,28 @@ namespace glerminal
{ {
glNamedBufferData(m_layer_scales_buffer, sizeof(m_layer_scales), m_layer_scales, GL_DYNAMIC_READ); glNamedBufferData(m_layer_scales_buffer, sizeof(m_layer_scales), m_layer_scales, GL_DYNAMIC_READ);
} }
void glerminal::glfw_key_handler(GLFWwindow* window, int key, int scancode, int action, int mods)
{
glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window));
if (self->m_pressed && action == GLFW_PRESS)
{
self->m_pressed(key);
}
if (self->m_released && action == GLFW_RELEASE)
{
self->m_released(key);
}
}
} }
void glerminal_run(glerminal_init_cb init, glerminal_main_cb main) void glerminal_run(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released)
{ {
try try
{ {
glerminal::glerminal* g = new glerminal::glerminal(init, main); glerminal::glerminal* g = new glerminal::glerminal(init, main, pressed, released);
g->run(); g->run();
delete g; delete g;
} }

View File

@ -30,7 +30,7 @@ namespace
int main() int main()
{ {
glerminal_run(init, mainloop); glerminal_run(init, mainloop, nullptr, nullptr);
return 0; return 0;
} }