fov slider input maps menu stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / skaterift.c
1 /*
2 * =============================================================================
3 *
4 * Copyright . . . -----, ,----- ,---. .---.
5 * 2021-2022 |\ /| | / | | | | /|
6 * | \ / | +-- / +----- +---' | / |
7 * | \ / | | / | | \ | / |
8 * | \/ | | / | | \ | / |
9 * ' ' '--' [] '----- '----- ' ' '---' SOFTWARE
10 *
11 * =============================================================================
12 *
13 * register: shader register & init scheduling
14 * init: initialization
15 * update: logic
16 * render: graphics
17 * free: resource free
18 *
19 */
20
21 #define VG_3D
22 #define VG_STATIC static
23 //#define VG_STATIC
24
25 //#define VG_MINIMAL_TEST
26 #ifndef VG_MINIMAL_TEST
27
28 #define SR_NETWORKED
29
30 #include "common.h"
31 #include "steam.h"
32 #include "render.h"
33 #include "audio.h"
34 #include "world.h"
35 #include "player.h"
36 #include "network.h"
37 #include "menu.h"
38
39 static int cl_ui = 1;
40
41 int main( int argc, char *argv[] )
42 {
43 vg_mem.use_libc_malloc = 0;
44 vg_set_mem_quota( 128*1024*1024 );
45 vg_enter( argc, argv, "Voyager Game Engine" );
46 }
47
48 VG_STATIC void highscores_save_at_exit(void*_)
49 {
50 highscores_serialize_all();
51 }
52
53 VG_STATIC void vg_preload(void)
54 {
55
56 vg_convar_push( (struct vg_convar){
57 .name = "cl_ui",
58 .data = &cl_ui,
59 .data_type = k_convar_dtype_i32,
60 .opt_i32 = { .min=0, .max=1, .clamp=1 },
61 .persistent = 1
62 });
63
64 vg_convar_push( (struct vg_convar){
65 .name = "cl_fov",
66 .data = &g_fov_option,
67 .data_type = k_convar_dtype_f32,
68 .opt_f32 = { .clamp = 0 },
69 .persistent = 1
70 });
71
72 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
73 vg_info(" 2021-2022 |\\ /| | / | | | | /| \n" );
74 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
75 vg_info(" | \\ / | | / | | \\ | / | \n" );
76 vg_info(" | \\/ | | / | | \\ | / | \n" );
77 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
78 "SOFTWARE\n" );
79
80 highscores_init( 2000, 50 );
81 if( !highscores_read() )
82 highscores_create_db();
83
84 vg_loader_highwater( NULL, highscores_save_at_exit, NULL );
85
86 //vg_sleep_ms(200);
87
88 steam_init();
89 vg_loader_highwater( NULL, steam_end, NULL );
90 vg_loader_highwater( network_init, network_end, NULL );
91 }
92
93 VG_STATIC void vg_load(void)
94 {
95 vg_loader_highwater( render_init, NULL, NULL );
96 vg_loader_highwater( menu_init, NULL, NULL );
97 vg_loader_highwater( world_init, NULL, NULL );
98 vg_loader_highwater( player_init, NULL, NULL );
99
100 vg_bake_shaders();
101 vg_loader_highwater( audio_init, audio_free, NULL );
102 world_audio_init();
103
104 /* 'systems' are completely loaded now */
105 strcpy( world.world_name, "maps/mp_mtzero.mdl" );
106 world_load();
107 vg_console_load_autos();
108 }
109
110 VG_STATIC void vg_start(void)
111 {
112 reset_player( 1, (const char *[]){ "start" } );
113 }
114
115 VG_STATIC void draw_origin_axis(void)
116 {
117 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 1.0f, 0.0f, 0.0f }, 0xffff0000 );
118 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 1.0f, 0.0f }, 0xff00ff00 );
119 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 0.0f, 1.0f }, 0xff0000ff );
120 }
121
122 VG_STATIC void vg_update( int loaded )
123 {
124 steam_update();
125
126 if( loaded )
127 {
128 draw_origin_axis();
129 network_update();
130
131 player_update_pre();
132 world_update( player.phys.rb.co );
133 }
134 }
135
136 VG_STATIC void vg_update_fixed( int loaded )
137 {
138 if( loaded )
139 {
140 player_update_fixed();
141 }
142 }
143
144 VG_STATIC void vg_update_post( int loaded )
145 {
146 if( loaded )
147 {
148 player_update_post();
149 menu_update();
150 }
151 }
152
153 VG_STATIC void vg_framebuffer_resize( int w, int h )
154 {
155 render_fb_resize();
156 water_fb_resize();
157 }
158
159 VG_STATIC void render_main_game(void)
160 {
161 m4x4f world_4x4;
162 m4x3_expand( camera_mtx_inverse, world_4x4 );
163
164 static float fov = 60.0f;
165
166 float fov_target = vg_lerpf( 90.0f, 110.0f, g_fov_option );
167
168 if( player.phys.on_board )
169 fov_target = vg_lerpf( 97.0f, 135.0f, g_fov_option );
170
171 if( cl_menu )
172 fov_target = menu_fov_target;
173
174 fov = vg_lerpf( fov, fov_target, vg.frame_delta * 2.0f );
175
176 gpipeline.fov = freecam? 60.0f: fov; /* 120 */
177 m4x4_projection( vg.pv, gpipeline.fov,
178 (float)vg.window_x / (float)vg.window_y,
179 0.1f, 2100.0f );
180
181 m4x4_mul( vg.pv, world_4x4, vg.pv );
182 glEnable( GL_DEPTH_TEST );
183
184 /*
185 * Draw world
186 */
187
188 int draw_solid = player.is_dead | freecam;
189 render_world( vg.pv, camera_mtx );
190
191 if( draw_solid )
192 draw_player( camera_mtx );
193
194 render_water_texture( camera_mtx );
195
196 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
197 render_water_surface( vg.pv, camera_mtx );
198 render_world_gates( vg.pv, player.phys.rb.co, camera_mtx );
199
200 if( cl_menu )
201 {
202 glClear( GL_DEPTH_BUFFER_BIT );
203 menu_render( vg.pv );
204 }
205
206 /* Copy the RGB of what we have into the background buffer */
207 glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
208 glBindFramebuffer( GL_DRAW_FRAMEBUFFER, gpipeline.fb_background );
209 glBlitFramebuffer( 0,0, vg.window_x, vg.window_y,
210 0,0, vg.window_x, vg.window_y,
211 GL_COLOR_BUFFER_BIT,
212 GL_LINEAR );
213
214 /* Clear out the colour buffer, but keep depth */
215 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
216 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
217
218 if( !player.is_dead )
219 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
220 else
221 glClear( GL_COLOR_BUFFER_BIT );
222
223 if( !draw_solid )
224 {
225 m4x4_projection( vg.pv, gpipeline.fov,
226 (float)vg.window_x / (float)vg.window_y,
227 0.05f, 60.0f );
228 m4x4_mul( vg.pv, world_4x4, vg.pv );
229 draw_player( camera_mtx );
230 }
231
232 /* Draw back in the background
233 *
234 * TODO: need to disable alpha write in the terrain shader so this works
235 * again.
236 */
237 glEnable(GL_BLEND);
238 glDisable(GL_DEPTH_TEST);
239 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
240 glBlendEquation(GL_FUNC_ADD);
241
242 shader_blit_use();
243 shader_blit_uTexMain( 0 );
244 glActiveTexture(GL_TEXTURE0);
245 glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background );
246
247 render_fsquad();
248 }
249
250 VG_STATIC void vg_render(void)
251 {
252 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
253 glViewport( 0,0, vg.window_x, vg.window_y );
254 glDisable( GL_DEPTH_TEST );
255
256 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
257 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
258
259 render_main_game();
260
261
262 /* Other shite */
263 glDisable(GL_BLEND);
264 glDisable( GL_DEPTH_TEST );
265 vg_lines_drawall( (float *)vg.pv );
266 glViewport( 0,0, vg.window_x, vg.window_y );
267 }
268
269 VG_STATIC void vg_ui(void)
270 {
271 menu_crap_ui();
272
273 #if 0
274 if( lightedit )
275 {
276 ui_global_ctx.cursor[0] = 10;
277 ui_global_ctx.cursor[1] = 10;
278 ui_global_ctx.cursor[2] = 200;
279 ui_global_ctx.cursor[3] = 20;
280
281 struct ub_world_lighting *wl = &gpipeline.ub_world_lighting;
282 struct ui_slider_vector
283 s5 = { .min=0.0f, .max=2.0f, .len=3, .data=wl->g_ambient_colour };
284
285 struct ui_slider
286 s8 = { .min=0.0f, .max=2.0f, .data = &gpipeline.shadow_spread },
287 s9 = { .min=0.0f, .max=25.0f, .data = &gpipeline.shadow_length };
288
289 for( int i=0; i<3; i++ )
290 run_light_widget( &gpipeline.widgets[i] );
291
292 gui_text( ui_global_ctx.cursor, "Ambient", 1, 0 );
293 ui_global_ctx.cursor[1] += 16;
294 ui_slider_vector( &ui_global_ctx, &s5 );
295
296 gui_text( ui_global_ctx.cursor, "Shadows", 1, 0 );
297 ui_global_ctx.cursor[1] += 16;
298 ui_slider( &ui_global_ctx, &s8 );
299 ui_slider( &ui_global_ctx, &s9 );
300
301 gui_text( ui_global_ctx.cursor, "Misc", 1, 0 );
302 ui_global_ctx.cursor[1] += 16;
303 struct ui_checkbox c1 = {.data = &wl->g_light_preview};
304 ui_checkbox( &ui_global_ctx, &c1 );
305
306 render_update_lighting_ub();
307 }
308 #endif
309
310 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
311 if( cl_ui )
312 {
313 render_world_routes_ui();
314 }
315 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
316
317 audio_debug_soundscapes();
318 }
319
320 #if 0
321 VG_STATIC void run_light_widget( struct light_widget *lw )
322 {
323 struct ui_checkbox c1 = { .data=&lw->enabled };
324
325 ui_checkbox( &ui_global_ctx, &c1 );
326
327 if( lw->enabled )
328 {
329 struct ui_slider_vector
330 colour = { .min=0.0f, .max=2.0f, .len=3, .data=lw->colour },
331 dir = { .min=-VG_PIf, .max=VG_PIf, .len=2, .data=lw->dir };
332
333 ui_slider_vector( &ui_global_ctx, &colour );
334 ui_global_ctx.cursor[1] += 4;
335 ui_slider_vector( &ui_global_ctx, &dir );
336 }
337 }
338 #endif
339
340 VG_STATIC void run_debug_info(void)
341 {
342 char buf[40];
343
344 snprintf( buf, 40, "%.2fm/s", v3_length( player.phys.rb.v ) );
345 ui_text( (ui_px [2]){ 0, 0 }, buf, 1, k_text_align_left );
346
347 snprintf( buf, 40, "%.2f %.2f %.2f m/s",
348 player.phys.a[0], player.phys.a[1], player.phys.a[2] );
349 ui_text( (ui_px [2]){ 0, 20 }, buf, 1, k_text_align_left );
350
351 snprintf( buf, 40, "pos %.2f %.2f %.2f",
352 player.phys.rb.co[0], player.phys.rb.co[1], player.phys.rb.co[2] );
353 ui_text( (ui_px [2]){ 0, 40 }, buf, 1, k_text_align_left );
354
355 if( vg.gamepad_ready )
356 {
357 for( int i=0; i<6; i++ )
358 {
359 snprintf( buf, 40, "%.2f", vg.gamepad.axes[i] );
360 ui_text( (ui_px [2]){ 0, (i+3)*20 }, buf, 1, k_text_align_left );
361 }
362 }
363 else
364 {
365 ui_text( (ui_px [2]){ 0, 60 },
366 "Gamepad not ready", 1, k_text_align_left );
367 }
368 }
369
370 #else
371
372 #define VG_TIMESTEP_FIXED (1.0/60.0)
373 #define VG_3D
374 #define VG_FRAMEBUFFER_RESIZE 1
375 #include "vg/vg.h"
376
377 int main( int argc, char *argv[] )
378 {
379 vg_prealloc_quota( 512*1024*1024 );
380 vg_enter( argc, argv, "Voyager Game Engine" );
381 }
382
383 VG_STATIC void vg_preload(void)
384 {
385 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
386 vg_info(" 2021-2022 |\\ /| | / | | | | /| \n" );
387 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
388 vg_info(" | \\ / | | / | | \\ | / | \n" );
389 vg_info(" | \\/ | | / | | \\ | / | \n" );
390 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
391 "SOFTWARE\n" );
392 }
393
394 VG_STATIC void vg_load(void)
395 {
396 vg_bake_shaders();
397 vg_console_load_autos();
398 }
399
400 VG_STATIC void vg_start(void)
401 {
402 }
403
404 VG_STATIC void vg_update( int loaded )
405 {
406 }
407
408 VG_STATIC void vg_update_fixed( int loaded )
409 {
410 }
411
412 VG_STATIC void vg_update_post( int loaded )
413 {
414 }
415
416 VG_STATIC void vg_framebuffer_resize( int w, int h )
417 {
418 }
419
420 VG_STATIC void vg_render(void)
421 {
422 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
423 glViewport( 0,0, vg.window_x, vg.window_y );
424 glDisable( GL_DEPTH_TEST );
425
426 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
427 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
428
429 /* Other shite */
430 glDisable(GL_BLEND);
431 glDisable( GL_DEPTH_TEST );
432 vg_lines_drawall( (float *)vg.pv );
433 glViewport( 0,0, vg.window_x, vg.window_y );
434 }
435
436 VG_STATIC void vg_ui(void)
437 {
438 }
439
440 #endif