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