Compare commits

..

No commits in common. "139681a3fd62122eee99b2a9dec852adac4a032e" and "996d14c93c922fdac7fde29ab4988c17e09421d7" have entirely different histories.

11 changed files with 61 additions and 214 deletions

4
.gitignore vendored
View File

@ -1,3 +1 @@
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(double dt) void mainloop(float dt)
{ {
static double time = 1; static float 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}); glerminal_run(init, mainloop, nullptr, nullptr);
} }

View File

@ -18,9 +18,9 @@ namespace
} }
} }
void mainloop(double dt) void mainloop(float dt)
{ {
static double time = 0; static float 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}); glerminal_run(init, mainloop, nullptr, nullptr);
} }

View File

@ -43,9 +43,9 @@ namespace
} }
} }
void mainloop(double dt) void mainloop(float dt)
{ {
static double time = 0; static float 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}); glerminal_run(init, mainloop, nullptr, nullptr);
} }

View File

@ -34,9 +34,9 @@ namespace
} }
} }
void mainloop(double dt) void mainloop(float dt)
{ {
static double time = 0; static float 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}); glerminal_run(init, mainloop, nullptr, nullptr);
} }

View File

@ -9,24 +9,17 @@ extern "C"
#endif #endif
typedef void (*glerminal_init_cb)(); typedef void (*glerminal_init_cb)();
typedef void (*glerminal_main_cb)(double dt); typedef void (*glerminal_main_cb)(float 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 params Initialization parameters * @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_params params); 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();
@ -37,43 +30,39 @@ 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, GRID_WIDTH) * @param x position of the cell in the range [0, 40)
* @param y position of the cell in the range [0, GRID_HEIGHT) * @param y position of the cell in the range [0, 25)
* @param layer layer of the cell in the range [0, LAYER_COUNT) * @param layer layer of the cell in the range [0, 8)
* @param sprite sprite's index in the range [0, 4096) * @param sprite sprite's index in the range [0, 256)
*/ */
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, GRID_WIDTH) * @param x position of the cell in the range [0, 40)
* @param y position of the cell in the range [0, GRID_HEIGHT) * @param y position of the cell in the range [0, 25)
* @param layer layer of the cell in the range [0, LAYER_COUNT) * @param layer layer of the cell in the range [0, 8)
* @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, GRID_WIDTH) * @param x position of the cell in the range [0, 40)
* @param y position of the cell in the range [0, GRID_HEIGHT) * @param y position of the cell in the range [0, 25)
* @param layer layer of the cell in the range [0, LAYER_COUNT) * @param layer layer of the cell in the range [0, 8)
* @param x_offset offset of the cell on the x axis in cells * @param x_offset offset of the cell on the x axis in the range [-128, 127], where 0 is no offset
* @param y_offset offset of the cell on the y axis in cells * @param y_offset offset of the cell on the y axis in the range [-128, 127], where 0 is no offset
*/ */
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 cell's color * @brief Set a layer's color
* @param x position of the cell in the range [0, GRID_WIDTH) * @param layer The layer to modify
* @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 cell's scale * @brief Set a layer's scale
* @param x position of the cell in the range [0, GRID_WIDTH) * @param layer The layer to modify
* @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,10 +26,4 @@ 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(double dt) void mainloop(float 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 keypressed(int key) void pressed(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, "keypress"); lua_getfield(L, -1, "pressed");
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 keyrelease(int key) void released(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, "keyrelease"); lua_getfield(L, -1, "released");
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,97 +194,6 @@ 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)
@ -323,15 +232,7 @@ int main(int argc, char** argv)
} }
else else
{ {
glerminal_init_params params; glerminal_run(init, mainloop, pressed, released);
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:
explicit glerminal(glerminal_init_params params); 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;
@ -83,9 +83,7 @@ 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_keypressed, m_keyreleased; glerminal_keys_cb m_pressed, m_released;
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;
@ -103,8 +101,6 @@ 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,13 +189,10 @@ namespace
namespace glerminal namespace glerminal
{ {
glerminal::glerminal(glerminal_init_params params) : glerminal::glerminal(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released) :
m_main(params.main), m_main(main),
m_keypressed(params.keypress), m_pressed(pressed),
m_keyreleased(params.keyrelease), m_released(released),
m_mousemoved(params.moved),
m_mousepressed(params.mousepress),
m_mousereleased(params.mouserelease),
m_cells{ }, m_cells{ },
m_offsets{ }, m_offsets{ },
m_sprites{ }, m_sprites{ },
@ -211,7 +208,7 @@ namespace glerminal
} }
// unsure if this should be an error // unsure if this should be an error
if (!params.init) if (!init)
{ {
throw std::runtime_error("No init callback provided."); throw std::runtime_error("No init callback provided.");
} }
@ -232,7 +229,7 @@ namespace glerminal
GLERMINAL_G = this; GLERMINAL_G = this;
params.init(); init();
} }
glerminal::~glerminal() glerminal::~glerminal()
@ -245,12 +242,12 @@ namespace glerminal
void glerminal::run() void glerminal::run()
{ {
double last = glfwGetTime(); float last = glfwGetTime();
while (!glfwWindowShouldClose(m_window)) while (!glfwWindowShouldClose(m_window))
{ {
glfwPollEvents(); glfwPollEvents();
const double current = glfwGetTime(); const float current = glfwGetTime();
m_main(current - last); m_main(current - last);
@ -385,9 +382,7 @@ namespace glerminal
} }
glfwSetWindowUserPointer(m_window, this); glfwSetWindowUserPointer(m_window, this);
glfwSetKeyCallback(m_window, glfw_key_handler); glfwSetKeyCallback(m_window, glerminal::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
@ -785,49 +780,23 @@ namespace glerminal
{ {
glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window)); glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window));
if (self->m_keypressed && action == GLFW_PRESS) if (self->m_pressed && action == GLFW_PRESS)
{ {
self->m_keypressed(key); self->m_pressed(key);
} }
if (self->m_keyreleased && action == GLFW_RELEASE) if (self->m_released && action == GLFW_RELEASE)
{ {
self->m_keyreleased(key); self->m_released(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_params params) 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(params); glerminal::glerminal* g = new glerminal::glerminal(init, main, pressed, released);
g->run(); g->run();
delete g; delete g;
} }

View File

@ -25,12 +25,12 @@ namespace
glerminal_quit(); glerminal_quit();
} }
void mainloop(double) {} void mainloop(float) {}
} }
int main() int main()
{ {
glerminal_run({init, mainloop}); glerminal_run(init, mainloop, nullptr, nullptr);
return 0; return 0;
} }