e1ca2fa9d046826375f563e5631266dc4c5ba7f3
[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 #define CELL_SHEET_X 512
7 #define CELL_SHEET_Y 512
8
9 SHADER_DEFINE( colour_shader,
10
11 // VERTEX
12 "layout (location=0) in vec2 a_co;"
13 "uniform mat4 uPv;"
14 "uniform mat4 uMdl;"
15 ""
16 "void main()"
17 "{"
18 " vec4 vert_pos = uPv * uMdl * vec4( a_co.x, 0.0, a_co.y, 1.0 );"
19 " gl_Position = vert_pos;"
20 "}",
21
22 // FRAGMENT
23 "out vec4 FragColor;"
24 "uniform vec4 uColour;"
25 ""
26 "void main()"
27 "{"
28 " FragColor = uColour;"
29 "}"
30 ,
31 UNIFORMS({ "uPv", "uMdl", "uColour" })
32 )
33
34 SHADER_DEFINE( tilemap_shader,
35
36 // VERTEX
37 "layout (location=0) in vec2 a_co;" // XY
38 "layout (location=1) in vec2 a_offset;" // XY offset
39 "layout (location=2) in vec4 a_data;" // atlas-uv, amt, other
40 "uniform mat4 uPv;"
41 "uniform vec2 uOrigin;"
42 ""
43 "out vec4 aCoords;"
44 ""
45 "void main()"
46 "{"
47 "vec2 world_coord = a_co+a_offset+uOrigin;"
48 "gl_Position = uPv * vec4( world_coord.x, 0.0, world_coord.y, 1.0 );"
49 "aCoords = vec4((a_co*0.98+0.01 + vec2(a_data.x,0.0)) * 0.0625, a_data.zw);"
50 "}",
51
52 // FRAGMENT
53 "uniform sampler2D uTexTiles;"
54 "uniform sampler2D uTexRipples;"
55 "uniform float uTime;"
56
57 "in vec4 aCoords;"
58 "out vec4 FragColor;"
59
60 "void main()"
61 "{"
62 "vec4 glyph = texture( uTexTiles, aCoords.xy );"
63 "vec4 ripple = texture( uTexRipples, vec2( glyph.x - uTime, glyph.y ));"
64 "float wstatus = -((aCoords.z/128.0)-1.0);" // -1 := None in, 0.0 := Hold middle, 1.0 := All out
65 "float dev = step( 0.0, glyph.x+wstatus ) * step( glyph.x+wstatus, 1.0 );" //+ abs(glyph.y-0.5) );"
66 "vec3 composite = mix( vec3(0.5,0.5,0.5), vec3(0.3,0.6,1.0) + ripple.xyz * 0.1, step( 0.75, glyph.a )*dev ) * glyph.z;"
67 "FragColor = vec4( composite, 1.0 );"
68 "}"
69 ,
70 UNIFORMS({ "uPv", "uTexTiles", "uTexRipples", "uTime", "uOrigin" })
71 )
72
73 mat4 m_projection;
74 mat4 m_view;
75 mat4 m_mdl;
76
77 int main( int argc, char *argv[] )
78 {
79 vg_init( argc, argv, "FishLadder" );
80 }
81
82 #define CELL_FLAG_INPUT 0x1
83 #define CELL_FLAG_OUTPUT 0x2
84 #define CELL_FLAG_IO (CELL_FLAG_INPUT|CELL_FLAG_OUTPUT)
85 #define CELL_FLAG_WALL 0x4
86 #define CELL_FLAG_HOVER 0x8
87 #define CELL_FLAG_ITER 0x10
88 #define CELL_FLAG_CANAL 0x20
89 #define CELL_FLAG_SPLIT 0x40 /* Does this cell split and have an incoming vertical connection? */
90 #define CELL_FLAG_WALKABLE (CELL_FLAG_IO|CELL_FLAG_CANAL)
91 #define CELL_FLAG_VISITED 0x80
92 #define CELL_FLAG_UPLVL 0x100
93 #define CELL_FLAG_MERGE 0x200
94
95 static struct
96 {
97 u32 x,y;
98
99 struct cell
100 {
101 u32 flags;
102 u32 model_id;
103
104 char *conditions;
105
106 int level;
107 int state;
108 int flowing[2];
109 int source_count;
110 }
111 * cells;
112
113 struct fish
114 {
115 int alive;
116 int co[2];
117 int dir[2];
118 char data;
119 }
120 fishes[ 20 ];
121 int num_fishes;
122
123 vec3 origin;
124 struct cell *selected;
125 int select_valid;
126 int playing;
127 u32 frame;
128
129 u32 *io;
130
131 struct vstack
132 {
133 struct vframe
134 {
135 int x, y;
136 int i;
137 }
138 frames[ 64 ];
139
140 int level;
141 u32 flags;
142 }
143 stack;
144
145 GLuint tile_texture;
146 GLuint flow_texture;
147
148 GLuint tiles_vao;
149 GLuint tiles_vbo;
150 }
151 map;
152
153 static void map_free(void)
154 {
155 for( int i = 0; i < arrlen( map.io ); i ++ )
156 {
157 arrfree( map.cells[ map.io[i] ].conditions );
158 }
159
160 arrfree( map.cells );
161 arrfree( map.io );
162 map.x = 0;
163 map.y = 0;
164 map.cells = NULL;
165 map.io = NULL;
166 }
167
168 static struct cell *map_tile( int pos[2] )
169 {
170 return map.cells + pos[1]*map.x + pos[0];
171 }
172
173 static struct cell *map_tile_at( int pos[2] )
174 {
175 if( pos[0] >= 0 && pos[0] < map.x && pos[1] >= 0 && pos[1] < map.y )
176 return map.cells + pos[1]*map.x + pos[0];
177 return NULL;
178 }
179
180 static struct cell *map_tile_at_cond( int pos[2], u32 flags )
181 {
182 struct cell *cell = map_tile_at( pos );
183 if( cell && (cell->flags & flags) )
184 return cell;
185
186 return NULL;
187 }
188
189 static void map_tile_coords_from_index( int i, int coords[2] )
190 {
191 coords[0] = i % map.x;
192 coords[1] = (i - coords[0])/map.x;
193 }
194
195 static void map_stack_refresh(void)
196 {
197 for( int i = 0; i < map.x*map.y; i ++ )
198 map.cells[i].flags &= ~CELL_FLAG_VISITED;
199 }
200
201 static void map_stack_init( int coords[2] )
202 {
203 map.stack.level = 0;
204 map.stack.frames[0].i = 0;
205 map.stack.frames[0].x = coords[0];
206 map.stack.frames[0].y = coords[1];
207 }
208
209 static struct cell *map_stack_next(void)
210 {
211 struct cell *tile = NULL;
212
213 while( !tile )
214 {
215 struct vframe *frame = &map.stack.frames[ map.stack.level ];
216
217 int output_dirs[][2] = { {0,1}, {-1,0}, {1,0} };
218
219 if( frame->i < 3 )
220 {
221 int *dir = output_dirs[ frame->i ];
222 tile = map_tile_at_cond( (int[2]){frame->x+dir[0], frame->y+dir[1]}, CELL_FLAG_WALKABLE );
223
224 if( tile && !(tile->flags & CELL_FLAG_VISITED) )
225 {
226 map.stack.level ++;
227 frame[1].i = 0;
228 frame[1].x = frame[0].x+dir[0];
229 frame[1].y = frame[0].y+dir[1];
230
231 tile->flags |= CELL_FLAG_VISITED;
232
233 if( frame->i == 0 )
234 frame->i = 400;
235 }
236 else
237 tile = NULL;
238
239 frame->i ++;
240 }
241 else
242 {
243 map.stack.level --;
244 tile = NULL;
245
246 if( map.stack.level < 0 )
247 return NULL;
248 }
249 }
250
251 return tile;
252 }
253
254 static void map_stack_current_coords( int coords[2], int offset )
255 {
256 coords[0] = map.stack.frames[ map.stack.level+offset ].x;
257 coords[1] = map.stack.frames[ map.stack.level+offset ].y;
258 }
259
260 static void map_update_visual(void)
261 {
262 u8 celldata[ 4096 ];
263
264 static int compute_flow_counter = 0;
265 compute_flow_counter ^= 0x1;
266
267 for( int i = 0; i < map.y*map.x; i ++ )
268 {
269 struct cell *cell = map.cells + i;
270
271 cell->flowing[compute_flow_counter] = cell->flowing[compute_flow_counter^0x1];
272
273 if( !map.cells[i].source_count )
274 cell->flowing[ compute_flow_counter ] = vg_max( cell->flowing[ compute_flow_counter ] - 10, 0 );
275
276 map.cells[i].source_count = 0;
277 }
278
279 for( int y = 0; y < map.y; y ++ )
280 {
281 for( int x = 0; x < map.x; x ++ )
282 {
283 struct cell *cur = map.cells + y*map.x + x;
284 u8 *cellbytes = celldata + (y*map.x+x)*4;
285
286 if( cur->flags & CELL_FLAG_WALKABLE )
287 {
288 struct cell *a, *b, *c, *d;
289
290 a = map_tile_at_cond( (int[2]){ x,y+1 }, CELL_FLAG_WALKABLE );
291 b = map_tile_at_cond( (int[2]){ x+1,y }, CELL_FLAG_WALKABLE );
292 c = map_tile_at_cond( (int[2]){ x,y-1 }, CELL_FLAG_WALKABLE );
293 d = map_tile_at_cond( (int[2]){ x-1,y }, CELL_FLAG_WALKABLE );
294
295 u32 config = (a?0x1:0x0) | (b?0x2:0x0) | (c?0x4:0x0) | (d?0x8:0x0);
296 cellbytes[ 0 ] = config;
297
298 if( cur->flags & CELL_FLAG_OUTPUT )
299 cur->flowing[ compute_flow_counter ] = 128;
300 }
301 else
302 {
303 // TODO: Random background tiles
304 cellbytes[ 0 ] = 1;
305 }
306 }
307 }
308
309 int const k_rate_flow = 16;
310
311 map_stack_refresh();
312 for( int i = 0; i < arrlen( map.io ); i ++ )
313 {
314 int inputcoord[2];
315 if( map.cells[ map.io[i] ].flags & CELL_FLAG_OUTPUT )
316 {
317 map_tile_coords_from_index( map.io[i], inputcoord );
318 map_stack_init( inputcoord );
319 struct cell *cell = map_tile_at( inputcoord );
320
321 int cr[2];
322
323 do
324 {
325 map_stack_current_coords( cr, 0 );
326
327 u8 *cellbytes = celldata + (cr[1]*map.x+cr[0])*4;
328
329 int outflow = cell->flowing[ compute_flow_counter ^ 0x1 ];
330 int sourcing = outflow? 1: 0;
331 int outrate = outflow == 128? k_rate_flow: 0;
332
333 struct cell *a, *b, *d;
334 a = map_tile( (int[2]){ cr[0], cr[1]+1 } );
335 b = map_tile( (int[2]){ cr[0]+1, cr[1] } );
336 d = map_tile( (int[2]){ cr[0]-1, cr[1] } );
337
338 int compute_l = 0, compute_r = 0;
339
340 switch( cellbytes[0] )
341 {
342 // DOWN
343 case 1: case 5: case 11: case 9: case 3:
344 a->flowing[ compute_flow_counter ] += outrate;
345 a->source_count += sourcing;
346 break;
347 case 2: case 6: compute_r = 1; break;
348 case 8: case 12: compute_l = 1; break;
349 case 10: case 14: compute_l = 1; compute_r = 1; break;
350 default: break;
351 }
352
353 if( compute_r && (celldata[ (cr[1]*map.x+cr[0]+1)*4 ] != 14) )
354 {
355 b->flowing[ compute_flow_counter ] += outrate;
356 b->source_count += sourcing;
357 }
358
359 if( compute_l && (celldata[ (cr[1]*map.x+cr[0]-1)*4 ] != 14) )
360 {
361 d->flowing[ compute_flow_counter ] += outrate;
362 d->source_count += sourcing;
363 }
364
365 if( cellbytes[0] == 10 )
366 {
367 int crl[2];
368 map_stack_current_coords( crl, -1 );
369
370 if( crl[0] < cr[0] )
371 {
372 cellbytes[0] = 15;
373 }
374 }
375 }
376 while( (cell = map_stack_next()) );
377 }
378 }
379
380 for( int i = 0; i < map.y*map.x; i ++ )
381 {
382 map.cells[i].flowing[ compute_flow_counter ] = vg_min( 128, map.cells[i].flowing[ compute_flow_counter ] );
383 celldata[ i*4+2 ] = map.cells[i].flowing[ compute_flow_counter ];
384 }
385
386 glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo );
387 glBufferSubData( GL_ARRAY_BUFFER, 16*sizeof(float) + 1024*2*sizeof(float), map.x*map.y*4, celldata );
388 }
389
390 static int map_load( const char *str )
391 {
392 map_free();
393
394 char *c = str;
395
396 // Scan for width
397 for(;; map.x ++)
398 {
399 if( str[map.x] == ';' )
400 break;
401 else if( !str[map.x] )
402 {
403 vg_error( "Unexpected EOF when parsing level!\n" );
404 return 0;
405 }
406 }
407
408 struct cell *row = arraddnptr( map.cells, map.x );
409 int cx = 0;
410 int reg_start = 0, reg_end = 0;
411
412 for(;;)
413 {
414 if( !*c )
415 break;
416
417 if( *c == ';' )
418 {
419 c ++;
420
421 // Parse attribs
422 if( *c != '\n' )
423 {
424 while( *c )
425 {
426 if( reg_start < reg_end )
427 {
428 if( *c >= 'a' && *c <= 'z' )
429 {
430 arrpush( map.cells[ map.io[ reg_start ] ].conditions, *c );
431 }
432 else
433 {
434 if( *c == ',' || *c == '\n' )
435 {
436 reg_start ++;
437
438 if( *c == '\n' )
439 break;
440 }
441 else
442 {
443 vg_error( "Unkown attrib '%c' (row: %u)\n", *c, map.y );
444 return 0;
445 }
446 }
447 }
448 else
449 {
450 vg_error( "Over-assigned values (row: %u)\n", map.y );
451 return 0;
452 }
453
454 c ++;
455 }
456 }
457
458 if( reg_start != reg_end )
459 {
460 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", map.y, reg_start, reg_end );
461 return 0;
462 }
463
464 if( cx != map.x )
465 {
466 vg_error( "Map row underflow (row: %u, %u<%u)\n", map.y, cx, map.x );
467 return 0;
468 }
469
470 row = arraddnptr( map.cells, map.x );
471 cx = 0;
472 map.y ++;
473 reg_end = reg_start = arrlen( map.io );
474 }
475 else
476 {
477 if( cx == map.x )
478 {
479 vg_error( "Map row overflow (row: %u, %u>%u)\n", map.y, cx, map.x );
480 return 0;
481 }
482
483 row[ cx ].conditions = NULL;
484 row[ cx ].flowing[ 0 ] = 0;
485 row[ cx ].source_count = 0;
486
487 // Parse the various cell types
488 if( *c == '+' || *c == '-' )
489 {
490 arrpush( map.io, cx + map.y*map.x );
491 row[ cx ++ ].flags = *c == '+'? CELL_FLAG_INPUT: CELL_FLAG_OUTPUT;
492 reg_end ++;
493 }
494 else if( *c == '#' )
495 {
496 row[ cx ++ ].flags = CELL_FLAG_WALL;
497 }
498 else
499 {
500 row[ cx ++ ].flags = 0x00;
501 }
502 }
503
504 c ++;
505 }
506
507 // Origin top left corner
508 map.origin[0] = -((float)map.x) * 0.5f;
509 map.origin[2] = -((float)map.y) * 0.5f;
510
511 float *offset_array = (float *)malloc( map.x*map.y*2*sizeof(float) );
512
513 for( int y = 0; y < map.y; y ++ )
514 {
515 for( int x = 0; x < map.x; x ++ )
516 {
517 float *coord = offset_array + (y*map.x+x)*2;
518 coord[0] = x;
519 coord[1] = y;
520 }
521 }
522
523 glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo );
524 glBufferSubData( GL_ARRAY_BUFFER, 16*sizeof(float), map.x*map.y*2*sizeof(float), offset_array );
525
526 free( offset_array );
527 vg_success( "Map loaded! (%u:%u)\n", map.x, map.y );
528 return 1;
529 }
530
531 static int map_tile_availible( int co[2] )
532 {
533 // Extract 5x5 grid surrounding tile
534 u32 blob = 0x1000;
535 for( int y = vg_max( co[1]-2, 0 ); y < vg_min( map.y, co[1]+3 ); y ++ )
536 for( int x = vg_max( co[0]-2, 0 ); x < vg_min( map.x, co[0]+3 ); x ++ )
537 {
538 struct cell *cell = map_tile( (int[2]){ x, y } );
539
540 if( cell && (cell->flags & CELL_FLAG_WALKABLE) )
541 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
542 }
543
544 // Run filter over center 3x3 grid to check for invalid configurations
545 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
546 for( int i = 0; i < vg_list_size(kernel); i ++ )
547 {
548 if( blob & (0x1 << (6+kernel[i])) )
549 {
550 // (reference window: 0x1CE7) Illegal moves
551 // 0100011100010 ;
552 // 0000001100011 ;
553 // 0000011000110 ;
554 // 0110001100000 ;
555 // 1100011000000 ;
556 // 0100001100010 ;
557 // 0100011000010 ;
558
559 u32 invalid[] = { 0x8E2, 0x63, 0xC6, 0xC60, 0x18C0, 0x862, 0x8C2 };
560 u32 window = blob >> kernel[i];
561
562 for( int j = 0; j < vg_list_size(invalid); j ++ )
563 if((window & invalid[j]) == invalid[j])
564 return 0;
565 }
566 }
567
568 return 1;
569 }
570
571 void vg_update(void)
572 {
573 // Update camera
574 float ratio = (float)vg_window_y / (float)vg_window_x;
575 float const size = 7.5f;
576 glm_ortho( -size, size, -size*ratio, size*ratio, 0.01f, 150.f, m_projection );
577
578 glm_mat4_identity( m_view );
579 glm_translate_z( m_view, -10.f );
580 glm_rotate_x( m_view, 1.5708f, m_view );
581
582 glm_mat4_mul( m_projection, m_view, vg_pv );
583
584 // Compute map update
585 for( int y = 0; y < map.y; y ++ )
586 {
587 for( int x = 0; x < map.x; x ++ )
588 {
589 struct cell *tile, *upper, *lower, *l, *r;
590 tile = map_tile_at( (int [2]){ x, y } );
591 tile->flags &= ~(CELL_FLAG_SPLIT|CELL_FLAG_MERGE|CELL_FLAG_UPLVL);
592
593 if( tile->flags & CELL_FLAG_WALKABLE )
594 {
595 r = map_tile_at_cond( (int[2]){ x+1, y }, CELL_FLAG_WALKABLE );
596 l = map_tile_at_cond( (int[2]){ x-1, y }, CELL_FLAG_WALKABLE );
597
598 if( r && l )
599 {
600 upper = map_tile_at_cond( (int[2]){ x, y-1 }, CELL_FLAG_WALKABLE );
601 lower = map_tile_at_cond( (int[2]){ x, y+1 }, CELL_FLAG_WALKABLE );
602
603 if( upper )
604 {
605 tile->flags |= CELL_FLAG_MERGE | CELL_FLAG_UPLVL;
606 }
607
608 if( lower )
609 {
610 tile->flags |= CELL_FLAG_SPLIT;
611 l->flags |= CELL_FLAG_UPLVL;
612 r->flags |= CELL_FLAG_UPLVL;
613 }
614 }
615 }
616 }
617 }
618
619 // Compute classification
620 /*
621 map_stack_refresh();
622
623 for( int i = 0; i < arrlen( map.io ); i ++ )
624 {
625 struct *cell cell = &map.cells[ map.io ];
626 int coords[2];
627
628 if( cell->flags & CELL_FLAG_INPUT )
629 {
630 map_tile_coords_from_index( map.io, coords );
631 map_stack_init( coords );
632
633 do
634 {
635 if( cell->flags & CELL_FLAG_CONNECTOR )
636 {
637
638 }
639 }
640 while( (cell = map_stack_next()) );
641 }
642 }*/
643
644 // Get mouse ray
645 vec3 ray_origin;
646 vec3 ray_dir;
647
648 mat4 pv_inverse;
649 vec4 vp = { 0.f, 0.f, vg_window_x, vg_window_y };
650 glm_mat4_inv( vg_pv, pv_inverse );
651 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, -1.f }, pv_inverse, vp, ray_dir );
652 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, 0.f }, pv_inverse, vp, ray_origin );
653 glm_vec3_sub( ray_dir, ray_origin, ray_dir );
654
655 // Get floor tile intersection
656 float ray_t = -ray_origin[1] / ray_dir[1];
657
658 vec3 tile_pos;
659 glm_vec3_copy( ray_origin, tile_pos );
660 glm_vec3_muladds( ray_dir, ray_t, tile_pos );
661 glm_vec3_sub( tile_pos, map.origin, tile_pos );
662
663 int tile_x = floorf( tile_pos[0] );
664 int tile_y = floorf( tile_pos[2] );
665
666 map.selected = map_tile_at( (int [2]){tile_x, tile_y} );
667
668 if( map.playing )
669 {
670 static int fish_counter = 0;
671 fish_counter ++;
672
673 if( fish_counter > 20 )
674 {
675 fish_counter = 0;
676
677 // Advance characters
678 for( int i = 0; i < map.num_fishes; i ++ )
679 {
680 struct fish *fish = map.fishes + i;
681
682 if( !fish->alive )
683 continue;
684
685 struct cell *tile, *next;
686 tile = map_tile_at( fish->co );
687
688 if( tile->flags & CELL_FLAG_OUTPUT )
689 {
690 vg_info( "Fish got zucced (%d)\n", i );
691 fish->alive = 0;
692 continue;
693 }
694
695 int die = 0;
696 if( tile->flags & CELL_FLAG_SPLIT )
697 {
698 die = 1;
699 int new_dir[][2] = { {0,-1},{1,0},{-1,0} };
700 int *test_dir;
701
702 for( int j = 0; j < 3; j ++ )
703 {
704 test_dir = new_dir[ tile->state ];
705 tile->state = (tile->state+1)%3;
706
707 next = map_tile_at( (int[2]){ fish->co[0]+test_dir[0], fish->co[1]+test_dir[1] } );
708 if( next && (next->flags & (CELL_FLAG_WALKABLE)) )
709 {
710 fish->dir[0] = test_dir[0];
711 fish->dir[1] = test_dir[1];
712 die = 0;
713 break;
714 }
715 }
716 }
717
718 next = map_tile_at( (int[2]){ fish->co[0]+fish->dir[0], fish->co[1]+fish->dir[1] } );
719 if( !next || (next && !(next->flags & CELL_FLAG_WALKABLE)) )
720 {
721 // Try UP
722 die = 1;
723 }
724
725 if( die )
726 {
727 vg_info( "Fish died! (%d)\n", i );
728 fish->alive = 0;
729 continue;
730 }
731
732
733 fish->co[0] += fish->dir[0];
734 fish->co[1] += fish->dir[1];
735 }
736
737 // Try spawn fish
738 for( int i = 0; i < arrlen( map.io ); i ++ )
739 {
740 struct cell *input = &map.cells[ map.io[i] ];
741
742 if( input->flags & CELL_FLAG_INPUT )
743 {
744 if( input->state < arrlen( input->conditions ) )
745 {
746 struct fish *fish = &map.fishes[ map.num_fishes ];
747 map_tile_coords_from_index( map.io[i], fish->co );
748
749 int output_dirs[][2] = { {0,-1}, {-1,0}, {1,0} };
750 int can_spawn = 0;
751
752 for( int i = 0; i < vg_list_size( output_dirs ); i ++ )
753 {
754 int *dir = output_dirs[i];
755 struct cell *next = map_tile_at( (int[2]){ fish->co[0]+dir[0], fish->co[1]+dir[1] } );
756 if( next && next->flags & CELL_FLAG_CANAL )
757 {
758 fish->dir[0] = dir[0];
759 fish->dir[1] = dir[1];
760 can_spawn = 1;
761 }
762 }
763
764 if( can_spawn )
765 {
766 fish->alive = 1;
767 input->state ++;
768 map.num_fishes ++;
769 }
770 }
771 }
772 }
773
774 vg_info( "There are now %u active fish\n", map.num_fishes );
775 }
776
777 if( vg_get_button_down( "go" ) )
778 {
779 map.playing = 0;
780 map.num_fishes = 0;
781
782 vg_info( "Ending!\n" );
783 }
784 }
785 else
786 {
787 if( vg_get_button_down( "go" ) )
788 {
789 map.playing = 1;
790
791 // Reset everything
792 for( int i = 0; i < map.x*map.y; i ++ )
793 map.cells[ i ].state = 0;
794
795 vg_info( "Starting!\n" );
796 }
797
798 if( map.selected )
799 {
800 map.select_valid = map_tile_availible( (int[2]){ tile_x, tile_y } );
801
802 if( map.select_valid )
803 {
804 if( vg_get_button_down("primary") )
805 {
806 if( map.selected->flags & CELL_FLAG_CANAL )
807 {
808 map.selected->flags &= ~(CELL_FLAG_CANAL);
809 }
810 else
811 {
812 map.selected->flags |= CELL_FLAG_CANAL;
813 }
814 }
815 }
816 }
817 }
818 }
819
820 GLuint tile_vao;
821 GLuint tile_vbo;
822
823 void vg_render(void)
824 {
825 glViewport( 0,0, vg_window_x, vg_window_y );
826
827 //glEnable( GL_DEPTH_TEST );
828 glClearColor( 0.94f, 0.94f, 0.94f, 1.0f );
829 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
830
831 /*
832 glBindVertexArray( tile_vao );
833
834 SHADER_USE( colour_shader );
835 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
836
837 for( int y = 0; y < map.y; y ++ )
838 {
839 for( int x = 0; x < map.x; x ++ )
840 {
841 glm_mat4_identity( m_mdl );
842 glm_translate( m_mdl,
843 (vec3){
844 map.origin[0] + (float)x,
845 0.f,
846 map.origin[2] + (float)y
847 }
848 );
849 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
850
851 struct cell *cell = &map.cells[ y*map.x+x ];
852
853 vec4 colour = { 0.7f, 0.7f, 0.7f, 1.f };
854
855 if( cell->flags & CELL_FLAG_INPUT ) glm_vec3_copy( (vec3){ 0.9f,0.5f,0.5f }, colour );
856 else if( cell->flags & CELL_FLAG_OUTPUT ) glm_vec3_copy( (vec3){ 0.5f,0.9f,0.5f }, colour );
857 else if( cell->flags & CELL_FLAG_WALL ) glm_vec3_copy( (vec3){ 0.1f,0.1f,0.1f }, colour );
858 else if( cell->flags & CELL_FLAG_CANAL ) glm_vec3_copy( (vec3){ 0.5f,0.5f,0.8f }, colour );
859
860 if( cell->flags & CELL_FLAG_SPLIT )
861 glm_vec3_copy( (vec3){ 0.6f, 0.f, 0.9f }, colour );
862 else if( cell->flags & CELL_FLAG_MERGE )
863 glm_vec3_copy( (vec3){ 0.f, 0.6f, 0.8f }, colour );
864
865 if( map.selected == cell )
866 {
867 if( !map.select_valid )
868 glm_vec3_copy( (vec3){ 1.f, 0.f, 0.f }, colour );
869
870 float flash = sinf( vg_time*2.5f ) * 0.25f + 0.75f;
871 glm_vec3_scale( colour, flash, colour );
872 }
873
874 glUniform4fv( SHADER_UNIFORM( colour_shader, "uColour" ), 1, colour );
875 glDrawArrays( GL_TRIANGLES, 0, 6 );
876 }
877 }
878
879 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 1.f, 0.f, 1.f, 1.f );
880
881 for( int i = 0; i < map.num_fishes; i ++ )
882 {
883 struct fish *fish = map.fishes + i;
884
885 if( fish->alive )
886 {
887 glm_mat4_identity( m_mdl );
888 glm_translate( m_mdl,
889 (vec3){
890 map.origin[0] + (float)fish->co[0] + 0.5f,
891 0.1f,
892 map.origin[2] + (float)fish->co[1] + 0.5f
893 }
894 );
895 glm_scale_uni( m_mdl, 0.2f );
896 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
897 glDrawArrays( GL_TRIANGLES, 0, 6 );
898 }
899 }
900 */
901
902 glBindVertexArray( map.tiles_vao );
903
904 map_update_visual();
905
906 SHADER_USE( tilemap_shader );
907 glUniformMatrix4fv( SHADER_UNIFORM( tilemap_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
908
909 glUniform1i( SHADER_UNIFORM( tilemap_shader, "uTexTiles" ), 0 );
910 glActiveTexture( GL_TEXTURE0 );
911 glBindTexture( GL_TEXTURE_2D, map.tile_texture );
912
913 glUniform1i( SHADER_UNIFORM( tilemap_shader, "uTexRipples" ), 1 );
914 glActiveTexture( GL_TEXTURE1 );
915 glBindTexture( GL_TEXTURE_2D, map.flow_texture );
916
917 glUniform2f( SHADER_UNIFORM( tilemap_shader, "uOrigin" ), map.origin[0], map.origin[2] );
918 glUniform1f( SHADER_UNIFORM( tilemap_shader, "uTime" ), vg_time * 0.5f );
919
920 glDrawArraysInstanced( GL_TRIANGLES, 0, 6, map.x*map.y );
921 }
922
923 void vg_register(void)
924 {
925 SHADER_INIT( colour_shader );
926 SHADER_INIT( tilemap_shader );
927 }
928
929 void vg_start(void)
930 {
931 glGenVertexArrays( 1, &tile_vao );
932 glGenBuffers( 1, &tile_vbo );
933
934 float quad_mesh[] =
935 {
936 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
937 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
938
939 // Padding
940 0.0f, 0.0f, 0.0f, 0.0f
941 };
942
943 glBindVertexArray( tile_vao );
944 glBindBuffer( GL_ARRAY_BUFFER, tile_vbo );
945 glBufferData
946 (
947 GL_ARRAY_BUFFER,
948 sizeof( quad_mesh ),
949 quad_mesh,
950 GL_STATIC_DRAW
951 );
952
953 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
954 glEnableVertexAttribArray( 0 );
955
956 VG_CHECK_GL();
957
958 // Create map buffers
959 glGenVertexArrays( 1, &map.tiles_vao );
960 glGenBuffers( 1, &map.tiles_vbo );
961
962 glBindVertexArray( map.tiles_vao );
963 glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo );
964 glBufferData( GL_ARRAY_BUFFER,
965 sizeof( quad_mesh ) +
966 sizeof( float )*2 * 1024 +
967 sizeof( u8 )*4 * 1024,
968 NULL,
969 GL_DYNAMIC_DRAW
970 );
971
972 glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof( quad_mesh ), quad_mesh );
973
974 // Base quad
975 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)0 );
976 glEnableVertexAttribArray( 0 );
977
978 // Offset, data arrays (instancing)
979 glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)(sizeof(quad_mesh)) );
980 glEnableVertexAttribArray( 1 );
981 glVertexAttribDivisor( 1, 1 );
982
983 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_FALSE, 4, (void*)(sizeof(quad_mesh)+sizeof(float)*2*1024) );
984 glEnableVertexAttribArray( 2 );
985 glVertexAttribDivisor( 2, 1 );
986
987 map.tile_texture = vg_tex2d_rgba( "textures/rivertiles_flowm.tga" );
988 vg_tex2d_nearest();
989 vg_tex2d_repeat();
990
991 map.flow_texture = vg_tex2d_rgba( "textures/rivertiles_ripple.tga" );
992 vg_tex2d_nearest();
993 vg_tex2d_repeat();
994
995 map_load
996 (
997 "##-#####-##;aaa,aa\n"
998 "# #;\n"
999 "# #;\n"
1000 "# #;\n"
1001 "# #;\n"
1002 "# #;\n"
1003 "# #;\n"
1004 "##+#####+##;aa,aaa\n"
1005 );
1006 }
1007
1008 void vg_free(void)
1009 {
1010 map_free();
1011
1012 glDeleteVertexArrays( 1, &tile_vao );
1013 glDeleteVertexArrays( 1, &map.tiles_vao );
1014
1015 glDeleteBuffers( 1, &tile_vbo );
1016 glDeleteBuffers( 1, &map.tiles_vbo );
1017
1018 glDeleteTextures( 1, &map.tile_texture );
1019 glDeleteTextures( 1, &map.flow_texture );
1020 }
1021
1022 void vg_ui(void)
1023 {
1024
1025 }