1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
4 #define VG_STEAM_APPID 1218140U
6 #include "fishladder_resources.h"
13 3. Merge (and explode)
14 4. Principle 1 (divide colours)
15 5. Principle 2 (combine colours)
25 New things to program:
26 UI text element renderer (SDF) DONE(sorta)
27 Particle system thing for ball collision
28 Level descriptions / titles HALF
30 Play button / Speed controller
37 const char *level_pack_1
[] = {
62 levels
[ vg_list_size( level_pack_1
) ];
64 career
= { .version
= 1 };
67 static void career_serialize(void)
69 vg_asset_write( "sav/game.sav", &career
, sizeof( struct career_state
) );
72 static void career_load(void)
75 struct career_state
*cr
= vg_asset_read_s( "sav/game.sav", &sz
);
77 memset( (void*)career
.levels
, 0, vg_list_size(level_pack_1
) * sizeof(struct career_level
) );
81 if( sz
> sizeof( struct career_state
) )
82 vg_warn( "This save file is too big! Some levels will be lost\n" );
84 if( sz
<= offsetof( struct career_state
, levels
) )
86 vg_error( "This save file is too small to have a header\n" );
91 u32
const size_header
= offsetof(struct career_state
, levels
);
92 u32
const size_levels
= sizeof(struct career_state
)-size_header
;
93 u32
const size_levels_input
= sz
- size_header
;
95 memcpy( (void*)career
.levels
, (void*)cr
->levels
, size_levels_input
);
97 if( sz
< sizeof( struct career_state
) )
99 memset( ((void*)career
.levels
) + size_levels_input
, 0, size_levels
-size_levels_input
);
103 vg_success( "Loaded save file... Info:\n" );
105 for( int i
= 0; i
< vg_list_size( career
.levels
); i
++ )
107 struct career_level
*lvl
= &career
.levels
[i
];
108 vg_info( "Score: %u, Time: %u, Completed: %u\n", lvl
->score
, lvl
->time
, lvl
->completed
);
113 vg_info( "No save file... Using blank one\n" );
121 #define FLAG_CANAL 0x1
122 #define FLAG_IS_TRIGGER 0x2
123 #define FLAG_RESERVED0 0x4
124 #define FLAG_RESERVED1 0x8
126 #define FLAG_INPUT 0x10
127 #define FLAG_OUTPUT 0x20
128 #define FLAG_WALL 0x40
130 #define FLAG_FLIP_FLOP 0x100
131 #define FLAG_TRIGGERED 0x200
132 #define FLAG_FLIP_ROTATING 0x400
133 #define FLAG_TARGETED 0x800
136 0000 0 | 0001 1 | 0010 2 | 0011 3
140 0100 4 | 0101 5 | 0110 6 | 0111 7
144 1000 8 | 1001 9 | 1010 10 | 1011 11
148 1100 12 | 1101 13 | 1110 14 | 1111 15
154 struct cell_description
162 cell_descriptions
[] =
166 { .start
= { 1, 0 }, .end
= { -1, 0 } },
167 { .start
= { 0, 1 }, .end
= { 0, -1 } },
168 { .start
= { 0, 1 }, .end
= { 1, 0 } },
170 { .start
= { -1, 0 }, .end
= { 1, 0 } },
171 { .start
= { -1, 0 }, .end
= { 1, 0 }, .is_linear
= 1 },
172 { .start
= { 0, 1 }, .end
= { -1, 0 } },
173 { .start
= { 0, 1 }, .is_special
= 1 },
175 { .start
= { 0, -1 }, .end
= { 0, 1 } },
176 { .start
= { 1, 0 }, .end
= { 0, -1 } },
177 { .start
= { 0, 1 }, .end
= { 0, -1 }, .is_linear
= 1 },
180 { .start
= { -1, 0 }, .end
= { 0, -1 } },
181 { .end
= { 0, -1 }, .is_special
= 1 },
188 k_cell_type_stub
= 0,
189 k_cell_type_ramp_right
= 3,
190 k_cell_type_ramp_left
= 6,
191 k_cell_type_split
= 7,
192 k_cell_type_merge
= 13,
193 k_cell_type_con_r
= 1,
194 k_cell_type_con_u
= 2,
195 k_cell_type_con_l
= 4,
196 k_cell_type_con_d
= 8
199 v2f
const curve_3
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
200 v2f
const curve_6
[] = {{0.5f
,1.0f
},{0.5f
,0.625f
},{0.375f
,0.5f
},{0.0f
,0.5f
}};
201 v2f
const curve_9
[] = {{1.0f
,0.5f
},{0.625f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
202 v2f
const curve_12
[]= {{0.0f
,0.5f
},{0.375f
,0.5f
},{0.5f
,0.375f
},{0.5f
,0.0f
}};
204 v2f
const curve_1
[] = {{1.0f
,0.5f
},{0.8f
,0.5f
},{0.3f
,0.5f
},{0.2f
,0.5f
}};
205 v2f
const curve_4
[] = {{0.0f
,0.5f
},{0.3f
,0.5f
},{0.5f
,0.5f
},{0.8f
,0.5f
}};
206 v2f
const curve_2
[] = {{0.5f
,1.0f
},{0.5f
,0.8f
},{0.5f
,0.3f
},{0.5f
,0.2f
}};
207 v2f
const curve_8
[] = {{0.5f
,0.0f
},{0.5f
,0.3f
},{0.5f
,0.5f
},{0.5f
,0.8f
}};
209 v2f
const curve_7
[] = {{0.5f
,0.8438f
},{0.875f
,0.8438f
},{0.625f
,0.5f
},{1.0f
,0.5f
}};
210 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
}};
212 float const curve_7_linear_section
= 0.1562f
;
215 { { 1.0f
, 0.9f
, 0.3f
},
216 { 0.2f
, 0.9f
, 0.14f
},
217 { 0.4f
, 0.8f
, 1.00f
} };
219 static void colour_code_v3( char cc
, v3f target
)
221 if( cc
>= 'a' && cc
<= 'z' )
225 if( id
< vg_list_size( colour_sets
) )
227 v3_copy( colour_sets
[ id
], target
);
232 v3_copy( (v3f
){0.0f
,0.0f
,0.0f
}, target
);
241 static void init_mesh( struct mesh
*m
, float const *tris
, u32 length
)
243 m
->elements
= length
/3;
244 glGenVertexArrays( 1, &m
->vao
);
245 glGenBuffers( 1, &m
->vbo
);
247 glBindVertexArray( m
->vao
);
248 glBindBuffer( GL_ARRAY_BUFFER
, m
->vbo
);
249 glBufferData( GL_ARRAY_BUFFER
, length
*sizeof(float), tris
, GL_STATIC_DRAW
);
251 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2 * sizeof(float), (void*)0 );
252 glEnableVertexAttribArray( 0 );
257 static void free_mesh( struct mesh
*m
)
259 glDeleteVertexArrays( 1, &m
->vao
);
260 glDeleteBuffers( 1, &m
->vbo
);
263 static void draw_mesh( int const start
, int const count
)
265 glDrawArrays( GL_TRIANGLES
, start
*3, count
*3 );
268 static void use_mesh( struct mesh
*m
)
270 glBindVertexArray( m
->vao
);
275 k_fish_state_soon_dead
= -1,
276 k_fish_state_dead
= 0,
279 k_fish_state_soon_alive
302 int sim_run
, max_runs
;
317 int condition_count
, recv_count
;
329 struct mesh tile
, circle
, numbers
;
332 GLuint vao
, vbo
, ebo
;
337 GLuint background_data
;
338 GLuint random_samples
;
340 int selected
, tile_x
, tile_y
;
347 enum e_fish_state state
;
359 struct career_level
*ptr_career_level
;
366 static void map_free(void)
368 arrfree( world
.data
);
379 world
.initialzed
= 0;
382 static void io_reset(void)
384 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
386 struct cell_terminal
*term
= &world
.io
[i
];
388 for( int j
= 0; j
< term
->run_count
; j
++ )
389 term
->runs
[j
].recv_count
= 0;
393 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
394 static int map_load( const char *str
, const char *name
)
396 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
405 if( c
[world
.w
] == ';' )
407 else if( !c
[world
.w
] )
409 vg_error( "Unexpected EOF when parsing level\n" );
414 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
416 int reg_start
= 0, reg_end
= 0;
418 u32
*links_to_make
= NULL
;
419 int links_satisfied
= 0;
421 char link_id_buffer
[32];
429 if( *c
== '\r' ) { c
++; continue; } // fuck off windows
435 if( *c
== '\r' ) c
++;
442 if( *c
== '\r' ) { c
++; continue; }
444 if( reg_start
< reg_end
)
446 struct cell_terminal
*terminal
= &world
.io
[ reg_start
];
447 struct terminal_run
*run
= &terminal
->runs
[ terminal
->run_count
-1 ];
449 if( *c
>= 'a' && *c
<= 'z' )
451 run
->conditions
[ run
->condition_count
++ ] = *c
;
455 if( *c
== ',' || *c
== '\n' )
464 terminal
->runs
[ terminal
->run_count
].condition_count
= 0;
465 terminal
->run_count
++;
466 world
.max_runs
= vg_max( world
.max_runs
, terminal
->run_count
);
470 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
477 if( links_satisfied
< arrlen( links_to_make
) )
479 struct cell
*target
= &world
.data
[ links_to_make
[ links_satisfied
] ];
481 if( (((u32
)*c
>= (u32
)'0') && ((u32
)*c
<= (u32
)'9')) || *c
== '-' )
483 if( link_id_n
>= vg_list_size( link_id_buffer
)-1 )
485 vg_error( "Number was way too long to be parsed (row: %u)\n", world
.h
);
489 link_id_buffer
[ link_id_n
++ ] = *c
;
491 else if( *c
== ',' || *c
== '\n' )
493 link_id_buffer
[ link_id_n
] = 0x00;
494 int value
= atoi( link_id_buffer
);
496 target
->links
[value
>= 0? 1:0] = abs(value
);
505 vg_error( "Invalid character '%c' (row: %u)\n", *c
, world
.h
);
511 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
520 // Registry length-error checks
521 if( reg_start
!= reg_end
)
523 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
527 if( links_satisfied
!= arrlen( links_to_make
) )
529 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world
.h
, links_satisfied
, arrlen( links_to_make
) );
535 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
539 row
= arraddnptr( world
.data
, world
.w
);
542 reg_end
= reg_start
= arrlen( world
.io
);
544 arrsetlen( links_to_make
, 0 );
551 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
555 // Tile initialization
557 struct cell
*cell
= &row
[ cx
];
560 if( *c
== '+' || *c
== '-' )
562 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
563 term
->id
= cx
+ world
.h
*world
.w
;
565 term
->runs
[0].condition_count
= 0;
567 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
570 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
571 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
573 // Canal flag bits (4bit/16 value):
579 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
581 if( cell
->state
& FLAG_IS_TRIGGER
)
582 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
588 else cell
->state
= 0x00;
596 // Update data texture to fill out the background
598 u8 info_buffer
[64*64*4];
599 for( int i
= 0; i
< 64*64; i
++ )
601 u8
*px
= &info_buffer
[i
*4];
608 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
609 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
612 arrfree( links_to_make
);
614 map_reclassify( NULL
, NULL
, 1 );
617 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
619 struct cell
*src
= &world
.data
[i
];
620 if( src
->state
& FLAG_IS_TRIGGER
)
622 int link_id
= src
->links
[0]?0:1;
623 if( src
->links
[link_id
] <= world
.h
*world
.w
)
625 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
626 if( (target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
) )
628 if( target
->links
[ link_id
] )
630 vg_error( "Link target was already targeted\n" );
636 target
->links
[ link_id
] = i
;
637 target
->state
|= FLAG_TARGETED
;
642 vg_error( "Link target was invalid\n" );
648 vg_error( "Link target out of bounds\n" );
654 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
658 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
659 world
.initialzed
= 1;
663 arrfree( links_to_make
);
668 static struct cell
*pcell( v2i pos
)
670 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
673 static void map_serialize( FILE *stream
)
675 for( int y
= 0; y
< world
.h
; y
++ )
677 for( int x
= 0; x
< world
.w
; x
++ )
679 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
681 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
682 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
683 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
684 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
686 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
688 else fputc( ' ', stream
);
691 fputc( ';', stream
);
693 int terminal_write_count
= 0;
695 for( int x
= 0; x
< world
.w
; x
++ )
697 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
699 struct cell_terminal
*term
= &world
.io
[i
];
700 if( term
->id
== y
*world
.w
+x
)
702 if( terminal_write_count
)
703 fputc( ',', stream
);
704 terminal_write_count
++;
706 for( int j
= 0; j
< term
->run_count
; j
++ )
708 struct terminal_run
*run
= &term
->runs
[j
];
710 for( int k
= 0; k
< run
->condition_count
; k
++ )
711 fputc( run
->conditions
[k
], stream
);
713 if( j
< term
->run_count
-1 )
714 fputc( ':', stream
);
720 for( int x
= 0; x
< world
.w
; x
++ )
722 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
723 if( cell
->state
& FLAG_IS_TRIGGER
)
725 if( terminal_write_count
)
726 fputc( ',', stream
);
727 terminal_write_count
++;
729 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
733 fputc( '\n', stream
);
737 int main( int argc
, char *argv
[] )
739 vg_init( argc
, argv
, "Marble Computing | SPACE: Test | LeftClick: Toggle tile | RightClick: Drag wire" );
743 static int console_credits( int argc
, char const *argv
[] )
745 vg_info( "Aknowledgements:\n" );
746 vg_info( " GLFW zlib/libpng glfw.org\n" );
747 vg_info( " miniaudio MIT0 miniaud.io\n" );
748 vg_info( " QOI MIT phoboslab.org\n" );
749 vg_info( " STB library MIT nothings.org\n" );
750 vg_info( " Weiholmir font justfredrik.itch.io\n" );
754 static int console_save_map( int argc
, char const *argv
[] )
756 if( !world
.initialzed
)
758 vg_error( "Tried to save uninitialized map!\n" );
762 char map_path
[ 256 ];
764 strcpy( map_path
, "sav/" );
765 strcat( map_path
, world
.map_name
);
766 strcat( map_path
, ".map" );
768 FILE *test_writer
= fopen( map_path
, "wb" );
771 vg_info( "Saving map to '%s'\n", map_path
);
772 map_serialize( test_writer
);
774 fclose( test_writer
);
779 vg_error( "Unable to open stream for writing\n" );
784 static int console_load_map( int argc
, char const *argv
[] )
786 char map_path
[ 256 ];
791 strcpy( map_path
, "sav/" );
792 strcat( map_path
, argv
[0] );
793 strcat( map_path
, ".map" );
795 char *text_source
= vg_textasset_read( map_path
);
799 strcpy( map_path
, "maps/" );
800 strcat( map_path
, argv
[0] );
801 strcat( map_path
, ".map" );
803 text_source
= vg_textasset_read( map_path
);
808 vg_info( "Loading map: '%s'\n", map_path
);
809 world
.ptr_career_level
= NULL
;
811 if( !map_load( text_source
, argv
[0] ) )
819 for( int i
= 0; i
< vg_list_size( level_pack_1
); i
++ )
821 if( !strcmp( level_pack_1
[i
], argv
[0] ) )
823 world
.ptr_career_level
= career
.levels
+ i
;
832 vg_error( "Missing maps '%s'\n", argv
[0] );
838 vg_error( "Missing argument <map_path>\n" );
843 static void simulation_stop(void)
845 world
.simulating
= 0;
846 world
.num_fishes
= 0;
852 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
854 vg_info( "Stopping simulation!\n" );
857 static int console_changelevel( int argc
, char const *argv
[] )
861 // Save current level
862 console_save_map( 0, NULL
);
863 if( console_load_map( argc
, argv
) )
871 vg_error( "Missing argument <map_path>\n" );
879 vg_function_push( (struct vg_cmd
){
880 .name
= "_map_write",
881 .function
= console_save_map
884 vg_function_push( (struct vg_cmd
){
886 .function
= console_load_map
889 vg_function_push( (struct vg_cmd
){
891 .function
= console_changelevel
894 vg_function_push( (struct vg_cmd
){
896 .function
= console_credits
903 0.0f
, 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
,
904 0.0f
, 0.0f
, 1.0f
, 1.0f
, 1.0f
, 0.0f
,
906 0.0f
, 0.0f
, 0.0f
, 1.0f
, 4.0f
, 1.0f
,
907 0.0f
, 0.0f
, 4.0f
, 1.0f
, 4.0f
, 0.0f
910 init_mesh( &world
.tile
, quad_mesh
, vg_list_size(quad_mesh
) );
915 float circle_mesh
[32*6*3];
916 int res
= vg_list_size( circle_mesh
) / (6*3);
918 for( int i
= 0; i
< res
; i
++ )
920 v2f v0
= { sinf( ((float)i
/(float)res
)*VG_TAUf
), cosf( ((float)i
/(float)res
)*VG_TAUf
) };
921 v2f v1
= { sinf( ((float)(i
+1)/(float)res
)*VG_TAUf
), cosf( ((float)(i
+1)/(float)res
)*VG_TAUf
) };
923 circle_mesh
[ i
*6+0 ] = 0.0f
;
924 circle_mesh
[ i
*6+1 ] = 0.0f
;
926 v2_copy( v0
, circle_mesh
+ 32*6 + i
*12 );
927 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+2 );
928 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+4 );
930 v2_copy( v1
, circle_mesh
+ 32*6 + i
*12+6 );
931 v2_muls( v1
, 0.8f
, circle_mesh
+ 32*6 + i
*12+8 );
932 v2_muls( v0
, 0.8f
, circle_mesh
+ 32*6 + i
*12+10 );
934 v2_copy( v0
, circle_mesh
+ i
*6+4 );
935 v2_copy( v1
, circle_mesh
+ i
*6+2 );
936 v2_copy( v0
, circle_mesh
+i
*6+4 );
937 v2_copy( v1
, circle_mesh
+i
*6+2 );
940 init_mesh( &world
.circle
, circle_mesh
, vg_list_size( circle_mesh
) );
945 init_mesh( &world
.numbers
,
947 vg_list_size( MESH_NUMBERS_BUFFER
)
950 for( int i
= 0; i
< 10; i
++ )
952 vg_info( "offset: %u, length: %u\n", MESH_NUMBERS_OFFSETS
[i
][0], MESH_NUMBERS_OFFSETS
[i
][1] );
958 int const num_segments
= 64;
960 struct mesh_wire
*mw
= &world
.wire
;
962 v2f wire_points
[ num_segments
* 2 ];
963 u16 wire_indices
[ 6*(num_segments
-1) ];
965 for( int i
= 0; i
< num_segments
; i
++ )
967 float l
= (float)i
/ (float)(num_segments
-1);
969 v2_copy( (v2f
){ l
, -0.5f
}, wire_points
[i
*2+0] );
970 v2_copy( (v2f
){ l
, 0.5f
}, wire_points
[i
*2+1] );
972 if( i
< num_segments
-1 )
974 wire_indices
[ i
*6+0 ] = i
*2 + 0;
975 wire_indices
[ i
*6+1 ] = i
*2 + 1;
976 wire_indices
[ i
*6+2 ] = i
*2 + 3;
977 wire_indices
[ i
*6+3 ] = i
*2 + 0;
978 wire_indices
[ i
*6+4 ] = i
*2 + 3;
979 wire_indices
[ i
*6+5 ] = i
*2 + 2;
983 glGenVertexArrays( 1, &mw
->vao
);
984 glGenBuffers( 1, &mw
->vbo
);
985 glGenBuffers( 1, &mw
->ebo
);
986 glBindVertexArray( mw
->vao
);
988 glBindBuffer( GL_ARRAY_BUFFER
, mw
->vbo
);
990 glBufferData( GL_ARRAY_BUFFER
, sizeof( wire_points
), wire_points
, GL_STATIC_DRAW
);
991 glBindVertexArray( mw
->vao
);
993 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mw
->ebo
);
994 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, sizeof( wire_indices
), wire_indices
, GL_STATIC_DRAW
);
997 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, 2*sizeof(float), (void*)0 );
998 glEnableVertexAttribArray( 0 );
1002 mw
->em
= vg_list_size( wire_indices
);
1005 // Create info data texture
1007 glGenTextures( 1, &world
.background_data
);
1008 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1009 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 64, 64, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
1013 // Create random smaples texture
1015 u8
*data
= malloc(512*512*2);
1016 for( int i
= 0; i
< 512*512*2; i
++ )
1017 data
[ i
] = rand()/(RAND_MAX
/255);
1019 glGenTextures( 1, &world
.random_samples
);
1020 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1021 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 512, 512, 0, GL_RG
, GL_UNSIGNED_BYTE
, data
);
1028 resource_load_main();
1030 // Restore gamestate
1032 console_load_map( 1, level_pack_1
);
1037 console_save_map( 0, NULL
);
1040 resource_free_main();
1042 glDeleteTextures( 1, &world
.background_data
);
1043 glDeleteTextures( 1, &world
.random_samples
);
1045 glDeleteVertexArrays( 1, &world
.wire
.vao
);
1046 glDeleteBuffers( 1, &world
.wire
.vbo
);
1047 glDeleteBuffers( 1, &world
.wire
.ebo
);
1049 free_mesh( &world
.tile
);
1050 free_mesh( &world
.circle
);
1051 free_mesh( &world
.numbers
);
1056 static int world_check_pos_ok( v2i co
)
1058 return (co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2)? 0: 1;
1061 static int cell_interactive( v2i co
)
1064 if( !world_check_pos_ok( co
) )
1068 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1071 // List of 3x3 configurations that we do not allow
1072 static u32 invalid_src
[][9] =
1104 // Statically compile invalid configurations into bitmasks
1105 static u32 invalid
[ vg_list_size(invalid_src
) ];
1107 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
1111 for( int j
= 0; j
< 3; j
++ )
1112 for( int k
= 0; k
< 3; k
++ )
1113 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
1115 invalid
[i
] = comped
;
1118 // Extract 5x5 grid surrounding tile
1120 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
1121 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
1123 struct cell
*cell
= pcell((v2i
){x
,y
});
1125 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
1126 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
1129 // Run filter over center 3x3 grid to check for invalid configurations
1130 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1131 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
1133 if( blob
& (0x1 << (6+kernel
[i
])) )
1135 u32 window
= blob
>> kernel
[i
];
1137 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
1138 if((window
& invalid
[j
]) == invalid
[j
])
1146 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
1148 v2i full_start
= { 1,1 };
1149 v2i full_end
= { world
.w
-1, world
.h
-1 };
1151 if( !start
|| !end
)
1158 u8 info_buffer
[64*64*4];
1161 int px0
= vg_max( start
[0], full_start
[0] ),
1162 px1
= vg_min( end
[0], full_end
[0] ),
1163 py0
= vg_max( start
[1], full_start
[1] ),
1164 py1
= vg_min( end
[1], full_end
[1] );
1166 for( int y
= py0
; y
< py1
; y
++ )
1168 for( int x
= px0
; x
< px1
; x
++ )
1170 struct cell
*cell
= pcell((v2i
){x
,y
});
1172 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
1177 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1179 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
1181 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
1182 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1190 if( cell
->state
& FLAG_WALL
)
1196 pcell((v2i
){x
,y
})->config
= config
;
1198 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
1199 info_px
[0] = height
;
1200 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
1206 ((cell
->state
& FLAG_IS_TRIGGER
) && (cell
->config
== 0xF || cell
->config
== k_cell_type_split
)) ||
1207 ((cell
->state
& FLAG_TARGETED
) && (cell
->config
!= k_cell_type_split
))
1208 ) && update_texbuffer
1210 cell
->state
&= ~(FLAG_TARGETED
|FLAG_IS_TRIGGER
);
1211 for( u32 i
= 0; i
< 2; i
++ )
1213 if( cell
->links
[i
] )
1215 struct cell
*other_ptr
= &world
.data
[ cell
->links
[i
] ];
1216 other_ptr
->links
[ i
] = 0;
1217 other_ptr
->state
&= ~FLAG_IS_TRIGGER
;
1219 if( other_ptr
->links
[ i
^ 0x1 ] == 0 )
1220 other_ptr
->state
&= ~FLAG_TARGETED
;
1230 if( update_texbuffer
)
1232 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1233 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
1237 u16 id_drag_from
= 0;
1241 void vg_update(void)
1243 // Fit within screen
1245 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
1246 r2
= (float)world
.h
/ (float)world
.w
,
1249 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
1250 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
1253 origin
[0] = floorf( -0.5f
* world
.w
);
1254 origin
[1] = floorf( -0.5f
* world
.h
);
1257 m3x3_identity( m_view
);
1258 m3x3_translate( m_view
, origin
);
1259 m3x3_mul( m_projection
, m_view
, vg_pv
);
1260 vg_projection_update();
1263 v2_copy( vg_mouse_ws
, world
.tile_pos
);
1265 world
.tile_x
= floorf( world
.tile_pos
[0] );
1266 world
.tile_y
= floorf( world
.tile_pos
[1] );
1269 if( !world
.simulating
&& !gui_want_mouse() )
1271 v2_copy( vg_mouse_ws
, drag_to_co
);
1273 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
1275 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
1277 static u32 modify_state
= 0;
1279 struct cell
*cell_ptr
= &world
.data
[world
.selected
];
1281 if( vg_get_button_down("primary") )
1283 modify_state
= (cell_ptr
->state
& FLAG_CANAL
) ^ FLAG_CANAL
;
1286 if( vg_get_button("primary") && ((cell_ptr
->state
& FLAG_CANAL
) != modify_state
) )
1288 cell_ptr
->state
&= ~FLAG_CANAL
;
1289 cell_ptr
->state
|= modify_state
;
1291 if( cell_ptr
->state
& FLAG_CANAL
)
1293 cell_ptr
->links
[0] = 0;
1294 cell_ptr
->links
[1] = 0;
1296 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
1301 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
1305 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
1306 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
1309 if( vg_get_button_down("secondary") && !(cell_ptr
->config
== k_cell_type_split
) )
1311 id_drag_from
= world
.selected
;
1312 drag_from_co
[0] = world
.tile_x
+ 0.5f
;
1313 drag_from_co
[1] = world
.tile_y
+ 0.5f
;
1316 if( id_drag_from
&& (cell_ptr
->config
== k_cell_type_split
) )
1318 float local_x
= vg_mouse_ws
[0] - (float)world
.tile_x
;
1319 drag_to_co
[0] = (float)world
.tile_x
+ (local_x
> 0.5f
? 0.75f
: 0.25f
);
1320 drag_to_co
[1] = (float)world
.tile_y
+ 0.25f
;
1322 if( vg_get_button_up("secondary") )
1324 struct cell
*drag_ptr
= &world
.data
[id_drag_from
];
1325 u32 link_id
= local_x
> 0.5f
? 1: 0;
1327 // Cleanup existing connections
1328 if( cell_ptr
->links
[ link_id
] )
1330 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id
, cell_ptr
->links
[ link_id
] );
1332 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1333 current_connection
->state
&= ~FLAG_IS_TRIGGER
;
1334 current_connection
->links
[ link_id
] = 0;
1337 if( drag_ptr
->links
[ link_id
^ 0x1 ] )
1339 vg_warn( "Destroying alternate link %u (%hu)\n", link_id
^ 0x1, drag_ptr
->links
[ link_id
^ 0x1 ] );
1341 struct cell
*current_connection
= &world
.data
[ drag_ptr
->links
[ link_id
^ 0x1 ]];
1342 if( !current_connection
->links
[ link_id
] )
1343 current_connection
->state
&= ~FLAG_TARGETED
;
1345 current_connection
->links
[ link_id
^ 0x1 ] = 0;
1346 drag_ptr
->links
[ link_id
^ 0x1 ] = 0;
1349 // Create the new connection
1350 vg_success( "Creating connection on link %u (%hu)\n", link_id
, id_drag_from
);
1352 cell_ptr
->links
[ link_id
] = id_drag_from
;
1353 drag_ptr
->links
[ link_id
] = world
.selected
;
1355 cell_ptr
->state
|= FLAG_TARGETED
;
1356 drag_ptr
->state
|= FLAG_IS_TRIGGER
;
1362 world
.selected
= -1;
1364 if( !(vg_get_button("secondary") && id_drag_from
) )
1369 world
.selected
= -1;
1373 // Simulation stop/start
1374 if( vg_get_button_down("go") )
1376 if( world
.simulating
)
1382 vg_success( "Starting simulation!\n" );
1384 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
1386 world
.simulating
= 1;
1387 world
.num_fishes
= 0;
1388 world
.sim_frame
= 0;
1389 world
.sim_start
= vg_time
;
1391 world
.sim_speed
= 2.5f
;
1393 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1394 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1401 if( world
.simulating
)
1403 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*world
.sim_speed
) )
1405 //vg_info( "frame: %u\n", world.sim_frame );
1406 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
1408 // Update splitter deltas
1409 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1411 struct cell
*cell
= &world
.data
[i
];
1412 if( cell
->config
== k_cell_type_split
)
1414 cell
->state
&= ~FLAG_FLIP_ROTATING
;
1416 if( cell
->state
& FLAG_IS_TRIGGER
)
1417 cell
->state
&= ~FLAG_TRIGGERED
;
1420 int alive_count
= 0;
1422 // Update fish positions
1423 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1425 struct fish
*fish
= &world
.fishes
[i
];
1427 if( fish
->state
== k_fish_state_soon_dead
)
1428 fish
->state
= k_fish_state_dead
;
1430 if( fish
->state
== k_fish_state_soon_alive
)
1431 fish
->state
= k_fish_state_alive
;
1433 if( fish
->state
< k_fish_state_alive
)
1436 struct cell
*cell_current
= pcell( fish
->pos
);
1438 if( fish
->state
== k_fish_state_alive
)
1441 if( cell_current
->state
& FLAG_OUTPUT
)
1443 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
1445 struct cell_terminal
*term
= &world
.io
[j
];
1447 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
1449 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1450 if( run
->recv_count
< vg_list_size( run
->recieved
) )
1451 run
->recieved
[ run
->recv_count
++ ] = fish
->payload
;
1457 fish
->state
= k_fish_state_dead
;
1462 if( cell_current
->config
== k_cell_type_merge
)
1467 fish
->flow_reversed
= 0;
1471 if( cell_current
->config
== k_cell_type_split
)
1474 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
1477 if( !(cell_current
->state
& FLAG_TARGETED
) )
1478 cell_current
->state
^= FLAG_FLIP_FLOP
;
1482 // Apply cell out-flow
1483 struct cell_description
*desc
= &cell_descriptions
[ cell_current
->config
];
1485 v2i_copy( fish
->flow_reversed
? desc
->start
: desc
->end
, fish
->dir
);
1489 v2i_add( fish
->pos
, fish
->dir
, pos_next
);
1491 struct cell
*cell_next
= pcell( pos_next
);
1493 if( cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1495 struct cell_description
*desc
= &cell_descriptions
[ cell_next
->config
];
1497 if( cell_next
->config
== k_cell_type_merge
)
1499 if( fish
->dir
[0] == 0 )
1500 fish
->state
= k_fish_state_dead
;
1502 fish
->flow_reversed
= 0;
1506 if( cell_next
->config
== k_cell_type_split
)
1508 if( fish
->dir
[0] == 0 )
1510 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1511 cell_next
->state
|= FLAG_FLIP_ROTATING
;
1513 fish
->flow_reversed
= 0;
1516 fish
->state
= k_fish_state_dead
;
1519 fish
->flow_reversed
= ( fish
->dir
[0] != -desc
->start
[0] ||
1520 fish
->dir
[1] != -desc
->start
[1] )? 1: 0;
1524 fish
->state
= world_check_pos_ok( fish
->pos
)? k_fish_state_bg
: k_fish_state_dead
;
1527 //v2i_add( fish->pos, fish->dir, fish->pos );
1529 else if( fish
->state
== k_fish_state_bg
)
1531 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1533 if( !world_check_pos_ok( fish
->pos
) )
1534 fish
->state
= k_fish_state_dead
;
1537 struct cell
*cell_entry
= pcell( fish
->pos
);
1539 if( cell_entry
->state
& FLAG_CANAL
)
1541 if( cell_entry
->config
== k_cell_type_con_r
|| cell_entry
->config
== k_cell_type_con_u
1542 || cell_entry
->config
== k_cell_type_con_l
|| cell_entry
->config
== k_cell_type_con_d
)
1544 sw_set_achievement( "CAN_DO_THAT" );
1546 fish
->state
= k_fish_state_soon_alive
;
1550 fish
->flow_reversed
= 1;
1552 switch( cell_entry
->config
)
1554 case k_cell_type_con_r
: fish
->dir
[0] = 1; break;
1555 case k_cell_type_con_l
: fish
->dir
[0] = -1; break;
1556 case k_cell_type_con_u
: fish
->dir
[1] = 1; break;
1557 case k_cell_type_con_d
: fish
->dir
[1] = -1; break;
1563 else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
1565 if( fish
->state
>= k_fish_state_alive
)
1569 // Second pass (triggers)
1570 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1572 struct fish
*fish
= &world
.fishes
[i
];
1574 if( fish
->state
== k_fish_state_alive
)
1576 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1577 struct cell
*cell_current
= pcell( fish
->pos
);
1579 if( cell_current
->state
& FLAG_IS_TRIGGER
)
1581 int trigger_id
= cell_current
->links
[0]?0:1;
1582 int connection_id
= cell_current
->links
[trigger_id
];
1583 int target_px
= connection_id
% world
.w
;
1584 int target_py
= (connection_id
- target_px
)/world
.w
;
1586 vg_line2( (v2f
){ fish
->pos
[0], fish
->pos
[1] }, (v2f
){ target_px
, target_py
}, 0xffffffff, 0xffffffff );
1588 struct cell
*target_peice
= &world
.data
[ cell_current
->links
[trigger_id
] ];
1590 cell_current
->state
|= FLAG_TRIGGERED
;
1593 target_peice
->state
|= FLAG_FLIP_FLOP
;
1595 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1600 // Third pass (collisions)
1601 struct fish
*fi
, *fj
;
1603 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1605 fi
= &world
.fishes
[i
];
1607 if( fi
->state
== k_fish_state_alive
)
1609 int continue_again
= 0;
1611 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1613 fj
= &world
.fishes
[j
];
1615 if( (fj
->state
== k_fish_state_alive
) )
1620 v2i_sub( fi
->pos
, fi
->dir
, fi_prev
);
1621 v2i_sub( fj
->pos
, fj
->dir
, fj_prev
);
1624 collide_next_frame
= (
1625 (fi
->pos
[0] == fj
->pos
[0]) &&
1626 (fi
->pos
[1] == fj
->pos
[1]))? 1: 0,
1627 collide_this_frame
= (
1628 (fi_prev
[0] == fj
->pos
[0]) &&
1629 (fi_prev
[1] == fj
->pos
[1]) &&
1630 (fj_prev
[0] == fi
->pos
[0]) &&
1631 (fj_prev
[1] == fi
->pos
[1])
1634 if( collide_next_frame
|| collide_this_frame
)
1636 sw_set_achievement( "BANG" );
1638 // Shatter death (+0.5s)
1639 float death_time
= collide_this_frame
? 0.0f
: 0.5f
;
1641 fi
->state
= k_fish_state_soon_dead
;
1642 fj
->state
= k_fish_state_soon_dead
;
1643 fi
->death_time
= death_time
;
1644 fj
->death_time
= death_time
;
1651 if( continue_again
)
1657 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1659 struct cell_terminal
*term
= &world
.io
[ i
];
1660 int posx
= term
->id
% world
.w
;
1661 int posy
= (term
->id
- posx
)/world
.w
;
1662 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1666 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].condition_count
)
1668 struct fish
*fish
= &world
.fishes
[ world
.num_fishes
];
1669 fish
->pos
[0] = posx
;
1670 fish
->pos
[1] = posy
;
1671 fish
->state
= k_fish_state_alive
;
1672 fish
->payload
= term
->runs
[ world
.sim_run
].conditions
[ world
.sim_frame
];
1674 struct cell
*cell_ptr
= pcell( fish
->pos
);
1676 if( cell_ptr
->config
!= k_cell_type_stub
)
1678 struct cell_description
*desc
= &cell_descriptions
[ cell_ptr
->config
];
1680 v2i_copy( desc
->start
, fish
->dir
);
1681 fish
->flow_reversed
= 1;
1683 world
.num_fishes
++;
1690 if( alive_count
== 0 )
1692 world
.completed
= 1;
1694 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1696 struct cell_terminal
*term
= &world
.io
[ i
];
1697 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1701 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1703 if( run
->recv_count
== run
->condition_count
)
1705 for( int j
= 0; j
< run
->condition_count
; j
++ )
1707 if( run
->recieved
[j
] != run
->conditions
[j
] )
1709 world
.completed
= 0;
1716 world
.completed
= 0;
1722 if( world
.completed
)
1724 if( world
.sim_run
< world
.max_runs
-1 )
1726 vg_success( "Run passed, starting next\n" );
1728 world
.sim_frame
= 0;
1729 world
.sim_start
= vg_time
;
1730 world
.num_fishes
= 0;
1732 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1733 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1739 vg_success( "Level passed!\n" );
1742 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1743 if( world
.data
[ i
].state
& FLAG_CANAL
)
1746 world
.score
= score
;
1747 world
.time
= world
.sim_frame
;
1752 if( world
.sim_run
> 0 )
1753 sw_set_achievement( "GOOD_ENOUGH" );
1755 vg_error( "Level failed :(\n" );
1758 // Copy into career data
1759 if( world
.ptr_career_level
)
1761 world
.ptr_career_level
->score
= world
.score
;
1762 world
.ptr_career_level
->time
= world
.time
;
1763 world
.ptr_career_level
->completed
= world
.completed
;
1766 simulation_stop(); // TODO: Async?
1773 float scaled_time
= 0.0f
;
1774 scaled_time
= (vg_time
-world
.sim_start
)*world
.sim_speed
;
1775 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1778 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1780 struct fish
*fish
= &world
.fishes
[i
];
1782 if( fish
->state
== k_fish_state_dead
)
1785 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1786 continue; // Todo: particle thing?
1788 struct cell
*cell
= pcell(fish
->pos
);
1789 struct cell_description
*desc
= &cell_descriptions
[ cell
->config
];
1793 float t
= world
.frame_lerp
;
1794 if( fish
->flow_reversed
&& !desc
->is_linear
)
1797 v2_copy( fish
->physics_co
, fish
->physics_v
);
1799 switch( cell
->config
)
1801 case k_cell_type_merge
:
1802 if( fish
->dir
[0] == 1 )
1807 case k_cell_type_con_r
: curve
= curve_1
; break;
1808 case k_cell_type_con_l
: curve
= curve_4
; break;
1809 case k_cell_type_con_u
: curve
= curve_2
; break;
1810 case k_cell_type_con_d
: curve
= curve_8
; break;
1811 case 3: curve
= curve_3
; break;
1812 case 6: curve
= curve_6
; break;
1813 case 9: curve
= curve_9
; break;
1814 case 12: curve
= curve_12
; break;
1816 if( t
> curve_7_linear_section
)
1818 t
-= curve_7_linear_section
;
1819 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1821 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1825 default: curve
= NULL
; break;
1831 float t3
= t
* t
* t
;
1833 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1834 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1835 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1837 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1838 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1839 fish
->physics_co
[0] += (float)fish
->pos
[0];
1840 fish
->physics_co
[1] += (float)fish
->pos
[1];
1845 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1846 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1848 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1849 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1855 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1857 v2i full_start
= { 0,0 };
1858 v2i full_end
= { world
.w
, world
.h
};
1860 if( !start
|| !end
)
1866 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1868 for( int y
= start
[1]; y
< end
[1]; y
++ )
1870 for( int x
= start
[0]; x
< end
[0]; x
++ )
1872 struct cell
*cell
= pcell((v2i
){x
,y
});
1873 int selected
= world
.selected
== y
*world
.w
+ x
;
1875 int tile_offsets
[][2] =
1877 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1878 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1879 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1880 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1883 int uv
[2] = { 3, 0 };
1885 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1887 uv
[0] = tile_offsets
[ cell
->config
][0];
1888 uv
[1] = tile_offsets
[ cell
->config
][1];
1891 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1894 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1896 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1904 static void draw_numbers( v3f coord
, int number
)
1907 v3_copy( coord
, pos
);
1908 int digits
[8]; int i
= 0;
1910 while( number
> 0 && i
< 8 )
1912 digits
[i
++] = number
% 10;
1913 number
= number
/ 10;
1916 for( int j
= 0; j
< i
; j
++ )
1918 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
1919 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
1920 pos
[0] += pos
[2] * 0.75f
;
1924 void vg_render(void)
1926 glViewport( 0,0, vg_window_x
, vg_window_y
);
1928 glDisable( GL_DEPTH_TEST
);
1929 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1930 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1932 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1933 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1935 // TILE SET RENDERING
1936 // todo: just slam everything into a mesh...
1937 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1939 // Currently we're uploading a fair amount of data every frame anyway.
1940 // NOTE: this is for final optimisations ONLY!
1941 // ======================================================================
1943 use_mesh( &world
.tile
);
1949 SHADER_USE( shader_background
);
1950 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1952 glActiveTexture( GL_TEXTURE0
);
1953 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1954 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1956 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1957 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1959 glActiveTexture( GL_TEXTURE1
);
1960 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1961 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1969 ui_begin( &ui_global_ctx
, 512, 256 );
1971 ui_global_ctx
.override_colour
= 0xff9a8a89;
1972 //ui_text( &ui_global_ctx, world.map_title, 6, 0 );
1973 ui_global_ctx
.override_colour
= 0xffffffff;
1975 ui_resolve( &ui_global_ctx
);
1978 m3x3_copy( vg_pv
, world_text
);
1979 m3x3_translate( world_text
, (v3f
){ 1.55f
, 1.9f
, 0.0f
} );
1980 m3x3_rotate( world_text
, VG_PIf
*0.5f
);
1981 m3x3_scale( world_text
, (v3f
){0.01f
,-0.01f
,0.01f
} );
1983 ui_draw( &ui_global_ctx
, world_text
);
1986 // =========================================================================================
1988 use_mesh( &world
.tile
);
1989 SHADER_USE( shader_tile_main
);
1992 m2x2_identity( subtransform
);
1993 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1994 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1995 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1996 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
1999 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2000 glBlendEquation(GL_FUNC_ADD
);
2003 vg_tex2d_bind( &tex_tile_data
, 0 );
2004 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
2006 vg_tex2d_bind( &tex_wood
, 1 );
2007 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
2009 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
2013 SHADER_USE( shader_ball
);
2014 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2016 vg_tex2d_bind( &tex_ball_noise
, 0 );
2017 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
2020 if( world
.simulating
)
2022 for( int i
= 0; i
< world
.num_fishes
; i
++ )
2024 struct fish
*fish
= &world
.fishes
[i
];
2026 if( fish
->state
== k_fish_state_dead
|| fish
->state
== k_fish_state_bg
)
2029 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
2032 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2033 colour_code_v3( fish
->payload
, dot_colour
);
2035 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
2036 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
2037 glUniform2f( SHADER_UNIFORM( shader_ball
, "uTexOffset" ), (float)i
* 1.2334, (float)i
* -0.3579f
);
2042 SHADER_USE( shader_tile_main
);
2045 vg_tex2d_bind( &tex_tile_data
, 0 );
2046 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
2048 vg_tex2d_bind( &tex_wood
, 1 );
2049 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
2051 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
2052 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
2055 for( int y
= 2; y
< world
.h
-2; y
++ )
2057 for( int x
= 2; x
< world
.w
-2; x
++ )
2059 struct cell
*cell
= pcell((v2i
){x
,y
});
2061 if( cell
->state
& FLAG_CANAL
)
2063 if( cell
->config
== k_cell_type_split
)
2065 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
2067 if( cell
->state
& FLAG_FLIP_ROTATING
)
2069 if( (world
.frame_lerp
> curve_7_linear_section
) )
2071 float const rotation_speed
= 0.4f
;
2072 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
2074 float t
= world
.frame_lerp
- curve_7_linear_section
;
2075 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
2085 m2x2_create_rotation( subtransform
, rotation
);
2087 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2088 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, cell
->state
& FLAG_TARGETED
? 3.0f
: 0.0f
, 0.0f
);
2096 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) && !id_drag_from
)
2098 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
2099 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
2101 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2102 map_reclassify( new_begin
, new_end
, 0 );
2104 m2x2_identity( subtransform
);
2105 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
2106 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2107 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
2109 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
2111 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2112 map_reclassify( new_begin
, new_end
, 0 );
2115 //glDisable(GL_BLEND);
2117 // Draw connecting wires
2118 glDisable(GL_BLEND
);
2120 SHADER_USE( shader_wire
);
2121 glBindVertexArray( world
.wire
.vao
);
2123 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2124 glUniform4f( SHADER_UNIFORM( shader_wire
, "uColour" ), 0.2f
, 0.2f
, 0.2f
, 1.0f
);
2128 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), 0.4f
);
2129 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), drag_from_co
[0], drag_from_co
[1], 0.06f
);
2130 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), drag_to_co
[0], drag_to_co
[1], 0.06f
);
2131 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2134 float rp_x1
= world
.frame_lerp
*9.0f
;
2135 float rp_x2
= 1.0f
-rp_x1
*expf(1.0f
-rp_x1
)* 0.36f
;
2137 for( int y
= 2; y
< world
.h
-2; y
++ )
2139 for( int x
= 2; x
< world
.w
-2; x
++ )
2141 struct cell
*cell
= pcell((v2i
){x
,y
});
2143 if( cell
->state
& FLAG_CANAL
)
2145 if( cell
->state
& FLAG_IS_TRIGGER
)
2147 int trigger_id
= cell
->links
[0]?0:1;
2149 int x2
= cell
->links
[trigger_id
] % world
.w
;
2150 int y2
= (cell
->links
[trigger_id
] - x2
) / world
.w
;
2155 startpoint
[0] = (float)x2
+ (trigger_id
? 0.75f
: 0.25f
);
2156 startpoint
[1] = (float)y2
+ 0.25f
;
2158 endpoint
[0] = x
+0.5f
;
2159 endpoint
[1] = y
+0.5f
;
2161 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), cell
->state
& FLAG_TRIGGERED
? rp_x2
* 0.4f
: 0.4f
);
2162 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), startpoint
[0], startpoint
[1], 0.04f
);
2163 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), endpoint
[0], endpoint
[1], 0.04f
);
2164 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2170 SHADER_USE( shader_tile_colour
);
2171 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2172 use_mesh( &world
.circle
);
2174 int const filled_start
= 0;
2175 int const filled_count
= 32;
2176 int const empty_start
= 32;
2177 int const empty_count
= 32*2;
2180 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2182 struct cell_terminal
*term
= &world
.io
[ i
];
2183 int posx
= term
->id
% world
.w
;
2184 int posy
= (term
->id
- posx
)/world
.w
;
2185 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
2187 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2189 for( int k
= 0; k
< term
->run_count
; k
++ )
2191 for( int j
= 0; j
< term
->runs
[k
].condition_count
; j
++ )
2193 float y_offset
= is_input
? 1.2f
: -0.2f
;
2194 y_offset
+= (is_input
? 0.2f
: -0.2f
) * (float)k
;
2196 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
2200 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2201 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2203 // Draw filled if tick not passed, draw empty if empty
2204 if( (world
.sim_frame
> j
&& world
.sim_run
>= k
) || world
.sim_run
> k
)
2205 draw_mesh( empty_start
, empty_count
);
2207 draw_mesh( filled_start
, filled_count
);
2211 if( term
->runs
[k
].recv_count
> j
)
2213 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
2214 v3_muls( dot_colour
, 0.8f
, dot_colour
);
2215 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2217 draw_mesh( filled_start
, filled_count
);
2220 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2221 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2223 draw_mesh( empty_start
, empty_count
);
2229 if( world
.simulating
)
2231 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
2232 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
);
2233 draw_mesh( filled_start
, filled_count
);
2237 float const score_bright
= 1.25f
;
2238 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
2239 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
2241 use_mesh( &world
.numbers
);
2242 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
2245 static ui_colourset flcol_list_a
= {
2247 .hover
= 0xffa09393,
2248 .active
= 0xffbfb1b0
2250 static ui_colourset flcol_list_b
= {
2252 .hover
= 0xffa09393,
2253 .active
= 0xffbfb1b0
2256 static ui_colourset flcol_list_complete_a
= {
2258 .hover
= 0xff8dc18f,
2259 .active
= 0xffb2ddb3
2262 static ui_colourset flcol_list_complete_b
= {
2264 .hover
= 0xff8dc18f,
2265 .active
= 0xffb2ddb3
2268 static ui_colourset flcol_list_locked
= {
2270 .hover
= 0xff655959,
2271 .active
= 0xff655959
2276 static void draw_levels_list( struct cmp_level
*levels
, int count
, int unlocked
)
2278 static struct ui_scrollbar sb
= {
2282 ui_px view_height
= ui_global_ctx
.cursor
[3];
2283 ui_px level_height
= 50;
2285 // Level scroll view
2288 gui_fill_rect( ui_global_ctx
.cursor
, 0xff5a4e4d );
2289 gui_set_clip( ui_global_ctx
.cursor
);
2291 ui_global_ctx
.cursor
[2] = 14;
2294 ui_px content_height
= count
*level_height
;
2295 if( content_height
> view_height
)
2297 ui_scrollbar( &ui_global_ctx
, &sb
, 1 );
2298 ui_global_ctx
.cursor
[1] -= ui_calculate_content_scroll( &sb
, content_height
);
2302 gui_fill_rect( ui_global_ctx
.cursor
, 0xff807373 );
2305 ui_global_ctx
.cursor
[2] = 240;
2306 ui_global_ctx
.cursor
[3] = level_height
;
2309 for( int i
= 0; i
< count
; i
++ )
2311 struct cmp_level
*lvl_info
= &levels
[i
];
2315 if( lvl_info
->completed_score
!= 0 )
2316 gui_override_colours( i
&0x1? &flcol_list_complete_a
: &flcol_list_complete_b
);
2318 gui_override_colours( i
&0x1? &flcol_list_a
: &flcol_list_b
);
2321 gui_override_colours( &flcol_list_locked
);
2325 if( gui_button( 2 + i
) == k_button_click
)
2327 console_changelevel( 1, &lvl_info
->map_name
);
2330 ui_global_ctx
.override_colour
= 0xffffffff;
2331 gui_text( lvl_info
->title
, 3, 0 );
2332 ui_global_ctx
.cursor
[1] += 18;
2333 gui_text( "incomplete", 2, 0 );
2337 gui_button( 2 + i
);
2339 ui_global_ctx
.override_colour
= 0xff786f6f;
2340 gui_text( "???", 3, 0 );
2341 ui_global_ctx
.cursor
[1] += 18;
2342 gui_text( "locked", 2, 0 );
2348 gui_reset_colours();
2356 ui_global_ctx
.cursor
[0] = 0;
2357 ui_global_ctx
.cursor
[1] = 0;
2358 ui_global_ctx
.cursor
[2] = 256;
2362 ui_global_ctx
.id_base
= 4 << 16;
2364 static int pack_selection
= 0;
2365 static struct pack_info
2367 struct cmp_level
*levels
;
2374 .levels
= cmp_levels_tutorials
,
2375 .level_count
= vg_list_size(cmp_levels_tutorials
),
2379 .levels
= cmp_levels_basic
,
2380 .level_count
= vg_list_size(cmp_levels_basic
),
2384 .levels
= cmp_levels_grad
,
2385 .level_count
= vg_list_size(cmp_levels_tutorials
),
2392 gui_fill_rect( ui_global_ctx
.cursor
, 0xff5577ff );
2394 gui_text( "ASSIGNMENTS", 4, 0 );
2396 ui_global_ctx
.cursor
[1] += 30;
2397 ui_global_ctx
.cursor
[3] = 25;
2401 ui_rect_pad( ui_global_ctx
.cursor
, 2 );
2402 ui_global_ctx
.cursor
[2] = 84;
2404 for( int i
= 0; i
< 3; i
++ )
2406 if( i
== pack_selection
)
2407 gui_override_colours( &flcol_list_locked
);
2409 if( gui_button( 2000 + i
) == k_button_click
)
2412 ui_global_ctx
.cursor
[1] += 2;
2413 gui_text( pack_infos
[i
].name
, 2, 0 );
2416 gui_reset_colours();
2421 ui_global_ctx
.cursor
[3] = 500;
2423 draw_levels_list( pack_infos
[ pack_selection
].levels
, pack_infos
[ pack_selection
].level_count
, 3 );