input update 1
[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 };
58
59 typedef struct font3d font3d;
60 struct font3d{
61 mdl_context mdl;
62 GLuint texture;
63 glmesh mesh;
64
65 ent_font info;
66 mdl_array_ptr font_variants,
67 glyphs;
68 };
69
70 VG_STATIC void font3d_load( font3d *font, const char *mdl_path, void *alloc )
71 {
72 mdl_open( &font->mdl, mdl_path, alloc );
73 mdl_load_metadata_block( &font->mdl, alloc );
74
75 vg_linear_clear( vg_mem.scratch );
76 mdl_array_ptr fonts;
77 mdl_load_array( &font->mdl, &fonts, "ent_font", vg_mem.scratch );
78 font->info = *((ent_font *)mdl_arritm(&fonts,0));
79
80 mdl_load_array( &font->mdl, &font->font_variants, "ent_font_variant", alloc);
81 mdl_load_array( &font->mdl, &font->glyphs, "ent_glyph", alloc );
82
83 vg_linear_clear( vg_mem.scratch );
84
85 if( !mdl_arrcount( &font->mdl.textures ) )
86 vg_fatal_error( "No texture in font file" );
87
88 mdl_texture *tex0 = mdl_arritm( &font->mdl.textures, 0 );
89 void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
90 mdl_fread_pack_file( &font->mdl, &tex0->file, data );
91
92 mdl_async_load_glmesh( &font->mdl, &font->mesh );
93 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
94 VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
95 &font->texture );
96
97 mdl_close( &font->mdl );
98 }
99
100 VG_STATIC void font3d_init(void)
101 {
102 shader_model_font_register();
103 }
104
105 VG_STATIC u32 font3d_find_variant( font3d *font, const char *name )
106 {
107 for( u32 i=0; i<mdl_arrcount( &font->font_variants ); i ++ ){
108 ent_font_variant *variant = mdl_arritm( &font->font_variants, i );
109
110 if( !strcmp( mdl_pstr( &font->mdl, variant->name ), name ) ){
111 return i;
112 }
113 }
114
115 return 0;
116 }
117
118 VG_STATIC void font3d_bind( font3d *font, camera *cam )
119 {
120 shader_model_font_use();
121 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
122 shader_model_font_uTexMain( 1 );
123 glActiveTexture( GL_TEXTURE1 );
124 glBindTexture( GL_TEXTURE_2D, font->texture );
125
126 shader_model_font_uPv( cam->mtx.pv );
127 mesh_bind( &font->mesh );
128 }
129
130 VG_STATIC ent_glyph *font3d_glyph( font3d *font, u32 variant_id, u32 utf32 )
131 {
132 if( utf32 < font->info.glyph_utf32_base ) return NULL;
133 if( utf32 >= font->info.glyph_utf32_base+font->info.glyph_count) return NULL;
134
135 u32 index = utf32 - font->info.glyph_utf32_base;
136 index += font->info.glyph_start;
137 index += font->info.glyph_count * variant_id;
138 return mdl_arritm( &font->glyphs, index );
139 }
140
141 VG_STATIC
142 float font3d_simple_draw( font3d *font, u32 variant_id, const char *text,
143 camera *cam, m4x3f transform )
144 {
145 v4f offset;
146 q_identity( offset );
147
148 m4x4f prev_mtx;
149
150 m4x3_expand( transform, prev_mtx );
151 m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
152
153 shader_model_font_uPvmPrev( prev_mtx );
154 shader_model_font_uMdl( transform );
155
156 const u8 *u8str = (u8*)text;
157
158 for( u32 i=0;; i++ ){
159 u32 c0 = u8str[i],
160 c1;
161
162 if( !c0 ) break;
163
164 ent_glyph *glyph0 = font3d_glyph( font, variant_id, c0 ),
165 *glyph1;
166
167 /* multibyte characters */
168 if( c0 >= 1 && c0 < k_SRglyph_ascii_min ){
169 c1 = u8str[i+1];
170 if( !c1 ) break;
171 glyph1 = font3d_glyph( font, variant_id, c1 );
172 }
173
174 if( c0 == k_SRglyph_ctrl_variant ){
175 variant_id = c1;
176 i ++;
177 continue;
178 }
179 else if( c0 == k_SRglyph_ctrl_size ){
180 offset[3] = (float)c1 * (1.0f/255.0f);
181 i ++;
182 continue;
183 }
184 else if( c0 == k_SRglyph_ctrl_baseline ){
185 offset[1] = 0.0f;
186 continue;
187 }
188 else if( c0 == k_SRglyph_ctrl_center ){
189 if( glyph1 )
190 offset[1] = (glyph1->size[1] - glyph1->size[1]*offset[3]) * 0.5f;
191 continue;
192 }
193 else if( c0 == k_SRglyph_ctrl_top ){
194 if( glyph1 )
195 offset[1] = glyph1->size[1] - glyph1->size[1]*offset[3];
196 continue;
197 }
198
199 if( !glyph0 ) continue;
200
201 if( glyph1 && (c0 == k_SRglyph_mod_square || c0 == k_SRglyph_mod_circle)){
202 v4f v0;
203 v2_sub( glyph0->size, glyph1->size, v0 );
204 v2_muladds( offset, v0, -0.5f, v0 );
205 v0[2] = offset[2];
206 v0[3] = offset[3];
207
208 shader_model_font_uOffset( v0 );
209 mesh_drawn( glyph0->indice_start, glyph0->indice_count );
210 continue;
211 }
212 else{
213 shader_model_font_uOffset( offset );
214 mesh_drawn( glyph0->indice_start, glyph0->indice_count );
215 }
216
217 offset[0] += glyph0->size[0]*offset[3];
218 }
219
220 return offset[0];
221 }
222
223 VG_STATIC
224 float font3d_string_width( font3d *font, u32 variant_id, const char *text )
225 {
226 float width = 0.0f;
227 for( int i=0;; i++ ){
228 u32 c = text[i];
229 if(!c) break;
230
231 ent_glyph *glyph = font3d_glyph( font, variant_id, c );
232 if( !glyph ) continue;
233
234 width += glyph->size[0];
235 }
236
237 return width;
238 }
239
240 #endif /* FONT_H */