fix try to load level -1
[fishladder.git] / fishladder.c
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 //#define VG_STEAM
4 #include "vg/vg.h"
5 #include "fishladder_resources.h"
6
7 const char *level_pack_1[] = {
8 "level0",
9 "level1",
10 "level2",
11 "level3",
12 "level4"
13 };
14
15 const char *level_pack[] =
16 {
17 // Level 0
18 "#########;\n"
19 "###-#####;acac\n"
20 "## ##;\n"
21 "## ##;\n"
22 "## ##;\n"
23 "## ##;\n"
24 "#####+###;acac\n"
25 "#########;\n",
26
27 // Level 1
28 "#########;\n"
29 "##-###-##;b,b\n"
30 "## ##;\n"
31 "## ##;\n"
32 "## ##;\n"
33 "## ##;\n"
34 "## ##;\n"
35 "####+####;bb\n"
36 "#########;\n",
37
38 // Level 2
39 "###########;\n"
40 "#####-#####;bbbbb\n"
41 "## ##;\n"
42 "## ###;\n"
43 "## # ##;\n"
44 "## ##;\n"
45 "###+##+####;bbb,bb\n"
46 "###########;\n",
47
48 // Level 3
49 "#############;\n"
50 "###-#####-###;a,aaa\n"
51 "## ##;\n"
52 "## ##;\n"
53 "## ##;\n"
54 "## ##;\n"
55 "## ##;\n"
56 "## ##;\n"
57 "######+######;aaaa\n"
58 "#############;\n",
59
60 // Level 4
61 "#############;\n"
62 "###-#####-###;aaa,aa\n"
63 "## ##;\n"
64 "## ##;\n"
65 "## ##;\n"
66 "## ##;\n"
67 "## ##;\n"
68 "## ##;\n"
69 "###+#####+###;aa,aaa\n"
70 "#############;\n",
71
72 // Level 5
73 "###############;\n"
74 "####-##########;abcb\n"
75 "## ##;\n"
76 "## ##;\n"
77 "## ##;\n"
78 "## ##;\n"
79 "## ##;\n"
80 "## ##;\n"
81 "## ##;\n"
82 "## ##;\n"
83 "## ##;\n"
84 "##########+####;bcba\n"
85 "###############;\n"
86 };
87
88 m3x3f m_projection;
89 m3x3f m_view;
90 m3x3f m_mdl;
91
92 #define FLAG_INPUT 0x1
93 #define FLAG_OUTPUT 0x2
94 #define FLAG_CANAL 0x4
95 #define FLAG_WALL 0x8
96 #define FLAG_FLIP_FLOP 0x100
97 #define FLAG_FLIP_ROTATING 0x200
98
99 /*
100 0000 0 | 0001 1 | 0010 2 | 0011 3
101 | | | | |
102 X | X= | X | X=
103 | | |
104 0100 4 | 0101 5 | 0110 6 | 0111 7
105 | | | | |
106 =X | =X= | =X | =X=
107 | | |
108 1000 8 | 1001 9 | 1010 10 | 1011 11
109 | | | | |
110 X | X= | X | X=
111 | | | | | | |
112 1100 12 | 1101 13 | 1110 14 | 1111 15
113 | | | | |
114 =X | =X= | =X | =X=
115 | | | | | | |
116 */
117
118 enum cell_type
119 {
120 k_cell_type_ramp_right = 3,
121 k_cell_type_ramp_left = 6,
122 k_cell_type_split = 7,
123 k_cell_type_merge = 13
124 };
125
126 v3f colour_sets[] =
127 { { 0.9f, 0.6f, 0.20f },
128 { 0.2f, 0.9f, 0.14f },
129 { 0.4f, 0.8f, 1.00f } };
130
131 static void colour_code_v3( char cc, v3f target )
132 {
133 if( cc >= 'a' && cc <= 'z' )
134 {
135 int id = cc - 'a';
136
137 if( id < vg_list_size( colour_sets ) )
138 {
139 v3_copy( colour_sets[ id ], target );
140 return;
141 }
142 }
143
144 v3_copy( (v3f){0.0f,0.0f,0.0f}, target );
145 }
146
147 struct mesh
148 {
149 GLuint vao, vbo;
150 u32 elements;
151 };
152
153 static void init_mesh( struct mesh *m, float *tris, u32 length )
154 {
155 m->elements = length/3;
156 glGenVertexArrays( 1, &m->vao );
157 glGenBuffers( 1, &m->vbo );
158
159 glBindVertexArray( m->vao );
160 glBindBuffer( GL_ARRAY_BUFFER, m->vbo );
161 glBufferData( GL_ARRAY_BUFFER, length*sizeof(float), tris, GL_STATIC_DRAW );
162
163 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
164 glEnableVertexAttribArray( 0 );
165
166 VG_CHECK_GL();
167 }
168
169 static void free_mesh( struct mesh *m )
170 {
171 glDeleteVertexArrays( 1, &m->vao );
172 glDeleteBuffers( 1, &m->vbo );
173 }
174
175 static void draw_mesh( int const start, int const count )
176 {
177 glDrawArrays( GL_TRIANGLES, start*3, count*3 );
178 }
179
180 static void use_mesh( struct mesh *m )
181 {
182 glBindVertexArray( m->vao );
183 }
184
185 struct world
186 {
187 struct cell
188 {
189 u32 state;
190 u8 config;
191 }
192 *data;
193
194 u32 frame;
195
196 u32 sim_frame;
197 float sim_start;
198 int simulating;
199
200 float frame_lerp;
201
202 struct cell_terminal
203 {
204 // TODO: Split into input/output structures
205 char *conditions;
206 char recv[12];
207 int recv_count;
208 int id;
209 }
210 *io;
211
212 u32 w, h;
213
214 struct mesh tile, circle;
215
216 GLuint background_data;
217 GLuint random_samples;
218
219 int selected, tile_x, tile_y;
220 v2f tile_pos;
221
222 struct fish
223 {
224 v2i pos;
225 v2i dir;
226 int alive;
227 char payload;
228 float death_time;
229 v2f physics_v;
230 v2f physics_co;
231 }
232 fishes[16];
233
234 int num_fishes;
235
236 char map_name[128];
237 } world = {};
238
239 static void map_free(void)
240 {
241 for( int i = 0; i < arrlen( world.io ); i ++ )
242 arrfree( world.io[ i ].conditions );
243
244 arrfree( world.data );
245 arrfree( world.io );
246
247 world.w = 0;
248 world.h = 0;
249 world.data = NULL;
250 world.io = NULL;
251 }
252
253 static void map_reclassify( v2i start, v2i end, int update_texbuffer );
254 static int map_load( const char *str, const char *name )
255 {
256 map_free();
257
258 char const *c = str;
259
260 // Scan for width
261 for(;; world.w ++)
262 {
263 if( str[world.w] == ';' )
264 break;
265 else if( !str[world.w] )
266 {
267 vg_error( "Unexpected EOF when parsing level\n" );
268 return 0;
269 }
270 }
271
272 struct cell *row = arraddnptr( world.data, world.w );
273 int cx = 0;
274 int reg_start = 0, reg_end = 0;
275
276 for(;;)
277 {
278 if( !*c )
279 break;
280
281 if( *c == ';' )
282 {
283 c ++;
284
285 // Parse attribs
286 if( *c != '\n' )
287 {
288 while( *c )
289 {
290 if( reg_start < reg_end )
291 {
292 if( *c >= 'a' && *c <= 'z' )
293 {
294 arrpush( world.io[ reg_start ].conditions, *c );
295 }
296 else
297 {
298 if( *c == ',' || *c == '\n' )
299 {
300 reg_start ++;
301
302 if( *c == '\n' )
303 break;
304 }
305 else
306 {
307 vg_error( "Unkown attribute '%c' (row: %u)\n", *c, world.h );
308 return 0;
309 }
310 }
311 }
312 else
313 {
314 vg_error( "Too many values to assign (row: %u)\n", world.h );
315 return 0;
316 }
317
318 c ++;
319 }
320 }
321
322 if( reg_start != reg_end )
323 {
324 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world.h, reg_start, reg_end );
325 return 0;
326 }
327
328 if( cx != world.w )
329 {
330 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world.h, cx, world.w );
331 return 0;
332 }
333
334 row = arraddnptr( world.data, world.w );
335 cx = 0;
336 world.h ++;
337 reg_end = reg_start = arrlen( world.io );
338 }
339 else
340 {
341 if( cx == world.w )
342 {
343 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world.h, cx, world.w );
344 return 0;
345 }
346
347 // Tile initialization
348 // row[ cx ] .. etc
349 struct cell *cell = &row[ cx ];
350
351 if( *c == '+' || *c == '-' )
352 {
353 struct cell_terminal term = { .id = cx + world.h*world.w };
354 arrpush( world.io, term );
355 cell->state = *c == '+'? FLAG_INPUT: FLAG_OUTPUT;
356 reg_end ++;
357 }
358 else if( *c == '#' ) cell->state = FLAG_WALL;
359 else if( *c == '*' ) cell->state = FLAG_CANAL;
360 else cell->state = 0x00;
361
362 cx ++;
363 }
364
365 c ++;
366 }
367
368 // Update data texture to fill out the background
369 {
370 u8 info_buffer[64*64*4];
371 for( int i = 0; i < 64*64; i ++ )
372 {
373 u8 *px = &info_buffer[i*4];
374 px[0] = 255;
375 px[1] = 0;
376 px[2] = 0;
377 px[3] = 0;
378 }
379
380 glBindTexture( GL_TEXTURE_2D, world.background_data );
381 glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 64, 64, GL_RGBA, GL_UNSIGNED_BYTE, info_buffer );
382 }
383
384 map_reclassify( NULL, NULL, 1 );
385 vg_success( "Map '%s' loaded! (%u:%u)\n", name, world.w, world.h );
386
387 strncpy( world.map_name, name, vg_list_size( world.map_name )-1 );
388 return 1;
389 }
390
391 static struct cell *pcell( v2i pos )
392 {
393 return &world.data[ pos[1]*world.w + pos[0] ];
394 }
395
396 static void map_serialize( FILE *stream )
397 {
398 for( int y = 0; y < world.h; y ++ )
399 {
400 for( int x = 0; x < world.w; x ++ )
401 {
402 struct cell *cell = pcell( (v2i){ x, y } );
403
404 if( cell->state & FLAG_WALL ) fputc( '#', stream );
405 else if( cell->state & FLAG_INPUT ) fputc( '+', stream );
406 else if( cell->state & FLAG_OUTPUT ) fputc( '-', stream );
407 else if( cell->state & FLAG_CANAL ) fputc( '*', stream );
408 else fputc( ' ', stream );
409 }
410
411 fputc( ';', stream );
412
413 int terminal_write_count = 0;
414
415 for( int x = 0; x < world.w; x ++ )
416 {
417 for( int i = 0; i < arrlen( world.io ); i ++ )
418 {
419 struct cell_terminal *term = &world.io[i];
420 if( term->id == y*world.w+x )
421 {
422 if( terminal_write_count )
423 fputc( ',', stream );
424 terminal_write_count ++;
425
426 for( int j = 0; j < arrlen( term->conditions ); j ++ )
427 fputc( term->conditions[j], stream );
428 }
429 }
430 }
431
432 fputc( '\n', stream );
433 }
434 }
435
436 int main( int argc, char *argv[] )
437 {
438 vg_init( argc, argv, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
439 }
440
441 static int console_save_map( int argc, char const *argv[] )
442 {
443 char map_path[ 256 ];
444
445 strcpy( map_path, "sav/" );
446 strcat( map_path, world.map_name );
447 strcat( map_path, ".map" );
448
449 FILE *test_writer = fopen( map_path, "wb" );
450 if( test_writer )
451 {
452 map_serialize( test_writer );
453
454 fclose( test_writer );
455 return 1;
456 }
457 else
458 {
459 vg_error( "Unable to open stream for writing\n" );
460 return 0;
461 }
462 }
463
464 static int console_load_map( int argc, char const *argv[] )
465 {
466 char map_path[ 256 ];
467
468 if( argc >= 1 )
469 {
470 // try from saves
471 strcpy( map_path, "sav/" );
472 strcat( map_path, argv[0] );
473 strcat( map_path, ".map" );
474
475 char *text_source = vg_textasset_read( map_path );
476
477 if( !text_source )
478 {
479 strcpy( map_path, "maps/" );
480 strcat( map_path, argv[0] );
481 strcat( map_path, ".map" );
482
483 text_source = vg_textasset_read( map_path );
484 }
485
486 if( text_source )
487 {
488 map_load( text_source, argv[0] );
489 free( text_source );
490 return 1;
491 }
492 else
493 {
494 vg_error( "Missing maps '%s'\n", argv[0] );
495 return 0;
496 }
497 }
498 else
499 {
500 vg_error( "Missing argument <map_path>\n" );
501 return 0;
502 }
503 }
504
505 static int console_changelevel( int argc, char const *argv[] )
506 {
507 if( argc >= 1 )
508 {
509 // Save current level
510 if( console_save_map( 0, NULL ) )
511 if( console_load_map( argc, argv ) )
512 return 1;
513 }
514 else
515 {
516 vg_error( "Missing argument <map_path>\n" );
517 }
518
519 return 0;
520 }
521
522 void vg_start(void)
523 {
524 vg_function_push( (struct vg_cmd){
525 .name = "map_write",
526 .function = console_save_map
527 });
528
529 vg_function_push( (struct vg_cmd){
530 .name = "map_load",
531 .function = console_load_map
532 });
533
534 vg_function_push( (struct vg_cmd){
535 .name = "changelevel",
536 .function = console_changelevel
537 });
538
539 // Quad mesh
540 {
541 float quad_mesh[] =
542 {
543 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
544 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
545
546 0.0f, 0.0f, 0.0f, 1.0f, 4.0f, 1.0f,
547 0.0f, 0.0f, 4.0f, 1.0f, 4.0f, 0.0f
548 };
549
550 init_mesh( &world.tile, quad_mesh, vg_list_size(quad_mesh) );
551 }
552
553 // Circle mesh
554 {
555 float circle_mesh[32*6*3];
556 int res = vg_list_size( circle_mesh ) / (6*3);
557
558 for( int i = 0; i < res; i ++ )
559 {
560 v2f v0 = { sinf( ((float)i/(float)res)*VG_TAUf ), cosf( ((float)i/(float)res)*VG_TAUf ) };
561 v2f v1 = { sinf( ((float)(i+1)/(float)res)*VG_TAUf ), cosf( ((float)(i+1)/(float)res)*VG_TAUf ) };
562
563 circle_mesh[ i*6+0 ] = 0.0f;
564 circle_mesh[ i*6+1 ] = 0.0f;
565
566 v2_copy( v0, circle_mesh + 32*6 + i*12 );
567 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+2 );
568 v2_copy( v1, circle_mesh + 32*6 + i*12+4 );
569
570 v2_copy( v1, circle_mesh + 32*6 + i*12+6 );
571 v2_muls( v1, 0.8f, circle_mesh + 32*6 + i*12+8 );
572 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+10 );
573
574 v2_copy( v0, circle_mesh + i*6+4 );
575 v2_copy( v1, circle_mesh + i*6+2 );
576 v2_copy( v0, circle_mesh+i*6+4 );
577 v2_copy( v1, circle_mesh+i*6+2 );
578 }
579
580 init_mesh( &world.circle, circle_mesh, vg_list_size( circle_mesh ) );
581 }
582
583 // Create info data texture
584 {
585 glGenTextures( 1, &world.background_data );
586 glBindTexture( GL_TEXTURE_2D, world.background_data );
587 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
588 vg_tex2d_nearest();
589 }
590
591 // Create random smaples texture
592 {
593 u8 *data = malloc(512*512*2);
594 for( int i = 0; i < 512*512*2; i ++ )
595 data[ i ] = rand()/(RAND_MAX/255);
596
597 glGenTextures( 1, &world.random_samples );
598 glBindTexture( GL_TEXTURE_2D, world.random_samples );
599 glTexImage2D( GL_TEXTURE_2D, 0, GL_RG, 512, 512, 0, GL_RG, GL_UNSIGNED_BYTE, data );
600 vg_tex2d_linear();
601 vg_tex2d_repeat();
602
603 free( data );
604 }
605
606 resource_load_main();
607
608 console_load_map( 1, level_pack_1 );
609 }
610
611 void vg_free(void)
612 {
613 console_save_map( 0, NULL );
614
615 resource_free_main();
616
617 glDeleteTextures( 1, &world.background_data );
618 glDeleteTextures( 1, &world.random_samples );
619
620 free_mesh( &world.tile );
621 free_mesh( &world.circle );
622
623 map_free();
624 }
625
626 static int cell_interactive( v2i co )
627 {
628 // Bounds check
629 if( co[0] < 2 || co[0] >= world.w-2 || co[1] < 2 || co[1] >= world.h-2 )
630 return 0;
631
632 // Flags check
633 if( world.data[ world.w*co[1] + co[0] ].state & (FLAG_WALL|FLAG_INPUT|FLAG_OUTPUT) )
634 return 0;
635
636 // List of 3x3 configurations that we do not allow
637 static u32 invalid_src[][9] =
638 {
639 { 0,1,0,
640 1,1,1,
641 0,1,0
642 },
643 { 0,0,0,
644 0,1,1,
645 0,1,1
646 },
647 { 0,0,0,
648 1,1,0,
649 1,1,0
650 },
651 { 0,1,1,
652 0,1,1,
653 0,0,0
654 },
655 { 1,1,0,
656 1,1,0,
657 0,0,0
658 },
659 { 0,1,0,
660 0,1,1,
661 0,1,0
662 },
663 { 0,1,0,
664 1,1,0,
665 0,1,0
666 }
667 };
668
669 // Statically compile invalid configurations into bitmasks
670 static u32 invalid[ vg_list_size(invalid_src) ];
671
672 for( int i = 0; i < vg_list_size(invalid_src); i ++ )
673 {
674 u32 comped = 0x00;
675
676 for( int j = 0; j < 3; j ++ )
677 for( int k = 0; k < 3; k ++ )
678 comped |= invalid_src[i][ j*3+k ] << ((j*5)+k);
679
680 invalid[i] = comped;
681 }
682
683 // Extract 5x5 grid surrounding tile
684 u32 blob = 0x1000;
685 for( int y = co[1]-2; y < co[1]+3; y ++ )
686 for( int x = co[0]-2; x < co[0]+3; x ++ )
687 {
688 struct cell *cell = pcell((v2i){x,y});
689
690 if( cell && (cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT)) )
691 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
692 }
693
694 // Run filter over center 3x3 grid to check for invalid configurations
695 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
696 for( int i = 0; i < vg_list_size(kernel); i ++ )
697 {
698 if( blob & (0x1 << (6+kernel[i])) )
699 {
700 u32 window = blob >> kernel[i];
701
702 for( int j = 0; j < vg_list_size(invalid); j ++ )
703 if((window & invalid[j]) == invalid[j])
704 return 0;
705 }
706 }
707
708 return 1;
709 }
710
711 static void map_reclassify( v2i start, v2i end, int update_texbuffer )
712 {
713 v2i full_start = { 1,1 };
714 v2i full_end = { world.w-1, world.h-1 };
715
716 if( !start || !end )
717 {
718 start = full_start;
719 end = full_end;
720 }
721
722 // Texture data
723 u8 info_buffer[64*64*4];
724 u32 pixel_id = 0;
725
726 int px0 = vg_max( start[0], full_start[0] ),
727 px1 = vg_min( end[0], full_end[0] ),
728 py0 = vg_max( start[1], full_start[1] ),
729 py1 = vg_min( end[1], full_end[1] );
730
731 for( int y = py0; y < py1; y ++ )
732 {
733 for( int x = px0; x < px1; x ++ )
734 {
735 struct cell *cell = pcell((v2i){x,y});
736
737 v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
738
739 u8 height = 0;
740 u8 config = 0x00;
741
742 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
743 {
744 for( int i = 0; i < vg_list_size( dirs ); i ++ )
745 {
746 struct cell *neighbour = pcell((v2i){x+dirs[i][0], y+dirs[i][1]});
747 if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
748 config |= 0x1 << i;
749 }
750
751 height = 128;
752 }
753 else
754 {
755 if( cell->state & FLAG_WALL )
756 height = 255;
757
758 config = 0xF;
759 }
760
761 pcell((v2i){x,y})->config = config;
762
763 u8 *info_px = &info_buffer[ (pixel_id ++)*4 ];
764 info_px[0] = height;
765 info_px[1] = cell->state & FLAG_WALL? 0: 255;
766 info_px[2] = 0;
767 info_px[3] = 0;
768 }
769 }
770
771 if( update_texbuffer )
772 {
773 glBindTexture( GL_TEXTURE_2D, world.background_data );
774 glTexSubImage2D( GL_TEXTURE_2D, 0, px0 + 16, py0 + 16, px1-px0, py1-py0, GL_RGBA, GL_UNSIGNED_BYTE, info_buffer );
775 }
776 }
777
778 v2f const curve_3[] = {{0.5f,1.0f},{0.5f,0.625f},{0.625f,0.5f},{1.0f,0.5f}};
779 v2f const curve_6[] = {{0.5f,1.0f},{0.5f,0.625f},{0.375f,0.5f},{0.0f,0.5f}};
780 v2f const curve_9[] = {{1.0f,0.5f},{0.625f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
781 v2f const curve_12[]= {{0.0f,0.5f},{0.375f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
782
783 v2f const curve_2[] = {{0.5f,1.0f},{0.5f,0.8f},{0.5f,0.3f},{0.5f,0.2f}};
784 v2f const curve_8[] = {{0.5f,0.8f},{0.5f,0.5f},{0.5f,0.3f},{0.5f,0.0f}};
785
786 v2f const curve_7[] = {{0.5f,0.8438f},{0.875f,0.8438f},{0.625f,0.5f},{1.0f,0.5f}};
787 v2f const curve_7_1[] = {{0.5f,0.8438f},{1.0f-0.875f,0.8438f},{1.0-0.625f,0.5f},{0.0f,0.5f}};
788
789 float const curve_7_linear_section = 0.1562f;
790
791 void vg_update(void)
792 {
793 static int curlevel = 0;
794 int changelvl = curlevel;
795 if( vg_get_button_down( "prev" ) ) { if( curlevel > 0 ) changelvl --; }
796 else if( vg_get_button_down( "next" ) ) { if( curlevel < vg_list_size( level_pack )-1 ) changelvl ++; }
797
798 if( changelvl != curlevel )
799 {
800 console_changelevel( 1, level_pack + changelvl );
801 curlevel = changelvl;
802
803 // TEMP!!! code dupe
804 world.simulating = 0;
805 world.num_fishes = 0;
806 world.sim_frame = 0;
807
808 for( int i = 0; i < arrlen( world.io ); i ++ )
809 world.io[i].recv_count = 0;
810
811 vg_info( "Stopping simulation!\n" );
812 }
813
814 // Fit within screen
815
816 float r1 = (float)vg_window_y / (float)vg_window_x,
817 r2 = (float)world.h / (float)world.w,
818 size;
819
820 size = ( r2 < r1? (float)world.w * 0.5f: ((float)world.h * 0.5f) / r1 ) + 2.5f;
821 m3x3_projection( m_projection, -size, size, -size*r1, size*r1 );
822
823 v3f origin;
824 origin[0] = floorf( -0.5f * world.w );
825 origin[1] = floorf( -0.5f * world.h );
826 origin[2] = 0.0f;
827
828 m3x3_identity( m_view );
829 m3x3_translate( m_view, origin );
830 m3x3_mul( m_projection, m_view, vg_pv );
831 vg_projection_update();
832
833 // Input stuff
834 v2_copy( vg_mouse_ws, world.tile_pos );
835
836 world.tile_x = floorf( world.tile_pos[0] );
837 world.tile_y = floorf( world.tile_pos[1] );
838
839 // Tilemap editing
840 if( !world.simulating )
841 {
842 if( cell_interactive( (v2i){ world.tile_x, world.tile_y } ))
843 {
844 world.selected = world.tile_y * world.w + world.tile_x;
845
846 if( vg_get_button_down("primary") )
847 {
848 world.data[ world.selected ].state ^= FLAG_CANAL;
849
850 if( world.data[ world.selected ].state & FLAG_CANAL )
851 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 3, 6 );
852 else
853 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 0, 3 );
854
855 map_reclassify( (v2i){ world.tile_x -2, world.tile_y -2 },
856 (v2i){ world.tile_x +2, world.tile_y +2 }, 1 );
857 }
858 }
859 else
860 world.selected = -1;
861 }
862 else world.selected = -1;
863
864 // Simulation stop/start
865 if( vg_get_button_down("go") )
866 {
867 if( world.simulating )
868 {
869 world.simulating = 0;
870 world.num_fishes = 0;
871 world.sim_frame = 0;
872
873 for( int i = 0; i < arrlen( world.io ); i ++ )
874 world.io[i].recv_count = 0;
875
876 vg_info( "Stopping simulation!\n" );
877
878 sfx_system_fadeout( &audio_system_balls_rolling, 44100 );
879 }
880 else
881 {
882 vg_success( "Starting simulation!\n" );
883
884 sfx_set_playrnd( &audio_rolls, &audio_system_balls_rolling, 0, 1 );
885
886 world.simulating = 1;
887 world.num_fishes = 0;
888 world.sim_frame = 0;
889 world.sim_start = vg_time;
890
891 for( int i = 0; i < world.w*world.h; i ++ )
892 {
893 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
894 }
895
896 for( int i = 0; i < arrlen( world.io ); i ++ )
897 world.io[i].recv_count = 0;
898 }
899 }
900
901 // Fish ticks
902 if( world.simulating )
903 {
904 while( world.sim_frame < (int)((vg_time-world.sim_start)*2.0f) )
905 {
906 //vg_info( "frame: %u\n", world.sim_frame );
907 sfx_set_playrnd( &audio_random, &audio_system_balls_switching, 0, 9 );
908
909 // Update splitter deltas
910 for( int i = 0; i < world.h*world.w; i ++ )
911 {
912 struct cell *cell = &world.data[i];
913 if( cell->config == k_cell_type_split )
914 {
915 cell->state &= ~FLAG_FLIP_ROTATING;
916 }
917 }
918
919 // Update fish positions
920 for( int i = 0; i < world.num_fishes; i ++ )
921 {
922 struct fish *fish = &world.fishes[i];
923 struct cell *cell_current = pcell( fish->pos );
924
925 if( fish->alive == -1 )
926 fish->alive = 0;
927
928 if( fish->alive != 1 )
929 continue;
930
931 // Apply to output
932 if( cell_current->state & FLAG_OUTPUT )
933 {
934 for( int j = 0; j < arrlen( world.io ); j ++ )
935 {
936 struct cell_terminal *term = &world.io[j];
937
938 if( term->id == fish->pos[1]*world.w + fish->pos[0] )
939 {
940 term->recv[ term->recv_count ++ ] = fish->payload;
941 break;
942 }
943 }
944
945 fish->alive = 0;
946 continue;
947 }
948
949 if( cell_current->config == k_cell_type_split )
950 {
951 // Flip flop L/R
952 fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
953 fish->dir[1] = 0;
954
955 cell_current->state ^= FLAG_FLIP_FLOP;
956 }
957 else if( cell_current->config == k_cell_type_merge )
958 {
959 // Can only move up
960 fish->dir[0] = 0;
961 fish->dir[1] = -1;
962 }
963 else
964 {
965 struct cell *cell_next = pcell( (v2i){ fish->pos[0]+fish->dir[0], fish->pos[1]+fish->dir[1] } );
966 if( !(cell_next->state & (FLAG_CANAL|FLAG_OUTPUT)) )
967 {
968 // Try other directions for valid, so down, left, right..
969 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
970 vg_info( "Trying some other directions...\n" );
971
972 for( int j = 0; j < vg_list_size(dirs); j ++ )
973 {
974 if( (dirs[j][0] == -fish->dir[0]) && (dirs[j][1] == -fish->dir[1]) )
975 continue;
976
977 if( pcell( (v2i){ fish->pos[0]+dirs[j][0], fish->pos[1]+dirs[j][1] } )->state & (FLAG_CANAL|FLAG_OUTPUT) )
978 {
979 fish->dir[0] = dirs[j][0];
980 fish->dir[1] = dirs[j][1];
981 }
982 }
983 }
984 }
985
986 fish->pos[0] += fish->dir[0];
987 fish->pos[1] += fish->dir[1];
988
989 struct cell *cell_entry = pcell( fish->pos );
990
991 if( !(cell_entry->state & (FLAG_INPUT|FLAG_CANAL|FLAG_OUTPUT) ))
992 fish->alive = 0;
993 else
994 {
995 if( fish->dir[0] )
996 {
997 if( cell_entry->config == k_cell_type_split ||
998 cell_entry->config == k_cell_type_ramp_right ||
999 cell_entry->config == k_cell_type_ramp_left )
1000 {
1001 // Special death (FALL)
1002 v2_sub( fish->physics_co, fish->physics_v, fish->physics_v );
1003 v2_divs( fish->physics_v, vg_time_delta, fish->physics_v );
1004
1005 fish->alive = -2;
1006 vg_warn( "Special death (fall)\n" );
1007 continue;
1008 }
1009 }
1010
1011 if( cell_entry->config == k_cell_type_split )
1012 {
1013 sfx_set_playrnd( &audio_splitter, &audio_system_balls_important, 0, 1 );
1014 cell_entry->state |= FLAG_FLIP_ROTATING;
1015 }
1016 }
1017 }
1018
1019 // Check for collisions
1020 for( int i = 0; i < world.num_fishes; i ++ )
1021 {
1022 if( world.fishes[i].alive == 1 )
1023 {
1024 for( int j = i+1; j < world.num_fishes; j ++ )
1025 {
1026 if( (world.fishes[j].alive == 1) && (world.fishes[i].pos[0] == world.fishes[j].pos[0]) &&
1027 (world.fishes[i].pos[1] == world.fishes[j].pos[1]) )
1028 {
1029 // Shatter death (+0.5s)
1030 world.fishes[i].alive = -1;
1031 world.fishes[j].alive = -1;
1032 world.fishes[i].death_time = 0.5f;
1033 world.fishes[j].death_time = 0.5f;
1034 }
1035 }
1036 }
1037 }
1038
1039 // Spawn fishes
1040 for( int i = 0; i < arrlen( world.io ); i ++ )
1041 {
1042 struct cell_terminal *term = &world.io[ i ];
1043 int posx = term->id % world.w;
1044 int posy = (term->id - posx)/world.w;
1045 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1046
1047 if( is_input )
1048 {
1049 if( world.sim_frame < arrlen( term->conditions ) )
1050 {
1051 struct fish *fish = &world.fishes[world.num_fishes++];
1052 fish->pos[0] = posx;
1053 fish->pos[1] = posy;
1054 fish->alive = 1;
1055 fish->payload = term->conditions[world.sim_frame];
1056
1057 int can_spawn = 0;
1058
1059 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
1060 for( int j = 0; j < vg_list_size(dirs); j ++ )
1061 if( pcell( (v2i){ posx+dirs[j][0], posy+dirs[j][1] } )->state & FLAG_CANAL )
1062 {
1063 fish->dir[0] = dirs[j][0];
1064 fish->dir[1] = dirs[j][1];
1065 can_spawn = 1;
1066 break;
1067 }
1068
1069 if( !can_spawn )
1070 world.num_fishes--;
1071 }
1072 }
1073 }
1074
1075 world.sim_frame ++;
1076 }
1077
1078 float scaled_time = 0.0f;
1079 scaled_time = (vg_time-world.sim_start)*2.0f;
1080 world.frame_lerp = scaled_time - (float)world.sim_frame;
1081
1082 // Update positions
1083 for( int i = 0; i < world.num_fishes; i ++ )
1084 {
1085 struct fish *fish = &world.fishes[i];
1086
1087 if( fish->alive == 0 )
1088 continue;
1089
1090 if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
1091 continue; // Todo: particle thing?
1092
1093 if( fish->alive == -2 )
1094 {
1095 v2_muladds( fish->physics_v, (v2f){ 0.0, -9.8f }, vg_time_delta, fish->physics_v );
1096 v2_muladds( fish->physics_co, fish->physics_v, vg_time_delta, fish->physics_co );
1097 }
1098 else
1099 {
1100 struct cell *cell = pcell(fish->pos);
1101 v2f const *curve;
1102
1103 float t = world.frame_lerp;
1104
1105 v2_copy( fish->physics_co, fish->physics_v );
1106
1107 switch( cell->config )
1108 {
1109 case 13:
1110 if( fish->dir[0] == 1 )
1111 curve = curve_12;
1112 else
1113 curve = curve_9;
1114 break;
1115 case 2: curve = curve_2; break;
1116 case 8: curve = curve_8; break;
1117 case 3: curve = curve_3; break;
1118 case 6: curve = curve_6; break;
1119 case 9: curve = curve_9; break;
1120 case 12: curve = curve_12; break;
1121 case 7:
1122 if( t > curve_7_linear_section )
1123 {
1124 t -= curve_7_linear_section;
1125 t *= (1.0f/(1.0f-curve_7_linear_section));
1126
1127 curve = cell->state & FLAG_FLIP_FLOP? curve_7: curve_7_1;
1128 }
1129 else curve = NULL;
1130 break;
1131 default: curve = NULL; break;
1132 }
1133
1134 if( curve )
1135 {
1136 float t2 = t * t;
1137 float t3 = t * t * t;
1138
1139 float cA = 3.0f*t2 - 3.0f*t3;
1140 float cB = 3.0f*t3 - 6.0f*t2 + 3.0f*t;
1141 float cC = 3.0f*t2 - t3 - 3.0f*t + 1.0f;
1142
1143 fish->physics_co[0] = t3*curve[3][0] + cA*curve[2][0] + cB*curve[1][0] + cC*curve[0][0];
1144 fish->physics_co[1] = t3*curve[3][1] + cA*curve[2][1] + cB*curve[1][1] + cC*curve[0][1];
1145 fish->physics_co[0] += (float)fish->pos[0];
1146 fish->physics_co[1] += (float)fish->pos[1];
1147 }
1148 else
1149 {
1150 v2f origin;
1151 origin[0] = (float)fish->pos[0] + (float)fish->dir[0]*-0.5f + 0.5f;
1152 origin[1] = (float)fish->pos[1] + (float)fish->dir[1]*-0.5f + 0.5f;
1153
1154 fish->physics_co[0] = origin[0] + (float)fish->dir[0]*t;
1155 fish->physics_co[1] = origin[1] + (float)fish->dir[1]*t;
1156 }
1157 }
1158 }
1159 }
1160 }
1161
1162 static void render_tiles( v2i start, v2i end, v4f const regular_colour, v4f const selected_colour )
1163 {
1164 v2i full_start = { 0,0 };
1165 v2i full_end = { world.w, world.h };
1166
1167 if( !start || !end )
1168 {
1169 start = full_start;
1170 end = full_end;
1171 }
1172
1173 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
1174
1175 for( int y = start[1]; y < end[1]; y ++ )
1176 {
1177 for( int x = start[0]; x < end[0]; x ++ )
1178 {
1179 struct cell *cell = pcell((v2i){x,y});
1180 int selected = world.selected == y*world.w + x;
1181
1182 int tile_offsets[][2] =
1183 {
1184 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1185 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1186 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1187 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1188 };
1189
1190 int uv[2] = { 3, 0 };
1191
1192 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
1193 {
1194 uv[0] = tile_offsets[ cell->config ][0];
1195 uv[1] = tile_offsets[ cell->config ][1];
1196 } else continue;
1197
1198 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y, uv[0], uv[1] );
1199 if( selected )
1200 {
1201 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, selected_colour );
1202 draw_mesh( 0, 2 );
1203 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
1204 }
1205 else
1206 draw_mesh( 0, 2 );
1207 }
1208 }
1209 }
1210
1211 void vg_render(void)
1212 {
1213 glViewport( 0,0, vg_window_x, vg_window_y );
1214
1215 glDisable( GL_DEPTH_TEST );
1216 glClearColor( 0.369768f, 0.3654f, 0.42f, 1.0f );
1217 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1218
1219 v4f const colour_default = {1.0f, 1.0f, 1.0f, 1.0f};
1220 v4f const colour_selected = {0.90f, 0.92f, 1.0f, 1.0f};
1221
1222 // TILE SET RENDERING
1223 // todo: just slam everything into a mesh...
1224 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1225 // in ~3 subBuffers
1226 // Currently we're uploading a fair amount of data every frame anyway.
1227 // NOTE: this is for final optimisations ONLY!
1228 // ======================================================================
1229
1230 use_mesh( &world.tile );
1231
1232 // Draw background
1233
1234 if(1){
1235
1236 SHADER_USE( shader_background );
1237 glUniformMatrix3fv( SHADER_UNIFORM( shader_background, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1238
1239 glActiveTexture( GL_TEXTURE0 );
1240 glBindTexture( GL_TEXTURE_2D, world.background_data );
1241 glUniform1i( SHADER_UNIFORM( shader_background, "uTexMain" ), 0 );
1242
1243 glUniform3f( SHADER_UNIFORM( shader_background, "uOffset" ), -16, -16, 64 );
1244 glUniform1f( SHADER_UNIFORM( shader_background, "uVariance" ), 0.02f );
1245
1246 glActiveTexture( GL_TEXTURE1 );
1247 glBindTexture( GL_TEXTURE_2D, world.random_samples );
1248 glUniform1i( SHADER_UNIFORM( shader_background, "uSamplerNoise" ), 1 );
1249
1250 draw_mesh( 0, 2 );
1251
1252 }
1253
1254
1255 SHADER_USE( shader_tile_main );
1256
1257 m2x2f subtransform;
1258 m2x2_identity( subtransform );
1259 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1260 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1261 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 0.0f );
1262 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 0.0f );
1263
1264 glEnable(GL_BLEND);
1265 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1266 glBlendEquation(GL_FUNC_ADD);
1267
1268 // Bind textures
1269 vg_tex2d_bind( &tex_tile_data, 0 );
1270 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
1271
1272 vg_tex2d_bind( &tex_wood, 1 );
1273 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
1274
1275 render_tiles( NULL, NULL, colour_default, colour_default );
1276
1277
1278
1279 SHADER_USE( shader_ball );
1280 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1281
1282 vg_tex2d_bind( &tex_ball, 0 );
1283 glUniform1i( SHADER_UNIFORM( shader_ball, "uTexMain" ), 0 );
1284
1285 // Draw 'fish'
1286 if( world.simulating )
1287 {
1288 for( int i = 0; i < world.num_fishes; i ++ )
1289 {
1290 struct fish *fish = &world.fishes[i];
1291
1292 if( fish->alive == 0 )
1293 continue;
1294
1295 if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
1296 continue;
1297
1298 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
1299 colour_code_v3( fish->payload, dot_colour );
1300
1301 glUniform3fv( SHADER_UNIFORM( shader_ball, "uColour" ), 1, dot_colour );
1302 glUniform2fv( SHADER_UNIFORM( shader_ball, "uOffset" ), 1, fish->physics_co );
1303 draw_mesh( 0, 32 );
1304 }
1305 }
1306
1307 SHADER_USE( shader_tile_main );
1308
1309 // Bind textures
1310 vg_tex2d_bind( &tex_tile_data, 0 );
1311 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
1312
1313 vg_tex2d_bind( &tex_wood, 1 );
1314 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
1315
1316 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 1.0f );
1317 render_tiles( NULL, NULL, colour_default, colour_selected );
1318
1319 // Draw splitters
1320
1321 for( int y = 0; y < world.h; y ++ )
1322 {
1323 for( int x = 0; x < world.w; x ++ )
1324 {
1325 struct cell *cell = pcell((v2i){x,y});
1326
1327 if( cell->config == k_cell_type_split )
1328 {
1329 float rotation = cell->state & FLAG_FLIP_FLOP? vg_rad( -45.0f ): vg_rad( 45.0f );
1330
1331 if( cell->state & FLAG_FLIP_ROTATING )
1332 {
1333 if( (world.frame_lerp > curve_7_linear_section) )
1334 {
1335 float const rotation_speed = 0.4f;
1336 if( (world.frame_lerp < 1.0f-rotation_speed) )
1337 {
1338 float t = world.frame_lerp - curve_7_linear_section;
1339 t *= -2.0f * (1.0f/(1.0f-(curve_7_linear_section+rotation_speed)));
1340 t += 1.0f;
1341
1342 rotation *= t;
1343 }
1344 else
1345 rotation *= -1.0f;
1346 }
1347 }
1348
1349 m2x2_create_rotation( subtransform, rotation );
1350
1351 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1352 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y + 0.125f, 0.0f, 0.0f );
1353 draw_mesh( 0, 2 );
1354 }
1355 }
1356 }
1357
1358 // Edit overlay
1359 if( world.selected != -1 && !(world.data[ world.selected ].state & FLAG_CANAL) )
1360 {
1361 v2i new_begin = { world.tile_x - 2, world.tile_y - 2 };
1362 v2i new_end = { world.tile_x + 2, world.tile_y + 2 };
1363
1364 world.data[ world.selected ].state ^= FLAG_CANAL;
1365 map_reclassify( new_begin, new_end, 0 );
1366
1367 m2x2_identity( subtransform );
1368 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 1.0f );
1369 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1370 glUniform2fv( SHADER_UNIFORM( shader_tile_main, "uMousePos" ), 1, world.tile_pos );
1371
1372 render_tiles( new_begin, new_end, colour_default, colour_default );
1373
1374 world.data[ world.selected ].state ^= FLAG_CANAL;
1375 map_reclassify( new_begin, new_end, 0 );
1376 }
1377
1378 //glDisable(GL_BLEND);
1379
1380 glDisable(GL_BLEND);
1381
1382 SHADER_USE( shader_tile_colour );
1383 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1384 use_mesh( &world.circle );
1385
1386 int const filled_start = 0;
1387 int const filled_count = 32;
1388 int const empty_start = 32;
1389 int const empty_count = 32*2;
1390
1391 // Draw i/o arrays
1392 for( int i = 0; i < arrlen( world.io ); i ++ )
1393 {
1394 struct cell_terminal *term = &world.io[ i ];
1395 int posx = term->id % world.w;
1396 int posy = (term->id - posx)/world.w;
1397 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1398
1399 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
1400
1401 for( int j = 0; j < arrlen( term->conditions ); j ++ )
1402 {
1403 float y_offset = is_input? 1.2f: -0.2f;
1404 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + y_offset, 0.1f );
1405
1406 if( is_input )
1407 {
1408 colour_code_v3( term->conditions[j], dot_colour );
1409 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1410
1411 // Draw filled if tick not passed, draw empty if empty
1412 if( world.sim_frame > j )
1413 draw_mesh( empty_start, empty_count );
1414 else
1415 draw_mesh( filled_start, filled_count );
1416 }
1417 else
1418 {
1419 if( term->recv_count > j )
1420 {
1421 colour_code_v3( term->recv[j], dot_colour );
1422 v3_muls( dot_colour, 0.8f, dot_colour );
1423 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1424
1425 draw_mesh( filled_start, filled_count );
1426 }
1427
1428 colour_code_v3( term->conditions[j], dot_colour );
1429 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1430
1431 draw_mesh( empty_start, empty_count );
1432 }
1433 }
1434 }
1435
1436 if( world.simulating )
1437 {
1438 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.0f, 0.0f, 0.0f, 1.0f );
1439 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), -0.5f + cosf( vg_time * 4.0f ) * 0.2f, sinf( vg_time * 4.0f ) * 0.2f + (float)world.h * 0.5f, 0.05f );
1440 draw_mesh( filled_start, filled_count );
1441 }
1442
1443 // Level selection UI
1444 float ratio = ((float)vg_window_x/(float)vg_window_y);
1445
1446 m3x3f ui_view = M3X3_IDENTITY;
1447 m3x3_scale( ui_view, (v3f){ 1.0f, -ratio, 1.0f } );
1448 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)ui_view );
1449
1450 // Calculate mouse in UIsp
1451 v3f mouse_ui_space = { ((float)vg_mouse[0] / (float)(vg_window_x)) * 2.0f - 1.0f,
1452 (((float)vg_mouse[1] / (float)(vg_window_y)) * 2.0f - 1.0f)*(1.0f/ratio), 0.0125f };
1453
1454 // Get selected level
1455 const float selection_scale = 0.05f;
1456 int const level_count = vg_list_size( level_pack_1 );
1457 int level_select = -1;
1458
1459 if( mouse_ui_space[0] <= -0.8f )
1460 {
1461 float levels_range = (float)level_count*selection_scale*0.6f;
1462 float level_offset = ((mouse_ui_space[1] + levels_range) / levels_range) * 0.5f * (float)level_count;
1463 level_select = floorf( level_offset );
1464
1465 // Draw selector
1466 if( level_select >= 0 && level_select < vg_list_size( level_pack_1 ) )
1467 {
1468 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.369768f, 0.3654f, 0.42f, 1.0f );
1469
1470 use_mesh( &world.tile );
1471 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ),
1472 -1.0f,
1473 (-(float)level_count + (float)level_select * 2.0f ) * selection_scale * 0.6f,
1474 selection_scale
1475 );
1476 draw_mesh( 2, 2 );
1477
1478 use_mesh( &world.circle );
1479
1480 if( vg_get_button_down( "primary" ) )
1481 {
1482 console_changelevel( 1, level_pack_1 + level_select );
1483 }
1484 }
1485 }
1486
1487 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.4f, 0.39f, 0.45f, 1.0f );
1488
1489 // Draw levels
1490 for( int i = 0; i < level_count; i ++ )
1491 {
1492 v3f level_ui_space = {
1493 -0.97f,
1494 (-(float)level_count + (float)i * 2.0f ) * selection_scale * 0.6f + selection_scale * 0.5f,
1495 selection_scale * 0.5f
1496 };
1497
1498 float scale = vg_clampf( 1.0f - fabsf(level_ui_space[1] - mouse_ui_space[1]) * 2.0f, 0.9f, 1.0f );
1499 level_ui_space[2] *= scale;
1500
1501 glUniform3fv( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), 1, level_ui_space );
1502 draw_mesh( empty_start, empty_count );
1503 }
1504
1505 glUniform3fv( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), 1, mouse_ui_space );
1506 draw_mesh( empty_start, empty_count );
1507 }
1508
1509 void vg_ui(void)
1510 {
1511 //ui_test();
1512 }