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