+ reset_player( 1, (const char *[]){"start"} );
+}
+
+VG_STATIC void world_process_resources(void)
+{
+ vg_info( "Loading textures\n" );
+ world.texture_count = world.meta->info.texture_count;
+ world.textures =
+ vg_linear_alloc( world.dynamic_vgl, sizeof(GLuint)*world.texture_count );
+
+ vg_acquire_thread_sync();
+ {
+ /* error texture */
+ world.textures[0] = vg_tex2d_new();
+ vg_tex2d_set_error();
+ vg_tex2d_nearest();
+ vg_tex2d_repeat();
+
+ for( int i=1; i<world.texture_count; i++ )
+ {
+ mdl_texture *tex = &world.meta->texture_buffer[i];
+
+ if( !tex->pack_offset )
+ {
+ vg_release_thread_sync();
+ vg_fatal_exit_loop( "World models must have packed textures!" );
+ }
+
+ vg_linear_clear( vg_mem.scratch );
+ world.textures[i] = vg_tex2d_new();
+ vg_tex2d_set_error();
+ vg_tex2d_qoi( world.meta->pack + tex->pack_offset, tex->pack_length,
+ mdl_pstr( world.meta, tex->pstr_name ));
+ vg_tex2d_nearest();
+ vg_tex2d_repeat();
+ }
+ }
+ vg_release_thread_sync();
+
+ vg_info( "Loading materials\n" );
+
+ u32 size = sizeof(struct world_material) * world.meta->info.material_count;
+ world.materials = vg_linear_alloc( world.dynamic_vgl, size );
+
+ world.material_count = world.meta->info.material_count;
+ memset( world.materials, 0, size );
+
+ for( int i=1; i<world.material_count; i++ )
+ world.materials[i].info = world.meta->material_buffer[i];
+
+ /* error material */
+ struct world_material *errmat = &world.materials[0];
+ v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour );
+ v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour1 );
+ errmat->info.flags = 0x00;
+ errmat->info.pstr_name = 0; /* useless? */
+ errmat->info.shader = -1;
+ errmat->info.tex_decal = 0;
+ errmat->info.tex_diffuse = 0;
+ errmat->info.tex_normal = 0;
+}
+
+VG_STATIC void world_unload(void)
+{
+ vg_acquire_thread_sync();
+
+ /* free meshes */
+ mesh_free( &world.mesh_route_lines );
+ mesh_free( &world.mesh_geo );
+ mesh_free( &world.mesh_no_collide );
+
+ world.time = 0.0;
+ world.rewind_from = 0.0;
+ world.rewind_to = 0.0;
+ world.last_use = 0.0;
+ world.active_gate = 0;
+ world.current_run_version = 2;
+ world.active_route_board = 0;
+ v3_zero( world.render_gate_pos );
+
+ for( int i=0; i<vg_list_size(world.ui_bars); i++ )
+ {
+ struct route_ui_bar *uib = &world.ui_bars[i];
+ uib->segment_start = 0;
+ uib->segment_count = 0;
+ uib->fade_start = 0;
+ uib->fade_count = 0;
+ uib->fade_timer_start = 0.0;
+ uib->xpos = 0.0f;
+ }
+
+ /* delete textures and meshes */
+ glDeleteTextures( world.texture_count, world.textures );
+
+ /* delete the entire block of memory */
+ vg_linear_clear( world.dynamic_vgl );
+ vg_linear_clear( world.audio_vgl );
+
+ /* clean dangling pointers */
+ world.meta = NULL;
+
+ world.textures = NULL;
+ world.texture_count = 0;
+ world.materials = NULL;
+ world.material_count = 0;
+
+ world.scene_geo = NULL;
+ world.scene_no_collide = NULL;
+ world.scene_lines = NULL;
+ world.grind_edges = NULL;
+ world.grind_edge_count = 0;
+
+ world.grind_bh = NULL;
+ world.geo_bh = NULL;
+ world.trigger_bh = NULL;
+ world.audio_bh = NULL;
+
+ world.spawns = NULL;
+ world.spawn_count = 0;
+
+ world.audio_things = NULL;
+ world.audio_things_count = 0;
+
+ world.triggers = NULL;
+ world.trigger_count = 0;
+
+ world.logic_relays = NULL;
+ world.relay_count = 0;
+
+ world.logic_achievements = NULL;
+ world.achievement_count = 0;
+
+ world.nodes = NULL;
+ world.node_count = 0;
+
+ world.routes = NULL;
+ world.route_count = 0;
+
+ world.gates = NULL;
+ world.gate_count = 0;
+
+ world.collectors = NULL;
+ world.collector_count = 0;
+
+ world.water.enabled = 0;
+
+ vg_release_thread_sync();
+}
+
+VG_STATIC void world_load(void)
+{
+ world_unload();
+
+ world.meta = mdl_load_full( world.dynamic_vgl, world.world_name );
+ vg_info( "Loading world: %s\n", world.world_name );
+
+ /* process resources from pack */
+ world_process_resources();
+
+ /* dynamic allocations */
+ world_ents_allocate();
+ world_routes_allocate();
+
+ /* meta processing */
+ world_routes_process();
+ world_entities_process();
+
+ /* main bulk */
+ world_generate();
+ world_routes_generate();
+ world_post_process();