enforce alignment
[carveJwlIkooP6JGAAIwe30JlM.git] / skaterift.c
1 /*
2 * =============================================================================
3 *
4 * Copyright . . . -----, ,----- ,---. .---.
5 * 2021-2022 |\ /| | / | | | | /|
6 * | \ / | +-- / +----- +---' | / |
7 * | \ / | | / | | \ | / |
8 * | \/ | | / | | \ | / |
9 * ' ' '--' [] '----- '----- ' ' '---' SOFTWARE
10 *
11 * =============================================================================
12 */
13
14 #define SR_NETWORKED
15 #include "common.h"
16 #include "conf.h"
17 #include "steam.h"
18 #include "render.h"
19 #include "audio.h"
20 #include "world.h"
21 #include "player.h"
22 #include "network.h"
23 #include "menu.h"
24
25 static int cl_ui = 1,
26 cl_view_id = 0;
27
28 int main( int argc, char *argv[] )
29 {
30 vg_mem.use_libc_malloc = 0;
31 vg_set_mem_quota( 128*1024*1024 );
32 vg_enter( argc, argv, "Voyager Game Engine" );
33
34 return 0;
35 }
36
37 VG_STATIC void highscores_save_at_exit(void)
38 {
39 highscores_serialize_all();
40 }
41
42 VG_STATIC void vg_launch_opt(void)
43 {
44
45 }
46
47 VG_STATIC void vg_preload(void)
48 {
49 g_conf_init();
50
51 vg_convar_push( (struct vg_convar){
52 .name = "cl_ui",
53 .data = &cl_ui,
54 .data_type = k_convar_dtype_i32,
55 .opt_i32 = { .min=0, .max=1, .clamp=1 },
56 .persistent = 0
57 });
58
59 vg_convar_push( (struct vg_convar){
60 .name = "cl_view_id",
61 .data = &cl_view_id,
62 .data_type = k_convar_dtype_i32,
63 .opt_i32 = { .min=0, .max=1, .clamp=1 },
64 .persistent = 0
65 });
66
67 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
68 vg_info(" 2021-2022 |\\ /| | / | | | | /| \n" );
69 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
70 vg_info(" | \\ / | | / | | \\ | / | \n" );
71 vg_info(" | \\/ | | / | | \\ | / | \n" );
72 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
73 "SOFTWARE\n" );
74
75 highscores_init( 2000, 50 );
76 if( !highscores_read() )
77 highscores_create_db();
78
79 vg_loader_step( NULL, highscores_save_at_exit );
80
81 steam_init();
82 vg_loader_step( NULL, steam_end );
83 vg_loader_step( network_init, network_end );
84 }
85
86 VG_STATIC void vg_load(void)
87 {
88 vg_loader_step( render_init, NULL );
89 vg_loader_step( menu_init, NULL );
90 vg_loader_step( world_init, NULL );
91 vg_loader_step( player_init, NULL );
92
93 vg_bake_shaders();
94 vg_loader_step( audio_init, audio_free );
95 world_audio_init();
96
97 /* 'systems' are completely loaded now */
98 strcpy( world.world_name, "maps/mp_mtzero.mdl" );
99 #if 0
100 strcpy( world.world_name, "maps/mp_gridmap.mdl" );
101 #endif
102 world_load();
103 vg_console_load_autos();
104 }
105
106 VG_STATIC void vg_start(void)
107 {
108 reset_player( 1, (const char *[]){ "start" } );
109 }
110
111 VG_STATIC void draw_origin_axis(void)
112 {
113 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 1.0f, 0.0f, 0.0f }, 0xffff0000 );
114 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 1.0f, 0.0f }, 0xff00ff00 );
115 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 0.0f, 1.0f }, 0xff0000ff );
116 }
117
118 VG_STATIC void vg_update(void)
119 {
120 steam_update();
121
122 if( vg.is_loaded )
123 {
124 draw_origin_axis();
125 network_update();
126
127 player_update_pre();
128 world_update( player.phys.rb.co );
129 }
130 }
131
132 VG_STATIC void vg_update_fixed(void)
133 {
134 if( vg.is_loaded )
135 {
136 player_update_fixed();
137 }
138 }
139
140 VG_STATIC void vg_update_post(void)
141 {
142 if( vg.is_loaded )
143 {
144 player_update_post();
145 menu_update();
146 }
147 }
148
149 VG_STATIC void vg_framebuffer_resize( int w, int h )
150 {
151 render_fb_resize();
152 }
153
154 VG_STATIC void present_view_with_post_processing(void)
155 {
156 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
157
158 glEnable(GL_BLEND);
159 glDisable(GL_DEPTH_TEST);
160 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
161 glBlendEquation(GL_FUNC_ADD);
162
163 if( cl_blur )
164 {
165 shader_blitblur_use();
166 shader_blitblur_uTexMain( 0 );
167 shader_blitblur_uTexMotion( 1 );
168 shader_blitblur_uBlurStrength(cl_blur_strength / (vg.frame_delta*60.0f));
169
170 v2f menu_blurring;
171 v2_muls( (v2f){ 0.04f, 0.001f }, menu_opacity, menu_blurring );
172 shader_blitblur_uOverrideDir( menu_blurring );
173
174 if( cl_view_id == 0 )
175 render_fb_bind_texture( gpipeline.fb_main, 0, 0 );
176 else if( cl_view_id == 1 )
177 render_fb_bind_texture( gpipeline.fb_main, 1, 0 );
178 else
179 render_fb_bind_texture( gpipeline.fb_main, 0, 0 );
180
181 render_fb_bind_texture( gpipeline.fb_main, 1, 1 );
182 }
183 else
184 {
185 shader_blit_use();
186 shader_blit_uTexMain( 0 );
187 render_fb_bind_texture( gpipeline.fb_main, 0, 0 );
188 }
189
190 render_fsquad();
191 }
192
193 VG_STATIC void render_player_transparent(void)
194 {
195 static camera small_cam; /* DOES NOT NEED TO BE STATIC BUT MINGW
196 SAIS OTHERWISE */
197
198 m4x3_copy( main_camera.transform, small_cam.transform );
199
200 small_cam.fov = main_camera.fov;
201 small_cam.nearz = 0.05f;
202 small_cam.farz = 60.0f;
203
204 camera_update_view( &small_cam );
205 camera_update_projection( &small_cam );
206 camera_finalize( &small_cam );
207
208 /* Draw player to window buffer and blend background ontop */
209
210 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
211 draw_player( &small_cam );
212 }
213
214 VG_STATIC void render_scene(void)
215 {
216 render_fb_bind( gpipeline.fb_main );
217 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
218 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
219
220 /* Draw world */
221 glEnable( GL_DEPTH_TEST );
222
223 render_world( &main_camera );
224
225 int player_transparent = !(player.is_dead || freecam),
226 player_draw = !cl_menu;
227
228 if( !player_transparent && player_draw )
229 draw_player( &main_camera );
230
231 render_water_texture( &main_camera );
232 render_fb_bind( gpipeline.fb_main );
233 render_water_surface( &main_camera );
234 render_world_gates( &main_camera );
235
236 if( player_transparent && player_draw )
237 render_player_transparent();
238 }
239
240 VG_STATIC void render_menu(void)
241 {
242 glClear( GL_DEPTH_BUFFER_BIT );
243 menu_render( &main_camera );
244 }
245
246 VG_STATIC void render_main_game(void)
247 {
248 static float fov = 60.0f;
249 float fov_target = vg_lerpf( 90.0f, 110.0f, cl_fov );
250 if( player.phys.on_board )
251 fov_target = vg_lerpf( 97.0f, 135.0f, cl_fov );
252 if( cl_menu )
253 fov_target = menu_fov_target;
254 fov = vg_lerpf( fov, fov_target, vg.frame_delta * 2.0f );
255 fov = freecam? 60.0f: fov;
256
257 main_camera.fov = fov;
258 main_camera.nearz = 0.1f;
259 main_camera.farz = 2100.0f;
260
261 camera_update_view( &main_camera );
262 camera_update_projection( &main_camera );
263 camera_finalize( &main_camera );
264
265 /* ========== Begin Frame ========== */
266
267 render_scene();
268 present_view_with_post_processing();
269
270 if( cl_menu )
271 {
272 render_menu();
273 render_player_transparent();
274 }
275
276 /* =========== End Frame =========== */
277 }
278
279 VG_STATIC void vg_render(void)
280 {
281 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
282
283 glViewport( 0,0, vg.window_x, vg.window_y );
284 glDisable( GL_DEPTH_TEST );
285
286 glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
287 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
288
289 render_main_game();
290
291 /* Other shite */
292 glDisable(GL_BLEND);
293 glDisable( GL_DEPTH_TEST );
294 vg_lines_drawall( (float *)main_camera.mtx.pv );
295 glViewport( 0,0, vg.window_x, vg.window_y );
296 }
297
298 VG_STATIC void vg_ui(void)
299 {
300 menu_crap_ui();
301
302 #if 0
303 if( lightedit )
304 {
305 ui_global_ctx.cursor[0] = 10;
306 ui_global_ctx.cursor[1] = 10;
307 ui_global_ctx.cursor[2] = 200;
308 ui_global_ctx.cursor[3] = 20;
309
310 struct ub_world_lighting *wl = &gpipeline.ub_world_lighting;
311 struct ui_slider_vector
312 s5 = { .min=0.0f, .max=2.0f, .len=3, .data=wl->g_ambient_colour };
313
314 struct ui_slider
315 s8 = { .min=0.0f, .max=2.0f, .data = &gpipeline.shadow_spread },
316 s9 = { .min=0.0f, .max=25.0f, .data = &gpipeline.shadow_length };
317
318 for( int i=0; i<3; i++ )
319 run_light_widget( &gpipeline.widgets[i] );
320
321 gui_text( ui_global_ctx.cursor, "Ambient", 1, 0 );
322 ui_global_ctx.cursor[1] += 16;
323 ui_slider_vector( &ui_global_ctx, &s5 );
324
325 gui_text( ui_global_ctx.cursor, "Shadows", 1, 0 );
326 ui_global_ctx.cursor[1] += 16;
327 ui_slider( &ui_global_ctx, &s8 );
328 ui_slider( &ui_global_ctx, &s9 );
329
330 gui_text( ui_global_ctx.cursor, "Misc", 1, 0 );
331 ui_global_ctx.cursor[1] += 16;
332 struct ui_checkbox c1 = {.data = &wl->g_light_preview};
333 ui_checkbox( &ui_global_ctx, &c1 );
334
335 render_update_lighting_ub();
336 }
337 #endif
338
339 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
340 if( cl_ui )
341 {
342 render_world_routes_ui();
343 }
344 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
345
346 audio_debug_soundscapes();
347 render_view_framebuffer_ui();
348 }
349
350 #if 0
351 VG_STATIC void run_light_widget( struct light_widget *lw )
352 {
353 struct ui_checkbox c1 = { .data=&lw->enabled };
354
355 ui_checkbox( &ui_global_ctx, &c1 );
356
357 if( lw->enabled )
358 {
359 struct ui_slider_vector
360 colour = { .min=0.0f, .max=2.0f, .len=3, .data=lw->colour },
361 dir = { .min=-VG_PIf, .max=VG_PIf, .len=2, .data=lw->dir };
362
363 ui_slider_vector( &ui_global_ctx, &colour );
364 ui_global_ctx.cursor[1] += 4;
365 ui_slider_vector( &ui_global_ctx, &dir );
366 }
367 }
368 #endif
369
370 VG_STATIC void run_debug_info(void)
371 {
372 char buf[40];
373
374 snprintf( buf, 40, "%.2fm/s", v3_length( player.phys.rb.v ) );
375 ui_text( (ui_px [2]){ 0, 0 }, buf, 1, k_text_align_left );
376
377 snprintf( buf, 40, "%.2f %.2f %.2f m/s",
378 player.phys.a[0], player.phys.a[1], player.phys.a[2] );
379 ui_text( (ui_px [2]){ 0, 20 }, buf, 1, k_text_align_left );
380
381 snprintf( buf, 40, "pos %.2f %.2f %.2f",
382 player.phys.rb.co[0], player.phys.rb.co[1], player.phys.rb.co[2] );
383 ui_text( (ui_px [2]){ 0, 40 }, buf, 1, k_text_align_left );
384
385 if( vg_input.controller_handle )
386 {
387 for( int i=0; i<vg_list_size(vg_input.controller_axises); i++ )
388 {
389 snprintf( buf, 40, "%.2f", vg_input.controller_axises[i] );
390 ui_text( (ui_px [2]){ 0, (i+3)*20 }, buf, 1, k_text_align_left );
391 }
392 }
393 else
394 {
395 ui_text( (ui_px [2]){ 0, 60 },
396 "Gamepad not ready", 1, k_text_align_left );
397 }
398 }