npcs and tutorial stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / font.h
diff --git a/font.h b/font.h
index b979732804a2fc6da21c1605f23bcb1ce895cb15..608c71d38c02c4953f37efeede7be00ebb7ed211 100644 (file)
--- a/font.h
+++ b/font.h
@@ -1,11 +1,11 @@
-#ifndef FONT_H
-#define FONT_H
-
+#pragma once
 #include "model.h"
 #include "entity.h"
-#include "camera.h"
+#include "vg/vg_camera.h"
 #include "shaders/model_font.h"
 #include "shaders/scene_font.h"
+#include "world_render.h"
+#include "depth_compare.h"
 
 enum efont_SRglyph{
    k_SRglyph_end           = 0x00, /* control characters */
@@ -55,7 +55,12 @@ enum efont_SRglyph{
    k_SRglyph_kbm_space     = 0xa1,
    k_SRglyph_kbm_return    = 0xa2,
    k_SRglyph_kbm_escape    = 0xa3,
-   k_SRglyph_kbm_mousemove = 0xa4
+   k_SRglyph_kbm_mousemove = 0xa4,
+   k_SRglyph_vg_ret        = 0xa5,
+   k_SRglyph_vg_link       = 0xa6,
+   k_SRglyph_vg_square     = 0xa7,
+   k_SRglyph_vg_triangle   = 0xa8,
+   k_SRglyph_vg_circle     = 0xa9
 };
 
 typedef struct font3d font3d;
@@ -69,18 +74,17 @@ struct font3d{
                  glyphs;
 };
 
-static void font3d_load( font3d *font, const char *mdl_path, void *alloc )
-{
+static void font3d_load( font3d *font, const char *mdl_path, void *alloc ){
    mdl_open( &font->mdl, mdl_path, alloc );
    mdl_load_metadata_block( &font->mdl, alloc );
 
    vg_linear_clear( vg_mem.scratch );
    mdl_array_ptr fonts;
-   mdl_load_array( &font->mdl, &fonts, "ent_font", vg_mem.scratch );
+   MDL_LOAD_ARRAY( &font->mdl, &fonts, ent_font, vg_mem.scratch );
    font->info = *((ent_font *)mdl_arritm(&fonts,0));
 
-   mdl_load_array( &font->mdl, &font->font_variants, "ent_font_variant", alloc);
-   mdl_load_array( &font->mdl, &font->glyphs, "ent_glyph", alloc );
+   MDL_LOAD_ARRAY( &font->mdl, &font->font_variants, ent_font_variant, alloc);
+   MDL_LOAD_ARRAY( &font->mdl, &font->glyphs, ent_glyph, alloc );
 
    vg_linear_clear( vg_mem.scratch );
 
@@ -91,7 +95,7 @@ static void font3d_load( font3d *font, const char *mdl_path, void *alloc )
    void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
    mdl_fread_pack_file( &font->mdl, &tex0->file, data );
 
-   mdl_async_load_glmesh( &font->mdl, &font->mesh );
+   mdl_async_load_glmesh( &font->mdl, &font->mesh, NULL );
    vg_tex2d_load_qoi_async( data, tex0->file.pack_size, 
                             VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
                             &font->texture );
@@ -99,14 +103,7 @@ static void font3d_load( font3d *font, const char *mdl_path, void *alloc )
    mdl_close( &font->mdl );
 }
 
-static void font3d_init(void)
-{
-   shader_model_font_register();
-   shader_scene_font_register();
-}
-
-static u32 font3d_find_variant( font3d *font, const char *name )
-{
+static u32 font3d_find_variant( font3d *font, const char *name ){
    for( u32 i=0; i<mdl_arrcount( &font->font_variants ); i ++ ){
       ent_font_variant *variant = mdl_arritm( &font->font_variants, i );
 
@@ -118,20 +115,64 @@ static u32 font3d_find_variant( font3d *font, const char *name )
    return 0;
 }
 
-static void font3d_bind( font3d *font, camera *cam )
-{
-   shader_model_font_use();
-   shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
-   shader_model_font_uTexMain( 1 );
-       glActiveTexture( GL_TEXTURE1 );
-       glBindTexture( GL_TEXTURE_2D, font->texture );
+struct _font3d_render{
+   v4f offset;
+   font3d *font;
+   u32 variant_id;
 
-   shader_model_font_uPv( cam->mtx.pv );
+   enum font_shader {
+      k_font_shader_default,
+      k_font_shader_world
+   }
+   shader;
+}
+static gui_font3d;
+
+/*
+ * world can be null if not using world shader 
+ */
+static void font3d_bind( font3d *font, enum font_shader shader, 
+                         int depth_compare, world_instance *world,
+                         vg_camera *cam ){
+   gui_font3d.shader = shader;
+   gui_font3d.font = font;
+   glActiveTexture( GL_TEXTURE1 );
+   glBindTexture( GL_TEXTURE_2D, font->texture );
+
+   if( shader == k_font_shader_default )
+   {
+      shader_model_font_use();
+      shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
+      shader_model_font_uTexMain( 1 );
+      shader_model_font_uDepthMode( depth_compare );
+
+      if( depth_compare ){
+         depth_compare_bind( 
+            shader_model_font_uTexSceneDepth,
+            shader_model_font_uInverseRatioDepth,
+            shader_model_font_uInverseRatioMain, cam );
+      }
+
+      shader_model_font_uPv( cam->mtx.pv );
+   }
+   else if( shader == k_font_shader_world )
+   {
+      shader_scene_font_use();
+      shader_scene_font_uTexGarbage(0);
+      shader_scene_font_uTexMain(1);
+
+      shader_scene_font_uPv( skaterift.cam.mtx.pv );
+      shader_scene_font_uTime( vg.time );
+
+      WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_font );
+
+      bind_terrain_noise();
+      shader_scene_font_uCamera( skaterift.cam.transform[3] );
+   }
    mesh_bind( &font->mesh );
 }
 
-static ent_glyph *font3d_glyph( font3d *font, u32 variant_id, u32 utf32 )
-{
+static ent_glyph *font3d_glyph( font3d *font, u32 variant_id, u32 utf32 ){
    if( utf32 < font->info.glyph_utf32_base ) return NULL;
    if( utf32 >= font->info.glyph_utf32_base+font->info.glyph_count) return NULL;
 
@@ -141,94 +182,88 @@ static ent_glyph *font3d_glyph( font3d *font, u32 variant_id, u32 utf32 )
    return mdl_arritm( &font->glyphs, index );
 }
 
-struct font3d_render{
-   v4f offset;
-   font3d *font;
-   const u8 *u8pch;
-   u32 variant_id;
-
-   enum font_shader {
-      k_font_shader_default,
-      k_font_shader_world
-   }
-   shader;
-};
-
-static 
-void font3d_begin( const char *text,
-                   camera *cam, m4x3f transform, struct font3d_render *render )
-{
-   v4_copy( (v4f){0.0f,0.0f,0.0f,1.0f}, render->offset );
+static void font3d_set_transform( const char *text,
+                                  vg_camera *cam, m4x3f transform ){
+   v4_copy( (v4f){0.0f,0.0f,0.0f,1.0f}, gui_font3d.offset );
 
    m4x4f prev_mtx;
    m4x3_expand( transform, prev_mtx );
    m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
 
-   if( render->shader == k_font_shader_default ){
+   if( gui_font3d.shader == k_font_shader_default ){
       shader_model_font_uPvmPrev( prev_mtx );
       shader_model_font_uMdl( transform );
    }
-   else if( render->shader == k_font_shader_world ){
+   else if( gui_font3d.shader == k_font_shader_world ){
       shader_scene_font_uPvmPrev( prev_mtx );
       shader_scene_font_uMdl( transform );
    }
-
-   render->u8pch = (u8*)text;
 }
 
-static void font3d_setoffset( struct font3d_render *render, v4f offset ){
-   if( render->shader == k_font_shader_default )
+static void font3d_setoffset( v4f offset ){
+   if( gui_font3d.shader == k_font_shader_default )
       shader_model_font_uOffset( offset );
-   else if( render->shader == k_font_shader_world )
+   else if( gui_font3d.shader == k_font_shader_world )
       shader_scene_font_uOffset( offset );
 }
 
-static void font3d_draw( struct font3d_render *render )
-{
+static void font3d_setcolour( v4f colour ){
+   if( gui_font3d.shader == k_font_shader_default )
+      shader_model_font_uColour( colour );
+#if 0
+   else if( gui_font3d.shader == k_font_shader_world )
+      shader_scene_font_uColour( colour );
+#endif
+}
+
+static void font3d_draw( const char *text ){
+   u8 *u8pch = (u8*)text;
+
    u32 max_chars = 512;
-   while( render->u8pch && max_chars ){
+   while( u8pch && max_chars ){
       max_chars --;
 
-      u32 c0 = *render->u8pch, c1;
-      render->u8pch ++;
+      u32 c0 = *u8pch, c1;
+      u8pch ++;
 
       if( !c0 ) break;
 
-      ent_glyph *glyph0 = font3d_glyph( render->font, render->variant_id, c0 ),
+      ent_glyph *glyph0 = font3d_glyph( gui_font3d.font, 
+                                        gui_font3d.variant_id, c0 ),
                 *glyph1 = NULL;
 
       /* multibyte characters */
       if( c0 >= 1 && c0 < k_SRglyph_ascii_min ){
-         c1 = *render->u8pch;
+         c1 = *u8pch;
          if( !c1 ) break;
-         glyph1 = font3d_glyph( render->font, render->variant_id, c1 );
+         glyph1 = font3d_glyph( gui_font3d.font, gui_font3d.variant_id, c1 );
       }
 
       if( c0 == k_SRglyph_ctrl_variant ){
-         render->variant_id = c1;
-         render->u8pch ++;
+         gui_font3d.variant_id = c1;
+         u8pch ++;
          continue;
       }
       else if( c0 == k_SRglyph_ctrl_size ){
-         render->offset[3] = (float)c1 * (1.0f/255.0f);
-         render->u8pch ++;
+         gui_font3d.offset[3] = (float)c1 * (1.0f/255.0f);
+         u8pch ++;
          continue;
       }
       else if( c0 == k_SRglyph_ctrl_baseline ){
-         render->offset[1] = 0.0f;
+         gui_font3d.offset[1] = 0.0f;
          continue;
       }
       else if( c0 == k_SRglyph_ctrl_center ){
          if( glyph1 ){
-            float diff = glyph1->size[1] - glyph1->size[1]*render->offset[3];
-            render->offset[1] = diff * 0.5f;
+            float diff = glyph1->size[1] - glyph1->size[1]*gui_font3d.offset[3];
+            gui_font3d.offset[1] = diff * 0.5f;
          }
          continue;
       }
       else if( c0 == k_SRglyph_ctrl_top ){
          if( glyph1 ){
-            float diff = glyph1->size[1] - glyph1->size[1]*render->offset[3];
-            render->offset[1] = diff;
+            float diff = glyph1->size[1] - glyph1->size[1]*gui_font3d.offset[3];
+            gui_font3d.offset[1] = diff;
          }
          continue;
       }
@@ -238,46 +273,34 @@ static void font3d_draw( struct font3d_render *render )
       if( glyph1 && (c0 == k_SRglyph_mod_square || c0 == k_SRglyph_mod_circle)){
          v4f v0;
          v2_sub( glyph0->size, glyph1->size, v0 );
-         v2_muladds( render->offset, v0, -0.5f, v0 );
-         v0[2] = render->offset[2];
-         v0[3] = render->offset[3];
+         v2_muladds( gui_font3d.offset, v0, -0.5f, v0 );
+         v0[2] = gui_font3d.offset[2];
+         v0[3] = gui_font3d.offset[3];
 
-         font3d_setoffset( render, v0 );
+         font3d_setoffset( v0 );
          mesh_drawn( glyph0->indice_start, glyph0->indice_count );
          continue;
       }
       else{
-         font3d_setoffset( render, render->offset );
+         font3d_setoffset( gui_font3d.offset );
          mesh_drawn( glyph0->indice_start, glyph0->indice_count );
       }
 
-      render->offset[0] += glyph0->size[0]*render->offset[3];
+      gui_font3d.offset[0] += glyph0->size[0]*gui_font3d.offset[3];
    }
-
-   render->u8pch = NULL;
 }
 
-static 
-float font3d_simple_draw( font3d *font, u32 variant_id, enum font_shader shader,
-                          const char *text, 
-                          camera *cam, m4x3f transform )
-{
+static f32 font3d_simple_draw( u32 variant_id, const char *text, 
+                               vg_camera *cam, m4x3f transform ){
    if( !text ) return 0.0f;
 
-   struct font3d_render render = {
-      .font = font,
-      .variant_id = variant_id,
-      .shader = shader,
-   };
-
-   font3d_begin( text, cam, transform, &render );
-   font3d_draw( &render );
-   return render.offset[0];
+   gui_font3d.variant_id = variant_id;
+   font3d_set_transform( text, cam, transform );
+   font3d_draw( text );
+   return gui_font3d.offset[0];
 }
 
-static 
-float font3d_string_width( font3d *font, u32 variant_id, const char *text )
-{
+static f32 font3d_string_width( u32 variant_id, const char *text ){
    if( !text ) return 0.0f;
    float width = 0.0f;
 
@@ -286,7 +309,7 @@ float font3d_string_width( font3d *font, u32 variant_id, const char *text )
       u32 c = buf[i];
       if(!c) break;
 
-      ent_glyph *glyph = font3d_glyph( font, variant_id, c );
+      ent_glyph *glyph = font3d_glyph( gui_font3d.font, variant_id, c );
       if( !glyph ) continue;
 
       width += glyph->size[0];
@@ -294,5 +317,3 @@ float font3d_string_width( font3d *font, u32 variant_id, const char *text )
 
    return width;
 }
-
-#endif /* FONT_H */