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