new menu start
[carveJwlIkooP6JGAAIwe30JlM.git] / menu.h
diff --git a/menu.h b/menu.h
index 7a008468802d1775848fdf358c48ec8002db9a41..f15b26b76836a105718893d5affc79ceb27ecf07 100644 (file)
--- a/menu.h
+++ b/menu.h
@@ -1,6 +1,177 @@
 #ifndef MENU_H
 #define MENU_H
 
+#include "common.h"
+#include "model.h"
+#include "world_render.h"
+#include "player.h"
+#include "conf.h"
+#include "shaders/model_menu.h"
+
+struct {
+   int active;
+   f32 factive;
+   int disable_open;
+
+   u32 page; /* current page index */
+   ent_menuitem *loc;
+   ent_camera   *cam;
+   camera        view;
+
+   mdl_context model;
+   GLuint texture;
+   glmesh mesh;
+
+   mdl_array_ptr items, markers, cameras;
+}
+static menu;
+
+static void menu_init(void)
+{
+   void *alloc = vg_mem.rtmemory;
+
+   mdl_open( &menu.model, "models/rs_menu.mdl", alloc );
+   mdl_load_metadata_block( &menu.model, alloc );
+
+   vg_linear_clear( vg_mem.scratch );
+
+   mdl_load_array( &menu.model, &menu.items,   "ent_menuitem", alloc );
+   mdl_load_array( &menu.model, &menu.markers, "ent_marker", alloc );
+   mdl_load_array( &menu.model, &menu.cameras, "ent_camera", alloc );
+
+   vg_linear_clear( vg_mem.scratch );
+
+   if( !mdl_arrcount( &menu.model.textures ) )
+      vg_fatal_error( "No texture in menu file" );
+
+   mdl_texture *tex0 = mdl_arritm( &menu.model.textures, 0 );
+   void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
+   mdl_fread_pack_file( &menu.model, &tex0->file, data );
+
+   mdl_async_load_glmesh( &menu.model, &menu.mesh );
+   vg_tex2d_load_qoi_async( data, tex0->file.pack_size, 
+                            VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
+                            &menu.texture );
+
+   mdl_close( &menu.model );
+   shader_model_menu_register();
+}
+
+static void menu_open_page( const char *name )
+{
+   u32 hash = vg_strdjb2( name );
+   for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
+      ent_menuitem *item = mdl_arritm( &menu.items, i );
+      
+      if( item->type == k_ent_menuitem_type_page ){
+         if( mdl_pstreq( &menu.model, item->page.pstr_name, name, hash ) ){
+            menu.page = __builtin_ctz( item->groups );
+            vg_info( "menu page: %u\n", menu.page );
+
+            if( item->page.id_entrypoint ){
+               u32 id = mdl_entity_id_id( item->page.id_entrypoint );
+               menu.loc = mdl_arritm( &menu.items, id );
+            }
+
+            if( item->page.id_viewpoint ){
+               u32 id = mdl_entity_id_id( item->page.id_viewpoint );
+               menu.cam = mdl_arritm( &menu.cameras, id );
+            }
+
+            return;
+         }
+      }
+   }
+}
+
+static void menu_update(void)
+{
+   if( button_down( k_srbind_mopen ) ){
+      if( menu.active ){
+         menu.active = 0;
+      }
+      else{
+         if( !menu.disable_open ){
+            menu.active = 1;
+            menu_open_page( "Main Menu" );
+         }
+      }
+   }
+
+   menu.factive = vg_lerpf( menu.factive, menu.active, 
+                            vg.time_frame_delta * 6.0f );
+
+   if( menu.factive > 0.01f ){
+      
+   }
+}
+
+VG_STATIC void menu_render_bg(void)
+{
+   glEnable(GL_BLEND);
+   glDisable(GL_DEPTH_TEST);
+   glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
+   glBlendEquation(GL_FUNC_ADD);
+
+   shader_blitcolour_use();
+   shader_blitcolour_uColour( (v4f){ 0.1f, 0.1f, 0.3f, menu.factive*0.5f } );
+   render_fsquad();
+}
+
+VG_STATIC void menu_render_fg(void)
+{
+   glEnable( GL_DEPTH_TEST );
+   glDisable( GL_BLEND );
+
+   if( menu.cam ){
+      menu.view.fov = menu.cam->fov;
+      menu.view.farz = 150.0f;
+      menu.view.nearz = 0.01f;
+      v3_copy( menu.cam->transform.co, menu.view.pos );
+
+      v3f v0;
+      mdl_transform_vector( &menu.cam->transform, (v3f){0.0f,-1.0f,0.0f}, v0 );
+      player_vector_angles( menu.view.angles, v0, 1.0f, 0.0f );
+      camera_update_transform( &menu.view );
+      camera_update_view( &menu.view );
+      camera_update_projection( &menu.view );
+      camera_finalize( &menu.view );
+   }
+   else return;
+
+   shader_model_menu_use();
+   shader_model_menu_uColour( (v4f){ 1.0f,1.0f,1.0f,1.0f} );
+   shader_model_menu_uTexMain( 1 );
+   glActiveTexture( GL_TEXTURE1 );
+   glBindTexture( GL_TEXTURE_2D, menu.texture );
+   shader_model_menu_uPv( menu.view.mtx.pv );
+   shader_model_menu_uPvmPrev( menu.view.mtx_prev.pv );
+
+   mesh_bind( &menu.mesh );
+
+   for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
+      ent_menuitem *item = mdl_arritm( &menu.items, i );
+
+      if(   item->type == k_ent_menuitem_type_page ) continue;
+      if( !(item->groups & (0x1 << menu.page)) ) continue;
+
+      m4x3f mmdl;
+      mdl_transform_m4x3( &item->transform, mmdl );
+      shader_model_menu_uMdl( mmdl );
+
+      for( u32 j=0; j<item->submesh_count; j++ ){
+         u32 index = item->submesh_start + j;
+         mdl_draw_submesh( mdl_arritm( &menu.model.submeshs, index ));
+      }
+   }
+}
+
+#endif /* MENU_H */
+
+#if 0
+#ifndef MENU_H
+#define MENU_H
+
 #include "common.h"
 #include "model.h"
 #include "world_render.h"
@@ -9,7 +180,6 @@
 
 #include "shaders/model_menu.h"
 #include "vg_steam_friends.h"
-#include "submodules/tinydir/tinydir.h"
 
 VG_STATIC mdl_context menu_model;
 VG_STATIC mdl_array_ptr   menu_markers;
@@ -30,14 +200,16 @@ VG_STATIC int         menu_enabled(void){ return cl_menu; }
 
 VG_STATIC const char *playermodels[] = { "ch_new", "ch_jordan", "ch_outlaw" };
 
-vg_tex2d tex_menu = { .path = "textures/menu.qoi",.flags = VG_TEXTURE_NEAREST };
+GLuint tex_menu;
 
+#if 0
 VG_STATIC struct input_binding input_menu_h,
                                input_menu_v,
                                input_menu_press,
                                input_menu_back,
                                input_menu_toggle,
                                input_menu_toggle_kbm;
+#endif
 
 VG_STATIC void menu_btn_quit( int event );
 VG_STATIC void menu_btn_skater( int event );
@@ -107,10 +279,11 @@ VG_STATIC int menu_vis( struct menu_btn_userdata ud )
       return 0;
 }
 
+#if 0
 VG_STATIC int menu_controller( struct menu_btn_userdata ud )
 {
    if( (game_menu.page & (k_menu_page_main|k_menu_page_settings)) 
-         && (ud.i == menu_display_controller) )
+         && (ud.i == steam_display_controller) )
       return 1;
    return 0;
 }
@@ -118,10 +291,11 @@ VG_STATIC int menu_controller( struct menu_btn_userdata ud )
 VG_STATIC int menu_controller_inf( struct menu_btn_userdata ud )
 {
    if( (game_menu.page & k_menu_page_settings) 
-        && (ud.i == menu_display_controller) )
+        && (ud.i == steam_display_controller) )
       return 1;
    return 0;
 }
+#endif
 
 struct menu_button
 {
@@ -218,16 +392,18 @@ VG_STATIC menu_buttons[] =
 {
    "res_info", menu_vis, {k_menu_page_settings},
 },
-{ "ctr_xbox",        menu_controller_inf, {k_menu_controller_type_xbox}},
-{ "ctr_xbox_text",   menu_controller_inf, {k_menu_controller_type_xbox}},
-{ "ctr_steam",       menu_controller_inf, {k_menu_controller_type_steam}},
-{ "ctr_steam_text",  menu_controller_inf, {k_menu_controller_type_steam}},
-{ "ctr_deck",        menu_controller_inf, {k_menu_controller_type_steam_deck}},
-{ "ctr_deck_text",   menu_controller_inf, {k_menu_controller_type_steam_deck}},
-{ "ctr_ps",          menu_controller_inf, {k_menu_controller_type_playstation}},
-{ "ctr_ps_text",     menu_controller_inf, {k_menu_controller_type_playstation}},
-{ "ctr_kbm",         menu_controller_inf, {k_menu_controller_type_keyboard}},
-{ "ctr_kbm_text",    menu_controller_inf, {k_menu_controller_type_keyboard}},
+#if 0
+{ "ctr_xbox",       menu_controller_inf, {k_steam_controller_type_xbox}},
+{ "ctr_xbox_text",  menu_controller_inf, {k_steam_controller_type_xbox}},
+{ "ctr_steam",      menu_controller_inf, {k_steam_controller_type_steam}},
+{ "ctr_steam_text", menu_controller_inf, {k_steam_controller_type_steam}},
+{ "ctr_deck",       menu_controller_inf, {k_steam_controller_type_steam_deck}},
+{ "ctr_deck_text",  menu_controller_inf, {k_steam_controller_type_steam_deck}},
+{ "ctr_ps",         menu_controller_inf, {k_steam_controller_type_playstation}},
+{ "ctr_ps_text",    menu_controller_inf, {k_steam_controller_type_playstation}},
+{ "ctr_kbm",        menu_controller_inf, {k_steam_controller_type_keyboard}},
+{ "ctr_kbm_text",   menu_controller_inf, {k_steam_controller_type_keyboard}},
+#endif
 {
    "text_paused", menu_vis, {k_menu_page_main} 
 },
@@ -243,10 +419,9 @@ VG_STATIC int menu_get_loc( const char *loc )
    return 0;
 }
 
-VG_STATIC int __respawn( int argc, const char *argv[] );
 VG_STATIC void menu_btn_reset( int event )
 {
-   __respawn(0,NULL);
+   localplayer_cmd_respawn( 0, NULL );
    cl_menu_go_away = 1;
    game_menu.page = 0;
 }
@@ -300,7 +475,8 @@ VG_STATIC void menu_btn_map( int event )
          struct menu_map_file *mf = &game_menu.maps_list[ game_menu.map_count ];
          
          vg_strncpy( file.name, mf->name, 
-                                vg_list_size(game_menu.maps_list[0].name)-1 );
+                                vg_list_size(game_menu.maps_list[0].name),
+                                k_strncpy_always_add_null );
 
          game_menu.map_count ++;
          if( game_menu.map_count == vg_list_size(game_menu.maps_list) )
@@ -315,6 +491,7 @@ VG_STATIC void menu_btn_map( int event )
 
 VG_STATIC void menu_crap_ui(void)
 {
+#if 0
    if( cl_menu && (game_menu.page == k_menu_page_map) ){
       ui_rect box;
       box[0] = vg.window_x/2 - 150;
@@ -343,6 +520,7 @@ VG_STATIC void menu_crap_ui(void)
          }
       }
    }
+#endif
 }
 
 VG_STATIC void steam_on_game_overlay( CallbackMsg_t *msg )
@@ -359,13 +537,7 @@ VG_STATIC void steam_on_game_overlay( CallbackMsg_t *msg )
 
 VG_STATIC void menu_init(void)
 {
-   vg_create_unnamed_input( &input_menu_h, k_input_type_axis );
-   vg_create_unnamed_input( &input_menu_v, k_input_type_axis );
-   vg_create_unnamed_input( &input_menu_back, k_input_type_button );
-   vg_create_unnamed_input( &input_menu_press, k_input_type_button );
-   vg_create_unnamed_input( &input_menu_toggle, k_input_type_button );
-   vg_create_unnamed_input( &input_menu_toggle_kbm, k_input_type_button );
-
+#if 0
    vg_apply_bind_str( &input_menu_h, "", "gp-ls-h" );
    vg_apply_bind_str( &input_menu_h, "+", "right" );
    vg_apply_bind_str( &input_menu_h, "-", "left" );
@@ -378,22 +550,21 @@ VG_STATIC void menu_init(void)
    vg_apply_bind_str( &input_menu_back, "", "\2escape" );
    vg_apply_bind_str( &input_menu_toggle_kbm, "", "\2escape" );
    vg_apply_bind_str( &input_menu_toggle, "", "\2gp-menu" );
+#endif
 
    vg_linear_clear( vg_mem.scratch );
 
    mdl_open( &menu_model, "models/rs_menu.mdl", vg_mem.rtmemory );
    mdl_load_metadata_block( &menu_model, vg_mem.rtmemory );
-   mdl_load_mesh_block( &menu_model, vg_mem.scratch );
    mdl_load_array( &menu_model, &menu_markers, "ent_marker", vg_mem.rtmemory );
    //mdl_invert_uv_coordinates( &menu_model );
+   mdl_async_load_glmesh( &menu_model, &menu_glmesh );
    mdl_close( &menu_model );
 
-   vg_acquire_thread_sync();
-   {
-      mdl_unpack_glmesh( &menu_model, &menu_glmesh );
-      vg_tex2d_init( (vg_tex2d *[]){ &tex_menu }, 1 );
-   }
-   vg_release_thread_sync();
+   vg_tex2d_load_qoi_async_file( "textures/menu.qoi", 
+                                 VG_TEX2D_CLAMP|VG_TEX2D_NEAREST, 
+                                 &tex_menu );
+
 
    for( int i=0; i<vg_list_size(menu_buttons); i++ ){
       struct menu_button *btn = &menu_buttons[i];
@@ -401,7 +572,7 @@ VG_STATIC void menu_init(void)
 
       if( !btn->mesh ){
          vg_info( "info: %s\n", btn->name );
-         vg_fatal_exit_loop( "Menu programming error" );
+         vg_fatal_error( "Menu programming error" );
       }
    }
 
@@ -431,6 +602,7 @@ VG_STATIC void menu_init(void)
 
 VG_STATIC void menu_run_directional(void)
 {
+#if 0
    struct menu_button *btn = &menu_buttons[ game_menu.loc ];
 
    if( vg_input_button_down( &input_menu_press ) ){
@@ -466,11 +638,15 @@ VG_STATIC void menu_run_directional(void)
          }
       }
    }
+#endif
 }
 
 VG_STATIC int menu_page_should_backout(void)
 {
+   return 0;
+#if 0
    return vg_input_button_down( &input_menu_back );
+#endif
 }
 
 VG_STATIC void menu_close(void)
@@ -500,6 +676,7 @@ VG_STATIC void menu_page_map(void)
    }
 
    if( game_menu.map_count > 0 ){
+#if 0 
       float v = input_menu_v.axis.value;
       if( (fabsf(v) > 0.7f) && (menu_input_cooldown <= 0.0f) ){
          audio_lock();
@@ -535,6 +712,7 @@ VG_STATIC void menu_page_map(void)
          menu_close();
 #endif
       }
+#endif
    }
 
    menu_fov_target = 80.0f;
@@ -554,6 +732,7 @@ VG_STATIC void menu_page_quit(void)
 void temp_update_playermodel(void);
 VG_STATIC void menu_page_skater(void)
 {
+#if 0
    float h = input_menu_h.axis.value;
    menu_fov_target = 97.0f;
 
@@ -597,19 +776,22 @@ VG_STATIC void menu_page_skater(void)
 
       temp_update_playermodel();
    }
+#endif
 }
 
 VG_STATIC void menu_slider( float *value, int set_value,
                             mdl_mesh *slider, v3f co_min, v3f co_max )
 {
+#if 0
    if( set_value ){
       float h = input_menu_h.axis.value;
       if( fabsf(h) > 0.04f )
-         *value += h * vg.frame_delta;
+         *value += h * vg.time_frame_delta;
       *value = vg_clampf( *value, 0.0f, 1.0f );
    }
 
    v3_lerp( co_min, co_max, *value, slider->transform.co );
+#endif
 }
 
 VG_STATIC void menu_page_settings(void)
@@ -641,17 +823,20 @@ VG_STATIC void menu_page_settings(void)
    }
 }
 
-player_instance *tmp_localplayer(void);
-
 VG_STATIC void menu_update(void)
 {
+#if 0
    vg_input_update( 1, &input_menu_h );
    vg_input_update( 1, &input_menu_v );
    vg_input_update( 1, &input_menu_back );
    vg_input_update( 1, &input_menu_press );
    vg_input_update( 1, &input_menu_toggle );
    vg_input_update( 1, &input_menu_toggle_kbm );
+#endif
+   return;
 
+
+#if 0
    int toggle_gp = vg_input_button_down( &input_menu_toggle ),
        toggle_kb = vg_input_button_down( &input_menu_toggle_kbm ),
        wait_for_a_sec = 0;
@@ -688,7 +873,7 @@ VG_STATIC void menu_update(void)
 
    /* Base */
    {
-      player_instance *player = tmp_localplayer();
+      player_instance *player = &localplayer;
       struct player_avatar *av = player->playeravatar;
       
       v3f center_rough;
@@ -732,7 +917,7 @@ VG_STATIC void menu_update(void)
       m3x3_mul( player->basis, menu_mdl_mtx, menu_mdl_mtx );
 
       menu_smooth_fov = vg_lerpf( menu_smooth_fov, menu_fov_target, 
-                                  vg.frame_delta * 8.2f );
+                                  vg.time_frame_delta * 8.2f );
    }
 
    /* Extra */
@@ -743,7 +928,7 @@ VG_STATIC void menu_update(void)
 
       float y = atan2f( delta[0], delta[2] ),
             p = -sinf(delta[1]),
-            dt = vg.frame_delta;
+            dt = vg.time_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 );
@@ -753,7 +938,7 @@ VG_STATIC void menu_update(void)
       menu_camera_angles[0] = fmodf( menu_camera_angles[0], VG_TAUf );
    }
 
-   float dt = vg.frame_delta * 6.0f;
+   float dt = vg.time_frame_delta * 6.0f;
    menu_opacity = vg_lerpf( menu_opacity, cl_menu&&!cl_menu_go_away, dt );
 
    if( menu_opacity <= 0.01f ){
@@ -764,8 +949,9 @@ VG_STATIC void menu_update(void)
    vg.time_rate = 1.0-(double)menu_opacity;
 
    if( cl_menu ){
-      menu_input_cooldown -= vg.frame_delta;
+      menu_input_cooldown -= vg.time_frame_delta;
    }
+#endif
 }
 
 /* https://iquilezles.org/articles/functions/ */
@@ -797,7 +983,9 @@ VG_STATIC void menu_render_fg( camera *cam )
    shader_model_menu_use();
    shader_model_menu_uColour( (v4f){ 1.0f,1.0f,1.0f,1.0f} );
    shader_model_menu_uTexMain( 1 );
-   vg_tex2d_bind( &tex_menu, 1 );
+
+   glActiveTexture( GL_TEXTURE1 );
+   glBindTexture( GL_TEXTURE_2D, tex_menu );
 
    shader_model_menu_uPv( cam->mtx.pv );
    shader_model_menu_uPvmPrev( cam->mtx_prev.pv );
@@ -810,8 +998,8 @@ VG_STATIC void menu_render_fg( camera *cam )
             tsize1 = i==game_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 * 7.0f  );
+      btn->falpha = vg_lerpf( btn->falpha, talpha, vg.time_frame_delta * 14.0f);
+      btn->fsize  = vg_lerpf( btn->fsize,  tsize,  vg.time_frame_delta * 7.0f );
 
       /* Colour */
       v4f vselected = {0.95f*1.3f,0.45f*1.3f,0.095f*1.3f, 1.0f},
@@ -858,3 +1046,4 @@ VG_STATIC void menu_render_fg( camera *cam )
 }
 
 #endif /* MENU_H */
+#endif