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