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