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