eaba149b68d06bd7514fd9fc0b4d250ca06e3b2b
[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 typedef struct font3d font3d;
10 struct font3d{
11 mdl_context mdl;
12 GLuint texture;
13 glmesh mesh;
14
15 ent_font info;
16 mdl_array_ptr font_variants,
17 glyphs;
18 };
19
20 VG_STATIC void font3d_load( font3d *font, const char *mdl_path, void *alloc )
21 {
22 mdl_open( &font->mdl, mdl_path, alloc );
23 mdl_load_metadata_block( &font->mdl, alloc );
24
25 vg_linear_clear( vg_mem.scratch );
26 mdl_array_ptr fonts;
27 mdl_load_array( &font->mdl, &fonts, "ent_font", vg_mem.scratch );
28 font->info = *((ent_font *)mdl_arritm(&fonts,0));
29
30 mdl_load_array( &font->mdl, &font->font_variants, "ent_font_variant", alloc);
31 mdl_load_array( &font->mdl, &font->glyphs, "ent_glyph", alloc );
32
33 vg_linear_clear( vg_mem.scratch );
34 mdl_load_mesh_block( &font->mdl, vg_mem.scratch );
35
36 if( !mdl_arrcount( &font->mdl.textures ) )
37 vg_fatal_exit_loop( "No texture in font file" );
38
39 mdl_texture *tex0 = mdl_arritm( &font->mdl.textures, 0 );
40 void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
41 mdl_fread_pack_file( &font->mdl, &tex0->file, data );
42
43 vg_acquire_thread_sync();
44 {
45 /* upload mesh */
46 mesh_upload( &font->mesh,
47 font->mdl.verts.data, font->mdl.verts.count,
48 font->mdl.indices.data, font->mdl.indices.count );
49
50 /* upload first texture */
51 font->texture = vg_tex2d_new();
52
53 vg_tex2d_set_error();
54 vg_tex2d_qoi( data, tex0->file.pack_size,
55 mdl_pstr( &font->mdl, tex0->file.pstr_path ));
56 vg_tex2d_nearest();
57 vg_tex2d_repeat();
58 }
59 vg_release_thread_sync();
60
61 mdl_close( &font->mdl );
62 }
63
64 VG_STATIC void font3d_init(void)
65 {
66 shader_model_font_register();
67 }
68
69 VG_STATIC u32 font3d_find_variant( font3d *font, const char *name )
70 {
71 for( u32 i=0; i<mdl_arrcount( &font->font_variants ); i ++ ){
72 ent_font_variant *variant = mdl_arritm( &font->font_variants, i );
73
74 if( !strcmp( mdl_pstr( &font->mdl, variant->name ), name ) ){
75 return i;
76 }
77 }
78
79 return 0;
80 }
81
82 VG_STATIC void font3d_bind( font3d *font, camera *cam )
83 {
84 shader_model_font_use();
85 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
86 shader_model_font_uTexMain( 1 );
87 glActiveTexture( GL_TEXTURE1 );
88 glBindTexture( GL_TEXTURE_2D, font->texture );
89
90 shader_model_font_uPv( cam->mtx.pv );
91 mesh_bind( &font->mesh );
92 }
93
94 VG_STATIC ent_glyph *font3d_glyph( font3d *font, u32 variant_id, u32 utf32 )
95 {
96 if( utf32 < font->info.glyph_utf32_base ) return NULL;
97 if( utf32 >= font->info.glyph_utf32_base+font->info.glyph_count) return NULL;
98
99 u32 index = utf32 - font->info.glyph_utf32_base;
100 index += font->info.glyph_start;
101 index += font->info.glyph_count * variant_id;
102 return mdl_arritm( &font->glyphs, index );
103 }
104
105 VG_STATIC
106 void font3d_simple_draw( font3d *font, u32 variant_id, const char *text,
107 camera *cam, m4x3f transform )
108 {
109 v3f offset;
110 v3_zero( offset );
111
112 m4x4f prev_mtx;
113
114 m4x3_expand( transform, prev_mtx );
115 m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
116
117 shader_model_font_uPvmPrev( prev_mtx );
118 shader_model_font_uMdl( transform );
119
120 for( int i=0;; i++ ){
121 u32 c = text[i];
122 if(!c) break;
123
124 ent_glyph *glyph = font3d_glyph( font, variant_id, c );
125 if( !glyph ) continue;
126
127 if( glyph->indice_count ){
128 shader_model_font_uOffset( offset );
129 mesh_drawn( glyph->indice_start, glyph->indice_count );
130 }
131 offset[0] += glyph->size[0];
132 }
133 }
134
135 VG_STATIC
136 float font3d_string_width( font3d *font, u32 variant_id, const char *text )
137 {
138 float width = 0.0f;
139 for( int i=0;; i++ ){
140 u32 c = text[i];
141 if(!c) break;
142
143 ent_glyph *glyph = font3d_glyph( font, variant_id, c );
144 if( !glyph ) continue;
145
146 width += glyph->size[0];
147 }
148
149 return width;
150 }
151
152 #endif /* FONT_H */