sorta ready
[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 = 0x00, /* control characters */
12 k_SRglyph_ctrl_variant = 0x01,
13 k_SRglyph_ctrl_size = 0x02, /* normalized 0-1 */
14 k_SRglyph_ctrl_center = 0x03, /* useful when text is scaled down */
15 k_SRglyph_ctrl_baseline = 0x04, /* . */
16 k_SRglyph_ctrl_top = 0x05, /* . */
17 k_SRglyph_mod_circle = 0x1e, /* surround and center next charater */
18 k_SRglyph_mod_square = 0x1f, /* surround and center next character */
19 k_SRglyph_ascii_min = 0x20, /* standard ascii */
20 k_SRglyph_ascii_max = 0x7e,
21 k_SRglyph_ps4_square = 0x7f,/* playstation buttons */
22 k_SRglyph_ps4_triangle = 0x80,
23 k_SRglyph_ps4_circle = 0x81,
24 k_SRglyph_ps4_cross = 0x82,
25 k_SRglyph_xb1_x = 0x83,/* xbox buttons */
26 k_SRglyph_xb1_y = 0x84,
27 k_SRglyph_xb1_a = 0x85,
28 k_SRglyph_xb1_b = 0x86,
29 k_SRglyph_gen_ls = 0x87,/* generic gamepad */
30 k_SRglyph_gen_lsh = 0x88,
31 k_SRglyph_gen_lsv = 0x89,
32 k_SRglyph_gen_lshv = 0x8a,
33 k_SRglyph_gen_rs = 0x8b,
34 k_SRglyph_gen_rsh = 0x8c,
35 k_SRglyph_gen_rsv = 0x8d,
36 k_SRglyph_gen_rshv = 0x8e,
37 k_SRglyph_gen_lt = 0x8f,
38 k_SRglyph_gen_rt = 0x90,
39 k_SRglyph_gen_lb = 0x91,
40 k_SRglyph_gen_rb = 0x92,
41 k_SRglyph_gen_left = 0x93,
42 k_SRglyph_gen_up = 0x94,
43 k_SRglyph_gen_right = 0x95,
44 k_SRglyph_gen_down = 0x96,
45 k_SRglyph_gen_options = 0x97,
46 k_SRglyph_gen_shareview = 0x98,
47 k_SRglyph_kbm_m0 = 0x99,/* mouse */
48 k_SRglyph_kbm_m1 = 0x9a,
49 k_SRglyph_kbm_m01 = 0x9b,
50 k_SRglyph_kbm_m2 = 0x9c,
51 k_SRglyph_kbm_m2s = 0x9d,
52 k_SRglyph_kbm_shift = 0x9e,/* modifiers */
53 k_SRglyph_kbm_ctrl = 0x9f,
54 k_SRglyph_kbm_alt = 0xa0,
55 k_SRglyph_kbm_space = 0xa1,
56 k_SRglyph_kbm_return = 0xa2,
57 k_SRglyph_kbm_escape = 0xa3,
58 k_SRglyph_kbm_mousemove = 0xa4
59 };
60
61 typedef struct font3d font3d;
62 struct font3d{
63 mdl_context mdl;
64 GLuint texture;
65 glmesh mesh;
66
67 ent_font info;
68 mdl_array_ptr font_variants,
69 glyphs;
70 };
71
72 VG_STATIC void font3d_load( font3d *font, const char *mdl_path, void *alloc )
73 {
74 mdl_open( &font->mdl, mdl_path, alloc );
75 mdl_load_metadata_block( &font->mdl, alloc );
76
77 vg_linear_clear( vg_mem.scratch );
78 mdl_array_ptr fonts;
79 mdl_load_array( &font->mdl, &fonts, "ent_font", vg_mem.scratch );
80 font->info = *((ent_font *)mdl_arritm(&fonts,0));
81
82 mdl_load_array( &font->mdl, &font->font_variants, "ent_font_variant", alloc);
83 mdl_load_array( &font->mdl, &font->glyphs, "ent_glyph", alloc );
84
85 vg_linear_clear( vg_mem.scratch );
86
87 if( !mdl_arrcount( &font->mdl.textures ) )
88 vg_fatal_error( "No texture in font file" );
89
90 mdl_texture *tex0 = mdl_arritm( &font->mdl.textures, 0 );
91 void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
92 mdl_fread_pack_file( &font->mdl, &tex0->file, data );
93
94 mdl_async_load_glmesh( &font->mdl, &font->mesh );
95 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
96 VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
97 &font->texture );
98
99 mdl_close( &font->mdl );
100 }
101
102 VG_STATIC void font3d_init(void)
103 {
104 shader_model_font_register();
105 }
106
107 VG_STATIC u32 font3d_find_variant( font3d *font, const char *name )
108 {
109 for( u32 i=0; i<mdl_arrcount( &font->font_variants ); i ++ ){
110 ent_font_variant *variant = mdl_arritm( &font->font_variants, i );
111
112 if( !strcmp( mdl_pstr( &font->mdl, variant->name ), name ) ){
113 return i;
114 }
115 }
116
117 return 0;
118 }
119
120 VG_STATIC void font3d_bind( font3d *font, camera *cam )
121 {
122 shader_model_font_use();
123 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
124 shader_model_font_uTexMain( 1 );
125 glActiveTexture( GL_TEXTURE1 );
126 glBindTexture( GL_TEXTURE_2D, font->texture );
127
128 shader_model_font_uPv( cam->mtx.pv );
129 mesh_bind( &font->mesh );
130 }
131
132 VG_STATIC ent_glyph *font3d_glyph( font3d *font, u32 variant_id, u32 utf32 )
133 {
134 if( utf32 < font->info.glyph_utf32_base ) return NULL;
135 if( utf32 >= font->info.glyph_utf32_base+font->info.glyph_count) return NULL;
136
137 u32 index = utf32 - font->info.glyph_utf32_base;
138 index += font->info.glyph_start;
139 index += font->info.glyph_count * variant_id;
140 return mdl_arritm( &font->glyphs, index );
141 }
142
143 struct font3d_render{
144 v4f offset;
145 font3d *font;
146 const u8 *u8pch;
147 u32 variant_id;
148 };
149
150 VG_STATIC
151 void font3d_begin( font3d *font, u32 variant_id,
152 camera *cam, m4x3f transform, struct font3d_render *render )
153 {
154 q_identity( render->offset );
155
156 m4x4f prev_mtx;
157 m4x3_expand( transform, prev_mtx );
158 m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
159
160 shader_model_font_uPvmPrev( prev_mtx );
161 shader_model_font_uMdl( transform );
162
163 render->font = font;
164 render->variant_id = variant_id;
165 }
166
167 VG_STATIC void font3d_draw( struct font3d_render *render )
168 {
169 u32 max_chars = 512;
170 while( render->u8pch && max_chars ){
171 max_chars --;
172
173 u32 c0 = *render->u8pch, c1;
174 render->u8pch ++;
175
176 if( !c0 ) break;
177
178 ent_glyph *glyph0 = font3d_glyph( render->font, render->variant_id, c0 ),
179 *glyph1 = NULL;
180
181 /* multibyte characters */
182 if( c0 >= 1 && c0 < k_SRglyph_ascii_min ){
183 c1 = *render->u8pch;
184 if( !c1 ) break;
185 glyph1 = font3d_glyph( render->font, render->variant_id, c1 );
186 }
187
188 if( c0 == k_SRglyph_ctrl_variant ){
189 render->variant_id = c1;
190 render->u8pch ++;
191 continue;
192 }
193 else if( c0 == k_SRglyph_ctrl_size ){
194 render->offset[3] = (float)c1 * (1.0f/255.0f);
195 render->u8pch ++;
196 continue;
197 }
198 else if( c0 == k_SRglyph_ctrl_baseline ){
199 render->offset[1] = 0.0f;
200 continue;
201 }
202 else if( c0 == k_SRglyph_ctrl_center ){
203 if( glyph1 ){
204 float diff = glyph1->size[1] - glyph1->size[1]*render->offset[3];
205 render->offset[1] = diff * 0.5f;
206 }
207 continue;
208 }
209 else if( c0 == k_SRglyph_ctrl_top ){
210 if( glyph1 ){
211 float diff = glyph1->size[1] - glyph1->size[1]*render->offset[3];
212 render->offset[1] = diff;
213 }
214 continue;
215 }
216
217 if( !glyph0 ) continue;
218
219 if( glyph1 && (c0 == k_SRglyph_mod_square || c0 == k_SRglyph_mod_circle)){
220 v4f v0;
221 v2_sub( glyph0->size, glyph1->size, v0 );
222 v2_muladds( render->offset, v0, -0.5f, v0 );
223 v0[2] = render->offset[2];
224 v0[3] = render->offset[3];
225
226 shader_model_font_uOffset( v0 );
227 mesh_drawn( glyph0->indice_start, glyph0->indice_count );
228 continue;
229 }
230 else{
231 shader_model_font_uOffset( render->offset );
232 mesh_drawn( glyph0->indice_start, glyph0->indice_count );
233 }
234
235 render->offset[0] += glyph0->size[0]*render->offset[3];
236 }
237
238 render->u8pch = NULL;
239 }
240
241 VG_STATIC
242 float font3d_simple_draw( font3d *font, u32 variant_id, const char *text,
243 camera *cam, m4x3f transform )
244 {
245 if( !text ) return 0.0f;
246
247 struct font3d_render render;
248 font3d_begin( font, variant_id, cam, transform, &render );
249 render.u8pch = (u8*)text;
250 font3d_draw( &render );
251 return render.offset[0];
252 }
253
254 VG_STATIC
255 float font3d_string_width( font3d *font, u32 variant_id, const char *text )
256 {
257 if( !text ) return 0.0f;
258 float width = 0.0f;
259 for( int i=0;; i++ ){
260 u32 c = text[i];
261 if(!c) break;
262
263 ent_glyph *glyph = font3d_glyph( font, variant_id, c );
264 if( !glyph ) continue;
265
266 width += glyph->size[0];
267 }
268
269 return width;
270 }
271
272 #endif /* FONT_H */