feat: mouse support
This commit is contained in:
parent
aa2be58cd3
commit
bc34748107
28
game.c
28
game.c
|
@ -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 draw_image(decoded_image img) {
|
||||
for (int i = 0; i < img.buf_size; i++) img.buf[i] = i;
|
||||
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;
|
||||
img.buf[CLAMP(mouse_y, 0, img.height) * img.width +
|
||||
CLAMP(mouse_x, 0, img.width)] = -1;
|
||||
}
|
||||
|
|
6
game.h
6
game.h
|
@ -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
|
||||
|
||||
|
|
43
main.c
43
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;
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
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() {
|
||||
|
||||
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");
|
||||
|
|
Loading…
Reference in a new issue