font works
[carveJwlIkooP6JGAAIwe30JlM.git] / font.h
1 #ifndef FONT_H
2 #define FONT_H
3
4 #include "model.h"
5 #include "entity.h"
6 #include "camera.h"
7 #include "shaders/model_font.h"
8
9
10 enum efont_SRglyph{
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
52 };
53
54 typedef struct font3d font3d;
55 struct font3d{
56 mdl_context mdl;
57 GLuint texture;
58 glmesh mesh;
59
60 ent_font info;
61 mdl_array_ptr font_variants,
62 glyphs;
63 };
64
65 VG_STATIC void font3d_load( font3d *font, const char *mdl_path, void *alloc )
66 {
67 mdl_open( &font->mdl, mdl_path, alloc );
68 mdl_load_metadata_block( &font->mdl, alloc );
69
70 vg_linear_clear( vg_mem.scratch );
71 mdl_array_ptr fonts;
72 mdl_load_array( &font->mdl, &fonts, "ent_font", vg_mem.scratch );
73 font->info = *((ent_font *)mdl_arritm(&fonts,0));
74
75 mdl_load_array( &font->mdl, &font->font_variants, "ent_font_variant", alloc);
76 mdl_load_array( &font->mdl, &font->glyphs, "ent_glyph", alloc );
77
78 vg_linear_clear( vg_mem.scratch );
79
80 if( !mdl_arrcount( &font->mdl.textures ) )
81 vg_fatal_error( "No texture in font file" );
82
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 );
86
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,
90 &font->texture );
91
92 mdl_close( &font->mdl );
93 }
94
95 VG_STATIC void font3d_init(void)
96 {
97 shader_model_font_register();
98 }
99
100 VG_STATIC u32 font3d_find_variant( font3d *font, const char *name )
101 {
102 for( u32 i=0; i<mdl_arrcount( &font->font_variants ); i ++ ){
103 ent_font_variant *variant = mdl_arritm( &font->font_variants, i );
104
105 if( !strcmp( mdl_pstr( &font->mdl, variant->name ), name ) ){
106 return i;
107 }
108 }
109
110 return 0;
111 }
112
113 VG_STATIC void font3d_bind( font3d *font, camera *cam )
114 {
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 );
120
121 shader_model_font_uPv( cam->mtx.pv );
122 mesh_bind( &font->mesh );
123 }
124
125 VG_STATIC ent_glyph *font3d_glyph( font3d *font, u32 variant_id, u32 utf32 )
126 {
127 if( utf32 < font->info.glyph_utf32_base ) return NULL;
128 if( utf32 >= font->info.glyph_utf32_base+font->info.glyph_count) return NULL;
129
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 );
134 }
135
136 VG_STATIC
137 float font3d_simple_draw( font3d *font, u32 variant_id, const char *text,
138 camera *cam, m4x3f transform )
139 {
140 v3f offset;
141 v3_zero( offset );
142
143 m4x4f prev_mtx;
144
145 m4x3_expand( transform, prev_mtx );
146 m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
147
148 shader_model_font_uPvmPrev( prev_mtx );
149 shader_model_font_uMdl( transform );
150
151 const u8 *u8str = (u8*)text;
152
153 for( int i=0;; i++ ){
154 u32 c = u8str[i];
155 if(!c) break;
156
157 if( c == k_SRglyph_ctrl_variant ){
158 variant_id = u8str[i+1];
159 continue;
160 }
161
162 ent_glyph *glyph = font3d_glyph( font, variant_id, c );
163 if( !glyph ) continue;
164
165 if( glyph->indice_count ){
166 if( c == k_SRglyph_mod_square || c == k_SRglyph_mod_circle ){
167 u32 c1 = u8str[i+1];
168 if( c1 == '\0' ) break;
169
170 ent_glyph *glyph1 = font3d_glyph( font, variant_id, c1 );
171
172 if( glyph1 ){
173 if( glyph1->indice_count ){
174 v3f v0;
175 v2_sub( glyph->size, glyph1->size, v0 );
176 v2_muladds( offset, v0, -0.5f, v0 );
177 v0[2] = 0.0f;
178
179 shader_model_font_uOffset( v0 );
180 mesh_drawn( glyph->indice_start, glyph->indice_count );
181
182 shader_model_font_uOffset( offset );
183 mesh_drawn( glyph1->indice_start, glyph1->indice_count );
184 offset[0] += glyph1->size[0];
185 }
186 }
187
188 i ++;
189 continue;
190 }
191 else{
192 shader_model_font_uOffset( offset );
193 mesh_drawn( glyph->indice_start, glyph->indice_count );
194 }
195 }
196 offset[0] += glyph->size[0];
197 }
198
199 return offset[0];
200 }
201
202 VG_STATIC
203 float font3d_string_width( font3d *font, u32 variant_id, const char *text )
204 {
205 float width = 0.0f;
206 for( int i=0;; i++ ){
207 u32 c = text[i];
208 if(!c) break;
209
210 ent_glyph *glyph = font3d_glyph( font, variant_id, c );
211 if( !glyph ) continue;
212
213 width += glyph->size[0];
214 }
215
216 return width;
217 }
218
219 #endif /* FONT_H */