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