commit 660a536979ab4771513c2b7ca2736b22038360bd parent 8fbc7396d6da1a130c86e57398a2c2c6c1ee2ef7 Author: Michael Savage <mikejsavage@gmail.com> Date: Tue Feb 2 19:36:16 +0000 Store all the compressed tiles in memory Diffstat:
terrain_manager.cc | | | 28 | +++++++++++++++------------- |
terrain_manager.h | | | 22 | ++++++++++++++-------- |
diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -64,20 +64,13 @@ static void terrain_load_tile( TerrainManager * tm, u32 tx, u32 ty, u32 vx, u32 vy ) { - char path[ 256 ]; - sprintf( path, "%s/%d_%d.tga", tm->dir, tx, ty ); - Heightmap * hm = &tm->tiles[ vx ][ vy ]; if( hm->vbo_verts != 0 ) heightmap_destroy( hm ); - size_t len; - u8 * contents = file_get_contents( path, &len ); - assert( contents ); - + CompressedTile ct = tm->compressed_tiles[ tx ][ ty ]; int width, height; - u8 * pixels = stbi_load_from_memory( contents, len, &width, &height, NULL, 1 ); - free( contents ); + u8 * pixels = stbi_load_from_memory( ct.data, ct.len, &width, &height, NULL, 1 ); if( !pixels ) err( 1, "stbi_load failed (%s)", stbi_failure_reason() ); @@ -90,19 +83,28 @@ void terrain_init( TerrainManager * tm, const char * tiles_dir, MemoryArena * arena ) { - assert( strlen( tiles_dir ) < array_count( tm->dir ) ); - strcpy( tm->dir, tiles_dir ); - tm->arena = arena; char dims_path[ 256 ]; - sprintf( dims_path, "%s/dims.txt", tm->dir ); + sprintf( dims_path, "%s/dims.txt", tiles_dir ); FILE * dims = fopen( dims_path, "r" ); fscanf( dims, "%d %d", &tm->width, &tm->height ); fclose( dims ); printf( "%d %d\n", tm->width, tm->height ); + for( u32 ty = 0; ty < tm->height / TILE_SIZE; ty++ ) { + for( u32 tx = 0; tx < tm->width / TILE_SIZE; tx++ ) { + char tile_path[ 256 ]; + // TODO: make sure this fits + sprintf( tile_path, "%s/%d_%d.tga.png", tiles_dir, tx, ty ); + + size_t len; + u8 * data = file_get_contents( tile_path, &len ); + tm->compressed_tiles[ tx ][ ty ] = { data, len }; + } + } + tm->shader = compile_shader( vert_src, frag_src, "colour" ); tm->at_pos = glGetAttribLocation( tm->shader, "position" ); diff --git a/terrain_manager.h b/terrain_manager.h @@ -8,14 +8,18 @@ #include "memory_arena.h" #include "heightmap.h" -static const u32 TILE_SIZE = 128; +static const u16 TILE_SIZE = 128; +static const u16 WORLD_SIZE = 170; -static const u32 VIEW_SIZE = 5; -static const u32 VIEW_HALF = VIEW_SIZE / 2; +static const u16 VIEW_SIZE = 5; +static const u16 VIEW_HALF = VIEW_SIZE / 2; -struct TerrainManager { - char dir[ 128 ]; +struct CompressedTile { + u8 * data; + size_t len; +}; +struct TerrainManager { MemoryArena * arena; u32 width, height; @@ -33,9 +37,11 @@ struct TerrainManager { // tile_x and tile_y are the coordinates of the tile we are centered on // view_x and view_y are indices into tiles of the tile we are centered on - u32 tile_x, tile_y; - u32 view_left, view_top; - Heightmap tiles[ VIEW_SIZE ][ VIEW_SIZE ]; + CompressedTile compressed_tiles[ WORLD_SIZE ][ WORLD_SIZE ]; + Heightmap tiles[ WORLD_SIZE ][ WORLD_SIZE ]; + u8 lods[ WORLD_SIZE ][ WORLD_SIZE ]; + s16 tile_x, tile_y; + s16 view_left, view_top; }; void terrain_init( TerrainManager * tm, const char * tiles_dir, MemoryArena * arena );