a7836139ffc574b06a082c35362b499d5f71a245
[carveJwlIkooP6JGAAIwe30JlM.git] / font.h
1 #pragma once
2 #include "model.h"
3 #include "entity.h"
4 #include "vg/vg_camera.h"
5 #include "shaders/model_font.h"
6 #include "shaders/scene_font.h"
7 #include "world_render.h"
8 #include "depth_compare.h"
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 k_SRglyph_vg_ret = 0xa5,
60 k_SRglyph_vg_link = 0xa6,
61 k_SRglyph_vg_square = 0xa7,
62 k_SRglyph_vg_triangle = 0xa8,
63 k_SRglyph_vg_circle = 0xa9
64 };
65
66 typedef struct font3d font3d;
67 struct font3d{
68 mdl_context mdl;
69 GLuint texture;
70 glmesh mesh;
71
72 ent_font info;
73 mdl_array_ptr font_variants,
74 glyphs;
75 };
76
77 static void font3d_load( font3d *font, const char *mdl_path, void *alloc ){
78 mdl_open( &font->mdl, mdl_path, alloc );
79 mdl_load_metadata_block( &font->mdl, alloc );
80
81 vg_linear_clear( vg_mem.scratch );
82 mdl_array_ptr fonts;
83 MDL_LOAD_ARRAY( &font->mdl, &fonts, ent_font, vg_mem.scratch );
84 font->info = *((ent_font *)mdl_arritm(&fonts,0));
85
86 MDL_LOAD_ARRAY( &font->mdl, &font->font_variants, ent_font_variant, alloc);
87 MDL_LOAD_ARRAY( &font->mdl, &font->glyphs, ent_glyph, alloc );
88
89 vg_linear_clear( vg_mem.scratch );
90
91 if( !mdl_arrcount( &font->mdl.textures ) )
92 vg_fatal_error( "No texture in font file" );
93
94 mdl_texture *tex0 = mdl_arritm( &font->mdl.textures, 0 );
95 void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
96 mdl_fread_pack_file( &font->mdl, &tex0->file, data );
97
98 mdl_async_load_glmesh( &font->mdl, &font->mesh, NULL );
99 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
100 VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
101 &font->texture );
102
103 mdl_close( &font->mdl );
104 }
105
106 static u32 font3d_find_variant( font3d *font, const char *name ){
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 struct _font3d_render{
119 v4f offset;
120 font3d *font;
121 u32 variant_id;
122
123 enum font_shader {
124 k_font_shader_default,
125 k_font_shader_world
126 }
127 shader;
128 }
129 static gui_font3d;
130
131 /*
132 * world can be null if not using world shader
133 */
134 static void font3d_bind( font3d *font, enum font_shader shader,
135 int depth_compare, world_instance *world,
136 vg_camera *cam ){
137 gui_font3d.shader = shader;
138 gui_font3d.font = font;
139 glActiveTexture( GL_TEXTURE1 );
140 glBindTexture( GL_TEXTURE_2D, font->texture );
141
142 if( shader == k_font_shader_default ){
143 shader_model_font_use();
144 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
145 shader_model_font_uTexMain( 1 );
146
147 shader_model_font_uDepthCompare( depth_compare );
148 if( depth_compare ){
149 depth_compare_bind(
150 shader_model_font_uTexSceneDepth,
151 shader_model_font_uInverseRatioDepth,
152 shader_model_font_uInverseRatioMain, cam );
153 }
154
155 shader_model_font_uPv( cam->mtx.pv );
156 }
157 else if( shader == k_font_shader_world ){
158 shader_scene_font_use();
159 shader_scene_font_uTexGarbage(0);
160 shader_scene_font_uTexMain(1);
161
162 shader_scene_font_uPv( skaterift.cam.mtx.pv );
163 shader_scene_font_uTime( vg.time );
164
165 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_font );
166
167 bind_terrain_noise();
168 shader_scene_font_uCamera( skaterift.cam.transform[3] );
169 }
170 mesh_bind( &font->mesh );
171 }
172
173 static ent_glyph *font3d_glyph( font3d *font, u32 variant_id, u32 utf32 ){
174 if( utf32 < font->info.glyph_utf32_base ) return NULL;
175 if( utf32 >= font->info.glyph_utf32_base+font->info.glyph_count) return NULL;
176
177 u32 index = utf32 - font->info.glyph_utf32_base;
178 index += font->info.glyph_start;
179 index += font->info.glyph_count * variant_id;
180 return mdl_arritm( &font->glyphs, index );
181 }
182
183 static void font3d_set_transform( const char *text,
184 vg_camera *cam, m4x3f transform ){
185 v4_copy( (v4f){0.0f,0.0f,0.0f,1.0f}, gui_font3d.offset );
186
187 m4x4f prev_mtx;
188 m4x3_expand( transform, prev_mtx );
189 m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
190
191 if( gui_font3d.shader == k_font_shader_default ){
192 shader_model_font_uPvmPrev( prev_mtx );
193 shader_model_font_uMdl( transform );
194 }
195 else if( gui_font3d.shader == k_font_shader_world ){
196 shader_scene_font_uPvmPrev( prev_mtx );
197 shader_scene_font_uMdl( transform );
198 }
199 }
200
201 static void font3d_setoffset( v4f offset ){
202 if( gui_font3d.shader == k_font_shader_default )
203 shader_model_font_uOffset( offset );
204 else if( gui_font3d.shader == k_font_shader_world )
205 shader_scene_font_uOffset( offset );
206 }
207
208 static void font3d_setcolour( v4f colour ){
209 if( gui_font3d.shader == k_font_shader_default )
210 shader_model_font_uColour( colour );
211 #if 0
212 else if( gui_font3d.shader == k_font_shader_world )
213 shader_scene_font_uColour( colour );
214 #endif
215 }
216
217 static void font3d_draw( const char *text ){
218 u8 *u8pch = (u8*)text;
219
220 u32 max_chars = 512;
221 while( u8pch && max_chars ){
222 max_chars --;
223
224 u32 c0 = *u8pch, c1;
225 u8pch ++;
226
227 if( !c0 ) break;
228
229 ent_glyph *glyph0 = font3d_glyph( gui_font3d.font,
230 gui_font3d.variant_id, c0 ),
231 *glyph1 = NULL;
232
233 /* multibyte characters */
234 if( c0 >= 1 && c0 < k_SRglyph_ascii_min ){
235 c1 = *u8pch;
236 if( !c1 ) break;
237 glyph1 = font3d_glyph( gui_font3d.font, gui_font3d.variant_id, c1 );
238 }
239
240 if( c0 == k_SRglyph_ctrl_variant ){
241 gui_font3d.variant_id = c1;
242 u8pch ++;
243 continue;
244 }
245 else if( c0 == k_SRglyph_ctrl_size ){
246 gui_font3d.offset[3] = (float)c1 * (1.0f/255.0f);
247 u8pch ++;
248 continue;
249 }
250 else if( c0 == k_SRglyph_ctrl_baseline ){
251 gui_font3d.offset[1] = 0.0f;
252 continue;
253 }
254 else if( c0 == k_SRglyph_ctrl_center ){
255 if( glyph1 ){
256 float diff = glyph1->size[1] - glyph1->size[1]*gui_font3d.offset[3];
257 gui_font3d.offset[1] = diff * 0.5f;
258 }
259 continue;
260 }
261 else if( c0 == k_SRglyph_ctrl_top ){
262 if( glyph1 ){
263 float diff = glyph1->size[1] - glyph1->size[1]*gui_font3d.offset[3];
264 gui_font3d.offset[1] = diff;
265 }
266 continue;
267 }
268
269 if( !glyph0 ) continue;
270
271 if( glyph1 && (c0 == k_SRglyph_mod_square || c0 == k_SRglyph_mod_circle)){
272 v4f v0;
273 v2_sub( glyph0->size, glyph1->size, v0 );
274 v2_muladds( gui_font3d.offset, v0, -0.5f, v0 );
275 v0[2] = gui_font3d.offset[2];
276 v0[3] = gui_font3d.offset[3];
277
278 font3d_setoffset( v0 );
279 mesh_drawn( glyph0->indice_start, glyph0->indice_count );
280 continue;
281 }
282 else{
283 font3d_setoffset( gui_font3d.offset );
284 mesh_drawn( glyph0->indice_start, glyph0->indice_count );
285 }
286
287 gui_font3d.offset[0] += glyph0->size[0]*gui_font3d.offset[3];
288 }
289 }
290
291 static f32 font3d_simple_draw( u32 variant_id, const char *text,
292 vg_camera *cam, m4x3f transform ){
293 if( !text ) return 0.0f;
294
295 gui_font3d.variant_id = variant_id;
296 font3d_set_transform( text, cam, transform );
297 font3d_draw( text );
298 return gui_font3d.offset[0];
299 }
300
301 static f32 font3d_string_width( u32 variant_id, const char *text ){
302 if( !text ) return 0.0f;
303 float width = 0.0f;
304
305 const u8 *buf = (const u8 *)text;
306 for( int i=0;; i++ ){
307 u32 c = buf[i];
308 if(!c) break;
309
310 ent_glyph *glyph = font3d_glyph( gui_font3d.font, variant_id, c );
311 if( !glyph ) continue;
312
313 width += glyph->size[0];
314 }
315
316 return width;
317 }