2 * =============================================================================
4 * Copyright . . . -----, ,----- ,---. .---.
5 * 2021-2023 |\ /| | / | | | | /|
6 * | \ / | +-- / +----- +---' | / |
7 * | \ / | | / | | \ | / |
8 * | \/ | | / | | \ | / |
9 * ' ' '--' [] '----- '----- ' ' '---' SOFTWARE
11 * =============================================================================
17 #define SR_USE_LOCALHOST
25 * --------------------- */
27 #include "skaterift.h"
40 #include "pointcloud.h"
42 #include "player_remote.h"
45 * ----------------- */
53 #include "highscores.c"
57 #include "player_remote.c"
58 #include "vg/vg_audio_dsp.h"
60 static struct player_avatar localplayer_avatar
;
61 static int k_tools_mode
= 0;
63 int main( int argc
, char *argv
[] ){
64 vg_mem
.use_libc_malloc
= 0;
65 vg_set_mem_quota( 160*1024*1024 );
66 vg_enter( argc
, argv
, "Voyager Game Engine" );
70 static void vg_launch_opt(void){
72 if( vg_long_opt( "noauth" ) ){
73 network_client
.auth_mode
= eServerModeNoAuthentication
;
76 if( (arg
= vg_long_opt_arg( "server" )) ){
77 vg_strncpy( arg
, network_client
.server_adress
, 64,
78 k_strncpy_overflow_fatal
);
81 if( vg_long_opt( "tools" ) ){
86 static void vg_preload(void){
87 //skaterift_read_savedata();
89 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
90 vg_info(" 2021-2023 |\\ /| | / | | | | /| \n" );
91 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
92 vg_info(" | \\ / | | / | | \\ | / | \n" );
93 vg_info(" | \\/ | | / | | \\ | / | \n" );
94 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
101 vg_loader_step( NULL
, steam_end
);
102 vg_loader_step( remote_players_init
, NULL
);
103 vg_loader_step( network_init
, network_end
);
106 static void load_playermodels(void){
108 shader_model_character_view_register();
109 shader_model_board_view_register();
110 shader_model_entity_register();
113 static void async_skaterift_player_start( void *payload
, u32 size
){
114 localplayer
.viewable_world
= world_current_instance();
115 localplayer_cmd_respawn( 1, (const char *[]){ "start" } );
118 static void async_call_ready( void *payload
, u32 size
){
119 skaterift
.op
= k_async_op_none
;
122 static void skaterift_restore_state(void){
124 strcpy( sav
.path
, str_skaterift_main_save
);
125 savedata_file_read( &sav
);
128 vg_msg_init( &kvsav
, sav
.buf
, sizeof(sav
.buf
) );
130 u32 board_reg_id
= 0,
133 vg_msg_cursor orig
= kvsav
.cur
;
135 if( vg_msg_seekframe( &kvsav
, "player" ) ){
139 skaterift_read_addon_alias( &kvsav
, "board", k_addon_type_board
, &q
);
140 u32 reg_id
= addon_match( &q
);
141 if( reg_id
!= 0xffffffff )
142 board_reg_id
= reg_id
;
145 skaterift_read_addon_alias( &kvsav
, "playermodel",
146 k_addon_type_player
, &q
);
147 reg_id
= addon_match( &q
);
148 if( reg_id
!= 0xffffffff )
149 player_reg_id
= reg_id
;
152 localplayer
.board_view_slot
=
153 addon_cache_create_viewer( k_addon_type_board
, board_reg_id
);
154 localplayer
.playermodel_view_slot
=
155 addon_cache_create_viewer( k_addon_type_player
, player_reg_id
);
158 if( vg_msg_seekframe( &kvsav
, "world" ) ){
162 skaterift_read_addon_alias( &kvsav
, "alias", k_addon_type_world
, &q
);
163 u32 reg_id
= addon_match( &q
);
164 if( reg_id
!= 0xffffffff ){
165 addon_reg
*reg
= get_addon_from_index( k_addon_type_world
, reg_id
);
167 world_static
.load_state
= k_world_loader_load
;
168 struct world_load_args args
= {
169 .purpose
= k_world_purpose_client
,
172 skaterift_world_load_thread( &args
);
174 world_set_active_instance( vg_msg_getkvu32( &kvsav
, "index", 0 ) );
175 world_static
.active_trigger_volume_count
= 0;
176 localplayer
.viewable_world
= world_current_instance();
180 vg_msg_getkvv3f( &kvsav
, "position", pos
, (v3f
){0.0f
,0.0f
,0.0f
} );
182 if( v3_length2(pos
) > 1.0f
)
183 player__setpos( pos
);
187 static void vg_load(void){
189 vg_async_call( async_call_ready
, NULL
, 0 );
193 vg_console_reg_cmd( "changeworld", skaterift_change_world_command
, NULL
);
195 vg_loader_step( render_init
, NULL
);
196 vg_loader_step( menu_init
, NULL
);
197 vg_loader_step( pointcloud_init
, NULL
);
198 vg_loader_step( world_init
, NULL
);
199 vg_loader_step( vehicle_init
, NULL
);
200 vg_loader_step( font3d_init
, NULL
);
201 vg_loader_step( gui_init
, NULL
);
203 vg_loader_step( player_init
, NULL
);
204 vg_loader_step( player_ragdoll_init
, NULL
);
207 vg_loader_step( addon_system_init
, NULL
);
208 vg_loader_step( workshop_init
, NULL
);
209 vg_loader_step( skateshop_init
, NULL
);
211 /* ----------------- */
212 vg_loader_step( load_playermodels
, NULL
);
215 u32 bytes
= 1024*1024*10;
216 skaterift
.replay
.data
= vg_linear_alloc( vg_mem
.rtmemory
, bytes
);
217 skaterift
.replay
.size
= bytes
;
218 replay_clear( &skaterift
.replay
);
220 player_avatar_load( &localplayer_avatar
, "models/ch_none.mdl" );
221 player__use_avatar( &localplayer_avatar
);
223 /* FIXME FIXME FIXME FIXME FIXME */
224 u32 mtx_size
= sizeof(m4x3f
)*localplayer_avatar
.sk
.bone_count
;
225 localplayer
.final_mtx
= vg_linear_alloc( vg_mem
.rtmemory
, mtx_size
);
226 netplayers
.final_mtx
= vg_linear_alloc( vg_mem
.rtmemory
,
227 mtx_size
*NETWORK_MAX_PLAYERS
);
229 player_model_load( &localplayer
.fallback_model
, "models/ch_none.mdl" );
232 /* --------------------- */
235 vg_loader_step( audio_init
, audio_free
);
237 /* 'systems' are completely loaded now */
239 /* Completing addon registrations
240 * -------------------------------------
243 /* understate diy. */
244 addon_reg
*spawn
= addon_mount_local_addon( "maps/mp_spawn",
249 vg_msg_init( &msg
, spawn
->metadata
, sizeof(spawn
->metadata
) );
250 vg_msg_frame( &msg
, "workshop" );
251 vg_msg_wkvstr( &msg
, "title", "Understate DIY" );
252 vg_msg_end_frame( &msg
);
253 vg_msg_wkvstr( &msg
, "location", "USA" );
254 spawn
->metadata_len
= msg
.cur
.co
;
258 addon_reg
*mtzero
= addon_mount_local_addon( "maps/mp_mtzero",
263 vg_msg_init( &msg
, spawn
->metadata
, sizeof(mtzero
->metadata
) );
264 vg_msg_frame( &msg
, "workshop" );
265 vg_msg_wkvstr( &msg
, "title", "Mt.Zero Island" );
266 vg_msg_end_frame( &msg
);
267 vg_msg_wkvstr( &msg
, "location", "Australia" );
268 mtzero
->metadata_len
= msg
.cur
.co
;
271 /* load home/permanent world manually */
272 world_static
.load_state
= k_world_loader_load
;
273 struct world_load_args args
= {
274 .purpose
= k_world_purpose_hub
,
277 skaterift_world_load_thread( &args
);
279 vg_async_call( async_skaterift_player_start
, NULL
, 0 );
283 global_skateshop
.selected_world_id
=1;
284 global_skateshop
.pointcloud_world_id
=1;
285 skateshop_world_preview_loader_thread( mtzero
); /* HACK */
287 vg_console_load_autos();
290 addon_mount_content_folder( k_addon_type_player
,
291 "playermodels", ".mdl" );
292 addon_mount_content_folder( k_addon_type_board
, "boards", ".mdl" );
293 addon_mount_content_folder( k_addon_type_world
, "maps", ".mdl" );
294 addon_mount_workshop_items();
295 vg_async_call( async_addon_reg_update
, NULL
, 0 );
299 skaterift_restore_state();
300 vg_loader_step( NULL
, skaterift_autosave_synchronous
);
302 //board_processview_thread(NULL);
303 vg_async_call( async_call_ready
, NULL
, 0 );
306 static void draw_origin_axis(void){
307 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, 0xffff0000 );
308 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 1.0f
, 0.0f
}, 0xff00ff00 );
309 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 0.0f
, 1.0f
}, 0xff0000ff );
311 static void skaterift_change_client_world_preupdate(void);
315 * ---------------------------------------------------------------------------*/
317 static void vg_pre_update(void){
319 skaterift_preupdate_inputs();
321 if( k_tools_mode
) return;
324 if( skaterift
.op
== k_async_op_clientloading
) return;
325 if( world_static
.load_state
== k_world_loader_preload
)
326 skaterift_change_client_world_preupdate();
329 skateshop_autostart_loading();
334 if( skaterift
.activity
& (k_skaterift_replay
|k_skaterift_menu
|
335 k_skaterift_respawning
) ) {
339 vg_slewf( &skaterift
.time_rate
, target
, vg
.time_frame_delta
* (1.0f
/0.3f
) );
340 vg
.time_rate
= vg_smoothstepf( skaterift
.time_rate
);
342 player__pre_update();
343 world_entity_focus_preupdate();
344 skaterift_replay_pre_update();
345 remote_sfx_pre_update();
347 world_update( world_current_instance(), localplayer
.rb
.co
);
348 audio_ambient_sprites_update( world_current_instance(), localplayer
.rb
.co
);
349 respawn_chooser_pre_update();
352 static void vg_fixed_update(void){
353 if( k_tools_mode
) return;
354 if( skaterift
.op
== k_async_op_clientloading
) return;
356 world_routes_fixedupdate( world_current_instance() );
358 vehicle_update_fixed();
361 static void vg_post_update(void){
362 if( k_tools_mode
) return;
363 if( skaterift
.op
== k_async_op_clientloading
) return;
365 player__post_update();
369 world_audio_sample_distances( localplayer
.rb
.co
, &sample_index
, &dist
);
372 vg_dsp
.echo_distances
[sample_index
] = dist
;
374 v3f ears
= { 1.0f
,0.0f
,0.0f
};
375 m3x3_mulv( skaterift
.cam
.transform
, ears
, ears
);
376 v3_copy( ears
, vg_audio
.external_listener_ears
);
377 v3_copy( skaterift
.cam
.transform
[3], vg_audio
.external_listener_pos
);
379 if( localplayer
.gate_waiting
){
380 m4x3_mulv( localplayer
.gate_waiting
->transport
,
381 vg_audio
.external_listener_pos
,
382 vg_audio
.external_listener_pos
);
385 v3_copy( localplayer
.rb
.v
, vg_audio
.external_lister_velocity
);
388 vehicle_update_post();
390 if( vg
.time
- skaterift
.last_autosave
> 20.0 ){
391 if( skaterift_autosave(1) ){
392 skaterift
.last_autosave
= vg
.time
;
399 * ---------------------------------------------------------------------------*/
401 static void vg_framebuffer_resize( int w
, int h
){
405 static void present_view_with_post_processing(void){
406 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
407 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
410 glDisable(GL_DEPTH_TEST
);
411 glBlendFunc(GL_ONE_MINUS_DST_ALPHA
, GL_DST_ALPHA
);
412 glBlendEquation(GL_FUNC_ADD
);
415 render_fb_inverse_ratio( gpipeline
.fb_main
, inverse
);
418 shader_blitblur_use();
419 shader_blitblur_uTexMain( 0 );
420 shader_blitblur_uTexMotion( 1 );
421 shader_blitblur_uBlurStrength( k_blur_strength
/
422 (vg
.time_frame_delta
*60.0) );
423 shader_blitblur_uInverseRatio( inverse
);
425 inverse
[0] -= 0.0001f
;
426 inverse
[1] -= 0.0001f
;
427 shader_blitblur_uClampUv( inverse
);
429 static f64 effect_time
= 133.0;
430 effect_time
+= vg
.time_frame_delta
;
431 shader_blitblur_uTime( effect_time
);
433 f32 glitch_strength
= 0.0f
;
434 if( skaterift
.activity
== k_skaterift_replay
){
435 glitch_strength
= 0.005f
;
436 if( skaterift
.replay_control
== k_replay_control_play
)
437 glitch_strength
= 0.005f
;
439 glitch_strength
+= 0.005f
*skaterift
.track_velocity
;
441 shader_blitblur_uGlitchStrength( glitch_strength
);
444 if( skaterift
.activity
== k_skaterift_menu
)
445 v2_muls( (v2f
){ 0.04f
, 0.001f
}, 1.0f
-skaterift
.time_rate
, override
);
448 shader_blitblur_uOverrideDir( override
);
450 render_fb_bind_texture( gpipeline
.fb_main
, 0, 0 );
451 render_fb_bind_texture( gpipeline
.fb_main
, 1, 1 );
455 shader_blit_uTexMain( 0 );
456 shader_blit_uInverseRatio( inverse
);
457 render_fb_bind_texture( gpipeline
.fb_main
, 0, 0 );
463 static void render_player_transparent(void){
464 static camera small_cam
; /* DOES NOT NEED TO BE STATIC BUT MINGW
467 m4x3_copy( skaterift
.cam
.transform
, small_cam
.transform
);
469 small_cam
.fov
= skaterift
.cam
.fov
;
470 small_cam
.nearz
= 0.05f
;
471 small_cam
.farz
= 60.0f
;
473 camera_update_view( &small_cam
);
474 camera_update_projection( &small_cam
);
475 camera_finalize( &small_cam
);
477 /* Draw player to window buffer and blend background ontop */
478 player__render( &small_cam
);
479 render_remote_players_tags( localplayer
.viewable_world
, &skaterift
.cam
);
482 static void render_scene(void){
484 glEnable( GL_DEPTH_TEST
);
486 world_instance
*view_world
= localplayer
.viewable_world
;
488 if( view_world
== NULL
){
489 glClearColor( 0.25f
, 0.25f
, 0.0f
, 1.0f
);
490 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
|GL_STENCIL_BUFFER_BIT
);
494 for( u32 i
=0; i
<vg_list_size(world_static
.instances
); i
++ ){
495 if( world_static
.instances
[i
].status
== k_world_status_loaded
){
496 world_prerender( &world_static
.instances
[i
] );
500 if( skaterift
.activity
== k_skaterift_respawning
){
501 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
502 glClearColor( 0.624f
, 0.659f
, 0.769f
, 0.0f
);
503 glClear( GL_COLOR_BUFFER_BIT
);
504 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
,
505 GL_COLOR_ATTACHMENT1
} );
506 view_world
= &world_static
.instances
[respawn_chooser
.world_id
];
507 render_world_override( view_world
);
508 render_world_routes( view_world
, &skaterift
.cam
, 1 );
511 render_world( view_world
, &skaterift
.cam
, 0 );
512 render_water_texture( view_world
, &skaterift
.cam
, 0 );
513 render_fb_bind( gpipeline
.fb_main
, 1 );
514 render_water_surface( view_world
, &skaterift
.cam
);
515 render_remote_players( view_world
, &skaterift
.cam
);
519 static void render_scene_gate_subview(void){
520 render_fb_bind( gpipeline
.fb_main
, 1 );
521 world_instance
*view_world
= localplayer
.viewable_world
;
522 if( !view_world
) return; /* ??? */
523 if( skaterift
.activity
== k_skaterift_respawning
) return;
526 if( localplayer
.gate_waiting
) depth
= 0;
527 render_world_gates( view_world
, &skaterift
.cam
, depth
);
530 static void skaterift_composite_maincamera(void){
531 camera_lerp( &localplayer
.cam
, &world_static
.focus_cam
,
532 vg_smoothstepf(world_static
.focus_strength
), &skaterift
.cam
);
534 if( skaterift
.activity
== k_skaterift_replay
){
535 skaterift_get_replay_camera( &skaterift
.cam
);
538 skaterift
.cam
.nearz
= 0.1f
;
539 skaterift
.cam
.farz
= 2100.0f
;
541 if( skaterift
.activity
== k_skaterift_respawning
){
542 camera_copy( &respawn_chooser
.cam
, &skaterift
.cam
);
543 skaterift
.cam
.nearz
= 4.0f
;
544 skaterift
.cam
.farz
= 3100.0f
;
547 camera_update_transform( &skaterift
.cam
);
549 if( skaterift
.activity
!= k_skaterift_respawning
){
550 if( localplayer
.gate_waiting
){
551 m3x3_mul( localplayer
.basis_gate
, skaterift
.cam
.transform
,
552 skaterift
.cam
.transform
);
555 m3x3_mul( localplayer
.basis
, skaterift
.cam
.transform
,
556 skaterift
.cam
.transform
);
560 camera_update_view( &skaterift
.cam
);
561 camera_update_projection( &skaterift
.cam
);
562 camera_finalize( &skaterift
.cam
);
565 static void render_main_game(void){
566 if( skaterift
.activity
== k_skaterift_replay
){
567 player__animate_from_replay( &skaterift
.replay
);
571 skaterift_record_frame( &skaterift
.replay
, 0 );
573 animate_remote_players();
574 player__pre_render();
576 skaterift_composite_maincamera();
578 /* --------------------------------------------------------------------- */
580 world_instance
*view_world
= localplayer
.viewable_world
;
581 if( (view_world
!= NULL
) && (skaterift
.activity
!= k_skaterift_respawning
) ){
582 render_world_cubemaps( view_world
);
584 ent_gate
*gate
= view_world
->rendering_gate
;
586 if( gate
->flags
& k_ent_gate_nonlocal
){
587 world_instance
*dest
= &world_static
.instances
[ gate
->target
];
588 render_world_cubemaps( dest
);
593 /* variable res target */
594 render_fb_bind( gpipeline
.fb_main
, 1 );
595 glClearColor( 0.0f
, 0.0f
, 0.0f
, 1.0f
);
596 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
|GL_STENCIL_BUFFER_BIT
);
599 glEnable( GL_DEPTH_TEST
);
601 /* full res target */
602 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
603 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
605 render_player_transparent(); /* needs to read the depth buffer before we fuck
606 it up with the oblique rendering inside the
609 /* continue with variable rate */
610 render_scene_gate_subview();
613 present_view_with_post_processing();
615 /* --------------------------------------------------------------------- */
617 /* capture the current resume frame at the very last point */
618 if( button_down( k_srbind_reset
) ){
619 if( skaterift
.activity
== k_skaterift_default
){
620 skaterift
.activity
= k_skaterift_replay
;
621 skaterift_record_frame( &skaterift
.replay
, 1 );
622 if( skaterift
.replay
.head
){
623 skaterift
.replay
.cursor
= skaterift
.replay
.head
->time
;
624 skaterift
.replay
.cursor_frame
= skaterift
.replay
.head
;
626 skaterift
.replay_control
= k_replay_control_scrub
;
631 static void vg_render(void){
633 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
634 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
635 glClearColor( 0.0f
, 0.0f
, 0.0f
, 0.0f
);
636 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
640 if( skaterift
.op
== k_async_op_clientloading
){
645 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
647 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
648 glDisable( GL_DEPTH_TEST
);
649 glDisable( GL_BLEND
);
651 glClearColor( 1.0f
, 0.0f
, 0.0f
, 0.0f
);
652 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
655 m4x4_copy( skaterift
.cam
.mtx
.pv
, vg
.pv
);
659 glDisable(GL_DEPTH_TEST
);
661 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
665 static void vg_gui(void){
666 if( skaterift
.op
== k_async_op_clientloading
) return;
669 if( skaterift
.activity
== k_skaterift_menu
){
670 glClear( GL_DEPTH_BUFFER_BIT
);
675 world_instance
*world
= world_current_instance();
677 skaterift_replay_imgui();
679 render_view_framebuffer_ui();
680 remote_player_network_imgui( vg
.pv
);
681 remote_players_imgui();
687 #include "skaterift_imgui_dev.c"