X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=menu.h;h=02ab25242a74c75837f862f20eb51fb6a230bf7b;hb=47941822dae18a018c985847b052e70214a3ccc6;hp=e062b30d7c0f69b1745810c9aae9841f8874b24b;hpb=15beb60ade240af4e00b0d204f7e89a4d35dca36;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/menu.h b/menu.h index e062b30..02ab252 100644 --- a/menu.h +++ b/menu.h @@ -8,80 +8,366 @@ #include "shaders/menu.h" -static mdl_header *menu_model; -static glmesh menu_glmesh; -static v3f menu_cam_pos, - menu_target_cam_pos; -static v4f menu_cam_q = { 0.0f, 0.0f, 0.0f, 1.0f }, - menu_target_cam_q; -static m4x3f menu_cam, menu_cam_inv; -static float menu_opacity = 0.0f; +VG_STATIC mdl_context menu_model; +VG_STATIC glmesh menu_glmesh; +VG_STATIC m4x3f menu_mdl_mtx; +VG_STATIC float menu_opacity = 0.0f; +VG_STATIC float menu_input_cooldown = 0.0f; +VG_STATIC float menu_fov_target = 97.0f; +VG_STATIC v2f menu_extra_angles; -static void menu_init(void) +VG_STATIC int menu_loc = 1, + menu_loc_last = 1; +VG_STATIC u32 menu_page = 0; + +VG_STATIC int cl_menu = 0, + cl_menu_go_away = 0; + +VG_STATIC const char *playermodels[] = { "ch_new", "ch_jordan", "ch_outlaw" }; + +VG_STATIC void menu_btn_paused( int event ); +VG_STATIC void menu_btn_quit( int event ); +VG_STATIC void menu_btn_skater( int event ); +VG_STATIC void menu_btn_map( int event ); +VG_STATIC void menu_btn_map_a( int event ); +VG_STATIC void menu_btn_map_b( int event ); +VG_STATIC void menu_btn_map_c( int event ); +VG_STATIC void menu_btn_fuckoff( int event ); + +struct menu_button { - menu_model = mdl_load( "models/rs_menu.mdl" ); + const char *name; + u32 areas; - if( !menu_model ) - vg_fatal_exit_loop( "No menu model" ); + void (*fn_press)(int event); + int links[4]; + mdl_node *pnode; + + float falpha, fsize; +} +VG_STATIC menu_buttons[] = +{ +/*0*/{"text_paused", 1, menu_btn_paused }, +/*1*/{"text_quit", 9, menu_btn_quit, {3,-1,4,2}}, +/*2*/{"text_skater", 3, menu_btn_skater, {3,1,4,-1}}, +/*3*/{"text_map", 5, menu_btn_map, {-1,-1,2,-1}}, +/*4*/{"text_about_game", 1, NULL, {2,-1,-1,-1}}, +/*5*/{"skater_left", 2, NULL, {-1,-1,-1,-1}}, +/*6*/{"skater_right", 2, NULL, {-1,-1,-1,-1}}, +/*7*/{"map_a", 4, menu_btn_map_a,{8, -1, 9, 9 }}, +/*8*/{"map_b", 4, menu_btn_map_b,{-1,7,7,-1}}, +/*9*/{"map_c", 4, menu_btn_map_c,{7,7,-1,8}}, +/*a*/{"g_map", 5, NULL }, +/*b*/{"g_controls", 1, NULL }, +/*c*/{"text_quitty", 8, NULL }, +/*d*/{"text_yes", 8, menu_btn_fuckoff,{-1,-1,-1,-1} }, +}; + +VG_STATIC void menu_btn_map_a( int event ){} +VG_STATIC void menu_btn_map_b( int event ){} +VG_STATIC void menu_btn_map_c( int event ){} + +VG_STATIC void menu_btn_paused( int event ) +{ + +} + +VG_STATIC void menu_btn_fuckoff( int event ) +{ + glfwSetWindowShouldClose( vg.window, 1 ); +} + +VG_STATIC void menu_btn_quit( int event ) +{ + menu_page = 0x8; + menu_loc = 0xd; +} + +VG_STATIC void menu_btn_map( int event ) +{ + menu_page = 0x4; + menu_loc = 7; +} + +VG_STATIC void menu_btn_skater( int event ) +{ + menu_page = 0x2; +} + +VG_STATIC void menu_init(void) +{ + vg_linear_clear( vg_mem.scratch ); + + mdl_open( &menu_model, "models/rs_menu.mdl" ); + mdl_load_metadata( &menu_model, vg_mem.rtmemory ); + mdl_load_mesh_data( &menu_model, vg_mem.scratch ); + mdl_close( &menu_model ); vg_acquire_thread_sync(); - mdl_unpack_glmesh( menu_model, &menu_glmesh ); + mdl_unpack_glmesh( &menu_model, &menu_glmesh ); vg_release_thread_sync(); + for( int i=0; ipnode = mdl_node_from_name( &menu_model, btn->name ); + + if( !btn->pnode ) + vg_fatal_exit_loop( "Menu programming error" ); + } + shader_menu_register(); } -static void menu_update( int enabled ) +VG_STATIC void menu_run_directional(void) +{ + struct menu_button *btn = &menu_buttons[ menu_loc ]; + + if( vg_get_button_down( "jump" ) ) + { + if( btn->fn_press ) + { + audio_lock(); + audio_play_oneshot( &audio_ui[0], 1.0f ); + audio_unlock(); + + btn->fn_press( 1 ); + return; + } + } + + if( menu_input_cooldown <= 0.0f ) + { + v2f dir = { vg_get_axis( "lookh" ), vg_get_axis( "lookv" ) }; + + if( v2_length2( dir ) > 0.8f*0.8f ) + { + int idir = 0; + + if( fabsf(dir[0]) > fabsf(dir[1]) ) + { + if( dir[0] > 0.0f ) idir = 2; + else idir = 0; + } + else + { + if( dir[1] > 0.0f ) idir = 3; + else idir = 1; + } + + int link = btn->links[idir]; + + if( link != -1 ) + { + menu_loc_last = menu_loc; + menu_loc = link; + menu_input_cooldown = 0.25f; + } + } + } +} + +VG_STATIC int menu_page_should_backout(void) +{ + return vg_get_button_down( "break" ); +} + +VG_STATIC void menu_close(void) +{ + cl_menu_go_away = 1; + menu_page = 0; +} + +VG_STATIC void menu_page_main(void) +{ + if( menu_page_should_backout() ) + { + menu_close(); + return; + } + + menu_fov_target = 112.0f; + menu_run_directional(); +} + +VG_STATIC void menu_page_map(void) +{ + if( menu_page_should_backout() ) + { + menu_page = 1; + menu_loc = 3; + } + + menu_fov_target = 80.0f; + menu_run_directional(); +} + +VG_STATIC void menu_page_quit(void) { - static int enabled_last = 0; + if( menu_page_should_backout() ) + { + menu_page = 1; + menu_loc = 1; + } + + menu_fov_target = 90.0f; + menu_run_directional(); +} + +VG_STATIC void menu_page_skater(void) +{ + float h = vg_get_axis( "lookh" ); + menu_fov_target = 97.0f; + + if( menu_page_should_backout() ) + { + menu_page = 1; + menu_loc = 2; + return; + } + + if( (fabsf(h) > 0.7f) && (menu_input_cooldown <= 0.0f) ) + { + if( h < 0.0f ) + { + cl_playermdl_id --; + if( cl_playermdl_id < 0 ) + cl_playermdl_id = 2; + + menu_buttons[5].fsize = 0.4f; + menu_buttons[5].falpha = 1.0f; + + menu_input_cooldown = 0.25f; + } + else + { + cl_playermdl_id ++; + if( cl_playermdl_id > 2 ) + cl_playermdl_id = 0; + + menu_buttons[6].fsize = 0.4f; + menu_buttons[6].falpha = 1.0f; + + menu_input_cooldown = 0.25f; + } + } +} + +VG_STATIC void menu_update(void) +{ + if( vg_get_button_down( "menu" ) ) + { + if( cl_menu ) + { + cl_menu_go_away = 1; + menu_page = 0; + } + else + { + cl_menu = 1; + menu_page = 1; + } + } + + if( menu_page == 1 ) + menu_page_main(); + else if( menu_page == 2 ) + menu_page_skater(); + else if( menu_page == 4 ) + menu_page_map(); + else if( menu_page == 8 ) + menu_page_quit(); + + struct menu_button *btn = &menu_buttons[ menu_loc ]; - if( enabled && !enabled_last ) + v3f pos; + v2f angles; + + /* Base */ { - v3_copy( player.camera[3], menu_cam_pos ); - m3x3_q( player.camera, menu_cam_q ); + v3f lookdir; + v3f *mtx = player.mdl.sk.final_mtx[player.mdl.id_head]; + m3x3_mulv( mtx, (v3f){-1.0f,0.0f,0.0f}, lookdir ); + + lookdir[1] = 0.0f; + v3_normalize( lookdir ); + + v3f center_rough; - if( player.phys.on_board ) + if( player.is_dead ) { - v4f r90; - q_axis_angle( r90, player.phys.rb.up, VG_PIf*-0.5f ); - q_mul( r90, player.phys.rb.q, menu_target_cam_q ); - m4x3_mulv( player.phys.rb.to_world, (v3f){-1.0f,1.6f,0.0f}, - menu_target_cam_pos ); + v3_copy(player.mdl.ragdoll[ player.mdl.id_hip-1 ].rb.co, center_rough); } else { - v4f r180; - q_axis_angle( r180, player.phys.rb.up, VG_PIf ); - q_mul( r180, player.phys.rb.q, menu_target_cam_q ); - m4x3_mulv( player.phys.rb.to_world, (v3f){0.0f,1.6f,-1.0f}, - menu_target_cam_pos ); + v3_add( player.camera_pos, player.visual_transform[3], center_rough ); + v3_muls( center_rough, 0.5f, center_rough ); } - q_normalize( menu_target_cam_q ); - q_normalize( menu_cam_q ); - menu_opacity = 0.0f; + v3_muladds( center_rough, lookdir, 1.5f, pos ); + v3_add( (v3f){ 0.0f,0.8f,0.0f}, pos, pos ); + + angles[1] = 0.0f; + angles[0] = -atan2f( lookdir[0], lookdir[2] ); + + /* setup model matrix */ + v4f qmenu_mdl; + q_axis_angle( qmenu_mdl, (v3f){0.0f,1.0f,0.0f}, -angles[0] ); + + q_m3x3( qmenu_mdl, menu_mdl_mtx ); + v3_copy( center_rough, menu_mdl_mtx[3] ); } - if( enabled_last && !enabled ) + /* Extra */ { - m3x3_q( player.camera, menu_target_cam_q ); - v3_copy( player.camera[3], menu_target_cam_pos ); + v3f delta; + v3_sub( btn->pnode->co, (v3f){ 0.0f,1.5f,-1.5f }, delta ); + v3_normalize( delta ); + + float y = atan2f( delta[0], delta[2] ), + p = -sinf(delta[1]), + dt = vg.frame_delta; + + menu_extra_angles[0] = vg_lerpf( menu_extra_angles[0], y, dt ); + menu_extra_angles[1] = vg_lerpf( menu_extra_angles[1], p, dt ); + + v2_muladds( angles, menu_extra_angles, 0.8f, angles ); + angles[0] = fmodf( angles[0], VG_TAUf ); + } + + /* Update camera */ + { + camera_angles[0] = vg_alerpf( camera_angles[0], angles[0], menu_opacity ); + camera_angles[1] = vg_lerpf ( camera_angles[1], angles[1], menu_opacity ); + v3_lerp( camera_pos, pos, menu_opacity, camera_pos ); + camera_update(); } - float dt = vg.time_delta * 6.0f; + float dt = vg.frame_delta * 6.0f; + menu_opacity = vg_lerpf( menu_opacity, cl_menu&&!cl_menu_go_away, dt ); - q_nlerp( menu_cam_q, menu_target_cam_q, dt, menu_cam_q ); - v3_lerp( menu_cam_pos, menu_target_cam_pos, dt, menu_cam_pos ); + if( menu_opacity <= 0.01f ) + { + cl_menu = 0; + cl_menu_go_away = 0; + } - q_m3x3( menu_cam_q, menu_cam ); - v3_copy( menu_cam_pos, menu_cam[3] ); - m4x3_invert_affine( menu_cam, menu_cam_inv ); - menu_opacity = vg_lerpf( menu_opacity, enabled, dt ); + vg.time_rate = 1.0-(double)menu_opacity; - enabled_last = enabled; + if( cl_menu ) + { + menu_input_cooldown -= vg.frame_delta; + } } -static void menu_render( m4x4f projection ) +/* https://iquilezles.org/articles/functions/ */ +float expSustainedImpulse( float x, float f, float k ) +{ + float s = fmaxf(x-f,0.0f); + return fminf( x*x/(f*f), 1.0f+(2.0f/f)*s*expf(-k*s)); +} + +VG_STATIC void menu_render( m4x4f projection ) { glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); @@ -105,10 +391,43 @@ static void menu_render( m4x4f projection ) shader_menu_uPv( projection ); mesh_bind( &menu_glmesh ); - m4x3_identity( mtx ); - shader_menu_uMdl( mtx ); - mesh_draw( &menu_glmesh ); + for( int i=0; iareas & menu_page? 1.0f: 0.0f, + tsize1 = i==menu_loc? 0.07f: 0.0f, + tsize = tsize0+tsize1; + + btn->falpha = vg_lerpf( btn->falpha, talpha, vg.frame_delta * 14.0f ); + btn->fsize = vg_lerpf( btn->fsize, tsize, vg.frame_delta * 3.0f ); + /* Colour */ + v4f vselected = {0.95f*1.3f,0.45f*1.3f,0.095f*1.3f, 1.0f}, + vnormal = {1.0f,1.0f,1.0f, 1.0f}, + vcurrent; + + v4_lerp( vnormal, vselected, btn->falpha, vcurrent ); + shader_menu_uColour( vcurrent ); + + /* Create matrix */ + m4x3f mtx_size; + mdl_node_transform( btn->pnode, mtx ); + m4x3_mul( menu_mdl_mtx, mtx, mtx ); + m4x3_identity( mtx_size ); + m4x3_scale( mtx_size, expSustainedImpulse( btn->fsize, 0.5f, 8.7f) ); + m4x3_mul( mtx, mtx_size, mtx ); + shader_menu_uMdl( mtx ); + + for( int j=0; jpnode->submesh_count; j++ ) + { + mdl_submesh *sm = + &menu_model.submesh_buffer[ btn->pnode->submesh_start+j ]; + mdl_draw_submesh( sm ); + } + } + + /* for( int i=0; inode_count; i++ ) { mdl_node *pnode = mdl_node_from_id( menu_model, i ); @@ -119,17 +438,13 @@ static void menu_render( m4x4f projection ) mdl_submesh_from_id( menu_model, pnode->submesh_start+j ); mdl_node_transform( pnode, mtx ); - m4x3_mul( player.phys.rb.to_world, mtx, mtx ); + m4x3_mul( menu_mdl_mtx, mtx, mtx ); shader_menu_uMdl( mtx ); mdl_draw_submesh( sm ); } } -} - -static void menu_free(void *_) -{ - mesh_free( &menu_glmesh ); + */ } #endif /* MENU_H */