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