e967bcf9bb3c8acf3cf85c714102434663111cd5
[carveJwlIkooP6JGAAIwe30JlM.git] / main.c
1 /*
2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
3 *
4 * module.h structure definitions
5 * module_submodule.h implementation
6 * module_submodule_2.h
7 */
8
9 #include "common.h"
10
11 /* Resources */
12 vg_tex2d tex_norwey = { .path = "textures/norway_foliage.qoi" };
13 vg_tex2d tex_grid = { .path = "textures/grid.qoi" };
14 vg_tex2d tex_sky = { .path = "textures/sky.qoi" };
15 vg_tex2d tex_gradients = { .path = "textures/gradients.qoi",
16 .flags = VG_TEXTURE_CLAMP };
17 vg_tex2d tex_cement = { .path = "textures/cement512.qoi" };
18 vg_tex2d tex_water = { .path = "textures/water.qoi" };
19
20 /* Convars */
21 static int debugview = 0;
22 static int sv_debugcam = 0;
23 static int lightedit = 0;
24 static int sv_scene = 0;
25 static int cl_ui = 1;
26
27 /* Components */
28 #define SR_NETWORKED
29
30 /* uncomment this to run the game without any graphics being drawn */
31 //#define SR_NETWORK_TEST
32
33 #include "steam.h"
34 #include "network.h"
35
36 #include "model.h"
37 //#include "road.h"
38 #include "scene.h"
39 //#include "ik.h"
40 #include "audio.h"
41 //#include "terrain.h"
42 //#include "character.h"
43 #include "ragdoll.h"
44 #include "rigidbody.h"
45 #include "render.h"
46 #include "world.h"
47 #include "player.h"
48
49 #include "shaders/blit.h"
50 #include "shaders/standard.h"
51 #include "shaders/unlit.h"
52
53 #include "physics_test.h"
54 #include "anim_test.h"
55
56 #include "gate.h"
57
58 void vg_register(void)
59 {
60 shader_blit_register();
61 shader_standard_register();
62 shader_vblend_register();
63 shader_unlit_register();
64
65 player_register();
66 world_register();
67 gate_register();
68 }
69
70 static void init_other(void)
71 {
72 player_init();
73 render_init();
74 gate_init();
75 world_init();
76 audio_init();
77 }
78
79 vg_tex2d *texture_list[] =
80 {
81 &tex_norwey,
82 &tex_gradients,
83 &tex_grid,
84 &tex_sky,
85 &tex_cement,
86 &tex_water,
87 &tex_water_surf
88 };
89
90 int main( int argc, char *argv[] )
91 {
92 highscores_init( 2000, 50 );
93 vg_init( argc, argv, "Voyager Game Engine" );
94 }
95
96 static int playermodel( int argc, char const *argv[] )
97 {
98 if( argc < 1 ) return 0;
99
100 glmesh old_mesh = player.mdl.mesh;
101
102 if( player_load_model( argv[0] ) )
103 mesh_free( &old_mesh );
104
105 return 1;
106 }
107
108 void vg_start(void)
109 {
110 steam_init();
111
112 vg_convar_push( (struct vg_convar){
113 .name = "fc",
114 .data = &freecam,
115 .data_type = k_convar_dtype_i32,
116 .opt_i32 = { .min=0, .max=1, .clamp=1 },
117 .persistent = 1
118 });
119
120 vg_convar_push( (struct vg_convar){
121 .name = "grid",
122 .data = &walk_grid_iterations,
123 .data_type = k_convar_dtype_i32,
124 .opt_i32 = { .min=0, .max=1, .clamp=0 },
125 .persistent = 1
126 });
127
128 vg_convar_push( (struct vg_convar){
129 .name = "fcs",
130 .data = &fc_speed,
131 .data_type = k_convar_dtype_f32,
132 .opt_f32 = { .clamp = 0 },
133 .persistent = 1
134 });
135
136 vg_convar_push( (struct vg_convar){
137 .name = "ledit",
138 .data = &lightedit,
139 .data_type = k_convar_dtype_i32,
140 .opt_i32 = { .min=0, .max=1, .clamp=1 },
141 .persistent = 1
142 });
143
144 vg_convar_push( (struct vg_convar){
145 .name = "cl_ui",
146 .data = &cl_ui,
147 .data_type = k_convar_dtype_i32,
148 .opt_i32 = { .min=0, .max=1, .clamp=1 },
149 .persistent = 1
150 });
151
152 vg_convar_push( (struct vg_convar){
153 .name = "walk_speed",
154 .data = &k_walkspeed,
155 .data_type = k_convar_dtype_f32,
156 .opt_f32 = { .clamp = 0 },
157 .persistent = 1
158 });
159
160 vg_convar_push( (struct vg_convar){
161 .name = "run_speed",
162 .data = &k_runspeed,
163 .data_type = k_convar_dtype_f32,
164 .opt_f32 = { .clamp = 0 },
165 .persistent = 1
166 });
167
168 vg_convar_push( (struct vg_convar){
169 .name = "walk_accel",
170 .data = &k_walk_accel,
171 .data_type = k_convar_dtype_f32,
172 .opt_f32 = { .clamp = 0 },
173 .persistent = 1
174 });
175
176 vg_convar_push( (struct vg_convar){
177 .name = "rd_floaty",
178 .data = &k_ragdoll_floatyiness,
179 .data_type = k_convar_dtype_f32,
180 .opt_f32 = { .clamp = 0 },
181 .persistent = 1
182 });
183
184 vg_convar_push( (struct vg_convar){
185 .name = "rd_floatd",
186 .data = &k_ragdoll_floatydrag,
187 .data_type = k_convar_dtype_f32,
188 .opt_f32 = { .clamp = 0 },
189 .persistent = 1
190 });
191
192 vg_convar_push( (struct vg_convar){
193 .name = "dt",
194 .data = &ktimestep,
195 .data_type = k_convar_dtype_f32,
196 .opt_f32 = { .clamp = 0 },
197 .persistent = 0
198 });
199
200 vg_convar_push( (struct vg_convar){
201 .name = "debugcam",
202 .data = &sv_debugcam,
203 .data_type = k_convar_dtype_i32,
204 .opt_i32 = { .min=0, .max=1, .clamp=0 },
205 .persistent = 1
206 });
207
208 vg_convar_push( (struct vg_convar){
209 .name = "debugview",
210 .data = &debugview,
211 .data_type = k_convar_dtype_i32,
212 .opt_i32 = { .min=0, .max=1, .clamp=0 },
213 .persistent = 1
214 });
215
216 vg_function_push( (struct vg_cmd){
217 .name = "reset",
218 .function = reset_player
219 });
220
221 vg_tex2d_init( texture_list, vg_list_size( texture_list ) );
222
223 init_other();
224
225 /*
226 * If we're in physics test mode we dont need to load anything else, this
227 * parameter is dev only. TODO: dev only cvars that don't ship with the game
228 * when building in release mode.
229 */
230
231 if( sv_scene == 0 )
232 {
233 player_load_model( "ch_new" );
234
235 world_load();
236
237 reset_player( 1, (const char *[]){ "start" } );
238 rb_init( &player.phys.rb );
239
240 network_init();
241 }
242 else if( sv_scene == 1 )
243 {
244 physics_test_start();
245 }
246 else if( sv_scene == 2 )
247 {
248 anim_test_start();
249 }
250 }
251
252 void vg_free(void)
253 {
254 network_end();
255 vg_tex2d_free( texture_list, vg_list_size(texture_list) );
256 /* TODO: THE REST OF THE GOD DAMN FREEING STUFF */
257 steam_end();
258
259 highscores_serialize_all();
260 highscores_free();
261 }
262
263 void vg_update(void)
264 {
265 steam_update();
266
267 if( sv_scene == 0 )
268 {
269 network_update();
270 player_update();
271 world_update( player.phys.rb.co );
272 //traffic_visualize( world.traffic, world.traffic_count );
273 //
274 /* TEMP */
275 if( glfwGetKey( vg_window, GLFW_KEY_J ))
276 {
277 v3_copy( player.camera_pos, world.mr_ball.co );
278 }
279 }
280 else if( sv_scene == 1 )
281 {
282 physics_test_update();
283 }
284 else if( sv_scene == 2 )
285 {
286 anim_test_update();
287 }
288 }
289
290 static void vg_framebuffer_resize( int w, int h )
291 {
292 render_fb_resize();
293 gate_fb_resize();
294 water_fb_resize();
295 }
296
297 static void draw_origin_axis(void)
298 {
299 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 1.0f, 0.0f, 0.0f }, 0xffff0000 );
300 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 1.0f, 0.0f }, 0xff00ff00 );
301 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 0.0f, 1.0f }, 0xff0000ff );
302 }
303
304 static void render_main_game(void)
305 {
306 float speed = freecam? 0.0f: v3_length( player.phys.rb.v );
307 v3f shake = { vg_randf()-0.5f, vg_randf()-0.5f, vg_randf()-0.5f };
308 v3_muls( shake, speed*0.01f, shake );
309
310 m4x4f world_4x4;
311 m4x3_expand( player.camera_inverse, world_4x4 );
312
313 gpipeline.fov = freecam? 60.0f: 125.0f; /* 120 */
314 m4x4_projection( vg_pv, gpipeline.fov,
315 (float)vg_window_x / (float)vg_window_y,
316 0.02f, 2100.0f );
317
318 m4x4_mul( vg_pv, world_4x4, vg_pv );
319
320 glEnable( GL_DEPTH_TEST );
321
322 /*
323 * Draw world
324 */
325
326 int draw_solid = player.is_dead | freecam;
327
328 render_world( vg_pv, player.camera );
329 if( draw_solid )
330 draw_player();
331 render_water_texture( player.camera );
332
333 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
334 render_water_surface( vg_pv, player.camera );
335
336 vg_tex2d_bind( &tex_water, 1 ); /*TODO: ?*/
337 render_world_gates( vg_pv, player.phys.rb.co, player.camera );
338
339 /* Copy the RGB of what we have into the background buffer */
340 glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
341 glBindFramebuffer( GL_DRAW_FRAMEBUFFER, gpipeline.fb_background );
342 glBlitFramebuffer( 0,0, vg_window_x, vg_window_y,
343 0,0, vg_window_x, vg_window_y,
344 GL_COLOR_BUFFER_BIT,
345 GL_LINEAR );
346
347 /* Clear out the colour buffer, but keep depth */
348 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
349 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
350
351 if( !player.is_dead )
352 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
353 else
354 glClear( GL_COLOR_BUFFER_BIT );
355
356 if( !draw_solid )
357 {
358 m4x4_projection( vg_pv, gpipeline.fov,
359 (float)vg_window_x / (float)vg_window_y,
360 0.01f, 600.0f );
361 m4x4_mul( vg_pv, world_4x4, vg_pv );
362 draw_player();
363 }
364
365 /* Draw back in the background
366 *
367 * TODO: need to disable alpha write in the terrain shader so this works
368 * again.
369 */
370 glEnable(GL_BLEND);
371 glDisable(GL_DEPTH_TEST);
372 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
373 glBlendEquation(GL_FUNC_ADD);
374
375 shader_blit_use();
376 shader_blit_uTexMain( 0 );
377 glActiveTexture(GL_TEXTURE0);
378 glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background );
379
380 render_fsquad();
381 glDisable(GL_BLEND);
382
383 /* Other shite */
384 glDisable( GL_DEPTH_TEST );
385 vg_lines_drawall( (float *)vg_pv );
386 glViewport( 0,0, vg_window_x, vg_window_y );
387 }
388
389 void vg_render(void)
390 {
391 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
392 glViewport( 0,0, vg_window_x, vg_window_y );
393
394 glDisable( GL_DEPTH_TEST );
395 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
396 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
397
398 #ifndef SR_NETWORK_TEST
399 draw_origin_axis();
400
401 if( sv_scene == 0 )
402 {
403 render_main_game();
404 }
405 else if( sv_scene == 1 )
406 {
407 physics_test_render();
408 }
409 else if( sv_scene == 2 )
410 {
411 anim_test_render( &tex_characters );
412 }
413 #endif
414 }
415
416 static void run_light_widget( struct light_widget *lw )
417 {
418 struct ui_checkbox c1 = { .data=&lw->enabled };
419
420 ui_checkbox( &ui_global_ctx, &c1 );
421
422 if( lw->enabled )
423 {
424 struct ui_slider_vector
425 colour = { .min=0.0f, .max=2.0f, .len=3, .data=lw->colour },
426 dir = { .min=-VG_PIf, .max=VG_PIf, .len=2, .data=lw->dir };
427
428 ui_slider_vector( &ui_global_ctx, &colour );
429 ui_global_ctx.cursor[1] += 4;
430 ui_slider_vector( &ui_global_ctx, &dir );
431 }
432 }
433
434 static void run_debug_info(void)
435 {
436 char buf[40];
437
438 snprintf( buf, 40, "%.2fm/s", v3_length( player.phys.rb.v ) );
439 gui_text( (ui_px [2]){ 0, 0 }, buf, 1, k_text_align_left );
440
441 snprintf( buf, 40, "%.2f %.2f %.2f m/s",
442 player.phys.a[0], player.phys.a[1], player.phys.a[2] );
443 gui_text( (ui_px [2]){ 0, 20 }, buf, 1, k_text_align_left );
444
445 snprintf( buf, 40, "pos %.2f %.2f %.2f",
446 player.phys.rb.co[0], player.phys.rb.co[1], player.phys.rb.co[2] );
447 gui_text( (ui_px [2]){ 0, 40 }, buf, 1, k_text_align_left );
448
449 if( vg_gamepad_ready )
450 {
451 for( int i=0; i<6; i++ )
452 {
453 snprintf( buf, 40, "%.2f", vg_gamepad.axes[i] );
454 gui_text( (ui_px [2]){ 0, (i+3)*20 }, buf, 1, k_text_align_left );
455 }
456 }
457 else
458 {
459 gui_text( (ui_px [2]){ 0, 60 },
460 "Gamepad not ready", 1, k_text_align_left );
461 }
462 }
463
464 void vg_ui(void)
465 {
466 if( lightedit )
467 {
468 ui_global_ctx.cursor[0] = 10;
469 ui_global_ctx.cursor[1] = 10;
470 ui_global_ctx.cursor[2] = 200;
471 ui_global_ctx.cursor[3] = 20;
472
473 struct ub_world_lighting *wl = &gpipeline.ub_world_lighting;
474 struct ui_slider_vector
475 s5 = { .min=0.0f, .max=2.0f, .len=3, .data=wl->g_ambient_colour };
476
477 struct ui_slider
478 s8 = { .min=0.0f, .max=2.0f, .data = &gpipeline.shadow_spread },
479 s9 = { .min=0.0f, .max=25.0f, .data = &gpipeline.shadow_length };
480
481 for( int i=0; i<3; i++ )
482 run_light_widget( &gpipeline.widgets[i] );
483
484 gui_text( ui_global_ctx.cursor, "Ambient", 1, 0 );
485 ui_global_ctx.cursor[1] += 16;
486 ui_slider_vector( &ui_global_ctx, &s5 );
487
488 gui_text( ui_global_ctx.cursor, "Shadows", 1, 0 );
489 ui_global_ctx.cursor[1] += 16;
490 ui_slider( &ui_global_ctx, &s8 );
491 ui_slider( &ui_global_ctx, &s9 );
492
493 gui_text( ui_global_ctx.cursor, "Misc", 1, 0 );
494 ui_global_ctx.cursor[1] += 16;
495 struct ui_checkbox c1 = {.data = &wl->g_light_preview};
496 ui_checkbox( &ui_global_ctx, &c1 );
497
498 render_update_lighting_ub();
499 }
500
501 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
502 if( cl_ui )
503 {
504 render_world_routes_ui();
505 }
506 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
507
508 audio_debug_soundscapes();
509 world_audio_debug();
510
511 #if 0
512 static double last_b_press = 0.0;
513
514 double localtime = vg_time - last_b_press;
515
516 world_routes_ui_updatetime( 0, localtime );
517 world_routes_ui_draw( 0, (v4f){ 1.0f,0.0f,1.0f,1.0f}, 9.0f );
518
519 if( glfwGetKey(vg_window,GLFW_KEY_B) )
520 world_routes_ui_notch( 0, localtime );
521
522 if( vg_time-last_b_press > 1.0 )
523 if( glfwGetKey(vg_window,GLFW_KEY_N) )
524 {
525 last_b_press = vg_time;
526 world_routes_ui_newseg( 0 );
527 }
528
529 static double last_m_press;
530 if( vg_time-last_m_press > 1.0 )
531 if( glfwGetKey( vg_window, GLFW_KEY_M) )
532 {
533 last_m_press = vg_time;
534
535 vg_info( "start: %u\n",world.routes.routes[0].ui.segment_count );
536 for( int i=0; i<world.routes.routes[0].ui.segment_count; i++ )
537 world_routes_ui_popfirst(0);
538
539 vg_info( "new: %u\n",world.routes.routes[0].ui.segment_count );
540 }
541 #endif
542 }