Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

moved bit to STL

  • Loading branch information...
commit 8aab383be00ebf9b313f41cfeee30abcb6701ceb 1 parent 2de05ab
rtv authored
View
73 libstage/block.cc
@@ -21,8 +21,8 @@ Block::Block( Model* mod,
pts( (stg_point_t*)g_memdup( pts, pt_count * sizeof(stg_point_t)) ),
color( color ),
inherit_color( inherit_color ),
- rendered_cells( g_ptr_array_sized_new(32) ),
- candidate_cells( g_ptr_array_sized_new(32) )
+ rendered_cells( new std::vector<Cell*> ),
+ candidate_cells( new std::vector<Cell*> )
{
assert( mod );
assert( pt_count > 0 );
@@ -42,8 +42,8 @@ Block::Block( Model* mod,
pts(NULL),
color(0),
inherit_color(true),
- rendered_cells( g_ptr_array_sized_new(32) ),
- candidate_cells( g_ptr_array_sized_new(32) )
+ rendered_cells( new std::vector<Cell*> ),
+ candidate_cells( new std::vector<Cell*> )
{
assert(mod);
assert(wf);
@@ -59,8 +59,8 @@ Block::~Block()
if( pts ) delete[] pts;
InvalidateModelPointCache();
- g_ptr_array_free( rendered_cells, TRUE );
- g_ptr_array_free( candidate_cells, TRUE );
+ delete rendered_cells;
+ delete candidate_cells;
}
void Block::Translate( double x, double y )
@@ -143,9 +143,10 @@ stg_color_t Block::GetColor()
GList* Block::AppendTouchingModels( GList* l )
{
// for every cell we are rendered into
- for( unsigned int i=0; i<rendered_cells->len; i++ )
+ for( unsigned int i=0; i<rendered_cells->size(); i++ )
{
- Cell* c = (Cell*)g_ptr_array_index( rendered_cells, i);
+ //Cell* c = (Cell*)g_ptr_array_index( rendered_cells, i);
+ Cell* c = (*rendered_cells)[i];
// for every block rendered into that cell
for( std::list<Block*>::iterator it = c->blocks.begin();
@@ -173,10 +174,11 @@ Model* Block::TestCollision()
if( mod->vis.obstacle_return )
// for every cell we may be rendered into
- for( unsigned int i=0; i<candidate_cells->len; i++ )
+ for( unsigned int i=0; i<candidate_cells->size(); i++ )
{
- Cell* c = (Cell*)g_ptr_array_index(candidate_cells, i);
-
+ //Cell* c = (Cell*)g_ptr_array_index(candidate_cells, i);
+ Cell* c = (*candidate_cells)[i];
+
// for every rendered into that cell
for( std::list<Block*>::iterator it = c->blocks.begin();
it != c->blocks.end();
@@ -202,24 +204,7 @@ Model* Block::TestCollision()
}
-void Block::RemoveFromCellArray( GPtrArray* ptrarray )
-{
- for( unsigned int i=0; i<ptrarray->len; i++ )
- ((Cell*)g_ptr_array_index(ptrarray, i))->RemoveBlock( this );
-}
-
-void Block::AddToCellArray( GPtrArray* ptrarray )
-{
- for( unsigned int i=0; i<ptrarray->len; i++ )
- ((Cell*)g_ptr_array_index(ptrarray, i))->AddBlock( this );
-}
-
-// used as a callback to gather an array of cells in a polygon
-void AppendCellToPtrArray( Cell* c, GPtrArray* a )
-{
- g_ptr_array_add( a, c );
-}
// used as a callback to gather an array of cells in a polygon
void AddBlockToCell( Cell* c, Block* block )
@@ -241,17 +226,34 @@ void Block::UnMap()
{
RemoveFromCellArray( rendered_cells );
- g_ptr_array_set_size( rendered_cells, 0 );
+ //g_ptr_array_set_size( rendered_cells, 0 );
+ rendered_cells->clear();
mapped = false;
}
+void Block::RemoveFromCellArray( std::vector<Cell*> * cells )
+{
+ for( std::vector<Cell*>::iterator it = cells->begin();
+ it != cells->end();
+ ++it )
+ (*it)->RemoveBlock( this);
+}
+
+void Block::AddToCellArray( std::vector<Cell*> * cells )
+{
+ for( std::vector<Cell*>::iterator it = cells->begin();
+ it != cells->end();
+ ++it )
+ (*it)->AddBlock( this);
+}
+
void Block::SwitchToTestedCells()
{
RemoveFromCellArray( rendered_cells );
AddToCellArray( candidate_cells );
// switch current and candidate cell pointers
- GPtrArray* tmp = rendered_cells;
+ std::vector<Cell*> * tmp = rendered_cells;
rendered_cells = candidate_cells;
candidate_cells = tmp;
@@ -291,6 +293,12 @@ void Block::InvalidateModelPointCache()
}
}
+// callback used below
+static void AppendCellToVector( Cell* c, std::vector<Cell*> * a )
+{
+ a->push_back( c );
+}
+
void Block::GenerateCandidateCells()
{
stg_point_t* mpts = GetPointsInModelCoords();
@@ -300,11 +308,12 @@ void Block::GenerateCandidateCells()
for( unsigned int i=0; i<pt_count; i++ )
gpts[i] = mod->LocalToGlobal( mpts[i] );
- g_ptr_array_set_size( candidate_cells, 0 );
+ //g_ptr_array_set_size( candidate_cells, 0 );
+ candidate_cells->clear();
mod->world->
ForEachCellInPolygon( gpts, pt_count,
- (stg_cell_callback_t)AppendCellToPtrArray,
+ (stg_cell_callback_t)AppendCellToVector,
candidate_cells );
delete[] gpts;
View
2  libstage/region.cc
@@ -69,7 +69,7 @@ void SuperRegion::Draw( bool drawall )
// outline regions with contents
glRecti( x<<RBITS, y<<RBITS,
(x+1)<<RBITS, (y+1)<<RBITS );
- else// if( r->cells )
+ else if( r->cells )
{
double left = x << RBITS;
double right = (x+1) << RBITS;
View
24 libstage/stage.hh
@@ -902,7 +902,6 @@ namespace Stg
{
friend class Model; // allow access to private members
friend class Block;
- //friend class StgTime;
friend class Canvas;
private:
@@ -1197,7 +1196,9 @@ namespace Stg
void SetZ( double min, double max );
void RecordRendering( Cell* cell )
- { g_ptr_array_add( rendered_cells, (gpointer)cell ); };
+ { //g_ptr_array_add( rendered_cells, (gpointer)cell ); };
+ rendered_cells->push_back( cell );
+ }
stg_point_t* Points( unsigned int *count )
{ if( count ) *count = pt_count; return pts; };
@@ -1205,11 +1206,8 @@ namespace Stg
//bool IntersectGlobalZ( stg_meters_t z )
//{ return( z >= global_zmin && z <= global_zmax ); }
- void AddToCellArray( GPtrArray* ptrarray );
- void RemoveFromCellArray( GPtrArray* ptrarray );
-
- //void AddToCellArray( std::vector<Cell*>& blocks );
- //void RemoveFromCellArray( std::vector<Cell*>& blocks );
+ void AddToCellArray( std::vector<Cell*>* blocks );
+ void RemoveFromCellArray( std::vector<Cell*>* blocks );
void GenerateCandidateCells();
@@ -1261,18 +1259,18 @@ namespace Stg
bool mapped;
- /** an array of pointers to cells into which this block has been
- rendered (speeds up UnMapping) */
- GPtrArray* rendered_cells;
-
+ /** record the cells into which this block has been rendered to
+ UnMapping them very quickly. */
+ std::vector<Cell*> * rendered_cells;
+
/** When moving a model, we test for collisions by generating, for
each block, a list of the cells in which it would be rendered if the
move were to be successful. If no collision occurs, the move is
allowed - the rendered cells are cleared, the potential cells are
written, and the pointers to the rendered and potential cells are
switched for next time (avoiding a memory copy).*/
- GPtrArray* candidate_cells;
-
+ std::vector<Cell*> * candidate_cells;
+
/** find the position of a block's point in model coordinates
(m) */
stg_point_t BlockPointToModelMeters( const stg_point_t& bpt );
View
7 libstage/world.cc
@@ -1006,6 +1006,9 @@ inline Cell* World::GetCellCreate( const int32_t x, const int32_t y )
->GetCellGlobalCreate( glob )) ;
}
+// TODO - each line end point is processed twice here - could save a
+// teeny bit of time if we did this more cleverly
+// also - replace C arrays with vectors?
void World::ForEachCellInPolygon( const stg_point_t pts[],
const unsigned int pt_count,
stg_cell_callback_t cb,
@@ -1048,6 +1051,10 @@ void World::ForEachCellInLine( const stg_point_t& pt1,
{
// find or create the cell at this location, then call the callback
// with the cell, block and user-defined argument
+
+ // TODO - could get rid of this callback, as we only ever use
+ // one function here we can speed it up a bit by having the code
+ // inline
(*cb)( GetCellCreate( x,y ), cb_arg );
// cleverly skip to the next cell
Please sign in to comment.
Something went wrong with that request. Please try again.