medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit fd14a29656146cbb49fbc7bfa9adff9b6444d12a
parent de4f216635ade797d2226a37354930daf8e142cd
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed Jan 18 20:18:31 +0200

Use file_get_array in terrain manager

Diffstat:
terrain_manager.cc | 62+++++++++++++++++++++++++-------------------------------------
terrain_manager.h | 12++++--------
2 files changed, 29 insertions(+), 45 deletions(-)
diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -2,6 +2,7 @@ #include "intrinsics.h" #include "log.h" +#include "array.h" #include "heightmap.h" #include "terrain_manager.h" #include "memory_arena.h" @@ -123,8 +124,8 @@ static WORK_QUEUE_CALLBACK( async_load_tile ) { s32 ty = alt->ty; free( alt ); - const CompressedTile & compressed_tile = tm->compressed_tiles[ tx ][ ty ]; - DecompressedTile & decompressed_tile = tm->decompressed_tiles[ tx ][ ty ]; + const CompressedTile & ct = tm->compressed_tiles[ tx ][ ty ]; + DecompressedTile & dt = tm->decompressed_tiles[ tx ][ ty ]; size_t num_points = ( TILE_SIZE + 1 ) * ( TILE_SIZE + 1 ); @@ -133,50 +134,51 @@ static WORK_QUEUE_CALLBACK( async_load_tile ) { u8 * decompressed_heightmap = ( u8 * ) malloc( decompressed_heightmap_size ); ASSERT( decompressed_heightmap != NULL ); int ok_heightmap = LZ4_decompress_safe( - ( const char * ) compressed_tile.heightmap_data, + ct.heightmap.ptr(), ( char * ) decompressed_heightmap, - compressed_tile.heightmap_size, decompressed_heightmap_size ); + ct.heightmap.num_bytes(), decompressed_heightmap_size ); ASSERT( ok_heightmap > 0 ); - heightmap_init( &decompressed_tile.heightmap, decompressed_heightmap, TILE_SIZE + 1, TILE_SIZE + 1 ); + heightmap_init( &dt.heightmap, decompressed_heightmap, TILE_SIZE + 1, TILE_SIZE + 1 ); // normalmap size_t decompressed_normalmap_size = num_points * sizeof( v3 ); v3 * decompressed_normalmap = ( v3 * ) malloc( decompressed_normalmap_size ); ASSERT( decompressed_normalmap != NULL ); int ok_normalmap = LZ4_decompress_safe( - ( const char * ) compressed_tile.normalmap_data, + ct.normalmap.ptr(), ( char * ) decompressed_normalmap, - compressed_tile.normalmap_size, decompressed_normalmap_size ); + ct.normalmap.num_bytes(), decompressed_normalmap_size ); ASSERT( ok_normalmap > 0 ); - decompressed_tile.normalmap = decompressed_normalmap; + dt.normalmap = decompressed_normalmap; // horizonmap + // size_t decompressed_horizonmap_size = squish::GetStorageRequirements( TILE_SIZE + 1, TILE_SIZE + 1, squish::kBc4 ); size_t decompressed_horizonmap_size = num_points * sizeof( float ); float * decompressed_horizonmap = ( float * ) malloc( decompressed_horizonmap_size ); ASSERT( decompressed_horizonmap != NULL ); int ok_horizonmap = LZ4_decompress_safe( - ( const char * ) compressed_tile.horizonmap_data, + ct.horizonmap.ptr(), ( char * ) decompressed_horizonmap, - compressed_tile.horizonmap_size, decompressed_horizonmap_size ); + ct.horizonmap.num_bytes(), decompressed_horizonmap_size ); ASSERT( ok_horizonmap > 0 ); // quadtree - size_t max_decompressed_quadtree_size = heightmap_quadtree_max_nodes( &decompressed_tile.heightmap ) * sizeof( HeightmapQuadTreeNode ); + size_t max_decompressed_quadtree_size = heightmap_quadtree_max_nodes( &dt.heightmap ) * sizeof( HeightmapQuadTreeNode ); HeightmapQuadTreeNode * decompressed_quadtree = ( HeightmapQuadTreeNode * ) malloc( max_decompressed_quadtree_size ); ASSERT( decompressed_quadtree != NULL ); int ok_quadtree = LZ4_decompress_safe( - ( const char * ) compressed_tile.quadtree_data, + ct.quadtree.ptr(), ( char * ) decompressed_quadtree, - compressed_tile.quadtree_size, max_decompressed_quadtree_size ); + ct.quadtree.num_bytes(), max_decompressed_quadtree_size ); ASSERT( ok_quadtree > 0 ); size_t decompressed_quadtree_size = checked_cast< size_t >( ok_quadtree ); - decompressed_tile.quadtree.dim = TILE_SIZE; - decompressed_tile.quadtree.nodes_memory = decompressed_quadtree; - decompressed_tile.quadtree.nodes = array< HeightmapQuadTreeNode >( decompressed_quadtree, decompressed_quadtree_size / sizeof( HeightmapQuadTreeNode ) ); - decompressed_tile.quadtree.hm = &decompressed_tile.heightmap; + dt.quadtree.dim = TILE_SIZE; + dt.quadtree.nodes_memory = decompressed_quadtree; + dt.quadtree.nodes = array< HeightmapQuadTreeNode >( decompressed_quadtree, decompressed_quadtree_size / sizeof( HeightmapQuadTreeNode ) ); + dt.quadtree.hm = &dt.heightmap; // generate btt // TODO: should generate a triangle mesh here instead of in gpubtt_init @@ -184,7 +186,7 @@ static WORK_QUEUE_CALLBACK( async_load_tile ) { void * btt_memory = malloc( megabytes( 16 ) ); memarena_init( &btt_arena, ( u8 * ) btt_memory, megabytes( 16 ) ); GPUTileData gpu_tile_data; - BTTs btt = btt_from_heightmap( &decompressed_tile.heightmap, &btt_arena ); + BTTs btt = btt_from_heightmap( &dt.heightmap, &btt_arena ); gpu_tile_data.btt = btt; gpu_tile_data.btt_memory = btt_memory; @@ -259,25 +261,11 @@ void terrain_init( str< 512 > horizonmap_path( "%s/%d_%d_horizons.lz4", tiles_dir, tx, ty ); str< 512 > quadtree_path( "%s/%d_%d_quadtree.lz4", tiles_dir, tx, ty ); - size_t heightmap_size, normalmap_size, horizonmap_size, quadtree_size; - u8 * heightmap = file_get_contents( - heightmap_path.c_str(), &heightmap_size ); - u8 * normalmap = file_get_contents( - normalmap_path.c_str(), &normalmap_size ); - u8 * horizonmap = file_get_contents( - horizonmap_path.c_str(), &horizonmap_size ); - u8 * quadtree = file_get_contents( - quadtree_path.c_str(), &quadtree_size ); - - CompressedTile & compressed_tile = tm->compressed_tiles[ tx ][ ty ]; - compressed_tile.heightmap_data = heightmap; - compressed_tile.heightmap_size = heightmap_size; - compressed_tile.normalmap_data = normalmap; - compressed_tile.normalmap_size = normalmap_size; - compressed_tile.horizonmap_data = horizonmap; - compressed_tile.horizonmap_size = horizonmap_size; - compressed_tile.quadtree_data = quadtree; - compressed_tile.quadtree_size = quadtree_size; + CompressedTile & ct = tm->compressed_tiles[ tx ][ ty ]; + ct.heightmap = file_get_array< char >( heightmap_path.c_str() ); + ct.normalmap = file_get_array< char >( normalmap_path.c_str() ); + ct.horizonmap = file_get_array< char >( horizonmap_path.c_str() ); + ct.quadtree = file_get_array< char >( quadtree_path.c_str() ); tm->tile_states[ tx ][ ty ] = TILE_EMPTY; } diff --git a/terrain_manager.h b/terrain_manager.h @@ -16,14 +16,10 @@ static const u16 VIEW_SIZE = 11; static const u16 VIEW_HALF = VIEW_SIZE / 2; struct CompressedTile { - u8 * heightmap_data; - size_t heightmap_size; - u8 * normalmap_data; - size_t normalmap_size; - u8 * horizonmap_data; - size_t horizonmap_size; - u8 * quadtree_data; - size_t quadtree_size; + array< char > heightmap; + array< char > normalmap; + array< char > horizonmap; + array< char > quadtree; }; struct GPUTileData {