Compare commits

...

5 Commits

Author SHA1 Message Date
Shylie
e5b017ad97 Don't include GL from GLFW 2024-05-30 12:09:48 -04:00
Shylie
fa8b8bfcf8 Fix unable to find glfw header (for real this time?) 2024-05-30 12:07:49 -04:00
Shylie
ca0addb54c Fix unable to find glfw header 2024-05-30 12:05:52 -04:00
Shylie
93df224dc8 Fetch entire repo history 2024-05-30 12:04:05 -04:00
Shylie
35902c2803 Add key callbacks, fix buffering issue 2024-05-30 10:57:36 -04:00
11 changed files with 80 additions and 15 deletions

View File

@ -17,7 +17,7 @@ jobs:
uses: actions/checkout@v4
with:
path: prev
fetch-depth: 2
fetch-depth: 0
submodules: recursive
lfs: true
- run: cd prev && git checkout 10a90a5f6a

View File

@ -71,7 +71,7 @@ target_link_libraries(glerminal
PRIVATE
glerminallib
lua_static
)
)
if (PROJECT_IS_TOP_LEVEL)
add_subdirectory(examples examples)

View File

@ -42,5 +42,5 @@ namespace
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)
{
glerminal_run(init, mainloop);
glerminal_run(init, mainloop, nullptr, nullptr);
}

View File

@ -83,5 +83,5 @@ namespace
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)
{
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_main_cb)(float dt);
typedef void (*glerminal_keys_cb)(int key);
/**
* @brief Call init once, then run the application's mainloop
* @param init initialization callback
* @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();

View File

@ -1,4 +1,6 @@
#include <glerminal.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
extern "C"
{
@ -131,6 +133,40 @@ namespace
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)
@ -155,7 +191,7 @@ int main(int argc, char** argv)
luaL_dofile(L, "main.lua");
glerminal_run(init, mainloop);
glerminal_run(init, mainloop, pressed, released);
lua_close(L);

View File

@ -25,7 +25,7 @@ namespace glerminal
class glerminal
{
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(const glerminal&) = delete;
@ -84,6 +84,7 @@ namespace glerminal
unsigned int m_sprites[CELL_SIZE * CELL_SIZE * (1 << (8 * sizeof(*m_cells)))];
glerminal_main_cb m_main;
glerminal_keys_cb m_pressed, m_released;
#ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT
mutable std::ofstream m_log;
@ -99,6 +100,8 @@ namespace glerminal
void update_sprites();
void update_layer_colors();
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
{
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_pressed(pressed),
m_released(released),
m_cells{ },
m_offsets{ },
m_sprites{ },
@ -237,8 +239,6 @@ namespace glerminal
GLERMINAL_G = this;
init();
glerminal_flush();
}
glerminal::~glerminal()
@ -261,6 +261,8 @@ namespace glerminal
m_main(current - last);
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);
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)
@ -362,6 +366,7 @@ namespace glerminal
{
glfwInit();
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
@ -381,6 +386,9 @@ namespace glerminal
{
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
@ -767,13 +775,28 @@ namespace glerminal
{
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
{
glerminal::glerminal* g = new glerminal::glerminal(init, main);
glerminal::glerminal* g = new glerminal::glerminal(init, main, pressed, released);
g->run();
delete g;
}

View File

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