7 #include "shaders/model_font.h"
11 k_SRglyph_end
= 0, /* control characters */
12 k_SRglyph_ctrl_variant
= 1,
13 k_SRglyph_mod_circle
= 0x1e, /* surround and center next charater */
14 k_SRglyph_mod_square
= 0x1f, /* surround and center next character */
15 k_SRglyph_ascii_min
= 0x20, /* standard ascii */
16 k_SRglyph_ascii_max
= 0x7e,
17 k_SRglyph_ps4_square
= 0x7f,/* playstation buttons */
18 k_SRglyph_ps4_triangle
= 0x80,
19 k_SRglyph_ps4_circle
= 0x81,
20 k_SRglyph_ps4_cross
= 0x82,
21 k_SRglyph_xb1_x
= 0x83,/* xbox buttons */
22 k_SRglyph_xb1_y
= 0x84,
23 k_SRglyph_xb1_b
= 0x85,
24 k_SRglyph_xb1_a
= 0x86,
25 k_SRglyph_gen_ls
= 0x87,/* generic gamepad */
26 k_SRglyph_gen_lsh
= 0x88,
27 k_SRglyph_gen_lsv
= 0x89,
28 k_SRglyph_gen_lshv
= 0x8a,
29 k_SRglyph_gen_rs
= 0x8b,
30 k_SRglyph_gen_rsh
= 0x8c,
31 k_SRglyph_gen_rsv
= 0x8d,
32 k_SRglyph_gen_rshv
= 0x8e,
33 k_SRglyph_gen_lt
= 0x8f,
34 k_SRglyph_gen_rt
= 0x90,
35 k_SRglyph_gen_lb
= 0x91,
36 k_SRglyph_gen_rb
= 0x92,
37 k_SRglyph_gen_left
= 0x93,
38 k_SRglyph_gen_up
= 0x94,
39 k_SRglyph_gen_right
= 0x95,
40 k_SRglyph_gen_down
= 0x96,
41 k_SRglyph_gen_options
= 0x97,
42 k_SRglyph_gen_shareview
= 0x98,
43 k_SRglyph_kbm_m0
= 0x99,/* mouse */
44 k_SRglyph_kbm_m1
= 0x9a,
45 k_SRglyph_kbm_m01
= 0x9b,
46 k_SRglyph_kbm_m2
= 0x9c,
47 k_SRglyph_kbm_m2s
= 0x9d,
48 k_SRglyph_kbm_shift
= 0x9e,/* modifiers */
49 k_SRglyph_kbm_ctrl
= 0x9f,
50 k_SRglyph_kbm_alt
= 0xa0,
51 k_SRglyph_kbm_space
= 0xa1
54 typedef struct font3d font3d
;
61 mdl_array_ptr font_variants
,
65 VG_STATIC
void font3d_load( font3d
*font
, const char *mdl_path
, void *alloc
)
67 mdl_open( &font
->mdl
, mdl_path
, alloc
);
68 mdl_load_metadata_block( &font
->mdl
, alloc
);
70 vg_linear_clear( vg_mem
.scratch
);
72 mdl_load_array( &font
->mdl
, &fonts
, "ent_font", vg_mem
.scratch
);
73 font
->info
= *((ent_font
*)mdl_arritm(&fonts
,0));
75 mdl_load_array( &font
->mdl
, &font
->font_variants
, "ent_font_variant", alloc
);
76 mdl_load_array( &font
->mdl
, &font
->glyphs
, "ent_glyph", alloc
);
78 vg_linear_clear( vg_mem
.scratch
);
80 if( !mdl_arrcount( &font
->mdl
.textures
) )
81 vg_fatal_error( "No texture in font file" );
83 mdl_texture
*tex0
= mdl_arritm( &font
->mdl
.textures
, 0 );
84 void *data
= vg_linear_alloc( vg_mem
.scratch
, tex0
->file
.pack_size
);
85 mdl_fread_pack_file( &font
->mdl
, &tex0
->file
, data
);
87 mdl_async_load_glmesh( &font
->mdl
, &font
->mesh
);
88 vg_tex2d_load_qoi_async( data
, tex0
->file
.pack_size
,
89 VG_TEX2D_NEAREST
|VG_TEX2D_REPEAT
|VG_TEX2D_NOMIP
,
92 mdl_close( &font
->mdl
);
95 VG_STATIC
void font3d_init(void)
97 shader_model_font_register();
100 VG_STATIC u32
font3d_find_variant( font3d
*font
, const char *name
)
102 for( u32 i
=0; i
<mdl_arrcount( &font
->font_variants
); i
++ ){
103 ent_font_variant
*variant
= mdl_arritm( &font
->font_variants
, i
);
105 if( !strcmp( mdl_pstr( &font
->mdl
, variant
->name
), name
) ){
113 VG_STATIC
void font3d_bind( font3d
*font
, camera
*cam
)
115 shader_model_font_use();
116 shader_model_font_uColour( (v4f
){1.0f
,1.0f
,1.0f
,1.0f
} );
117 shader_model_font_uTexMain( 1 );
118 glActiveTexture( GL_TEXTURE1
);
119 glBindTexture( GL_TEXTURE_2D
, font
->texture
);
121 shader_model_font_uPv( cam
->mtx
.pv
);
122 mesh_bind( &font
->mesh
);
125 VG_STATIC ent_glyph
*font3d_glyph( font3d
*font
, u32 variant_id
, u32 utf32
)
127 if( utf32
< font
->info
.glyph_utf32_base
) return NULL
;
128 if( utf32
>= font
->info
.glyph_utf32_base
+font
->info
.glyph_count
) return NULL
;
130 u32 index
= utf32
- font
->info
.glyph_utf32_base
;
131 index
+= font
->info
.glyph_start
;
132 index
+= font
->info
.glyph_count
* variant_id
;
133 return mdl_arritm( &font
->glyphs
, index
);
137 float font3d_simple_draw( font3d
*font
, u32 variant_id
, const char *text
,
138 camera
*cam
, m4x3f transform
)
145 m4x3_expand( transform
, prev_mtx
);
146 m4x4_mul( cam
->mtx_prev
.pv
, prev_mtx
, prev_mtx
);
148 shader_model_font_uPvmPrev( prev_mtx
);
149 shader_model_font_uMdl( transform
);
151 const u8
*u8str
= (u8
*)text
;
153 for( int i
=0;; i
++ ){
157 if( c
== k_SRglyph_ctrl_variant
){
158 variant_id
= u8str
[i
+1];
162 ent_glyph
*glyph
= font3d_glyph( font
, variant_id
, c
);
163 if( !glyph
) continue;
165 if( glyph
->indice_count
){
166 if( c
== k_SRglyph_mod_square
|| c
== k_SRglyph_mod_circle
){
168 if( c1
== '\0' ) break;
170 ent_glyph
*glyph1
= font3d_glyph( font
, variant_id
, c1
);
173 if( glyph1
->indice_count
){
175 v2_sub( glyph
->size
, glyph1
->size
, v0
);
176 v2_muladds( offset
, v0
, -0.5f
, v0
);
179 shader_model_font_uOffset( v0
);
180 mesh_drawn( glyph
->indice_start
, glyph
->indice_count
);
182 shader_model_font_uOffset( offset
);
183 mesh_drawn( glyph1
->indice_start
, glyph1
->indice_count
);
184 offset
[0] += glyph1
->size
[0];
192 shader_model_font_uOffset( offset
);
193 mesh_drawn( glyph
->indice_start
, glyph
->indice_count
);
196 offset
[0] += glyph
->size
[0];
203 float font3d_string_width( font3d
*font
, u32 variant_id
, const char *text
)
206 for( int i
=0;; i
++ ){
210 ent_glyph
*glyph
= font3d_glyph( font
, variant_id
, c
);
211 if( !glyph
) continue;
213 width
+= glyph
->size
[0];