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