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