|
diff --git a/all.h b/all.h
|
| 1 |
#ifndef ALL_H |
1 |
#ifndef ALL_H |
| 2 |
#define ALL_H |
2 |
#define ALL_H |
| 3 |
|
3 |
|
|
|
4 |
#include <stdio.h> |
|
|
5 |
#include <stdlib.h> |
|
|
6 |
|
|
|
7 |
#define GL_SILENCE_DEPRECATION |
|
|
8 |
#ifdef __APPLE__ |
|
|
9 |
#include <OpenGL/gl.h> |
|
|
10 |
#include <GLUT/glut.h> |
|
|
11 |
#else |
|
|
12 |
#include <GL/gl.h> |
|
|
13 |
#include <GL/glut.h> |
|
|
14 |
#endif |
|
|
15 |
|
|
|
16 |
#ifndef M_PI |
|
|
17 |
#define M_PI 3.14159265358979323846 |
|
|
18 |
#endif |
|
|
19 |
|
| 4 |
typedef struct { |
20 |
typedef struct { |
| 5 |
float x, y, z; |
21 |
float x, y, z; |
| 6 |
} Vec2; |
22 |
} Vec2; |
| ... |
| 21 |
unsigned char *pixels; |
37 |
unsigned char *pixels; |
| 22 |
} Image; |
38 |
} Image; |
| 23 |
|
39 |
|
|
|
40 |
typedef struct { |
|
|
41 |
int target_fps; |
|
|
42 |
int width; |
|
|
43 |
int height; |
|
|
44 |
int last_time; |
|
|
45 |
|
|
|
46 |
float x_offset; |
|
|
47 |
float y_offset; |
|
|
48 |
float cube_angle; |
|
|
49 |
GLuint texture_id; |
|
|
50 |
} Game; |
|
|
51 |
|
|
|
52 |
extern Game game; |
|
|
53 |
|
| 24 |
#define COLOR_RED (Vec3){ .x = 1.0f, .y = 0.0f, .z = 0.0f } |
54 |
#define COLOR_RED (Vec3){ .x = 1.0f, .y = 0.0f, .z = 0.0f } |
| 25 |
#define COLOR_GREEN (Vec3){ .x = 0.0f, .y = 1.0f, .z = 0.0f } |
55 |
#define COLOR_GREEN (Vec3){ .x = 0.0f, .y = 1.0f, .z = 0.0f } |
| 26 |
#define COLOR_BLUE (Vec3){ .x = 0.0f, .y = 0.0f, .z = 1.0f } |
56 |
#define COLOR_BLUE (Vec3){ .x = 0.0f, .y = 0.0f, .z = 1.0f } |
| ... |
| 33 |
#define COLOR_PURPLE (Vec3){ .x = 0.5f, .y = 0.0f, .z = 0.5f } |
63 |
#define COLOR_PURPLE (Vec3){ .x = 0.5f, .y = 0.0f, .z = 0.5f } |
| 34 |
|
64 |
|
| 35 |
Image read_ppm_image(const char *filename); |
65 |
Image read_ppm_image(const char *filename); |
|
|
66 |
|
|
|
67 |
void handle_keyboard_event(unsigned char key, int x, int y); |
|
|
68 |
void handle_mouse_event(int button, int state, int x, int y); |
| 36 |
|
69 |
|
| 37 |
#endif |
70 |
#endif |
|
diff --git a/keyboard.c b/keyboard.c
|
|
|
1 |
#include "all.h" |
|
|
2 |
|
|
|
3 |
void handle_keyboard_event(unsigned char key, int x, int y) { |
|
|
4 |
(void)x; (void)y; |
|
|
5 |
float fraction = 0.05f; |
|
|
6 |
|
|
|
7 |
switch (key) { |
|
|
8 |
case 27: exit(0); break; |
|
|
9 |
case 'w': game.y_offset += fraction; break; |
|
|
10 |
case 's': game.y_offset -= fraction; break; |
|
|
11 |
case 'a': game.x_offset -= fraction; break; |
|
|
12 |
case 'd': game.x_offset += fraction; break; |
|
|
13 |
case '+': |
|
|
14 |
game.target_fps += 5; |
|
|
15 |
printf("Target FPS: %d\n", game.target_fps); |
|
|
16 |
break; |
|
|
17 |
case '-': |
|
|
18 |
if (game.target_fps > 5) game.target_fps -= 5; |
|
|
19 |
printf("Target FPS: %d\n", game.target_fps); |
|
|
20 |
break; |
|
|
21 |
default: |
|
|
22 |
printf("Unknown key: %c\n", key); |
|
|
23 |
break; |
|
|
24 |
} |
|
|
25 |
} |
|
|
26 |
|
|
diff --git a/main.c b/main.c
|
| ... |
| 14 |
#define NONSTD_IMPLEMENTATION |
14 |
#define NONSTD_IMPLEMENTATION |
| 15 |
#include "nonstd.h" |
15 |
#include "nonstd.h" |
| 16 |
|
16 |
|
| 17 |
#define GL_SILENCE_DEPRECATION |
17 |
Game game = {0}; |
| 18 |
|
|
|
| 19 |
#ifndef M_PI |
|
|
| 20 |
#define M_PI 3.14159265358979323846 |
|
|
| 21 |
#endif |
|
|
| 22 |
|
|
|
| 23 |
#ifdef __APPLE__ |
|
|
| 24 |
#include <OpenGL/gl.h> |
|
|
| 25 |
#include <GLUT/glut.h> |
|
|
| 26 |
#else |
|
|
| 27 |
#include <GL/gl.h> |
|
|
| 28 |
#include <GL/glut.h> |
|
|
| 29 |
#endif |
|
|
| 30 |
|
18 |
|
| 31 |
// A packed structure representing a single vertex. |
19 |
// A packed structure representing a single vertex. |
| 32 |
// The GPU needs to know the "stride" (size) of this struct to jump between vertices. |
20 |
// The GPU needs to know the "stride" (size) of this struct to jump between vertices. |
| ... |
| 68 |
v.z*c + cross.z*s + z*dot*(1-c) |
56 |
v.z*c + cross.z*s + z*dot*(1-c) |
| 69 |
}; |
57 |
}; |
| 70 |
} |
58 |
} |
| 71 |
|
|
|
| 72 |
typedef struct { |
|
|
| 73 |
int target_fps; |
|
|
| 74 |
int width; |
|
|
| 75 |
int height; |
|
|
| 76 |
int last_time; |
|
|
| 77 |
|
|
|
| 78 |
float x_offset; |
|
|
| 79 |
float y_offset; |
|
|
| 80 |
float cube_angle; |
|
|
| 81 |
GLuint texture_id; |
|
|
| 82 |
} Game; |
|
|
| 83 |
|
|
|
| 84 |
Game game = {0}; |
|
|
| 85 |
|
59 |
|
| 86 |
void init_texture(void) { |
60 |
void init_texture(void) { |
| 87 |
Image img = read_ppm_image("textures/test.ppm"); |
61 |
Image img = read_ppm_image("textures/test.ppm"); |
| ... |
| 276 |
glOrtho(0.5 - 0.5 * aspect, 0.5 + 0.5 * aspect, 0.0, 1.0, -1.0, 1.0); |
250 |
glOrtho(0.5 - 0.5 * aspect, 0.5 + 0.5 * aspect, 0.0, 1.0, -1.0, 1.0); |
| 277 |
} else { |
251 |
} else { |
| 278 |
glOrtho(0.0, 1.0, 0.5 - 0.5 / aspect, 0.5 + 0.5 / aspect, -1.0, 1.0); |
252 |
glOrtho(0.0, 1.0, 0.5 - 0.5 / aspect, 0.5 + 0.5 / aspect, -1.0, 1.0); |
| 279 |
} |
|
|
| 280 |
} |
|
|
| 281 |
|
|
|
| 282 |
void handle_mouse_event(int button, int state, int x, int y) { |
|
|
| 283 |
switch (button) { |
|
|
| 284 |
case GLUT_LEFT_BUTTON: |
|
|
| 285 |
if (state == GLUT_DOWN) printf("Left button clicked (x: %d, y: %d)\n", x, y); |
|
|
| 286 |
break; |
|
|
| 287 |
case GLUT_MIDDLE_BUTTON: |
|
|
| 288 |
if (state == GLUT_DOWN) printf("Middle button clicked (x: %d, y: %d)\n", x, y); |
|
|
| 289 |
break; |
|
|
| 290 |
case GLUT_RIGHT_BUTTON: |
|
|
| 291 |
if (state == GLUT_DOWN) printf("Right button clicked (x: %d, y: %d)\n", x, y); |
|
|
| 292 |
break; |
|
|
| 293 |
default: break; |
|
|
| 294 |
} |
|
|
| 295 |
} |
|
|
| 296 |
|
|
|
| 297 |
void handle_keyboard_event(unsigned char key, int x, int y) { |
|
|
| 298 |
(void)x; (void)y; |
|
|
| 299 |
float fraction = 0.05f; |
|
|
| 300 |
|
|
|
| 301 |
switch (key) { |
|
|
| 302 |
case 27: exit(0); break; |
|
|
| 303 |
case 'w': game.y_offset += fraction; break; |
|
|
| 304 |
case 's': game.y_offset -= fraction; break; |
|
|
| 305 |
case 'a': game.x_offset -= fraction; break; |
|
|
| 306 |
case 'd': game.x_offset += fraction; break; |
|
|
| 307 |
case '+': |
|
|
| 308 |
game.target_fps += 5; |
|
|
| 309 |
printf("Target FPS: %d\n", game.target_fps); |
|
|
| 310 |
break; |
|
|
| 311 |
case '-': |
|
|
| 312 |
if (game.target_fps > 5) game.target_fps -= 5; |
|
|
| 313 |
printf("Target FPS: %d\n", game.target_fps); |
|
|
| 314 |
break; |
|
|
| 315 |
default: |
|
|
| 316 |
printf("Unknown key: %c\n", key); |
|
|
| 317 |
break; |
|
|
| 318 |
} |
253 |
} |
| 319 |
} |
254 |
} |
| 320 |
|
255 |
|
| ... |
|
diff --git a/mouse.c b/mouse.c
|
|
|
1 |
#include "all.h" |
|
|
2 |
|
|
|
3 |
void handle_mouse_event(int button, int state, int x, int y) { |
|
|
4 |
switch (button) { |
|
|
5 |
case GLUT_LEFT_BUTTON: |
|
|
6 |
if (state == GLUT_DOWN) printf("Left button clicked (x: %d, y: %d)\n", x, y); |
|
|
7 |
break; |
|
|
8 |
case GLUT_MIDDLE_BUTTON: |
|
|
9 |
if (state == GLUT_DOWN) printf("Middle button clicked (x: %d, y: %d)\n", x, y); |
|
|
10 |
break; |
|
|
11 |
case GLUT_RIGHT_BUTTON: |
|
|
12 |
if (state == GLUT_DOWN) printf("Right button clicked (x: %d, y: %d)\n", x, y); |
|
|
13 |
break; |
|
|
14 |
default: break; |
|
|
15 |
} |
|
|
16 |
} |
|
|
17 |
|