From 49a2d3825e61f53012816135ac4c4278c0b27632 Mon Sep 17 00:00:00 2001 From: hgn Date: Tue, 20 Jul 2021 13:26:26 +0100 Subject: [PATCH] basic selection code --- fishladder.c | 94 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 14 deletions(-) diff --git a/fishladder.c b/fishladder.c index e2d5ec1..f6673f7 100644 --- a/fishladder.c +++ b/fishladder.c @@ -42,6 +42,9 @@ int main( int argc, char *argv[] ) #define CELL_FLAG_OUTPUT 0x2 #define CELL_FLAG_IO (CELL_FLAG_INPUT|CELL_FLAG_OUTPUT) #define CELL_FLAG_WALL 0x4 +#define CELL_FLAG_HOVER 0x8 +#define CELL_FLAG_ITER 0x10 +#define CELL_FLAG_CANAL 0x20 static struct { @@ -50,11 +53,31 @@ static struct struct cell { u32 flags; + u32 model_id; + char *conditions; } * cells; + vec3 origin; + struct cell *selected; + int select_valid; + u32 *io; + + struct vstack + { + struct vframe + { + int x, y; + int i; + } + frames[ 64 ]; + + int level; + u32 flags; + } + stack; } map; @@ -73,6 +96,13 @@ static void map_free(void) map.io = NULL; } +static struct cell *map_tile_at( int pos[2] ) +{ + if( pos[0] >= 0 && pos[0] < map.x && pos[1] >= 0 && pos[1] < map.y ) + return map.cells + pos[1]*map.x + pos[0]; + return NULL; +} + static int map_load( const char *str ) { map_free(); @@ -188,6 +218,10 @@ static int map_load( const char *str ) c ++; } + // Origin top left corner + map.origin[0] = -((float)map.x) * 0.5f; + map.origin[2] = -((float)map.y) * 0.5f; + vg_success( "Map loaded! (%u:%u)\n", map.x, map.y ); return 1; } @@ -201,7 +235,7 @@ void vg_update(void) glm_mat4_identity( m_view ); glm_translate_z( m_view, -10.f ); - glm_rotate_x( m_view, -1.0f, m_view ); + glm_rotate_x( m_view, 1.0f, m_view ); glm_mat4_mul( m_projection, m_view, m_pv ); @@ -222,12 +256,12 @@ void vg_update(void) vec3 tile_pos; glm_vec3_copy( ray_origin, tile_pos ); glm_vec3_muladds( ray_dir, ray_t, tile_pos ); + glm_vec3_sub( tile_pos, map.origin, tile_pos ); - tile_pos[0] = floorf( tile_pos[0] + 0.5f ); - tile_pos[2] = floorf( tile_pos[2] + 0.5f ); + int tile_x = floorf( tile_pos[0] ); + int tile_y = floorf( tile_pos[2] ); - glm_mat4_identity( m_mdl ); - glm_translate( m_mdl, tile_pos ); + map.selected = map_tile_at( (int [2]){tile_x, tile_y} ); } GLuint tile_vao; @@ -243,12 +277,43 @@ void vg_render(void) SHADER_USE( colour_shader ); glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uPv" ), 1, GL_FALSE, (float *)m_pv ); - glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.5f, 0.5f, 0.5f, 1.0f ); - - glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl ); - glBindVertexArray( tile_vao ); - glDrawArrays( GL_TRIANGLES, 0, 6 ); + for( int y = 0; y < map.y; y ++ ) + { + for( int x = 0; x < map.x; x ++ ) + { + glm_mat4_identity( m_mdl ); + glm_translate( m_mdl, + (vec3){ + map.origin[0] + (float)x + 0.5f, + 0.f, + map.origin[2] + (float)y + 0.5f + } + ); + glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl ); + + struct cell *cell = &map.cells[ y*map.x+x ]; + + if( map.selected != cell ) + { + if( cell->flags & CELL_FLAG_INPUT ) + glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.9f, 0.5f, 0.5f, 1.0f ); + else if( cell->flags & CELL_FLAG_OUTPUT ) + glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.5f, 0.9f, 0.5f, 1.0f ); + else if( cell->flags & CELL_FLAG_WALL ) + glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.1f, 0.1f, 0.1f, 1.0f ); + else + glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.7f, 0.7f, 0.7f, 1.0f ); + } + else + { + float flash = sinf( vg_time*2.5f ) * 0.25f + 0.75f; + glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), flash,flash,flash, 1.0f ); + } + + glDrawArrays( GL_TRIANGLES, 0, 6 ); + } + } } void vg_start(void) @@ -285,12 +350,13 @@ void vg_start(void) map_load ( - "###########;\n" - "# + #;abab\n" + "#####-#####;aa\n" + "# #;\n" + "# #;\n" + "# -;bb\n" "# #;\n" "# #;\n" - "# - - #;aa,bb\n" - "###########;\n" + "#####+#####;abab\n" ); } -- 2.25.1