+ v2_copy( result_view, view_point );
+ v2_copy( (v2f){0.0f,0.0f}, drag_offset );
+ }
+ }
+
+ // Zooming
+ {
+ v2f mview_local;
+ v2f mview_new;
+ v2f mview_cur;
+ v2f mview_delta;
+ float rsize;
+
+ rsize = size-world.st.zoom;
+
+ v2_div( vg_mouse, (v2f){ vg_window_x*0.5f, vg_window_y*0.5f }, mview_local );
+ v2_add( (v2f){ -rsize, -rsize*r1 }, (v2f){ mview_local[0]*rsize, (2.0f-mview_local[1])*rsize*r1 }, mview_cur );
+
+ world.st.zoom = vg_clampf( world.st.zoom + vg_mouse_wheel[1], 0.0f, size - 4.0f );
+
+ // Recalculate new position
+ rsize = size-world.st.zoom;
+ v2_add( (v2f){ -rsize, -rsize*r1 }, (v2f){ mview_local[0]*rsize, (2.0f-mview_local[1])*rsize*r1 }, mview_new );
+
+ // Apply offset
+ v2_sub( mview_new, mview_cur, mview_delta );
+ v2_add( mview_delta, view_point, view_point );
+ }
+
+ // Tilemap
+ // ========================================================================================================
+ if( !is_simulation_running() && !gui_want_mouse() )
+ {
+ v2_copy( vg_mouse_ws, world.drag_to_co );
+
+ if( cell_interactive( (v2i){ world.tile_x, world.tile_y } ))
+ {
+ world.selected = world.tile_y * world.w + world.tile_x;
+
+ static u32 modify_state = 0;
+ struct cell *cell_ptr = &world.data[world.selected];
+
+ if( !(cell_ptr->state & FLAG_EMITTER) )
+ {
+ if( vg_get_button_down("primary") )
+ modify_state = (cell_ptr->state & FLAG_CANAL) ^ FLAG_CANAL;
+
+ if( vg_get_button("primary") && ((cell_ptr->state & FLAG_CANAL) != modify_state) )
+ {
+ cell_ptr->state &= ~FLAG_CANAL;
+ cell_ptr->state |= modify_state;
+
+ if( cell_ptr->state & FLAG_CANAL )
+ {
+ cell_ptr->links[0] = 0;
+ cell_ptr->links[1] = 0;
+
+ sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 3, 6 );
+ world.score ++;
+ }
+ else
+ {
+ sfx_set_playrnd( &audio_tile_mod, &audio_system_sfx, 0, 3 );
+ world.score --;
+ }
+
+ map_reclassify((v2i){ world.tile_x -2, world.tile_y -2 },
+ (v2i){ world.tile_x +2, world.tile_y +2 }, 1 );
+ }
+
+ if( vg_get_button_down("secondary") && (cell_ptr->state & FLAG_CANAL) && !(cell_ptr->config == k_cell_type_split) )
+ {
+ world.id_drag_from = world.selected;
+
+ struct cell_description *desc = &cell_descriptions[ world.data[world.id_drag_from].config ];
+ v2_add( desc->trigger_pos, (v2f){ world.tile_x, world.tile_y }, world.drag_from_co );
+ }
+ }
+
+ float local_x = vg_mouse_ws[0] - (float)world.tile_x;
+
+ if( vg_get_button_up("secondary") && world.id_drag_from == world.selected )
+ {
+ u32 link_id = cell_ptr->links[ 0 ]? 0: 1;
+
+ // break existing connection off
+ if( cell_ptr->links[ link_id ] )
+ {
+ struct cell *current_connection = &world.data[ cell_ptr->links[ link_id ]];
+
+ if( !current_connection->links[ link_id ^ 0x1 ] )
+ current_connection->state &= ~FLAG_TARGETED;
+
+ current_connection->links[ link_id ] = 0;
+ cell_ptr->links[ link_id ] = 0;
+ }
+
+ cell_ptr->state &= ~FLAG_IS_TRIGGER;
+ world.id_drag_from = 0;
+ }
+
+ else if( world.id_drag_from && (cell_ptr->state & (FLAG_CANAL|FLAG_EMITTER)) &&
+ ((cell_ptr->config == k_cell_type_split) || (cell_ptr->state & FLAG_EMITTER)) )
+ {
+ world.drag_to_co[0] = (float)world.tile_x + (local_x > 0.5f? 0.75f: 0.25f);
+ world.drag_to_co[1] = (float)world.tile_y + 0.25f;
+
+ if( vg_get_button_up("secondary") )
+ {
+ struct cell *drag_ptr = &world.data[world.id_drag_from];
+ u32 link_id = local_x > 0.5f? 1: 0;
+
+ // Cleanup existing connections
+ if( cell_ptr->links[ link_id ] )
+ {
+ vg_warn( "Destroying existing connection on link %u (%hu)\n", link_id, cell_ptr->links[ link_id ] );
+
+ struct cell *current_connection = &world.data[ cell_ptr->links[ link_id ]];
+ current_connection->state &= ~FLAG_IS_TRIGGER;
+ current_connection->links[ link_id ] = 0;
+ }
+
+ for( u32 i = 0; i < 2; i ++ )
+ {
+ if( drag_ptr->links[ i ] )
+ {
+ vg_warn( "Destroying link %u (%hu)\n", i, drag_ptr->links[ i ] );
+
+ struct cell *current_connection = &world.data[ drag_ptr->links[ i ]];
+ if( current_connection->links[ i ^ 0x1 ] == 0 )
+ current_connection->state &= ~FLAG_TARGETED;
+
+ current_connection->links[ i ] = 0;
+ drag_ptr->links[ i ] = 0;
+ }
+ }
+
+ // Create the new connection
+ vg_success( "Creating connection on link %u (%hu)\n", link_id, world.id_drag_from );
+
+ cell_ptr->links[ link_id ] = world.id_drag_from;
+ drag_ptr->links[ link_id ] = world.selected;
+
+ cell_ptr->state |= FLAG_TARGETED;
+ drag_ptr->state |= FLAG_IS_TRIGGER;
+ world.id_drag_from = 0;
+ }
+ }
+ }
+ else
+ {
+ world.selected = -1;
+ }
+
+ if( !(vg_get_button("secondary") && world.id_drag_from) )
+ world.id_drag_from = 0;
+ }
+ else
+ {
+ world.selected = -1;
+ world.id_drag_from = 0;
+ }
+
+ // Marble state updates
+ // ========================================================================================================
+ if( is_simulation_running() )
+ {
+ float old_time = world.sim_internal_time;
+
+ if( !world.st.buttons[ k_world_button_pause ].state )
+ world.sim_internal_time = world.sim_internal_ref + (vg_time-world.sim_delta_ref) * world.sim_delta_speed;
+ else
+ world.sim_internal_time = vg_lerpf( world.sim_internal_time, world.sim_internal_ref + world.pause_offset_target, vg_time_delta*15.0f );
+ world.sim_internal_delta = world.sim_internal_time-old_time;
+
+ world.sim_target = (int)floorf(world.sim_internal_time);
+
+ int success_this_frame = 0;
+ int failure_this_frame = 0;
+
+ while( world.sim_frame < world.sim_target )
+ {
+ sfx_set_playrnd( &audio_random, &audio_system_balls_switching, 0, 8 );
+
+ // Update splitter deltas
+ for( int i = 0; i < world.h*world.w; i ++ )
+ {
+ struct cell *cell = &world.data[i];
+ if( cell->config == k_cell_type_split )
+ {
+ cell->state &= ~FLAG_FLIP_ROTATING;
+ }
+ if( cell->state & FLAG_IS_TRIGGER )
+ cell->state &= ~FLAG_TRIGGERED;
+ }
+
+ int alive_count = 0;
+
+ // Update fish positions
+ for( int i = 0; i < world.num_fishes; i ++ )
+ {
+ struct fish *fish = &world.fishes[i];
+
+ if( fish->state == k_fish_state_soon_dead )
+ fish->state = k_fish_state_dead;
+
+ if( fish->state == k_fish_state_soon_alive )
+ fish->state = k_fish_state_alive;
+
+ if( fish->state < k_fish_state_alive )
+ continue;
+
+ struct cell *cell_current = pcell( fish->pos );
+
+ if( fish->state == k_fish_state_alive )
+ {
+ // Apply to output
+ if( cell_current->state & FLAG_OUTPUT )
+ {
+ for( int j = 0; j < arrlen( world.io ); j ++ )
+ {
+ struct cell_terminal *term = &world.io[j];
+
+ if( v2i_eq( term->pos, fish->pos ) )
+ {
+ struct terminal_run *run = &term->runs[ world.sim_run ];
+ if( run->recv_count < vg_list_size( run->recieved ) )
+ {
+ if( fish->payload == run->conditions[ run->recv_count ] )
+ success_this_frame = 1;
+ else
+ failure_this_frame = 1;
+
+ run->recieved[ run->recv_count ++ ] = fish->payload;
+ }
+ else
+ failure_this_frame = 1;
+
+ break;
+ }
+ }
+
+ fish->state = k_fish_state_dead;
+ fish->death_time = -1000.0f;
+ continue;
+ }
+
+
+ if( cell_current->config == k_cell_type_merge )
+ {
+ // Can only move up
+ fish->dir[0] = 0;
+ fish->dir[1] = -1;
+ fish->flow_reversed = 0;
+ }
+ else
+ {
+ if( cell_current->config == k_cell_type_split )
+ {
+ // Flip flop L/R
+ fish->dir[0] = cell_current->state&FLAG_FLIP_FLOP?1:-1;
+ fish->dir[1] = 0;
+
+ if( !(cell_current->state & FLAG_TARGETED) )
+ cell_current->state ^= FLAG_FLIP_FLOP;
+ }
+ else
+ {
+ // Apply cell out-flow
+ struct cell_description *desc = &cell_descriptions[ cell_current->config ];
+
+ v2i_copy( fish->flow_reversed? desc->start: desc->end, fish->dir );
+ }
+
+ v2i pos_next;
+ v2i_add( fish->pos, fish->dir, pos_next );
+
+ struct cell *cell_next = pcell( pos_next );
+
+ if( cell_next->state & (FLAG_CANAL|FLAG_OUTPUT) )
+ {
+ struct cell_description *desc = &cell_descriptions[ cell_next->config ];
+
+ if( cell_next->config == k_cell_type_merge )
+ {
+ if( fish->dir[0] == 0 )
+ {
+ fish->state = k_fish_state_dead;
+ fish->death_time = world.sim_internal_time;
+ }
+ else
+ fish->flow_reversed = 0;
+ }
+ else
+ {
+ if( cell_next->config == k_cell_type_split )
+ {
+ if( fish->dir[0] == 0 )
+ {
+ sfx_set_playrnd( &audio_splitter, &audio_system_balls_important, 0, 1 );
+ cell_next->state |= FLAG_FLIP_ROTATING;
+
+ fish->flow_reversed = 0;
+ }
+ else
+ {
+ fish->state = k_fish_state_dead;
+ fish->death_time = world.sim_internal_time;
+ }
+ }
+ else
+ fish->flow_reversed = ( fish->dir[0] != -desc->start[0] ||
+ fish->dir[1] != -desc->start[1] )? 1: 0;
+ }
+ }
+ else
+ {
+ if( world_check_pos_ok( fish->pos, 2 ) )
+ fish->state = k_fish_state_bg;
+ else
+ {
+ fish->state = k_fish_state_dead;
+ fish->death_time = world.sim_internal_time;
+ }
+ }
+ }
+
+ //v2i_add( fish->pos, fish->dir, fish->pos );
+ }
+ else if( fish->state == k_fish_state_bg )
+ {
+ v2i_add( fish->pos, fish->dir, fish->pos );
+
+ if( !world_check_pos_ok( fish->pos, 2 ) )
+ {
+ fish->state = k_fish_state_dead;
+ fish->death_time = -1000.0f;
+ }
+ else
+ {
+ struct cell *cell_entry = pcell( fish->pos );
+
+ if( cell_entry->state & FLAG_CANAL )
+ {
+ if( cell_entry->config == k_cell_type_con_r || cell_entry->config == k_cell_type_con_u
+ || cell_entry->config == k_cell_type_con_l || cell_entry->config == k_cell_type_con_d )
+ {
+ #ifdef VG_STEAM
+ sw_set_achievement( "CAN_DO_THAT" );
+ #endif
+
+ fish->state = k_fish_state_soon_alive;
+
+ fish->dir[0] = 0;
+ fish->dir[1] = 0;
+ fish->flow_reversed = 1;
+
+ switch( cell_entry->config )
+ {
+ case k_cell_type_con_r: fish->dir[0] = 1; break;
+ case k_cell_type_con_l: fish->dir[0] = -1; break;
+ case k_cell_type_con_u: fish->dir[1] = 1; break;
+ case k_cell_type_con_d: fish->dir[1] = -1; break;
+ }
+ }
+ }
+ }
+ }
+ else { vg_error( "fish behaviour unimplemented for behaviour type (%d)\n" ); }
+
+ if( fish->state >= k_fish_state_alive )
+ alive_count ++;
+ }
+
+ // Second pass (triggers)
+ for( int i = 0; i < world.num_fishes; i ++ )
+ {
+ struct fish *fish = &world.fishes[i];
+
+ if( fish->state == k_fish_state_alive )
+ {
+ v2i_add( fish->pos, fish->dir, fish->pos );
+ struct cell *cell_current = pcell( fish->pos );
+
+ if( cell_current->state & FLAG_IS_TRIGGER )
+ {
+ int trigger_id = cell_current->links[0]?0:1;
+
+ struct cell *target_peice = &world.data[ cell_current->links[trigger_id] ];
+
+ if( target_peice->state & FLAG_EMITTER )
+ {
+ struct fish *fish = &world.fishes[ world.num_fishes ];
+ lcell( cell_current->links[trigger_id], fish->pos );
+
+ fish->state = k_fish_state_soon_alive;
+ fish->payload = target_peice->cc;
+
+ if( target_peice->config != k_cell_type_stub )
+ {
+ struct cell_description *desc = &cell_descriptions[ target_peice->config ];
+ v2i_copy( desc->start, fish->dir );
+ fish->flow_reversed = 1;
+
+ world.num_fishes ++;
+ alive_count ++;
+ }
+ }
+ else
+ {
+ if( trigger_id )
+ target_peice->state |= FLAG_FLIP_FLOP;
+ else
+ target_peice->state &= ~FLAG_FLIP_FLOP;
+ }
+
+ cell_current->state |= FLAG_TRIGGERED;
+ }
+ }
+ }
+
+ // Third pass (collisions)
+ struct fish *fi, *fj;
+
+ for( int i = 0; i < world.num_fishes; i ++ )
+ {
+ fi = &world.fishes[i];
+
+ if( fi->state == k_fish_state_alive )
+ {
+ int continue_again = 0;
+
+ for( int j = i+1; j < world.num_fishes; j ++ )
+ {
+ fj = &world.fishes[j];
+
+ if( fj->state == k_fish_state_alive )
+ {
+ v2i fi_prev;
+ v2i fj_prev;
+
+ v2i_sub( fi->pos, fi->dir, fi_prev );
+ v2i_sub( fj->pos, fj->dir, fj_prev );
+
+ int
+ collide_next_frame = (
+ (fi->pos[0] == fj->pos[0]) &&
+ (fi->pos[1] == fj->pos[1]))? 1: 0,
+ collide_this_frame = (
+ (fi_prev[0] == fj->pos[0]) &&
+ (fi_prev[1] == fj->pos[1]) &&
+ (fj_prev[0] == fi->pos[0]) &&
+ (fj_prev[1] == fi->pos[1])
+ )? 1: 0;
+
+ if( collide_next_frame || collide_this_frame )
+ {
+ #ifdef VG_STEAM
+ sw_set_achievement( "BANG" );
+ #endif
+
+ // Shatter death (+0.5s)
+ float death_time = world.sim_internal_time + ( collide_this_frame? 0.0f: 0.5f );
+
+ fi->state = k_fish_state_soon_dead;
+ fj->state = k_fish_state_soon_dead;
+ fi->death_time = death_time;
+ fj->death_time = death_time;
+
+ continue_again = 1;
+ break;
+ }
+ }
+ }
+ if( continue_again )
+ continue;
+ }
+ }
+
+ // Spawn fishes
+ for( int i = 0; i < arrlen( world.io ); i ++ )
+ {
+ struct cell_terminal *term = &world.io[ i ];
+ int is_input = pcell(term->pos)->state & FLAG_INPUT;
+
+ if( is_input )
+ {
+ if( world.sim_frame < term->runs[ world.sim_run ].condition_count )
+ {
+ char emit = term->runs[ world.sim_run ].conditions[ world.sim_frame ];
+ if( emit == ' ' )
+ continue;
+
+ struct fish *fish = &world.fishes[ world.num_fishes ];
+ v2i_copy( term->pos, fish->pos );
+
+ fish->state = k_fish_state_alive;
+ fish->payload = emit;
+
+ struct cell *cell_ptr = pcell( fish->pos );
+
+ if( cell_ptr->config != k_cell_type_stub )
+ {
+ struct cell_description *desc = &cell_descriptions[ cell_ptr->config ];
+
+ v2i_copy( desc->start, fish->dir );
+ fish->flow_reversed = 1;
+
+ world.num_fishes ++;
+ alive_count ++;
+ }
+ }
+ }
+ }
+
+ if( alive_count == 0 )
+ {
+ world.completed = 1;
+
+ for( int i = 0; i < arrlen( world.io ); i ++ )
+ {
+ struct cell_terminal *term = &world.io[ i ];
+
+ if( pcell(term->pos)->state & FLAG_OUTPUT )
+ {
+ struct terminal_run *run = &term->runs[ world.sim_run ];
+
+ if( run->recv_count == run->condition_count )
+ {
+ for( int j = 0; j < run->condition_count; j ++ )
+ {
+ if( run->recieved[j] != run->conditions[j] )
+ {
+ world.completed = 0;
+ break;
+ }
+ }
+ }
+ else
+ {
+ world.completed = 0;
+ break;
+ }
+ }
+ }
+
+ if( world.completed )
+ {
+ if( world.sim_run < world.max_runs-1 )
+ {
+ vg_success( "Run passed, starting next\n" );
+ world.sim_run ++;
+ world.sim_frame = 0;
+ world.sim_target = 0;
+ world.num_fishes = 0;
+
+ // Reset timing reference points
+ world.sim_delta_ref = vg_time;
+ world.sim_internal_ref = 0.0f;
+
+ if( world.st.buttons[ k_world_button_pause ].state )
+ world.pause_offset_target = 0.5f;
+ else
+ world.pause_offset_target = 0.0f;
+
+ world.sim_internal_time = 0.0f;
+
+ for( int i = 0; i < world.w*world.h; i ++ )
+ world.data[ i ].state &= ~FLAG_FLIP_FLOP;
+
+ continue;
+ }
+ else
+ {
+ vg_success( "Level passed!\n" );
+
+ u32 score = 0;
+ for( int i = 0; i < world.w*world.h; i ++ )
+ if( world.data[ i ].state & FLAG_CANAL )
+ score ++;
+
+ world.score = score;
+ world.time = world.sim_frame;
+
+ // Copy into career data
+ if( world.pCmpLevel )
+ {
+ career_pass_level( world.pCmpLevel, world.score, 1 );
+ }
+
+ sfx_set_play( &audio_tones, &audio_system_balls_extra, 9 );
+ failure_this_frame = 0;
+ success_this_frame = 0;
+ }
+ }
+ else
+ {
+ #ifdef VG_STEAM
+ if( world.sim_run > 0 )
+ sw_set_achievement( "GOOD_ENOUGH" );
+ #endif
+
+ vg_error( "Level failed :(\n" );
+ }
+
+ simulation_stop();
+ break;
+ }
+
+ world.sim_frame ++;
+ }
+
+ // Sounds
+ if( failure_this_frame )
+ {
+ sfx_set_play( &audio_tones, &audio_system_balls_extra, 0 );
+ }
+ else if( success_this_frame )
+ {
+ static int succes_counter = 0;
+
+ sfx_set_play( &audio_tones, &audio_system_balls_extra, 1+(succes_counter++) );
+
+ if( succes_counter == 7 )
+ succes_counter = 0;
+ }
+
+ // Position update
+ // =====================================================================================================
+
+ world.frame_lerp = world.sim_internal_time - floorf( world.sim_internal_time );
+
+ for( int i = 0; i < world.num_fishes; i ++ )
+ {
+ struct fish *fish = &world.fishes[i];
+
+ if( fish->state == k_fish_state_dead )
+ continue;
+
+ if( fish->state == k_fish_state_soon_dead && (world.sim_internal_time > fish->death_time) )
+ continue; // Todo: particle thing?
+
+ struct cell *cell = pcell(fish->pos);
+ struct cell_description *desc = &cell_descriptions[ cell->config ];
+
+ v2f const *curve;
+
+ float t = world.frame_lerp;
+ if( fish->flow_reversed && !desc->is_linear )
+ t = 1.0f-t;
+
+ v2_copy( fish->physics_co, fish->physics_v );
+
+ switch( cell->config )
+ {
+ case k_cell_type_merge:
+ if( fish->dir[0] == 1 )
+ curve = curve_12;
+ else
+ curve = curve_9;
+ break;
+ case k_cell_type_con_r: curve = curve_1; break;
+ case k_cell_type_con_l: curve = curve_4; break;
+ case k_cell_type_con_u: curve = curve_2; break;
+ case k_cell_type_con_d: curve = curve_8; break;
+ case 3: curve = curve_3; break;
+ case 6: curve = curve_6; break;
+ case 9: curve = curve_9; break;
+ case 12: curve = curve_12; break;
+ case 7:
+ if( t > curve_7_linear_section )
+ {
+ t -= curve_7_linear_section;
+ t *= (1.0f/(1.0f-curve_7_linear_section));
+
+ curve = cell->state & FLAG_FLIP_FLOP? curve_7: curve_7_1;
+ }
+ else curve = NULL;
+ break;
+ default: curve = NULL; break;
+ }
+
+ if( curve )
+ {
+ float t2 = t * t;
+ float t3 = t * t * t;
+
+ float cA = 3.0f*t2 - 3.0f*t3;
+ float cB = 3.0f*t3 - 6.0f*t2 + 3.0f*t;
+ float cC = 3.0f*t2 - t3 - 3.0f*t + 1.0f;
+
+ fish->physics_co[0] = t3*curve[3][0] + cA*curve[2][0] + cB*curve[1][0] + cC*curve[0][0];
+ fish->physics_co[1] = t3*curve[3][1] + cA*curve[2][1] + cB*curve[1][1] + cC*curve[0][1];
+ fish->physics_co[0] += (float)fish->pos[0];
+ fish->physics_co[1] += (float)fish->pos[1];
+ }
+ else
+ {
+ v2f origin;
+ origin[0] = (float)fish->pos[0] + (float)fish->dir[0]*-0.5f + 0.5f;
+ origin[1] = (float)fish->pos[1] + (float)fish->dir[1]*-0.5f + 0.5f;
+
+ fish->physics_co[0] = origin[0] + (float)fish->dir[0]*t;
+ fish->physics_co[1] = origin[1] + (float)fish->dir[1]*t;
+ }
+
+ v2_sub( fish->physics_co, fish->physics_v, fish->physics_v );
+ v2_divs( fish->physics_v, world.sim_internal_delta, fish->physics_v );
+ }
+ }
+}
+
+static void render_tiles( v2i start, v2i end, v4f const regular_colour, v4f const selected_colour )
+{
+ v2i full_start = { 0,0 };
+ v2i full_end = { world.w, world.h };
+
+ if( !start || !end )
+ {
+ start = full_start;
+ end = full_end;
+ }
+
+ glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
+
+ for( int y = start[1]; y < end[1]; y ++ )
+ {
+ for( int x = start[0]; x < end[0]; x ++ )
+ {
+ struct cell *cell = pcell((v2i){x,y});
+ int selected = world.selected == y*world.w + x;
+
+ int tile_offsets[][2] =
+ {
+ {2, 0}, {0, 3}, {0, 2}, {2, 2},
+ {1, 0}, {2, 3}, {3, 2}, {1, 3},
+ {3, 1}, {0, 1}, {1, 2}, {2, 1},
+ {1, 1}, {3, 3}, {2, 1}, {2, 1}
+ };
+
+ int uv[2] = { 3, 0 };
+
+ if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT|FLAG_EMITTER) )
+ {
+ uv[0] = tile_offsets[ cell->config ][0];
+ uv[1] = tile_offsets[ cell->config ][1];
+ } else continue;
+
+ glUniform4f( SHADER_UNIFORM( shader_tile_main, "uOffset" ), (float)x, (float)y, uv[0], uv[1] );
+ if( selected )
+ {
+ glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, selected_colour );
+ draw_mesh( 0, 2 );
+ glUniform4fv( SHADER_UNIFORM( shader_tile_main, "uColour" ), 1, regular_colour );
+ }
+ else
+ draw_mesh( 0, 2 );
+ }
+ }
+}
+
+static int world_button_exec( struct world_button *btn, v2f texture, v3f colour, enum world_button_status *status )
+{
+ static v2i click_grab = { -9999, -9999 };
+
+ // Reset click_grab
+ if( !btn )
+ {
+ click_grab[0] = -9999;
+ click_grab[1] = -9999;
+ return 0;
+ }
+
+ v2i click_tile = { world.tile_x, world.tile_y };
+
+ int triggered = 0;
+ int is_hovering = v2i_eq( click_tile, btn->position );
+
+ // Set up light targets before logic runs
+ if( btn->state )
+ btn->light_target = is_hovering? 0.9f: 0.8f;
+ else
+ btn->light_target = is_hovering? 0.2f: 0.0f;
+
+ if( vg_get_button( "primary" ) && is_hovering )
+ btn->light_target = 1.0f;
+
+ // Process click action
+ if( is_hovering )
+ {
+ if( vg_get_button_down( "primary" ) && is_hovering )
+ v2i_copy( click_tile, click_grab );
+ else if( v2i_eq( click_grab, click_tile ) && vg_get_button_up( "primary" ) )
+ {
+ // Click event
+ *status = btn->state? k_world_button_on_disable: k_world_button_on_enable;
+
+ if( btn->mode == k_world_button_mode_toggle )
+ btn->state ^= 0x1;
+
+ sfx_set_play( &audio_clicks, &audio_system_ui, btn->state? 1:0 );
+ triggered = 1;
+ }
+ }
+
+ // Drawing stage
+ v4f final_colour;
+
+ btn->light = vg_lerpf( btn->light, btn->light_target + btn->extra_light, vg_time_delta*26.0f );
+
+ v3_copy( colour, final_colour );
+ final_colour[3] = btn->light;
+
+ glUniform4f( SHADER_UNIFORM( shader_buttons, "uOffset" ),
+ btn->position[0],
+ btn->position[1],
+ texture[0],
+ texture[1]
+ );
+ glUniform4fv( SHADER_UNIFORM( shader_buttons, "uColour" ), 1, final_colour );
+ draw_mesh( 0, 2 );
+
+ return triggered;
+}
+
+static void level_selection_buttons(void)
+{
+ v3f tutorial_colour = { 0.204f, 0.345f, 0.553f };
+ v3f locked_colour = { 0.2f, 0.2f, 0.2f };
+
+ struct cmp_level *switch_level_to = NULL;
+
+ for( int i = 0; i < vg_list_size( career_packs ); i ++ )
+ {
+ struct career_level_pack *grid = &career_packs[i];
+
+ for( int j = 0; j < grid->count; j ++ )
+ {
+ struct cmp_level *lvl = &grid->pack[ j ];
+
+ if( world.pCmpLevel == lvl )
+ lvl->btn.extra_light = 0.35f + fabsf(sinf( vg_time * 2.0f )) * 0.05f;
+ else lvl->btn.extra_light = 0.2f;
+
+ if( lvl->completed_score )
+ lvl->btn.extra_light += 0.8f;
+
+ enum world_button_status status;
+ if( world_button_exec(
+ &lvl->btn,
+ (v2f){0.0f,0.0f},
+ lvl->unlocked? (lvl->is_tutorial? tutorial_colour: grid->primary_colour): locked_colour,
+ &status
+ ))
+ {
+ if( status == k_world_button_on_enable && lvl->unlocked )
+ switch_level_to = lvl;
+ }
+ }
+ }
+
+ if( switch_level_to )
+ {
+ world.st.lvl_to_load = switch_level_to;
+ world.st.lvl_load_time = vg_time + 0.25f;
+ world.st.world_transition = 1.0f;
+
+ /*
+ if( console_changelevel( 1, &switch_level_to->map_name ) )
+ {
+ world.pCmpLevel = switch_level_to;
+ gen_level_text( world.pCmpLevel );
+ }
+ */
+ }
+}
+
+void vg_render(void)
+{
+ glViewport( 0,0, vg_window_x, vg_window_y );
+
+ glDisable( GL_DEPTH_TEST );
+ glClearColor( 0.369768f, 0.3654f, 0.42f, 1.0f );
+ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
+
+ v4f const colour_default = {1.0f, 1.0f, 1.0f, 1.0f};
+ v4f const colour_selected = {0.90f, 0.92f, 1.0f, 1.0f};
+
+ int const circle_base = 6;
+ int const filled_start = circle_base+0;
+ int const filled_count = circle_base+32;
+ int const empty_start = circle_base+32;
+ int const empty_count = circle_base+32*2;
+
+ if( !world.initialzed )
+ return;
+
+ // BACKGROUND
+ // ========================================================================================================
+ use_mesh( &world.shapes );
+
+ SHADER_USE( shader_background );
+ glUniformMatrix3fv( SHADER_UNIFORM( shader_background, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
+
+ glActiveTexture( GL_TEXTURE0 );
+ glBindTexture( GL_TEXTURE_2D, world.background_data );
+ glUniform1i( SHADER_UNIFORM( shader_background, "uTexMain" ), 0 );
+
+ glUniform3f( SHADER_UNIFORM( shader_background, "uOffset" ), -16, -16, 64 );
+ glUniform1f( SHADER_UNIFORM( shader_background, "uVariance" ), 0.02f );
+
+ glActiveTexture( GL_TEXTURE1 );
+ glBindTexture( GL_TEXTURE_2D, world.random_samples );
+ glUniform1i( SHADER_UNIFORM( shader_background, "uSamplerNoise" ), 1 );
+ glUniform1f( SHADER_UNIFORM( shader_background, "uVisibility" ), 1.0f ); //world.st.world_transition );
+
+ draw_mesh( 0, 2 );
+
+ // TILESET BACKGROUND LAYER
+ // ========================================================================================================
+ use_mesh( &world.shapes );
+ SHADER_USE( shader_tile_main );
+
+ m2x2f subtransform;
+ m2x2_identity( subtransform );
+ glUniformMatrix2fv( SHADER_UNIFORM( shader_tile_main, "uSubTransform" ), 1, GL_FALSE, (float *)subtransform );
+ glUniformMatrix3fv( SHADER_UNIFORM( shader_tile_main, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
+ glUniform1f( SHADER_UNIFORM( shader_tile_main, "uGhost" ), 0.0f );
+ glUniform1f( SHADER_UNIFORM( shader_tile_main, "uForeground" ), 0.0f );
+ glUniform1f( SHADER_UNIFORM( shader_tile_main, "uVisibility" ), world.st.world_transition * 2.0f );
+
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glBlendEquation(GL_FUNC_ADD);
+
+ // Bind textures
+ vg_tex2d_bind( &tex_tile_data, 0 );
+ glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexGlyphs" ), 0 );
+
+ vg_tex2d_bind( &tex_wood, 1 );
+ glUniform1i( SHADER_UNIFORM( shader_tile_main, "uTexWood" ), 1 );
+
+ render_tiles( NULL, NULL, colour_default, colour_default );
+
+ // MARBLES
+ // ========================================================================================================
+ SHADER_USE( shader_ball );
+ glUniformMatrix3fv( SHADER_UNIFORM( shader_ball, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
+
+ vg_tex2d_bind( &tex_ball_noise, 0 );
+ glUniform1i( SHADER_UNIFORM( shader_ball, "uTexMain" ), 0 );
+
+ if( world.st.buttons[ k_world_button_sim ].state )
+ {
+ for( int i = 0; i < world.num_fishes; i ++ )
+ {
+ struct fish *fish = &world.fishes[i];
+ v3f render_pos;
+ render_pos[2] = 1.0f;