diff options
| author | orangerot <orangerot@orangerot.dev> | 2025-10-14 04:11:36 +0200 |
|---|---|---|
| committer | orangerot <orangerot@orangerot.dev> | 2025-10-14 04:11:36 +0200 |
| commit | bc34748107f1ed8608f5a665f34577764cc65bbb (patch) | |
| tree | bee688a2c1d0f97591d28671c60f65760c939024 /main.c | |
| parent | aa2be58cd30d5b9f1eb274454a9cca7a9739d62d (diff) | |
feat: mouse support
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 43 |
1 files changed, 37 insertions, 6 deletions
@@ -11,8 +11,9 @@ unsigned int SCR_WIDTH = 800; unsigned int SCR_HEIGHT = 600; -extern void character_callback(GLFWwindow* window, unsigned int codepoint); -extern void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); +extern void key_callback(int key, int scancode, int action, int mods); +extern void cursor_position_callback(int xpos, int ypos); +extern void mouse_button_callback(int button, int action, int mods); extern void draw_image(decoded_image img); struct decoded_image canvas = { @@ -53,14 +54,43 @@ const char *fragment_shader_source = void framebuffer_size_callback(GLFWwindow* window, int width, int height) { + (void) window; + glViewport(0, 0, width, height); SCR_WIDTH = width; SCR_HEIGHT = height; } -const uint32_t COLOR_RGBA = 0xFF21FF00; +void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { + (void) window; + + key_callback(key, scancode, action, mods); +} + +void glfw_cursor_position_callback(GLFWwindow* window, double xpos, double ypos) { + (void) window; + printf("%f %f\n", xpos, ypos); + + float scale_x = fmin( + (float) SCR_HEIGHT / SCR_WIDTH * (float) canvas.height / canvas.width, + 1.0 + ); + float scale_y = fmin((float) SCR_WIDTH / SCR_HEIGHT * (float) canvas.width / canvas.height, + 1.0 + ); + cursor_position_callback( + ((xpos - (SCR_WIDTH - scale_x * SCR_WIDTH )/2) / (scale_x * SCR_WIDTH)) * canvas.width, + ((ypos - (SCR_HEIGHT - scale_y * SCR_HEIGHT)/2) / (scale_y * SCR_HEIGHT)) * canvas.height + ); +} + + +void glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int mods) { + (void) window; + mouse_button_callback(button, action, mods); +} -int main(int argc, const char *argv[]) { +int main() { canvas.buf = malloc(canvas.buf_size * sizeof(int)); @@ -81,8 +111,9 @@ int main(int argc, const char *argv[]) { } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); - glfwSetCharCallback(window, character_callback); - glfwSetKeyCallback(window, key_callback); + glfwSetKeyCallback(window, glfw_key_callback); + glfwSetCursorPosCallback(window, glfw_cursor_position_callback); + glfwSetMouseButtonCallback(window, glfw_mouse_button_callback); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { printf("Failed to initialize GLAD\n"); |
