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;
360 struct cmp_level
*pCmpLevel
;
367 void leaderboard_set_score( struct cmp_level
*cmp_level
, u32 score
);
369 static void map_free(void)
371 arrfree( world
.data
);
382 world
.initialzed
= 0;
385 static void io_reset(void)
387 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
389 struct cell_terminal
*term
= &world
.io
[i
];
391 for( int j
= 0; j
< term
->run_count
; j
++ )
392 term
->runs
[j
].recv_count
= 0;
396 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
);
397 static int map_load( const char *str
, const char *name
)
399 //TODO: It may be worthwhile, at this point, to switch to binary encoding for save data
408 if( c
[world
.w
] == ';' )
410 else if( !c
[world
.w
] )
412 vg_error( "Unexpected EOF when parsing level\n" );
417 struct cell
*row
= arraddnptr( world
.data
, world
.w
);
419 int reg_start
= 0, reg_end
= 0;
421 u32
*links_to_make
= NULL
;
422 int links_satisfied
= 0;
424 char link_id_buffer
[32];
432 if( *c
== '\r' ) { c
++; continue; } // fuck off windows
438 if( *c
== '\r' ) c
++;
445 if( *c
== '\r' ) { c
++; continue; }
447 if( reg_start
< reg_end
)
449 struct cell_terminal
*terminal
= &world
.io
[ reg_start
];
450 struct terminal_run
*run
= &terminal
->runs
[ terminal
->run_count
-1 ];
452 if( *c
>= 'a' && *c
<= 'z' )
454 run
->conditions
[ run
->condition_count
++ ] = *c
;
458 if( *c
== ',' || *c
== '\n' )
467 terminal
->runs
[ terminal
->run_count
].condition_count
= 0;
468 terminal
->run_count
++;
469 world
.max_runs
= vg_max( world
.max_runs
, terminal
->run_count
);
473 vg_error( "Unkown attribute '%c' (row: %u)\n", *c
, world
.h
);
480 if( links_satisfied
< arrlen( links_to_make
) )
482 struct cell
*target
= &world
.data
[ links_to_make
[ links_satisfied
] ];
484 if( (((u32
)*c
>= (u32
)'0') && ((u32
)*c
<= (u32
)'9')) || *c
== '-' )
486 if( link_id_n
>= vg_list_size( link_id_buffer
)-1 )
488 vg_error( "Number was way too long to be parsed (row: %u)\n", world
.h
);
492 link_id_buffer
[ link_id_n
++ ] = *c
;
494 else if( *c
== ',' || *c
== '\n' )
496 link_id_buffer
[ link_id_n
] = 0x00;
497 int value
= atoi( link_id_buffer
);
499 target
->links
[value
>= 0? 1:0] = abs(value
);
508 vg_error( "Invalid character '%c' (row: %u)\n", *c
, world
.h
);
514 vg_error( "Too many values to assign (row: %u)\n", world
.h
);
523 // Registry length-error checks
524 if( reg_start
!= reg_end
)
526 vg_error( "Not enough spawn values assigned (row: %u, %u of %u)\n", world
.h
, reg_start
, reg_end
);
530 if( links_satisfied
!= arrlen( links_to_make
) )
532 vg_error( "Not enough link values assigned (row: %u, %u of %u)\n", world
.h
, links_satisfied
, arrlen( links_to_make
) );
538 vg_error( "Not enough cells to match previous row definition (row: %u, %u<%u)\n", world
.h
, cx
, world
.w
);
542 row
= arraddnptr( world
.data
, world
.w
);
545 reg_end
= reg_start
= arrlen( world
.io
);
547 arrsetlen( links_to_make
, 0 );
554 vg_error( "Too many cells to match previous row definition (row: %u, %u>%u)\n", world
.h
, cx
, world
.w
);
558 // Tile initialization
560 struct cell
*cell
= &row
[ cx
];
563 if( *c
== '+' || *c
== '-' )
565 struct cell_terminal
*term
= arraddnptr( world
.io
, 1 );
566 term
->id
= cx
+ world
.h
*world
.w
;
568 term
->runs
[0].condition_count
= 0;
570 cell
->state
= *c
== '+'? FLAG_INPUT
: FLAG_OUTPUT
;
573 else if( *c
== '#' ) cell
->state
= FLAG_WALL
;
574 else if( ((u32
)*c
>= (u32
)'A') && ((u32
)*c
<= (u32
)'A'+0xf) )
576 // Canal flag bits (4bit/16 value):
582 cell
->state
= ((u32
)*c
- (u32
)'A') & (FLAG_CANAL
|FLAG_IS_TRIGGER
);
584 if( cell
->state
& FLAG_IS_TRIGGER
)
585 arrpush( links_to_make
, cx
+ world
.h
*world
.w
);
591 else cell
->state
= 0x00;
599 // Update data texture to fill out the background
601 u8 info_buffer
[64*64*4];
602 for( int i
= 0; i
< 64*64; i
++ )
604 u8
*px
= &info_buffer
[i
*4];
611 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
612 glTexSubImage2D( GL_TEXTURE_2D
, 0, 0, 0, 64, 64, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
615 arrfree( links_to_make
);
617 map_reclassify( NULL
, NULL
, 1 );
620 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
622 struct cell
*src
= &world
.data
[i
];
623 if( src
->state
& FLAG_IS_TRIGGER
)
625 int link_id
= src
->links
[0]?0:1;
626 if( src
->links
[link_id
] <= world
.h
*world
.w
)
628 struct cell
*target
= &world
.data
[ src
->links
[link_id
] ];
629 if( (target
->state
& FLAG_CANAL
) && (target
->config
== k_cell_type_split
) )
631 if( target
->links
[ link_id
] )
633 vg_error( "Link target was already targeted\n" );
639 target
->links
[ link_id
] = i
;
640 target
->state
|= FLAG_TARGETED
;
645 vg_error( "Link target was invalid\n" );
651 vg_error( "Link target out of bounds\n" );
657 vg_success( "Map '%s' loaded! (%u:%u)\n", name
, world
.w
, world
.h
);
661 strncpy( world
.map_name
, name
, vg_list_size( world
.map_name
)-1 );
662 world
.initialzed
= 1;
666 arrfree( links_to_make
);
671 static struct cell
*pcell( v2i pos
)
673 return &world
.data
[ pos
[1]*world
.w
+ pos
[0] ];
676 static void map_serialize( FILE *stream
)
678 for( int y
= 0; y
< world
.h
; y
++ )
680 for( int x
= 0; x
< world
.w
; x
++ )
682 struct cell
*cell
= pcell( (v2i
){ x
, y
} );
684 if( cell
->state
& FLAG_WALL
) fputc( '#', stream
);
685 else if( cell
->state
& FLAG_INPUT
) fputc( '+', stream
);
686 else if( cell
->state
& FLAG_OUTPUT
) fputc( '-', stream
);
687 else if( cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
) )
689 fputc( (cell
->state
& (FLAG_CANAL
|FLAG_IS_TRIGGER
|FLAG_RESERVED0
|FLAG_RESERVED1
)) + (u32
)'A', stream
);
691 else fputc( ' ', stream
);
694 fputc( ';', stream
);
696 int terminal_write_count
= 0;
698 for( int x
= 0; x
< world
.w
; x
++ )
700 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
702 struct cell_terminal
*term
= &world
.io
[i
];
703 if( term
->id
== y
*world
.w
+x
)
705 if( terminal_write_count
)
706 fputc( ',', stream
);
707 terminal_write_count
++;
709 for( int j
= 0; j
< term
->run_count
; j
++ )
711 struct terminal_run
*run
= &term
->runs
[j
];
713 for( int k
= 0; k
< run
->condition_count
; k
++ )
714 fputc( run
->conditions
[k
], stream
);
716 if( j
< term
->run_count
-1 )
717 fputc( ':', stream
);
723 for( int x
= 0; x
< world
.w
; x
++ )
725 struct cell
*cell
= pcell( (v2i
){ x
,y
} );
726 if( cell
->state
& FLAG_IS_TRIGGER
)
728 if( terminal_write_count
)
729 fputc( ',', stream
);
730 terminal_write_count
++;
732 fprintf( stream
, "%d", cell
->links
[0]? -cell
->links
[0]: cell
->links
[1] );
736 fputc( '\n', stream
);
740 int main( int argc
, char *argv
[] )
742 vg_init( argc
, argv
, "Marble Computing | SPACE: Test | LeftClick: Toggle tile | RightClick: Drag wire" );
746 static int console_credits( int argc
, char const *argv
[] )
748 vg_info( "Aknowledgements:\n" );
749 vg_info( " GLFW zlib/libpng glfw.org\n" );
750 vg_info( " miniaudio MIT0 miniaud.io\n" );
751 vg_info( " QOI MIT phoboslab.org\n" );
752 vg_info( " STB library MIT nothings.org\n" );
753 vg_info( " Weiholmir font justfredrik.itch.io\n" );
757 static int console_save_map( int argc
, char const *argv
[] )
759 if( !world
.initialzed
)
761 vg_error( "Tried to save uninitialized map!\n" );
765 char map_path
[ 256 ];
767 strcpy( map_path
, "sav/" );
768 strcat( map_path
, world
.map_name
);
769 strcat( map_path
, ".map" );
771 FILE *test_writer
= fopen( map_path
, "wb" );
774 vg_info( "Saving map to '%s'\n", map_path
);
775 map_serialize( test_writer
);
777 fclose( test_writer
);
782 vg_error( "Unable to open stream for writing\n" );
787 static int console_load_map( int argc
, char const *argv
[] )
789 char map_path
[ 256 ];
794 strcpy( map_path
, "sav/" );
795 strcat( map_path
, argv
[0] );
796 strcat( map_path
, ".map" );
798 char *text_source
= vg_textasset_read( map_path
);
802 strcpy( map_path
, "maps/" );
803 strcat( map_path
, argv
[0] );
804 strcat( map_path
, ".map" );
806 text_source
= vg_textasset_read( map_path
);
811 vg_info( "Loading map: '%s'\n", map_path
);
812 world
.pCmpLevel
= NULL
;
814 if( !map_load( text_source
, argv
[0] ) )
825 vg_error( "Missing maps '%s'\n", argv
[0] );
831 vg_error( "Missing argument <map_path>\n" );
836 static void simulation_stop(void)
838 world
.simulating
= 0;
839 world
.num_fishes
= 0;
845 sfx_system_fadeout( &audio_system_balls_rolling
, 44100 );
847 vg_info( "Stopping simulation!\n" );
850 static int console_changelevel( int argc
, char const *argv
[] )
854 // Save current level
855 console_save_map( 0, NULL
);
856 if( console_load_map( argc
, argv
) )
864 vg_error( "Missing argument <map_path>\n" );
870 void leaderboard_found( LeaderboardFindResult_t
*pCallback
);
871 void leaderboard_downloaded( LeaderboardScoresDownloaded_t
*pCallback
);
875 // Steamworks callbacks
876 sw_leaderboard_found
= &leaderboard_found
;
877 sw_leaderboard_downloaded
= &leaderboard_downloaded
;
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
);
1038 console_save_map( 0, NULL
);
1041 resource_free_main();
1043 glDeleteTextures( 1, &world
.background_data
);
1044 glDeleteTextures( 1, &world
.random_samples
);
1046 glDeleteVertexArrays( 1, &world
.wire
.vao
);
1047 glDeleteBuffers( 1, &world
.wire
.vbo
);
1048 glDeleteBuffers( 1, &world
.wire
.ebo
);
1050 free_mesh( &world
.tile
);
1051 free_mesh( &world
.circle
);
1052 free_mesh( &world
.numbers
);
1057 static int world_check_pos_ok( v2i co
)
1059 return (co
[0] < 2 || co
[0] >= world
.w
-2 || co
[1] < 2 || co
[1] >= world
.h
-2)? 0: 1;
1062 static int cell_interactive( v2i co
)
1065 if( !world_check_pos_ok( co
) )
1069 if( world
.data
[ world
.w
*co
[1] + co
[0] ].state
& (FLAG_WALL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1072 // List of 3x3 configurations that we do not allow
1073 static u32 invalid_src
[][9] =
1105 // Statically compile invalid configurations into bitmasks
1106 static u32 invalid
[ vg_list_size(invalid_src
) ];
1108 for( int i
= 0; i
< vg_list_size(invalid_src
); i
++ )
1112 for( int j
= 0; j
< 3; j
++ )
1113 for( int k
= 0; k
< 3; k
++ )
1114 comped
|= invalid_src
[i
][ j
*3+k
] << ((j
*5)+k
);
1116 invalid
[i
] = comped
;
1119 // Extract 5x5 grid surrounding tile
1121 for( int y
= co
[1]-2; y
< co
[1]+3; y
++ )
1122 for( int x
= co
[0]-2; x
< co
[0]+3; x
++ )
1124 struct cell
*cell
= pcell((v2i
){x
,y
});
1126 if( cell
&& (cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
)) )
1127 blob
|= 0x1 << ((y
-(co
[1]-2))*5 + x
-(co
[0]-2));
1130 // Run filter over center 3x3 grid to check for invalid configurations
1131 int kernel
[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
1132 for( int i
= 0; i
< vg_list_size(kernel
); i
++ )
1134 if( blob
& (0x1 << (6+kernel
[i
])) )
1136 u32 window
= blob
>> kernel
[i
];
1138 for( int j
= 0; j
< vg_list_size(invalid
); j
++ )
1139 if((window
& invalid
[j
]) == invalid
[j
])
1147 static void map_reclassify( v2i start
, v2i end
, int update_texbuffer
)
1149 v2i full_start
= { 1,1 };
1150 v2i full_end
= { world
.w
-1, world
.h
-1 };
1152 if( !start
|| !end
)
1159 u8 info_buffer
[64*64*4];
1162 int px0
= vg_max( start
[0], full_start
[0] ),
1163 px1
= vg_min( end
[0], full_end
[0] ),
1164 py0
= vg_max( start
[1], full_start
[1] ),
1165 py1
= vg_min( end
[1], full_end
[1] );
1167 for( int y
= py0
; y
< py1
; y
++ )
1169 for( int x
= px0
; x
< px1
; x
++ )
1171 struct cell
*cell
= pcell((v2i
){x
,y
});
1173 v2i dirs
[] = {{1,0},{0,1},{-1,0},{0,-1}};
1178 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1180 for( int i
= 0; i
< vg_list_size( dirs
); i
++ )
1182 struct cell
*neighbour
= pcell((v2i
){x
+dirs
[i
][0], y
+dirs
[i
][1]});
1183 if( neighbour
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1191 if( cell
->state
& FLAG_WALL
)
1197 pcell((v2i
){x
,y
})->config
= config
;
1199 u8
*info_px
= &info_buffer
[ (pixel_id
++)*4 ];
1200 info_px
[0] = height
;
1201 info_px
[1] = cell
->state
& FLAG_WALL
? 0: 255;
1207 ((cell
->state
& FLAG_IS_TRIGGER
) && (cell
->config
== 0xF || cell
->config
== k_cell_type_split
)) ||
1208 ((cell
->state
& FLAG_TARGETED
) && (cell
->config
!= k_cell_type_split
))
1209 ) && update_texbuffer
1211 cell
->state
&= ~(FLAG_TARGETED
|FLAG_IS_TRIGGER
);
1212 for( u32 i
= 0; i
< 2; i
++ )
1214 if( cell
->links
[i
] )
1216 struct cell
*other_ptr
= &world
.data
[ cell
->links
[i
] ];
1217 other_ptr
->links
[ i
] = 0;
1218 other_ptr
->state
&= ~FLAG_IS_TRIGGER
;
1220 if( other_ptr
->links
[ i
^ 0x1 ] == 0 )
1221 other_ptr
->state
&= ~FLAG_TARGETED
;
1231 if( update_texbuffer
)
1233 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1234 glTexSubImage2D( GL_TEXTURE_2D
, 0, px0
+ 16, py0
+ 16, px1
-px0
, py1
-py0
, GL_RGBA
, GL_UNSIGNED_BYTE
, info_buffer
);
1238 u16 id_drag_from
= 0;
1242 void vg_update(void)
1244 // Fit within screen
1246 float r1
= (float)vg_window_y
/ (float)vg_window_x
,
1247 r2
= (float)world
.h
/ (float)world
.w
,
1250 size
= ( r2
< r1
? (float)world
.w
* 0.5f
: ((float)world
.h
* 0.5f
) / r1
) + 2.5f
;
1251 m3x3_projection( m_projection
, -size
, size
, -size
*r1
, size
*r1
);
1254 origin
[0] = floorf( -0.5f
* world
.w
);
1255 origin
[1] = floorf( -0.5f
* world
.h
);
1258 m3x3_identity( m_view
);
1259 m3x3_translate( m_view
, origin
);
1260 m3x3_mul( m_projection
, m_view
, vg_pv
);
1261 vg_projection_update();
1264 v2_copy( vg_mouse_ws
, world
.tile_pos
);
1266 world
.tile_x
= floorf( world
.tile_pos
[0] );
1267 world
.tile_y
= floorf( world
.tile_pos
[1] );
1270 if( !world
.simulating
&& !gui_want_mouse() )
1272 v2_copy( vg_mouse_ws
, drag_to_co
);
1274 if( cell_interactive( (v2i
){ world
.tile_x
, world
.tile_y
} ))
1276 world
.selected
= world
.tile_y
* world
.w
+ world
.tile_x
;
1278 static u32 modify_state
= 0;
1280 struct cell
*cell_ptr
= &world
.data
[world
.selected
];
1282 if( vg_get_button_down("primary") )
1284 modify_state
= (cell_ptr
->state
& FLAG_CANAL
) ^ FLAG_CANAL
;
1287 if( vg_get_button("primary") && ((cell_ptr
->state
& FLAG_CANAL
) != modify_state
) )
1289 cell_ptr
->state
&= ~FLAG_CANAL
;
1290 cell_ptr
->state
|= modify_state
;
1292 if( cell_ptr
->state
& FLAG_CANAL
)
1294 cell_ptr
->links
[0] = 0;
1295 cell_ptr
->links
[1] = 0;
1297 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 3, 6 );
1302 sfx_set_playrnd( &audio_tile_mod
, &audio_system_sfx
, 0, 3 );
1306 map_reclassify( (v2i
){ world
.tile_x
-2, world
.tile_y
-2 },
1307 (v2i
){ world
.tile_x
+2, world
.tile_y
+2 }, 1 );
1310 if( vg_get_button_down("secondary") && !(cell_ptr
->config
== k_cell_type_split
) )
1312 id_drag_from
= world
.selected
;
1313 drag_from_co
[0] = world
.tile_x
+ 0.5f
;
1314 drag_from_co
[1] = world
.tile_y
+ 0.5f
;
1317 if( id_drag_from
&& (cell_ptr
->config
== k_cell_type_split
) )
1319 float local_x
= vg_mouse_ws
[0] - (float)world
.tile_x
;
1320 drag_to_co
[0] = (float)world
.tile_x
+ (local_x
> 0.5f
? 0.75f
: 0.25f
);
1321 drag_to_co
[1] = (float)world
.tile_y
+ 0.25f
;
1323 if( vg_get_button_up("secondary") )
1325 struct cell
*drag_ptr
= &world
.data
[id_drag_from
];
1326 u32 link_id
= local_x
> 0.5f
? 1: 0;
1328 // Cleanup existing connections
1329 if( cell_ptr
->links
[ link_id
] )
1331 vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id
, cell_ptr
->links
[ link_id
] );
1333 struct cell
*current_connection
= &world
.data
[ cell_ptr
->links
[ link_id
]];
1334 current_connection
->state
&= ~FLAG_IS_TRIGGER
;
1335 current_connection
->links
[ link_id
] = 0;
1338 if( drag_ptr
->links
[ link_id
^ 0x1 ] )
1340 vg_warn( "Destroying alternate link %u (%hu)\n", link_id
^ 0x1, drag_ptr
->links
[ link_id
^ 0x1 ] );
1342 struct cell
*current_connection
= &world
.data
[ drag_ptr
->links
[ link_id
^ 0x1 ]];
1343 if( !current_connection
->links
[ link_id
] )
1344 current_connection
->state
&= ~FLAG_TARGETED
;
1346 current_connection
->links
[ link_id
^ 0x1 ] = 0;
1347 drag_ptr
->links
[ link_id
^ 0x1 ] = 0;
1350 // Create the new connection
1351 vg_success( "Creating connection on link %u (%hu)\n", link_id
, id_drag_from
);
1353 cell_ptr
->links
[ link_id
] = id_drag_from
;
1354 drag_ptr
->links
[ link_id
] = world
.selected
;
1356 cell_ptr
->state
|= FLAG_TARGETED
;
1357 drag_ptr
->state
|= FLAG_IS_TRIGGER
;
1363 world
.selected
= -1;
1365 if( !(vg_get_button("secondary") && id_drag_from
) )
1370 world
.selected
= -1;
1374 // Simulation stop/start
1375 if( vg_get_button_down("go") )
1377 if( world
.simulating
)
1383 vg_success( "Starting simulation!\n" );
1385 sfx_set_playrnd( &audio_rolls
, &audio_system_balls_rolling
, 0, 1 );
1387 world
.simulating
= 1;
1388 world
.num_fishes
= 0;
1389 world
.sim_frame
= 0;
1390 world
.sim_start
= vg_time
;
1392 world
.sim_speed
= 2.5f
;
1394 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1395 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1402 if( world
.simulating
)
1404 while( world
.sim_frame
< (int)((vg_time
-world
.sim_start
)*world
.sim_speed
) )
1406 //vg_info( "frame: %u\n", world.sim_frame );
1407 sfx_set_playrnd( &audio_random
, &audio_system_balls_switching
, 0, 9 );
1409 // Update splitter deltas
1410 for( int i
= 0; i
< world
.h
*world
.w
; i
++ )
1412 struct cell
*cell
= &world
.data
[i
];
1413 if( cell
->config
== k_cell_type_split
)
1415 cell
->state
&= ~FLAG_FLIP_ROTATING
;
1417 if( cell
->state
& FLAG_IS_TRIGGER
)
1418 cell
->state
&= ~FLAG_TRIGGERED
;
1421 int alive_count
= 0;
1423 // Update fish positions
1424 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1426 struct fish
*fish
= &world
.fishes
[i
];
1428 if( fish
->state
== k_fish_state_soon_dead
)
1429 fish
->state
= k_fish_state_dead
;
1431 if( fish
->state
== k_fish_state_soon_alive
)
1432 fish
->state
= k_fish_state_alive
;
1434 if( fish
->state
< k_fish_state_alive
)
1437 struct cell
*cell_current
= pcell( fish
->pos
);
1439 if( fish
->state
== k_fish_state_alive
)
1442 if( cell_current
->state
& FLAG_OUTPUT
)
1444 for( int j
= 0; j
< arrlen( world
.io
); j
++ )
1446 struct cell_terminal
*term
= &world
.io
[j
];
1448 if( term
->id
== fish
->pos
[1]*world
.w
+ fish
->pos
[0] )
1450 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1451 if( run
->recv_count
< vg_list_size( run
->recieved
) )
1452 run
->recieved
[ run
->recv_count
++ ] = fish
->payload
;
1458 fish
->state
= k_fish_state_dead
;
1463 if( cell_current
->config
== k_cell_type_merge
)
1468 fish
->flow_reversed
= 0;
1472 if( cell_current
->config
== k_cell_type_split
)
1475 fish
->dir
[0] = cell_current
->state
&FLAG_FLIP_FLOP
?1:-1;
1478 if( !(cell_current
->state
& FLAG_TARGETED
) )
1479 cell_current
->state
^= FLAG_FLIP_FLOP
;
1483 // Apply cell out-flow
1484 struct cell_description
*desc
= &cell_descriptions
[ cell_current
->config
];
1486 v2i_copy( fish
->flow_reversed
? desc
->start
: desc
->end
, fish
->dir
);
1490 v2i_add( fish
->pos
, fish
->dir
, pos_next
);
1492 struct cell
*cell_next
= pcell( pos_next
);
1494 if( cell_next
->state
& (FLAG_CANAL
|FLAG_OUTPUT
) )
1496 struct cell_description
*desc
= &cell_descriptions
[ cell_next
->config
];
1498 if( cell_next
->config
== k_cell_type_merge
)
1500 if( fish
->dir
[0] == 0 )
1501 fish
->state
= k_fish_state_dead
;
1503 fish
->flow_reversed
= 0;
1507 if( cell_next
->config
== k_cell_type_split
)
1509 if( fish
->dir
[0] == 0 )
1511 sfx_set_playrnd( &audio_splitter
, &audio_system_balls_important
, 0, 1 );
1512 cell_next
->state
|= FLAG_FLIP_ROTATING
;
1514 fish
->flow_reversed
= 0;
1517 fish
->state
= k_fish_state_dead
;
1520 fish
->flow_reversed
= ( fish
->dir
[0] != -desc
->start
[0] ||
1521 fish
->dir
[1] != -desc
->start
[1] )? 1: 0;
1525 fish
->state
= world_check_pos_ok( fish
->pos
)? k_fish_state_bg
: k_fish_state_dead
;
1528 //v2i_add( fish->pos, fish->dir, fish->pos );
1530 else if( fish
->state
== k_fish_state_bg
)
1532 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1534 if( !world_check_pos_ok( fish
->pos
) )
1535 fish
->state
= k_fish_state_dead
;
1538 struct cell
*cell_entry
= pcell( fish
->pos
);
1540 if( cell_entry
->state
& FLAG_CANAL
)
1542 if( cell_entry
->config
== k_cell_type_con_r
|| cell_entry
->config
== k_cell_type_con_u
1543 || cell_entry
->config
== k_cell_type_con_l
|| cell_entry
->config
== k_cell_type_con_d
)
1545 sw_set_achievement( "CAN_DO_THAT" );
1547 fish
->state
= k_fish_state_soon_alive
;
1551 fish
->flow_reversed
= 1;
1553 switch( cell_entry
->config
)
1555 case k_cell_type_con_r
: fish
->dir
[0] = 1; break;
1556 case k_cell_type_con_l
: fish
->dir
[0] = -1; break;
1557 case k_cell_type_con_u
: fish
->dir
[1] = 1; break;
1558 case k_cell_type_con_d
: fish
->dir
[1] = -1; break;
1564 else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
1566 if( fish
->state
>= k_fish_state_alive
)
1570 // Second pass (triggers)
1571 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1573 struct fish
*fish
= &world
.fishes
[i
];
1575 if( fish
->state
== k_fish_state_alive
)
1577 v2i_add( fish
->pos
, fish
->dir
, fish
->pos
);
1578 struct cell
*cell_current
= pcell( fish
->pos
);
1580 if( cell_current
->state
& FLAG_IS_TRIGGER
)
1582 int trigger_id
= cell_current
->links
[0]?0:1;
1583 int connection_id
= cell_current
->links
[trigger_id
];
1584 int target_px
= connection_id
% world
.w
;
1585 int target_py
= (connection_id
- target_px
)/world
.w
;
1587 vg_line2( (v2f
){ fish
->pos
[0], fish
->pos
[1] }, (v2f
){ target_px
, target_py
}, 0xffffffff, 0xffffffff );
1589 struct cell
*target_peice
= &world
.data
[ cell_current
->links
[trigger_id
] ];
1591 cell_current
->state
|= FLAG_TRIGGERED
;
1594 target_peice
->state
|= FLAG_FLIP_FLOP
;
1596 target_peice
->state
&= ~FLAG_FLIP_FLOP
;
1601 // Third pass (collisions)
1602 struct fish
*fi
, *fj
;
1604 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1606 fi
= &world
.fishes
[i
];
1608 if( fi
->state
== k_fish_state_alive
)
1610 int continue_again
= 0;
1612 for( int j
= i
+1; j
< world
.num_fishes
; j
++ )
1614 fj
= &world
.fishes
[j
];
1616 if( (fj
->state
== k_fish_state_alive
) )
1621 v2i_sub( fi
->pos
, fi
->dir
, fi_prev
);
1622 v2i_sub( fj
->pos
, fj
->dir
, fj_prev
);
1625 collide_next_frame
= (
1626 (fi
->pos
[0] == fj
->pos
[0]) &&
1627 (fi
->pos
[1] == fj
->pos
[1]))? 1: 0,
1628 collide_this_frame
= (
1629 (fi_prev
[0] == fj
->pos
[0]) &&
1630 (fi_prev
[1] == fj
->pos
[1]) &&
1631 (fj_prev
[0] == fi
->pos
[0]) &&
1632 (fj_prev
[1] == fi
->pos
[1])
1635 if( collide_next_frame
|| collide_this_frame
)
1637 sw_set_achievement( "BANG" );
1639 // Shatter death (+0.5s)
1640 float death_time
= collide_this_frame
? 0.0f
: 0.5f
;
1642 fi
->state
= k_fish_state_soon_dead
;
1643 fj
->state
= k_fish_state_soon_dead
;
1644 fi
->death_time
= death_time
;
1645 fj
->death_time
= death_time
;
1652 if( continue_again
)
1658 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1660 struct cell_terminal
*term
= &world
.io
[ i
];
1661 int posx
= term
->id
% world
.w
;
1662 int posy
= (term
->id
- posx
)/world
.w
;
1663 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1667 if( world
.sim_frame
< term
->runs
[ world
.sim_run
].condition_count
)
1669 struct fish
*fish
= &world
.fishes
[ world
.num_fishes
];
1670 fish
->pos
[0] = posx
;
1671 fish
->pos
[1] = posy
;
1672 fish
->state
= k_fish_state_alive
;
1673 fish
->payload
= term
->runs
[ world
.sim_run
].conditions
[ world
.sim_frame
];
1675 struct cell
*cell_ptr
= pcell( fish
->pos
);
1677 if( cell_ptr
->config
!= k_cell_type_stub
)
1679 struct cell_description
*desc
= &cell_descriptions
[ cell_ptr
->config
];
1681 v2i_copy( desc
->start
, fish
->dir
);
1682 fish
->flow_reversed
= 1;
1684 world
.num_fishes
++;
1691 if( alive_count
== 0 )
1693 world
.completed
= 1;
1695 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
1697 struct cell_terminal
*term
= &world
.io
[ i
];
1698 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
1702 struct terminal_run
*run
= &term
->runs
[ world
.sim_run
];
1704 if( run
->recv_count
== run
->condition_count
)
1706 for( int j
= 0; j
< run
->condition_count
; j
++ )
1708 if( run
->recieved
[j
] != run
->conditions
[j
] )
1710 world
.completed
= 0;
1717 world
.completed
= 0;
1723 if( world
.completed
)
1725 if( world
.sim_run
< world
.max_runs
-1 )
1727 vg_success( "Run passed, starting next\n" );
1729 world
.sim_frame
= 0;
1730 world
.sim_start
= vg_time
;
1731 world
.num_fishes
= 0;
1733 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1734 world
.data
[ i
].state
&= ~FLAG_FLIP_FLOP
;
1740 vg_success( "Level passed!\n" );
1743 for( int i
= 0; i
< world
.w
*world
.h
; i
++ )
1744 if( world
.data
[ i
].state
& FLAG_CANAL
)
1747 world
.score
= score
;
1748 world
.time
= world
.sim_frame
;
1753 if( world
.sim_run
> 0 )
1754 sw_set_achievement( "GOOD_ENOUGH" );
1756 vg_error( "Level failed :(\n" );
1759 // Copy into career data
1760 if( world
.pCmpLevel
)
1762 if( world
.score
> world
.pCmpLevel
->completed_score
)
1763 leaderboard_set_score( world
.pCmpLevel
, world
.score
);
1765 world
.pCmpLevel
->completed_score
= world
.score
;
1768 simulation_stop(); // TODO: Async?
1775 float scaled_time
= 0.0f
;
1776 scaled_time
= (vg_time
-world
.sim_start
)*world
.sim_speed
;
1777 world
.frame_lerp
= scaled_time
- (float)world
.sim_frame
;
1780 for( int i
= 0; i
< world
.num_fishes
; i
++ )
1782 struct fish
*fish
= &world
.fishes
[i
];
1784 if( fish
->state
== k_fish_state_dead
)
1787 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
1788 continue; // Todo: particle thing?
1790 struct cell
*cell
= pcell(fish
->pos
);
1791 struct cell_description
*desc
= &cell_descriptions
[ cell
->config
];
1795 float t
= world
.frame_lerp
;
1796 if( fish
->flow_reversed
&& !desc
->is_linear
)
1799 v2_copy( fish
->physics_co
, fish
->physics_v
);
1801 switch( cell
->config
)
1803 case k_cell_type_merge
:
1804 if( fish
->dir
[0] == 1 )
1809 case k_cell_type_con_r
: curve
= curve_1
; break;
1810 case k_cell_type_con_l
: curve
= curve_4
; break;
1811 case k_cell_type_con_u
: curve
= curve_2
; break;
1812 case k_cell_type_con_d
: curve
= curve_8
; break;
1813 case 3: curve
= curve_3
; break;
1814 case 6: curve
= curve_6
; break;
1815 case 9: curve
= curve_9
; break;
1816 case 12: curve
= curve_12
; break;
1818 if( t
> curve_7_linear_section
)
1820 t
-= curve_7_linear_section
;
1821 t
*= (1.0f
/(1.0f
-curve_7_linear_section
));
1823 curve
= cell
->state
& FLAG_FLIP_FLOP
? curve_7
: curve_7_1
;
1827 default: curve
= NULL
; break;
1833 float t3
= t
* t
* t
;
1835 float cA
= 3.0f
*t2
- 3.0f
*t3
;
1836 float cB
= 3.0f
*t3
- 6.0f
*t2
+ 3.0f
*t
;
1837 float cC
= 3.0f
*t2
- t3
- 3.0f
*t
+ 1.0f
;
1839 fish
->physics_co
[0] = t3
*curve
[3][0] + cA
*curve
[2][0] + cB
*curve
[1][0] + cC
*curve
[0][0];
1840 fish
->physics_co
[1] = t3
*curve
[3][1] + cA
*curve
[2][1] + cB
*curve
[1][1] + cC
*curve
[0][1];
1841 fish
->physics_co
[0] += (float)fish
->pos
[0];
1842 fish
->physics_co
[1] += (float)fish
->pos
[1];
1847 origin
[0] = (float)fish
->pos
[0] + (float)fish
->dir
[0]*-0.5f
+ 0.5f
;
1848 origin
[1] = (float)fish
->pos
[1] + (float)fish
->dir
[1]*-0.5f
+ 0.5f
;
1850 fish
->physics_co
[0] = origin
[0] + (float)fish
->dir
[0]*t
;
1851 fish
->physics_co
[1] = origin
[1] + (float)fish
->dir
[1]*t
;
1857 static void render_tiles( v2i start
, v2i end
, v4f
const regular_colour
, v4f
const selected_colour
)
1859 v2i full_start
= { 0,0 };
1860 v2i full_end
= { world
.w
, world
.h
};
1862 if( !start
|| !end
)
1868 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1870 for( int y
= start
[1]; y
< end
[1]; y
++ )
1872 for( int x
= start
[0]; x
< end
[0]; x
++ )
1874 struct cell
*cell
= pcell((v2i
){x
,y
});
1875 int selected
= world
.selected
== y
*world
.w
+ x
;
1877 int tile_offsets
[][2] =
1879 {2, 0}, {0, 3}, {0, 2}, {2, 2},
1880 {1, 0}, {2, 3}, {3, 2}, {1, 3},
1881 {3, 1}, {0, 1}, {1, 2}, {2, 1},
1882 {1, 1}, {3, 3}, {2, 1}, {2, 1}
1885 int uv
[2] = { 3, 0 };
1887 if( cell
->state
& (FLAG_CANAL
|FLAG_INPUT
|FLAG_OUTPUT
) )
1889 uv
[0] = tile_offsets
[ cell
->config
][0];
1890 uv
[1] = tile_offsets
[ cell
->config
][1];
1893 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
, uv
[0], uv
[1] );
1896 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, selected_colour
);
1898 glUniform4fv( SHADER_UNIFORM( shader_tile_main
, "uColour" ), 1, regular_colour
);
1906 static void draw_numbers( v3f coord
, int number
)
1909 v3_copy( coord
, pos
);
1910 int digits
[8]; int i
= 0;
1912 while( number
> 0 && i
< 8 )
1914 digits
[i
++] = number
% 10;
1915 number
= number
/ 10;
1918 for( int j
= 0; j
< i
; j
++ )
1920 glUniform3fv( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), 1, pos
);
1921 draw_mesh( MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][0], MESH_NUMBERS_OFFSETS
[digits
[i
-j
-1]][1] );
1922 pos
[0] += pos
[2] * 0.75f
;
1926 void vg_render(void)
1928 glViewport( 0,0, vg_window_x
, vg_window_y
);
1930 glDisable( GL_DEPTH_TEST
);
1931 glClearColor( 0.369768f
, 0.3654f
, 0.42f
, 1.0f
);
1932 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
1934 v4f
const colour_default
= {1.0f
, 1.0f
, 1.0f
, 1.0f
};
1935 v4f
const colour_selected
= {0.90f
, 0.92f
, 1.0f
, 1.0f
};
1937 // TILE SET RENDERING
1938 // todo: just slam everything into a mesh...
1939 // when user modifies a tile the neighbours can be easily uploaded to gpu mem
1941 // Currently we're uploading a fair amount of data every frame anyway.
1942 // NOTE: this is for final optimisations ONLY!
1943 // ======================================================================
1945 use_mesh( &world
.tile
);
1951 SHADER_USE( shader_background
);
1952 glUniformMatrix3fv( SHADER_UNIFORM( shader_background
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1954 glActiveTexture( GL_TEXTURE0
);
1955 glBindTexture( GL_TEXTURE_2D
, world
.background_data
);
1956 glUniform1i( SHADER_UNIFORM( shader_background
, "uTexMain" ), 0 );
1958 glUniform3f( SHADER_UNIFORM( shader_background
, "uOffset" ), -16, -16, 64 );
1959 glUniform1f( SHADER_UNIFORM( shader_background
, "uVariance" ), 0.02f
);
1961 glActiveTexture( GL_TEXTURE1
);
1962 glBindTexture( GL_TEXTURE_2D
, world
.random_samples
);
1963 glUniform1i( SHADER_UNIFORM( shader_background
, "uSamplerNoise" ), 1 );
1971 ui_begin( &ui_global_ctx
, 512, 256 );
1973 ui_global_ctx
.override_colour
= 0xff9a8a89;
1974 //ui_text( &ui_global_ctx, world.map_title, 6, 0 );
1975 ui_global_ctx
.override_colour
= 0xffffffff;
1977 ui_resolve( &ui_global_ctx
);
1980 m3x3_copy( vg_pv
, world_text
);
1981 m3x3_translate( world_text
, (v3f
){ 1.55f
, 1.9f
, 0.0f
} );
1982 m3x3_rotate( world_text
, VG_PIf
*0.5f
);
1983 m3x3_scale( world_text
, (v3f
){0.01f
,-0.01f
,0.01f
} );
1985 ui_draw( &ui_global_ctx
, world_text
);
1988 // =========================================================================================
1990 use_mesh( &world
.tile
);
1991 SHADER_USE( shader_tile_main
);
1994 m2x2_identity( subtransform
);
1995 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
1996 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
1997 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 0.0f
);
1998 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 0.0f
);
2001 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
2002 glBlendEquation(GL_FUNC_ADD
);
2005 vg_tex2d_bind( &tex_tile_data
, 0 );
2006 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
2008 vg_tex2d_bind( &tex_wood
, 1 );
2009 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
2011 render_tiles( NULL
, NULL
, colour_default
, colour_default
);
2015 SHADER_USE( shader_ball
);
2016 glUniformMatrix3fv( SHADER_UNIFORM( shader_ball
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2018 vg_tex2d_bind( &tex_ball_noise
, 0 );
2019 glUniform1i( SHADER_UNIFORM( shader_ball
, "uTexMain" ), 0 );
2022 if( world
.simulating
)
2024 for( int i
= 0; i
< world
.num_fishes
; i
++ )
2026 struct fish
*fish
= &world
.fishes
[i
];
2028 if( fish
->state
== k_fish_state_dead
|| fish
->state
== k_fish_state_bg
)
2031 if( fish
->state
== k_fish_state_soon_dead
&& (world
.frame_lerp
> fish
->death_time
) )
2034 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2035 colour_code_v3( fish
->payload
, dot_colour
);
2037 glUniform3fv( SHADER_UNIFORM( shader_ball
, "uColour" ), 1, dot_colour
);
2038 glUniform2fv( SHADER_UNIFORM( shader_ball
, "uOffset" ), 1, fish
->physics_co
);
2039 glUniform2f( SHADER_UNIFORM( shader_ball
, "uTexOffset" ), (float)i
* 1.2334, (float)i
* -0.3579f
);
2044 SHADER_USE( shader_tile_main
);
2047 vg_tex2d_bind( &tex_tile_data
, 0 );
2048 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexGlyphs" ), 0 );
2050 vg_tex2d_bind( &tex_wood
, 1 );
2051 glUniform1i( SHADER_UNIFORM( shader_tile_main
, "uTexWood" ), 1 );
2053 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uForeground" ), 1.0f
);
2054 render_tiles( NULL
, NULL
, colour_default
, colour_selected
);
2057 for( int y
= 2; y
< world
.h
-2; y
++ )
2059 for( int x
= 2; x
< world
.w
-2; x
++ )
2061 struct cell
*cell
= pcell((v2i
){x
,y
});
2063 if( cell
->state
& FLAG_CANAL
)
2065 if( cell
->config
== k_cell_type_split
)
2067 float rotation
= cell
->state
& FLAG_FLIP_FLOP
? vg_rad( -45.0f
): vg_rad( 45.0f
);
2069 if( cell
->state
& FLAG_FLIP_ROTATING
)
2071 if( (world
.frame_lerp
> curve_7_linear_section
) )
2073 float const rotation_speed
= 0.4f
;
2074 if( (world
.frame_lerp
< 1.0f
-rotation_speed
) )
2076 float t
= world
.frame_lerp
- curve_7_linear_section
;
2077 t
*= -2.0f
* (1.0f
/(1.0f
-(curve_7_linear_section
+rotation_speed
)));
2087 m2x2_create_rotation( subtransform
, rotation
);
2089 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2090 glUniform4f( SHADER_UNIFORM( shader_tile_main
, "uOffset" ), (float)x
, (float)y
+ 0.125f
, cell
->state
& FLAG_TARGETED
? 3.0f
: 0.0f
, 0.0f
);
2098 if( world
.selected
!= -1 && !(world
.data
[ world
.selected
].state
& FLAG_CANAL
) && !id_drag_from
)
2100 v2i new_begin
= { world
.tile_x
- 2, world
.tile_y
- 2 };
2101 v2i new_end
= { world
.tile_x
+ 2, world
.tile_y
+ 2 };
2103 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2104 map_reclassify( new_begin
, new_end
, 0 );
2106 m2x2_identity( subtransform
);
2107 glUniform1f( SHADER_UNIFORM( shader_tile_main
, "uGhost" ), 1.0f
);
2108 glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main
, "uSubTransform" ), 1, GL_FALSE
, (float *)subtransform
);
2109 glUniform2fv( SHADER_UNIFORM( shader_tile_main
, "uMousePos" ), 1, world
.tile_pos
);
2111 render_tiles( new_begin
, new_end
, colour_default
, colour_default
);
2113 world
.data
[ world
.selected
].state
^= FLAG_CANAL
;
2114 map_reclassify( new_begin
, new_end
, 0 );
2117 //glDisable(GL_BLEND);
2119 // Draw connecting wires
2120 glDisable(GL_BLEND
);
2122 SHADER_USE( shader_wire
);
2123 glBindVertexArray( world
.wire
.vao
);
2125 glUniformMatrix3fv( SHADER_UNIFORM( shader_wire
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2126 glUniform4f( SHADER_UNIFORM( shader_wire
, "uColour" ), 0.2f
, 0.2f
, 0.2f
, 1.0f
);
2130 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), 0.4f
);
2131 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), drag_from_co
[0], drag_from_co
[1], 0.06f
);
2132 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), drag_to_co
[0], drag_to_co
[1], 0.06f
);
2133 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2136 float rp_x1
= world
.frame_lerp
*9.0f
;
2137 float rp_x2
= 1.0f
-rp_x1
*expf(1.0f
-rp_x1
)* 0.36f
;
2139 for( int y
= 2; y
< world
.h
-2; y
++ )
2141 for( int x
= 2; x
< world
.w
-2; x
++ )
2143 struct cell
*cell
= pcell((v2i
){x
,y
});
2145 if( cell
->state
& FLAG_CANAL
)
2147 if( cell
->state
& FLAG_IS_TRIGGER
)
2149 int trigger_id
= cell
->links
[0]?0:1;
2151 int x2
= cell
->links
[trigger_id
] % world
.w
;
2152 int y2
= (cell
->links
[trigger_id
] - x2
) / world
.w
;
2157 startpoint
[0] = (float)x2
+ (trigger_id
? 0.75f
: 0.25f
);
2158 startpoint
[1] = (float)y2
+ 0.25f
;
2160 endpoint
[0] = x
+0.5f
;
2161 endpoint
[1] = y
+0.5f
;
2163 glUniform1f( SHADER_UNIFORM( shader_wire
, "uCurve" ), cell
->state
& FLAG_TRIGGERED
? rp_x2
* 0.4f
: 0.4f
);
2164 glUniform3f( SHADER_UNIFORM( shader_wire
, "uStart" ), startpoint
[0], startpoint
[1], 0.04f
);
2165 glUniform3f( SHADER_UNIFORM( shader_wire
, "uEnd" ), endpoint
[0], endpoint
[1], 0.04f
);
2166 glDrawElements( GL_TRIANGLES
, world
.wire
.em
, GL_UNSIGNED_SHORT
, (void*)(0) );
2172 SHADER_USE( shader_tile_colour
);
2173 glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_colour
, "uPv" ), 1, GL_FALSE
, (float *)vg_pv
);
2174 use_mesh( &world
.circle
);
2176 int const filled_start
= 0;
2177 int const filled_count
= 32;
2178 int const empty_start
= 32;
2179 int const empty_count
= 32*2;
2182 for( int i
= 0; i
< arrlen( world
.io
); i
++ )
2184 struct cell_terminal
*term
= &world
.io
[ i
];
2185 int posx
= term
->id
% world
.w
;
2186 int posy
= (term
->id
- posx
)/world
.w
;
2187 int is_input
= world
.data
[ term
->id
].state
& FLAG_INPUT
;
2189 v4f dot_colour
= { 0.0f
, 0.0f
, 0.0f
, 1.0f
};
2191 for( int k
= 0; k
< term
->run_count
; k
++ )
2193 for( int j
= 0; j
< term
->runs
[k
].condition_count
; j
++ )
2195 float y_offset
= is_input
? 1.2f
: -0.2f
;
2196 y_offset
+= (is_input
? 0.2f
: -0.2f
) * (float)k
;
2198 glUniform3f( SHADER_UNIFORM( shader_tile_colour
, "uOffset" ), (float)posx
+ 0.2f
+ 0.2f
* (float)j
, (float)posy
+ y_offset
, 0.1f
);
2202 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2203 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2205 // Draw filled if tick not passed, draw empty if empty
2206 if( (world
.sim_frame
> j
&& world
.sim_run
>= k
) || world
.sim_run
> k
)
2207 draw_mesh( empty_start
, empty_count
);
2209 draw_mesh( filled_start
, filled_count
);
2213 if( term
->runs
[k
].recv_count
> j
)
2215 colour_code_v3( term
->runs
[k
].recieved
[j
], dot_colour
);
2216 v3_muls( dot_colour
, 0.8f
, dot_colour
);
2217 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2219 draw_mesh( filled_start
, filled_count
);
2222 colour_code_v3( term
->runs
[k
].conditions
[j
], dot_colour
);
2223 glUniform4fv( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 1, dot_colour
);
2225 draw_mesh( empty_start
, empty_count
);
2231 if( world
.simulating
)
2233 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ), 0.0f
, 0.0f
, 0.0f
, 1.0f
);
2234 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
);
2235 draw_mesh( filled_start
, filled_count
);
2239 float const score_bright
= 1.25f
;
2240 glUniform4f( SHADER_UNIFORM( shader_tile_colour
, "uColour" ),
2241 0.4f
*score_bright
, 0.39f
*score_bright
, 0.45f
*score_bright
, 1.0f
);
2243 use_mesh( &world
.numbers
);
2244 draw_numbers( (v3f
){ 2.0f
, (float)world
.h
-1.875f
, 0.3333f
}, world
.score
);
2247 static ui_colourset flcol_list_a
= {
2249 .hover
= 0xffa09393,
2250 .active
= 0xffbfb1b0
2252 static ui_colourset flcol_list_b
= {
2254 .hover
= 0xffa09393,
2255 .active
= 0xffbfb1b0
2258 static ui_colourset flcol_list_complete_a
= {
2260 .hover
= 0xff8dc18f,
2261 .active
= 0xffb2ddb3
2264 static ui_colourset flcol_list_complete_b
= {
2266 .hover
= 0xff8dc18f,
2267 .active
= 0xffb2ddb3
2270 static ui_colourset flcol_list_locked
= {
2272 .hover
= 0xff655959,
2273 .active
= 0xff655959
2278 SteamLeaderboard_t steam_leaderboard
;
2279 int leaderboard_show
;
2281 struct leaderboard_player
2288 char score_text
[ 16 ];
2289 char player_name
[ 48 ];
2290 GLuint texture
; // Not dynamic
2292 leaderboard_players
[10];
2293 int leaderboard_count
;
2297 struct cmp_level
*level
;
2304 struct cmp_level
*level_selected
;
2311 static int pack_selection
= 0;
2312 static struct pack_info
2314 struct cmp_level
*levels
;
2321 .levels
= cmp_levels_tutorials
,
2322 .level_count
= vg_list_size(cmp_levels_tutorials
),
2326 .levels
= cmp_levels_basic
,
2327 .level_count
= vg_list_size(cmp_levels_basic
),
2331 .levels
= cmp_levels_grad
,
2332 .level_count
= vg_list_size(cmp_levels_tutorials
),
2338 ui_global_ctx
.cursor
[0] = 0;
2339 ui_global_ctx
.cursor
[1] = 0;
2340 ui_global_ctx
.cursor
[2] = 256;
2344 ui_global_ctx
.id_base
= 4 << 16;
2348 gui_capture_mouse( 9999 );
2349 gui_fill_rect( ui_global_ctx
.cursor
, 0xff5577ff );
2351 gui_text( "ASSIGNMENTS", 8, 0 );
2353 ui_global_ctx
.cursor
[1] += 30;
2354 ui_global_ctx
.cursor
[3] = 25;
2358 ui_rect_pad( ui_global_ctx
.cursor
, 2 );
2359 ui_global_ctx
.cursor
[2] = 84;
2361 for( int i
= 0; i
< 3; i
++ )
2363 if( i
== pack_selection
)
2364 gui_override_colours( &flcol_list_locked
);
2366 if( gui_button( 2000 + i
) == k_button_click
)
2369 ui_global_ctx
.cursor
[1] += 2;
2370 gui_text( pack_infos
[i
].name
, 4, 0 );
2373 gui_reset_colours();
2378 ui_global_ctx
.cursor
[3] = 500;
2380 // DRAW LEVEL SELECTION LIST
2382 struct cmp_level
*levels
= pack_infos
[ pack_selection
].levels
;
2383 int count
= pack_infos
[ pack_selection
].level_count
;
2384 int unlocked
= 3000;
2386 static struct ui_scrollbar sb
= {
2390 ui_px view_height
= ui_global_ctx
.cursor
[3];
2391 ui_px level_height
= 50;
2393 // Level scroll view
2396 gui_fill_rect( ui_global_ctx
.cursor
, 0xff5a4e4d );
2397 gui_set_clip( ui_global_ctx
.cursor
);
2399 ui_global_ctx
.cursor
[2] = 14;
2402 ui_px content_height
= count
*level_height
;
2403 if( content_height
> view_height
)
2405 ui_scrollbar( &ui_global_ctx
, &sb
, 1 );
2406 ui_global_ctx
.cursor
[1] -= ui_calculate_content_scroll( &sb
, content_height
);
2410 gui_fill_rect( ui_global_ctx
.cursor
, 0xff807373 );
2413 ui_global_ctx
.cursor
[2] = 240;
2414 ui_global_ctx
.cursor
[3] = level_height
;
2417 for( int i
= 0; i
< count
; i
++ )
2419 struct cmp_level
*lvl_info
= &levels
[i
];
2423 if( lvl_info
->completed_score
!= 0 )
2424 gui_override_colours( i
&0x1? &flcol_list_complete_a
: &flcol_list_complete_b
);
2426 gui_override_colours( i
&0x1? &flcol_list_a
: &flcol_list_b
);
2429 gui_override_colours( &flcol_list_locked
);
2433 if( gui_button( 2 + i
) == k_button_click
)
2435 ui_data
.level_selected
= &levels
[i
];
2436 ui_data
.leaderboard_show
= 0;
2437 sw_find_leaderboard( ui_data
.level_selected
->map_name
);
2440 ui_global_ctx
.override_colour
= 0xffffffff;
2441 gui_text( lvl_info
->title
, 6, 0 );
2442 ui_global_ctx
.cursor
[1] += 18;
2443 gui_text( "incomplete", 4, 0 );
2447 gui_button( 2 + i
);
2449 ui_global_ctx
.override_colour
= 0xff786f6f;
2450 gui_text( "???", 6, 0 );
2451 ui_global_ctx
.cursor
[1] += 18;
2452 gui_text( "locked", 4, 0 );
2458 gui_reset_colours();
2466 // Selected level UI
2467 // ============================================================
2469 if( ui_data
.level_selected
)
2471 ui_global_ctx
.cursor
[0] += 16;
2472 ui_global_ctx
.cursor
[1] += 16;
2473 ui_global_ctx
.cursor
[2] = 512-40;
2474 ui_global_ctx
.cursor
[3] = 560-16;
2478 gui_capture_mouse( 9999 );
2480 gui_fill_rect( ui_global_ctx
.cursor
, 0xff5a4e4d );
2481 ui_global_ctx
.cursor
[1] += 4;
2482 gui_text( ui_data
.level_selected
->title
, 6, 0 );
2484 ui_global_ctx
.cursor
[1] += 30;
2485 ui_rect_pad( ui_global_ctx
.cursor
, 8 );
2486 ui_global_ctx
.cursor
[3] = 300;
2490 gui_fill_rect( ui_global_ctx
.cursor
, 0xff655959 );
2494 ui_text_use_paragraph( &ui_global_ctx
);
2495 ui_global_ctx
.cursor
[1] += 2;
2497 gui_text( ui_data
.level_selected
->description
, 5, 0 );
2498 ui_text_use_title( &ui_global_ctx
);
2500 // Buttons at the bottom
2501 ui_global_ctx
.cursor
[3] = 30;
2502 ui_global_ctx
.cursor
[2] = 80;
2505 ui_global_ctx
.cursor
[1] -= 8;
2507 if( gui_button( 3000 ) == k_button_click
)
2509 ui_data
.level_selected
= NULL
;
2511 gui_text( "Back", 6, 0 );
2515 ui_global_ctx
.cursor
[2] = 170;
2516 ui_global_ctx
.cursor
[0] -= 8 + 170 + 2;
2519 gui_override_colours( &flcol_list_locked
);
2520 if( gui_button( 3001 ) == k_button_click
)
2521 vg_error( "UNIMPLEMENTED\n" );
2523 gui_text( "Restore Solution", 6, 0 );
2527 ui_global_ctx
.cursor
[0] += 2;
2528 ui_global_ctx
.cursor
[2] = 80;
2531 gui_override_colours( &flcol_list_complete_a
);
2532 if( gui_button( 3002 ) == k_button_click
)
2534 console_changelevel( 1, &ui_data
.level_selected
->map_name
);
2535 world
.pCmpLevel
= ui_data
.level_selected
;
2537 ui_data
.level_selected
= NULL
;
2538 ui_data
.leaderboard_show
= 0;
2540 gui_text( "Play", 6, 0 );
2544 gui_reset_colours();
2548 if( ui_data
.leaderboard_show
)
2550 ui_global_ctx
.cursor
[0] += 16;
2551 ui_global_ctx
.cursor
[3] = 250;
2556 gui_fill_rect( ui_global_ctx
.cursor
, 0xff5a4e4d );
2558 ui_rect_pad( ui_global_ctx
.cursor
, 8 );
2562 ui_global_ctx
.cursor
[3] = 32+8;
2564 for( int i
= 0; i
< ui_data
.leaderboard_count
; i
++ )
2568 struct leaderboard_player
*player
= &ui_data
.leaderboard_players
[i
];
2570 ui_global_ctx
.cursor
[0] += 4;
2571 ui_global_ctx
.cursor
[1] += 4;
2572 ui_global_ctx
.cursor
[2] = 32;
2573 ui_global_ctx
.cursor
[3] = 32;
2577 gui_push_image( ui_global_ctx
.cursor
, player
->texture
);
2581 gui_text( player
->player_name
, 6, 0 );
2583 ui_global_ctx
.cursor
[2] = 50;
2586 gui_text( player
->score_text
, 6, 0 );
2590 ui_global_ctx
.cursor
[1] += 2;
2601 void leaderboard_dispatch_score(void)
2603 sw_upload_leaderboard_score(
2604 ui_data
.upload_request
.level
->steam_leaderboard
,
2605 k_ELeaderboardUploadScoreMethodKeepBest
,
2606 ui_data
.upload_request
.score
,
2611 ui_data
.upload_request
.is_waiting
= 0;
2613 vg_success( "Dispatched leaderboard score\n" );
2616 void leaderboard_found( LeaderboardFindResult_t
*pCallback
)
2618 if( !pCallback
->m_bLeaderboardFound
)
2620 vg_error( "Leaderboard could not be found\n" );
2621 ui_data
.steam_leaderboard
= 0;
2625 const char *recieved_name
= sw_get_leaderboard_name( pCallback
->m_hSteamLeaderboard
);
2627 // Update UI state and request entries if this callback found the current UI level
2628 if( ui_data
.level_selected
)
2630 if( !strcmp( recieved_name
, ui_data
.level_selected
->map_name
) )
2632 sw_download_leaderboard_entries( pCallback
->m_hSteamLeaderboard
, k_ELeaderboardDataRequestFriends
, 0, 10 );
2633 ui_data
.level_selected
->steam_leaderboard
= pCallback
->m_hSteamLeaderboard
;
2637 // Dispatch the waiting request if there was one
2638 if( ui_data
.upload_request
.is_waiting
)
2640 if( !strcmp( recieved_name
, ui_data
.upload_request
.level
->map_name
) )
2642 ui_data
.upload_request
.level
->steam_leaderboard
= pCallback
->m_hSteamLeaderboard
;
2643 leaderboard_dispatch_score();
2649 void leaderboard_downloaded( LeaderboardScoresDownloaded_t
*pCallback
)
2651 // Update UI if this leaderboard matches what we currently have in view
2652 if( ui_data
.level_selected
->steam_leaderboard
== pCallback
->m_hSteamLeaderboard
)
2654 vg_info( "Recieved %d entries\n", pCallback
->m_cEntryCount
);
2655 ui_data
.leaderboard_count
= VG_MIN( pCallback
->m_cEntryCount
, 10 );
2657 for( int i
= 0; i
< ui_data
.leaderboard_count
; i
++ )
2659 LeaderboardEntry_t entry
;
2660 sw_get_downloaded_entry( pCallback
->m_hSteamLeaderboardEntries
, i
, &entry
, NULL
, 0 );
2662 struct leaderboard_player
*player
= &ui_data
.leaderboard_players
[i
];
2664 player
->id
= entry
.m_steamIDUser
.m_unAll64Bits
;
2665 strncpy( player
->player_name
, sw_get_friend_persona_name( player
->id
), vg_list_size( player
->player_name
)-1 );
2666 player
->score
= entry
.m_nScore
;
2668 snprintf( player
->score_text
, vg_list_size(player
->score_text
), "%d", player
->score
);
2669 player
->texture
= sw_get_player_image( player
->id
);
2671 if( player
->texture
== 0 )
2672 player
->texture
= tex_unkown
.name
;
2675 if( ui_data
.leaderboard_count
)
2676 ui_data
.leaderboard_show
= 1;
2678 ui_data
.leaderboard_show
= 0;
2680 else vg_warn( "Downloaded leaderboard does not match requested!\n" );
2683 void leaderboard_set_score( struct cmp_level
*cmp_level
, u32 score
)
2685 if( ui_data
.upload_request
.is_waiting
)
2686 vg_warn( "You are uploading leaderboard entries too quickly!\n" );
2688 ui_data
.upload_request
.level
= cmp_level
;
2689 ui_data
.upload_request
.score
= score
;
2690 ui_data
.upload_request
.is_waiting
= 1;
2692 // If leaderboard ID has been downloaded already then just immediately dispatch this
2693 if( cmp_level
->steam_leaderboard
)
2694 leaderboard_dispatch_score();
2696 sw_find_leaderboard( cmp_level
->map_name
);