1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
6 #define CELL_SHEET_X 256
7 #define CELL_SHEET_Y 256
9 SHADER_DEFINE( colour_shader
,
12 "layout (location=0) in vec2 a_co;"
18 " vec4 vert_pos = uPv * uMdl * vec4( a_co.x, 0.0, a_co.y, 1.0 );"
19 " gl_Position = vert_pos;"
24 "uniform vec4 uColour;"
28 " FragColor = uColour;"
31 UNIFORMS({ "uPv", "uMdl", "uColour" })
34 SHADER_DEFINE( tilemap_shader
,
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
41 "uniform vec2 uOrigin;"
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 = (a_co*0.98+0.01 + a_data.xy) * 0.125;"
53 "uniform sampler2D uTexTiles;"
60 "vec4 glyph = texture( uTexTiles, aCoords );"
61 "FragColor = vec4( glyph.xyz, 1.0 );"
64 UNIFORMS({ "uPv", "uTexTiles", "uOrigin" })
71 int main( int argc
, char *argv
[] )
73 vg_init( argc
, argv
, "FishLadder" );
76 #define CELL_FLAG_INPUT 0x1
77 #define CELL_FLAG_OUTPUT 0x2
78 #define CELL_FLAG_IO (CELL_FLAG_INPUT|CELL_FLAG_OUTPUT)
79 #define CELL_FLAG_WALL 0x4
80 #define CELL_FLAG_HOVER 0x8
81 #define CELL_FLAG_ITER 0x10
82 #define CELL_FLAG_CANAL 0x20
83 #define CELL_FLAG_SPLIT 0x40 /* Does this cell split and have an incoming vertical connection? */
84 #define CELL_FLAG_WALKABLE (CELL_FLAG_IO|CELL_FLAG_CANAL)
85 #define CELL_FLAG_VISITED 0x80
86 #define CELL_FLAG_UPLVL 0x100
87 #define CELL_FLAG_MERGE 0x200
116 struct cell
*selected
;
145 static void map_free(void)
147 for( int i
= 0; i
< arrlen( map
.io
); i
++ )
149 arrfree( map
.cells
[ map
.io
[i
] ].conditions
);
152 arrfree( map
.cells
);
160 static struct cell
*map_tile_at( int pos
[2] )
162 if( pos
[0] >= 0 && pos
[0] < map
.x
&& pos
[1] >= 0 && pos
[1] < map
.y
)
163 return map
.cells
+ pos
[1]*map
.x
+ pos
[0];
167 static struct cell
*map_tile_at_cond( int pos
[2], u32 flags
)
169 struct cell
*cell
= map_tile_at( pos
);
170 if( cell
&& (cell
->flags
& flags
) )
176 static void map_tile_coords_from_index( int i
, int coords
[2] )
178 coords
[0] = i
% map
.x
;
179 coords
[1] = (i
- coords
[0])/map
.x
;
182 static void map_stack_refresh(void)
184 for( int i
= 0; i
< map
.x
*map
.y
; i
++ )
185 map
.cells
[i
].flags
&= ~CELL_FLAG_VISITED
;
188 static void map_stack_init( int coords
[2] )
191 map
.stack
.frames
[0].i
= 0;
192 map
.stack
.frames
[0].x
= coords
[0];
193 map
.stack
.frames
[0].y
= coords
[1];
196 static struct cell
*map_stack_next(void)
198 struct cell
*tile
= NULL
;
202 struct vframe
*frame
= &map
.stack
.frames
[ map
.stack
.level
];
204 int output_dirs
[][2] = { {0,-1}, {-1,0}, {1,0} };
208 int *dir
= output_dirs
[ frame
->i
];
209 tile
= map_tile_at( (int[2]){frame
->x
+dir
[0], frame
->y
+dir
[1]} );
212 if( tile
&& !(tile
->flags
& CELL_FLAG_VISITED
) )
216 frame
[1].x
= frame
[0].x
+dir
[0];
217 frame
[1].y
= frame
[0].y
+dir
[1];
227 if( map
.stack
.level
< 0 )
235 static void map_update_visual(void)
239 for( int i
= 0; i
< map
.x
*map
.y
; i
++ )
241 celldata
[i
*4+0] = i
& 0x7;
242 celldata
[i
*4+1] = (i
& ~0x7) >> 3;
245 glBindBuffer( GL_ARRAY_BUFFER
, map
.tiles_vbo
);
246 glBufferSubData( GL_ARRAY_BUFFER
, 16*sizeof(float) + 1024*2*sizeof(float), map
.x
*map
.y
*4, celldata
);
249 static int map_load( const char *str
)
258 if( str
[map
.x
] == ';' )
260 else if( !str
[map
.x
] )
262 vg_error( "Unexpected EOF when parsing level!\n" );
267 struct cell
*row
= arraddnptr( map
.cells
, map
.x
);
269 int reg_start
= 0, reg_end
= 0;
285 if( reg_start
< reg_end
)
287 if( *c
>= 'a' && *c
<= 'z' )
289 arrpush( map
.cells
[ map
.io
[ reg_start
] ].conditions
, *c
);
293 if( *c
== ',' || *c
== '\n' )
302 vg_error( "Unkown attrib '%c' (row: %u)\n", *c
, map
.y
);
309 vg_error( "Over-assigned values (row: %u)\n", map
.y
);
317 if( reg_start
!= reg_end
)
319 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", map
.y
, reg_start
, reg_end
);
325 vg_error( "Map row underflow (row: %u, %u<%u)\n", map
.y
, cx
, map
.x
);
329 row
= arraddnptr( map
.cells
, map
.x
);
332 reg_end
= reg_start
= arrlen( map
.io
);
338 vg_error( "Map row overflow (row: %u, %u>%u)\n", map
.y
, cx
, map
.x
);
342 row
[ cx
].conditions
= NULL
;
344 // Parse the various cell types
345 if( *c
== '+' || *c
== '-' )
347 arrpush( map
.io
, cx
+ map
.y
*map
.x
);
348 row
[ cx
++ ].flags
= *c
== '+'? CELL_FLAG_INPUT
: CELL_FLAG_OUTPUT
;
353 row
[ cx
++ ].flags
= CELL_FLAG_WALL
;
357 row
[ cx
++ ].flags
= 0x00;
364 // Origin top left corner
365 map
.origin
[0] = -((float)map
.x
) * 0.5f
;
366 map
.origin
[2] = -((float)map
.y
) * 0.5f
;
368 float *offset_array
= (float *)malloc( map
.x
*map
.y
*2*sizeof(float) );
370 for( int y
= 0; y
< map
.y
; y
++ )
372 for( int x
= 0; x
< map
.x
; x
++ )
374 float *coord
= offset_array
+ (y
*map
.x
+x
)*2;
380 glBindBuffer( GL_ARRAY_BUFFER
, map
.tiles_vbo
);
381 glBufferSubData( GL_ARRAY_BUFFER
, 16*sizeof(float), map
.x
*map
.y
*2*sizeof(float), offset_array
);
383 free( offset_array
);
384 vg_success( "Map loaded! (%u:%u)\n", map
.x
, map
.y
);
388 static int map_tile_availible( int co
[2] )
390 // Extract 5x5 grid surrounding tile
392 for( int y
= vg_max( co
[1]-2, 0 ); y
< vg_min( map
.y
, co
[1]+3 ); y
++ )
393 for( int x
= vg_max( co
[0]-2, 0 ); x
< vg_min( map
.x
, co
[0]+3 ); x
++ )
395 struct cell
*cell
= map_tile_at( (int[2]){ x
, y
} );
397 if( cell
&& (cell
->flags
& CELL_FLAG_WALKABLE
) )
398 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
401 // Run filter over center 3x3 grid to check for invalid configurations
402 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
403 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
405 if( blob
& (0x1 << (6+kernel
[i
])) )
407 // (reference window: 0x1CE7) Illegal moves
416 u32 invalid
[] = { 0x8E2, 0x63, 0xC6, 0xC60, 0x18C0, 0x862, 0x8C2 };
417 u32 window
= blob
>> kernel
[i
];
419 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
420 if((window
& invalid
[j
]) == invalid
[j
])
431 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
432 float const size
= 7.5f
;
433 glm_ortho( -size
, size
, -size
*ratio
, size
*ratio
, 0.01f
, 150.f
, m_projection
);
435 glm_mat4_identity( m_view
);
436 glm_translate_z( m_view
, -10.f
);
437 glm_rotate_x( m_view
, 1.5708f
, m_view
);
439 glm_mat4_mul( m_projection
, m_view
, vg_pv
);
441 // Compute map update
442 for( int y
= 0; y
< map
.y
; y
++ )
444 for( int x
= 0; x
< map
.x
; x
++ )
446 struct cell
*tile
, *upper
, *lower
, *l
, *r
;
447 tile
= map_tile_at( (int [2]){ x
, y
} );
448 tile
->flags
&= ~(CELL_FLAG_SPLIT
|CELL_FLAG_MERGE
|CELL_FLAG_UPLVL
);
450 if( tile
->flags
& CELL_FLAG_WALKABLE
)
452 r
= map_tile_at_cond( (int[2]){ x
+1, y
}, CELL_FLAG_WALKABLE
);
453 l
= map_tile_at_cond( (int[2]){ x
-1, y
}, CELL_FLAG_WALKABLE
);
457 upper
= map_tile_at_cond( (int[2]){ x
, y
-1 }, CELL_FLAG_WALKABLE
);
458 lower
= map_tile_at_cond( (int[2]){ x
, y
+1 }, CELL_FLAG_WALKABLE
);
462 tile
->flags
|= CELL_FLAG_MERGE
| CELL_FLAG_UPLVL
;
467 tile
->flags
|= CELL_FLAG_SPLIT
;
468 l
->flags
|= CELL_FLAG_UPLVL
;
469 r
->flags
|= CELL_FLAG_UPLVL
;
476 // Compute classification
480 for( int i = 0; i < arrlen( map.io ); i ++ )
482 struct *cell cell = &map.cells[ map.io ];
485 if( cell->flags & CELL_FLAG_INPUT )
487 map_tile_coords_from_index( map.io, coords );
488 map_stack_init( coords );
492 if( cell->flags & CELL_FLAG_CONNECTOR )
497 while( (cell = map_stack_next()) );
506 vec4 vp
= { 0.f
, 0.f
, vg_window_x
, vg_window_y
};
507 glm_mat4_inv( vg_pv
, pv_inverse
);
508 glm_unprojecti( (vec3
){ vg_mouse_x
, vg_window_y
-vg_mouse_y
, -1.f
}, pv_inverse
, vp
, ray_dir
);
509 glm_unprojecti( (vec3
){ vg_mouse_x
, vg_window_y
-vg_mouse_y
, 0.f
}, pv_inverse
, vp
, ray_origin
);
510 glm_vec3_sub( ray_dir
, ray_origin
, ray_dir
);
512 // Get floor tile intersection
513 float ray_t
= -ray_origin
[1] / ray_dir
[1];
516 glm_vec3_copy( ray_origin
, tile_pos
);
517 glm_vec3_muladds( ray_dir
, ray_t
, tile_pos
);
518 glm_vec3_sub( tile_pos
, map
.origin
, tile_pos
);
520 int tile_x
= floorf( tile_pos
[0] );
521 int tile_y
= floorf( tile_pos
[2] );
523 map
.selected
= map_tile_at( (int [2]){tile_x
, tile_y
} );
527 static int fish_counter
= 0;
530 if( fish_counter
> 20 )
534 // Advance characters
535 for( int i
= 0; i
< map
.num_fishes
; i
++ )
537 struct fish
*fish
= map
.fishes
+ i
;
542 struct cell
*tile
, *next
;
543 tile
= map_tile_at( fish
->co
);
545 if( tile
->flags
& CELL_FLAG_OUTPUT
)
547 vg_info( "Fish got zucced (%d)\n", i
);
553 if( tile
->flags
& CELL_FLAG_SPLIT
)
556 int new_dir
[][2] = { {0,-1},{1,0},{-1,0} };
559 for( int j
= 0; j
< 3; j
++ )
561 test_dir
= new_dir
[ tile
->state
];
562 tile
->state
= (tile
->state
+1)%3;
564 next
= map_tile_at( (int[2]){ fish
->co
[0]+test_dir
[0], fish
->co
[1]+test_dir
[1] } );
565 if( next
&& (next
->flags
& (CELL_FLAG_WALKABLE
)) )
567 fish
->dir
[0] = test_dir
[0];
568 fish
->dir
[1] = test_dir
[1];
575 next
= map_tile_at( (int[2]){ fish
->co
[0]+fish
->dir
[0], fish
->co
[1]+fish
->dir
[1] } );
576 if( !next
|| (next
&& !(next
->flags
& CELL_FLAG_WALKABLE
)) )
584 vg_info( "Fish died! (%d)\n", i
);
590 fish
->co
[0] += fish
->dir
[0];
591 fish
->co
[1] += fish
->dir
[1];
595 for( int i
= 0; i
< arrlen( map
.io
); i
++ )
597 struct cell
*input
= &map
.cells
[ map
.io
[i
] ];
599 if( input
->flags
& CELL_FLAG_INPUT
)
601 if( input
->state
< arrlen( input
->conditions
) )
603 struct fish
*fish
= &map
.fishes
[ map
.num_fishes
];
604 map_tile_coords_from_index( map
.io
[i
], fish
->co
);
606 int output_dirs
[][2] = { {0,-1}, {-1,0}, {1,0} };
609 for( int i
= 0; i
< vg_list_size( output_dirs
); i
++ )
611 int *dir
= output_dirs
[i
];
612 struct cell
*next
= map_tile_at( (int[2]){ fish
->co
[0]+dir
[0], fish
->co
[1]+dir
[1] } );
613 if( next
&& next
->flags
& CELL_FLAG_CANAL
)
615 fish
->dir
[0] = dir
[0];
616 fish
->dir
[1] = dir
[1];
631 vg_info( "There are now %u active fish\n", map
.num_fishes
);
634 if( vg_get_button_down( "go" ) )
639 vg_info( "Ending!\n" );
644 if( vg_get_button_down( "go" ) )
649 for( int i
= 0; i
< map
.x
*map
.y
; i
++ )
650 map
.cells
[ i
].state
= 0;
652 vg_info( "Starting!\n" );
657 map
.select_valid
= map_tile_availible( (int[2]){ tile_x
, tile_y
} );
659 if( map
.select_valid
)
661 if( vg_get_button_down("primary") )
663 if( map
.selected
->flags
& CELL_FLAG_CANAL
)
665 map
.selected
->flags
&= ~(CELL_FLAG_CANAL
);
669 map
.selected
->flags
|= CELL_FLAG_CANAL
;
682 glViewport( 0,0, vg_window_x
, vg_window_y
);
684 //glEnable( GL_DEPTH_TEST );
685 glClearColor( 0.94f
, 0.94f
, 0.94f
, 1.0f
);
686 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
689 glBindVertexArray( tile_vao );
691 SHADER_USE( colour_shader );
692 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
694 for( int y = 0; y < map.y; y ++ )
696 for( int x = 0; x < map.x; x ++ )
698 glm_mat4_identity( m_mdl );
699 glm_translate( m_mdl,
701 map.origin[0] + (float)x,
703 map.origin[2] + (float)y
706 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
708 struct cell *cell = &map.cells[ y*map.x+x ];
710 vec4 colour = { 0.7f, 0.7f, 0.7f, 1.f };
712 if( cell->flags & CELL_FLAG_INPUT ) glm_vec3_copy( (vec3){ 0.9f,0.5f,0.5f }, colour );
713 else if( cell->flags & CELL_FLAG_OUTPUT ) glm_vec3_copy( (vec3){ 0.5f,0.9f,0.5f }, colour );
714 else if( cell->flags & CELL_FLAG_WALL ) glm_vec3_copy( (vec3){ 0.1f,0.1f,0.1f }, colour );
715 else if( cell->flags & CELL_FLAG_CANAL ) glm_vec3_copy( (vec3){ 0.5f,0.5f,0.8f }, colour );
717 if( cell->flags & CELL_FLAG_SPLIT )
718 glm_vec3_copy( (vec3){ 0.6f, 0.f, 0.9f }, colour );
719 else if( cell->flags & CELL_FLAG_MERGE )
720 glm_vec3_copy( (vec3){ 0.f, 0.6f, 0.8f }, colour );
722 if( map.selected == cell )
724 if( !map.select_valid )
725 glm_vec3_copy( (vec3){ 1.f, 0.f, 0.f }, colour );
727 float flash = sinf( vg_time*2.5f ) * 0.25f + 0.75f;
728 glm_vec3_scale( colour, flash, colour );
731 glUniform4fv( SHADER_UNIFORM( colour_shader, "uColour" ), 1, colour );
732 glDrawArrays( GL_TRIANGLES, 0, 6 );
736 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 1.f, 0.f, 1.f, 1.f );
738 for( int i = 0; i < map.num_fishes; i ++ )
740 struct fish *fish = map.fishes + i;
744 glm_mat4_identity( m_mdl );
745 glm_translate( m_mdl,
747 map.origin[0] + (float)fish->co[0] + 0.5f,
749 map.origin[2] + (float)fish->co[1] + 0.5f
752 glm_scale_uni( m_mdl, 0.2f );
753 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
754 glDrawArrays( GL_TRIANGLES, 0, 6 );
759 glBindVertexArray( map
.tiles_vao
);
763 SHADER_USE( tilemap_shader
);
764 glUniformMatrix4fv( SHADER_UNIFORM( tilemap_shader
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
766 glUniform1i( SHADER_UNIFORM( tilemap_shader
, "uTexTiles" ), 0 );
767 glActiveTexture( GL_TEXTURE0
);
768 glBindTexture( GL_TEXTURE_2D
, map
.tile_texture
);
770 glUniform2f( SHADER_UNIFORM( tilemap_shader
, "uOrigin" ), map
.origin
[0], map
.origin
[2] );
772 glDrawArraysInstanced( GL_TRIANGLES
, 0, 6, map
.x
*map
.y
);
775 void vg_register(void)
777 SHADER_INIT( colour_shader
);
778 SHADER_INIT( tilemap_shader
);
783 glGenVertexArrays( 1, &tile_vao
);
784 glGenBuffers( 1, &tile_vbo
);
788 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
789 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
792 0.0f
, 0.0f
, 0.0f
, 0.0f
795 glBindVertexArray( tile_vao
);
796 glBindBuffer( GL_ARRAY_BUFFER
, tile_vbo
);
805 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
806 glEnableVertexAttribArray( 0 );
810 // Create map buffers
811 glGenVertexArrays( 1, &map
.tiles_vao
);
812 glGenBuffers( 1, &map
.tiles_vbo
);
814 glBindVertexArray( map
.tiles_vao
);
815 glBindBuffer( GL_ARRAY_BUFFER
, map
.tiles_vbo
);
816 glBufferData( GL_ARRAY_BUFFER
,
817 sizeof( quad_mesh
) +
818 sizeof( float )*2 * 1024 +
819 sizeof( u8
)*4 * 1024,
824 glBufferSubData( GL_ARRAY_BUFFER
, 0, sizeof( quad_mesh
), quad_mesh
);
827 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)0 );
828 glEnableVertexAttribArray( 0 );
830 // Offset, data arrays (instancing)
831 glVertexAttribPointer( 1, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)(sizeof(quad_mesh
)) );
832 glEnableVertexAttribArray( 1 );
833 glVertexAttribDivisor( 1, 1 );
835 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_FALSE
, 4, (void*)(sizeof(quad_mesh
)+sizeof(float)*2*1024) );
836 glEnableVertexAttribArray( 2 );
837 glVertexAttribDivisor( 2, 1 );
839 map
.tile_texture
= vg_tex2d_rgba( "textures/rivertiles_flowm.tga" );
843 map
.flow_texture
= vg_tex2d_rgba( "textures/rivertiles_ripple.tga" );
862 glDeleteVertexArrays( 1, &tile_vao
);
863 glDeleteVertexArrays( 1, &map
.tiles_vao
);
865 glDeleteBuffers( 1, &tile_vbo
);
866 glDeleteBuffers( 1, &map
.tiles_vbo
);
868 glDeleteTextures( 1, &map
.tile_texture
);
869 glDeleteTextures( 1, &map
.flow_texture
);