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