adjust walkout anim length & masked anim blending
[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 #include "shaders/scene_font.h"
9 #include "world_render.h"
10 #include "depth_compare.h"
11
12 enum efont_SRglyph{
13 k_SRglyph_end = 0x00, /* control characters */
14 k_SRglyph_ctrl_variant = 0x01,
15 k_SRglyph_ctrl_size = 0x02, /* normalized 0-1 */
16 k_SRglyph_ctrl_center = 0x03, /* useful when text is scaled down */
17 k_SRglyph_ctrl_baseline = 0x04, /* . */
18 k_SRglyph_ctrl_top = 0x05, /* . */
19 k_SRglyph_mod_circle = 0x1e, /* surround and center next charater */
20 k_SRglyph_mod_square = 0x1f, /* surround and center next character */
21 k_SRglyph_ascii_min = 0x20, /* standard ascii */
22 k_SRglyph_ascii_max = 0x7e,
23 k_SRglyph_ps4_square = 0x7f,/* playstation buttons */
24 k_SRglyph_ps4_triangle = 0x80,
25 k_SRglyph_ps4_circle = 0x81,
26 k_SRglyph_ps4_cross = 0x82,
27 k_SRglyph_xb1_x = 0x83,/* xbox buttons */
28 k_SRglyph_xb1_y = 0x84,
29 k_SRglyph_xb1_a = 0x85,
30 k_SRglyph_xb1_b = 0x86,
31 k_SRglyph_gen_ls = 0x87,/* generic gamepad */
32 k_SRglyph_gen_lsh = 0x88,
33 k_SRglyph_gen_lsv = 0x89,
34 k_SRglyph_gen_lshv = 0x8a,
35 k_SRglyph_gen_rs = 0x8b,
36 k_SRglyph_gen_rsh = 0x8c,
37 k_SRglyph_gen_rsv = 0x8d,
38 k_SRglyph_gen_rshv = 0x8e,
39 k_SRglyph_gen_lt = 0x8f,
40 k_SRglyph_gen_rt = 0x90,
41 k_SRglyph_gen_lb = 0x91,
42 k_SRglyph_gen_rb = 0x92,
43 k_SRglyph_gen_left = 0x93,
44 k_SRglyph_gen_up = 0x94,
45 k_SRglyph_gen_right = 0x95,
46 k_SRglyph_gen_down = 0x96,
47 k_SRglyph_gen_options = 0x97,
48 k_SRglyph_gen_shareview = 0x98,
49 k_SRglyph_kbm_m0 = 0x99,/* mouse */
50 k_SRglyph_kbm_m1 = 0x9a,
51 k_SRglyph_kbm_m01 = 0x9b,
52 k_SRglyph_kbm_m2 = 0x9c,
53 k_SRglyph_kbm_m2s = 0x9d,
54 k_SRglyph_kbm_shift = 0x9e,/* modifiers */
55 k_SRglyph_kbm_ctrl = 0x9f,
56 k_SRglyph_kbm_alt = 0xa0,
57 k_SRglyph_kbm_space = 0xa1,
58 k_SRglyph_kbm_return = 0xa2,
59 k_SRglyph_kbm_escape = 0xa3,
60 k_SRglyph_kbm_mousemove = 0xa4,
61 k_SRglyph_vg_ret = 0xa5,
62 k_SRglyph_vg_link = 0xa6,
63 k_SRglyph_vg_square = 0xa7,
64 k_SRglyph_vg_triangle = 0xa8,
65 k_SRglyph_vg_circle = 0xa9
66 };
67
68 typedef struct font3d font3d;
69 struct font3d{
70 mdl_context mdl;
71 GLuint texture;
72 glmesh mesh;
73
74 ent_font info;
75 mdl_array_ptr font_variants,
76 glyphs;
77 };
78
79 static void font3d_load( font3d *font, const char *mdl_path, void *alloc ){
80 mdl_open( &font->mdl, mdl_path, alloc );
81 mdl_load_metadata_block( &font->mdl, alloc );
82
83 vg_linear_clear( vg_mem.scratch );
84 mdl_array_ptr fonts;
85 MDL_LOAD_ARRAY( &font->mdl, &fonts, ent_font, vg_mem.scratch );
86 font->info = *((ent_font *)mdl_arritm(&fonts,0));
87
88 MDL_LOAD_ARRAY( &font->mdl, &font->font_variants, ent_font_variant, alloc);
89 MDL_LOAD_ARRAY( &font->mdl, &font->glyphs, ent_glyph, alloc );
90
91 vg_linear_clear( vg_mem.scratch );
92
93 if( !mdl_arrcount( &font->mdl.textures ) )
94 vg_fatal_error( "No texture in font file" );
95
96 mdl_texture *tex0 = mdl_arritm( &font->mdl.textures, 0 );
97 void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
98 mdl_fread_pack_file( &font->mdl, &tex0->file, data );
99
100 mdl_async_load_glmesh( &font->mdl, &font->mesh, NULL );
101 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
102 VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
103 &font->texture );
104
105 mdl_close( &font->mdl );
106 }
107
108 static void font3d_init(void){
109 shader_model_font_register();
110 shader_scene_font_register();
111 }
112
113 static u32 font3d_find_variant( font3d *font, const char *name ){
114 for( u32 i=0; i<mdl_arrcount( &font->font_variants ); i ++ ){
115 ent_font_variant *variant = mdl_arritm( &font->font_variants, i );
116
117 if( !strcmp( mdl_pstr( &font->mdl, variant->name ), name ) ){
118 return i;
119 }
120 }
121
122 return 0;
123 }
124
125 struct _font3d_render{
126 v4f offset;
127 font3d *font;
128 u32 variant_id;
129
130 enum font_shader {
131 k_font_shader_default,
132 k_font_shader_world
133 }
134 shader;
135 }
136 static gui_font3d;
137
138 /*
139 * world can be null if not using world shader
140 */
141 static void font3d_bind( font3d *font, enum font_shader shader,
142 int depth_compare, world_instance *world,
143 camera *cam ){
144 gui_font3d.shader = shader;
145 gui_font3d.font = font;
146 glActiveTexture( GL_TEXTURE1 );
147 glBindTexture( GL_TEXTURE_2D, font->texture );
148
149 if( shader == k_font_shader_default ){
150 shader_model_font_use();
151 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
152 shader_model_font_uTexMain( 1 );
153
154 shader_model_font_uDepthCompare( depth_compare );
155 if( depth_compare ){
156 depth_compare_bind(
157 shader_model_font_uTexSceneDepth,
158 shader_model_font_uInverseRatioDepth,
159 shader_model_font_uInverseRatioMain, cam );
160 }
161
162 shader_model_font_uPv( cam->mtx.pv );
163 }
164 else if( shader == k_font_shader_world ){
165 assert( world );
166 shader_scene_font_use();
167 shader_scene_font_uTexGarbage(0);
168 shader_scene_font_uTexMain(1);
169
170 shader_scene_font_uPv( skaterift.cam.mtx.pv );
171 shader_scene_font_uTime( vg.time );
172
173 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_font );
174
175 bind_terrain_noise();
176 shader_scene_font_uCamera( skaterift.cam.transform[3] );
177 }
178 mesh_bind( &font->mesh );
179 }
180
181 static ent_glyph *font3d_glyph( font3d *font, u32 variant_id, u32 utf32 ){
182 if( utf32 < font->info.glyph_utf32_base ) return NULL;
183 if( utf32 >= font->info.glyph_utf32_base+font->info.glyph_count) return NULL;
184
185 u32 index = utf32 - font->info.glyph_utf32_base;
186 index += font->info.glyph_start;
187 index += font->info.glyph_count * variant_id;
188 return mdl_arritm( &font->glyphs, index );
189 }
190
191 static void font3d_set_transform( const char *text,
192 camera *cam, m4x3f transform ){
193 v4_copy( (v4f){0.0f,0.0f,0.0f,1.0f}, gui_font3d.offset );
194
195 m4x4f prev_mtx;
196 m4x3_expand( transform, prev_mtx );
197 m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
198
199 if( gui_font3d.shader == k_font_shader_default ){
200 shader_model_font_uPvmPrev( prev_mtx );
201 shader_model_font_uMdl( transform );
202 }
203 else if( gui_font3d.shader == k_font_shader_world ){
204 shader_scene_font_uPvmPrev( prev_mtx );
205 shader_scene_font_uMdl( transform );
206 }
207 }
208
209 static void font3d_setoffset( v4f offset ){
210 if( gui_font3d.shader == k_font_shader_default )
211 shader_model_font_uOffset( offset );
212 else if( gui_font3d.shader == k_font_shader_world )
213 shader_scene_font_uOffset( offset );
214 }
215
216 static void font3d_setcolour( v4f colour ){
217 if( gui_font3d.shader == k_font_shader_default )
218 shader_model_font_uColour( colour );
219 #if 0
220 else if( gui_font3d.shader == k_font_shader_world )
221 shader_scene_font_uColour( colour );
222 #endif
223 }
224
225 static void font3d_draw( const char *text ){
226 u8 *u8pch = (u8*)text;
227
228 u32 max_chars = 512;
229 while( u8pch && max_chars ){
230 max_chars --;
231
232 u32 c0 = *u8pch, c1;
233 u8pch ++;
234
235 if( !c0 ) break;
236
237 ent_glyph *glyph0 = font3d_glyph( gui_font3d.font,
238 gui_font3d.variant_id, c0 ),
239 *glyph1 = NULL;
240
241 /* multibyte characters */
242 if( c0 >= 1 && c0 < k_SRglyph_ascii_min ){
243 c1 = *u8pch;
244 if( !c1 ) break;
245 glyph1 = font3d_glyph( gui_font3d.font, gui_font3d.variant_id, c1 );
246 }
247
248 if( c0 == k_SRglyph_ctrl_variant ){
249 gui_font3d.variant_id = c1;
250 u8pch ++;
251 continue;
252 }
253 else if( c0 == k_SRglyph_ctrl_size ){
254 gui_font3d.offset[3] = (float)c1 * (1.0f/255.0f);
255 u8pch ++;
256 continue;
257 }
258 else if( c0 == k_SRglyph_ctrl_baseline ){
259 gui_font3d.offset[1] = 0.0f;
260 continue;
261 }
262 else if( c0 == k_SRglyph_ctrl_center ){
263 if( glyph1 ){
264 float diff = glyph1->size[1] - glyph1->size[1]*gui_font3d.offset[3];
265 gui_font3d.offset[1] = diff * 0.5f;
266 }
267 continue;
268 }
269 else if( c0 == k_SRglyph_ctrl_top ){
270 if( glyph1 ){
271 float diff = glyph1->size[1] - glyph1->size[1]*gui_font3d.offset[3];
272 gui_font3d.offset[1] = diff;
273 }
274 continue;
275 }
276
277 if( !glyph0 ) continue;
278
279 if( glyph1 && (c0 == k_SRglyph_mod_square || c0 == k_SRglyph_mod_circle)){
280 v4f v0;
281 v2_sub( glyph0->size, glyph1->size, v0 );
282 v2_muladds( gui_font3d.offset, v0, -0.5f, v0 );
283 v0[2] = gui_font3d.offset[2];
284 v0[3] = gui_font3d.offset[3];
285
286 font3d_setoffset( v0 );
287 mesh_drawn( glyph0->indice_start, glyph0->indice_count );
288 continue;
289 }
290 else{
291 font3d_setoffset( gui_font3d.offset );
292 mesh_drawn( glyph0->indice_start, glyph0->indice_count );
293 }
294
295 gui_font3d.offset[0] += glyph0->size[0]*gui_font3d.offset[3];
296 }
297 }
298
299 static f32 font3d_simple_draw( u32 variant_id, const char *text,
300 camera *cam, m4x3f transform ){
301 if( !text ) return 0.0f;
302
303 gui_font3d.variant_id = variant_id;
304 font3d_set_transform( text, cam, transform );
305 font3d_draw( text );
306 return gui_font3d.offset[0];
307 }
308
309 static f32 font3d_string_width( u32 variant_id, const char *text ){
310 if( !text ) return 0.0f;
311 float width = 0.0f;
312
313 const u8 *buf = (const u8 *)text;
314 for( int i=0;; i++ ){
315 u32 c = buf[i];
316 if(!c) break;
317
318 ent_glyph *glyph = font3d_glyph( gui_font3d.font, variant_id, c );
319 if( !glyph ) continue;
320
321 width += glyph->size[0];
322 }
323
324 return width;
325 }
326
327 #endif /* FONT_H */