From bc34748107f1ed8608f5a665f34577764cc65bbb Mon Sep 17 00:00:00 2001 From: orangerot Date: Tue, 14 Oct 2025 04:11:36 +0200 Subject: feat: mouse support --- main.c | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index d092082..78af96d 100644 --- a/main.c +++ b/main.c @@ -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"); -- cgit v1.2.3