Permalink
Browse files

replacing some C style arrays with vectors and strings, for consistency

  • Loading branch information...
1 parent 49cd634 commit e8d54f873205ba8acd01015d90559950de03f832 Richard Vaughan committed Aug 27, 2010
Showing with 300 additions and 355 deletions.
  1. +45 −50 libstage/#blockgroup.cc#
  2. +19 −25 libstage/block.cc
  3. +36 −42 libstage/blockgroup.cc
  4. +5 −5 libstage/main.cc
  5. +26 −30 libstage/model.cc
  6. +18 −31 libstage/model_ranger.cc
  7. +51 −75 libstage/stage.cc
  8. +27 −21 libstage/stage.hh
  9. +26 −27 libstage/world.cc
  10. +15 −17 libstage/worldfile.cc
  11. +3 −3 libstage/worldfile.hh
  12. +1 −1 libstage/worldgui.cc
  13. +28 −28 worlds/fasr.world
View
@@ -71,7 +71,7 @@ void BlockGroup::CalcSize()
minx = miny = billion;
maxx = maxy = -billion;
- size.z = 0.0; // grow to largest z we see
+x size.z = 0.0; // grow to largest z we see
FOR_EACH( it, blocks )
{
@@ -254,69 +254,64 @@ void BlockGroup::LoadBlock( Model* mod, Worldfile* wf, int entity )
void BlockGroup::LoadBitmap( Model* mod, const std::string& bitmapfile, Worldfile* wf )
{
- Print_DEBUG1( "attempting to load bitmap \"%s\n", bitmapfile );
-
- char full[_POSIX_PATH_MAX];
+ PRINT_DEBUG1( "attempting to load bitmap \"%s\n", bitmapfile );
+
+ std::string full;
if( bitmapfile[0] == '/' )
- strncpy( full, bitmapfile.c_str(), _POSIX_PATH_MAX );
+ full = bitmapfile;
else
- {
- char *tmp = strdup(wf->filename.c_str());
- snprintf( full, _POSIX_PATH_MAX,
- "%s/%s", dirname(tmp), bitmapfile.c_str() );
- free(tmp);
- }
+ {
+ char* workaround_const = strdup(wf->filename.c_str());
+ full = std::string(dirname(workaround_const)) + "/" + bitmapfile;
+ free( workaround_const );
+ }
PRINT_DEBUG1( "attempting to load image %s", full );
-
- stg_rotrect_t* rects = NULL;
- unsigned int rect_count = 0;
+
+ std::vector<rotrect_t> rects;
unsigned int width, height;
- if( stg_rotrects_from_image_file( full,
- &rects,
- &rect_count,
- &width, &height ) )
+ if( rotrects_from_image_file( full,
+ rects,
+ width,
+ height ) )
{
PRINT_ERR1( "failed to load rects from image file \"%s\"",
- full );
+ full.c_str() );
return;
}
//printf( "found %d rects in \"%s\" at %p\n",
// rect_count, full, rects );
- if( rects && (rect_count > 0) )
- {
- // TODO fix this
- Color col( 1.0, 0.0, 1.0, 1.0 );
-
- for( unsigned int r=0; r<rect_count; r++ )
+ // TODO fix this
+ Color col( 1.0, 0.0, 1.0, 1.0 );
+
+ FOR_EACH( rect, rects )
{
- stg_point_t pts[4];
-
- double x = rects[r].pose.x;
- double y = rects[r].pose.y;
- double w = rects[r].size.x;
- double h = rects[r].size.y;
-
- pts[0].x = x;
- pts[0].y = y;
- pts[1].x = x + w;
- pts[1].y = y;
- pts[2].x = x + w;
- pts[2].y = y + h;
- pts[3].x = x;
- pts[3].y = y + h;
-
- AppendBlock( new Block( mod,
- pts,4,
- 0,1,
- col,
- true ) );
+ std::vector<point_t> pts(4);
+
+ double x = rect->pose.x;
+ double y = rect->pose.y;
+ double w = rect->size.x;
+ double h = rect->size.y;
+
+ pts[0].x = x;
+ pts[0].y = y;
+ pts[1].x = x + w;
+ pts[1].y = y;
+ pts[2].x = x + w;
+ pts[2].y = y + h;
+ pts[3].x = x;
+ pts[3].y = y + h;
+
+ AppendBlock( new Block( mod,
+ pts,
+ 0,1,
+ col,
+ true,
+ false) );
}
- free( rects );
- }
CalcSize();
}
@@ -325,8 +320,8 @@ void BlockGroup::LoadBitmap( Model* mod, const std::string& bitmapfile, Worldfil
void BlockGroup::Rasterize( uint8_t* data,
unsigned int width,
unsigned int height,
- stg_meters_t cellwidth,
- stg_meters_t cellheight )
+ meters_t cellwidth,
+ meters_t cellheight )
{
FOR_EACH( it, blocks )
(*it)->Rasterize( data, width, height, cellwidth, cellheight );
View
@@ -11,17 +11,15 @@ static void canonicalize_winding(vector<point_t>& pts);
blocks. The point data is copied, so pts can safely be freed
after calling this.*/
Block::Block( Model* mod,
- point_t* pts,
- size_t pt_count,
+ const std::vector<point_t>& pts,
meters_t zmin,
meters_t zmax,
Color color,
bool inherit_color,
bool wheel ) :
mod( mod ),
mpts(),
- pt_count( pt_count ),
- pts(),
+ pts(pts),
local_z( zmin, zmax ),
color( color ),
inherit_color( inherit_color ),
@@ -31,12 +29,6 @@ Block::Block( Model* mod,
gpts()
{
assert( mod );
- assert( pt_count > 0 );
-
- // copy the argument point data into the member vector
- this->pts.reserve( pt_count );
- for( size_t p=0; p<pt_count; p++ )
- this->pts.push_back( pts[p] );
canonicalize_winding(this->pts);
}
@@ -46,7 +38,6 @@ Block::Block( Model* mod,
int entity)
: mod( mod ),
mpts(),
- pt_count(0),
pts(),
color(),
inherit_color(true),
@@ -319,15 +310,18 @@ void Block::InvalidateModelPointCache()
void Block::GenerateCandidateCells()
{
candidate_cells->clear();
-
+
+ const unsigned int pt_count = pts.size();
+
if( mpts.size() == 0 )
- {
+ {
// no valid cache of model coord points, so generate them
- mpts.resize( pt_count );
-
- for( unsigned int i=0; i<pt_count; i++ )
- mpts[i] = BlockPointToModelMeters( pts[i] );
- }
+ mpts.resize( pts.size() );
+
+
+ for( unsigned int i=0; i<pt_count; i++ )
+ mpts[i] = BlockPointToModelMeters( pts[i] );
+ }
gpts.clear();
mod->LocalToPixels( mpts, gpts );
@@ -336,7 +330,7 @@ void Block::GenerateCandidateCells()
mod->world->ForEachCellInLine( gpts[i],
gpts[(i+1)%pt_count],
*candidate_cells );
-
+
// set global Z
Pose gpose = mod->GetGlobalPose();
gpose.z += mod->geom.pose.z;
@@ -365,7 +359,8 @@ void Block::Rasterize( uint8_t* data,
{
//printf( "rasterize block %p : w: %u h: %u scale %.2f %.2f offset %.2f %.2f\n",
// this, width, height, scalex, scaley, offsetx, offsety );
-
+
+ const unsigned int pt_count = pts.size();
for( unsigned int i=0; i<pt_count; i++ )
{
// convert points from local to model coords
@@ -490,16 +485,15 @@ void Block::DrawSolid()
void Block::Load( Worldfile* wf, int entity )
{
- pt_count = wf->ReadInt( entity, "points", 0);
- pts.resize( pt_count );
-
+ const unsigned int pt_count = wf->ReadInt( entity, "points", 0);
+
char key[128];
for( unsigned int p=0; p<pt_count; p++ )
{
snprintf(key, sizeof(key), "point[%d]", p );
- pts[p].x = wf->ReadTupleLength(entity, key, 0, 0);
- pts[p].y = wf->ReadTupleLength(entity, key, 1, 0);
+ pts.push_back( point_t( wf->ReadTupleLength(entity, key, 0, 0),
+ wf->ReadTupleLength(entity, key, 1, 0) ));
}
local_z.min = wf->ReadTupleLength( entity, "z", 0, 0.0 );
View
@@ -255,69 +255,63 @@ void BlockGroup::LoadBlock( Model* mod, Worldfile* wf, int entity )
void BlockGroup::LoadBitmap( Model* mod, const std::string& bitmapfile, Worldfile* wf )
{
PRINT_DEBUG1( "attempting to load bitmap \"%s\n", bitmapfile );
-
- char full[_POSIX_PATH_MAX];
+
+ std::string full;
if( bitmapfile[0] == '/' )
- strncpy( full, bitmapfile.c_str(), _POSIX_PATH_MAX );
+ full = bitmapfile;
else
- {
- char *tmp = strdup(wf->filename.c_str());
- snprintf( full, _POSIX_PATH_MAX,
- "%s/%s", dirname(tmp), bitmapfile.c_str() );
- free(tmp);
- }
+ {
+ char* workaround_const = strdup(wf->filename.c_str());
+ full = std::string(dirname(workaround_const)) + "/" + bitmapfile;
+ free( workaround_const );
+ }
PRINT_DEBUG1( "attempting to load image %s", full );
-
- rotrect_t* rects = NULL;
- unsigned int rect_count = 0;
+
+ std::vector<rotrect_t> rects;
unsigned int width, height;
if( rotrects_from_image_file( full,
- &rects,
- &rect_count,
- &width, &height ) )
+ rects,
+ width,
+ height ) )
{
PRINT_ERR1( "failed to load rects from image file \"%s\"",
- full );
+ full.c_str() );
return;
}
//printf( "found %d rects in \"%s\" at %p\n",
// rect_count, full, rects );
- if( rects && (rect_count > 0) )
- {
- // TODO fix this
- Color col( 1.0, 0.0, 1.0, 1.0 );
-
- for( unsigned int r=0; r<rect_count; r++ )
+ // TODO fix this
+ Color col( 1.0, 0.0, 1.0, 1.0 );
+
+ FOR_EACH( rect, rects )
{
- point_t pts[4];
-
- double x = rects[r].pose.x;
- double y = rects[r].pose.y;
- double w = rects[r].size.x;
- double h = rects[r].size.y;
-
- pts[0].x = x;
- pts[0].y = y;
- pts[1].x = x + w;
- pts[1].y = y;
- pts[2].x = x + w;
- pts[2].y = y + h;
- pts[3].x = x;
- pts[3].y = y + h;
-
- AppendBlock( new Block( mod,
- pts,4,
+ std::vector<point_t> pts(4);
+
+ double x = rect->pose.x;
+ double y = rect->pose.y;
+ double w = rect->size.x;
+ double h = rect->size.y;
+
+ pts[0].x = x;
+ pts[0].y = y;
+ pts[1].x = x + w;
+ pts[1].y = y;
+ pts[2].x = x + w;
+ pts[2].y = y + h;
+ pts[3].x = x;
+ pts[3].y = y + h;
+
+ AppendBlock( new Block( mod,
+ pts,
0,1,
col,
true,
false) );
}
- free( rects );
- }
CalcSize();
}
View
@@ -14,14 +14,14 @@ const char* USAGE =
"USAGE: stage [options] <worldfile1> [worldfile2 ... worldfileN]\n"
"Available [options] are:\n"
" --clock : print simulation time peridically on standard output\n"
- " -c : short for --clock\n"
+ " -c : equivalent to --clock\n"
" --gui : run without a GUI\n"
- " -g : short for --gui\n"
+ " -g : equivalent to --gui\n"
" --help : print this message\n"
" --args \"str\" : define an argument string to be passed to all controllers\n"
- " -a \"str\" : short for --args\n"
- " -h : short for --help\n"
- " -? : short for --help";
+ " -a \"str\" : equivalent to --args \"str\"\n"
+ " -h : equivalent to --help\n"
+ " -? : equivalent to --help";
/* options descriptor */
static struct option longopts[] = {
Oops, something went wrong.

0 comments on commit e8d54f8

Please sign in to comment.