1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
6 #define CELL_SHEET_X 512
7 #define CELL_SHEET_Y 512
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 = vec4((a_co*0.98+0.01 + vec2(a_data.x,0.0)) * 0.0625, a_data.zw);"
53 "uniform sampler2D uTexTiles;"
54 "uniform sampler2D uTexRipples;"
55 "uniform float uTime;"
62 "vec4 glyph = texture( uTexTiles, aCoords.xy );"
63 "vec4 ripple = texture( uTexRipples, vec2( glyph.x - uTime, glyph.y ));"
64 "float wstatus = -((aCoords.z/128.0)-1.0);" // -1 := None in, 0.0 := Hold middle, 1.0 := All out
65 "float dev = step( 0.0, glyph.x+wstatus ) * step( glyph.x+wstatus, 1.0 );" //+ abs(glyph.y-0.5) );"
66 "vec3 composite = mix( vec3(0.5,0.5,0.5), vec3(0.3,0.6,1.0) + ripple.xyz * 0.1, step( 0.75, glyph.a )*dev ) * glyph.z;"
67 "FragColor = vec4( composite, 1.0 );"
70 UNIFORMS({ "uPv", "uTexTiles", "uTexRipples", "uTime", "uOrigin" })
77 int main( int argc
, char *argv
[] )
79 vg_init( argc
, argv
, "FishLadder" );
82 #define CELL_FLAG_INPUT 0x1
83 #define CELL_FLAG_OUTPUT 0x2
84 #define CELL_FLAG_IO (CELL_FLAG_INPUT|CELL_FLAG_OUTPUT)
85 #define CELL_FLAG_WALL 0x4
86 #define CELL_FLAG_HOVER 0x8
87 #define CELL_FLAG_ITER 0x10
88 #define CELL_FLAG_CANAL 0x20
89 #define CELL_FLAG_SPLIT 0x40 /* Does this cell split and have an incoming vertical connection? */
90 #define CELL_FLAG_WALKABLE (CELL_FLAG_IO|CELL_FLAG_CANAL)
91 #define CELL_FLAG_VISITED 0x80
92 #define CELL_FLAG_UPLVL 0x100
93 #define CELL_FLAG_MERGE 0x200
123 struct cell
*selected
;
152 static void map_free(void)
154 for( int i
= 0; i
< arrlen( map
.io
); i
++ )
156 arrfree( map
.cells
[ map
.io
[i
] ].conditions
);
159 arrfree( map
.cells
);
167 static struct cell
*map_tile( int pos
[2] )
169 return map
.cells
+ pos
[1]*map
.x
+ pos
[0];
172 static struct cell
*map_tile_at( int pos
[2] )
174 if( pos
[0] >= 0 && pos
[0] < map
.x
&& pos
[1] >= 0 && pos
[1] < map
.y
)
175 return map
.cells
+ pos
[1]*map
.x
+ pos
[0];
179 static struct cell
*map_tile_at_cond( int pos
[2], u32 flags
)
181 struct cell
*cell
= map_tile_at( pos
);
182 if( cell
&& (cell
->flags
& flags
) )
188 static void map_tile_coords_from_index( int i
, int coords
[2] )
190 coords
[0] = i
% map
.x
;
191 coords
[1] = (i
- coords
[0])/map
.x
;
194 static void map_stack_refresh(void)
196 for( int i
= 0; i
< map
.x
*map
.y
; i
++ )
197 map
.cells
[i
].flags
&= ~CELL_FLAG_VISITED
;
200 static void map_stack_init( int coords
[2] )
203 map
.stack
.frames
[0].i
= 0;
204 map
.stack
.frames
[0].x
= coords
[0];
205 map
.stack
.frames
[0].y
= coords
[1];
208 static struct cell
*map_stack_next(void)
210 struct cell
*tile
= NULL
;
214 struct vframe
*frame
= &map
.stack
.frames
[ map
.stack
.level
];
216 int output_dirs
[][2] = { {0,1}, {-1,0}, {1,0} };
220 int *dir
= output_dirs
[ frame
->i
];
221 tile
= map_tile_at_cond( (int[2]){frame
->x
+dir
[0], frame
->y
+dir
[1]}, CELL_FLAG_WALKABLE
);
223 if( tile
&& !(tile
->flags
& CELL_FLAG_VISITED
) )
227 frame
[1].x
= frame
[0].x
+dir
[0];
228 frame
[1].y
= frame
[0].y
+dir
[1];
230 tile
->flags
|= CELL_FLAG_VISITED
;
245 if( map
.stack
.level
< 0 )
253 static void map_stack_current_coords( int coords
[2], int offset
)
255 coords
[0] = map
.stack
.frames
[ map
.stack
.level
+offset
].x
;
256 coords
[1] = map
.stack
.frames
[ map
.stack
.level
+offset
].y
;
259 static void map_update_visual(void)
263 static int compute_flow_counter
= 0;
264 compute_flow_counter
^= 0x1;
266 for( int i
= 0; i
< map
.y
*map
.x
; i
++ )
268 map
.cells
[i
].flowing
[compute_flow_counter
] = map
.cells
[i
].flowing
[compute_flow_counter
^0x1];
270 if( map
.cells
[i
].flowing
[compute_flow_counter
] )
271 map
.cells
[i
].flowing
[compute_flow_counter
] --;
274 for( int y
= 0; y
< map
.y
; y
++ )
276 for( int x
= 0; x
< map
.x
; x
++ )
278 struct cell
*cur
= map
.cells
+ y
*map
.x
+ x
;
279 u8
*cellbytes
= celldata
+ (y
*map
.x
+x
)*4;
281 if( cur
->flags
& CELL_FLAG_WALKABLE
)
283 struct cell
*a
, *b
, *c
, *d
;
285 a
= map_tile_at_cond( (int[2]){ x
,y
+1 }, CELL_FLAG_WALKABLE
);
286 b
= map_tile_at_cond( (int[2]){ x
+1,y
}, CELL_FLAG_WALKABLE
);
287 c
= map_tile_at_cond( (int[2]){ x
,y
-1 }, CELL_FLAG_WALKABLE
);
288 d
= map_tile_at_cond( (int[2]){ x
-1,y
}, CELL_FLAG_WALKABLE
);
290 u32 config
= (a
?0x1:0x0) | (b
?0x2:0x0) | (c
?0x4:0x0) | (d
?0x8:0x0);
291 cellbytes
[ 0 ] = config
;
293 if( cur
->flags
& CELL_FLAG_OUTPUT
)
294 cur
->flowing
[ compute_flow_counter
] = 128;
298 // TODO: Random background tiles
304 int const k_rate_flow
= 16;
307 for( int i
= 0; i
< arrlen( map
.io
); i
++ )
310 if( map
.cells
[ map
.io
[i
] ].flags
& CELL_FLAG_OUTPUT
)
312 map_tile_coords_from_index( map
.io
[i
], inputcoord
);
313 map_stack_init( inputcoord
);
314 struct cell
*cell
= map_tile_at( inputcoord
);
320 map_stack_current_coords( cr
, 0 );
322 u8
*cellbytes
= celldata
+ (cr
[1]*map
.x
+cr
[0])*4;
324 if( cell
->flowing
[ compute_flow_counter
^ 0x1 ] == 128 )
326 switch( cellbytes
[0] )
329 case 1: case 5: case 11: case 9: case 3:
330 map_tile( (int[2]){ cr
[0], cr
[1]+1 } )->flowing
[ compute_flow_counter
] += k_rate_flow
;
335 if( celldata
[ (cr
[1]*map
.x
+cr
[0]+1)*4 ] != 14 )
336 map_tile( (int[2]){ cr
[0]+1, cr
[1] } )->flowing
[ compute_flow_counter
] += k_rate_flow
;
341 if( celldata
[ (cr
[1]*map
.x
+cr
[0]-1)*4 ] != 14 )
342 map_tile( (int[2]){ cr
[0]-1, cr
[1] } )->flowing
[ compute_flow_counter
] += k_rate_flow
;
347 if( celldata
[ (cr
[1]*map
.x
+cr
[0]+1)*4 ] != 14 )
348 map_tile( (int[2]){ cr
[0]+1, cr
[1] } )->flowing
[ compute_flow_counter
] += k_rate_flow
;
349 if( celldata
[ (cr
[1]*map
.x
+cr
[0]-1)*4 ] != 14 )
350 map_tile( (int[2]){ cr
[0]-1, cr
[1] } )->flowing
[ compute_flow_counter
] += k_rate_flow
;
356 if( cellbytes
[0] == 10 )
359 map_stack_current_coords( crl
, -1 );
367 while( (cell
= map_stack_next()) );
371 for( int i
= 0; i
< map
.y
*map
.x
; i
++ )
373 map
.cells
[i
].flowing
[ compute_flow_counter
] = vg_min( 128, map
.cells
[i
].flowing
[ compute_flow_counter
] );
374 celldata
[ i
*4+2 ] = map
.cells
[i
].flowing
[ compute_flow_counter
];
380 glBindBuffer( GL_ARRAY_BUFFER
, map
.tiles_vbo
);
381 glBufferSubData( GL_ARRAY_BUFFER
, 16*sizeof(float) + 1024*2*sizeof(float), map
.x
*map
.y
*4, celldata
);
384 static int map_load( const char *str
)
393 if( str
[map
.x
] == ';' )
395 else if( !str
[map
.x
] )
397 vg_error( "Unexpected EOF when parsing level!\n" );
402 struct cell
*row
= arraddnptr( map
.cells
, map
.x
);
404 int reg_start
= 0, reg_end
= 0;
420 if( reg_start
< reg_end
)
422 if( *c
>= 'a' && *c
<= 'z' )
424 arrpush( map
.cells
[ map
.io
[ reg_start
] ].conditions
, *c
);
428 if( *c
== ',' || *c
== '\n' )
437 vg_error( "Unkown attrib '%c' (row: %u)\n", *c
, map
.y
);
444 vg_error( "Over-assigned values (row: %u)\n", map
.y
);
452 if( reg_start
!= reg_end
)
454 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", map
.y
, reg_start
, reg_end
);
460 vg_error( "Map row underflow (row: %u, %u<%u)\n", map
.y
, cx
, map
.x
);
464 row
= arraddnptr( map
.cells
, map
.x
);
467 reg_end
= reg_start
= arrlen( map
.io
);
473 vg_error( "Map row overflow (row: %u, %u>%u)\n", map
.y
, cx
, map
.x
);
477 row
[ cx
].conditions
= NULL
;
478 row
[ cx
].flowing
[ 0 ] = 0;
480 // Parse the various cell types
481 if( *c
== '+' || *c
== '-' )
483 arrpush( map
.io
, cx
+ map
.y
*map
.x
);
484 row
[ cx
++ ].flags
= *c
== '+'? CELL_FLAG_INPUT
: CELL_FLAG_OUTPUT
;
489 row
[ cx
++ ].flags
= CELL_FLAG_WALL
;
493 row
[ cx
++ ].flags
= 0x00;
500 // Origin top left corner
501 map
.origin
[0] = -((float)map
.x
) * 0.5f
;
502 map
.origin
[2] = -((float)map
.y
) * 0.5f
;
504 float *offset_array
= (float *)malloc( map
.x
*map
.y
*2*sizeof(float) );
506 for( int y
= 0; y
< map
.y
; y
++ )
508 for( int x
= 0; x
< map
.x
; x
++ )
510 float *coord
= offset_array
+ (y
*map
.x
+x
)*2;
516 glBindBuffer( GL_ARRAY_BUFFER
, map
.tiles_vbo
);
517 glBufferSubData( GL_ARRAY_BUFFER
, 16*sizeof(float), map
.x
*map
.y
*2*sizeof(float), offset_array
);
519 free( offset_array
);
520 vg_success( "Map loaded! (%u:%u)\n", map
.x
, map
.y
);
524 static int map_tile_availible( int co
[2] )
526 // Extract 5x5 grid surrounding tile
528 for( int y
= vg_max( co
[1]-2, 0 ); y
< vg_min( map
.y
, co
[1]+3 ); y
++ )
529 for( int x
= vg_max( co
[0]-2, 0 ); x
< vg_min( map
.x
, co
[0]+3 ); x
++ )
531 struct cell
*cell
= map_tile( (int[2]){ x
, y
} );
533 if( cell
&& (cell
->flags
& CELL_FLAG_WALKABLE
) )
534 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
537 // Run filter over center 3x3 grid to check for invalid configurations
538 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
539 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
541 if( blob
& (0x1 << (6+kernel
[i
])) )
543 // (reference window: 0x1CE7) Illegal moves
552 u32 invalid
[] = { 0x8E2, 0x63, 0xC6, 0xC60, 0x18C0, 0x862, 0x8C2 };
553 u32 window
= blob
>> kernel
[i
];
555 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
556 if((window
& invalid
[j
]) == invalid
[j
])
567 float ratio
= (float)vg_window_y
/ (float)vg_window_x
;
568 float const size
= 7.5f
;
569 glm_ortho( -size
, size
, -size
*ratio
, size
*ratio
, 0.01f
, 150.f
, m_projection
);
571 glm_mat4_identity( m_view
);
572 glm_translate_z( m_view
, -10.f
);
573 glm_rotate_x( m_view
, 1.5708f
, m_view
);
575 glm_mat4_mul( m_projection
, m_view
, vg_pv
);
577 // Compute map update
578 for( int y
= 0; y
< map
.y
; y
++ )
580 for( int x
= 0; x
< map
.x
; x
++ )
582 struct cell
*tile
, *upper
, *lower
, *l
, *r
;
583 tile
= map_tile_at( (int [2]){ x
, y
} );
584 tile
->flags
&= ~(CELL_FLAG_SPLIT
|CELL_FLAG_MERGE
|CELL_FLAG_UPLVL
);
586 if( tile
->flags
& CELL_FLAG_WALKABLE
)
588 r
= map_tile_at_cond( (int[2]){ x
+1, y
}, CELL_FLAG_WALKABLE
);
589 l
= map_tile_at_cond( (int[2]){ x
-1, y
}, CELL_FLAG_WALKABLE
);
593 upper
= map_tile_at_cond( (int[2]){ x
, y
-1 }, CELL_FLAG_WALKABLE
);
594 lower
= map_tile_at_cond( (int[2]){ x
, y
+1 }, CELL_FLAG_WALKABLE
);
598 tile
->flags
|= CELL_FLAG_MERGE
| CELL_FLAG_UPLVL
;
603 tile
->flags
|= CELL_FLAG_SPLIT
;
604 l
->flags
|= CELL_FLAG_UPLVL
;
605 r
->flags
|= CELL_FLAG_UPLVL
;
612 // Compute classification
616 for( int i = 0; i < arrlen( map.io ); i ++ )
618 struct *cell cell = &map.cells[ map.io ];
621 if( cell->flags & CELL_FLAG_INPUT )
623 map_tile_coords_from_index( map.io, coords );
624 map_stack_init( coords );
628 if( cell->flags & CELL_FLAG_CONNECTOR )
633 while( (cell = map_stack_next()) );
642 vec4 vp
= { 0.f
, 0.f
, vg_window_x
, vg_window_y
};
643 glm_mat4_inv( vg_pv
, pv_inverse
);
644 glm_unprojecti( (vec3
){ vg_mouse_x
, vg_window_y
-vg_mouse_y
, -1.f
}, pv_inverse
, vp
, ray_dir
);
645 glm_unprojecti( (vec3
){ vg_mouse_x
, vg_window_y
-vg_mouse_y
, 0.f
}, pv_inverse
, vp
, ray_origin
);
646 glm_vec3_sub( ray_dir
, ray_origin
, ray_dir
);
648 // Get floor tile intersection
649 float ray_t
= -ray_origin
[1] / ray_dir
[1];
652 glm_vec3_copy( ray_origin
, tile_pos
);
653 glm_vec3_muladds( ray_dir
, ray_t
, tile_pos
);
654 glm_vec3_sub( tile_pos
, map
.origin
, tile_pos
);
656 int tile_x
= floorf( tile_pos
[0] );
657 int tile_y
= floorf( tile_pos
[2] );
659 map
.selected
= map_tile_at( (int [2]){tile_x
, tile_y
} );
663 static int fish_counter
= 0;
666 if( fish_counter
> 20 )
670 // Advance characters
671 for( int i
= 0; i
< map
.num_fishes
; i
++ )
673 struct fish
*fish
= map
.fishes
+ i
;
678 struct cell
*tile
, *next
;
679 tile
= map_tile_at( fish
->co
);
681 if( tile
->flags
& CELL_FLAG_OUTPUT
)
683 vg_info( "Fish got zucced (%d)\n", i
);
689 if( tile
->flags
& CELL_FLAG_SPLIT
)
692 int new_dir
[][2] = { {0,-1},{1,0},{-1,0} };
695 for( int j
= 0; j
< 3; j
++ )
697 test_dir
= new_dir
[ tile
->state
];
698 tile
->state
= (tile
->state
+1)%3;
700 next
= map_tile_at( (int[2]){ fish
->co
[0]+test_dir
[0], fish
->co
[1]+test_dir
[1] } );
701 if( next
&& (next
->flags
& (CELL_FLAG_WALKABLE
)) )
703 fish
->dir
[0] = test_dir
[0];
704 fish
->dir
[1] = test_dir
[1];
711 next
= map_tile_at( (int[2]){ fish
->co
[0]+fish
->dir
[0], fish
->co
[1]+fish
->dir
[1] } );
712 if( !next
|| (next
&& !(next
->flags
& CELL_FLAG_WALKABLE
)) )
720 vg_info( "Fish died! (%d)\n", i
);
726 fish
->co
[0] += fish
->dir
[0];
727 fish
->co
[1] += fish
->dir
[1];
731 for( int i
= 0; i
< arrlen( map
.io
); i
++ )
733 struct cell
*input
= &map
.cells
[ map
.io
[i
] ];
735 if( input
->flags
& CELL_FLAG_INPUT
)
737 if( input
->state
< arrlen( input
->conditions
) )
739 struct fish
*fish
= &map
.fishes
[ map
.num_fishes
];
740 map_tile_coords_from_index( map
.io
[i
], fish
->co
);
742 int output_dirs
[][2] = { {0,-1}, {-1,0}, {1,0} };
745 for( int i
= 0; i
< vg_list_size( output_dirs
); i
++ )
747 int *dir
= output_dirs
[i
];
748 struct cell
*next
= map_tile_at( (int[2]){ fish
->co
[0]+dir
[0], fish
->co
[1]+dir
[1] } );
749 if( next
&& next
->flags
& CELL_FLAG_CANAL
)
751 fish
->dir
[0] = dir
[0];
752 fish
->dir
[1] = dir
[1];
767 vg_info( "There are now %u active fish\n", map
.num_fishes
);
770 if( vg_get_button_down( "go" ) )
775 vg_info( "Ending!\n" );
780 if( vg_get_button_down( "go" ) )
785 for( int i
= 0; i
< map
.x
*map
.y
; i
++ )
786 map
.cells
[ i
].state
= 0;
788 vg_info( "Starting!\n" );
793 map
.select_valid
= map_tile_availible( (int[2]){ tile_x
, tile_y
} );
795 if( map
.select_valid
)
797 if( vg_get_button_down("primary") )
799 if( map
.selected
->flags
& CELL_FLAG_CANAL
)
801 map
.selected
->flags
&= ~(CELL_FLAG_CANAL
);
805 map
.selected
->flags
|= CELL_FLAG_CANAL
;
818 glViewport( 0,0, vg_window_x
, vg_window_y
);
820 //glEnable( GL_DEPTH_TEST );
821 glClearColor( 0.94f
, 0.94f
, 0.94f
, 1.0f
);
822 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
825 glBindVertexArray( tile_vao );
827 SHADER_USE( colour_shader );
828 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
830 for( int y = 0; y < map.y; y ++ )
832 for( int x = 0; x < map.x; x ++ )
834 glm_mat4_identity( m_mdl );
835 glm_translate( m_mdl,
837 map.origin[0] + (float)x,
839 map.origin[2] + (float)y
842 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
844 struct cell *cell = &map.cells[ y*map.x+x ];
846 vec4 colour = { 0.7f, 0.7f, 0.7f, 1.f };
848 if( cell->flags & CELL_FLAG_INPUT ) glm_vec3_copy( (vec3){ 0.9f,0.5f,0.5f }, colour );
849 else if( cell->flags & CELL_FLAG_OUTPUT ) glm_vec3_copy( (vec3){ 0.5f,0.9f,0.5f }, colour );
850 else if( cell->flags & CELL_FLAG_WALL ) glm_vec3_copy( (vec3){ 0.1f,0.1f,0.1f }, colour );
851 else if( cell->flags & CELL_FLAG_CANAL ) glm_vec3_copy( (vec3){ 0.5f,0.5f,0.8f }, colour );
853 if( cell->flags & CELL_FLAG_SPLIT )
854 glm_vec3_copy( (vec3){ 0.6f, 0.f, 0.9f }, colour );
855 else if( cell->flags & CELL_FLAG_MERGE )
856 glm_vec3_copy( (vec3){ 0.f, 0.6f, 0.8f }, colour );
858 if( map.selected == cell )
860 if( !map.select_valid )
861 glm_vec3_copy( (vec3){ 1.f, 0.f, 0.f }, colour );
863 float flash = sinf( vg_time*2.5f ) * 0.25f + 0.75f;
864 glm_vec3_scale( colour, flash, colour );
867 glUniform4fv( SHADER_UNIFORM( colour_shader, "uColour" ), 1, colour );
868 glDrawArrays( GL_TRIANGLES, 0, 6 );
872 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 1.f, 0.f, 1.f, 1.f );
874 for( int i = 0; i < map.num_fishes; i ++ )
876 struct fish *fish = map.fishes + i;
880 glm_mat4_identity( m_mdl );
881 glm_translate( m_mdl,
883 map.origin[0] + (float)fish->co[0] + 0.5f,
885 map.origin[2] + (float)fish->co[1] + 0.5f
888 glm_scale_uni( m_mdl, 0.2f );
889 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
890 glDrawArrays( GL_TRIANGLES, 0, 6 );
895 glBindVertexArray( map
.tiles_vao
);
899 SHADER_USE( tilemap_shader
);
900 glUniformMatrix4fv( SHADER_UNIFORM( tilemap_shader
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
902 glUniform1i( SHADER_UNIFORM( tilemap_shader
, "uTexTiles" ), 0 );
903 glActiveTexture( GL_TEXTURE0
);
904 glBindTexture( GL_TEXTURE_2D
, map
.tile_texture
);
906 glUniform1i( SHADER_UNIFORM( tilemap_shader
, "uTexRipples" ), 1 );
907 glActiveTexture( GL_TEXTURE1
);
908 glBindTexture( GL_TEXTURE_2D
, map
.flow_texture
);
910 glUniform2f( SHADER_UNIFORM( tilemap_shader
, "uOrigin" ), map
.origin
[0], map
.origin
[2] );
911 glUniform1f( SHADER_UNIFORM( tilemap_shader
, "uTime" ), vg_time
* 0.5f
);
913 glDrawArraysInstanced( GL_TRIANGLES
, 0, 6, map
.x
*map
.y
);
916 void vg_register(void)
918 SHADER_INIT( colour_shader
);
919 SHADER_INIT( tilemap_shader
);
924 glGenVertexArrays( 1, &tile_vao
);
925 glGenBuffers( 1, &tile_vbo
);
929 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
930 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
933 0.0f
, 0.0f
, 0.0f
, 0.0f
936 glBindVertexArray( tile_vao
);
937 glBindBuffer( GL_ARRAY_BUFFER
, tile_vbo
);
946 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
947 glEnableVertexAttribArray( 0 );
951 // Create map buffers
952 glGenVertexArrays( 1, &map
.tiles_vao
);
953 glGenBuffers( 1, &map
.tiles_vbo
);
955 glBindVertexArray( map
.tiles_vao
);
956 glBindBuffer( GL_ARRAY_BUFFER
, map
.tiles_vbo
);
957 glBufferData( GL_ARRAY_BUFFER
,
958 sizeof( quad_mesh
) +
959 sizeof( float )*2 * 1024 +
960 sizeof( u8
)*4 * 1024,
965 glBufferSubData( GL_ARRAY_BUFFER
, 0, sizeof( quad_mesh
), quad_mesh
);
968 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)0 );
969 glEnableVertexAttribArray( 0 );
971 // Offset, data arrays (instancing)
972 glVertexAttribPointer( 1, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)(sizeof(quad_mesh
)) );
973 glEnableVertexAttribArray( 1 );
974 glVertexAttribDivisor( 1, 1 );
976 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_FALSE
, 4, (void*)(sizeof(quad_mesh
)+sizeof(float)*2*1024) );
977 glEnableVertexAttribArray( 2 );
978 glVertexAttribDivisor( 2, 1 );
980 map
.tile_texture
= vg_tex2d_rgba( "textures/rivertiles_flowm.tga" );
984 map
.flow_texture
= vg_tex2d_rgba( "textures/rivertiles_ripple.tga" );
990 "##-#####-##;aaa,aa\n"
997 "##+#####+##;aa,aaa\n"
1005 glDeleteVertexArrays( 1, &tile_vao
);
1006 glDeleteVertexArrays( 1, &map
.tiles_vao
);
1008 glDeleteBuffers( 1, &tile_vbo
);
1009 glDeleteBuffers( 1, &map
.tiles_vbo
);
1011 glDeleteTextures( 1, &map
.tile_texture
);
1012 glDeleteTextures( 1, &map
.flow_texture
);