fix out of order IO drawing
[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 "level5",
14 "level6",
15 "level7_combine",
16 "xor_small",
17 "sort",
18 "thirds"
19 };
20
21 #pragma pack(push,1)
22 struct career_state
23 {
24 u32 version;
25
26 struct career_level
27 {
28 u32 score;
29 u32 time;
30 u32 completed;
31 }
32 levels[ vg_list_size( level_pack_1 ) ];
33 }
34 career = { .version = 1 };
35 #pragma pack(pop)
36
37 static void career_serialize(void)
38 {
39 vg_asset_write( "sav/game.sav", &career, sizeof( struct career_state ) );
40 }
41
42 static void career_load(void)
43 {
44 i64 sz;
45 struct career_state *cr = vg_asset_read_s( "sav/game.sav", &sz );
46
47 memset( (void*)career.levels, 0, vg_list_size(level_pack_1) * sizeof(struct career_level) );
48
49 if( cr )
50 {
51 if( sz > sizeof( struct career_state ) )
52 vg_warn( "This save file is too big! Some levels will be lost\n" );
53
54 if( sz <= offsetof( struct career_state, levels ) )
55 {
56 vg_error( "This save file is too small to have a header\n" );
57 free( cr );
58 return;
59 }
60
61 u32 const size_header = offsetof(struct career_state, levels);
62 u32 const size_levels = sizeof(struct career_state)-size_header;
63 u32 const size_levels_input = sz - size_header;
64
65 memcpy( (void*)career.levels, (void*)cr->levels, size_levels_input );
66
67 if( sz < sizeof( struct career_state ) )
68 {
69 memset( ((void*)career.levels) + size_levels_input, 0, size_levels-size_levels_input );
70 }
71
72 free( cr );
73 vg_success( "Loaded save file... Info:\n" );
74
75 for( int i = 0; i < vg_list_size( career.levels ); i ++ )
76 {
77 struct career_level *lvl = &career.levels[i];
78 vg_info( "Score: %u, Time: %u, Completed: %u\n", lvl->score, lvl->time, lvl->completed );
79 }
80 }
81 else
82 {
83 vg_info( "No save file... Using blank one\n" );
84 }
85 }
86
87 m3x3f m_projection;
88 m3x3f m_view;
89 m3x3f m_mdl;
90
91 #define FLAG_CANAL 0x1
92 #define FLAG_IS_TRIGGER 0x2
93 #define FLAG_RESERVED0 0x4
94 #define FLAG_RESERVED1 0x8
95
96 #define FLAG_INPUT 0x10
97 #define FLAG_OUTPUT 0x20
98 #define FLAG_WALL 0x40
99
100 #define FLAG_FLIP_FLOP 0x100
101 #define FLAG_TRIGGERED 0x200
102 #define FLAG_FLIP_ROTATING 0x400
103 #define FLAG_TARGETED 0x800
104
105 /*
106 0000 0 | 0001 1 | 0010 2 | 0011 3
107 | | | | |
108 X | X= | X | X=
109 | | |
110 0100 4 | 0101 5 | 0110 6 | 0111 7
111 | | | | |
112 =X | =X= | =X | =X=
113 | | |
114 1000 8 | 1001 9 | 1010 10 | 1011 11
115 | | | | |
116 X | X= | X | X=
117 | | | | | | |
118 1100 12 | 1101 13 | 1110 14 | 1111 15
119 | | | | |
120 =X | =X= | =X | =X=
121 | | | | | | |
122 */
123
124 enum cell_type
125 {
126 k_cell_type_ramp_right = 3,
127 k_cell_type_ramp_left = 6,
128 k_cell_type_split = 7,
129 k_cell_type_merge = 13
130 };
131
132 v3f colour_sets[] =
133 { { 0.9f, 0.6f, 0.20f },
134 { 0.2f, 0.9f, 0.14f },
135 { 0.4f, 0.8f, 1.00f } };
136
137 static void colour_code_v3( char cc, v3f target )
138 {
139 if( cc >= 'a' && cc <= 'z' )
140 {
141 int id = cc - 'a';
142
143 if( id < vg_list_size( colour_sets ) )
144 {
145 v3_copy( colour_sets[ id ], target );
146 return;
147 }
148 }
149
150 v3_copy( (v3f){0.0f,0.0f,0.0f}, target );
151 }
152
153 struct mesh
154 {
155 GLuint vao, vbo;
156 u32 elements;
157 };
158
159 static void init_mesh( struct mesh *m, float const *tris, u32 length )
160 {
161 m->elements = length/3;
162 glGenVertexArrays( 1, &m->vao );
163 glGenBuffers( 1, &m->vbo );
164
165 glBindVertexArray( m->vao );
166 glBindBuffer( GL_ARRAY_BUFFER, m->vbo );
167 glBufferData( GL_ARRAY_BUFFER, length*sizeof(float), tris, GL_STATIC_DRAW );
168
169 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
170 glEnableVertexAttribArray( 0 );
171
172 VG_CHECK_GL();
173 }
174
175 static void free_mesh( struct mesh *m )
176 {
177 glDeleteVertexArrays( 1, &m->vao );
178 glDeleteBuffers( 1, &m->vbo );
179 }
180
181 static void draw_mesh( int const start, int const count )
182 {
183 glDrawArrays( GL_TRIANGLES, start*3, count*3 );
184 }
185
186 static void use_mesh( struct mesh *m )
187 {
188 glBindVertexArray( m->vao );
189 }
190
191 struct world
192 {
193 #pragma pack(push,1)
194 struct cell
195 {
196 u16 state;
197 u16 links[2];
198 u8 config;
199 u8 pad0;
200 }
201 *data;
202 #pragma pack(pop)
203
204 int frame;
205
206 int initialzed;
207
208 int sim_frame;
209 float sim_start;
210 int simulating;
211 int sim_run, max_runs;
212
213 float frame_lerp;
214
215 struct cell_terminal
216 {
217 //char *conditions;
218 //char recv[12];
219
220 struct terminal_run
221 {
222 char conditions[8];
223 char recieved[8];
224
225 int condition_count, recv_count;
226 }
227 runs[8];
228
229 int run_count;
230
231 int id;
232 }
233 *io;
234
235 int w, h;
236
237 struct mesh tile, circle, numbers;
238 struct mesh_wire
239 {
240 GLuint vao, vbo, ebo;
241 u32 em;
242 }
243 wire;
244
245 GLuint background_data;
246 GLuint random_samples;
247
248 int selected, tile_x, tile_y;
249 v2f tile_pos;
250
251 struct fish
252 {
253 v2i pos;
254 v2i dir;
255 int alive;
256 char payload;
257 float death_time;
258 v2f physics_v;
259 v2f physics_co;
260 }
261 fishes[16];
262
263 int num_fishes;
264
265 char map_name[128];
266 struct career_level *ptr_career_level;
267
268 u32 score;
269 u32 completed;
270 u32 time;
271
272 } world = {};
273
274 static void map_free(void)
275 {
276 arrfree( world.data );
277 arrfree( world.io );
278
279 world.w = 0;
280 world.h = 0;
281 world.data = NULL;
282 world.io = NULL;
283 world.score = 0;
284 world.time = 0;
285 world.completed = 0;
286 world.max_runs = 0;
287 world.initialzed = 0;
288 }
289
290 static void io_reset(void)
291 {
292 for( int i = 0; i < arrlen( world.io ); i ++ )
293 {
294 struct cell_terminal *term = &world.io[i];
295
296 for( int j = 0; j < term->run_count; j ++ )
297 term->runs[j].recv_count = 0;
298 }
299 }
300
301 static void map_reclassify( v2i start, v2i end, int update_texbuffer );
302 static int map_load( const char *str, const char *name )
303 {
304 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
305
306 map_free();
307
308 char const *c = str;
309
310 // Scan for width
311 for(;; world.w ++)
312 {
313 if( str[world.w] == ';' )
314 break;
315 else if( !str[world.w] )
316 {
317 vg_error( "Unexpected EOF when parsing level\n" );
318 return 0;
319 }
320 }
321
322 struct cell *row = arraddnptr( world.data, world.w );
323 int cx = 0;
324 int reg_start = 0, reg_end = 0;
325
326 u32 *links_to_make = NULL;
327 int links_satisfied = 0;
328
329 char link_id_buffer[32];
330 int link_id_n = 0;
331
332 for(;;)
333 {
334 if( !*c )
335 break;
336
337 if( *c == '\r' ) { c ++; continue; } // fuck off windows
338
339 if( *c == ';' )
340 {
341 c ++;
342
343 if( *c == '\r' ) c ++;
344
345 // Parse attribs
346 if( *c != '\n' )
347 {
348 while( *c )
349 {
350 if( *c == '\r' ) { c ++; continue; }
351
352 if( reg_start < reg_end )
353 {
354 struct cell_terminal *terminal = &world.io[ reg_start ];
355 struct terminal_run *run = &terminal->runs[ terminal->run_count-1 ];
356
357 if( *c >= 'a' && *c <= 'z' )
358 {
359 run->conditions[ run->condition_count ++ ] = *c;
360 }
361 else
362 {
363 if( *c == ',' || *c == '\n' )
364 {
365 reg_start ++;
366
367 if( *c == '\n' )
368 break;
369 }
370 else if( *c == ':' )
371 {
372 terminal->runs[ terminal->run_count ].condition_count = 0;
373 terminal->run_count ++;
374 world.max_runs = vg_max( world.max_runs, terminal->run_count );
375 }
376 else
377 {
378 vg_error( "Unkown attribute '%c' (row: %u)\n", *c, world.h );
379 goto IL_REG_ERROR;
380 }
381 }
382 }
383 else
384 {
385 if( links_satisfied < arrlen( links_to_make ) )
386 {
387 struct cell *target = &world.data[ links_to_make[ links_satisfied ] ];
388
389 if( (((u32)*c >= (u32)'0') && ((u32)*c <= (u32)'9')) || *c == '-' )
390 {
391 if( link_id_n >= vg_list_size( link_id_buffer )-1 )
392 {
393 vg_error( "Number was way too long to be parsed (row: %u)\n", world.h );
394 goto IL_REG_ERROR;
395 }
396
397 link_id_buffer[ link_id_n ++ ] = *c;
398 }
399 else if( *c == ',' || *c == '\n' )
400 {
401 link_id_buffer[ link_id_n ] = 0x00;
402 int value = atoi( link_id_buffer );
403
404 target->links[value >= 0? 1:0] = abs(value);
405 links_satisfied ++;
406 link_id_n = 0;
407
408 if( *c == '\n' )
409 break;
410 }
411 else
412 {
413 vg_error( "Invalid character '%c' (row: %u)\n", *c, world.h );
414 goto IL_REG_ERROR;
415 }
416 }
417 else
418 {
419 vg_error( "Too many values to assign (row: %u)\n", world.h );
420 goto IL_REG_ERROR;
421 }
422 }
423
424 c ++;
425 }
426 }
427
428 // Registry length-error checks
429 if( reg_start != reg_end )
430 {
431 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world.h, reg_start, reg_end );
432 goto IL_REG_ERROR;
433 }
434
435 if( links_satisfied != arrlen( links_to_make ) )
436 {
437 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world.h, links_satisfied, arrlen( links_to_make ) );
438 goto IL_REG_ERROR;
439 }
440
441 if( cx != world.w )
442 {
443 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world.h, cx, world.w );
444 goto IL_REG_ERROR;
445 }
446
447 row = arraddnptr( world.data, world.w );
448 cx = 0;
449 world.h ++;
450 reg_end = reg_start = arrlen( world.io );
451
452 arrsetlen( links_to_make, 0 );
453 links_satisfied = 0;
454 }
455 else
456 {
457 if( cx == world.w )
458 {
459 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world.h, cx, world.w );
460 goto IL_REG_ERROR;
461 }
462
463 // Tile initialization
464 // row[ cx ] .. etc
465 struct cell *cell = &row[ cx ];
466 cell->config = 0xF;
467
468 if( *c == '+' || *c == '-' )
469 {
470 struct cell_terminal *term = arraddnptr( world.io, 1 );
471 term->id = cx + world.h*world.w;
472 term->run_count = 1;
473 term->runs[0].condition_count = 0;
474
475 cell->state = *c == '+'? FLAG_INPUT: FLAG_OUTPUT;
476 reg_end ++;
477 }
478 else if( *c == '#' ) cell->state = FLAG_WALL;
479 else if( ((u32)*c >= (u32)'A') && ((u32)*c <= (u32)'A'+0xf) )
480 {
481 // Canal flag bits (4bit/16 value):
482 // 0: Canal present
483 // 1: Is trigger
484 // 2: Reserved
485 // 3: Reserved
486
487 cell->state = ((u32)*c - (u32)'A') & (FLAG_CANAL|FLAG_IS_TRIGGER);
488
489 if( cell->state & FLAG_IS_TRIGGER )
490 arrpush( links_to_make, cx + world.h*world.w );
491
492 cell->links[0] = 0;
493 cell->links[1] = 0;
494 world.score ++;
495 }
496 else cell->state = 0x00;
497
498 cx ++;
499 }
500
501 c ++;
502 }
503
504 // Update data texture to fill out the background
505 {
506 u8 info_buffer[64*64*4];
507 for( int i = 0; i < 64*64; i ++ )
508 {
509 u8 *px = &info_buffer[i*4];
510 px[0] = 255;
511 px[1] = 0;
512 px[2] = 0;
513 px[3] = 0;
514 }
515
516 glBindTexture( GL_TEXTURE_2D, world.background_data );
517 glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 64, 64, GL_RGBA, GL_UNSIGNED_BYTE, info_buffer );
518 }
519
520 arrfree( links_to_make );
521
522 map_reclassify( NULL, NULL, 1 );
523
524 // Validate links
525 for( int i = 0; i < world.h*world.w; i ++ )
526 {
527 struct cell *src = &world.data[i];
528 if( src->state & FLAG_IS_TRIGGER )
529 {
530 int link_id = src->links[0]?0:1;
531 if( src->links[link_id] <= world.h*world.w )
532 {
533 struct cell *target = &world.data[ src->links[link_id] ];
534 if( (target->state & FLAG_CANAL) && (target->config == k_cell_type_split) )
535 {
536 if( target->links[ link_id ] )
537 {
538 vg_error( "Link target was already targeted\n" );
539 goto IL_REG_ERROR;
540 }
541 else
542 {
543 // Valid link
544 target->links[ link_id ] = i;
545 target->state |= FLAG_TARGETED;
546 }
547 }
548 else
549 {
550 vg_error( "Link target was invalid\n" );
551 goto IL_REG_ERROR;
552 }
553 }
554 else
555 {
556 vg_error( "Link target out of bounds\n" );
557 goto IL_REG_ERROR;
558 }
559 }
560 }
561
562 vg_success( "Map '%s' loaded! (%u:%u)\n", name, world.w, world.h );
563
564 io_reset();
565
566 strncpy( world.map_name, name, vg_list_size( world.map_name )-1 );
567 world.initialzed = 1;
568 return 1;
569
570 IL_REG_ERROR:
571 arrfree( links_to_make );
572 map_free();
573 return 0;
574 }
575
576 static struct cell *pcell( v2i pos )
577 {
578 return &world.data[ pos[1]*world.w + pos[0] ];
579 }
580
581 static void map_serialize( FILE *stream )
582 {
583 for( int y = 0; y < world.h; y ++ )
584 {
585 for( int x = 0; x < world.w; x ++ )
586 {
587 struct cell *cell = pcell( (v2i){ x, y } );
588
589 if( cell->state & FLAG_WALL ) fputc( '#', stream );
590 else if( cell->state & FLAG_INPUT ) fputc( '+', stream );
591 else if( cell->state & FLAG_OUTPUT ) fputc( '-', stream );
592 else if( cell->state & (FLAG_CANAL|FLAG_IS_TRIGGER|FLAG_RESERVED0|FLAG_RESERVED1) )
593 {
594 fputc( (cell->state & (FLAG_CANAL|FLAG_IS_TRIGGER|FLAG_RESERVED0|FLAG_RESERVED1)) + (u32)'A', stream );
595 }
596 else fputc( ' ', stream );
597 }
598
599 fputc( ';', stream );
600
601 int terminal_write_count = 0;
602
603 for( int x = 0; x < world.w; x ++ )
604 {
605 for( int i = 0; i < arrlen( world.io ); i ++ )
606 {
607 struct cell_terminal *term = &world.io[i];
608 if( term->id == y*world.w+x )
609 {
610 if( terminal_write_count )
611 fputc( ',', stream );
612 terminal_write_count ++;
613
614 for( int j = 0; j < term->run_count; j ++ )
615 {
616 struct terminal_run *run = &term->runs[j];
617
618 for( int k = 0; k < run->condition_count; k ++ )
619 fputc( run->conditions[k], stream );
620
621 if( j < term->run_count-1 )
622 fputc( ':', stream );
623 }
624 }
625 }
626 }
627
628 for( int x = 0; x < world.w; x ++ )
629 {
630 struct cell *cell = pcell( (v2i){ x,y } );
631 if( cell->state & FLAG_IS_TRIGGER )
632 {
633 if( terminal_write_count )
634 fputc( ',', stream );
635 terminal_write_count ++;
636
637 fprintf( stream, "%d", cell->links[0]? -cell->links[0]: cell->links[1] );
638 }
639 }
640
641 fputc( '\n', stream );
642 }
643 }
644
645 int main( int argc, char *argv[] )
646 {
647 vg_init( argc, argv, "Marble Computing | SPACE: Test | LeftClick: Toggle tile | RightClick: Drag wire" );
648 }
649
650 static int console_credits( int argc, char const *argv[] )
651 {
652 vg_info( "Aknowledgements:\n" );
653 vg_info( " GLFW zlib/libpng glfw.org\n" );
654 vg_info( " miniaudio MIT0 miniaud.io\n" );
655 vg_info( " QOI MIT phoboslab.org\n" );
656 vg_info( " STB library MIT nothings.org\n" );
657 vg_info( " Weiholmir font justfredrik.itch.io\n" );
658 return 0;
659 }
660
661 static int console_save_map( int argc, char const *argv[] )
662 {
663 if( !world.initialzed )
664 {
665 vg_error( "Tried to save uninitialized map!\n" );
666 return 0;
667 }
668
669 char map_path[ 256 ];
670
671 strcpy( map_path, "sav/" );
672 strcat( map_path, world.map_name );
673 strcat( map_path, ".map" );
674
675 FILE *test_writer = fopen( map_path, "wb" );
676 if( test_writer )
677 {
678 vg_info( "Saving map to '%s'\n", map_path );
679 map_serialize( test_writer );
680
681 fclose( test_writer );
682 return 1;
683 }
684 else
685 {
686 vg_error( "Unable to open stream for writing\n" );
687 return 0;
688 }
689 }
690
691 static int console_load_map( int argc, char const *argv[] )
692 {
693 char map_path[ 256 ];
694
695 if( argc >= 1 )
696 {
697 // try from saves
698 strcpy( map_path, "sav/" );
699 strcat( map_path, argv[0] );
700 strcat( map_path, ".map" );
701
702 char *text_source = vg_textasset_read( map_path );
703
704 if( !text_source )
705 {
706 strcpy( map_path, "maps/" );
707 strcat( map_path, argv[0] );
708 strcat( map_path, ".map" );
709
710 text_source = vg_textasset_read( map_path );
711 }
712
713 if( text_source )
714 {
715 vg_info( "Loading map: '%s'\n", map_path );
716 world.ptr_career_level = NULL;
717
718 if( !map_load( text_source, argv[0] ) )
719 {
720 free( text_source );
721 return 0;
722 }
723
724 free( text_source );
725
726 for( int i = 0; i < vg_list_size( level_pack_1 ); i ++ )
727 {
728 if( !strcmp( level_pack_1[i], argv[0] ) )
729 {
730 world.ptr_career_level = career.levels + i;
731 break;
732 }
733 }
734
735 return 1;
736 }
737 else
738 {
739 vg_error( "Missing maps '%s'\n", argv[0] );
740 return 0;
741 }
742 }
743 else
744 {
745 vg_error( "Missing argument <map_path>\n" );
746 return 0;
747 }
748 }
749
750 static void simulation_stop(void)
751 {
752 world.simulating = 0;
753 world.num_fishes = 0;
754 world.sim_frame = 0;
755
756 io_reset();
757
758 sfx_system_fadeout( &audio_system_balls_rolling, 44100 );
759
760 vg_info( "Stopping simulation!\n" );
761 }
762
763 static int console_changelevel( int argc, char const *argv[] )
764 {
765 if( argc >= 1 )
766 {
767 // Save current level
768 console_save_map( 0, NULL );
769 if( console_load_map( argc, argv ) )
770 {
771 simulation_stop();
772 return 1;
773 }
774 }
775 else
776 {
777 vg_error( "Missing argument <map_path>\n" );
778 }
779
780 return 0;
781 }
782
783 void vg_start(void)
784 {
785 vg_function_push( (struct vg_cmd){
786 .name = "_map_write",
787 .function = console_save_map
788 });
789
790 vg_function_push( (struct vg_cmd){
791 .name = "_map_load",
792 .function = console_load_map
793 });
794
795 vg_function_push( (struct vg_cmd){
796 .name = "map",
797 .function = console_changelevel
798 });
799
800 vg_function_push( (struct vg_cmd){
801 .name = "credits",
802 .function = console_credits
803 });
804
805 // Quad mesh
806 {
807 float quad_mesh[] =
808 {
809 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
810 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
811
812 0.0f, 0.0f, 0.0f, 1.0f, 4.0f, 1.0f,
813 0.0f, 0.0f, 4.0f, 1.0f, 4.0f, 0.0f
814 };
815
816 init_mesh( &world.tile, quad_mesh, vg_list_size(quad_mesh) );
817 }
818
819 // Circle mesh
820 {
821 float circle_mesh[32*6*3];
822 int res = vg_list_size( circle_mesh ) / (6*3);
823
824 for( int i = 0; i < res; i ++ )
825 {
826 v2f v0 = { sinf( ((float)i/(float)res)*VG_TAUf ), cosf( ((float)i/(float)res)*VG_TAUf ) };
827 v2f v1 = { sinf( ((float)(i+1)/(float)res)*VG_TAUf ), cosf( ((float)(i+1)/(float)res)*VG_TAUf ) };
828
829 circle_mesh[ i*6+0 ] = 0.0f;
830 circle_mesh[ i*6+1 ] = 0.0f;
831
832 v2_copy( v0, circle_mesh + 32*6 + i*12 );
833 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+2 );
834 v2_copy( v1, circle_mesh + 32*6 + i*12+4 );
835
836 v2_copy( v1, circle_mesh + 32*6 + i*12+6 );
837 v2_muls( v1, 0.8f, circle_mesh + 32*6 + i*12+8 );
838 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+10 );
839
840 v2_copy( v0, circle_mesh + i*6+4 );
841 v2_copy( v1, circle_mesh + i*6+2 );
842 v2_copy( v0, circle_mesh+i*6+4 );
843 v2_copy( v1, circle_mesh+i*6+2 );
844 }
845
846 init_mesh( &world.circle, circle_mesh, vg_list_size( circle_mesh ) );
847 }
848
849 // Numbers mesh
850 {
851 init_mesh( &world.numbers,
852 MESH_NUMBERS_BUFFER,
853 vg_list_size( MESH_NUMBERS_BUFFER )
854 );
855
856 for( int i = 0; i < 10; i ++ )
857 {
858 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS[i][0], MESH_NUMBERS_OFFSETS[i][1] );
859 }
860 }
861
862 // Create wire mesh
863 {
864 int const num_segments = 64;
865
866 struct mesh_wire *mw = &world.wire;
867
868 v2f wire_points[ num_segments * 2 ];
869 u16 wire_indices[ 6*(num_segments-1) ];
870
871 for( int i = 0; i < num_segments; i ++ )
872 {
873 float l = (float)i / (float)(num_segments-1);
874
875 v2_copy( (v2f){ l, -0.5f }, wire_points[i*2+0] );
876 v2_copy( (v2f){ l, 0.5f }, wire_points[i*2+1] );
877
878 if( i < num_segments-1 )
879 {
880 wire_indices[ i*6+0 ] = i*2 + 0;
881 wire_indices[ i*6+1 ] = i*2 + 1;
882 wire_indices[ i*6+2 ] = i*2 + 3;
883 wire_indices[ i*6+3 ] = i*2 + 0;
884 wire_indices[ i*6+4 ] = i*2 + 3;
885 wire_indices[ i*6+5 ] = i*2 + 2;
886 }
887 }
888
889 glGenVertexArrays( 1, &mw->vao );
890 glGenBuffers( 1, &mw->vbo );
891 glGenBuffers( 1, &mw->ebo );
892 glBindVertexArray( mw->vao );
893
894 glBindBuffer( GL_ARRAY_BUFFER, mw->vbo );
895
896 glBufferData( GL_ARRAY_BUFFER, sizeof( wire_points ), wire_points, GL_STATIC_DRAW );
897 glBindVertexArray( mw->vao );
898
899 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mw->ebo );
900 glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( wire_indices ), wire_indices, GL_STATIC_DRAW );
901
902 // XY
903 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)0 );
904 glEnableVertexAttribArray( 0 );
905
906 VG_CHECK_GL();
907
908 mw->em = vg_list_size( wire_indices );
909 }
910
911 // Create info data texture
912 {
913 glGenTextures( 1, &world.background_data );
914 glBindTexture( GL_TEXTURE_2D, world.background_data );
915 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
916 vg_tex2d_nearest();
917 }
918
919 // Create random smaples texture
920 {
921 u8 *data = malloc(512*512*2);
922 for( int i = 0; i < 512*512*2; i ++ )
923 data[ i ] = rand()/(RAND_MAX/255);
924
925 glGenTextures( 1, &world.random_samples );
926 glBindTexture( GL_TEXTURE_2D, world.random_samples );
927 glTexImage2D( GL_TEXTURE_2D, 0, GL_RG, 512, 512, 0, GL_RG, GL_UNSIGNED_BYTE, data );
928 vg_tex2d_linear();
929 vg_tex2d_repeat();
930
931 free( data );
932 }
933
934 resource_load_main();
935
936 // Restore gamestate
937 career_load();
938 console_load_map( 1, level_pack_1 );
939 }
940
941 void vg_free(void)
942 {
943 console_save_map( 0, NULL );
944 career_serialize();
945
946 resource_free_main();
947
948 glDeleteTextures( 1, &world.background_data );
949 glDeleteTextures( 1, &world.random_samples );
950
951 glDeleteVertexArrays( 1, &world.wire.vao );
952 glDeleteBuffers( 1, &world.wire.vbo );
953 glDeleteBuffers( 1, &world.wire.ebo );
954
955 free_mesh( &world.tile );
956 free_mesh( &world.circle );
957 free_mesh( &world.numbers );
958
959 map_free();
960 }
961
962 static int cell_interactive( v2i co )
963 {
964 // Bounds check
965 if( co[0] < 2 || co[0] >= world.w-2 || co[1] < 2 || co[1] >= world.h-2 )
966 return 0;
967
968 // Flags check
969 if( world.data[ world.w*co[1] + co[0] ].state & (FLAG_WALL|FLAG_INPUT|FLAG_OUTPUT) )
970 return 0;
971
972 // List of 3x3 configurations that we do not allow
973 static u32 invalid_src[][9] =
974 {
975 { 0,1,0,
976 1,1,1,
977 0,1,0
978 },
979 { 0,0,0,
980 0,1,1,
981 0,1,1
982 },
983 { 0,0,0,
984 1,1,0,
985 1,1,0
986 },
987 { 0,1,1,
988 0,1,1,
989 0,0,0
990 },
991 { 1,1,0,
992 1,1,0,
993 0,0,0
994 },
995 { 0,1,0,
996 0,1,1,
997 0,1,0
998 },
999 { 0,1,0,
1000 1,1,0,
1001 0,1,0
1002 }
1003 };
1004
1005 // Statically compile invalid configurations into bitmasks
1006 static u32 invalid[ vg_list_size(invalid_src) ];
1007
1008 for( int i = 0; i < vg_list_size(invalid_src); i ++ )
1009 {
1010 u32 comped = 0x00;
1011
1012 for( int j = 0; j < 3; j ++ )
1013 for( int k = 0; k < 3; k ++ )
1014 comped |= invalid_src[i][ j*3+k ] << ((j*5)+k);
1015
1016 invalid[i] = comped;
1017 }
1018
1019 // Extract 5x5 grid surrounding tile
1020 u32 blob = 0x1000;
1021 for( int y = co[1]-2; y < co[1]+3; y ++ )
1022 for( int x = co[0]-2; x < co[0]+3; x ++ )
1023 {
1024 struct cell *cell = pcell((v2i){x,y});
1025
1026 if( cell && (cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT)) )
1027 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
1028 }
1029
1030 // Run filter over center 3x3 grid to check for invalid configurations
1031 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1032 for( int i = 0; i < vg_list_size(kernel); i ++ )
1033 {
1034 if( blob & (0x1 << (6+kernel[i])) )
1035 {
1036 u32 window = blob >> kernel[i];
1037
1038 for( int j = 0; j < vg_list_size(invalid); j ++ )
1039 if((window & invalid[j]) == invalid[j])
1040 return 0;
1041 }
1042 }
1043
1044 return 1;
1045 }
1046
1047 static void map_reclassify( v2i start, v2i end, int update_texbuffer )
1048 {
1049 v2i full_start = { 1,1 };
1050 v2i full_end = { world.w-1, world.h-1 };
1051
1052 if( !start || !end )
1053 {
1054 start = full_start;
1055 end = full_end;
1056 }
1057
1058 // Texture data
1059 u8 info_buffer[64*64*4];
1060 u32 pixel_id = 0;
1061
1062 int px0 = vg_max( start[0], full_start[0] ),
1063 px1 = vg_min( end[0], full_end[0] ),
1064 py0 = vg_max( start[1], full_start[1] ),
1065 py1 = vg_min( end[1], full_end[1] );
1066
1067 for( int y = py0; y < py1; y ++ )
1068 {
1069 for( int x = px0; x < px1; x ++ )
1070 {
1071 struct cell *cell = pcell((v2i){x,y});
1072
1073 v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
1074
1075 u8 height = 0;
1076 u8 config = 0x00;
1077
1078 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
1079 {
1080 for( int i = 0; i < vg_list_size( dirs ); i ++ )
1081 {
1082 struct cell *neighbour = pcell((v2i){x+dirs[i][0], y+dirs[i][1]});
1083 if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
1084 config |= 0x1 << i;
1085 }
1086
1087 height = 128;
1088 }
1089 else
1090 {
1091 if( cell->state & FLAG_WALL )
1092 height = 255;
1093
1094 config = 0xF;
1095 }
1096
1097 pcell((v2i){x,y})->config = config;
1098
1099 u8 *info_px = &info_buffer[ (pixel_id ++)*4 ];
1100 info_px[0] = height;
1101 info_px[1] = cell->state & FLAG_WALL? 0: 255;
1102 info_px[2] = 0;
1103 info_px[3] = 0;
1104
1105 if(
1106 (
1107 ((cell->state & FLAG_IS_TRIGGER) && (cell->config == 0xF || cell->config == k_cell_type_split)) ||
1108 ((cell->state & FLAG_TARGETED) && (cell->config != k_cell_type_split))
1109 ) && update_texbuffer
1110 ){
1111 cell->state &= ~(FLAG_TARGETED|FLAG_IS_TRIGGER);
1112 for( u32 i = 0; i < 2; i ++ )
1113 {
1114 if( cell->links[i] )
1115 {
1116 struct cell *other_ptr = &world.data[ cell->links[i] ];
1117 other_ptr->links[ i ] = 0;
1118 other_ptr->state &= ~FLAG_IS_TRIGGER;
1119
1120 if( other_ptr->links[ i ^ 0x1 ] == 0 )
1121 other_ptr->state &= ~FLAG_TARGETED;
1122 }
1123 }
1124
1125 cell->links[0] = 0;
1126 cell->links[1] = 0;
1127 }
1128 }
1129 }
1130
1131 if( update_texbuffer )
1132 {
1133 glBindTexture( GL_TEXTURE_2D, world.background_data );
1134 glTexSubImage2D( GL_TEXTURE_2D, 0, px0 + 16, py0 + 16, px1-px0, py1-py0, GL_RGBA, GL_UNSIGNED_BYTE, info_buffer );
1135 }
1136 }
1137
1138
1139 v2f const curve_3[] = {{0.5f,1.0f},{0.5f,0.625f},{0.625f,0.5f},{1.0f,0.5f}};
1140 v2f const curve_6[] = {{0.5f,1.0f},{0.5f,0.625f},{0.375f,0.5f},{0.0f,0.5f}};
1141 v2f const curve_9[] = {{1.0f,0.5f},{0.625f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
1142 v2f const curve_12[]= {{0.0f,0.5f},{0.375f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
1143
1144 v2f const curve_1[] = {{1.0f,0.5f},{0.8f,0.5f},{0.3f,0.5f},{0.2f,0.5f}};
1145 v2f const curve_4[] = {{0.0f,0.5f},{0.3f,0.5f},{0.5f,0.5f},{0.8f,0.5f}};
1146 v2f const curve_2[] = {{0.5f,1.0f},{0.5f,0.8f},{0.5f,0.3f},{0.5f,0.2f}};
1147 v2f const curve_8[] = {{0.5f,0.8f},{0.5f,0.5f},{0.5f,0.3f},{0.5f,0.0f}};
1148
1149 v2f const curve_7[] = {{0.5f,0.8438f},{0.875f,0.8438f},{0.625f,0.5f},{1.0f,0.5f}};
1150 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}};
1151
1152 float const curve_7_linear_section = 0.1562f;
1153
1154 u16 id_drag_from = 0;
1155 v2f drag_from_co;
1156 v2f drag_to_co;
1157
1158 void vg_update(void)
1159 {
1160 // Fit within screen
1161
1162 float r1 = (float)vg_window_y / (float)vg_window_x,
1163 r2 = (float)world.h / (float)world.w,
1164 size;
1165
1166 size = ( r2 < r1? (float)world.w * 0.5f: ((float)world.h * 0.5f) / r1 ) + 2.5f;
1167 m3x3_projection( m_projection, -size, size, -size*r1, size*r1 );
1168
1169 v3f origin;
1170 origin[0] = floorf( -0.5f * world.w );
1171 origin[1] = floorf( -0.5f * world.h );
1172 origin[2] = 0.0f;
1173
1174 m3x3_identity( m_view );
1175 m3x3_translate( m_view, origin );
1176 m3x3_mul( m_projection, m_view, vg_pv );
1177 vg_projection_update();
1178
1179 // Input stuff
1180 v2_copy( vg_mouse_ws, world.tile_pos );
1181
1182 world.tile_x = floorf( world.tile_pos[0] );
1183 world.tile_y = floorf( world.tile_pos[1] );
1184
1185 // Tilemap editing
1186 if( !world.simulating )
1187 {
1188 v2_copy( vg_mouse_ws, drag_to_co );
1189
1190 if( cell_interactive( (v2i){ world.tile_x, world.tile_y } ))
1191 {
1192 world.selected = world.tile_y * world.w + world.tile_x;
1193
1194 static u32 modify_state = 0;
1195
1196 struct cell *cell_ptr = &world.data[world.selected];
1197
1198 if( vg_get_button_down("primary") )
1199 {
1200 modify_state = (cell_ptr->state & FLAG_CANAL) ^ FLAG_CANAL;
1201 }
1202
1203 if( vg_get_button("primary") && ((cell_ptr->state & FLAG_CANAL) != modify_state) )
1204 {
1205 cell_ptr->state &= ~FLAG_CANAL;
1206 cell_ptr->state |= modify_state;
1207
1208 if( cell_ptr->state & FLAG_CANAL )
1209 {
1210 cell_ptr->links[0] = 0;
1211 cell_ptr->links[1] = 0;
1212
1213 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 3, 6 );
1214 world.score ++;
1215 }
1216 else
1217 {
1218 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 0, 3 );
1219 world.score --;
1220 }
1221
1222 map_reclassify( (v2i){ world.tile_x -2, world.tile_y -2 },
1223 (v2i){ world.tile_x +2, world.tile_y +2 }, 1 );
1224 }
1225
1226 if( vg_get_button_down("secondary") && !(cell_ptr->config == k_cell_type_split) )
1227 {
1228 id_drag_from = world.selected;
1229 drag_from_co[0] = world.tile_x + 0.5f;
1230 drag_from_co[1] = world.tile_y + 0.5f;
1231 }
1232
1233 if( id_drag_from && (cell_ptr->config == k_cell_type_split) )
1234 {
1235 float local_x = vg_mouse_ws[0] - (float)world.tile_x;
1236 drag_to_co[0] = (float)world.tile_x + (local_x > 0.5f? 0.75f: 0.25f);
1237 drag_to_co[1] = (float)world.tile_y + 0.25f;
1238
1239 if( vg_get_button_up("secondary") )
1240 {
1241 struct cell *drag_ptr = &world.data[id_drag_from];
1242 u32 link_id = local_x > 0.5f? 1: 0;
1243
1244 // Cleanup existing connections
1245 if( cell_ptr->links[ link_id ] )
1246 {
1247 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id, cell_ptr->links[ link_id ] );
1248
1249 struct cell *current_connection = &world.data[ cell_ptr->links[ link_id ]];
1250 current_connection->state &= ~FLAG_IS_TRIGGER;
1251 current_connection->links[ link_id ] = 0;
1252 }
1253
1254 if( drag_ptr->links[ link_id ^ 0x1 ] )
1255 {
1256 vg_warn( "Destroying alternate link %u (%hu)\n", link_id ^ 0x1, drag_ptr->links[ link_id ^ 0x1 ] );
1257
1258 struct cell *current_connection = &world.data[ drag_ptr->links[ link_id ^ 0x1 ]];
1259 if( !current_connection->links[ link_id ] )
1260 current_connection->state &= ~FLAG_TARGETED;
1261
1262 current_connection->links[ link_id ^ 0x1 ] = 0;
1263 drag_ptr->links[ link_id ^ 0x1 ] = 0;
1264 }
1265
1266 // Create the new connection
1267 vg_success( "Creating connection on link %u (%hu)\n", link_id, id_drag_from );
1268
1269 cell_ptr->links[ link_id ] = id_drag_from;
1270 drag_ptr->links[ link_id ] = world.selected;
1271
1272 cell_ptr->state |= FLAG_TARGETED;
1273 drag_ptr->state |= FLAG_IS_TRIGGER;
1274 id_drag_from = 0;
1275 }
1276 }
1277 }
1278 else
1279 world.selected = -1;
1280
1281 if( !(vg_get_button("secondary") && id_drag_from) )
1282 id_drag_from = 0;
1283 }
1284 else
1285 {
1286 world.selected = -1;
1287 id_drag_from = 0;
1288 }
1289
1290 // Simulation stop/start
1291 if( vg_get_button_down("go") )
1292 {
1293 if( world.simulating )
1294 {
1295 simulation_stop();
1296 }
1297 else
1298 {
1299 vg_success( "Starting simulation!\n" );
1300
1301 sfx_set_playrnd( &audio_rolls, &audio_system_balls_rolling, 0, 1 );
1302
1303 world.simulating = 1;
1304 world.num_fishes = 0;
1305 world.sim_frame = 0;
1306 world.sim_start = vg_time;
1307 world.sim_run = 0;
1308
1309 for( int i = 0; i < world.w*world.h; i ++ )
1310 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
1311
1312 io_reset();
1313 }
1314 }
1315
1316 // Fish ticks
1317 if( world.simulating )
1318 {
1319 while( world.sim_frame < (int)((vg_time-world.sim_start)*2.0f) )
1320 {
1321 //vg_info( "frame: %u\n", world.sim_frame );
1322 sfx_set_playrnd( &audio_random, &audio_system_balls_switching, 0, 9 );
1323
1324 // Update splitter deltas
1325 for( int i = 0; i < world.h*world.w; i ++ )
1326 {
1327 struct cell *cell = &world.data[i];
1328 if( cell->config == k_cell_type_split )
1329 {
1330 cell->state &= ~FLAG_FLIP_ROTATING;
1331 }
1332 if( cell->state & FLAG_IS_TRIGGER )
1333 cell->state &= ~FLAG_TRIGGERED;
1334 }
1335
1336 int alive_count = 0;
1337
1338 // Update fish positions
1339 for( int i = 0; i < world.num_fishes; i ++ )
1340 {
1341 struct fish *fish = &world.fishes[i];
1342
1343 if( fish->alive == -1 )
1344 fish->alive = 0;
1345
1346 if( fish->alive != 1 )
1347 continue;
1348
1349 struct cell *cell_current = pcell( fish->pos );
1350
1351 // Apply to output
1352 if( cell_current->state & FLAG_OUTPUT )
1353 {
1354 for( int j = 0; j < arrlen( world.io ); j ++ )
1355 {
1356 struct cell_terminal *term = &world.io[j];
1357
1358 if( term->id == fish->pos[1]*world.w + fish->pos[0] )
1359 {
1360 struct terminal_run *run = &term->runs[ world.sim_run ];
1361 if( run->recv_count < vg_list_size( run->recieved ) )
1362 run->recieved[ run->recv_count ++ ] = fish->payload;
1363
1364 break;
1365 }
1366 }
1367
1368 fish->alive = 0;
1369 continue;
1370 }
1371
1372 if( cell_current->config == k_cell_type_split )
1373 {
1374 // Flip flop L/R
1375 fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
1376 fish->dir[1] = 0;
1377
1378 if( !(cell_current->state & FLAG_TARGETED) )
1379 cell_current->state ^= FLAG_FLIP_FLOP;
1380 }
1381 else if( cell_current->config == k_cell_type_merge )
1382 {
1383 // Can only move up
1384 fish->dir[0] = 0;
1385 fish->dir[1] = -1;
1386 }
1387 else
1388 {
1389 struct cell *cell_next = pcell( (v2i){ fish->pos[0]+fish->dir[0], fish->pos[1]+fish->dir[1] } );
1390 if( !(cell_next->state & (FLAG_CANAL|FLAG_OUTPUT)) )
1391 {
1392 // Try other directions for valid, so down, left, right..
1393 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
1394 //vg_info( "Trying some other directions...\n" );
1395
1396 for( int j = 0; j < vg_list_size(dirs); j ++ )
1397 {
1398 if( (dirs[j][0] == -fish->dir[0]) && (dirs[j][1] == -fish->dir[1]) )
1399 continue;
1400
1401 if( pcell( (v2i){ fish->pos[0]+dirs[j][0], fish->pos[1]+dirs[j][1] } )->state & (FLAG_CANAL|FLAG_OUTPUT) )
1402 {
1403 fish->dir[0] = dirs[j][0];
1404 fish->dir[1] = dirs[j][1];
1405 }
1406 }
1407 }
1408 }
1409
1410 fish->pos[0] += fish->dir[0];
1411 fish->pos[1] += fish->dir[1];
1412
1413 struct cell *cell_entry = pcell( fish->pos );
1414
1415 if( !(cell_entry->state & (FLAG_INPUT|FLAG_CANAL|FLAG_OUTPUT) ))
1416 fish->alive = 0;
1417 else
1418 {
1419 if( fish->dir[0] )
1420 {
1421 if( cell_entry->config == k_cell_type_split ||
1422 cell_entry->config == k_cell_type_ramp_right ||
1423 cell_entry->config == k_cell_type_ramp_left )
1424 {
1425 // Special death (FALL)
1426 v2_sub( fish->physics_co, fish->physics_v, fish->physics_v );
1427 v2_divs( fish->physics_v, vg_time_delta, fish->physics_v );
1428
1429 fish->alive = -2;
1430 vg_warn( "Special death (fall)\n" );
1431 continue;
1432 }
1433 }
1434
1435 if( cell_entry->config == k_cell_type_split )
1436 {
1437 sfx_set_playrnd( &audio_splitter, &audio_system_balls_important, 0, 1 );
1438 cell_entry->state |= FLAG_FLIP_ROTATING;
1439 }
1440 }
1441
1442 if( fish->alive )
1443 alive_count ++;
1444 }
1445
1446 // Second pass (triggers)
1447 for( int i = 0; i < world.num_fishes; i ++ )
1448 {
1449 struct fish *fish = &world.fishes[i];
1450
1451 if( fish->alive == 1 )
1452 {
1453 struct cell *cell_current = pcell( fish->pos );
1454
1455 if( cell_current->state & FLAG_IS_TRIGGER )
1456 {
1457 int trigger_id = cell_current->links[0]?0:1;
1458 int connection_id = cell_current->links[trigger_id];
1459 int target_px = connection_id % world.w;
1460 int target_py = (connection_id - target_px)/world.w;
1461
1462 vg_line2( (v2f){ fish->pos[0], fish->pos[1] }, (v2f){ target_px, target_py }, 0xffffffff, 0xffffffff );
1463
1464 struct cell *target_peice = &world.data[ cell_current->links[trigger_id] ];
1465
1466 cell_current->state |= FLAG_TRIGGERED;
1467
1468 if( trigger_id )
1469 target_peice->state |= FLAG_FLIP_FLOP;
1470 else
1471 target_peice->state &= ~FLAG_FLIP_FLOP;
1472 }
1473 }
1474 }
1475
1476 // Third pass (collisions)
1477 for( int i = 0; i < world.num_fishes; i ++ )
1478 {
1479 if( world.fishes[i].alive == 1 )
1480 {
1481 for( int j = i+1; j < world.num_fishes; j ++ )
1482 {
1483 if( (world.fishes[j].alive == 1) && (world.fishes[i].pos[0] == world.fishes[j].pos[0]) &&
1484 (world.fishes[i].pos[1] == world.fishes[j].pos[1]) )
1485 {
1486 // Shatter death (+0.5s)
1487 world.fishes[i].alive = -1;
1488 world.fishes[j].alive = -1;
1489 world.fishes[i].death_time = 0.5f;
1490 world.fishes[j].death_time = 0.5f;
1491 }
1492 }
1493 }
1494 }
1495
1496 // Spawn fishes
1497 for( int i = 0; i < arrlen( world.io ); i ++ )
1498 {
1499 struct cell_terminal *term = &world.io[ i ];
1500 int posx = term->id % world.w;
1501 int posy = (term->id - posx)/world.w;
1502 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1503
1504 if( is_input )
1505 {
1506 if( world.sim_frame < term->runs[ world.sim_run ].condition_count )
1507 {
1508 struct fish *fish = &world.fishes[world.num_fishes++];
1509 fish->pos[0] = posx;
1510 fish->pos[1] = posy;
1511 fish->alive = 1;
1512 fish->payload = term->runs[ world.sim_run ].conditions[ world.sim_frame ];
1513
1514 int can_spawn = 0;
1515
1516 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
1517 for( int j = 0; j < vg_list_size(dirs); j ++ )
1518 if( pcell( (v2i){ posx+dirs[j][0], posy+dirs[j][1] } )->state & FLAG_CANAL )
1519 {
1520 fish->dir[0] = dirs[j][0];
1521 fish->dir[1] = dirs[j][1];
1522 can_spawn = 1;
1523 break;
1524 }
1525
1526 if( !can_spawn )
1527 world.num_fishes--;
1528 else
1529 alive_count ++;
1530 }
1531 }
1532 }
1533
1534 if( alive_count == 0 )
1535 {
1536 world.completed = 1;
1537
1538 for( int i = 0; i < arrlen( world.io ); i ++ )
1539 {
1540 struct cell_terminal *term = &world.io[ i ];
1541 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1542
1543 if( !is_input )
1544 {
1545 struct terminal_run *run = &term->runs[ world.sim_run ];
1546
1547 if( run->recv_count == run->condition_count )
1548 {
1549 for( int j = 0; j < run->condition_count; j ++ )
1550 {
1551 if( run->recieved[j] != run->conditions[j] )
1552 {
1553 world.completed = 0;
1554 break;
1555 }
1556 }
1557 }
1558 else
1559 {
1560 world.completed = 0;
1561 break;
1562 }
1563 }
1564 }
1565
1566 if( world.completed )
1567 {
1568 if( world.sim_run < world.max_runs-1 )
1569 {
1570 vg_success( "Run passed, starting next\n" );
1571 world.sim_run ++;
1572 world.sim_frame = 0;
1573 world.sim_start = vg_time;
1574 world.num_fishes = 0;
1575 continue;
1576 }
1577 else
1578 {
1579 vg_success( "Level passed!\n" );
1580
1581 u32 score = 0;
1582 for( int i = 0; i < world.w*world.h; i ++ )
1583 if( world.data[ i ].state & FLAG_CANAL )
1584 score ++;
1585
1586 world.score = score;
1587 world.time = world.sim_frame;
1588 }
1589 }
1590 else
1591 {
1592 vg_error( "Level failed :(\n" );
1593 }
1594
1595 // Copy into career data
1596 if( world.ptr_career_level )
1597 {
1598 world.ptr_career_level->score = world.score;
1599 world.ptr_career_level->time = world.time;
1600 world.ptr_career_level->completed = world.completed;
1601 }
1602
1603 simulation_stop(); // TODO: Async?
1604 break;
1605 }
1606
1607 world.sim_frame ++;
1608 }
1609
1610 float scaled_time = 0.0f;
1611 scaled_time = (vg_time-world.sim_start)*2.0f;
1612 world.frame_lerp = scaled_time - (float)world.sim_frame;
1613
1614 // Update positions
1615 for( int i = 0; i < world.num_fishes; i ++ )
1616 {
1617 struct fish *fish = &world.fishes[i];
1618
1619 if( fish->alive == 0 )
1620 continue;
1621
1622 if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
1623 continue; // Todo: particle thing?
1624
1625 if( fish->alive == -2 )
1626 {
1627 v2_muladds( fish->physics_v, (v2f){ 0.0, -9.8f }, vg_time_delta, fish->physics_v );
1628 v2_muladds( fish->physics_co, fish->physics_v, vg_time_delta, fish->physics_co );
1629 }
1630 else
1631 {
1632 struct cell *cell = pcell(fish->pos);
1633 v2f const *curve;
1634
1635 float t = world.frame_lerp;
1636
1637 v2_copy( fish->physics_co, fish->physics_v );
1638
1639 switch( cell->config )
1640 {
1641 case 13:
1642 if( fish->dir[0] == 1 )
1643 curve = curve_12;
1644 else
1645 curve = curve_9;
1646 break;
1647 case 1: curve = curve_1; break;
1648 case 4: curve = curve_4; break;
1649 case 2: curve = curve_2; break;
1650 case 8: curve = curve_8; break;
1651 case 3: curve = curve_3; break;
1652 case 6: curve = curve_6; break;
1653 case 9: curve = curve_9; break;
1654 case 12: curve = curve_12; break;
1655 case 7:
1656 if( t > curve_7_linear_section )
1657 {
1658 t -= curve_7_linear_section;
1659 t *= (1.0f/(1.0f-curve_7_linear_section));
1660
1661 curve = cell->state & FLAG_FLIP_FLOP? curve_7: curve_7_1;
1662 }
1663 else curve = NULL;
1664 break;
1665 default: curve = NULL; break;
1666 }
1667
1668 if( curve )
1669 {
1670 float t2 = t * t;
1671 float t3 = t * t * t;
1672
1673 float cA = 3.0f*t2 - 3.0f*t3;
1674 float cB = 3.0f*t3 - 6.0f*t2 + 3.0f*t;
1675 float cC = 3.0f*t2 - t3 - 3.0f*t + 1.0f;
1676
1677 fish->physics_co[0] = t3*curve[3][0] + cA*curve[2][0] + cB*curve[1][0] + cC*curve[0][0];
1678 fish->physics_co[1] = t3*curve[3][1] + cA*curve[2][1] + cB*curve[1][1] + cC*curve[0][1];
1679 fish->physics_co[0] += (float)fish->pos[0];
1680 fish->physics_co[1] += (float)fish->pos[1];
1681 }
1682 else
1683 {
1684 v2f origin;
1685 origin[0] = (float)fish->pos[0] + (float)fish->dir[0]*-0.5f + 0.5f;
1686 origin[1] = (float)fish->pos[1] + (float)fish->dir[1]*-0.5f + 0.5f;
1687
1688 fish->physics_co[0] = origin[0] + (float)fish->dir[0]*t;
1689 fish->physics_co[1] = origin[1] + (float)fish->dir[1]*t;
1690 }
1691 }
1692 }
1693 }
1694 }
1695
1696 static void render_tiles( v2i start, v2i end, v4f const regular_colour, v4f const selected_colour )
1697 {
1698 v2i full_start = { 0,0 };
1699 v2i full_end = { world.w, world.h };
1700
1701 if( !start || !end )
1702 {
1703 start = full_start;
1704 end = full_end;
1705 }
1706
1707 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
1708
1709 for( int y = start[1]; y < end[1]; y ++ )
1710 {
1711 for( int x = start[0]; x < end[0]; x ++ )
1712 {
1713 struct cell *cell = pcell((v2i){x,y});
1714 int selected = world.selected == y*world.w + x;
1715
1716 int tile_offsets[][2] =
1717 {
1718 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1719 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1720 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1721 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1722 };
1723
1724 int uv[2] = { 3, 0 };
1725
1726 if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
1727 {
1728 uv[0] = tile_offsets[ cell->config ][0];
1729 uv[1] = tile_offsets[ cell->config ][1];
1730 } else continue;
1731
1732 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y, uv[0], uv[1] );
1733 if( selected )
1734 {
1735 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, selected_colour );
1736 draw_mesh( 0, 2 );
1737 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
1738 }
1739 else
1740 draw_mesh( 0, 2 );
1741 }
1742 }
1743 }
1744
1745 static void draw_numbers( v3f coord, int number )
1746 {
1747 v3f pos;
1748 v3_copy( coord, pos );
1749 int digits[8]; int i = 0;
1750
1751 while( number > 0 && i < 8 )
1752 {
1753 digits[i ++] = number % 10;
1754 number = number / 10;
1755 }
1756
1757 for( int j = 0; j < i; j ++ )
1758 {
1759 glUniform3fv( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), 1, pos );
1760 draw_mesh( MESH_NUMBERS_OFFSETS[digits[i-j-1]][0], MESH_NUMBERS_OFFSETS[digits[i-j-1]][1] );
1761 pos[0] += pos[2] * 0.75f;
1762 }
1763 }
1764
1765 void vg_render(void)
1766 {
1767 glViewport( 0,0, vg_window_x, vg_window_y );
1768
1769 glDisable( GL_DEPTH_TEST );
1770 glClearColor( 0.369768f, 0.3654f, 0.42f, 1.0f );
1771 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
1772
1773 v4f const colour_default = {1.0f, 1.0f, 1.0f, 1.0f};
1774 v4f const colour_selected = {0.90f, 0.92f, 1.0f, 1.0f};
1775
1776 // TILE SET RENDERING
1777 // todo: just slam everything into a mesh...
1778 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1779 // in ~3 subBuffers
1780 // Currently we're uploading a fair amount of data every frame anyway.
1781 // NOTE: this is for final optimisations ONLY!
1782 // ======================================================================
1783
1784 use_mesh( &world.tile );
1785
1786 // Draw background
1787
1788 if(1){
1789
1790 SHADER_USE( shader_background );
1791 glUniformMatrix3fv( SHADER_UNIFORM( shader_background, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1792
1793 glActiveTexture( GL_TEXTURE0 );
1794 glBindTexture( GL_TEXTURE_2D, world.background_data );
1795 glUniform1i( SHADER_UNIFORM( shader_background, "uTexMain" ), 0 );
1796
1797 glUniform3f( SHADER_UNIFORM( shader_background, "uOffset" ), -16, -16, 64 );
1798 glUniform1f( SHADER_UNIFORM( shader_background, "uVariance" ), 0.02f );
1799
1800 glActiveTexture( GL_TEXTURE1 );
1801 glBindTexture( GL_TEXTURE_2D, world.random_samples );
1802 glUniform1i( SHADER_UNIFORM( shader_background, "uSamplerNoise" ), 1 );
1803
1804 draw_mesh( 0, 2 );
1805
1806 }
1807
1808
1809 SHADER_USE( shader_tile_main );
1810
1811 m2x2f subtransform;
1812 m2x2_identity( subtransform );
1813 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1814 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1815 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 0.0f );
1816 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 0.0f );
1817
1818 glEnable(GL_BLEND);
1819 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1820 glBlendEquation(GL_FUNC_ADD);
1821
1822 // Bind textures
1823 vg_tex2d_bind( &tex_tile_data, 0 );
1824 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
1825
1826 vg_tex2d_bind( &tex_wood, 1 );
1827 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
1828
1829 render_tiles( NULL, NULL, colour_default, colour_default );
1830
1831
1832
1833 SHADER_USE( shader_ball );
1834 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1835
1836 vg_tex2d_bind( &tex_ball, 0 );
1837 glUniform1i( SHADER_UNIFORM( shader_ball, "uTexMain" ), 0 );
1838
1839 // Draw 'fish'
1840 if( world.simulating )
1841 {
1842 for( int i = 0; i < world.num_fishes; i ++ )
1843 {
1844 struct fish *fish = &world.fishes[i];
1845
1846 if( fish->alive == 0 )
1847 continue;
1848
1849 if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
1850 continue;
1851
1852 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
1853 colour_code_v3( fish->payload, dot_colour );
1854
1855 glUniform3fv( SHADER_UNIFORM( shader_ball, "uColour" ), 1, dot_colour );
1856 glUniform2fv( SHADER_UNIFORM( shader_ball, "uOffset" ), 1, fish->physics_co );
1857 draw_mesh( 0, 32 );
1858 }
1859 }
1860
1861 SHADER_USE( shader_tile_main );
1862
1863 // Bind textures
1864 vg_tex2d_bind( &tex_tile_data, 0 );
1865 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
1866
1867 vg_tex2d_bind( &tex_wood, 1 );
1868 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
1869
1870 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 1.0f );
1871 render_tiles( NULL, NULL, colour_default, colour_selected );
1872
1873 // Draw splitters
1874 for( int y = 2; y < world.h-2; y ++ )
1875 {
1876 for( int x = 2; x < world.w-2; x ++ )
1877 {
1878 struct cell *cell = pcell((v2i){x,y});
1879
1880 if( cell->state & FLAG_CANAL )
1881 {
1882 if( cell->config == k_cell_type_split )
1883 {
1884 float rotation = cell->state & FLAG_FLIP_FLOP? vg_rad( -45.0f ): vg_rad( 45.0f );
1885
1886 if( cell->state & FLAG_FLIP_ROTATING )
1887 {
1888 if( (world.frame_lerp > curve_7_linear_section) )
1889 {
1890 float const rotation_speed = 0.4f;
1891 if( (world.frame_lerp < 1.0f-rotation_speed) )
1892 {
1893 float t = world.frame_lerp - curve_7_linear_section;
1894 t *= -2.0f * (1.0f/(1.0f-(curve_7_linear_section+rotation_speed)));
1895 t += 1.0f;
1896
1897 rotation *= t;
1898 }
1899 else
1900 rotation *= -1.0f;
1901 }
1902 }
1903
1904 m2x2_create_rotation( subtransform, rotation );
1905
1906 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1907 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y + 0.125f, cell->state & FLAG_TARGETED? 3.0f: 0.0f, 0.0f );
1908 draw_mesh( 0, 2 );
1909 }
1910 }
1911 }
1912 }
1913
1914 // Edit overlay
1915 if( world.selected != -1 && !(world.data[ world.selected ].state & FLAG_CANAL) && !id_drag_from )
1916 {
1917 v2i new_begin = { world.tile_x - 2, world.tile_y - 2 };
1918 v2i new_end = { world.tile_x + 2, world.tile_y + 2 };
1919
1920 world.data[ world.selected ].state ^= FLAG_CANAL;
1921 map_reclassify( new_begin, new_end, 0 );
1922
1923 m2x2_identity( subtransform );
1924 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 1.0f );
1925 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1926 glUniform2fv( SHADER_UNIFORM( shader_tile_main, "uMousePos" ), 1, world.tile_pos );
1927
1928 render_tiles( new_begin, new_end, colour_default, colour_default );
1929
1930 world.data[ world.selected ].state ^= FLAG_CANAL;
1931 map_reclassify( new_begin, new_end, 0 );
1932 }
1933
1934 //glDisable(GL_BLEND);
1935
1936 // Draw connecting wires
1937 glDisable(GL_BLEND);
1938
1939 SHADER_USE( shader_wire );
1940 glBindVertexArray( world.wire.vao );
1941
1942 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1943 glUniform4f( SHADER_UNIFORM( shader_wire, "uColour" ), 0.2f, 0.2f, 0.2f, 1.0f );
1944
1945 if( id_drag_from )
1946 {
1947 glUniform1f( SHADER_UNIFORM( shader_wire, "uCurve" ), 0.4f );
1948 glUniform3f( SHADER_UNIFORM( shader_wire, "uStart" ), drag_from_co[0], drag_from_co[1], 0.06f );
1949 glUniform3f( SHADER_UNIFORM( shader_wire, "uEnd" ), drag_to_co[0], drag_to_co[1], 0.06f );
1950 glDrawElements( GL_TRIANGLES, world.wire.em, GL_UNSIGNED_SHORT, (void*)(0) );
1951 }
1952
1953 float rp_x1 = world.frame_lerp*9.0f;
1954 float rp_x2 = 1.0f-rp_x1*expf(1.0f-rp_x1)* 0.36f;
1955
1956 for( int y = 2; y < world.h-2; y ++ )
1957 {
1958 for( int x = 2; x < world.w-2; x ++ )
1959 {
1960 struct cell *cell = pcell((v2i){x,y});
1961
1962 if( cell->state & FLAG_CANAL )
1963 {
1964 if( cell->state & FLAG_IS_TRIGGER )
1965 {
1966 int trigger_id = cell->links[0]?0:1;
1967
1968 int x2 = cell->links[trigger_id] % world.w;
1969 int y2 = (cell->links[trigger_id] - x2) / world.w;
1970
1971 v2f startpoint;
1972 v2f endpoint;
1973
1974 startpoint[0] = (float)x2 + (trigger_id? 0.75f: 0.25f);
1975 startpoint[1] = (float)y2 + 0.25f;
1976
1977 endpoint[0] = x+0.5f;
1978 endpoint[1] = y+0.5f;
1979
1980 glUniform1f( SHADER_UNIFORM( shader_wire, "uCurve" ), cell->state & FLAG_TRIGGERED? rp_x2 * 0.4f: 0.4f );
1981 glUniform3f( SHADER_UNIFORM( shader_wire, "uStart" ), startpoint[0], startpoint[1], 0.04f );
1982 glUniform3f( SHADER_UNIFORM( shader_wire, "uEnd" ), endpoint[0], endpoint[1], 0.04f );
1983 glDrawElements( GL_TRIANGLES, world.wire.em, GL_UNSIGNED_SHORT, (void*)(0) );
1984 }
1985 }
1986 }
1987 }
1988
1989 SHADER_USE( shader_tile_colour );
1990 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1991 use_mesh( &world.circle );
1992
1993 int const filled_start = 0;
1994 int const filled_count = 32;
1995 int const empty_start = 32;
1996 int const empty_count = 32*2;
1997
1998 // Draw i/o arrays
1999 for( int i = 0; i < arrlen( world.io ); i ++ )
2000 {
2001 struct cell_terminal *term = &world.io[ i ];
2002 int posx = term->id % world.w;
2003 int posy = (term->id - posx)/world.w;
2004 int is_input = world.data[ term->id ].state & FLAG_INPUT;
2005
2006 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
2007
2008 for( int k = 0; k < term->run_count; k ++ )
2009 {
2010 for( int j = 0; j < term->runs[k].condition_count; j ++ )
2011 {
2012 float y_offset = is_input? 1.2f: -0.2f;
2013 y_offset += (is_input? 0.2f: -0.2f) * (float)k;
2014
2015 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + y_offset, 0.1f );
2016
2017 if( is_input )
2018 {
2019 colour_code_v3( term->runs[k].conditions[j], dot_colour );
2020 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
2021
2022 // Draw filled if tick not passed, draw empty if empty
2023 if( (world.sim_frame > j && world.sim_run >= k) || world.sim_run > k )
2024 draw_mesh( empty_start, empty_count );
2025 else
2026 draw_mesh( filled_start, filled_count );
2027 }
2028 else
2029 {
2030 if( term->runs[k].recv_count > j )
2031 {
2032 colour_code_v3( term->runs[k].recieved[j], dot_colour );
2033 v3_muls( dot_colour, 0.8f, dot_colour );
2034 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
2035
2036 draw_mesh( filled_start, filled_count );
2037 }
2038
2039 colour_code_v3( term->runs[k].conditions[j], dot_colour );
2040 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
2041
2042 draw_mesh( empty_start, empty_count );
2043 }
2044 }
2045 }
2046 }
2047
2048 if( world.simulating )
2049 {
2050 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.0f, 0.0f, 0.0f, 1.0f );
2051 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 );
2052 draw_mesh( filled_start, filled_count );
2053 }
2054
2055 // Draw score
2056 float const score_bright = 1.25f;
2057 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ),
2058 0.4f*score_bright, 0.39f*score_bright, 0.45f*score_bright, 1.0f );
2059
2060 use_mesh( &world.numbers );
2061 draw_numbers( (v3f){ 2.0f, (float)world.h-1.875f, 0.3333f }, world.score );
2062
2063 // Level selection UI
2064 use_mesh( &world.circle );
2065 float ratio = ((float)vg_window_x/(float)vg_window_y);
2066
2067 m3x3f ui_view = M3X3_IDENTITY;
2068 m3x3_scale( ui_view, (v3f){ 1.0f, ratio, 1.0f } );
2069 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)ui_view );
2070
2071 // Calculate mouse in UIsp
2072 v3f mouse_ui_space = { ((float)vg_mouse[0] / (float)(vg_window_x)) * 2.0f - 1.0f,
2073 (((float)vg_mouse[1] / (float)(vg_window_y)) * 2.0f - 1.0f)*(-1.0f/ratio), 0.0125f };
2074
2075 // Get selected level
2076 const float selection_scale = 0.05f;
2077 int const level_count = vg_list_size( level_pack_1 );
2078 int level_select = -1;
2079
2080 if( mouse_ui_space[0] <= -0.8f )
2081 {
2082 float levels_range = (float)level_count*selection_scale*0.6f;
2083 float level_offset = ((-mouse_ui_space[1] + levels_range) / levels_range) * 0.5f * (float)level_count;
2084 level_select = ceilf( level_offset );
2085
2086 // Draw selector
2087 if( level_select >= 0 && level_select < vg_list_size( level_pack_1 ) )
2088 {
2089 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.369768f, 0.3654f, 0.42f, 1.0f );
2090
2091 use_mesh( &world.tile );
2092 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ),
2093 -1.0f,
2094 ((float)level_count - (float)level_select * 2.0f ) * selection_scale * 0.6f,
2095 selection_scale
2096 );
2097 draw_mesh( 2, 2 );
2098
2099 use_mesh( &world.circle );
2100
2101 if( vg_get_button_down( "primary" ) )
2102 {
2103 console_changelevel( 1, level_pack_1 + level_select );
2104 }
2105 }
2106 }
2107 else mouse_ui_space[1] = INFINITY;
2108
2109 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.4f, 0.39f, 0.45f, 1.0f );
2110
2111 // Draw levels
2112 for( int i = 0; i < level_count; i ++ )
2113 {
2114 struct career_level *clevel = &career.levels[i];
2115
2116 v3f level_ui_space = {
2117 -0.97f,
2118 ((float)level_count - (float)i * 2.0f ) * selection_scale * 0.6f + selection_scale * 0.5f,
2119 selection_scale * 0.5f
2120 };
2121
2122 float scale = vg_clampf( 1.0f - fabsf(level_ui_space[1] - mouse_ui_space[1]) * 2.0f, 0.9f, 1.0f );
2123 level_ui_space[2] *= scale;
2124
2125 glUniform3fv( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), 1, level_ui_space );
2126
2127 if( clevel->completed )
2128 draw_mesh( filled_start, filled_count );
2129 else
2130 draw_mesh( empty_start, empty_count );
2131 }
2132
2133 // Level scores
2134 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.4f*1.25f, 0.39f*1.25f, 0.45f*1.25f, 1.0f );
2135
2136 use_mesh( &world.numbers );
2137 for( int i = 0; i < level_count; i ++ )
2138 {
2139 struct career_level *clevel = &career.levels[i];
2140
2141 v3f level_ui_space = {
2142 -0.94f,
2143 ((float)level_count - (float)i * 2.0f ) * selection_scale * 0.6f + selection_scale * 0.5f,
2144 0.02f
2145 };
2146
2147 if( clevel->completed )
2148 draw_numbers( level_ui_space, clevel->score );
2149
2150 level_ui_space[0] = -0.975f;
2151 level_ui_space[1] -= 0.01f;
2152 draw_numbers( level_ui_space, i );
2153 }
2154
2155 //use_mesh( &world.numbers );
2156 //draw_numbers( (v3f){ 0.0f, -0.5f, 0.1f }, 128765 );
2157 }
2158
2159 void vg_ui(void)
2160 {
2161 ui_test();
2162 }