diff --git a/Makefile b/Makefile index cc7b8d49d1c729aa4de759b8bb370ab19ec7340a..c31d5f51776b6c9f923a7355ba605f724ebb1836 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CFLAGS := -std=c99 -Wall -Wextra -ggdb LDFLAGS := -lm SYSTEM := $(shell uname -s) -SOURCES := main.c ppm.c +SOURCES := main.c ppm.c keyboard.c mouse.c ifeq ($(SYSTEM), Linux) LDFLAGS += -lGL -lglut diff --git a/all.h b/all.h index 2ae5e7b23038501d1eb1a987189efe268230d008..ddb628655e1c8108c9e4d889670301c40cde7eb6 100644 --- a/all.h +++ b/all.h @@ -1,6 +1,22 @@ #ifndef ALL_H #define ALL_H +#include +#include + +#define GL_SILENCE_DEPRECATION +#ifdef __APPLE__ + #include + #include +#else + #include + #include +#endif + +#ifndef M_PI + #define M_PI 3.14159265358979323846 +#endif + typedef struct { float x, y, z; } Vec2; @@ -21,6 +37,20 @@ int color_space; unsigned char *pixels; } Image; +typedef struct { + int target_fps; + int width; + int height; + int last_time; + + float x_offset; + float y_offset; + float cube_angle; + GLuint texture_id; +} Game; + +extern Game game; + #define COLOR_RED (Vec3){ .x = 1.0f, .y = 0.0f, .z = 0.0f } #define COLOR_GREEN (Vec3){ .x = 0.0f, .y = 1.0f, .z = 0.0f } #define COLOR_BLUE (Vec3){ .x = 0.0f, .y = 0.0f, .z = 1.0f } @@ -33,5 +63,8 @@ #define COLOR_ORANGE (Vec3){ .x = 1.0f, .y = 0.5f, .z = 0.0f } #define COLOR_PURPLE (Vec3){ .x = 0.5f, .y = 0.0f, .z = 0.5f } Image read_ppm_image(const char *filename); + +void handle_keyboard_event(unsigned char key, int x, int y); +void handle_mouse_event(int button, int state, int x, int y); #endif diff --git a/keyboard.c b/keyboard.c new file mode 100644 index 0000000000000000000000000000000000000000..9e9377b399fc0f9be04eec007e5831dae6aa135c --- /dev/null +++ b/keyboard.c @@ -0,0 +1,26 @@ +#include "all.h" + +void handle_keyboard_event(unsigned char key, int x, int y) { + (void)x; (void)y; + float fraction = 0.05f; + + switch (key) { + case 27: exit(0); break; + case 'w': game.y_offset += fraction; break; + case 's': game.y_offset -= fraction; break; + case 'a': game.x_offset -= fraction; break; + case 'd': game.x_offset += fraction; break; + case '+': + game.target_fps += 5; + printf("Target FPS: %d\n", game.target_fps); + break; + case '-': + if (game.target_fps > 5) game.target_fps -= 5; + printf("Target FPS: %d\n", game.target_fps); + break; + default: + printf("Unknown key: %c\n", key); + break; + } +} + diff --git a/main.c b/main.c index 4d070acbbcfbc397563140bd69df13b0eb570f31..c9286229a669d0706cab1077cca552e573831840 100644 --- a/main.c +++ b/main.c @@ -14,19 +14,7 @@ #define NONSTD_IMPLEMENTATION #include "nonstd.h" -#define GL_SILENCE_DEPRECATION - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - -#ifdef __APPLE__ - #include - #include -#else - #include - #include -#endif +Game game = {0}; // A packed structure representing a single vertex. // The GPU needs to know the "stride" (size) of this struct to jump between vertices. @@ -68,20 +56,6 @@ v.y*c + cross.y*s + y*dot*(1-c), v.z*c + cross.z*s + z*dot*(1-c) }; } - -typedef struct { - int target_fps; - int width; - int height; - int last_time; - - float x_offset; - float y_offset; - float cube_angle; - GLuint texture_id; -} Game; - -Game game = {0}; void init_texture(void) { Image img = read_ppm_image("textures/test.ppm"); @@ -276,45 +250,6 @@ if (w >= h) { glOrtho(0.5 - 0.5 * aspect, 0.5 + 0.5 * aspect, 0.0, 1.0, -1.0, 1.0); } else { glOrtho(0.0, 1.0, 0.5 - 0.5 / aspect, 0.5 + 0.5 / aspect, -1.0, 1.0); - } -} - -void handle_mouse_event(int button, int state, int x, int y) { - switch (button) { - case GLUT_LEFT_BUTTON: - if (state == GLUT_DOWN) printf("Left button clicked (x: %d, y: %d)\n", x, y); - break; - case GLUT_MIDDLE_BUTTON: - if (state == GLUT_DOWN) printf("Middle button clicked (x: %d, y: %d)\n", x, y); - break; - case GLUT_RIGHT_BUTTON: - if (state == GLUT_DOWN) printf("Right button clicked (x: %d, y: %d)\n", x, y); - break; - default: break; - } -} - -void handle_keyboard_event(unsigned char key, int x, int y) { - (void)x; (void)y; - float fraction = 0.05f; - - switch (key) { - case 27: exit(0); break; - case 'w': game.y_offset += fraction; break; - case 's': game.y_offset -= fraction; break; - case 'a': game.x_offset -= fraction; break; - case 'd': game.x_offset += fraction; break; - case '+': - game.target_fps += 5; - printf("Target FPS: %d\n", game.target_fps); - break; - case '-': - if (game.target_fps > 5) game.target_fps -= 5; - printf("Target FPS: %d\n", game.target_fps); - break; - default: - printf("Unknown key: %c\n", key); - break; } } diff --git a/mouse.c b/mouse.c new file mode 100644 index 0000000000000000000000000000000000000000..352dfbd1b72897c8bdeef2e3723b9463d5776020 --- /dev/null +++ b/mouse.c @@ -0,0 +1,17 @@ +#include "all.h" + +void handle_mouse_event(int button, int state, int x, int y) { + switch (button) { + case GLUT_LEFT_BUTTON: + if (state == GLUT_DOWN) printf("Left button clicked (x: %d, y: %d)\n", x, y); + break; + case GLUT_MIDDLE_BUTTON: + if (state == GLUT_DOWN) printf("Middle button clicked (x: %d, y: %d)\n", x, y); + break; + case GLUT_RIGHT_BUTTON: + if (state == GLUT_DOWN) printf("Right button clicked (x: %d, y: %d)\n", x, y); + break; + default: break; + } +} +