medfall

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

commit e519c8953eed9f3ad4ac31f4c446650e3ea15c83
parent 0f522205317e5a7d8e27e1ea45238c851db8c7ab
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri Dec  9 20:41:13 +0200

Make heightmap_build_quadtree fill in a user provided array

Diffstat:
heightmap.cc | 11+++++++----
heightmap.h | 5+++--
mod_btt.cc | 5++++-
3 files changed, 14 insertions(+), 7 deletions(-)
diff --git a/heightmap.cc b/heightmap.cc @@ -245,17 +245,20 @@ static void heightmap_build_quadtree_node( HeightmapQuadTree * qt, size_t node_i qt->nodes[ node_idx ].max_z = hi; } -HeightmapQuadTree heightmap_build_quadtree( const Heightmap * hm ) { +size_t heightmap_quadtree_max_nodes( const Heightmap * hm ) { // with a power of 2 + 1 sized quadtree it should be exactly 4/3 (1 + // 1/4 + 1/16 + ...) but with other sizes it gets funny. 1.34 seems to // work with minimal wasted space - size_t num_nodes = 1.34 * ( hm->width - 1 ) * ( hm->width - 1 ); + return 1.34 * ( hm->width - 1 ) * ( hm->width - 1 ); +} + +HeightmapQuadTree heightmap_build_quadtree( const Heightmap * hm, array< HeightmapQuadTreeNode > & nodes ) { + ASSERT( nodes.n >= heightmap_quadtree_max_nodes( hm ) ); HeightmapQuadTree qt; qt.hm = hm; qt.dim = hm->width - 1; - qt.nodes_memory = ( HeightmapQuadTreeNode * ) malloc( num_nodes * sizeof( HeightmapQuadTreeNode ) ); - qt.nodes = array< HeightmapQuadTreeNode >( qt.nodes_memory, num_nodes ); + qt.nodes = nodes; memset( qt.nodes.ptr(), 0, qt.nodes.num_bytes() ); AABBu32 aabb( v3u32( 0, 0, 0 ), v3u32( qt.dim, qt.dim, 255 ) ); diff --git a/heightmap.h b/heightmap.h @@ -2,7 +2,7 @@ #define _HEIGHTMAP_H_ #include "intrinsics.h" -#include "memory_arena.h" +#include "array.h" #include "linear_algebra.h" class Heightmap { @@ -35,7 +35,8 @@ struct HeightmapQuadTree { const Heightmap * hm; }; -HeightmapQuadTree heightmap_build_quadtree( const Heightmap * hm ); +size_t heightmap_quadtree_max_nodes( const Heightmap * hm ); +HeightmapQuadTree heightmap_build_quadtree( const Heightmap * hm, array< HeightmapQuadTreeNode > & nodes ); // TODO: these are all pretty general // struct Intersection { diff --git a/mod_btt.cc b/mod_btt.cc @@ -170,7 +170,10 @@ extern "C" GAME_INIT( game_init ) { int w, h; u8 * pixels = stbi_load( "terrains/" TERRAIN_NAME, &w, &h, NULL, 1 ); heightmap_init( &game->hm, pixels, w, h ); - qt = heightmap_build_quadtree( &game->hm ); + + size_t num_nodes = heightmap_quadtree_max_nodes( &game->hm ); + array< HeightmapQuadTreeNode > nodes = memarena_push_array( &mem->persistent_arena, HeightmapQuadTreeNode, num_nodes ); + qt = heightmap_build_quadtree( &game->hm, nodes ); size_t horizons_size = ( w + 1 ) * ( h + 1 ) * sizeof( float ); horizons = ( float * ) malloc( horizons_size );