add guidelines for editing
authorhgn <hgodden00@gmail.com>
Sat, 23 Oct 2021 20:52:21 +0000 (21:52 +0100)
committerhgn <hgodden00@gmail.com>
Sat, 23 Oct 2021 20:52:21 +0000 (21:52 +0100)
fishladder.c

index eb86953ebe0d5ce3d90fb749d7ae99894d06477b..60bfb6a5267e972a047d88a16579edc2d076376d 100644 (file)
@@ -70,6 +70,7 @@ SHADER_DEFINE( shader_tile_main,
        "uniform mat2 uSubTransform;"
        ""
        "out vec4 aTexCoords;"
+       "out vec2 aWorldCoords;"
        ""
        "vec2 hash22(vec2 p)"
        "{"
@@ -80,15 +81,16 @@ SHADER_DEFINE( shader_tile_main,
        ""
        "void main()"
        "{"
-               // Create texture coords
-               "vec2 random_offset = floor(hash22(uOffset.xy) * 4.0) * 0.25;"
-               "vec2 edge_safe_coords = a_co * 0.98 + 0.01;"
-               "aTexCoords = vec4((edge_safe_coords + uOffset.zw) * 0.25, edge_safe_coords * 0.25 + random_offset );"
-               
                // Vertex transform
                "vec2 subtransform = uSubTransform * (a_co-0.5) + 0.5;"
                "vec3 worldpos = vec3( subtransform + uOffset.xy, 1.0 );"
                "gl_Position = vec4( uPv * worldpos, 1.0 );"
+
+               // Create texture coords
+               "vec2 random_offset = floor(hash22(uOffset.xy) * 4.0) * 0.25;"
+               "vec2 edge_safe_coords = a_co * 0.98 + 0.01;"
+               "aTexCoords = vec4((edge_safe_coords + uOffset.zw) * 0.25, edge_safe_coords * 0.25 + random_offset );"
+               "aWorldCoords = worldpos.xy;"           
        "}",
        
        // FRAGMENT
@@ -96,8 +98,12 @@ SHADER_DEFINE( shader_tile_main,
        ""
        "uniform sampler2D uTexGlyphs;"
        "uniform sampler2D uTexWood;"
+       "uniform float uGhost;"
+       "uniform vec2 uMousePos;"
+       "uniform vec4 uColour;"
        ""
        "in vec4 aTexCoords;"
+       "in vec2 aWorldCoords;"
        ""
        "void main()"
        "{"
@@ -109,10 +115,15 @@ SHADER_DEFINE( shader_tile_main,
                
                "vec3 shadows = mix( vec3( 0.85, 0.7344, 0.561 ), vec3(1.0,1.0,1.0), glyph.r );"
                
-               "FragColor = vec4( wood_comp * shadows, glyph.b );"
+               "vec4 output_regular = vec4( wood_comp * shadows, glyph.b );"
+               
+               "float ghost_dist = clamp( 1.5 - distance(uMousePos, aWorldCoords), 0.0, 1.0 );"
+               "vec4 output_ghost = vec4( 1.0, 1.0, 1.0, glyph.g * ghost_dist );"
+               
+               "FragColor = mix( output_regular, output_ghost, uGhost ) * uColour;"
        "}"
        ,
-       UNIFORMS({ "uPv", "uOffset", "uTexGlyphs", "uTexWood", "uSubTransform" })
+       UNIFORMS({ "uPv", "uOffset", "uTexGlyphs", "uTexWood", "uSubTransform", "uGhost", "uMousePos", "uColour" })
 )
 
 const char *level_pack[] = 
@@ -326,7 +337,8 @@ struct world
        
        struct mesh tile, circle;
        
-       int selected;
+       int selected, tile_x, tile_y;
+       v2f tile_pos;
        
        struct fish
        {
@@ -746,19 +758,17 @@ void vg_update(void)
        vg_projection_update();
        
        // Input stuff
+       v2_copy( vg_mouse_ws, world.tile_pos );
        
-       v2f tile_pos;
-       v2_copy( vg_mouse_ws, tile_pos );
-       
-       int tile_x = floorf( tile_pos[0] );
-       int tile_y = floorf( tile_pos[1] );
+       world.tile_x = floorf( world.tile_pos[0] );
+       world.tile_y = floorf( world.tile_pos[1] );
 
        // Tilemap editing
        if( !world.simulating )
        {
-               if( cell_interactive( (v2i){ tile_x, tile_y } ))
+               if( cell_interactive( (v2i){ world.tile_x, world.tile_y } ))
                {
-                       world.selected = tile_y * world.w + tile_x;
+                       world.selected = world.tile_y * world.w + world.tile_x;
                        
                        if( vg_get_button_down("primary") )
                        {
@@ -769,7 +779,8 @@ void vg_update(void)
                                else
                                        sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 0, 3 );
                                        
-                               map_reclassify( (v2i){ tile_x -2, tile_y -2 }, (v2i){ tile_x +2, tile_y +2 } );
+                               map_reclassify(         (v2i){ world.tile_x -2, world.tile_y -2 }, 
+                                                                               (v2i){ world.tile_x +2, world.tile_y +2 } );
                        }
                }
                else
@@ -941,11 +952,22 @@ void vg_update(void)
        }
 }
 
-static void render_tiles(void)
+static void render_tiles( v2i start, v2i end, v4f const regular_colour, v4f const selected_colour )
 {
-       for( int y = 0; y < world.h; y ++ )
+       v2i full_start = { 0,0 };
+       v2i full_end = { world.w, world.h };
+       
+       if( !start || !end )
        {
-               for( int x = 0; x < world.w; x ++ )
+               start = full_start;
+               end = full_end;
+       }
+       
+       glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
+
+       for( int y = start[1]; y < end[1]; y ++ )
+       {
+               for( int x = start[0]; x < end[0]; x ++ )
                {
                        struct cell *cell = pcell((v2i){x,y});
                        int selected = world.selected == y*world.w + x;
@@ -967,7 +989,14 @@ static void render_tiles(void)
                        }
                        
                        glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y, uv[0], uv[1] );
-                       draw_mesh( 0, 2 );
+                       if( selected )
+                       {
+                               glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, selected_colour );
+                               draw_mesh( 0, 2 );
+                               glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
+                       }
+                       else                    
+                               draw_mesh( 0, 2 );
                }
        }
 }
@@ -988,6 +1017,9 @@ void vg_render(void)
                frame_lerp = scaled_time - (float)world.sim_frame;
        }
        
+       v4f const colour_default = {1.0f, 1.0f, 1.0f, 1.0f};
+       v4f const colour_selected = {0.90f, 0.92f, 1.0f, 1.0f};
+       
        v2f const curve_3[] = {{0.5f,1.0f},{0.5f,0.625f},{0.625f,0.5f},{1.0f,0.5f}};
        v2f const curve_6[] = {{0.5f,1.0f},{0.5f,0.625f},{0.375f,0.5f},{0.0f,0.5f}};
        v2f const curve_9[] = {{1.0f,0.5f},{0.625f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
@@ -1014,6 +1046,7 @@ void vg_render(void)
        m2x2_identity( subtransform );
        glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );  
        glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
+       glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 0.0f );
        
        // Bind textures
        glActiveTexture( GL_TEXTURE0 );
@@ -1024,7 +1057,7 @@ void vg_render(void)
        glBindTexture( GL_TEXTURE_2D, tex_wood );
        glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
        
-       render_tiles();
+       render_tiles( NULL, NULL, colour_default, colour_default );
        
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -1125,7 +1158,7 @@ void vg_render(void)
        glBindTexture( GL_TEXTURE_2D, tex_wood );
        glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
        
-       render_tiles();
+       render_tiles( NULL, NULL, colour_default, colour_selected  );
 
        //      Draw splitters
 
@@ -1166,6 +1199,26 @@ void vg_render(void)
                }
        }
        
+       // Edit overlay
+       if( world.selected != -1 && !(world.data[ world.selected ].state & FLAG_CANAL) )
+       {
+               v2i new_begin = { world.tile_x - 2, world.tile_y - 2 };
+               v2i new_end = { world.tile_x + 2, world.tile_y + 2 };
+       
+               world.data[ world.selected ].state ^= FLAG_CANAL;
+               map_reclassify( new_begin, new_end );
+               
+               m2x2_identity( subtransform );
+               glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 1.0f );
+               glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );  
+               glUniform2fv( SHADER_UNIFORM( shader_tile_main, "uMousePos" ), 1, world.tile_pos );
+               
+               render_tiles( new_begin, new_end, colour_default, colour_default );
+               
+               world.data[ world.selected ].state ^= FLAG_CANAL;
+               map_reclassify( new_begin, new_end );
+       }
+       
        //glDisable(GL_BLEND);
        
        glDisable(GL_BLEND);