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