2 * =============================================================================
4 * Copyright . . . -----, ,----- ,---. .---.
5 * 2021-2022 |\ /| | / | | | | /|
6 * | \ / | +-- / +----- +---' | / |
7 * | \ / | | / | | \ | / |
8 * | \/ | | / | | \ | / |
9 * ' ' '--' [] '----- '----- ' ' '---' SOFTWARE
11 * =============================================================================
14 #define VG_STATIC static
29 int main( int argc
, char *argv
[] )
31 vg_mem
.use_libc_malloc
= 0;
32 vg_set_mem_quota( 128*1024*1024 );
33 vg_enter( argc
, argv
, "Voyager Game Engine" );
38 VG_STATIC
void highscores_save_at_exit(void*_
)
40 highscores_serialize_all();
43 VG_STATIC
void vg_launch_opt(void)
48 VG_STATIC
void vg_preload(void)
50 vg_convar_push( (struct vg_convar
){
53 .data_type
= k_convar_dtype_i32
,
54 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
58 vg_convar_push( (struct vg_convar
){
60 .data
= &g_fov_option
,
61 .data_type
= k_convar_dtype_f32
,
62 .opt_f32
= { .clamp
= 0 },
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(" ' ' '--' [] '----- '----- ' ' '---' "
74 highscores_init( 2000, 50 );
75 if( !highscores_read() )
76 highscores_create_db();
78 vg_loader_highwater( NULL
, highscores_save_at_exit
, NULL
);
83 vg_loader_highwater( NULL
, steam_end
, NULL
);
84 vg_loader_highwater( network_init
, network_end
, NULL
);
87 VG_STATIC
void vg_load(void)
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
);
95 vg_loader_highwater( audio_init
, audio_free
, NULL
);
98 /* 'systems' are completely loaded now */
99 strcpy( world
.world_name
, "maps/mp_mtzero.mdl" );
101 vg_console_load_autos();
104 VG_STATIC
void vg_start(void)
106 reset_player( 1, (const char *[]){ "start" } );
109 VG_STATIC
void draw_origin_axis(void)
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 );
116 VG_STATIC
void vg_update( int loaded
)
126 world_update( player
.phys
.rb
.co
);
130 VG_STATIC
void vg_update_fixed( int loaded
)
134 player_update_fixed();
138 VG_STATIC
void vg_update_post( int loaded
)
142 player_update_post();
147 VG_STATIC
void vg_framebuffer_resize( int w
, int h
)
153 VG_STATIC
void render_main_game(void)
156 m4x3_expand( camera_mtx_inverse
, world_4x4
);
158 static float fov
= 60.0f
;
160 float fov_target
= vg_lerpf( 90.0f
, 110.0f
, g_fov_option
);
162 if( player
.phys
.on_board
)
163 fov_target
= vg_lerpf( 97.0f
, 135.0f
, g_fov_option
);
166 fov_target
= menu_fov_target
;
168 fov
= vg_lerpf( fov
, fov_target
, vg
.frame_delta
* 2.0f
);
170 gpipeline
.fov
= freecam
? 60.0f
: fov
; /* 120 */
171 m4x4_projection( vg
.pv
, gpipeline
.fov
,
172 (float)vg
.window_x
/ (float)vg
.window_y
,
175 m4x4_mul( vg
.pv
, world_4x4
, vg
.pv
);
176 glEnable( GL_DEPTH_TEST
);
182 int draw_solid
= player
.is_dead
| freecam
;
183 render_world( vg
.pv
, camera_mtx
);
186 draw_player( camera_mtx
);
188 render_water_texture( camera_mtx
);
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
);
196 glClear( GL_DEPTH_BUFFER_BIT
);
197 menu_render( vg
.pv
);
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
,
208 /* Clear out the colour buffer, but keep depth */
209 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
210 glClearColor( 0.0f
, 0.0f
, 0.0f
, 0.0f
);
212 if( !player
.is_dead
)
213 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
215 glClear( GL_COLOR_BUFFER_BIT
);
219 m4x4_projection( vg
.pv
, gpipeline
.fov
,
220 (float)vg
.window_x
/ (float)vg
.window_y
,
222 m4x4_mul( vg
.pv
, world_4x4
, vg
.pv
);
223 draw_player( camera_mtx
);
226 /* Draw back in the background
228 * TODO: need to disable alpha write in the terrain shader so this works
232 glDisable(GL_DEPTH_TEST
);
233 glBlendFunc(GL_ONE_MINUS_DST_ALPHA
, GL_DST_ALPHA
);
234 glBlendEquation(GL_FUNC_ADD
);
237 shader_blit_uTexMain( 0 );
238 glActiveTexture(GL_TEXTURE0
);
239 glBindTexture( GL_TEXTURE_2D
, gpipeline
.rgb_background
);
244 VG_STATIC
void vg_render(void)
246 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
247 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
248 glDisable( GL_DEPTH_TEST
);
250 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
251 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
258 glDisable( GL_DEPTH_TEST
);
259 vg_lines_drawall( (float *)vg
.pv
);
260 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
263 VG_STATIC
void vg_ui(void)
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;
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
};
280 s8
= { .min
=0.0f
, .max
=2.0f
, .data
= &gpipeline
.shadow_spread
},
281 s9
= { .min
=0.0f
, .max
=25.0f
, .data
= &gpipeline
.shadow_length
};
283 for( int i
=0; i
<3; i
++ )
284 run_light_widget( &gpipeline
.widgets
[i
] );
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
);
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
);
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
);
300 render_update_lighting_ub();
304 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
307 render_world_routes_ui();
309 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
311 audio_debug_soundscapes();
315 VG_STATIC
void run_light_widget( struct light_widget
*lw
)
317 struct ui_checkbox c1
= { .data
=&lw
->enabled
};
319 ui_checkbox( &ui_global_ctx
, &c1
);
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
};
327 ui_slider_vector( &ui_global_ctx
, &colour
);
328 ui_global_ctx
.cursor
[1] += 4;
329 ui_slider_vector( &ui_global_ctx
, &dir
);
334 VG_STATIC
void run_debug_info(void)
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
);
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
);
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
);
349 if( vg_input
.controller_handle
)
351 for( int i
=0; i
<vg_list_size(vg_input
.controller_axises
); i
++ )
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
);
359 ui_text( (ui_px
[2]){ 0, 60 },
360 "Gamepad not ready", 1, k_text_align_left
);