new menu start
[carveJwlIkooP6JGAAIwe30JlM.git] / menu.h
diff --git a/menu.h b/menu.h
index 6612494a8492a899c8733878b68d32a4599459ec..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"
@@ -875,3 +1046,4 @@ VG_STATIC void menu_render_fg( camera *cam )
 }
 
 #endif /* MENU_H */
+#endif