bb261856b3661e59da76644867c4ec1c69e6f409
[fishladder.git] / fishladder.c
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 //#define VG_STEAM
4 #include "vg/vg.h"
5
6 #define CELL_SHEET_X 256
7 #define CELL_SHEET_Y 256
8
9 SHADER_DEFINE( colour_shader,
10
11 // VERTEX
12 "layout (location=0) in vec2 a_co;"
13 "uniform mat4 uPv;"
14 "uniform mat4 uMdl;"
15 ""
16 "void main()"
17 "{"
18 " vec4 vert_pos = uPv * uMdl * vec4( a_co.x, 0.0, a_co.y, 1.0 );"
19 " gl_Position = vert_pos;"
20 "}",
21
22 // FRAGMENT
23 "out vec4 FragColor;"
24 "uniform vec4 uColour;"
25 ""
26 "void main()"
27 "{"
28 " FragColor = uColour;"
29 "}"
30 ,
31 UNIFORMS({ "uPv", "uMdl", "uColour" })
32 )
33
34 SHADER_DEFINE( tilemap_shader,
35
36 // VERTEX
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
40 "uniform mat4 uPv;"
41 "uniform vec2 uOrigin;"
42 ""
43 "out vec2 aCoords;"
44 ""
45 "void main()"
46 "{"
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 = (a_co*0.98+0.01 + a_data.xy) * 0.125;"
50 "}",
51
52 // FRAGMENT
53 "uniform sampler2D uTexTiles;"
54
55 "in vec2 aCoords;"
56 "out vec4 FragColor;"
57
58 "void main()"
59 "{"
60 "vec4 glyph = texture( uTexTiles, aCoords );"
61 "FragColor = vec4( glyph.xyz, 1.0 );"
62 "}"
63 ,
64 UNIFORMS({ "uPv", "uTexTiles", "uOrigin" })
65 )
66
67 mat4 m_projection;
68 mat4 m_view;
69 mat4 m_mdl;
70
71 int main( int argc, char *argv[] )
72 {
73 vg_init( argc, argv, "FishLadder" );
74 }
75
76 #define CELL_FLAG_INPUT 0x1
77 #define CELL_FLAG_OUTPUT 0x2
78 #define CELL_FLAG_IO (CELL_FLAG_INPUT|CELL_FLAG_OUTPUT)
79 #define CELL_FLAG_WALL 0x4
80 #define CELL_FLAG_HOVER 0x8
81 #define CELL_FLAG_ITER 0x10
82 #define CELL_FLAG_CANAL 0x20
83 #define CELL_FLAG_SPLIT 0x40 /* Does this cell split and have an incoming vertical connection? */
84 #define CELL_FLAG_WALKABLE (CELL_FLAG_IO|CELL_FLAG_CANAL)
85 #define CELL_FLAG_VISITED 0x80
86 #define CELL_FLAG_UPLVL 0x100
87 #define CELL_FLAG_MERGE 0x200
88
89 static struct
90 {
91 u32 x,y;
92
93 struct cell
94 {
95 u32 flags;
96 u32 model_id;
97
98 char *conditions;
99
100 int level;
101 int state;
102 }
103 * cells;
104
105 struct fish
106 {
107 int alive;
108 int co[2];
109 int dir[2];
110 char data;
111 }
112 fishes[ 20 ];
113 int num_fishes;
114
115 vec3 origin;
116 struct cell *selected;
117 int select_valid;
118 int playing;
119 u32 frame;
120
121 u32 *io;
122
123 struct vstack
124 {
125 struct vframe
126 {
127 int x, y;
128 int i;
129 }
130 frames[ 64 ];
131
132 int level;
133 u32 flags;
134 }
135 stack;
136
137 GLuint tile_texture;
138 GLuint flow_texture;
139
140 GLuint tiles_vao;
141 GLuint tiles_vbo;
142 }
143 map;
144
145 static void map_free(void)
146 {
147 for( int i = 0; i < arrlen( map.io ); i ++ )
148 {
149 arrfree( map.cells[ map.io[i] ].conditions );
150 }
151
152 arrfree( map.cells );
153 arrfree( map.io );
154 map.x = 0;
155 map.y = 0;
156 map.cells = NULL;
157 map.io = NULL;
158 }
159
160 static struct cell *map_tile_at( int pos[2] )
161 {
162 if( pos[0] >= 0 && pos[0] < map.x && pos[1] >= 0 && pos[1] < map.y )
163 return map.cells + pos[1]*map.x + pos[0];
164 return NULL;
165 }
166
167 static struct cell *map_tile_at_cond( int pos[2], u32 flags )
168 {
169 struct cell *cell = map_tile_at( pos );
170 if( cell && (cell->flags & flags) )
171 return cell;
172
173 return NULL;
174 }
175
176 static void map_tile_coords_from_index( int i, int coords[2] )
177 {
178 coords[0] = i % map.x;
179 coords[1] = (i - coords[0])/map.x;
180 }
181
182 static void map_stack_refresh(void)
183 {
184 for( int i = 0; i < map.x*map.y; i ++ )
185 map.cells[i].flags &= ~CELL_FLAG_VISITED;
186 }
187
188 static void map_stack_init( int coords[2] )
189 {
190 map.stack.level = 0;
191 map.stack.frames[0].i = 0;
192 map.stack.frames[0].x = coords[0];
193 map.stack.frames[0].y = coords[1];
194 }
195
196 static struct cell *map_stack_next(void)
197 {
198 struct cell *tile = NULL;
199
200 while( !tile )
201 {
202 struct vframe *frame = &map.stack.frames[ map.stack.level ];
203
204 int output_dirs[][2] = { {0,-1}, {-1,0}, {1,0} };
205
206 if( frame->i < 3 )
207 {
208 int *dir = output_dirs[ frame->i ];
209 tile = map_tile_at( (int[2]){frame->x+dir[0], frame->y+dir[1]} );
210 frame->i ++;
211
212 if( tile && !(tile->flags & CELL_FLAG_VISITED) )
213 {
214 map.stack.level ++;
215 frame[1].i = 0;
216 frame[1].x = frame[0].x+dir[0];
217 frame[1].y = frame[0].y+dir[1];
218 }
219 else
220 tile = NULL;
221 }
222 else
223 {
224 map.stack.level --;
225 tile = NULL;
226
227 if( map.stack.level < 0 )
228 return NULL;
229 }
230 }
231
232 return tile;
233 }
234
235 static void map_update_visual(void)
236 {
237 u8 celldata[ 4096 ];
238
239 for( int i = 0; i < map.x*map.y; i ++ )
240 {
241 celldata[i*4+0] = i & 0x7;
242 celldata[i*4+1] = (i & ~0x7) >> 3;
243 }
244
245 glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo );
246 glBufferSubData( GL_ARRAY_BUFFER, 16*sizeof(float) + 1024*2*sizeof(float), map.x*map.y*4, celldata );
247 }
248
249 static int map_load( const char *str )
250 {
251 map_free();
252
253 char *c = str;
254
255 // Scan for width
256 for(;; map.x ++)
257 {
258 if( str[map.x] == ';' )
259 break;
260 else if( !str[map.x] )
261 {
262 vg_error( "Unexpected EOF when parsing level!\n" );
263 return 0;
264 }
265 }
266
267 struct cell *row = arraddnptr( map.cells, map.x );
268 int cx = 0;
269 int reg_start = 0, reg_end = 0;
270
271 for(;;)
272 {
273 if( !*c )
274 break;
275
276 if( *c == ';' )
277 {
278 c ++;
279
280 // Parse attribs
281 if( *c != '\n' )
282 {
283 while( *c )
284 {
285 if( reg_start < reg_end )
286 {
287 if( *c >= 'a' && *c <= 'z' )
288 {
289 arrpush( map.cells[ map.io[ reg_start ] ].conditions, *c );
290 }
291 else
292 {
293 if( *c == ',' || *c == '\n' )
294 {
295 reg_start ++;
296
297 if( *c == '\n' )
298 break;
299 }
300 else
301 {
302 vg_error( "Unkown attrib '%c' (row: %u)\n", *c, map.y );
303 return 0;
304 }
305 }
306 }
307 else
308 {
309 vg_error( "Over-assigned values (row: %u)\n", map.y );
310 return 0;
311 }
312
313 c ++;
314 }
315 }
316
317 if( reg_start != reg_end )
318 {
319 vg_error( "Not enough values assigned (row: %u, %u of %u)\n", map.y, reg_start, reg_end );
320 return 0;
321 }
322
323 if( cx != map.x )
324 {
325 vg_error( "Map row underflow (row: %u, %u<%u)\n", map.y, cx, map.x );
326 return 0;
327 }
328
329 row = arraddnptr( map.cells, map.x );
330 cx = 0;
331 map.y ++;
332 reg_end = reg_start = arrlen( map.io );
333 }
334 else
335 {
336 if( cx == map.x )
337 {
338 vg_error( "Map row overflow (row: %u, %u>%u)\n", map.y, cx, map.x );
339 return 0;
340 }
341
342 row[ cx ].conditions = NULL;
343
344 // Parse the various cell types
345 if( *c == '+' || *c == '-' )
346 {
347 arrpush( map.io, cx + map.y*map.x );
348 row[ cx ++ ].flags = *c == '+'? CELL_FLAG_INPUT: CELL_FLAG_OUTPUT;
349 reg_end ++;
350 }
351 else if( *c == '#' )
352 {
353 row[ cx ++ ].flags = CELL_FLAG_WALL;
354 }
355 else
356 {
357 row[ cx ++ ].flags = 0x00;
358 }
359 }
360
361 c ++;
362 }
363
364 // Origin top left corner
365 map.origin[0] = -((float)map.x) * 0.5f;
366 map.origin[2] = -((float)map.y) * 0.5f;
367
368 float *offset_array = (float *)malloc( map.x*map.y*2*sizeof(float) );
369
370 for( int y = 0; y < map.y; y ++ )
371 {
372 for( int x = 0; x < map.x; x ++ )
373 {
374 float *coord = offset_array + (y*map.x+x)*2;
375 coord[0] = x;
376 coord[1] = y;
377 }
378 }
379
380 glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo );
381 glBufferSubData( GL_ARRAY_BUFFER, 16*sizeof(float), map.x*map.y*2*sizeof(float), offset_array );
382
383 free( offset_array );
384 vg_success( "Map loaded! (%u:%u)\n", map.x, map.y );
385 return 1;
386 }
387
388 static int map_tile_availible( int co[2] )
389 {
390 // Extract 5x5 grid surrounding tile
391 u32 blob = 0x1000;
392 for( int y = vg_max( co[1]-2, 0 ); y < vg_min( map.y, co[1]+3 ); y ++ )
393 for( int x = vg_max( co[0]-2, 0 ); x < vg_min( map.x, co[0]+3 ); x ++ )
394 {
395 struct cell *cell = map_tile_at( (int[2]){ x, y } );
396
397 if( cell && (cell->flags & CELL_FLAG_WALKABLE) )
398 blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
399 }
400
401 // Run filter over center 3x3 grid to check for invalid configurations
402 int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
403 for( int i = 0; i < vg_list_size(kernel); i ++ )
404 {
405 if( blob & (0x1 << (6+kernel[i])) )
406 {
407 // (reference window: 0x1CE7) Illegal moves
408 // 0100011100010 ;
409 // 0000001100011 ;
410 // 0000011000110 ;
411 // 0110001100000 ;
412 // 1100011000000 ;
413 // 0100001100010 ;
414 // 0100011000010 ;
415
416 u32 invalid[] = { 0x8E2, 0x63, 0xC6, 0xC60, 0x18C0, 0x862, 0x8C2 };
417 u32 window = blob >> kernel[i];
418
419 for( int j = 0; j < vg_list_size(invalid); j ++ )
420 if((window & invalid[j]) == invalid[j])
421 return 0;
422 }
423 }
424
425 return 1;
426 }
427
428 void vg_update(void)
429 {
430 // Update camera
431 float ratio = (float)vg_window_y / (float)vg_window_x;
432 float const size = 7.5f;
433 glm_ortho( -size, size, -size*ratio, size*ratio, 0.01f, 150.f, m_projection );
434
435 glm_mat4_identity( m_view );
436 glm_translate_z( m_view, -10.f );
437 glm_rotate_x( m_view, 1.5708f, m_view );
438
439 glm_mat4_mul( m_projection, m_view, vg_pv );
440
441 // Compute map update
442 for( int y = 0; y < map.y; y ++ )
443 {
444 for( int x = 0; x < map.x; x ++ )
445 {
446 struct cell *tile, *upper, *lower, *l, *r;
447 tile = map_tile_at( (int [2]){ x, y } );
448 tile->flags &= ~(CELL_FLAG_SPLIT|CELL_FLAG_MERGE|CELL_FLAG_UPLVL);
449
450 if( tile->flags & CELL_FLAG_WALKABLE )
451 {
452 r = map_tile_at_cond( (int[2]){ x+1, y }, CELL_FLAG_WALKABLE );
453 l = map_tile_at_cond( (int[2]){ x-1, y }, CELL_FLAG_WALKABLE );
454
455 if( r && l )
456 {
457 upper = map_tile_at_cond( (int[2]){ x, y-1 }, CELL_FLAG_WALKABLE );
458 lower = map_tile_at_cond( (int[2]){ x, y+1 }, CELL_FLAG_WALKABLE );
459
460 if( upper )
461 {
462 tile->flags |= CELL_FLAG_MERGE | CELL_FLAG_UPLVL;
463 }
464
465 if( lower )
466 {
467 tile->flags |= CELL_FLAG_SPLIT;
468 l->flags |= CELL_FLAG_UPLVL;
469 r->flags |= CELL_FLAG_UPLVL;
470 }
471 }
472 }
473 }
474 }
475
476 // Compute classification
477 /*
478 map_stack_refresh();
479
480 for( int i = 0; i < arrlen( map.io ); i ++ )
481 {
482 struct *cell cell = &map.cells[ map.io ];
483 int coords[2];
484
485 if( cell->flags & CELL_FLAG_INPUT )
486 {
487 map_tile_coords_from_index( map.io, coords );
488 map_stack_init( coords );
489
490 do
491 {
492 if( cell->flags & CELL_FLAG_CONNECTOR )
493 {
494
495 }
496 }
497 while( (cell = map_stack_next()) );
498 }
499 }*/
500
501 // Get mouse ray
502 vec3 ray_origin;
503 vec3 ray_dir;
504
505 mat4 pv_inverse;
506 vec4 vp = { 0.f, 0.f, vg_window_x, vg_window_y };
507 glm_mat4_inv( vg_pv, pv_inverse );
508 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, -1.f }, pv_inverse, vp, ray_dir );
509 glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, 0.f }, pv_inverse, vp, ray_origin );
510 glm_vec3_sub( ray_dir, ray_origin, ray_dir );
511
512 // Get floor tile intersection
513 float ray_t = -ray_origin[1] / ray_dir[1];
514
515 vec3 tile_pos;
516 glm_vec3_copy( ray_origin, tile_pos );
517 glm_vec3_muladds( ray_dir, ray_t, tile_pos );
518 glm_vec3_sub( tile_pos, map.origin, tile_pos );
519
520 int tile_x = floorf( tile_pos[0] );
521 int tile_y = floorf( tile_pos[2] );
522
523 map.selected = map_tile_at( (int [2]){tile_x, tile_y} );
524
525 if( map.playing )
526 {
527 static int fish_counter = 0;
528 fish_counter ++;
529
530 if( fish_counter > 20 )
531 {
532 fish_counter = 0;
533
534 // Advance characters
535 for( int i = 0; i < map.num_fishes; i ++ )
536 {
537 struct fish *fish = map.fishes + i;
538
539 if( !fish->alive )
540 continue;
541
542 struct cell *tile, *next;
543 tile = map_tile_at( fish->co );
544
545 if( tile->flags & CELL_FLAG_OUTPUT )
546 {
547 vg_info( "Fish got zucced (%d)\n", i );
548 fish->alive = 0;
549 continue;
550 }
551
552 int die = 0;
553 if( tile->flags & CELL_FLAG_SPLIT )
554 {
555 die = 1;
556 int new_dir[][2] = { {0,-1},{1,0},{-1,0} };
557 int *test_dir;
558
559 for( int j = 0; j < 3; j ++ )
560 {
561 test_dir = new_dir[ tile->state ];
562 tile->state = (tile->state+1)%3;
563
564 next = map_tile_at( (int[2]){ fish->co[0]+test_dir[0], fish->co[1]+test_dir[1] } );
565 if( next && (next->flags & (CELL_FLAG_WALKABLE)) )
566 {
567 fish->dir[0] = test_dir[0];
568 fish->dir[1] = test_dir[1];
569 die = 0;
570 break;
571 }
572 }
573 }
574
575 next = map_tile_at( (int[2]){ fish->co[0]+fish->dir[0], fish->co[1]+fish->dir[1] } );
576 if( !next || (next && !(next->flags & CELL_FLAG_WALKABLE)) )
577 {
578 // Try UP
579 die = 1;
580 }
581
582 if( die )
583 {
584 vg_info( "Fish died! (%d)\n", i );
585 fish->alive = 0;
586 continue;
587 }
588
589
590 fish->co[0] += fish->dir[0];
591 fish->co[1] += fish->dir[1];
592 }
593
594 // Try spawn fish
595 for( int i = 0; i < arrlen( map.io ); i ++ )
596 {
597 struct cell *input = &map.cells[ map.io[i] ];
598
599 if( input->flags & CELL_FLAG_INPUT )
600 {
601 if( input->state < arrlen( input->conditions ) )
602 {
603 struct fish *fish = &map.fishes[ map.num_fishes ];
604 map_tile_coords_from_index( map.io[i], fish->co );
605
606 int output_dirs[][2] = { {0,-1}, {-1,0}, {1,0} };
607 int can_spawn = 0;
608
609 for( int i = 0; i < vg_list_size( output_dirs ); i ++ )
610 {
611 int *dir = output_dirs[i];
612 struct cell *next = map_tile_at( (int[2]){ fish->co[0]+dir[0], fish->co[1]+dir[1] } );
613 if( next && next->flags & CELL_FLAG_CANAL )
614 {
615 fish->dir[0] = dir[0];
616 fish->dir[1] = dir[1];
617 can_spawn = 1;
618 }
619 }
620
621 if( can_spawn )
622 {
623 fish->alive = 1;
624 input->state ++;
625 map.num_fishes ++;
626 }
627 }
628 }
629 }
630
631 vg_info( "There are now %u active fish\n", map.num_fishes );
632 }
633
634 if( vg_get_button_down( "go" ) )
635 {
636 map.playing = 0;
637 map.num_fishes = 0;
638
639 vg_info( "Ending!\n" );
640 }
641 }
642 else
643 {
644 if( vg_get_button_down( "go" ) )
645 {
646 map.playing = 1;
647
648 // Reset everything
649 for( int i = 0; i < map.x*map.y; i ++ )
650 map.cells[ i ].state = 0;
651
652 vg_info( "Starting!\n" );
653 }
654
655 if( map.selected )
656 {
657 map.select_valid = map_tile_availible( (int[2]){ tile_x, tile_y } );
658
659 if( map.select_valid )
660 {
661 if( vg_get_button_down("primary") )
662 {
663 if( map.selected->flags & CELL_FLAG_CANAL )
664 {
665 map.selected->flags &= ~(CELL_FLAG_CANAL);
666 }
667 else
668 {
669 map.selected->flags |= CELL_FLAG_CANAL;
670 }
671 }
672 }
673 }
674 }
675 }
676
677 GLuint tile_vao;
678 GLuint tile_vbo;
679
680 void vg_render(void)
681 {
682 glViewport( 0,0, vg_window_x, vg_window_y );
683
684 //glEnable( GL_DEPTH_TEST );
685 glClearColor( 0.94f, 0.94f, 0.94f, 1.0f );
686 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
687
688 /*
689 glBindVertexArray( tile_vao );
690
691 SHADER_USE( colour_shader );
692 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
693
694 for( int y = 0; y < map.y; y ++ )
695 {
696 for( int x = 0; x < map.x; x ++ )
697 {
698 glm_mat4_identity( m_mdl );
699 glm_translate( m_mdl,
700 (vec3){
701 map.origin[0] + (float)x,
702 0.f,
703 map.origin[2] + (float)y
704 }
705 );
706 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
707
708 struct cell *cell = &map.cells[ y*map.x+x ];
709
710 vec4 colour = { 0.7f, 0.7f, 0.7f, 1.f };
711
712 if( cell->flags & CELL_FLAG_INPUT ) glm_vec3_copy( (vec3){ 0.9f,0.5f,0.5f }, colour );
713 else if( cell->flags & CELL_FLAG_OUTPUT ) glm_vec3_copy( (vec3){ 0.5f,0.9f,0.5f }, colour );
714 else if( cell->flags & CELL_FLAG_WALL ) glm_vec3_copy( (vec3){ 0.1f,0.1f,0.1f }, colour );
715 else if( cell->flags & CELL_FLAG_CANAL ) glm_vec3_copy( (vec3){ 0.5f,0.5f,0.8f }, colour );
716
717 if( cell->flags & CELL_FLAG_SPLIT )
718 glm_vec3_copy( (vec3){ 0.6f, 0.f, 0.9f }, colour );
719 else if( cell->flags & CELL_FLAG_MERGE )
720 glm_vec3_copy( (vec3){ 0.f, 0.6f, 0.8f }, colour );
721
722 if( map.selected == cell )
723 {
724 if( !map.select_valid )
725 glm_vec3_copy( (vec3){ 1.f, 0.f, 0.f }, colour );
726
727 float flash = sinf( vg_time*2.5f ) * 0.25f + 0.75f;
728 glm_vec3_scale( colour, flash, colour );
729 }
730
731 glUniform4fv( SHADER_UNIFORM( colour_shader, "uColour" ), 1, colour );
732 glDrawArrays( GL_TRIANGLES, 0, 6 );
733 }
734 }
735
736 glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 1.f, 0.f, 1.f, 1.f );
737
738 for( int i = 0; i < map.num_fishes; i ++ )
739 {
740 struct fish *fish = map.fishes + i;
741
742 if( fish->alive )
743 {
744 glm_mat4_identity( m_mdl );
745 glm_translate( m_mdl,
746 (vec3){
747 map.origin[0] + (float)fish->co[0] + 0.5f,
748 0.1f,
749 map.origin[2] + (float)fish->co[1] + 0.5f
750 }
751 );
752 glm_scale_uni( m_mdl, 0.2f );
753 glUniformMatrix4fv( SHADER_UNIFORM( colour_shader, "uMdl" ), 1, GL_FALSE, (float *)m_mdl );
754 glDrawArrays( GL_TRIANGLES, 0, 6 );
755 }
756 }
757 */
758
759 glBindVertexArray( map.tiles_vao );
760
761 map_update_visual();
762
763 SHADER_USE( tilemap_shader );
764 glUniformMatrix4fv( SHADER_UNIFORM( tilemap_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
765
766 glUniform1i( SHADER_UNIFORM( tilemap_shader, "uTexTiles" ), 0 );
767 glActiveTexture( GL_TEXTURE0 );
768 glBindTexture( GL_TEXTURE_2D, map.tile_texture );
769
770 glUniform2f( SHADER_UNIFORM( tilemap_shader, "uOrigin" ), map.origin[0], map.origin[2] );
771
772 glDrawArraysInstanced( GL_TRIANGLES, 0, 6, map.x*map.y );
773 }
774
775 void vg_register(void)
776 {
777 SHADER_INIT( colour_shader );
778 SHADER_INIT( tilemap_shader );
779 }
780
781 void vg_start(void)
782 {
783 glGenVertexArrays( 1, &tile_vao );
784 glGenBuffers( 1, &tile_vbo );
785
786 float quad_mesh[] =
787 {
788 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
789 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
790
791 // Padding
792 0.0f, 0.0f, 0.0f, 0.0f
793 };
794
795 glBindVertexArray( tile_vao );
796 glBindBuffer( GL_ARRAY_BUFFER, tile_vbo );
797 glBufferData
798 (
799 GL_ARRAY_BUFFER,
800 sizeof( quad_mesh ),
801 quad_mesh,
802 GL_STATIC_DRAW
803 );
804
805 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0 );
806 glEnableVertexAttribArray( 0 );
807
808 VG_CHECK_GL();
809
810 // Create map buffers
811 glGenVertexArrays( 1, &map.tiles_vao );
812 glGenBuffers( 1, &map.tiles_vbo );
813
814 glBindVertexArray( map.tiles_vao );
815 glBindBuffer( GL_ARRAY_BUFFER, map.tiles_vbo );
816 glBufferData( GL_ARRAY_BUFFER,
817 sizeof( quad_mesh ) +
818 sizeof( float )*2 * 1024 +
819 sizeof( u8 )*4 * 1024,
820 NULL,
821 GL_DYNAMIC_DRAW
822 );
823
824 glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof( quad_mesh ), quad_mesh );
825
826 // Base quad
827 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)0 );
828 glEnableVertexAttribArray( 0 );
829
830 // Offset, data arrays (instancing)
831 glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)(sizeof(quad_mesh)) );
832 glEnableVertexAttribArray( 1 );
833 glVertexAttribDivisor( 1, 1 );
834
835 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_FALSE, 4, (void*)(sizeof(quad_mesh)+sizeof(float)*2*1024) );
836 glEnableVertexAttribArray( 2 );
837 glVertexAttribDivisor( 2, 1 );
838
839 map.tile_texture = vg_tex2d_rgba( "textures/rivertiles_flowm.tga" );
840 vg_tex2d_nearest();
841 vg_tex2d_repeat();
842
843 map.flow_texture = vg_tex2d_rgba( "textures/rivertiles_ripple.tga" );
844
845
846 map_load
847 (
848 "#####-#####;aa\n"
849 "# #;\n"
850 "# #;\n"
851 "# -;bb\n"
852 "# #;\n"
853 "# #;\n"
854 "#####+#####;abab\n"
855 );
856 }
857
858 void vg_free(void)
859 {
860 map_free();
861
862 glDeleteVertexArrays( 1, &tile_vao );
863 glDeleteVertexArrays( 1, &map.tiles_vao );
864
865 glDeleteBuffers( 1, &tile_vbo );
866 glDeleteBuffers( 1, &map.tiles_vbo );
867
868 glDeleteTextures( 1, &map.tile_texture );
869 glDeleteTextures( 1, &map.flow_texture );
870 }
871
872 void vg_ui(void)
873 {
874
875 }