1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
5 #include "fishladder_resources.h"
7 const char *level_pack_1
[] = {
32 levels
[ vg_list_size( level_pack_1
) ];
34 career
= { .version
= 1 };
37 static void career_serialize(void)
39 vg_asset_write( "sav/game.sav", &career
, sizeof( struct career_state
) );
42 static void career_load(void)
45 struct career_state
*cr
= vg_asset_read_s( "sav/game.sav", &sz
);
47 memset( (void*)career
.levels
, 0, vg_list_size(level_pack_1
) * sizeof(struct career_level
) );
51 if( sz
> sizeof( struct career_state
) )
52 vg_warn( "This save file is too big! Some levels will be lost\n" );
54 if( sz
<= offsetof( struct career_state
, levels
) )
56 vg_error( "This save file is too small to have a header\n" );
61 u32
const size_header
= offsetof(struct career_state
, levels
);
62 u32
const size_levels
= sizeof(struct career_state
)-size_header
;
63 u32
const size_levels_input
= sz
- size_header
;
65 memcpy( (void*)career
.levels
, (void*)cr
->levels
, size_levels_input
);
67 if( sz
< sizeof( struct career_state
) )
69 memset( ((void*)career
.levels
) + size_levels_input
, 0, size_levels
-size_levels_input
);
73 vg_success( "Loaded save file... Info:\n" );
75 for( int i
= 0; i
< vg_list_size( career
.levels
); i
++ )
77 struct career_level
*lvl
= &career
.levels
[i
];
78 vg_info( "Score: %u, Time: %u, Completed: %u\n", lvl
->score
, lvl
->time
, lvl
->completed
);
83 vg_info( "No save file... Using blank one\n" );
91 #define FLAG_CANAL 0x1
92 #define FLAG_IS_TRIGGER 0x2
93 #define FLAG_RESERVED0 0x4
94 #define FLAG_RESERVED1 0x8
96 #define FLAG_INPUT 0x10
97 #define FLAG_OUTPUT 0x20
98 #define FLAG_WALL 0x40
100 #define FLAG_FLIP_FLOP 0x100
101 #define FLAG_TRIGGERED 0x200
102 #define FLAG_FLIP_ROTATING 0x400
103 #define FLAG_TARGETED 0x800
106 0000 0 | 0001 1 | 0010 2 | 0011 3
110 0100 4 | 0101 5 | 0110 6 | 0111 7
114 1000 8 | 1001 9 | 1010 10 | 1011 11
118 1100 12 | 1101 13 | 1110 14 | 1111 15
126 k_cell_type_ramp_right
= 3,
127 k_cell_type_ramp_left
= 6,
128 k_cell_type_split
= 7,
129 k_cell_type_merge
= 13,
130 k_cell_type_con_r
= 1,
131 k_cell_type_con_u
= 2,
132 k_cell_type_con_l
= 4,
133 k_cell_type_con_d
= 8
137 { { 0.9f
, 0.6f
, 0.20f
},
138 { 0.2f
, 0.9f
, 0.14f
},
139 { 0.4f
, 0.8f
, 1.00f
} };
141 static void colour_code_v3( char cc
, v3f target
)
143 if( cc
>= 'a' && cc
<= 'z' )
147 if( id
< vg_list_size( colour_sets
) )
149 v3_copy( colour_sets
[ id
], target
);
154 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
163 static void init_mesh( struct mesh
*m
, float const *tris
, u32 length
)
165 m
->elements
= length
/3;
166 glGenVertexArrays( 1, &m
->vao
);
167 glGenBuffers( 1, &m
->vbo
);
169 glBindVertexArray( m
->vao
);
170 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
171 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
173 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
174 glEnableVertexAttribArray( 0 );
179 static void free_mesh( struct mesh
*m
)
181 glDeleteVertexArrays( 1, &m
->vao
);
182 glDeleteBuffers( 1, &m
->vbo
);
185 static void draw_mesh( int const start
, int const count
)
187 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
190 static void use_mesh( struct mesh
*m
)
192 glBindVertexArray( m
->vao
);
197 k_fish_state_soon_dead
= -1,
198 k_fish_state_dead
= 0,
223 int sim_run
, max_runs
;
237 int condition_count
, recv_count
;
249 struct mesh tile
, circle
, numbers
;
252 GLuint vao
, vbo
, ebo
;
257 GLuint background_data
;
258 GLuint random_samples
;
260 int selected
, tile_x
, tile_y
;
267 enum e_fish_state state
;
278 struct career_level
*ptr_career_level
;
286 static void map_free(void)
288 arrfree( world
.data
);
299 world
.initialzed
= 0;
302 static void io_reset(void)
304 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
306 struct cell_terminal
*term
= &world
.io
[i
];
308 for( int j
= 0; j
< term
->run_count
; j
++ )
309 term
->runs
[j
].recv_count
= 0;
313 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
314 static int map_load( const char *str
, const char *name
)
316 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
325 if( str
[world
.w
] == ';' )
327 else if( !str
[world
.w
] )
329 vg_error( "Unexpected EOF when parsing level\n" );
334 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
336 int reg_start
= 0, reg_end
= 0;
338 u32
*links_to_make
= NULL
;
339 int links_satisfied
= 0;
341 char link_id_buffer
[32];
349 if( *c
== '\r' ) { c
++; continue; } // fuck off windows
355 if( *c
== '\r' ) c
++;
362 if( *c
== '\r' ) { c
++; continue; }
364 if( reg_start
< reg_end
)
366 struct cell_terminal
*terminal
= &world
.io
[ reg_start
];
367 struct terminal_run
*run
= &terminal
->runs
[ terminal
->run_count
-1 ];
369 if( *c
>= 'a' && *c
<= 'z' )
371 run
->conditions
[ run
->condition_count
++ ] = *c
;
375 if( *c
== ',' || *c
== '\n' )
384 terminal
->runs
[ terminal
->run_count
].condition_count
= 0;
385 terminal
->run_count
++;
386 world
.max_runs
= vg_max( world
.max_runs
, terminal
->run_count
);
390 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
397 if( links_satisfied
< arrlen( links_to_make
) )
399 struct cell
*target
= &world
.data
[ links_to_make
[ links_satisfied
] ];
401 if( (((u32
)*c
>= (u32
)'0') && ((u32
)*c
<= (u32
)'9')) || *c
== '-' )
403 if( link_id_n
>= vg_list_size( link_id_buffer
)-1 )
405 vg_error( "Number was way too long to be parsed (row: %u)\n", world
.h
);
409 link_id_buffer
[ link_id_n
++ ] = *c
;
411 else if( *c
== ',' || *c
== '\n' )
413 link_id_buffer
[ link_id_n
] = 0x00;
414 int value
= atoi( link_id_buffer
);
416 target
->links
[value
>= 0? 1:0] = abs(value
);
425 vg_error( "Invalid character '%c' (row: %u)\n", *c
, world
.h
);
431 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
440 // Registry length-error checks
441 if( reg_start
!= reg_end
)
443 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
447 if( links_satisfied
!= arrlen( links_to_make
) )
449 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world
.h
, links_satisfied
, arrlen( links_to_make
) );
455 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
459 row
= arraddnptr( world
.data
, world
.w
);
462 reg_end
= reg_start
= arrlen( world
.io
);
464 arrsetlen( links_to_make
, 0 );
471 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
475 // Tile initialization
477 struct cell
*cell
= &row
[ cx
];
480 if( *c
== '+' || *c
== '-' )
482 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
483 term
->id
= cx
+ world
.h
*world
.w
;
485 term
->runs
[0].condition_count
= 0;
487 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
490 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
491 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
493 // Canal flag bits (4bit/16 value):
499 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
501 if( cell
->state
& FLAG_IS_TRIGGER
)
502 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
508 else cell
->state
= 0x00;
516 // Update data texture to fill out the background
518 u8 info_buffer
[64*64*4];
519 for( int i
= 0; i
< 64*64; i
++ )
521 u8
*px
= &info_buffer
[i
*4];
528 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
529 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
532 arrfree( links_to_make
);
534 map_reclassify( NULL
, NULL
, 1 );
537 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
539 struct cell
*src
= &world
.data
[i
];
540 if( src
->state
& FLAG_IS_TRIGGER
)
542 int link_id
= src
->links
[0]?0:1;
543 if( src
->links
[link_id
] <= world
.h
*world
.w
)
545 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
546 if( (target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
) )
548 if( target
->links
[ link_id
] )
550 vg_error( "Link target was already targeted\n" );
556 target
->links
[ link_id
] = i
;
557 target
->state
|= FLAG_TARGETED
;
562 vg_error( "Link target was invalid\n" );
568 vg_error( "Link target out of bounds\n" );
574 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
578 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
579 world
.initialzed
= 1;
583 arrfree( links_to_make
);
588 static struct cell
*pcell( v2i pos
)
590 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
593 static void map_serialize( FILE *stream
)
595 for( int y
= 0; y
< world
.h
; y
++ )
597 for( int x
= 0; x
< world
.w
; x
++ )
599 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
601 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
602 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
603 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
604 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
606 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
608 else fputc( ' ', stream
);
611 fputc( ';', stream
);
613 int terminal_write_count
= 0;
615 for( int x
= 0; x
< world
.w
; x
++ )
617 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
619 struct cell_terminal
*term
= &world
.io
[i
];
620 if( term
->id
== y
*world
.w
+x
)
622 if( terminal_write_count
)
623 fputc( ',', stream
);
624 terminal_write_count
++;
626 for( int j
= 0; j
< term
->run_count
; j
++ )
628 struct terminal_run
*run
= &term
->runs
[j
];
630 for( int k
= 0; k
< run
->condition_count
; k
++ )
631 fputc( run
->conditions
[k
], stream
);
633 if( j
< term
->run_count
-1 )
634 fputc( ':', stream
);
640 for( int x
= 0; x
< world
.w
; x
++ )
642 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
643 if( cell
->state
& FLAG_IS_TRIGGER
)
645 if( terminal_write_count
)
646 fputc( ',', stream
);
647 terminal_write_count
++;
649 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
653 fputc( '\n', stream
);
657 int main( int argc
, char *argv
[] )
659 vg_init( argc
, argv
, "Marble Computing | SPACE: Test | LeftClick: Toggle tile | RightClick: Drag wire" );
662 static int console_credits( int argc
, char const *argv
[] )
664 vg_info( "Aknowledgements:\n" );
665 vg_info( " GLFW zlib/libpng glfw.org\n" );
666 vg_info( " miniaudio MIT0 miniaud.io\n" );
667 vg_info( " QOI MIT phoboslab.org\n" );
668 vg_info( " STB library MIT nothings.org\n" );
669 vg_info( " Weiholmir font justfredrik.itch.io\n" );
673 static int console_save_map( int argc
, char const *argv
[] )
675 if( !world
.initialzed
)
677 vg_error( "Tried to save uninitialized map!\n" );
681 char map_path
[ 256 ];
683 strcpy( map_path
, "sav/" );
684 strcat( map_path
, world
.map_name
);
685 strcat( map_path
, ".map" );
687 FILE *test_writer
= fopen( map_path
, "wb" );
690 vg_info( "Saving map to '%s'\n", map_path
);
691 map_serialize( test_writer
);
693 fclose( test_writer
);
698 vg_error( "Unable to open stream for writing\n" );
703 static int console_load_map( int argc
, char const *argv
[] )
705 char map_path
[ 256 ];
710 strcpy( map_path
, "sav/" );
711 strcat( map_path
, argv
[0] );
712 strcat( map_path
, ".map" );
714 char *text_source
= vg_textasset_read( map_path
);
718 strcpy( map_path
, "maps/" );
719 strcat( map_path
, argv
[0] );
720 strcat( map_path
, ".map" );
722 text_source
= vg_textasset_read( map_path
);
727 vg_info( "Loading map: '%s'\n", map_path
);
728 world
.ptr_career_level
= NULL
;
730 if( !map_load( text_source
, argv
[0] ) )
738 for( int i
= 0; i
< vg_list_size( level_pack_1
); i
++ )
740 if( !strcmp( level_pack_1
[i
], argv
[0] ) )
742 world
.ptr_career_level
= career
.levels
+ i
;
751 vg_error( "Missing maps '%s'\n", argv
[0] );
757 vg_error( "Missing argument <map_path>\n" );
762 static void simulation_stop(void)
764 world
.simulating
= 0;
765 world
.num_fishes
= 0;
770 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
772 vg_info( "Stopping simulation!\n" );
775 static int console_changelevel( int argc
, char const *argv
[] )
779 // Save current level
780 console_save_map( 0, NULL
);
781 if( console_load_map( argc
, argv
) )
789 vg_error( "Missing argument <map_path>\n" );
797 vg_function_push( (struct vg_cmd
){
798 .name
= "_map_write",
799 .function
= console_save_map
802 vg_function_push( (struct vg_cmd
){
804 .function
= console_load_map
807 vg_function_push( (struct vg_cmd
){
809 .function
= console_changelevel
812 vg_function_push( (struct vg_cmd
){
814 .function
= console_credits
821 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
822 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
824 0.0f
, 0.0f
, 0.0f
, 1.0f
, 4.0f
, 1.0f
,
825 0.0f
, 0.0f
, 4.0f
, 1.0f
, 4.0f
, 0.0f
828 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
833 float circle_mesh
[32*6*3];
834 int res
= vg_list_size( circle_mesh
) / (6*3);
836 for( int i
= 0; i
< res
; i
++ )
838 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
839 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
841 circle_mesh
[ i
*6+0 ] = 0.0f
;
842 circle_mesh
[ i
*6+1 ] = 0.0f
;
844 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
845 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
846 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
848 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
849 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
850 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
852 v2_copy( v0
, circle_mesh
+ i
*6+4 );
853 v2_copy( v1
, circle_mesh
+ i
*6+2 );
854 v2_copy( v0
, circle_mesh
+i
*6+4 );
855 v2_copy( v1
, circle_mesh
+i
*6+2 );
858 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
863 init_mesh( &world
.numbers
,
865 vg_list_size( MESH_NUMBERS_BUFFER
)
868 for( int i
= 0; i
< 10; i
++ )
870 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS
[i
][0], MESH_NUMBERS_OFFSETS
[i
][1] );
876 int const num_segments
= 64;
878 struct mesh_wire
*mw
= &world
.wire
;
880 v2f wire_points
[ num_segments
* 2 ];
881 u16 wire_indices
[ 6*(num_segments
-1) ];
883 for( int i
= 0; i
< num_segments
; i
++ )
885 float l
= (float)i
/ (float)(num_segments
-1);
887 v2_copy( (v2f
){ l
, -0.5f
}, wire_points
[i
*2+0] );
888 v2_copy( (v2f
){ l
, 0.5f
}, wire_points
[i
*2+1] );
890 if( i
< num_segments
-1 )
892 wire_indices
[ i
*6+0 ] = i
*2 + 0;
893 wire_indices
[ i
*6+1 ] = i
*2 + 1;
894 wire_indices
[ i
*6+2 ] = i
*2 + 3;
895 wire_indices
[ i
*6+3 ] = i
*2 + 0;
896 wire_indices
[ i
*6+4 ] = i
*2 + 3;
897 wire_indices
[ i
*6+5 ] = i
*2 + 2;
901 glGenVertexArrays( 1, &mw
->vao
);
902 glGenBuffers( 1, &mw
->vbo
);
903 glGenBuffers( 1, &mw
->ebo
);
904 glBindVertexArray( mw
->vao
);
906 glBindBuffer( GL_ARRAY_BUFFER
, mw
->vbo
);
908 glBufferData( GL_ARRAY_BUFFER
, sizeof( wire_points
), wire_points
, GL_STATIC_DRAW
);
909 glBindVertexArray( mw
->vao
);
911 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mw
->ebo
);
912 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, sizeof( wire_indices
), wire_indices
, GL_STATIC_DRAW
);
915 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)0 );
916 glEnableVertexAttribArray( 0 );
920 mw
->em
= vg_list_size( wire_indices
);
923 // Create info data texture
925 glGenTextures( 1, &world
.background_data
);
926 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
927 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
931 // Create random smaples texture
933 u8
*data
= malloc(512*512*2);
934 for( int i
= 0; i
< 512*512*2; i
++ )
935 data
[ i
] = rand()/(RAND_MAX
/255);
937 glGenTextures( 1, &world
.random_samples
);
938 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
939 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
946 resource_load_main();
950 console_load_map( 1, level_pack_1
);
955 console_save_map( 0, NULL
);
958 resource_free_main();
960 glDeleteTextures( 1, &world
.background_data
);
961 glDeleteTextures( 1, &world
.random_samples
);
963 glDeleteVertexArrays( 1, &world
.wire
.vao
);
964 glDeleteBuffers( 1, &world
.wire
.vbo
);
965 glDeleteBuffers( 1, &world
.wire
.ebo
);
967 free_mesh( &world
.tile
);
968 free_mesh( &world
.circle
);
969 free_mesh( &world
.numbers
);
974 static int world_check_pos_ok( v2i co
)
976 return (co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2)? 0: 1;
979 static int cell_interactive( v2i co
)
982 if( !world_check_pos_ok( co
) )
986 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
989 // List of 3x3 configurations that we do not allow
990 static u32 invalid_src
[][9] =
1022 // Statically compile invalid configurations into bitmasks
1023 static u32 invalid
[ vg_list_size(invalid_src
) ];
1025 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
1029 for( int j
= 0; j
< 3; j
++ )
1030 for( int k
= 0; k
< 3; k
++ )
1031 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
1033 invalid
[i
] = comped
;
1036 // Extract 5x5 grid surrounding tile
1038 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
1039 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
1041 struct cell
*cell
= pcell((v2i
){x
,y
});
1043 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
1044 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
1047 // Run filter over center 3x3 grid to check for invalid configurations
1048 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1049 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
1051 if( blob
& (0x1 << (6+kernel
[i
])) )
1053 u32 window
= blob
>> kernel
[i
];
1055 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
1056 if((window
& invalid
[j
]) == invalid
[j
])
1064 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
1066 v2i full_start
= { 1,1 };
1067 v2i full_end
= { world
.w
-1, world
.h
-1 };
1069 if( !start
|| !end
)
1076 u8 info_buffer
[64*64*4];
1079 int px0
= vg_max( start
[0], full_start
[0] ),
1080 px1
= vg_min( end
[0], full_end
[0] ),
1081 py0
= vg_max( start
[1], full_start
[1] ),
1082 py1
= vg_min( end
[1], full_end
[1] );
1084 for( int y
= py0
; y
< py1
; y
++ )
1086 for( int x
= px0
; x
< px1
; x
++ )
1088 struct cell
*cell
= pcell((v2i
){x
,y
});
1090 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
1095 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1097 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
1099 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
1100 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1108 if( cell
->state
& FLAG_WALL
)
1114 pcell((v2i
){x
,y
})->config
= config
;
1116 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
1117 info_px
[0] = height
;
1118 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
1124 ((cell
->state
& FLAG_IS_TRIGGER
) && (cell
->config
== 0xF || cell
->config
== k_cell_type_split
)) ||
1125 ((cell
->state
& FLAG_TARGETED
) && (cell
->config
!= k_cell_type_split
))
1126 ) && update_texbuffer
1128 cell
->state
&= ~(FLAG_TARGETED
|FLAG_IS_TRIGGER
);
1129 for( u32 i
= 0; i
< 2; i
++ )
1131 if( cell
->links
[i
] )
1133 struct cell
*other_ptr
= &world
.data
[ cell
->links
[i
] ];
1134 other_ptr
->links
[ i
] = 0;
1135 other_ptr
->state
&= ~FLAG_IS_TRIGGER
;
1137 if( other_ptr
->links
[ i
^ 0x1 ] == 0 )
1138 other_ptr
->state
&= ~FLAG_TARGETED
;
1148 if( update_texbuffer
)
1150 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1151 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
1156 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1157 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
1158 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1159 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
1161 v2f
const curve_1
[] = {{1.0f
,0.5f
},{0.8f
,0.5f
},{0.3f
,0.5f
},{0.2f
,0.5f
}};
1162 v2f
const curve_4
[] = {{0.0f
,0.5f
},{0.3f
,0.5f
},{0.5f
,0.5f
},{0.8f
,0.5f
}};
1163 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
1164 v2f
const curve_8
[] = {{0.5f
,0.8f
},{0.5f
,0.5f
},{0.5f
,0.3f
},{0.5f
,0.0f
}};
1166 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
1167 v2f
const curve_7_1
[] = {{0.5f
,0.8438f
},{1.0f
-0.875f
,0.8438f
},{1.0-0.625f
,0.5f
},{0.0f
,0.5f
}};
1169 float const curve_7_linear_section
= 0.1562f
;
1171 u16 id_drag_from
= 0;
1175 void vg_update(void)
1177 // Fit within screen
1179 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
1180 r2
= (float)world
.h
/ (float)world
.w
,
1183 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
1184 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
1187 origin
[0] = floorf( -0.5f
* world
.w
);
1188 origin
[1] = floorf( -0.5f
* world
.h
);
1191 m3x3_identity( m_view
);
1192 m3x3_translate( m_view
, origin
);
1193 m3x3_mul( m_projection
, m_view
, vg_pv
);
1194 vg_projection_update();
1197 v2_copy( vg_mouse_ws
, world
.tile_pos
);
1199 world
.tile_x
= floorf( world
.tile_pos
[0] );
1200 world
.tile_y
= floorf( world
.tile_pos
[1] );
1203 if( !world
.simulating
)
1205 v2_copy( vg_mouse_ws
, drag_to_co
);
1207 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
1209 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
1211 static u32 modify_state
= 0;
1213 struct cell
*cell_ptr
= &world
.data
[world
.selected
];
1215 if( vg_get_button_down("primary") )
1217 modify_state
= (cell_ptr
->state
& FLAG_CANAL
) ^ FLAG_CANAL
;
1220 if( vg_get_button("primary") && ((cell_ptr
->state
& FLAG_CANAL
) != modify_state
) )
1222 cell_ptr
->state
&= ~FLAG_CANAL
;
1223 cell_ptr
->state
|= modify_state
;
1225 if( cell_ptr
->state
& FLAG_CANAL
)
1227 cell_ptr
->links
[0] = 0;
1228 cell_ptr
->links
[1] = 0;
1230 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
1235 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
1239 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
1240 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
1243 if( vg_get_button_down("secondary") && !(cell_ptr
->config
== k_cell_type_split
) )
1245 id_drag_from
= world
.selected
;
1246 drag_from_co
[0] = world
.tile_x
+ 0.5f
;
1247 drag_from_co
[1] = world
.tile_y
+ 0.5f
;
1250 if( id_drag_from
&& (cell_ptr
->config
== k_cell_type_split
) )
1252 float local_x
= vg_mouse_ws
[0] - (float)world
.tile_x
;
1253 drag_to_co
[0] = (float)world
.tile_x
+ (local_x
> 0.5f
? 0.75f
: 0.25f
);
1254 drag_to_co
[1] = (float)world
.tile_y
+ 0.25f
;
1256 if( vg_get_button_up("secondary") )
1258 struct cell
*drag_ptr
= &world
.data
[id_drag_from
];
1259 u32 link_id
= local_x
> 0.5f
? 1: 0;
1261 // Cleanup existing connections
1262 if( cell_ptr
->links
[ link_id
] )
1264 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id
, cell_ptr
->links
[ link_id
] );
1266 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1267 current_connection
->state
&= ~FLAG_IS_TRIGGER
;
1268 current_connection
->links
[ link_id
] = 0;
1271 if( drag_ptr
->links
[ link_id
^ 0x1 ] )
1273 vg_warn( "Destroying alternate link %u (%hu)\n", link_id
^ 0x1, drag_ptr
->links
[ link_id
^ 0x1 ] );
1275 struct cell
*current_connection
= &world
.data
[ drag_ptr
->links
[ link_id
^ 0x1 ]];
1276 if( !current_connection
->links
[ link_id
] )
1277 current_connection
->state
&= ~FLAG_TARGETED
;
1279 current_connection
->links
[ link_id
^ 0x1 ] = 0;
1280 drag_ptr
->links
[ link_id
^ 0x1 ] = 0;
1283 // Create the new connection
1284 vg_success( "Creating connection on link %u (%hu)\n", link_id
, id_drag_from
);
1286 cell_ptr
->links
[ link_id
] = id_drag_from
;
1287 drag_ptr
->links
[ link_id
] = world
.selected
;
1289 cell_ptr
->state
|= FLAG_TARGETED
;
1290 drag_ptr
->state
|= FLAG_IS_TRIGGER
;
1296 world
.selected
= -1;
1298 if( !(vg_get_button("secondary") && id_drag_from
) )
1303 world
.selected
= -1;
1307 // Simulation stop/start
1308 if( vg_get_button_down("go") )
1310 if( world
.simulating
)
1316 vg_success( "Starting simulation!\n" );
1318 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
1320 world
.simulating
= 1;
1321 world
.num_fishes
= 0;
1322 world
.sim_frame
= 0;
1323 world
.sim_start
= vg_time
;
1326 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1327 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1334 if( world
.simulating
)
1336 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*2.0f
) )
1338 //vg_info( "frame: %u\n", world.sim_frame );
1339 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
1341 // Update splitter deltas
1342 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1344 struct cell
*cell
= &world
.data
[i
];
1345 if( cell
->config
== k_cell_type_split
)
1347 cell
->state
&= ~FLAG_FLIP_ROTATING
;
1349 if( cell
->state
& FLAG_IS_TRIGGER
)
1350 cell
->state
&= ~FLAG_TRIGGERED
;
1353 int alive_count
= 0;
1355 // Update fish positions
1356 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1358 struct fish
*fish
= &world
.fishes
[i
];
1360 if( fish
->state
== k_fish_state_soon_dead
)
1361 fish
->state
= k_fish_state_dead
;
1363 if( fish
->state
< k_fish_state_alive
)
1366 struct cell
*cell_current
= pcell( fish
->pos
);
1368 if( fish
->state
== k_fish_state_alive
)
1371 if( cell_current
->state
& FLAG_OUTPUT
)
1373 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
1375 struct cell_terminal
*term
= &world
.io
[j
];
1377 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
1379 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1380 if( run
->recv_count
< vg_list_size( run
->recieved
) )
1381 run
->recieved
[ run
->recv_count
++ ] = fish
->payload
;
1387 fish
->state
= k_fish_state_dead
;
1391 if( cell_current
->config
== k_cell_type_split
)
1394 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
1397 if( !(cell_current
->state
& FLAG_TARGETED
) )
1398 cell_current
->state
^= FLAG_FLIP_FLOP
;
1400 else if( cell_current
->config
== k_cell_type_merge
)
1408 struct cell
*cell_next
= pcell( (v2i
){ fish
->pos
[0]+fish
->dir
[0], fish
->pos
[1]+fish
->dir
[1] } );
1409 if( !(cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
)) )
1411 // Try other directions for valid, so down, left, right..
1412 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
1413 //vg_info( "Trying some other directions...\n" );
1415 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
1417 if( (dirs
[j
][0] == -fish
->dir
[0]) && (dirs
[j
][1] == -fish
->dir
[1]) )
1420 if( pcell( (v2i
){ fish
->pos
[0]+dirs
[j
][0], fish
->pos
[1]+dirs
[j
][1] } )->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1422 fish
->dir
[0] = dirs
[j
][0];
1423 fish
->dir
[1] = dirs
[j
][1];
1429 fish
->pos
[0] += fish
->dir
[0];
1430 fish
->pos
[1] += fish
->dir
[1];
1432 struct cell
*cell_entry
= pcell( fish
->pos
);
1434 if( !(cell_entry
->state
& (FLAG_INPUT
|FLAG_CANAL
|FLAG_OUTPUT
) ))
1436 if( world_check_pos_ok( fish
->pos
) )
1437 fish
->state
= k_fish_state_bg
;
1439 fish
->state
= k_fish_state_dead
;
1445 if( cell_entry
->config
== k_cell_type_split
||
1446 cell_entry
->config
== k_cell_type_ramp_right
||
1447 cell_entry
->config
== k_cell_type_ramp_left
)
1449 // Special death (FALL)
1451 v2_sub( fish->physics_co, fish->physics_v, fish->physics_v );
1452 v2_divs( fish->physics_v, vg_time_delta, fish->physics_v );
1455 fish
->state
= k_fish_state_dead
;
1456 vg_error( "REMOVE THIS CONDITION\n" );
1461 if( cell_entry
->config
== k_cell_type_split
)
1463 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1464 cell_entry
->state
|= FLAG_FLIP_ROTATING
;
1468 else if( fish
->state
== k_fish_state_bg
)
1470 fish
->pos
[0] += fish
->dir
[0];
1471 fish
->pos
[1] += fish
->dir
[1];
1473 if( !world_check_pos_ok( fish
->pos
) )
1474 fish
->state
= k_fish_state_dead
;
1477 struct cell
*cell_entry
= pcell( fish
->pos
);
1479 if( cell_entry
->state
& FLAG_CANAL
)
1481 if( cell_entry
->config
== k_cell_type_con_r
|| cell_entry
->config
== k_cell_type_con_u
1482 || cell_entry
->config
== k_cell_type_con_l
|| cell_entry
->config
== k_cell_type_con_d
)
1484 fish
->state
= k_fish_state_alive
;
1489 switch( cell_entry
->config
)
1491 case k_cell_type_con_r
: fish
->dir
[0] = 1; break;
1492 case k_cell_type_con_l
: fish
->dir
[0] = -1; break;
1493 case k_cell_type_con_u
: fish
->dir
[1] = 1; break;
1494 case k_cell_type_con_d
: fish
->dir
[1] = -1; break;
1500 else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
1502 if( fish
->state
>= k_fish_state_alive
)
1506 // Second pass (triggers)
1507 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1509 struct fish
*fish
= &world
.fishes
[i
];
1511 if( fish
->state
== k_fish_state_alive
)
1513 struct cell
*cell_current
= pcell( fish
->pos
);
1515 if( cell_current
->state
& FLAG_IS_TRIGGER
)
1517 int trigger_id
= cell_current
->links
[0]?0:1;
1518 int connection_id
= cell_current
->links
[trigger_id
];
1519 int target_px
= connection_id
% world
.w
;
1520 int target_py
= (connection_id
- target_px
)/world
.w
;
1522 vg_line2( (v2f
){ fish
->pos
[0], fish
->pos
[1] }, (v2f
){ target_px
, target_py
}, 0xffffffff, 0xffffffff );
1524 struct cell
*target_peice
= &world
.data
[ cell_current
->links
[trigger_id
] ];
1526 cell_current
->state
|= FLAG_TRIGGERED
;
1529 target_peice
->state
|= FLAG_FLIP_FLOP
;
1531 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1536 // Third pass (collisions)
1537 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1539 if( world
.fishes
[i
].state
== k_fish_state_alive
)
1541 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1543 if( (world
.fishes
[j
].state
== k_fish_state_alive
) &&
1544 (world
.fishes
[i
].pos
[0] == world
.fishes
[j
].pos
[0]) &&
1545 (world
.fishes
[i
].pos
[1] == world
.fishes
[j
].pos
[1]) )
1547 // Shatter death (+0.5s)
1548 world
.fishes
[i
].state
= k_fish_state_soon_dead
;
1549 world
.fishes
[j
].state
= k_fish_state_soon_dead
;
1550 world
.fishes
[i
].death_time
= 0.5f
;
1551 world
.fishes
[j
].death_time
= 0.5f
;
1558 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1560 struct cell_terminal
*term
= &world
.io
[ i
];
1561 int posx
= term
->id
% world
.w
;
1562 int posy
= (term
->id
- posx
)/world
.w
;
1563 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1567 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].condition_count
)
1569 struct fish
*fish
= &world
.fishes
[world
.num_fishes
++];
1570 fish
->pos
[0] = posx
;
1571 fish
->pos
[1] = posy
;
1572 fish
->state
= k_fish_state_alive
;
1573 fish
->payload
= term
->runs
[ world
.sim_run
].conditions
[ world
.sim_frame
];
1577 v2i dirs
[] = {{1,0},{-1,0},{0,-1}};
1578 for( int j
= 0; j
< vg_list_size(dirs
); j
++ )
1579 if( pcell( (v2i
){ posx
+dirs
[j
][0], posy
+dirs
[j
][1] } )->state
& FLAG_CANAL
)
1581 fish
->dir
[0] = dirs
[j
][0];
1582 fish
->dir
[1] = dirs
[j
][1];
1595 if( alive_count
== 0 )
1597 world
.completed
= 1;
1599 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1601 struct cell_terminal
*term
= &world
.io
[ i
];
1602 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1606 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1608 if( run
->recv_count
== run
->condition_count
)
1610 for( int j
= 0; j
< run
->condition_count
; j
++ )
1612 if( run
->recieved
[j
] != run
->conditions
[j
] )
1614 world
.completed
= 0;
1621 world
.completed
= 0;
1627 if( world
.completed
)
1629 if( world
.sim_run
< world
.max_runs
-1 )
1631 vg_success( "Run passed, starting next\n" );
1633 world
.sim_frame
= 0;
1634 world
.sim_start
= vg_time
;
1635 world
.num_fishes
= 0;
1640 vg_success( "Level passed!\n" );
1643 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1644 if( world
.data
[ i
].state
& FLAG_CANAL
)
1647 world
.score
= score
;
1648 world
.time
= world
.sim_frame
;
1653 vg_error( "Level failed :(\n" );
1656 // Copy into career data
1657 if( world
.ptr_career_level
)
1659 world
.ptr_career_level
->score
= world
.score
;
1660 world
.ptr_career_level
->time
= world
.time
;
1661 world
.ptr_career_level
->completed
= world
.completed
;
1664 simulation_stop(); // TODO: Async?
1671 float scaled_time
= 0.0f
;
1672 scaled_time
= (vg_time
-world
.sim_start
)*2.0f
;
1673 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1676 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1678 struct fish
*fish
= &world
.fishes
[i
];
1680 if( fish
->state
== k_fish_state_dead
)
1683 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1684 continue; // Todo: particle thing?
1686 struct cell
*cell
= pcell(fish
->pos
);
1689 float t
= world
.frame_lerp
;
1692 v2_copy( fish
->physics_co
, fish
->physics_v
);
1694 switch( cell
->config
)
1697 if( fish
->dir
[0] == 1 )
1702 case k_cell_type_con_r
: curve
= curve_1
;
1703 if( fish
->dir
[0] == 1 ) t
= ti
;
1705 case k_cell_type_con_l
: curve
= curve_4
;
1706 if( fish
->dir
[0] == -1 ) t
= ti
;
1708 case k_cell_type_con_u
: curve
= curve_2
;
1709 if( fish
->dir
[1] == 1 ) t
= ti
;
1711 case k_cell_type_con_d
: curve
= curve_8
;
1712 if( fish
->dir
[1] == 1 ) t
= ti
;
1714 case 3: curve
= curve_3
; break;
1715 case 6: curve
= curve_6
; break;
1716 case 9: curve
= curve_9
; break;
1717 case 12: curve
= curve_12
; break;
1719 if( t
> curve_7_linear_section
)
1721 t
-= curve_7_linear_section
;
1722 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1724 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1728 default: curve
= NULL
; break;
1734 float t3
= t
* t
* t
;
1736 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1737 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1738 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1740 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1741 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1742 fish
->physics_co
[0] += (float)fish
->pos
[0];
1743 fish
->physics_co
[1] += (float)fish
->pos
[1];
1748 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1749 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1751 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1752 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1758 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1760 v2i full_start
= { 0,0 };
1761 v2i full_end
= { world
.w
, world
.h
};
1763 if( !start
|| !end
)
1769 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1771 for( int y
= start
[1]; y
< end
[1]; y
++ )
1773 for( int x
= start
[0]; x
< end
[0]; x
++ )
1775 struct cell
*cell
= pcell((v2i
){x
,y
});
1776 int selected
= world
.selected
== y
*world
.w
+ x
;
1778 int tile_offsets
[][2] =
1780 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1781 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1782 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1783 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1786 int uv
[2] = { 3, 0 };
1788 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1790 uv
[0] = tile_offsets
[ cell
->config
][0];
1791 uv
[1] = tile_offsets
[ cell
->config
][1];
1794 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1797 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1799 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1807 static void draw_numbers( v3f coord
, int number
)
1810 v3_copy( coord
, pos
);
1811 int digits
[8]; int i
= 0;
1813 while( number
> 0 && i
< 8 )
1815 digits
[i
++] = number
% 10;
1816 number
= number
/ 10;
1819 for( int j
= 0; j
< i
; j
++ )
1821 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
1822 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
1823 pos
[0] += pos
[2] * 0.75f
;
1827 void vg_render(void)
1829 glViewport( 0,0, vg_window_x
, vg_window_y
);
1831 glDisable( GL_DEPTH_TEST
);
1832 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1833 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1835 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1836 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1838 // TILE SET RENDERING
1839 // todo: just slam everything into a mesh...
1840 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1842 // Currently we're uploading a fair amount of data every frame anyway.
1843 // NOTE: this is for final optimisations ONLY!
1844 // ======================================================================
1846 use_mesh( &world
.tile
);
1852 SHADER_USE( shader_background
);
1853 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1855 glActiveTexture( GL_TEXTURE0
);
1856 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1857 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1859 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1860 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1862 glActiveTexture( GL_TEXTURE1
);
1863 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1864 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1871 SHADER_USE( shader_tile_main
);
1874 m2x2_identity( subtransform
);
1875 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1876 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1877 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1878 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1881 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1882 glBlendEquation(GL_FUNC_ADD
);
1885 vg_tex2d_bind( &tex_tile_data
, 0 );
1886 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1888 vg_tex2d_bind( &tex_wood
, 1 );
1889 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1891 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
1895 SHADER_USE( shader_ball
);
1896 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1898 vg_tex2d_bind( &tex_ball
, 0 );
1899 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
1902 if( world
.simulating
)
1904 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1906 struct fish
*fish
= &world
.fishes
[i
];
1908 if( fish
->state
== k_fish_state_dead
|| fish
->state
== k_fish_state_bg
)
1911 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1914 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
1915 colour_code_v3( fish
->payload
, dot_colour
);
1917 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
1918 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
1923 SHADER_USE( shader_tile_main
);
1926 vg_tex2d_bind( &tex_tile_data
, 0 );
1927 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
1929 vg_tex2d_bind( &tex_wood
, 1 );
1930 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
1932 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
1933 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
1936 for( int y
= 2; y
< world
.h
-2; y
++ )
1938 for( int x
= 2; x
< world
.w
-2; x
++ )
1940 struct cell
*cell
= pcell((v2i
){x
,y
});
1942 if( cell
->state
& FLAG_CANAL
)
1944 if( cell
->config
== k_cell_type_split
)
1946 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
1948 if( cell
->state
& FLAG_FLIP_ROTATING
)
1950 if( (world
.frame_lerp
> curve_7_linear_section
) )
1952 float const rotation_speed
= 0.4f
;
1953 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
1955 float t
= world
.frame_lerp
- curve_7_linear_section
;
1956 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
1966 m2x2_create_rotation( subtransform
, rotation
);
1968 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1969 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, cell
->state
& FLAG_TARGETED
? 3.0f
: 0.0f
, 0.0f
);
1977 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) && !id_drag_from
)
1979 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
1980 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
1982 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1983 map_reclassify( new_begin
, new_end
, 0 );
1985 m2x2_identity( subtransform
);
1986 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
1987 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1988 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
1990 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
1992 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
1993 map_reclassify( new_begin
, new_end
, 0 );
1996 //glDisable(GL_BLEND);
1998 // Draw connecting wires
1999 glDisable(GL_BLEND
);
2001 SHADER_USE( shader_wire
);
2002 glBindVertexArray( world
.wire
.vao
);
2004 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2005 glUniform4f( SHADER_UNIFORM( shader_wire
, "uColour" ), 0.2f
, 0.2f
, 0.2f
, 1.0f
);
2009 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), 0.4f
);
2010 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), drag_from_co
[0], drag_from_co
[1], 0.06f
);
2011 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), drag_to_co
[0], drag_to_co
[1], 0.06f
);
2012 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2015 float rp_x1
= world
.frame_lerp
*9.0f
;
2016 float rp_x2
= 1.0f
-rp_x1
*expf(1.0f
-rp_x1
)* 0.36f
;
2018 for( int y
= 2; y
< world
.h
-2; y
++ )
2020 for( int x
= 2; x
< world
.w
-2; x
++ )
2022 struct cell
*cell
= pcell((v2i
){x
,y
});
2024 if( cell
->state
& FLAG_CANAL
)
2026 if( cell
->state
& FLAG_IS_TRIGGER
)
2028 int trigger_id
= cell
->links
[0]?0:1;
2030 int x2
= cell
->links
[trigger_id
] % world
.w
;
2031 int y2
= (cell
->links
[trigger_id
] - x2
) / world
.w
;
2036 startpoint
[0] = (float)x2
+ (trigger_id
? 0.75f
: 0.25f
);
2037 startpoint
[1] = (float)y2
+ 0.25f
;
2039 endpoint
[0] = x
+0.5f
;
2040 endpoint
[1] = y
+0.5f
;
2042 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), cell
->state
& FLAG_TRIGGERED
? rp_x2
* 0.4f
: 0.4f
);
2043 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), startpoint
[0], startpoint
[1], 0.04f
);
2044 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), endpoint
[0], endpoint
[1], 0.04f
);
2045 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2051 SHADER_USE( shader_tile_colour
);
2052 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2053 use_mesh( &world
.circle
);
2055 int const filled_start
= 0;
2056 int const filled_count
= 32;
2057 int const empty_start
= 32;
2058 int const empty_count
= 32*2;
2061 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2063 struct cell_terminal
*term
= &world
.io
[ i
];
2064 int posx
= term
->id
% world
.w
;
2065 int posy
= (term
->id
- posx
)/world
.w
;
2066 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
2068 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2070 for( int k
= 0; k
< term
->run_count
; k
++ )
2072 for( int j
= 0; j
< term
->runs
[k
].condition_count
; j
++ )
2074 float y_offset
= is_input
? 1.2f
: -0.2f
;
2075 y_offset
+= (is_input
? 0.2f
: -0.2f
) * (float)k
;
2077 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
2081 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2082 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2084 // Draw filled if tick not passed, draw empty if empty
2085 if( (world
.sim_frame
> j
&& world
.sim_run
>= k
) || world
.sim_run
> k
)
2086 draw_mesh( empty_start
, empty_count
);
2088 draw_mesh( filled_start
, filled_count
);
2092 if( term
->runs
[k
].recv_count
> j
)
2094 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
2095 v3_muls( dot_colour
, 0.8f
, dot_colour
);
2096 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2098 draw_mesh( filled_start
, filled_count
);
2101 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2102 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2104 draw_mesh( empty_start
, empty_count
);
2110 if( world
.simulating
)
2112 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
2113 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), -0.5f
+ cosf( vg_time
* 4.0f
) * 0.2f
, sinf( vg_time
* 4.0f
) * 0.2f
+ (float)world
.h
* 0.5f
, 0.05f
);
2114 draw_mesh( filled_start
, filled_count
);
2118 float const score_bright
= 1.25f
;
2119 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
2120 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
2122 use_mesh( &world
.numbers
);
2123 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
2125 // Level selection UI
2126 use_mesh( &world
.circle
);
2127 float ratio
= ((float)vg_window_x
/(float)vg_window_y
);
2129 m3x3f ui_view
= M3X3_IDENTITY
;
2130 m3x3_scale( ui_view
, (v3f
){ 1.0f
, ratio
, 1.0f
} );
2131 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)ui_view
);
2133 // Calculate mouse in UIsp
2134 v3f mouse_ui_space
= { ((float)vg_mouse
[0] / (float)(vg_window_x
)) * 2.0f
- 1.0f
,
2135 (((float)vg_mouse
[1] / (float)(vg_window_y
)) * 2.0f
- 1.0f
)*(-1.0f
/ratio
), 0.0125f
};
2137 // Get selected level
2138 const float selection_scale
= 0.05f
;
2139 int const level_count
= vg_list_size( level_pack_1
);
2140 int level_select
= -1;
2142 if( mouse_ui_space
[0] <= -0.8f
)
2144 float levels_range
= (float)level_count
*selection_scale
*0.6f
;
2145 float level_offset
= ((-mouse_ui_space
[1] + levels_range
) / levels_range
) * 0.5f
* (float)level_count
;
2146 level_select
= ceilf( level_offset
);
2149 if( level_select
>= 0 && level_select
< vg_list_size( level_pack_1
) )
2151 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
2153 use_mesh( &world
.tile
);
2154 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ),
2156 ((float)level_count
- (float)level_select
* 2.0f
) * selection_scale
* 0.6f
,
2161 use_mesh( &world
.circle
);
2163 if( vg_get_button_down( "primary" ) )
2165 console_changelevel( 1, level_pack_1
+ level_select
);
2169 else mouse_ui_space
[1] = INFINITY
;
2171 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
, 0.39f
, 0.45f
, 1.0f
);
2174 for( int i
= 0; i
< level_count
; i
++ )
2176 struct career_level
*clevel
= &career
.levels
[i
];
2178 v3f level_ui_space
= {
2180 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2181 selection_scale
* 0.5f
2184 float scale
= vg_clampf( 1.0f
- fabsf(level_ui_space
[1] - mouse_ui_space
[1]) * 2.0f
, 0.9f
, 1.0f
);
2185 level_ui_space
[2] *= scale
;
2187 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, level_ui_space
);
2189 if( clevel
->completed
)
2190 draw_mesh( filled_start
, filled_count
);
2192 draw_mesh( empty_start
, empty_count
);
2196 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.4f
*1.25f
, 0.39f
*1.25f
, 0.45f
*1.25f
, 1.0f
);
2198 use_mesh( &world
.numbers
);
2199 for( int i
= 0; i
< level_count
; i
++ )
2201 struct career_level
*clevel
= &career
.levels
[i
];
2203 v3f level_ui_space
= {
2205 ((float)level_count
- (float)i
* 2.0f
) * selection_scale
* 0.6f
+ selection_scale
* 0.5f
,
2209 if( clevel
->completed
)
2210 draw_numbers( level_ui_space
, clevel
->score
);
2212 level_ui_space
[0] = -0.975f
;
2213 level_ui_space
[1] -= 0.01f
;
2214 draw_numbers( level_ui_space
, i
);
2217 //use_mesh( &world.numbers );
2218 //draw_numbers( (v3f){ 0.0f, -0.5f, 0.1f }, 128765 );