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