1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
6 SHADER_DEFINE( shader_tile_colour
,
9 "layout (location=0) in vec2 a_co;"
11 "uniform vec2 uOffset;"
15 "gl_Position = vec4( uPv * vec3( a_co + uOffset, 1.0 ), 1.0 );"
20 "uniform vec4 uColour;"
24 "FragColor = uColour;"
27 UNIFORMS({ "uPv", "uOffset", "uColour" })
34 #define FLAG_INPUT 0x1
35 #define FLAG_OUTPUT 0x2
36 #define FLAG_CANAL 0x4
65 static void map_free(void)
67 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
68 arrfree( world
.io
[ i
].conditions
);
70 arrfree( world
.data
);
79 static int map_load( const char *str
)
88 if( str
[world
.w
] == ';' )
90 else if( !str
[world
.w
] )
92 vg_error( "Unexpected EOF when parsing level\n" );
97 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
99 int reg_start
= 0, reg_end
= 0;
115 if( reg_start
< reg_end
)
117 if( *c
>= 'a' && *c
<= 'z' )
119 arrpush( world
.io
[ reg_start
].conditions
, *c
);
123 if( *c
== ',' || *c
== '\n' )
132 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
139 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
147 if( reg_start
!= reg_end
)
149 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
155 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
159 row
= arraddnptr( world
.data
, world
.w
);
162 reg_end
= reg_start
= arrlen( world
.io
);
168 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
172 // Tile initialization
174 row
[ cx
].water
[0] = 0;
175 row
[ cx
].water
[1] = 0;
177 if( *c
== '+' || *c
== '-' )
179 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
180 arrpush( world
.io
, term
);
181 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
186 row
[ cx
++ ].state
= FLAG_WALL
;
190 row
[ cx
++ ].state
= 0x00;
197 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
201 int main( int argc
, char *argv
[] )
203 vg_init( argc
, argv
, "FishLadder" );
206 void vg_register(void)
208 SHADER_INIT( shader_tile_colour
);
213 glGenVertexArrays( 1, &world
.tile_vao
);
214 glGenBuffers( 1, &world
.tile_vbo
);
218 0.05f
, 0.05f
, 0.05f
, 0.95f
, 0.95f
, 0.95f
,
219 0.05f
, 0.05f
, 0.95f
, 0.95f
, 0.95f
, 0.05f
222 glBindVertexArray( world
.tile_vao
);
223 glBindBuffer( GL_ARRAY_BUFFER
, world
.tile_vbo
);
232 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
233 glEnableVertexAttribArray( 0 );
240 "###-#####-###;aaa,aa\n"
247 "###+#####+###;aa,aaa\n"
254 glDeleteVertexArrays( 1, &world
.tile_vao
);
255 glDeleteBuffers( 1, &world
.tile_vbo
);
262 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
263 float const size
= 9.5f
;
266 origin
[0] = -0.5f
* world
.w
;
267 origin
[1] = -0.5f
* world
.h
;
270 m3x3_projection( m_projection
, -size
, size
, size
*ratio
, -size
*ratio
);
271 m3x3_identity( m_view
);
272 m3x3_translate( m_view
, origin
);
273 m3x3_mul( m_projection
, m_view
, vg_pv
);
274 vg_projection_update();
277 v2_copy( vg_mouse_ws
, tile_pos
);
279 int tile_x
= floorf( tile_pos
[0] );
280 int tile_y
= floorf( tile_pos
[1] );
282 if( tile_x
>= 2 && tile_x
< world
.w
-2 && tile_y
>= 2 && tile_y
<= world
.h
-2 )
284 world
.selected
= tile_y
* world
.w
+ tile_x
;
286 struct cell
*cell
= &world
.data
[tile_y
*world
.w
+tile_x
];
287 if( cell
->state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
293 if( vg_get_button_down("primary") )
295 cell
->state
^= FLAG_CANAL
;
303 static int update_tick
= 0;
306 if( update_tick
> 5 )
310 u32 buffer_id
= world
.frame
& 0x1;
311 u32 buffer_next
= buffer_id
^ 0x1;
313 for( int y
= 1; y
< world
.h
-1; y
++ )
315 for( int x
= 1; x
< world
.w
-1; x
++ )
317 struct cell
*cell
= &world
.data
[y
*world
.w
+x
];
319 if( cell
->state
& FLAG_INPUT
)
320 cell
->water
[ buffer_next
] = 8;
324 cell
->water
[ buffer_next
] = 0;
326 if( cell
->state
& FLAG_CANAL
)
328 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
330 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
332 struct cell
*neighbour
= &world
.data
[(y
+dirs
[i
][1])*world
.w
+x
+dirs
[i
][0]];
334 if( neighbour
->water
[ buffer_id
] > cell
->water
[ buffer_next
]+1 )
337 cell
->water
[ buffer_next
] = neighbour
->water
[ buffer_id
]-1;
342 if( !has_source
&& cell
->water
[ buffer_id
] )
343 cell
->water
[ buffer_next
] = cell
->water
[ buffer_id
]-1;
354 glViewport( 0,0, vg_window_x
, vg_window_y
);
356 glDisable( GL_DEPTH_TEST
);
357 glClearColor( 0.01f
, 0.01f
, 0.01f
, 1.0f
);
358 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
360 glBindVertexArray( world
.tile_vao
);
361 SHADER_USE( shader_tile_colour
);
362 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
364 for( int y
= 0; y
< world
.h
; y
++ )
366 for( int x
= 0; x
< world
.w
; x
++ )
368 glUniform2f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)x
, (float)y
);
372 struct cell
*cell
= &world
.data
[y
*world
.w
+x
];
374 if( cell
->state
& FLAG_WALL
) { v4_copy( (v4f
){ 0.2f
, 0.2f
, 0.2f
, 1.0f
}, colour
); }
375 else if( cell
->state
& FLAG_CANAL
) { v4_copy( (v4f
){ 0.6f
, 0.6f
, 0.6f
, 1.0f
}, colour
); }
376 else if( cell
->state
& FLAG_INPUT
) { v4_copy( (v4f
){ 0.2f
, 0.3f
, 0.7f
, 1.0f
}, colour
); }
377 else if( cell
->state
& FLAG_OUTPUT
) { v4_copy( (v4f
){ 0.2f
, 0.7f
, 0.3f
, 1.0f
}, colour
); }
378 else v4_copy( (v4f
){ 0.9f
, 0.9f
, 0.9f
, 1.0f
}, colour
);
380 if( cell
->water
[world
.frame
&0x1] )
381 v4_copy( (v4f
){ 0.2f
, 0.3f
, 0.7f
* (float)(cell
->water
[world
.frame
&0x1]) * (1.0f
/8.0f
), 1.0f
}, colour
);
383 if( world
.selected
== y
*world
.w
+ x
)
384 v3_muls( colour
, sinf( vg_time
)*0.25f
+ 0.5f
, colour
);
386 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, colour
);
388 glDrawArrays( GL_TRIANGLES
, 0, 6 );