ac1efcfc19045eec2175c81e72359e0385b43d3e
[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[] =
8 {
9 // Level 0
10 "#########;\n"
11 "###-#####;acac\n"
12 "## ##;\n"
13 "## ##;\n"
14 "## ##;\n"
15 "## ##;\n"
16 "#####+###;acac\n"
17 "#########;\n",
18
19 // Level 1
20 "#########;\n"
21 "##-###-##;b,b\n"
22 "## ##;\n"
23 "## ##;\n"
24 "## ##;\n"
25 "## ##;\n"
26 "## ##;\n"
27 "####+####;bb\n"
28 "#########;\n",
29
30 // Level 2
31 "###########;\n"
32 "#####-#####;bbbbb\n"
33 "## ##;\n"
34 "## ###;\n"
35 "## # ##;\n"
36 "## ##;\n"
37 "###+##+####;bbb,bb\n"
38 "###########;\n",
39
40 // Level 3
41 "#############;\n"
42 "###-#####-###;a,aaa\n"
43 "## ##;\n"
44 "## ##;\n"
45 "## ##;\n"
46 "## ##;\n"
47 "## ##;\n"
48 "## ##;\n"
49 "######+######;aaaa\n"
50 "#############;\n",
51
52 // Level 4
53 "#############;\n"
54 "###-#####-###;aaa,aa\n"
55 "## ##;\n"
56 "## ##;\n"
57 "## ##;\n"
58 "## ##;\n"
59 "## ##;\n"
60 "## ##;\n"
61 "###+#####+###;aa,aaa\n"
62 "#############;\n"
63 };
64
65 m3x3f m_projection;
66 m3x3f m_view;
67 m3x3f m_mdl;
68
69 #define FLAG_INPUT 0x1
70 #define FLAG_OUTPUT 0x2
71 #define FLAG_CANAL 0x4
72 #define FLAG_WALL 0x8
73 #define FLAG_FLIP_FLOP 0x100
74 #define FLAG_FLIP_ROTATING 0x200
75
76 /*
77 0000 0 | 0001 1 | 0010 2 | 0011 3
78 | | | | |
79 X | X= | X | X=
80 | | |
81 0100 4 | 0101 5 | 0110 6 | 0111 7
82 | | | | |
83 =X | =X= | =X | =X=
84 | | |
85 1000 8 | 1001 9 | 1010 10 | 1011 11
86 | | | | |
87 X | X= | X | X=
88 | | | | | | |
89 1100 12 | 1101 13 | 1110 14 | 1111 15
90 | | | | |
91 =X | =X= | =X | =X=
92 | | | | | | |
93 */
94
95 enum cell_type
96 {
97 k_cell_type_split = 7,
98 k_cell_type_merge = 13
99 };
100
101 v3f colour_sets[] =
102 { { 0.9f, 0.2f, 0.01f },
103 { 0.2f, 0.9f, 0.14f },
104 { 0.1f, 0.3f, 0.85f } };
105
106 static void colour_code_v3( char cc, v3f target )
107 {
108 if( cc >= 'a' && cc <= 'z' )
109 {
110 int id = cc - 'a';
111
112 if( id < vg_list_size( colour_sets ) )
113 {
114 v3_copy( colour_sets[ id ], target );
115 return;
116 }
117 }
118
119 v3_copy( (v3f){0.0f,0.0f,0.0f}, target );
120 }
121
122 struct mesh
123 {
124 GLuint vao, vbo;
125 u32 elements;
126 };
127
128 static void init_mesh( struct mesh *m, float *tris, u32 length )
129 {
130 m->elements = length/3;
131 glGenVertexArrays( 1, &m->vao );
132 glGenBuffers( 1, &m->vbo );
133
134 glBindVertexArray( m->vao );
135 glBindBuffer( GL_ARRAY_BUFFER, m->vbo );
136 glBufferData( GL_ARRAY_BUFFER, length*sizeof(float), tris, GL_STATIC_DRAW );
137
138 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
139 glEnableVertexAttribArray( 0 );
140
141 VG_CHECK_GL();
142 }
143
144 static void free_mesh( struct mesh *m )
145 {
146 glDeleteVertexArrays( 1, &m->vao );
147 glDeleteBuffers( 1, &m->vbo );
148 }
149
150 static void draw_mesh( int const start, int const count )
151 {
152 glDrawArrays( GL_TRIANGLES, start*3, count*3 );
153 }
154
155 static void use_mesh( struct mesh *m )
156 {
157 glBindVertexArray( m->vao );
158 }
159
160 struct world
161 {
162 struct cell
163 {
164 u32 state;
165 u8 config;
166 }
167 *data;
168
169 u32 frame;
170
171 u32 sim_frame;
172 float sim_start;
173 int simulating;
174
175 struct cell_terminal
176 {
177 // TODO: Split into input/output structures
178 char *conditions;
179 char recv[12];
180 int recv_count;
181 int id;
182 }
183 *io;
184
185 u32 w, h;
186
187 struct mesh tile, circle;
188
189 int selected, tile_x, tile_y;
190 v2f tile_pos;
191
192 struct fish
193 {
194 v2i pos;
195 v2i dir;
196 int alive;
197 char payload;
198 }
199 fishes[16];
200
201 int num_fishes;
202 } world = {};
203
204 static void map_free(void)
205 {
206 for( int i = 0; i < arrlen( world.io ); i ++ )
207 arrfree( world.io[ i ].conditions );
208
209 arrfree( world.data );
210 arrfree( world.io );
211
212 world.w = 0;
213 world.h = 0;
214 world.data = NULL;
215 world.io = NULL;
216 }
217
218 static void map_reclassify( v2i start, v2i end );
219 static int map_load( const char *str )
220 {
221 map_free();
222
223 char const *c = str;
224
225 // Scan for width
226 for(;; world.w ++)
227 {
228 if( str[world.w] == ';' )
229 break;
230 else if( !str[world.w] )
231 {
232 vg_error( "Unexpected EOF when parsing level\n" );
233 return 0;
234 }
235 }
236
237 struct cell *row = arraddnptr( world.data, world.w );
238 int cx = 0;
239 int reg_start = 0, reg_end = 0;
240
241 for(;;)
242 {
243 if( !*c )
244 break;
245
246 if( *c == ';' )
247 {
248 c ++;
249
250 // Parse attribs
251 if( *c != '\n' )
252 {
253 while( *c )
254 {
255 if( reg_start < reg_end )
256 {
257 if( *c >= 'a' && *c <= 'z' )
258 {
259 arrpush( world.io[ reg_start ].conditions, *c );
260 }
261 else
262 {
263 if( *c == ',' || *c == '\n' )
264 {
265 reg_start ++;
266
267 if( *c == '\n' )
268 break;
269 }
270 else
271 {
272 vg_error( "Unkown attribute '%c' (row: %u)\n", *c, world.h );
273 return 0;
274 }
275 }
276 }
277 else
278 {
279 vg_error( "Too many values to assign (row: %u)\n", world.h );
280 return 0;
281 }
282
283 c ++;
284 }
285 }
286
287 if( reg_start != reg_end )
288 {
289 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world.h, reg_start, reg_end );
290 return 0;
291 }
292
293 if( cx != world.w )
294 {
295 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world.h, cx, world.w );
296 return 0;
297 }
298
299 row = arraddnptr( world.data, world.w );
300 cx = 0;
301 world.h ++;
302 reg_end = reg_start = arrlen( world.io );
303 }
304 else
305 {
306 if( cx == world.w )
307 {
308 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world.h, cx, world.w );
309 return 0;
310 }
311
312 // Tile initialization
313 // row[ cx ] .. etc
314
315 if( *c == '+' || *c == '-' )
316 {
317 struct cell_terminal term = { .id = cx + world.h*world.w };
318 arrpush( world.io, term );
319 row[ cx ++ ].state = *c == '+'? FLAG_INPUT: FLAG_OUTPUT;
320 reg_end ++;
321 }
322 else if( *c == '#' )
323 {
324 row[ cx ++ ].state = FLAG_WALL;
325 }
326 else
327 {
328 row[ cx ++ ].state = 0x00;
329 }
330 }
331
332 c ++;
333 }
334
335 map_reclassify( NULL, NULL );
336 vg_success( "Map loaded! (%u:%u)\n", world.w, world.h );
337 return 1;
338 }
339
340 static struct cell *pcell( v2i pos )
341 {
342 return &world.data[ pos[1]*world.w + pos[0] ];
343 }
344
345 int main( int argc, char *argv[] )
346 {
347 vg_init( argc, argv, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
348 }
349
350 void vg_start(void)
351 {
352 // Quad mesh
353 {
354 float quad_mesh[] =
355 {
356 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
357 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
358
359 0.48f, 0.48f, 0.5f, 0.52f, 0.52f, 0.52f, // Static dot
360 0.375f, 0.25f, 0.5f, 0.75f, 0.625f, 0.25f, // Downwards pointing arrow
361 0.25f, 0.625f, 0.75f, 0.5f, 0.25f, 0.375f, // Left
362 0.625f, 0.75f, 0.5f, 0.25f, 0.375f, 0.75f, // up
363 0.75f, 0.375f, 0.25f, 0.5f, 0.75f, 0.625f
364 };
365
366 init_mesh( &world.tile, quad_mesh, vg_list_size(quad_mesh) );
367 }
368
369 // Circle mesh
370 {
371 float circle_mesh[32*6*3];
372 int res = vg_list_size( circle_mesh ) / (6*3);
373
374 for( int i = 0; i < res; i ++ )
375 {
376 v2f v0 = { sinf( ((float)i/(float)res)*VG_TAUf ), cosf( ((float)i/(float)res)*VG_TAUf ) };
377 v2f v1 = { sinf( ((float)(i+1)/(float)res)*VG_TAUf ), cosf( ((float)(i+1)/(float)res)*VG_TAUf ) };
378
379 circle_mesh[ i*6+0 ] = 0.0f;
380 circle_mesh[ i*6+1 ] = 0.0f;
381
382 v2_copy( v0, circle_mesh + 32*6 + i*12 );
383 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+2 );
384 v2_copy( v1, circle_mesh + 32*6 + i*12+4 );
385
386 v2_copy( v1, circle_mesh + 32*6 + i*12+6 );
387 v2_muls( v1, 0.8f, circle_mesh + 32*6 + i*12+8 );
388 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+10 );
389
390 v2_copy( v0, circle_mesh + i*6+4 );
391 v2_copy( v1, circle_mesh + i*6+2 );
392 v2_copy( v0, circle_mesh+i*6+4 );
393 v2_copy( v1, circle_mesh+i*6+2 );
394 }
395
396 init_mesh( &world.circle, circle_mesh, vg_list_size( circle_mesh ) );
397 }
398
399 resource_load_main();
400
401 map_load( level_pack[ 0 ] );
402 }
403
404 void vg_free(void)
405 {
406 resource_free_main();
407
408 free_mesh( &world.tile );
409 free_mesh( &world.circle );
410
411 map_free();
412 }
413
414 static int cell_interactive( v2i co )
415 {
416 // Bounds check
417 if( co[0] < 2 || co[0] >= world.w-2 || co[1] < 2 || co[1] >= world.h-2 )
418 return 0;
419
420 // Flags check
421 if( world.data[ world.w*co[1] + co[0] ].state & (FLAG_WALL|FLAG_INPUT|FLAG_OUTPUT) )
422 return 0;
423
424 // List of 3x3 configurations that we do not allow
425 static u32 invalid_src[][9] =
426 {
427 { 0,1,0,
428 1,1,1,
429 0,1,0
430 },
431 { 0,0,0,
432 0,1,1,
433 0,1,1
434 },
435 { 0,0,0,
436 1,1,0,
437 1,1,0
438 },
439 { 0,1,1,
440 0,1,1,
441 0,0,0
442 },
443 { 1,1,0,
444 1,1,0,
445 0,0,0
446 },
447 { 0,1,0,
448 0,1,1,
449 0,1,0
450 },
451 { 0,1,0,
452 1,1,0,
453 0,1,0
454 }
455 };
456
457 // Statically compile invalid configurations into bitmasks
458 static u32 invalid[ vg_list_size(invalid_src) ];
459
460 for( int i = 0; i < vg_list_size(invalid_src); i ++ )
461 {
462 u32 comped = 0x00;
463
464 for( int j = 0; j < 3; j ++ )
465 for( int k = 0; k < 3; k ++ )
466 comped |= invalid_src[i][ j*3+k ] << ((j*5)+k);
467
468 invalid[i] = comped;
469 }
470
471 // Extract 5x5 grid surrounding tile
472 u32 blob = 0x1000;
473 for( int y = co[1]-2; y < co[1]+3; y ++ )
474 for( int x = co[0]-2; x < co[0]+3; x ++ )
475 {
476 struct cell *cell = pcell((v2i){x,y});
477
478 if( cell && (cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT)) )
479 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
480 }
481
482 // Run filter over center 3x3 grid to check for invalid configurations
483 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
484 for( int i = 0; i < vg_list_size(kernel); i ++ )
485 {
486 if( blob & (0x1 << (6+kernel[i])) )
487 {
488 u32 window = blob >> kernel[i];
489
490 for( int j = 0; j < vg_list_size(invalid); j ++ )
491 if((window & invalid[j]) == invalid[j])
492 return 0;
493 }
494 }
495
496 return 1;
497 }
498
499 static void map_reclassify( v2i start, v2i end )
500 {
501 v2i full_start = { 2,2 };
502 v2i full_end = { world.w-2, world.h-2 };
503
504 if( !start || !end )
505 {
506 start = full_start;
507 end = full_end;
508 }
509
510 for( int y = vg_max( start[1], full_start[1] ); y < vg_min( end[1], full_end[1] ); y ++ )
511 {
512 for( int x = vg_max( start[0], full_start[0] ); x < vg_min( end[0], full_end[0] ); x ++ )
513 {
514 v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
515
516 u8 config = 0x00;
517
518 if( pcell((v2i){x,y})->state & FLAG_CANAL )
519 {
520 for( int i = 0; i < vg_list_size( dirs ); i ++ )
521 {
522 struct cell *neighbour = pcell((v2i){x+dirs[i][0], y+dirs[i][1]});
523 if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
524 config |= 0x1 << i;
525 }
526 }
527 else config = 0xF;
528
529 pcell((v2i){x,y})->config = config;
530 }
531 }
532 }
533
534 void vg_update(void)
535 {
536 static int curlevel = 0;
537 int changelvl = curlevel;
538 if( vg_get_button_down( "prev" ) ) { if( curlevel > 0 ) changelvl --; }
539 else if( vg_get_button_down( "next" ) ) { if( curlevel < vg_list_size( level_pack )-1 ) changelvl ++; }
540
541 if( changelvl != curlevel )
542 {
543 map_load( level_pack[ changelvl ] );
544 curlevel = changelvl;
545
546 // TEMP!!! code dupe
547 world.simulating = 0;
548 world.num_fishes = 0;
549 world.sim_frame = 0;
550
551 for( int i = 0; i < arrlen( world.io ); i ++ )
552 world.io[i].recv_count = 0;
553
554 vg_info( "Stopping simulation!\n" );
555 }
556
557 float ratio = (float)vg_window_y / (float)vg_window_x;
558 float const size = 9.5f;
559
560 v3f origin;
561 origin[0] = -0.5f * world.w;
562 origin[1] = -0.5f * world.h;
563 origin[2] = 0.0f;
564
565 m3x3_projection( m_projection, -size, size, -size*ratio, size*ratio );
566 m3x3_identity( m_view );
567 m3x3_translate( m_view, origin );
568 m3x3_mul( m_projection, m_view, vg_pv );
569 vg_projection_update();
570
571 // Input stuff
572 v2_copy( vg_mouse_ws, world.tile_pos );
573
574 world.tile_x = floorf( world.tile_pos[0] );
575 world.tile_y = floorf( world.tile_pos[1] );
576
577 // Tilemap editing
578 if( !world.simulating )
579 {
580 if( cell_interactive( (v2i){ world.tile_x, world.tile_y } ))
581 {
582 world.selected = world.tile_y * world.w + world.tile_x;
583
584 if( vg_get_button_down("primary") )
585 {
586 world.data[ world.selected ].state ^= FLAG_CANAL;
587
588 if( world.data[ world.selected ].state & FLAG_CANAL )
589 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 3, 6 );
590 else
591 sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 0, 3 );
592
593 map_reclassify( (v2i){ world.tile_x -2, world.tile_y -2 },
594 (v2i){ world.tile_x +2, world.tile_y +2 } );
595 }
596 }
597 else
598 world.selected = -1;
599 }
600
601 // Simulation stop/start
602 if( vg_get_button_down("go") )
603 {
604 if( world.simulating )
605 {
606 world.simulating = 0;
607 world.num_fishes = 0;
608 world.sim_frame = 0;
609
610 for( int i = 0; i < arrlen( world.io ); i ++ )
611 world.io[i].recv_count = 0;
612
613 vg_info( "Stopping simulation!\n" );
614 }
615 else
616 {
617 vg_success( "Starting simulation!\n" );
618
619 sfx_set_playrnd( &audio_rolls, &audio_system_balls_rolling, 0, 1 );
620
621 world.simulating = 1;
622 world.num_fishes = 0;
623 world.sim_frame = 0;
624 world.sim_start = vg_time;
625
626 for( int i = 0; i < world.w*world.h; i ++ )
627 {
628 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
629 }
630
631 for( int i = 0; i < arrlen( world.io ); i ++ )
632 world.io[i].recv_count = 0;
633 }
634 }
635
636 // Fish ticks
637 if( world.simulating )
638 {
639 while( world.sim_frame < (int)((vg_time-world.sim_start)*2.0f) )
640 {
641 vg_info( "frame: %u\n", world.sim_frame );
642
643 for( int i = 0; i < arrlen( world.io ); i ++ )
644 {
645 struct cell_terminal *term = &world.io[ i ];
646 int posx = term->id % world.w;
647 int posy = (term->id - posx)/world.w;
648 int is_input = world.data[ term->id ].state & FLAG_INPUT;
649
650 if( is_input )
651 {
652 if( world.sim_frame < arrlen( term->conditions ) )
653 {
654 struct fish *fish = &world.fishes[world.num_fishes++];
655 fish->pos[0] = posx;
656 fish->pos[1] = posy;
657 fish->alive = 1;
658 fish->payload = term->conditions[world.sim_frame];
659
660 int can_spawn = 0;
661
662 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
663 for( int j = 0; j < vg_list_size(dirs); j ++ )
664 if( pcell( (v2i){ posx+dirs[j][0], posy+dirs[j][1] } )->state & FLAG_CANAL )
665 {
666 fish->dir[0] = dirs[j][0];
667 fish->dir[1] = dirs[j][1];
668 can_spawn = 1;
669 break;
670 }
671
672 if( !can_spawn )
673 world.num_fishes--;
674 }
675 }
676 }
677
678 // Update splitter deltas
679 for( int i = 0; i < world.h*world.w; i ++ )
680 {
681 struct cell *cell = &world.data[i];
682 if( cell->config == k_cell_type_split )
683 {
684 cell->state &= ~FLAG_FLIP_ROTATING;
685 }
686 }
687
688 for( int i = 0; i < world.num_fishes; i ++ )
689 {
690 struct fish *fish = &world.fishes[i];
691 struct cell *cell_current = pcell( fish->pos );
692
693 if( !fish->alive )
694 continue;
695
696 // Apply to output
697 if( cell_current->state & FLAG_OUTPUT )
698 {
699 for( int j = 0; j < arrlen( world.io ); j ++ )
700 {
701 struct cell_terminal *term = &world.io[j];
702
703 if( term->id == fish->pos[1]*world.w + fish->pos[0] )
704 {
705 term->recv[ term->recv_count ++ ] = fish->payload;
706 break;
707 }
708 }
709
710 fish->alive = 0;
711 continue;
712 }
713
714 if( cell_current->config == k_cell_type_split )
715 {
716 // Flip flop L/R
717 fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
718 fish->dir[1] = 0;
719
720 cell_current->state ^= FLAG_FLIP_FLOP;
721 }
722 else if( cell_current->config == k_cell_type_merge )
723 {
724 // Can only move up
725 fish->dir[0] = 0;
726 fish->dir[1] = -1;
727 }
728 else
729 {
730 struct cell *cell_next = pcell( (v2i){ fish->pos[0]+fish->dir[0], fish->pos[1]+fish->dir[1] } );
731 if( !(cell_next->state & (FLAG_CANAL|FLAG_OUTPUT)) )
732 {
733 // Try other directions for valid, so down, left, right..
734 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
735 vg_info( "Trying some other directions...\n" );
736
737 for( int j = 0; j < vg_list_size(dirs); j ++ )
738 {
739 if( (dirs[j][0] == -fish->dir[0]) && (dirs[j][1] == -fish->dir[1]) )
740 continue;
741
742 if( pcell( (v2i){ fish->pos[0]+dirs[j][0], fish->pos[1]+dirs[j][1] } )->state & (FLAG_CANAL|FLAG_OUTPUT) )
743 {
744 fish->dir[0] = dirs[j][0];
745 fish->dir[1] = dirs[j][1];
746 }
747 }
748 }
749 }
750
751 fish->pos[0] += fish->dir[0];
752 fish->pos[1] += fish->dir[1];
753
754 struct cell *cell_entry = pcell( fish->pos );
755
756 if( !(cell_entry->state & (FLAG_INPUT|FLAG_CANAL|FLAG_OUTPUT) ))
757 fish->alive = 0;
758 else
759 if( cell_entry->config == k_cell_type_split )
760 {
761 sfx_set_playrnd( &audio_splitter, &audio_system_balls_important, 0, 1 );
762 cell_entry->state |= FLAG_FLIP_ROTATING;
763 }
764 }
765
766 world.sim_frame ++;
767 }
768 }
769 }
770
771 static void render_tiles( v2i start, v2i end, v4f const regular_colour, v4f const selected_colour )
772 {
773 v2i full_start = { 0,0 };
774 v2i full_end = { world.w, world.h };
775
776 if( !start || !end )
777 {
778 start = full_start;
779 end = full_end;
780 }
781
782 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
783
784 for( int y = start[1]; y < end[1]; y ++ )
785 {
786 for( int x = start[0]; x < end[0]; x ++ )
787 {
788 struct cell *cell = pcell((v2i){x,y});
789 int selected = world.selected == y*world.w + x;
790
791 int tile_offsets[][2] =
792 {
793 {2, 0}, {0, 3}, {0, 2}, {2, 2},
794 {1, 0}, {2, 3}, {3, 2}, {1, 3},
795 {3, 1}, {0, 1}, {1, 2}, {2, 1},
796 {1, 1}, {3, 3}, {2, 1}, {2, 1}
797 };
798
799 int uv[2] = { 3, 0 };
800
801 if( cell->state & FLAG_CANAL )
802 {
803 uv[0] = tile_offsets[ cell->config ][0];
804 uv[1] = tile_offsets[ cell->config ][1];
805 }
806
807 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y, uv[0], uv[1] );
808 if( selected )
809 {
810 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, selected_colour );
811 draw_mesh( 0, 2 );
812 glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
813 }
814 else
815 draw_mesh( 0, 2 );
816 }
817 }
818 }
819
820 void vg_render(void)
821 {
822 glViewport( 0,0, vg_window_x, vg_window_y );
823
824 glDisable( GL_DEPTH_TEST );
825 glClearColor( 0.8f, 0.8f, 0.8f, 1.0f );
826 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
827
828 float scaled_time = 0.0f, frame_lerp = 0.0f;
829
830 if( world.simulating )
831 {
832 scaled_time = (vg_time-world.sim_start)*2.0f;
833 frame_lerp = scaled_time - (float)world.sim_frame;
834 }
835
836 v4f const colour_default = {1.0f, 1.0f, 1.0f, 1.0f};
837 v4f const colour_selected = {0.90f, 0.92f, 1.0f, 1.0f};
838
839 v2f const curve_3[] = {{0.5f,1.0f},{0.5f,0.625f},{0.625f,0.5f},{1.0f,0.5f}};
840 v2f const curve_6[] = {{0.5f,1.0f},{0.5f,0.625f},{0.375f,0.5f},{0.0f,0.5f}};
841 v2f const curve_9[] = {{1.0f,0.5f},{0.625f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
842 v2f const curve_12[]= {{0.0f,0.5f},{0.375f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
843
844 v2f const curve_7[] = {{0.5f,0.8438f},{0.875f,0.8438f},{0.625f,0.5f},{1.0f,0.5f}};
845 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}};
846
847 float const curve_7_linear_section = 0.1562f;
848
849 // TILE SET RENDERING
850 // todo: just slam everything into a mesh...
851 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
852 // in ~3 subBuffers
853 // Currently we're uploading a fair amount of data every frame anyway.
854 // NOTE: this is for final optimisations ONLY!
855 // ======================================================================
856
857 use_mesh( &world.tile );
858
859 SHADER_USE( shader_tile_main );
860
861 m2x2f subtransform;
862 m2x2_identity( subtransform );
863 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
864 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
865 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 0.0f );
866
867 // Bind textures
868 vg_tex2d_bind( &tex_tile_data, 0 );
869 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
870
871 vg_tex2d_bind( &tex_wood, 1 );
872 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
873
874 render_tiles( NULL, NULL, colour_default, colour_default );
875
876 glEnable(GL_BLEND);
877 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
878 glBlendEquation(GL_FUNC_ADD);
879
880 SHADER_USE( shader_ball );
881 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
882
883 vg_tex2d_bind( &tex_ball, 0 );
884 glUniform1i( SHADER_UNIFORM( shader_ball, "uTexMain" ), 0 );
885
886 // Draw 'fish'
887 if( world.simulating )
888 {
889 for( int i = 0; i < world.num_fishes; i ++ )
890 {
891 struct fish *fish = &world.fishes[i];
892
893 if( !fish->alive )
894 continue;
895
896 // Evaluate position
897 struct cell *cell = pcell(fish->pos);
898 v2f fish_pos;
899
900 v2f const *curve;
901
902 float t = frame_lerp;
903
904 switch( cell->config )
905 {
906 case 13:
907 if( fish->dir[0] == 1 )
908 curve = curve_12;
909 else
910 curve = curve_9;
911 break;
912 case 3: curve = curve_3; break;
913 case 6: curve = curve_6; break;
914 case 9: curve = curve_9; break;
915 case 12: curve = curve_12; break;
916 case 7:
917 if( t > curve_7_linear_section )
918 {
919 t -= curve_7_linear_section;
920 t *= (1.0f/(1.0f-curve_7_linear_section));
921
922 curve = cell->state & FLAG_FLIP_FLOP? curve_7: curve_7_1;
923 }
924 else curve = NULL;
925 break;
926 default: curve = NULL; break;
927 }
928
929 if( curve )
930 {
931 float t2 = t * t;
932 float t3 = t * t * t;
933
934 float cA = 3.0f*t2 - 3.0f*t3;
935 float cB = 3.0f*t3 - 6.0f*t2 + 3.0f*t;
936 float cC = 3.0f*t2 - t3 - 3.0f*t + 1.0f;
937
938 fish_pos[0] = t3*curve[3][0] + cA*curve[2][0] + cB*curve[1][0] + cC*curve[0][0];
939 fish_pos[1] = t3*curve[3][1] + cA*curve[2][1] + cB*curve[1][1] + cC*curve[0][1];
940 fish_pos[0] += (float)fish->pos[0];
941 fish_pos[1] += (float)fish->pos[1];
942 }
943 else
944 {
945 v2f origin;
946 origin[0] = (float)fish->pos[0] + (float)fish->dir[0]*-0.5f + 0.5f;
947 origin[1] = (float)fish->pos[1] + (float)fish->dir[1]*-0.5f + 0.5f;
948
949 fish_pos[0] = origin[0] + (float)fish->dir[0]*t;
950 fish_pos[1] = origin[1] + (float)fish->dir[1]*t;
951 }
952
953 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
954 colour_code_v3( fish->payload, dot_colour );
955
956 glUniform3fv( SHADER_UNIFORM( shader_ball, "uColour" ), 1, dot_colour );
957 glUniform2f( SHADER_UNIFORM( shader_ball, "uOffset" ), fish_pos[0], fish_pos[1] );
958 draw_mesh( 0, 32 );
959 }
960 }
961
962
963 SHADER_USE( shader_tile_main );
964
965 // Bind textures
966 vg_tex2d_bind( &tex_tile_data, 0 );
967 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
968
969 vg_tex2d_bind( &tex_wood, 1 );
970 glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
971
972 render_tiles( NULL, NULL, colour_default, colour_selected );
973
974 // Draw splitters
975
976 for( int y = 0; y < world.h; y ++ )
977 {
978 for( int x = 0; x < world.w; x ++ )
979 {
980 struct cell *cell = pcell((v2i){x,y});
981
982 if( cell->config == k_cell_type_split )
983 {
984 float rotation = cell->state & FLAG_FLIP_FLOP? vg_rad( -45.0f ): vg_rad( 45.0f );
985
986 if( cell->state & FLAG_FLIP_ROTATING )
987 {
988 if( (frame_lerp > curve_7_linear_section) )
989 {
990 float const rotation_speed = 0.4f;
991 if( (frame_lerp < 1.0f-rotation_speed) )
992 {
993 float t = frame_lerp - curve_7_linear_section;
994 t *= -2.0f * (1.0f/(1.0f-(curve_7_linear_section+rotation_speed)));
995 t += 1.0f;
996
997 rotation *= t;
998 }
999 else
1000 rotation *= -1.0f;
1001 }
1002 }
1003
1004 m2x2_create_rotation( subtransform, rotation );
1005
1006 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1007 glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y + 0.125f, 0.0f, 0.0f );
1008 draw_mesh( 0, 2 );
1009 }
1010 }
1011 }
1012
1013 // Edit overlay
1014 if( world.selected != -1 && !(world.data[ world.selected ].state & FLAG_CANAL) )
1015 {
1016 v2i new_begin = { world.tile_x - 2, world.tile_y - 2 };
1017 v2i new_end = { world.tile_x + 2, world.tile_y + 2 };
1018
1019 world.data[ world.selected ].state ^= FLAG_CANAL;
1020 map_reclassify( new_begin, new_end );
1021
1022 m2x2_identity( subtransform );
1023 glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 1.0f );
1024 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
1025 glUniform2fv( SHADER_UNIFORM( shader_tile_main, "uMousePos" ), 1, world.tile_pos );
1026
1027 render_tiles( new_begin, new_end, colour_default, colour_default );
1028
1029 world.data[ world.selected ].state ^= FLAG_CANAL;
1030 map_reclassify( new_begin, new_end );
1031 }
1032
1033 //glDisable(GL_BLEND);
1034
1035 glDisable(GL_BLEND);
1036
1037 SHADER_USE( shader_tile_colour );
1038 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
1039 use_mesh( &world.circle );
1040
1041 // Draw i/o arrays
1042 for( int i = 0; i < arrlen( world.io ); i ++ )
1043 {
1044 struct cell_terminal *term = &world.io[ i ];
1045 int posx = term->id % world.w;
1046 int posy = (term->id - posx)/world.w;
1047 int is_input = world.data[ term->id ].state & FLAG_INPUT;
1048
1049 int const filled_start = 0;
1050 int const filled_count = 32;
1051 int const empty_start = 32;
1052 int const empty_count = 32*2;
1053
1054 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
1055
1056 for( int j = 0; j < arrlen( term->conditions ); j ++ )
1057 {
1058 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + 0.2f, 0.1f );
1059
1060 if( is_input )
1061 {
1062 colour_code_v3( term->conditions[j], dot_colour );
1063 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1064
1065 // Draw filled if tick not passed, draw empty if empty
1066 if( world.sim_frame > j )
1067 draw_mesh( empty_start, empty_count );
1068 else
1069 draw_mesh( filled_start, filled_count );
1070 }
1071 else
1072 {
1073 if( term->recv_count > j )
1074 {
1075 colour_code_v3( term->recv[j], dot_colour );
1076 v3_muls( dot_colour, 0.8f, dot_colour );
1077 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1078
1079 draw_mesh( filled_start, filled_count );
1080 }
1081
1082 colour_code_v3( term->conditions[j], dot_colour );
1083 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
1084
1085 draw_mesh( empty_start, empty_count );
1086 }
1087 }
1088 }
1089
1090 if( world.simulating )
1091 {
1092 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.0f, 0.0f, 0.0f, 1.0f );
1093 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 );
1094 draw_mesh( 0, 32 );
1095 }
1096 }
1097
1098 void vg_ui(void)
1099 {
1100 //ui_test();
1101 }