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