fix bug with rolling over output
[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
6 SHADER_DEFINE( shader_tile_colour,
7
8 // VERTEX
9 "layout (location=0) in vec2 a_co;"
10 "uniform mat3 uPv;"
11 "uniform vec3 uOffset;"
12 ""
13 "void main()"
14 "{"
15 "gl_Position = vec4( uPv * vec3( a_co * uOffset.z + uOffset.xy, 1.0 ), 1.0 );"
16 "}",
17
18 // FRAGMENT
19 "out vec4 FragColor;"
20 "uniform vec4 uColour;"
21 ""
22 "void main()"
23 "{"
24 "FragColor = uColour;"
25 "}"
26 ,
27 UNIFORMS({ "uPv", "uOffset", "uColour" })
28 )
29
30 m3x3f m_projection;
31 m3x3f m_view;
32 m3x3f m_mdl;
33
34 #define FLAG_INPUT 0x1
35 #define FLAG_OUTPUT 0x2
36 #define FLAG_CANAL 0x4
37 #define FLAG_WALL 0x8
38 #define FLAG_DROP_L 0x10
39 #define FLAG_SPLIT 0x20
40 #define FLAG_MERGER 0x40
41 #define FLAG_DROP_R 0x80
42 #define FLAG_FLIP_FLOP 0x100
43
44 v3f colour_sets[] =
45 { { 0.9f, 0.2f, 0.01f },
46 { 0.2f, 0.9f, 0.14f },
47 { 0.1f, 0.3f, 0.85f } };
48
49 static void colour_code_v3( char cc, v3f target )
50 {
51 if( cc >= 'a' && cc <= 'z' )
52 {
53 int id = cc - 'a';
54
55 if( id < vg_list_size( colour_sets ) )
56 {
57 v3_copy( colour_sets[ id ], target );
58 return;
59 }
60 }
61
62 v3_copy( (v3f){0.0f,0.0f,0.0f}, target );
63 }
64
65 struct mesh
66 {
67 GLuint vao, vbo;
68 u32 elements;
69 };
70
71 static void init_mesh( struct mesh *m, float *tris, u32 length )
72 {
73 m->elements = length/3;
74 glGenVertexArrays( 1, &m->vao );
75 glGenBuffers( 1, &m->vbo );
76
77 glBindVertexArray( m->vao );
78 glBindBuffer( GL_ARRAY_BUFFER, m->vbo );
79 glBufferData( GL_ARRAY_BUFFER, length*sizeof(float), tris, GL_STATIC_DRAW );
80
81 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
82 glEnableVertexAttribArray( 0 );
83
84 VG_CHECK_GL();
85 }
86
87 static void free_mesh( struct mesh *m )
88 {
89 glDeleteVertexArrays( 1, &m->vao );
90 glDeleteBuffers( 1, &m->vbo );
91 }
92
93 static void draw_mesh( int const start, int const count )
94 {
95 glDrawArrays( GL_TRIANGLES, start*3, count*3 );
96 }
97
98 static void use_mesh( struct mesh *m )
99 {
100 glBindVertexArray( m->vao );
101 }
102
103 struct world
104 {
105 struct cell
106 {
107 u32 state;
108 u8 config;
109 }
110 *data;
111
112 u32 frame;
113
114 u32 sim_frame;
115 float sim_start;
116 int simulating;
117
118 struct cell_terminal
119 {
120 // TODO: Split into input/output structures
121 char *conditions;
122 char recv[12];
123 int recv_count;
124 int id;
125 }
126 *io;
127
128 u32 w, h;
129
130 struct mesh tile, circle, splitter_l, splitter_r;
131
132 int selected;
133
134 struct fish
135 {
136 v2i pos;
137 v2i dir;
138 int alive;
139 char payload;
140 }
141 fishes[16];
142
143 int num_fishes;
144 } world = {};
145
146 static void map_free(void)
147 {
148 for( int i = 0; i < arrlen( world.io ); i ++ )
149 arrfree( world.io[ i ].conditions );
150
151 arrfree( world.data );
152 arrfree( world.io );
153
154 world.w = 0;
155 world.h = 0;
156 world.data = NULL;
157 world.io = NULL;
158 }
159
160 static int map_load( const char *str )
161 {
162 map_free();
163
164 char const *c = str;
165
166 // Scan for width
167 for(;; world.w ++)
168 {
169 if( str[world.w] == ';' )
170 break;
171 else if( !str[world.w] )
172 {
173 vg_error( "Unexpected EOF when parsing level\n" );
174 return 0;
175 }
176 }
177
178 struct cell *row = arraddnptr( world.data, world.w );
179 int cx = 0;
180 int reg_start = 0, reg_end = 0;
181
182 for(;;)
183 {
184 if( !*c )
185 break;
186
187 if( *c == ';' )
188 {
189 c ++;
190
191 // Parse attribs
192 if( *c != '\n' )
193 {
194 while( *c )
195 {
196 if( reg_start < reg_end )
197 {
198 if( *c >= 'a' && *c <= 'z' )
199 {
200 arrpush( world.io[ reg_start ].conditions, *c );
201 }
202 else
203 {
204 if( *c == ',' || *c == '\n' )
205 {
206 reg_start ++;
207
208 if( *c == '\n' )
209 break;
210 }
211 else
212 {
213 vg_error( "Unkown attribute '%c' (row: %u)\n", *c, world.h );
214 return 0;
215 }
216 }
217 }
218 else
219 {
220 vg_error( "Too many values to assign (row: %u)\n", world.h );
221 return 0;
222 }
223
224 c ++;
225 }
226 }
227
228 if( reg_start != reg_end )
229 {
230 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world.h, reg_start, reg_end );
231 return 0;
232 }
233
234 if( cx != world.w )
235 {
236 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world.h, cx, world.w );
237 return 0;
238 }
239
240 row = arraddnptr( world.data, world.w );
241 cx = 0;
242 world.h ++;
243 reg_end = reg_start = arrlen( world.io );
244 }
245 else
246 {
247 if( cx == world.w )
248 {
249 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world.h, cx, world.w );
250 return 0;
251 }
252
253 // Tile initialization
254 // row[ cx ] .. etc
255
256 if( *c == '+' || *c == '-' )
257 {
258 struct cell_terminal term = { .id = cx + world.h*world.w };
259 arrpush( world.io, term );
260 row[ cx ++ ].state = *c == '+'? FLAG_INPUT: FLAG_OUTPUT;
261 reg_end ++;
262 }
263 else if( *c == '#' )
264 {
265 row[ cx ++ ].state = FLAG_WALL;
266 }
267 else
268 {
269 row[ cx ++ ].state = 0x00;
270 }
271 }
272
273 c ++;
274 }
275
276 vg_success( "Map loaded! (%u:%u)\n", world.w, world.h );
277 return 1;
278 }
279
280 static struct cell *pcell( v2i pos )
281 {
282 return &world.data[ pos[1]*world.w + pos[0] ];
283 }
284
285 int main( int argc, char *argv[] )
286 {
287 vg_init( argc, argv, "FishLadder" );
288 }
289
290 void vg_register(void)
291 {
292 SHADER_INIT( shader_tile_colour );
293 }
294
295 void vg_start(void)
296 {
297 // Quad mesh
298 {
299 float quad_mesh[] =
300 {
301 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
302 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
303
304 0.48f, 0.48f, 0.5f, 0.52f, 0.52f, 0.52f, // Static dot
305 0.375f, 0.25f, 0.5f, 0.75f, 0.625f, 0.25f, // Downwards pointing arrow
306 0.25f, 0.625f, 0.75f, 0.5f, 0.25f, 0.375f, // Left
307 0.625f, 0.75f, 0.5f, 0.25f, 0.375f, 0.75f, // up
308 0.75f, 0.375f, 0.25f, 0.5f, 0.75f, 0.625f
309 };
310
311 init_mesh( &world.tile, quad_mesh, vg_list_size(quad_mesh) );
312 }
313
314 // Circle mesh
315 {
316 float circle_mesh[32*6*3];
317 int res = vg_list_size( circle_mesh ) / (6*3);
318
319 for( int i = 0; i < res; i ++ )
320 {
321 v2f v0 = { sinf( ((float)i/(float)res)*VG_TAUf ), cosf( ((float)i/(float)res)*VG_TAUf ) };
322 v2f v1 = { sinf( ((float)(i+1)/(float)res)*VG_TAUf ), cosf( ((float)(i+1)/(float)res)*VG_TAUf ) };
323
324 circle_mesh[ i*6+0 ] = 0.0f;
325 circle_mesh[ i*6+1 ] = 0.0f;
326
327 v2_copy( v0, circle_mesh + 32*6 + i*12 );
328 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+2 );
329 v2_copy( v1, circle_mesh + 32*6 + i*12+4 );
330
331 v2_copy( v1, circle_mesh + 32*6 + i*12+6 );
332 v2_muls( v1, 0.8f, circle_mesh + 32*6 + i*12+8 );
333 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+10 );
334
335 v2_copy( v0, circle_mesh + i*6+4 );
336 v2_copy( v1, circle_mesh + i*6+2 );
337 v2_copy( v0, circle_mesh+i*6+4 );
338 v2_copy( v1, circle_mesh+i*6+2 );
339 }
340
341 init_mesh( &world.circle, circle_mesh, vg_list_size( circle_mesh ) );
342 }
343
344 // splitters (temp)
345 {
346 float splitter_l[] =
347 {
348 #include "models/splitter_l.obj.h"
349 };
350 float splitter_r[] =
351 {
352 #include "models/splitter_r.obj.h"
353 };
354
355 init_mesh( &world.splitter_l, splitter_l, vg_list_size( splitter_l ) );
356 init_mesh( &world.splitter_r, splitter_r, vg_list_size( splitter_r ) );
357 }
358
359 map_load
360 (
361 "#############;\n"
362 "###-#####-###;aaa,aa\n"
363 "## ##;\n"
364 "## ##;\n"
365 "## ##;\n"
366 "## ##;\n"
367 "## ##;\n"
368 "## ##;\n"
369 "###+#####+###;aa,aaa\n"
370 "#############;\n"
371 );
372 }
373
374 void vg_free(void)
375 {
376 free_mesh( &world.tile );
377 free_mesh( &world.circle );
378 free_mesh( &world.splitter_l );
379 free_mesh( &world.splitter_r );
380
381 map_free();
382 }
383
384 static int cell_interactive( v2i co )
385 {
386 // Bounds check
387 if( co[0] < 2 || co[0] >= world.w-2 || co[1] < 2 || co[1] >= world.h-2 )
388 return 0;
389
390 // Flags check
391 if( world.data[ world.w*co[1] + co[0] ].state & (FLAG_WALL|FLAG_INPUT|FLAG_OUTPUT) )
392 return 0;
393
394 // List of 3x3 configurations that we do not allow
395 static u32 invalid_src[][9] =
396 {
397 { 0,1,0,
398 1,1,1,
399 0,1,0
400 },
401 { 0,0,0,
402 0,1,1,
403 0,1,1
404 },
405 { 0,0,0,
406 1,1,0,
407 1,1,0
408 },
409 { 0,1,1,
410 0,1,1,
411 0,0,0
412 },
413 { 1,1,0,
414 1,1,0,
415 0,0,0
416 },
417 { 0,1,0,
418 0,1,1,
419 0,1,0
420 },
421 { 0,1,0,
422 1,1,0,
423 0,1,0
424 }
425 };
426
427 // Statically compile invalid configurations into bitmasks
428 static u32 invalid[ vg_list_size(invalid_src) ];
429
430 for( int i = 0; i < vg_list_size(invalid_src); i ++ )
431 {
432 u32 comped = 0x00;
433
434 for( int j = 0; j < 3; j ++ )
435 for( int k = 0; k < 3; k ++ )
436 comped |= invalid_src[i][ j*3+k ] << ((j*5)+k);
437
438 invalid[i] = comped;
439 }
440
441 // Extract 5x5 grid surrounding tile
442 u32 blob = 0x1000;
443 for( int y = co[1]-2; y < co[1]+3; y ++ )
444 for( int x = co[0]-2; x < co[0]+3; x ++ )
445 {
446 struct cell *cell = pcell((v2i){x,y});
447
448 if( cell && (cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT)) )
449 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
450 }
451
452 // Run filter over center 3x3 grid to check for invalid configurations
453 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
454 for( int i = 0; i < vg_list_size(kernel); i ++ )
455 {
456 if( blob & (0x1 << (6+kernel[i])) )
457 {
458 u32 window = blob >> kernel[i];
459
460 for( int j = 0; j < vg_list_size(invalid); j ++ )
461 if((window & invalid[j]) == invalid[j])
462 return 0;
463 }
464 }
465
466 return 1;
467 }
468
469 void vg_update(void)
470 {
471 float ratio = (float)vg_window_y / (float)vg_window_x;
472 float const size = 9.5f;
473
474 v3f origin;
475 origin[0] = -0.5f * world.w;
476 origin[1] = -0.5f * world.h;
477 origin[2] = 0.0f;
478
479 m3x3_projection( m_projection, -size, size, -size*ratio, size*ratio );
480 m3x3_identity( m_view );
481 m3x3_translate( m_view, origin );
482 m3x3_mul( m_projection, m_view, vg_pv );
483 vg_projection_update();
484
485 // Input stuff
486
487 v2f tile_pos;
488 v2_copy( vg_mouse_ws, tile_pos );
489
490 int tile_x = floorf( tile_pos[0] );
491 int tile_y = floorf( tile_pos[1] );
492
493 // Tilemap editing
494 if( !world.simulating )
495 {
496 if( cell_interactive( (v2i){ tile_x, tile_y } ))
497 {
498 world.selected = tile_y * world.w + tile_x;
499
500 if( vg_get_button_down("primary") )
501 {
502 world.data[ world.selected ].state ^= FLAG_CANAL;
503 }
504 }
505 else
506 world.selected = -1;
507 }
508
509 // Simulation stop/start
510 if( vg_get_button_down("go") )
511 {
512 if( world.simulating )
513 {
514 world.simulating = 0;
515 world.num_fishes = 0;
516 world.sim_frame = 0;
517
518 for( int i = 0; i < arrlen( world.io ); i ++ )
519 world.io[i].recv_count = 0;
520
521 vg_info( "Stopping simulation!\n" );
522 }
523 else
524 {
525 vg_success( "Starting simulation!\n" );
526
527 world.simulating = 1;
528 world.num_fishes = 0;
529 world.sim_frame = 0;
530 world.sim_start = vg_time;
531
532 for( int i = 0; i < world.w*world.h; i ++ )
533 {
534 world.data[ i ].state &= ~FLAG_FLIP_FLOP;
535 }
536
537 for( int i = 0; i < arrlen( world.io ); i ++ )
538 world.io[i].recv_count = 0;
539 }
540 }
541
542 // Simulation stuff
543 // ========================================================
544
545 // Reclassify world. TODO: Move into own function
546 for( int y = 2; y < world.h-2; y ++ )
547 {
548 for( int x = 2; x < world.w-2; x ++ )
549 {
550 v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
551
552 u8 config = 0x00;
553
554 if( pcell((v2i){x,y})->state & FLAG_CANAL )
555 {
556 for( int i = 0; i < vg_list_size( dirs ); i ++ )
557 {
558 struct cell *neighbour = pcell((v2i){x+dirs[i][0], y+dirs[i][1]});
559 if( neighbour->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
560 config |= 0x1 << i;
561 }
562 } else config = 0xF;
563
564 pcell((v2i){x,y})->config = config;
565 pcell((v2i){x,y})->state &= ~(FLAG_DROP_L|FLAG_DROP_R|FLAG_SPLIT|FLAG_MERGER);
566 }
567 }
568
569 for( int y = 2; y < world.h-2; y ++ )
570 for( int x = 2; x < world.w-2; x ++ )
571 {
572 // R,D,L,- 1110 (splitter, 1 drop created)
573
574 // R,-,L,U - 1011 (merger, 2 drop created)
575
576 u8 config = pcell((v2i){x,y})->config;
577
578 if( config == 0x7 ) // splitter
579 {
580 world.data[y*world.w+x ].state |= (FLAG_SPLIT | FLAG_DROP_L | FLAG_DROP_R);
581 }
582 else if( config == 0xD )
583 {
584 world.data[y*world.w+x-1].state |= FLAG_DROP_R;
585 world.data[y*world.w+x+1].state |= FLAG_DROP_L;
586 world.data[y*world.w+x].state |= FLAG_MERGER;
587 }
588 }
589
590 // Fish ticks
591 if( world.simulating )
592 {
593 while( world.sim_frame < (int)((vg_time-world.sim_start)*2.0f) )
594 {
595 vg_info( "frame: %u\n", world.sim_frame );
596
597 for( int i = 0; i < arrlen( world.io ); i ++ )
598 {
599 struct cell_terminal *term = &world.io[ i ];
600 int posx = term->id % world.w;
601 int posy = (term->id - posx)/world.w;
602 int is_input = world.data[ term->id ].state & FLAG_INPUT;
603
604 if( is_input )
605 {
606 if( world.sim_frame < arrlen( term->conditions ) )
607 {
608 struct fish *fish = &world.fishes[world.num_fishes++];
609 fish->pos[0] = posx;
610 fish->pos[1] = posy;
611 fish->alive = 1;
612 fish->payload = term->conditions[world.sim_frame];
613
614 int can_spawn = 0;
615
616 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
617 for( int j = 0; j < vg_list_size(dirs); j ++ )
618 {
619 v2i target;
620 if( pcell( (v2i){ posx+dirs[j][0], posy+dirs[j][1] } )->state & FLAG_CANAL )
621 {
622 fish->dir[0] = dirs[j][0];
623 fish->dir[1] = dirs[j][1];
624 can_spawn = 1;
625 break;
626 }
627 }
628
629 if( !can_spawn )
630 world.num_fishes--;
631 }
632 }
633 }
634
635 for( int i = 0; i < world.num_fishes; i ++ )
636 {
637 struct fish *fish = &world.fishes[i];
638 struct cell *cell_current = pcell( fish->pos );
639
640 if( !fish->alive )
641 continue;
642
643 // Apply to output
644 if( cell_current->state & FLAG_OUTPUT )
645 {
646 for( int j = 0; j < arrlen( world.io ); j ++ )
647 {
648 struct cell_terminal *term = &world.io[j];
649
650 if( term->id == fish->pos[1]*world.w + fish->pos[0] )
651 {
652 term->recv[ term->recv_count ++ ] = fish->payload;
653 break;
654 }
655 }
656
657 fish->alive = 0;
658 continue;
659 }
660
661 if( !(cell_current->state & (FLAG_INPUT|FLAG_CANAL)) )
662 {
663 fish->alive = 0;
664 }
665 else
666 {
667 if( cell_current->state & FLAG_SPLIT )
668 {
669 // Flip flop L/R
670 fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
671 fish->dir[1] = 0;
672
673 cell_current->state ^= FLAG_FLIP_FLOP;
674 }
675 else if( cell_current->state & FLAG_MERGER )
676 {
677 // Can only move up
678 fish->dir[0] = 0;
679 fish->dir[1] = -1;
680 }
681 else
682 {
683 struct cell *cell_next = pcell( (v2i){ fish->pos[0]+fish->dir[0], fish->pos[1]+fish->dir[1] } );
684 if( !(cell_next->state & (FLAG_CANAL|FLAG_OUTPUT)) )
685 {
686 // Try other directions for valid, so down, left, right..
687 v2i dirs[] = {{1,0},{-1,0},{0,-1}};
688 vg_info( "Trying some other directions...\n" );
689
690 for( int j = 0; j < vg_list_size(dirs); j ++ )
691 {
692 if( (dirs[j][0] == -fish->dir[0]) && (dirs[j][1] == -fish->dir[1]) )
693 continue;
694
695 if( pcell( (v2i){ fish->pos[0]+dirs[j][0], fish->pos[1]+dirs[j][1] } )->state & (FLAG_CANAL|FLAG_OUTPUT) )
696 {
697 fish->dir[0] = dirs[j][0];
698 fish->dir[1] = dirs[j][1];
699 }
700 }
701 }
702 }
703
704 fish->pos[0] += fish->dir[0];
705 fish->pos[1] += fish->dir[1];
706 }
707 }
708
709 world.sim_frame ++;
710 }
711 }
712 }
713
714 void vg_render(void)
715 {
716 glViewport( 0,0, vg_window_x, vg_window_y );
717
718 glDisable( GL_DEPTH_TEST );
719 glClearColor( 0.8f, 0.8f, 0.8f, 1.0f );
720 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
721
722 use_mesh( &world.tile );
723 SHADER_USE( shader_tile_colour );
724 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
725
726 // Shadow layer
727 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.5f, 0.5f, 0.5f, 1.0f );
728 for( int y = 0; y < world.h; y ++ )
729 for( int x = 0; x < world.w; x ++ )
730 {
731 struct cell *cell = pcell((v2i){x,y});
732
733 if( cell->state & FLAG_CANAL )
734 {
735 continue;
736 }
737
738 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)x - 0.2f, (float)y - 0.15f, 1.0f );
739 draw_mesh( 0, 2 );
740 }
741
742 for( int y = 0; y < world.h; y ++ )
743 {
744 for( int x = 0; x < world.w; x ++ )
745 {
746 struct cell *cell = pcell((v2i){x,y});
747 int selected = world.selected == y*world.w + x;
748
749 if( cell->state & FLAG_SPLIT )
750 {
751 glUniform4f( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 0.9f, 0.9f, 0.9f, 1.0f );
752 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)x, (float)y, 1.0f );
753
754 struct mesh *splitter = cell->state & FLAG_FLIP_FLOP? &world.splitter_r: &world.splitter_l;
755
756 use_mesh( splitter );
757 draw_mesh( 0, splitter->elements );
758 use_mesh( &world.tile );
759 }
760
761 if( (cell->state & FLAG_CANAL) && !selected )
762 continue;
763
764 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)x, (float)y, 1.0f );
765
766 v4f colour;
767
768 if( cell->state & FLAG_WALL ) { v4_copy( (v4f){ 0.2f, 0.2f, 0.2f, 1.0f }, colour ); }
769 else if( cell->state & FLAG_CANAL ) { v4_copy( (v4f){ 0.6f, 0.6f, 0.6f, 1.0f }, colour ); }
770 else if( cell->state & FLAG_INPUT ) { v4_copy( (v4f){ 0.5f, 0.5f, 0.5f, 1.0f }, colour ); }
771 else if( cell->state & FLAG_OUTPUT ) { v4_copy( (v4f){ 0.2f, 0.7f, 0.3f, 1.0f }, colour ); }
772 else v4_copy( (v4f){ 0.9f, 0.9f, 0.9f, 1.0f }, colour );
773
774 //if( cell->water[world.frame&0x1] )
775 // v4_copy( (v4f){ 0.2f, 0.3f, 0.7f * (float)(cell->water[world.frame&0x1]) * (1.0f/16.0f), 1.0f }, colour );
776
777 if( selected )
778 v3_muls( colour, sinf( vg_time )*0.25f + 0.5f, colour );
779
780 //if( cell->state & (FLAG_SPLIT) )
781 // v4_copy( (v4f){ 0.75f, 0.75f, 0.02f, 1.0f }, colour );
782 //if( cell->state & (FLAG_MERGER) )
783 // v4_copy( (v4f){ 0.75f, 0.02f, 0.75f, 1.0f }, colour );
784
785 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, colour );
786
787 draw_mesh( 0, 2 );
788 }
789 }
790
791 use_mesh( &world.circle );
792
793 // Draw i/o arrays
794 for( int i = 0; i < arrlen( world.io ); i ++ )
795 {
796 struct cell_terminal *term = &world.io[ i ];
797 int posx = term->id % world.w;
798 int posy = (term->id - posx)/world.w;
799 int is_input = world.data[ term->id ].state & FLAG_INPUT;
800
801 int const filled_start = 0;
802 int const filled_count = 32;
803 int const empty_start = 32;
804 int const empty_count = 32*2;
805
806 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
807
808 for( int j = 0; j < arrlen( term->conditions ); j ++ )
809 {
810 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + 0.2f, 0.1f );
811
812 if( is_input )
813 {
814 colour_code_v3( term->conditions[j], dot_colour );
815 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
816
817 // Draw filled if tick not passed, draw empty if empty
818 if( world.sim_frame > j )
819 draw_mesh( empty_start, empty_count );
820 else
821 draw_mesh( filled_start, filled_count );
822 }
823 else
824 {
825 if( term->recv_count > j )
826 {
827 colour_code_v3( term->recv[j], dot_colour );
828 v3_muls( dot_colour, 0.8f, dot_colour );
829 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
830
831 draw_mesh( filled_start, filled_count );
832 }
833
834 colour_code_v3( term->conditions[j], dot_colour );
835 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
836
837 draw_mesh( empty_start, empty_count );
838 }
839 }
840 }
841
842 // Draw 'fish'
843 if( world.simulating )
844 {
845 float scaled_time = (vg_time-world.sim_start)*2.0f;
846 float lerp = 1.0f-(scaled_time - (float)world.sim_frame);
847
848 v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
849
850 for( int i = 0; i < world.num_fishes; i ++ )
851 {
852 struct fish *fish = &world.fishes[i];
853
854 if( !fish->alive )
855 continue;
856
857 colour_code_v3( fish->payload, dot_colour );
858 glUniform4fv( SHADER_UNIFORM( shader_tile_colour, "uColour" ), 1, dot_colour );
859
860 glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)fish->pos[0] + 0.5f - (float)fish->dir[0]*lerp, (float)fish->pos[1] + 0.25f - (float)fish->dir[1]*lerp, 0.25f );
861 draw_mesh( 0, 32 );
862 }
863 }
864 }
865
866 void vg_ui(void){}