nametag rendering
[carveJwlIkooP6JGAAIwe30JlM.git] / gui.h
1 #ifndef GUI_H
2 #define GUI_H
3
4 #include "font.h"
5 #include "input.h"
6 #include "player.h"
7
8 enum gui_icon {
9 k_gui_icon_tick = 0,
10 k_gui_icon_exclaim,
11 k_gui_icon_board,
12 k_gui_icon_world,
13 k_gui_icon_rift,
14
15 k_gui_icon_count,
16 };
17
18 struct{
19 struct gui_helper{
20 const char *bindstr, *text;
21 }
22 helpers[4];
23 u32 helper_count;
24
25 struct icon_call {
26 enum gui_icon icon;
27 v4f location;
28 }
29 icon_draw_buffer[32];
30 u32 icon_draw_count;
31
32 char location[64];
33 f64 location_time;
34
35 f32 factive;
36 font3d font;
37
38 v3f trick_co;
39 enum guitrick_type{
40 k_guitrick_type_none,
41 k_guitrick_type_ollie,
42 k_guitrick_type_trick,
43 k_guitrick_type_backflip,
44 k_guitrick_type_pump,
45 k_guitrick_type_isc
46 }
47 trick_type;
48
49 mdl_context model_icons;
50 GLuint icons_texture;
51 glmesh icons_mesh;
52
53 mdl_submesh *icons[ k_gui_icon_count ];
54 }
55 static gui;
56
57 static
58 void gui_helper_action( const char *bindstr, const char *text ){
59 if( gui.helper_count >= vg_list_size(gui.helpers) ){
60 vg_error( "Too many helpers\n" );
61 return;
62 }
63
64 struct gui_helper *helper = &gui.helpers[ gui.helper_count ++ ];
65 helper->bindstr = bindstr;
66 helper->text = text;
67 }
68
69 static
70 void gui_draw(void)
71 {
72 if( v3_dist2(localplayer.rb.co,gui.trick_co) > 2.0f ){
73 gui.trick_type = k_guitrick_type_none;
74 }
75
76 if( gui.trick_type == k_guitrick_type_pump ){
77 gui_helper_action(axis_display_string(k_sraxis_grab),"Crouch");
78 }
79 else if( gui.trick_type == k_guitrick_type_backflip ){
80 gui_helper_action(joystick_display_string(k_srjoystick_steer,1),"Flip");
81 }
82 else if( gui.trick_type == k_guitrick_type_ollie ){
83 gui_helper_action(button_display_string(k_srbind_jump),"Ollie");
84 }
85 else if( gui.trick_type == k_guitrick_type_trick ){
86 gui_helper_action(button_display_string(k_srbind_trick0),"Shuvit");
87 gui_helper_action(button_display_string(k_srbind_trick1),"Kickflip");
88 gui_helper_action(button_display_string(k_srbind_trick2),"Tre-Flip");
89 }
90 else if( gui.trick_type == k_guitrick_type_isc ){
91 gui_helper_action(button_display_string(k_srbind_camera),"Camera");
92 gui_helper_action(button_display_string(k_srbind_use), "Skate/Walk");
93 }
94
95 camera ortho;
96
97 float fl = 0.0f,
98 fr = vg.window_x,
99 fb = 0.0f,
100 ft = vg.window_y,
101 rl = 1.0f / (fr-fl),
102 tb = 1.0f / (ft-fb);
103
104 m4x4_zero( ortho.mtx.p );
105 ortho.mtx.p[0][0] = 2.0f * rl;
106 ortho.mtx.p[1][1] = 2.0f * tb;
107 ortho.mtx.p[3][0] = (fr + fl) * -rl;
108 ortho.mtx.p[3][1] = (ft + fb) * -tb;
109 ortho.mtx.p[3][3] = 1.0f;
110 m4x3_identity( ortho.transform );
111 camera_update_view( &ortho );
112 m4x4_mul( ortho.mtx.p, ortho.mtx.v, ortho.mtx.pv ); /* HACK */
113 camera_finalize( &ortho );
114
115
116 /* icons */
117 font3d_bind( &gui.font, k_font_shader_default, 0, NULL, &ortho );
118 mesh_bind( &gui.icons_mesh );
119
120 m4x3f mmdl;
121 m4x3_identity( mmdl );
122 shader_model_font_uMdl( mmdl );
123
124 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
125 glActiveTexture( GL_TEXTURE0 );
126 glBindTexture( GL_TEXTURE_2D, gui.icons_texture );
127 shader_model_font_uTexMain( 0 );
128
129 for( u32 i=0; i<gui.icon_draw_count; i++ ){
130 struct icon_call *call = &gui.icon_draw_buffer[i];
131 shader_model_font_uOffset( call->location );
132
133 mdl_submesh *sm = gui.icons[ call->icon ];
134 if( sm )
135 mdl_draw_submesh( sm );
136 }
137
138 gui.icon_draw_count = 0;
139
140
141
142 gui.factive = vg_lerpf( gui.factive, gui.helper_count?1.0f:0.0f,
143 vg.time_frame_delta*2.0f );
144
145 if( gui.factive > 0.01f ){
146 /* draw bottom bar */
147 glEnable(GL_BLEND);
148 glDisable(GL_DEPTH_TEST);
149 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
150 glBlendEquation(GL_FUNC_ADD);
151
152 shader_blitcolour_use();
153 shader_blitcolour_uColour( (v4f){ 0.0f, 0.0f, 0.0f, gui.factive*0.8f } );
154 render_fsquad1();
155 }
156
157 f64 loc_t = (vg.time_real - gui.location_time) / 5.0;
158 if( (loc_t < 1.0) && (gui.location_time != 0.0) ){
159 /* yep this code is a mess, i dont care anymore */
160 glEnable(GL_BLEND);
161 glDisable(GL_DEPTH_TEST);
162 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
163 glBlendEquation(GL_FUNC_ADD);
164
165 f32 t = 1.0f-vg_minf(1.0f,vg_minf(loc_t*20.0f,2.0f-loc_t*2.0f)),
166 o = 1.0f-t*t*(2.0f-t);
167
168 shader_blitcolour_use();
169 shader_blitcolour_uColour( (v4f){ 0.0f, 0.0f, 0.0f, o*0.5f } );
170 render_fsquad2();
171
172 f32 dy = ft/0.79f,
173 scale = dy*0x1p-4f*0.5f;
174
175 m3x3_identity( mmdl );
176 m3x3_scale( mmdl, (v3f){scale,scale,scale} );
177 v3_zero( mmdl[3] );
178
179 f32 pad = dy*0x1p-4f*0.125f;
180 f32 w = font3d_string_width( 2, gui.location );
181
182 mmdl[3][0] = fr*0.5f - w*scale*0.5f;
183 mmdl[3][1] = 0.3f*ft+pad*2.0f;
184
185 font3d_bind( &gui.font, k_font_shader_default, 0, NULL, &ortho );
186 shader_model_font_uColour( (v4f){1.2f,1.2f,1.2f,o} );
187 font3d_simple_draw( 2, gui.location, &ortho, mmdl );
188 }
189 else
190 font3d_bind( &gui.font, k_font_shader_default, 0, NULL, &ortho );
191
192 float dy = ft/0.79f,
193 scale = dy*0x1p-4f*0.75f;
194
195 m3x3_identity( mmdl );
196 m3x3_scale( mmdl, (v3f){scale,scale,scale} );
197 v3_zero( mmdl[3] );
198
199 float pad = dy*0x1p-4f*0.125f;
200 mmdl[3][0] = pad*2.0f;
201 mmdl[3][1] = pad;
202
203 for( u32 i=0; i<gui.helper_count; i++ ){
204 struct gui_helper *helper = &gui.helpers[i];
205
206 font3d_setcolour( (v4f){1.0f,1.0f,1.0f,1.0f} );
207 font3d_simple_draw( 2, helper->bindstr, &ortho, mmdl );
208
209 const char *make_smaller = "\x02\xaf\x03 ";
210 font3d_draw( make_smaller );
211 font3d_draw( helper->text );
212
213 float w = gui_font3d.offset[0]+1.0f;
214 mmdl[3][0] += w*scale;
215 }
216
217 gui.helper_count = 0;
218 }
219
220 static
221 int gui_location_print_ccmd( int argc, const char *argv[] ){
222 if( argc > 0 ){
223 char new_loc[64];
224 vg_str str;
225 vg_strnull( &str, new_loc, 64 );
226 for( int i=0; i<argc; i++ ){
227 vg_strcat( &str, argv[i] );
228 vg_strcat( &str, " " );
229 }
230 if( !strcmp(gui.location,new_loc) ) return 0;
231 vg_strncpy( new_loc, gui.location, 64, k_strncpy_always_add_null );
232 gui.location_time = vg.time_real;
233 }
234 return 0;
235 }
236
237 static int gui_showtrick_ccmd( int argc, const char *argv[] ){
238 if( argc == 1 ){
239 enum guitrick_type type = k_guitrick_type_none;
240 if( !strcmp( argv[0], "pump" ) ) type = k_guitrick_type_pump;
241 else if( !strcmp( argv[0], "flip" ) ) type = k_guitrick_type_backflip;
242 else if( !strcmp( argv[0], "ollie" ) ) type = k_guitrick_type_ollie;
243 else if( !strcmp( argv[0], "trick" ) ) type = k_guitrick_type_trick;
244 else if( !strcmp( argv[0], "misc" ) ) type = k_guitrick_type_isc;
245 else return 1;
246 gui.trick_type = type;
247 v3_copy( localplayer.rb.co, gui.trick_co );
248 return 0;
249 }
250 return 1;
251 }
252
253 static void gui_draw_icon( enum gui_icon icon, v2f co, f32 size ){
254 if( gui.icon_draw_count == vg_list_size(gui.icon_draw_buffer) )
255 return;
256
257 struct icon_call *call = &gui.icon_draw_buffer[ gui.icon_draw_count ++ ];
258
259 call->icon = icon;
260 call->location[0] = co[0] * (f32)vg.window_x;
261 call->location[1] = co[1] * (f32)vg.window_y;
262 call->location[2] = 0.0f;
263 call->location[3] = size * (f32)vg.window_x;
264 }
265
266 static mdl_submesh *gui_find_icon( const char *name ){
267 mdl_mesh *mesh = mdl_find_mesh( &gui.model_icons, name );
268 if( mesh ){
269 if( mesh->submesh_count ){
270 return mdl_arritm( &gui.model_icons.submeshs, mesh->submesh_start );
271 }
272 }
273
274 return NULL;
275 }
276
277 static void gui_init(void){
278 font3d_load( &gui.font, "models/rs_font.mdl", vg_mem.rtmemory );
279 vg_console_reg_cmd( "gui_location", gui_location_print_ccmd, NULL );
280 vg_console_reg_cmd( "showtrick", gui_showtrick_ccmd, NULL );
281
282 /* load icons */
283 void *alloc = vg_mem.rtmemory;
284 mdl_open( &gui.model_icons, "models/rs_icons.mdl", alloc );
285 mdl_load_metadata_block( &gui.model_icons, alloc );
286
287 gui.icons[ k_gui_icon_tick ] = gui_find_icon( "icon_tick" );
288 gui.icons[ k_gui_icon_exclaim ] = gui_find_icon( "icon_exclaim" );
289 gui.icons[ k_gui_icon_board ] = gui_find_icon( "icon_board" );
290 gui.icons[ k_gui_icon_world ] = gui_find_icon( "icon_world" );
291 gui.icons[ k_gui_icon_rift ] = gui_find_icon( "icon_rift" );
292
293 vg_linear_clear( vg_mem.scratch );
294 if( !mdl_arrcount( &gui.model_icons.textures ) )
295 vg_fatal_error( "No texture in menu file" );
296 mdl_texture *tex0 = mdl_arritm( &gui.model_icons.textures, 0 );
297 void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
298 mdl_fread_pack_file( &gui.model_icons, &tex0->file, data );
299 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
300 VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
301 &gui.icons_texture );
302
303 mdl_async_load_glmesh( &gui.model_icons, &gui.icons_mesh );
304 mdl_close( &gui.model_icons );
305 }
306
307 #endif /* GUI_H */