27bea90b33cd203526803093f5c7b51d482293fa
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
38 #define FLAG_DROP 0x10
39 #define FLAG_SPLIT 0x20
68 static void map_free(void)
70 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
71 arrfree( world
.io
[ i
].conditions
);
73 arrfree( world
.data
);
82 static int map_load( const char *str
)
91 if( str
[world
.w
] == ';' )
93 else if( !str
[world
.w
] )
95 vg_error( "Unexpected EOF when parsing level\n" );
100 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
102 int reg_start
= 0, reg_end
= 0;
118 if( reg_start
< reg_end
)
120 if( *c
>= 'a' && *c
<= 'z' )
122 arrpush( world
.io
[ reg_start
].conditions
, *c
);
126 if( *c
== ',' || *c
== '\n' )
135 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
142 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
150 if( reg_start
!= reg_end
)
152 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
158 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
162 row
= arraddnptr( world
.data
, world
.w
);
165 reg_end
= reg_start
= arrlen( world
.io
);
171 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
175 // Tile initialization
177 row
[ cx
].water
[0] = 0;
178 row
[ cx
].water
[1] = 0;
180 if( *c
== '+' || *c
== '-' )
182 struct cell_terminal term
= { .id
= cx
+ world
.h
*world
.w
};
183 arrpush( world
.io
, term
);
184 row
[ cx
++ ].state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
189 row
[ cx
++ ].state
= FLAG_WALL
;
193 row
[ cx
++ ].state
= 0x00;
200 vg_success( "Map loaded! (%u:%u)\n", world
.w
, world
.h
);
204 int main( int argc
, char *argv
[] )
206 vg_init( argc
, argv
, "FishLadder" );
209 void vg_register(void)
211 SHADER_INIT( shader_tile_colour
);
216 glGenVertexArrays( 1, &world
.tile_vao
);
217 glGenBuffers( 1, &world
.tile_vbo
);
221 0.05f
, 0.05f
, 0.05f
, 0.95f
, 0.95f
, 0.95f
,
222 0.05f
, 0.05f
, 0.95f
, 0.95f
, 0.95f
, 0.05f
225 glBindVertexArray( world
.tile_vao
);
226 glBindBuffer( GL_ARRAY_BUFFER
, world
.tile_vbo
);
235 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
236 glEnableVertexAttribArray( 0 );
243 "###-#####-###;aaa,aa\n"
250 "###+#####+###;aa,aaa\n"
257 glDeleteVertexArrays( 1, &world
.tile_vao
);
258 glDeleteBuffers( 1, &world
.tile_vbo
);
263 static int cell_interactive( v2i co
)
266 if( co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2 )
270 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
273 // List of 3x3 configurations that we do not allow
274 static u32 invalid_src
[][9] =
306 // Statically compile invalid configurations into bitmasks
307 static u32 invalid
[ vg_list_size(invalid_src
) ];
309 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
313 for( int j
= 0; j
< 3; j
++ )
314 for( int k
= 0; k
< 3; k
++ )
315 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
320 // Extract 5x5 grid surrounding tile
322 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
323 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
325 struct cell
*cell
= &world
.data
[ world
.w
*y
+ x
];
327 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
328 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
331 // Run filter over center 3x3 grid to check for invalid configurations
332 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
333 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
335 if( blob
& (0x1 << (6+kernel
[i
])) )
337 u32 window
= blob
>> kernel
[i
];
339 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
340 if((window
& invalid
[j
]) == invalid
[j
])
350 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
351 float const size
= 9.5f
;
354 origin
[0] = -0.5f
* world
.w
;
355 origin
[1] = -0.5f
* world
.h
;
358 m3x3_projection( m_projection
, -size
, size
, size
*ratio
, -size
*ratio
);
359 m3x3_identity( m_view
);
360 m3x3_translate( m_view
, origin
);
361 m3x3_mul( m_projection
, m_view
, vg_pv
);
362 vg_projection_update();
365 v2_copy( vg_mouse_ws
, tile_pos
);
367 int tile_x
= floorf( tile_pos
[0] );
368 int tile_y
= floorf( tile_pos
[1] );
370 if( cell_interactive( (v2i
){ tile_x
, tile_y
} ))
372 world
.selected
= tile_y
* world
.w
+ tile_x
;
374 if( vg_get_button_down("primary") )
376 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
382 // Reclassify world. TODO: Move into own function
383 for( int y
= 2; y
< world
.h
-2; y
++ )
385 for( int x
= 2; x
< world
.w
-2; x
++ )
387 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
391 if( world
.data
[y
*world
.w
+x
].state
& FLAG_CANAL
)
393 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
395 struct cell
*neighbour
= &world
.data
[(y
+dirs
[i
][1])*world
.w
+x
+dirs
[i
][0]];
396 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
401 world
.data
[ y
*world
.w
+x
].config
= config
;
402 world
.data
[ y
*world
.w
+x
].state
&= ~(FLAG_DROP
|FLAG_SPLIT
);
406 for( int y
= 2; y
< world
.h
-2; y
++ )
407 for( int x
= 2; x
< world
.w
-2; x
++ )
409 // R,D,L,- 1110 (splitter, 1 drop created)
411 // R,-,L,U - 1011 (merger, 2 drop created)
413 u8 config
= world
.data
[y
*world
.w
+x
].config
;
417 world
.data
[y
*world
.w
+x
].state
|= (FLAG_SPLIT
| FLAG_DROP
);
419 else if( config
== 0xD )
421 world
.data
[y
*world
.w
+x
-1].state
|= FLAG_DROP
;
422 world
.data
[y
*world
.w
+x
+1].state
|= FLAG_DROP
;
439 configs (Downlevels marked)
451 Path tracing with min function
463 Path tracing with min function
489 static int update_tick
= 0;
492 if( update_tick
> 5 )
496 u32 buffer_id
= world
.frame
& 0x1;
497 u32 buffer_next
= buffer_id
^ 0x1;
499 for( int y
= 1; y
< world
.h
-1; y
++ )
501 for( int x
= 1; x
< world
.w
-1; x
++ )
503 struct cell
*cell
= &world
.data
[y
*world
.w
+x
];
505 if( cell
->state
& (FLAG_OUTPUT
|FLAG_DROP
) )
506 cell
->water
[ buffer_next
] = 16;
510 cell
->water
[ buffer_next
] = 0;
512 if( cell
->state
& FLAG_CANAL
)
514 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
516 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
518 struct cell
*neighbour
= &world
.data
[(y
+dirs
[i
][1])*world
.w
+x
+dirs
[i
][0]];
520 if( neighbour
->water
[ buffer_id
] > cell
->water
[ buffer_next
]+1 )
523 cell
->water
[ buffer_next
] = neighbour
->water
[ buffer_id
]-1;
528 if( !has_source
&& cell
->water
[ buffer_id
] )
529 cell
->water
[ buffer_next
] = cell
->water
[ buffer_id
]-1;
540 glViewport( 0,0, vg_window_x
, vg_window_y
);
542 glDisable( GL_DEPTH_TEST
);
543 glClearColor( 0.01f
, 0.01f
, 0.01f
, 1.0f
);
544 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
546 glBindVertexArray( world
.tile_vao
);
547 SHADER_USE( shader_tile_colour
);
548 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
550 for( int y
= 0; y
< world
.h
; y
++ )
552 for( int x
= 0; x
< world
.w
; x
++ )
554 glUniform2f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)x
, (float)y
);
558 struct cell
*cell
= &world
.data
[y
*world
.w
+x
];
560 if( cell
->state
& FLAG_WALL
) { v4_copy( (v4f
){ 0.2f
, 0.2f
, 0.2f
, 1.0f
}, colour
); }
561 else if( cell
->state
& FLAG_CANAL
) { v4_copy( (v4f
){ 0.6f
, 0.6f
, 0.6f
, 1.0f
}, colour
); }
562 else if( cell
->state
& FLAG_INPUT
) { v4_copy( (v4f
){ 0.2f
, 0.3f
, 0.7f
, 1.0f
}, colour
); }
563 else if( cell
->state
& FLAG_OUTPUT
) { v4_copy( (v4f
){ 0.2f
, 0.7f
, 0.3f
, 1.0f
}, colour
); }
564 else v4_copy( (v4f
){ 0.9f
, 0.9f
, 0.9f
, 1.0f
}, colour
);
566 if( cell
->water
[world
.frame
&0x1] )
567 v4_copy( (v4f
){ 0.2f
, 0.3f
, 0.7f
* (float)(cell
->water
[world
.frame
&0x1]) * (1.0f
/8.0f
), 1.0f
}, colour
);
569 if( world
.selected
== y
*world
.w
+ x
)
570 v3_muls( colour
, sinf( vg_time
)*0.25f
+ 0.5f
, colour
);
572 if( cell
->state
& (FLAG_DROP
|FLAG_SPLIT
) )
573 v4_copy( (v4f
){ 0.75f
, 0.75f
, 0.02f
, 1.0f
}, colour
);
575 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, colour
);
577 glDrawArrays( GL_TRIANGLES
, 0, 6 );