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 | |
| parent | aa2be58cd30d5b9f1eb274454a9cca7a9739d62d (diff) | |
feat: mouse support
| -rw-r--r-- | game.c | 26 | ||||
| -rw-r--r-- | game.h | 6 | ||||
| -rw-r--r-- | main.c | 43 |
3 files changed, 61 insertions, 14 deletions
@@ -1,16 +1,15 @@ - -#include <stdio.h> #include <glad/glad.h> #include <GLFW/glfw3.h> -#include <math.h> #include "game.h" -void character_callback(GLFWwindow* window, unsigned int codepoint) { - printf("%c\n", codepoint); -} +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define MAX(a,b) (((a)>(b))?(a):(b)) +#define CLAMP(x,a,b) (MIN(MAX(x,a),b)) -void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { +int mouse_x = 0, mouse_y = 0; + +void key_callback(int key, int scancode, int action, int mods) { if (action != GLFW_PRESS) return; switch (key) { case GLFW_KEY_ENTER: @@ -20,6 +19,17 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod } } +void cursor_position_callback(int xpos, int ypos) { + mouse_x = xpos; + mouse_y = ypos; +} + +void mouse_button_callback(int button, int action, int mods) { + if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {} +} + void draw_image(decoded_image img) { - for (int i = 0; i < img.buf_size; i++) img.buf[i] = i; + // for (int i = 0; i < img.buf_size; i++) img.buf[i] = i; + img.buf[CLAMP(mouse_y, 0, img.height) * img.width + + CLAMP(mouse_x, 0, img.width)] = -1; } @@ -2,9 +2,15 @@ #include <stddef.h> #include <stdint.h> +#ifndef GAME_H +#define GAME_H + typedef struct decoded_image { size_t width; size_t height; uint32_t *buf; size_t buf_size; } decoded_image; + +#endif // GAME_H + @@ -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"); |
