MENY
[carveJwlIkooP6JGAAIwe30JlM.git] / main.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 SR_NETWORKED
22 #define VG_3D
23 #include "common.h"
24 #include "steam.h"
25 #include "render.h"
26 #include "audio.h"
27 #include "world.h"
28 #include "player.h"
29 #include "network.h"
30 #include "menu.h"
31
32 static int cl_ui = 1,
33 cl_menu = 0;
34
35 int main( int argc, char *argv[] )
36 {
37 vg_enter( argc, argv, "Voyager Game Engine" );
38 }
39
40 static void highscores_save_at_exit(void*_)
41 {
42 highscores_serialize_all();
43 highscores_free();
44 }
45
46 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_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
57 vg_info(" 2021-2022 |\\ /| | / | | | | /| \n" );
58 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
59 vg_info(" | \\ / | | / | | \\ | / | \n" );
60 vg_info(" | \\/ | | / | | \\ | / | \n" );
61 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
62 "SOFTWARE\n" );
63
64 highscores_init( 2000, 50 );
65 if( !highscores_read() )
66 highscores_create_db();
67
68 vg_loader_highwater( NULL, highscores_save_at_exit, NULL );
69
70 vg_sleep_ms(200);
71
72 steam_init();
73 vg_loader_highwater( NULL, steam_end, NULL );
74 vg_loader_highwater( network_init, network_end, NULL );
75 }
76
77 void vg_load(void)
78 {
79 vg_loader_highwater( render_init, render_free, NULL );
80 vg_loader_highwater( menu_init, menu_free, NULL );
81 vg_loader_highwater( world_init, world_free, NULL );
82 vg_loader_highwater( player_init, NULL, NULL );
83
84 if( !vg_bake_shaders() )
85 vg_fatal_exit_loop( "Did not load all shaders" );
86
87 vg_loader_highwater( audio_init, audio_free, NULL );
88
89 /* FInal step */
90 world_load();
91 vg_console_load_autos();
92 }
93
94 static void vg_start(void)
95 {
96 player_load_model( "ch_jordan" );
97 reset_player( 1, (const char *[]){ "start" } );
98 }
99
100 static void draw_origin_axis(void)
101 {
102 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 1.0f, 0.0f, 0.0f }, 0xffff0000 );
103 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 1.0f, 0.0f }, 0xff00ff00 );
104 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 0.0f, 1.0f }, 0xff0000ff );
105 }
106
107 void vg_update( int loaded )
108 {
109 steam_update();
110
111 if( loaded )
112 {
113 if( vg_get_button_down( "menu" ) )
114 {
115 cl_menu = !cl_menu;
116 }
117
118 draw_origin_axis();
119 network_update();
120
121 if( !cl_menu )
122 {
123 player_update_pre();
124 world_update( player.phys.rb.co );
125 }
126
127 menu_update( cl_menu );
128 }
129 }
130
131 static void vg_update_fixed( int loaded )
132 {
133 if( loaded && !cl_menu )
134 {
135 player_update_fixed();
136 }
137 }
138
139 static void vg_update_post( int loaded )
140 {
141 if( loaded && !cl_menu )
142 {
143 player_update_post();
144 }
145 }
146
147 static void vg_framebuffer_resize( int w, int h )
148 {
149 render_fb_resize();
150 gate_fb_resize();
151 water_fb_resize();
152 }
153
154 static void render_main_game(void)
155 {
156 v3f *active_cam_inv = NULL,
157 *active_cam = NULL;
158
159 m4x4f world_4x4;
160
161 if( cl_menu )
162 {
163 active_cam = menu_cam;
164 active_cam_inv = menu_cam_inv;
165 }
166 else
167 {
168 active_cam_inv = player.camera_inverse;
169 active_cam = player.camera;
170 }
171
172 m4x3_expand( active_cam_inv, world_4x4 );
173
174 static float fov = 97.0f;
175 float fov_target = (player.phys.on_board&&!cl_menu)? 125.0f: 108.0f;
176 fov = vg_lerpf( fov, fov_target, vg.time_delta * 2.0f );
177
178 gpipeline.fov = freecam? 60.0f: fov; /* 120 */
179 m4x4_projection( vg.pv, gpipeline.fov,
180 (float)vg.window_x / (float)vg.window_y,
181 0.1f, 2100.0f );
182
183 m4x4_mul( vg.pv, world_4x4, vg.pv );
184 glEnable( GL_DEPTH_TEST );
185
186 /*
187 * Draw world
188 */
189
190 int draw_solid = player.is_dead | freecam;
191
192 render_world( vg.pv, active_cam );
193 if( draw_solid )
194 draw_player( active_cam );
195
196 render_water_texture( active_cam );
197
198 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
199 render_water_surface( vg.pv, active_cam );
200 render_world_gates( vg.pv, player.phys.rb.co, active_cam );
201
202 if( cl_menu )
203 menu_render( vg.pv );
204
205 /* Copy the RGB of what we have into the background buffer */
206 glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
207 glBindFramebuffer( GL_DRAW_FRAMEBUFFER, gpipeline.fb_background );
208 glBlitFramebuffer( 0,0, vg.window_x, vg.window_y,
209 0,0, vg.window_x, vg.window_y,
210 GL_COLOR_BUFFER_BIT,
211 GL_LINEAR );
212
213 /* Clear out the colour buffer, but keep depth */
214 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
215 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
216
217 if( !player.is_dead )
218 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
219 else
220 glClear( GL_COLOR_BUFFER_BIT );
221
222 if( !draw_solid )
223 {
224 m4x4_projection( vg.pv, gpipeline.fov,
225 (float)vg.window_x / (float)vg.window_y,
226 0.01f, 600.0f );
227 m4x4_mul( vg.pv, world_4x4, vg.pv );
228 draw_player( active_cam );
229 }
230
231 /* Draw back in the background
232 *
233 * TODO: need to disable alpha write in the terrain shader so this works
234 * again.
235 */
236 glEnable(GL_BLEND);
237 glDisable(GL_DEPTH_TEST);
238 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
239 glBlendEquation(GL_FUNC_ADD);
240
241 shader_blit_use();
242 shader_blit_uTexMain( 0 );
243 glActiveTexture(GL_TEXTURE0);
244 glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background );
245
246 render_fsquad();
247 }
248
249 void vg_render(void)
250 {
251 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
252 glViewport( 0,0, vg.window_x, vg.window_y );
253 glDisable( GL_DEPTH_TEST );
254
255 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
256 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
257
258 render_main_game();
259
260
261 /* Other shite */
262 glDisable(GL_BLEND);
263 glDisable( GL_DEPTH_TEST );
264 vg_lines_drawall( (float *)vg.pv );
265 glViewport( 0,0, vg.window_x, vg.window_y );
266 }
267
268 void vg_ui(void)
269 {
270 #if 0
271 if( cl_menu )
272 {
273 ui_rect menu =
274 {
275 vg.window_x / 2 - 150,
276 vg.window_y / 2 - 50,
277 300,
278 130
279 };
280
281 ui_fill_rect( &ui_global_ctx, menu, 0xa0000000 );
282
283 ui_text( &ui_global_ctx, (ui_rect){ menu[0]+menu[2]/2,menu[1]+6, 0, 0 },
284 steam_username_at_startup,
285 1, k_text_align_center );
286 menu[1] += 24;
287 menu[3] -= 30;
288
289 ui_rect_pad( menu, 8 );
290 ui_fill_rect( &ui_global_ctx, menu, 0x90ffffff );
291 ui_rect_pad( menu, 2 );
292 ui_fill_rect( &ui_global_ctx, menu, 0xa0000000 );
293
294 menu[1] += 32;
295 ui_text( &ui_global_ctx, (ui_rect){ menu[0]+menu[2]/2,menu[1], 0, 0 },
296 "Exit", 2, k_text_align_center );
297
298 if( vg_get_button_down( "jump" ) )
299 {
300 glfwSetWindowShouldClose( vg.window, 1 );
301 }
302 }
303
304 if( lightedit )
305 {
306 ui_global_ctx.cursor[0] = 10;
307 ui_global_ctx.cursor[1] = 10;
308 ui_global_ctx.cursor[2] = 200;
309 ui_global_ctx.cursor[3] = 20;
310
311 struct ub_world_lighting *wl = &gpipeline.ub_world_lighting;
312 struct ui_slider_vector
313 s5 = { .min=0.0f, .max=2.0f, .len=3, .data=wl->g_ambient_colour };
314
315 struct ui_slider
316 s8 = { .min=0.0f, .max=2.0f, .data = &gpipeline.shadow_spread },
317 s9 = { .min=0.0f, .max=25.0f, .data = &gpipeline.shadow_length };
318
319 for( int i=0; i<3; i++ )
320 run_light_widget( &gpipeline.widgets[i] );
321
322 gui_text( ui_global_ctx.cursor, "Ambient", 1, 0 );
323 ui_global_ctx.cursor[1] += 16;
324 ui_slider_vector( &ui_global_ctx, &s5 );
325
326 gui_text( ui_global_ctx.cursor, "Shadows", 1, 0 );
327 ui_global_ctx.cursor[1] += 16;
328 ui_slider( &ui_global_ctx, &s8 );
329 ui_slider( &ui_global_ctx, &s9 );
330
331 gui_text( ui_global_ctx.cursor, "Misc", 1, 0 );
332 ui_global_ctx.cursor[1] += 16;
333 struct ui_checkbox c1 = {.data = &wl->g_light_preview};
334 ui_checkbox( &ui_global_ctx, &c1 );
335
336 render_update_lighting_ub();
337 }
338 #endif
339
340 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
341 if( cl_ui )
342 {
343 render_world_routes_ui();
344 }
345 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
346
347 audio_debug_soundscapes();
348 }
349
350 #if 0
351 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 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 gui_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 gui_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 gui_text( (ui_px [2]){ 0, 40 }, buf, 1, k_text_align_left );
384
385 if( vg.gamepad_ready )
386 {
387 for( int i=0; i<6; i++ )
388 {
389 snprintf( buf, 40, "%.2f", vg.gamepad.axes[i] );
390 gui_text( (ui_px [2]){ 0, (i+3)*20 }, buf, 1, k_text_align_left );
391 }
392 }
393 else
394 {
395 gui_text( (ui_px [2]){ 0, 60 },
396 "Gamepad not ready", 1, k_text_align_left );
397 }
398 }