-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.c
More file actions
362 lines (280 loc) · 9.19 KB
/
game.c
File metadata and controls
362 lines (280 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "game.h"
#include "darray.h"
#include "event.h"
#include "nude.h"
#include "text.h"
#include "xeno.h"
#include "mesh.h"
#include "camera.h"
#include <float.h>
#include <stdarg.h>
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <vadefs.h>
#define _USE_MATH_DEFINES
#include "mm.h"
#include "input.h"
#include "collision.h"
#include "framegraph.h"
#include "texture.h"
#include <stdlib.h>
#include <stdio.h>
#include "asset_loader.h"
typedef struct {
vec3_t vel;
vec3_t pos;
mesh_t mesh;
} ship_t;
struct {
vec3_t ship_vel;
vec3_t ship_pos;
mesh_t ship_mesh;
mesh_t planet_mesh;
mesh_queue_t* mesh_queue;
} game_state = { 0 };
struct {
vec3_t direction;
float intensity;
} direct_light = {
vec3_down,
0.5
};
static camera_t camera = camera_perspective_default;
static camera_t td_camera = {
{{1, 0, 0, 0}},
{{0, 5, 0}},
{{0, 0, 0}},
90.0f,
0.1f,
100.0f,
2,
1,
{0}, {0}, {0},
90 * DEG2RAD
};
typedef struct {
camera_t* camera;
mat4_t* target;
} camera_follow_t;
static void camera_folow(camera_follow_t* follow) {
}
static camera_t* current_camera = &td_camera;
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 800
#define MAX_MODELS 8
static Game game = {0};
static float ASPECT_RATIO = 1;
static vec2_t WINDOW_ORIGIN = {0};
static float TIME_ON_FRAME = 0;
static float TIME_SCALE = 1.0;
static int mx, my, wx, wy;
static double draw_time, update_time;
static asset_blob_t asset_blob = {0};
static uint8_t* asset_data = NULL;
static size_t asset_data_size = 0;
vec3_t global_mesh_pos = {0};
typedef struct {
vec3_t o;
vec3_t d;
} ray_t;
ray_t ray;
ray_t
ray_from_mouse(int x,
int y,
camera_t* camera) {
ray_t result;
float ndc_x = (2.0f * x) / game.width - 1.0f;
float ndc_y = 1.0f - (2.0f * y) / game.height;
mat4_t view = camera_view(camera);
mat4_t proj = camera_projection(camera, ASPECT_RATIO);
mat4_t inv_proj = m4_inv(proj);
mat4_t inv_view = m4_inv(view);
vec4_t ray_clip = {{ndc_x, ndc_y, -1.0f, 1.0f}};
vec4_t ray_view = m4_mul_v4(inv_proj, ray_clip);
ray_view.z = -1.0f;
ray_view.w = 0.0f;
vec4_t ray_world = m4_mul_v4(inv_view, ray_view);
result.o = camera->position;
result.d = (vec3_t){{ray_world.x, -ray_world.y, ray_world.z}};
result.d = v3_normalized(result.d);
return result;
}
vec3_t
ray_intersect_xz(vec3_t origin,
vec3_t direction) {
float t = -origin.y / direction.y;
vec3_t intersection = v3_add(origin, v3_scale(direction, t));
return intersection;
}
void
assets_load(const char* path) {
asset_data_size = x_file_size(path);
asset_data = x_alloc(asset_data_size, 0);
asset_data = x_file_read(path, asset_data, asset_data_size);
if (!asset_blob_init(&asset_blob, asset_data, (uint32_t)asset_data_size)) {
printf("Failed to initialize asset blob\n");
return;
}
if (!asset_blob_load(&asset_blob)) {
printf("Failed to load asset chunks\n");
return;
}
printf("Successfully loaded %u textures and %u meshes\n",
asset_blob.texture_count, asset_blob.mesh_count);
asset_mesh_t* asset_mesh = asset_get_mesh(&asset_blob, 0);
game_state.ship_mesh = asset_mesh_to_mesh(asset_mesh);
game_state.ship_mesh.transform = mat4_identity;
game_state.ship_mesh.texture = asset_get_texture(&asset_blob, 0);
}
/* ============= INIT =============== */
Game*
g_init(const char* path) {
game.target_fps = 60;
game.width = SCREEN_WIDTH;
game.height = SCREEN_HEIGHT;
ASPECT_RATIO = (float) game.height / game.width;
WINDOW_ORIGIN = (vec2_t) {{ (float) game.width / 2, (float) game.height / 2 }};
float aspect_x = (float)game.width / (float)game.height;
float fov_x = atan(tan(current_camera->fov / 2) * aspect_x) * 2;
init_planes(fov_x, current_camera->fov, current_camera->near, current_camera->far);
framegraph_size_set(SCREEN_WIDTH, SCREEN_HEIGHT);
assets_load(path);
game_state.mesh_queue = nude_mesh_queue_create();
return &game;
}
void
g_term() {
da_destroy(game_state.ship_mesh.vertices);
da_destroy(game_state.ship_mesh.indices);
da_destroy(game_state.ship_mesh.normals);
da_destroy(game_state.ship_mesh.colors);
da_destroy(game_state.ship_mesh.uvs);
da_destroy(game_state.ship_mesh.uv_indices);
if (asset_data) {
x_free(asset_data, 0);
asset_data = NULL;
}
nude_mesh_queue_destroy(game_state.mesh_queue);
}
int activated = 0;
void
g_input(float dt) {
if (input_get_key(KEY_CODE_SHIFT)) {
if (input_get_button_down(BUTTON_MOUSE_LEFT)) {
activated = 1;
input_get_mouse_pos(&mx, &my);
x_window_position_get(&wx, &wy);
}
if (activated && input_get_button(BUTTON_MOUSE_LEFT)) {
printf("SHIFT");
int x, y, dx, dy;
activated = 0;
input_get_mouse_pos(&x, &y);
dx = x - mx;
dy = y - my;
x_window_position_set(wx + dx, wy + dy);
}
} else { activated = 0; }
if (input_get_key_down(KEY_CODE_O)) {
TIME_SCALE *= 0.5;
TIME_SCALE = MAX(0, TIME_SCALE);
x_time_scale_set(TIME_SCALE);
}
if (input_get_key_down(KEY_CODE_P)) {
TIME_SCALE *= 2;
TIME_SCALE = MIN(TIME_SCALE, 100);
x_time_scale_set(TIME_SCALE);
}
if(input_get_key(KEY_CODE_ESC)) {
b_event_dispatch(APP_QUIT);
}
#ifdef ENABLE_SREC
if (TIME_ON_FRAME > 3.0) {
b_event_dispatch(APP_QUIT);
}
#endif
if (input_get_key_down(KEY_CODE_1)) {
n_flag_toggle(DRAW_FLAG_SHADE);
}
if (input_get_key_down(KEY_CODE_2)) {
n_flag_toggle(DRAW_FLAG_WIREFRAME);
}
if (input_get_key_down(KEY_CODE_3)) {
n_flag_toggle(DRAW_FLAG_DOT);
}
if (input_get_key_down(KEY_CODE_4)) {
n_flag_toggle(DRAW_FLAG_CULLING);
}
if (input_get_key_down(KEY_CODE_5)) {
n_flag_toggle(DRAW_FLAG_BACKFACE);
}
if (input_get_key_down(KEY_CODE_6)) {
n_flag_toggle(DRAW_FLAG_NORMALS);
}
if (input_get_key_down(KEY_CODE_7)) {
n_flag_toggle(DRAW_FLAG_FULLRECT);
}
if (input_get_button(BUTTON_MOUSE_RIGHT)) {
int x, y;
input_get_mouse_pos(&x, &y);
ray = ray_from_mouse(x, y, current_camera);
vec3_t intersection = ray_intersect_xz(ray.o, ray.d);
global_mesh_pos = intersection;
}
}
void
draw_buttons() {
if (input_get_key(KEY_CODE_W)) {
n_text_draw(game.color, 100, 100, "W", 0xffffffff, 2, 2);
}
if (input_get_key(KEY_CODE_S)) {
n_text_draw(game.color, 100, 120, "S", 0xffffffff, 2, 2);
}
if (input_get_key(KEY_CODE_A)) {
n_text_draw(game.color, 80, 120, "A", 0xffffffff, 2, 2);
}
if (input_get_key(KEY_CODE_D)) {
n_text_draw(game.color, 120, 120, "D", 0xffffffff, 2, 2);
}
}
void
g_update(float dt) {
double start_frame = x_get_absolute_time();
TIME_ON_FRAME = x_get_time();
g_input(dt);
camera_update(current_camera, dt);
game_state.ship_mesh.transform = mat4_identity;
rotor3_t rot = r3_from_euler(0, 0, 0);
mat4_t rot_mat = r3_rotate(rot);
game_state.ship_mesh.transform = m4_translate(global_mesh_pos.x, global_mesh_pos.y, global_mesh_pos.z);
framegraph_store(0, draw_time, x_time_frame_get());
float frame_ratio = get_frame_avg_ratio();
int pos = last_frame_pos(frame_ratio);
/* END OF THE UPDATE */
update_time = x_get_absolute_time() - start_frame;
n_clear_color_set(0xff000000);
n_size_set(game.width, game.height);
n_clear(game.color, game.depth);
n_grid_dot_draw(game.color, game.width, game.height, 10, TIME_ON_FRAME);
nude_mesh_queue_clear(game_state.mesh_queue);
nude_mesh_queue_add(game_state.mesh_queue, game_state.ship_mesh, camera_view(current_camera), camera_projection(current_camera, ASPECT_RATIO));
nude_render(game_state.mesh_queue, game.color, game.depth, game.width, game.height);
n_ctx_view_set(camera_view(current_camera));
n_ctx_proj_set(camera_projection(current_camera, ASPECT_RATIO));
n_draw_ray(game.color, ray.o, ray.d, 0x00ff00ff);
if (input_get_key(KEY_CODE_SHIFT)) {
/* FRAME STATISTICS */
const char* text = format_text("frame: %.1fms", x_time_frame_get() * 1000);
int tx, ty;
n_text_size(text, 2, 2, &tx, &ty);
n_text_draw(game.color, WINDOW_ORIGIN.x - tx * 0.5, ty, text, 0xffffffff, 2, 2);
framegraph_draw(game.color, 1, 0xff00ff00);
text = format_text("draw: %.4fms", (x_time_frame_get() * frame_ratio) * 1000);
n_text_size(text, 2, 2, &tx, &ty);
n_text_draw(game.color, SCREEN_WIDTH - tx - 10, ty / 2 + pos, text, 0xffffffff, 2, 2);
}
draw_buttons();
/* END OF DRAW */
draw_time = x_get_absolute_time() - start_frame;
}