Skip to content

Commit 41d193c

Browse files
committed
fixed evil bug that may have been causing weird behaviour or crashes for some people
1 parent 55b9c56 commit 41d193c

File tree

7 files changed

+133
-121
lines changed

7 files changed

+133
-121
lines changed

libstage/block.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ void Block::MapLine( const point_int_t& start,
458458

459459
while( n )
460460
{
461-
Region* reg( w->GetSuperRegion( GETSREG(globx), GETSREG(globy) )
461+
Region* reg( w->GetSuperRegionCreate( point_int_t(GETSREG(globx), GETSREG(globy)))
462462
->GetRegion( GETREG(globx), GETREG(globy) ));
463463

464464
//printf( "REGION %p\n", reg );

libstage/region.cc

Lines changed: 74 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ SuperRegion::SuperRegion( World* world, point_int_t origin )
5858
world(world),
5959
count(0)
6060
{
61-
for( int32_t r=0; r<SUPERREGIONSIZE;r++)
62-
regions[r].superregion = this;
61+
for( int32_t c=0; c<SUPERREGIONSIZE;++c)
62+
regions[c].superregion = this;
6363
}
6464

6565
SuperRegion::~SuperRegion()
@@ -82,6 +82,8 @@ void SuperRegion::RemoveBlock()
8282

8383
void SuperRegion::DrawOccupancy() const
8484
{
85+
//printf( "SR origin (%d,%d) this %p\n", origin.x, origin.y, this );
86+
8587
glPushMatrix();
8688
GLfloat scale = 1.0/world->Resolution();
8789
glScalef( scale, scale, 1.0 ); // XX TODO - this seems slightly
@@ -95,70 +97,80 @@ void SuperRegion::DrawOccupancy() const
9597
glRecti( 0,0, 1<<SRBITS, 1<<SRBITS );
9698

9799
// outline regions
98-
const Region* r = &regions[0];
99-
char buf[32];
100-
101-
glColor3f( 0,1,0 );
102-
for( int y=0; y<SUPERREGIONWIDTH; ++y )
103-
for( int x=0; x<SUPERREGIONWIDTH; ++x )
104-
{
105-
if( r->count ) // region contains some occupied cells
106-
{
107-
// outline the region
108-
glRecti( x<<RBITS, y<<RBITS,
109-
(x+1)<<RBITS, (y+1)<<RBITS );
110-
111-
// show how many cells are occupied
112-
snprintf( buf, 15, "%lu", r->count );
113-
Gl::draw_string( x<<RBITS, y<<RBITS, 0, buf );
114-
115-
// draw a rectangle around each occupied cell
116-
for( int p=0; p<REGIONWIDTH; ++p )
117-
for( int q=0; q<REGIONWIDTH; ++q )
118-
if( r->cells[p+(q*REGIONWIDTH)].blocks.size() )
119-
{
120-
GLfloat xx = p+(x<<RBITS);
121-
GLfloat yy = q+(y<<RBITS);
122-
glRecti( xx, yy, xx+1, yy+1);
123-
}
124-
}
125-
else if( r->cells ) // empty but used previously
126-
{
127-
double left = x << RBITS;
128-
double right = (x+1) << RBITS;
129-
double bottom = y << RBITS;
130-
double top = (y+1) << RBITS;
131-
132-
double d = 3.0;
133-
134-
// draw little corner markers for regions with memory
135-
// allocated but no contents
136-
glBegin( GL_LINES );
137-
glVertex2f( left, bottom );
138-
glVertex2f( left+d, bottom );
139-
glVertex2f( left, bottom );
140-
glVertex2f( left, bottom+d );
141-
glVertex2f( left, top );
142-
glVertex2f( left+d, top );
143-
glVertex2f( left, top );
144-
glVertex2f( left, top-d );
145-
glVertex2f( right, top );
146-
glVertex2f( right-d, top );
147-
glVertex2f( right, top );
148-
glVertex2f( right, top-d );
149-
glVertex2f( right, bottom );
150-
glVertex2f( right-d, bottom );
151-
glVertex2f( right, bottom );
152-
glVertex2f( right, bottom+d );
153-
glEnd();
154-
}
100+
if( regions )
101+
{
102+
const Region* r = &regions[0];
103+
char buf[32];
155104

156-
++r; // next region quickly
105+
glColor3f( 0,1,0 );
106+
for( int y=0; y<SUPERREGIONWIDTH; ++y )
107+
for( int x=0; x<SUPERREGIONWIDTH; ++x )
108+
{
109+
if( r->count ) // region contains some occupied cells
110+
{
111+
// outline the region
112+
glRecti( x<<RBITS, y<<RBITS,
113+
(x+1)<<RBITS, (y+1)<<RBITS );
114+
115+
// show how many cells are occupied
116+
snprintf( buf, 15, "%lu", r->count );
117+
Gl::draw_string( x<<RBITS, y<<RBITS, 0, buf );
118+
119+
// draw a rectangle around each occupied cell
120+
for( int p=0; p<REGIONWIDTH; ++p )
121+
for( int q=0; q<REGIONWIDTH; ++q )
122+
if( r->cells[p+(q*REGIONWIDTH)].blocks.size() )
123+
{
124+
GLfloat xx = p+(x<<RBITS);
125+
GLfloat yy = q+(y<<RBITS);
126+
glRecti( xx, yy, xx+1, yy+1);
127+
}
128+
}
129+
else if( r->cells ) // empty but used previously
130+
{
131+
double left = x << RBITS;
132+
double right = (x+1) << RBITS;
133+
double bottom = y << RBITS;
134+
double top = (y+1) << RBITS;
135+
136+
double d = 3.0;
137+
138+
// draw little corner markers for regions with memory
139+
// allocated but no contents
140+
glBegin( GL_LINES );
141+
glVertex2f( left, bottom );
142+
glVertex2f( left+d, bottom );
143+
glVertex2f( left, bottom );
144+
glVertex2f( left, bottom+d );
145+
glVertex2f( left, top );
146+
glVertex2f( left+d, top );
147+
glVertex2f( left, top );
148+
glVertex2f( left, top-d );
149+
glVertex2f( right, top );
150+
glVertex2f( right-d, top );
151+
glVertex2f( right, top );
152+
glVertex2f( right, top-d );
153+
glVertex2f( right, bottom );
154+
glVertex2f( right-d, bottom );
155+
glVertex2f( right, bottom );
156+
glVertex2f( right, bottom+d );
157+
glEnd();
158+
}
159+
160+
++r; // next region quickly
161+
}
157162
}
158-
163+
else
164+
{ // outline region-collected superregion
165+
glColor3f( 1,1,0 );
166+
glRecti( 0,0, (1<<SRBITS)-1, (1<<SRBITS)-1 );
167+
glColor3f( 0,0,1 );
168+
}
169+
170+
char buf[32];
159171
snprintf( buf, 15, "%lu", count );
160172
Gl::draw_string( 1<<SBITS, 1<<SBITS, 0, buf );
161-
173+
162174
glPopMatrix();
163175
}
164176

libstage/region.hh

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ namespace Stg
1111
{
1212

1313
// a bit of experimenting suggests that these values are fast. YMMV.
14-
//const int32_t RBITS( 5 ); // regions contain (2^RBITS)^2 pixels
15-
//const int32_t SBITS( 5 );// superregions contain (2^SBITS)^2 regions
1614
const int32_t RBITS( 5 ); // regions contain (2^RBITS)^2 pixels
1715
const int32_t SBITS( 5 );// superregions contain (2^SBITS)^2 regions
1816
const int32_t SRBITS( RBITS+SBITS );
@@ -32,16 +30,12 @@ namespace Stg
3230

3331
// this is slightly faster than the inline method above, but not as safe
3432
//#define GETREG(X) (( (static_cast<int32_t>(X)) & REGIONMASK ) >> RBITS)
35-
36-
// class Region;
37-
33+
3834
class Cell
3935
{
40-
//friend class Region;
4136
friend class SuperRegion;
4237
friend class World;
43-
//friend class Block;
44-
38+
4539
private:
4640
std::vector<Block*> blocks;
4741

@@ -66,7 +60,6 @@ namespace Stg
6660

6761
private:
6862
Cell* cells;
69-
7063
unsigned long count; // number of blocks rendered into this region
7164

7265
// vector of garbage collected cell arrays to reallocate before
@@ -105,24 +98,18 @@ namespace Stg
10598

10699
SuperRegion* superregion;
107100

108-
//bool Occupied() const { return( count > 0 ); }
109-
//bool Used() const { return( cells ? true : false ); }
110-
111101
}; // class Region
112102

113103
class SuperRegion
114104
{
115-
// friend class World;
116-
// friend class Model;
117-
118105
private:
119106
Region regions[SUPERREGIONSIZE];
120107

121108
point_int_t origin;
122109
World* world;
123110

124111
unsigned long count; // number of blocks rendered into this superregion
125-
112+
126113
public:
127114
SuperRegion( World* world, point_int_t origin );
128115
~SuperRegion();
@@ -131,8 +118,6 @@ namespace Stg
131118
{
132119
return( &regions[ x + y * SUPERREGIONWIDTH ]);
133120
}
134-
135-
//inline const Region* GetRegionImmut( int32_t x, int32_t y ) const;
136121

137122
void DrawOccupancy() const;
138123
void DrawVoxels() const;

libstage/stage.hh

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -495,16 +495,16 @@ namespace Stg
495495
point_int_t( int x, int y ) : x(x), y(y){}
496496
point_int_t() : x(0), y(0){}
497497

498-
/** required to put these in sorted containers like std::map */
499-
bool operator<( const point_int_t& other ) const
500-
{
501-
if( x < other.x ) return true;
502-
if( other.x < x ) return false;
503-
return y < other.y;
504-
}
505-
506-
bool operator==( const point_int_t& other ) const
507-
{ return ((x == other.x) && (y == other.y) ); }
498+
/** required to put these in sorted containers like std::map */
499+
bool operator<( const point_int_t& other ) const
500+
{
501+
if( x < other.x ) return true;
502+
if( other.x < x ) return false;
503+
return y < other.y;
504+
}
505+
506+
bool operator==( const point_int_t& other ) const
507+
{ return ((x == other.x) && (y == other.y) ); }
508508
};
509509

510510
typedef std::vector<point_int_t> PointIntVec;
@@ -956,14 +956,9 @@ namespace Stg
956956
virtual Model* RecentlySelectedModel() const { return NULL; }
957957

958958
SuperRegion* AddSuperRegion( const point_int_t& coord );
959-
SuperRegion* GetSuperRegion( int32_t x, int32_t y );
960-
void ExpireSuperRegion( SuperRegion* sr );
961-
962-
/** add a Cell pointer to the vector for each cell on the line from
963-
pt1 to pt2 inclusive */
964-
// void MapLine( const point_int_t& pt1,
965-
// const point_int_t& pt2,
966-
// Block* block );
959+
SuperRegion* GetSuperRegion( const point_int_t& org );
960+
SuperRegion* GetSuperRegionCreate( const point_int_t& org );
961+
//void ExpireSuperRegion( SuperRegion* sr );
967962

968963
/** convert a distance in meters to a distance in world occupancy
969964
grid pixels */

libstage/texture_manager.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ Fl_Shared_Image* TextureManager::loadImage( const char* filename )
2323

2424
GLuint TextureManager::loadTexture( const char *filename )
2525
{
26-
printf( "attemopt to load texture %s\n", filename );
27-
2826
GLuint texName;
2927
Fl_Shared_Image *img = loadImage( filename );
3028
if( img == NULL ) {

libstage/world.cc

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ World::~World( void )
196196
SuperRegion* World::CreateSuperRegion( point_int_t origin )
197197
{
198198
SuperRegion* sr = new SuperRegion( this, origin );
199-
superregions[ origin ] = sr;
199+
superregions[origin] = sr;
200200
dirty = true; // force redraw
201201
return sr;
202202
}
203203

204204
void World::DestroySuperRegion( SuperRegion* sr )
205205
{
206-
superregions.erase( sr->GetOrigin() );
206+
superregions.erase( sr->GetOrigin() );
207207
delete sr;
208208
}
209209

@@ -788,11 +788,15 @@ RaytraceResult World::Raytrace( const Ray& r )
788788
// inline calls have a noticeable (2-3%) effect on performance.
789789
while( n > 0 ) // while we are still not at the ray end
790790
{
791-
Region* reg( GetSuperRegion( GETSREG(globx), GETSREG(globy) )
792-
->GetRegion( GETREG(globx), GETREG(globy) ));
791+
SuperRegion* sr = GetSuperRegion( point_int_t(GETSREG(globx), GETSREG(globy)) );
792+
//SuperRegion* sr = GetSuperRegion( point_int_t(GETSREG(globx), GETSREG(globy)) );
793793

794-
795-
if( reg->count ) // if the region contains any objects
794+
Region* reg = NULL;
795+
796+
if( sr )
797+
reg = sr->GetRegion( GETREG(globx), GETREG(globy) );
798+
799+
if( reg && reg->count ) // if the region contains any objects
796800
{
797801
//assert( reg->cells.size() );
798802

@@ -973,9 +977,10 @@ void World::Reload( void )
973977

974978
SuperRegion* World::AddSuperRegion( const point_int_t& sup )
975979
{
976-
//printf( "Creating super region [ %d %d ]\n", sup.x, sup.y );
977980
SuperRegion* sr = CreateSuperRegion( sup );
978981

982+
//printf( "Created super region [%d, %d] %p\n", sup.x, sup.y, sr );
983+
979984
// the bounds of the world have changed
980985
//printf( "lower left (%.2f,%.2f,%.2f)\n", pt.x, pt.y, pt.z );
981986

@@ -998,31 +1003,43 @@ SuperRegion* World::AddSuperRegion( const point_int_t& sup )
9981003
}
9991004

10001005

1001-
inline SuperRegion* World::GetSuperRegion( const int32_t x, const int32_t y )
1006+
inline SuperRegion* World::GetSuperRegion( const point_int_t& org )
10021007
{
10031008
// around 99% of the time the SR is the same as last
10041009
// lookup - cache gives a 4% overall speed up :)
1005-
1006-
point_int_t org(x,y);
10071010

10081011
if( sr_cached && sr_cached->GetOrigin() == org )
10091012
return sr_cached;
10101013

1011-
// point_int_t pt(x,y);
1014+
SuperRegion* sr = NULL;
1015+
1016+
// I despise some STL syntax sometimes...
1017+
std::map<point_int_t,SuperRegion*>::iterator it =
1018+
superregions.find(org);
1019+
1020+
if( it != superregions.end() )
1021+
sr = it->second;
10121022

1013-
SuperRegion* sr = superregions[ org ];
1023+
if( sr ) sr_cached = sr;
10141024

1015-
if( sr == NULL ) // no superregion exists! make a new one
1016-
sr = AddSuperRegion( org );
1025+
return sr;
1026+
}
10171027

1018-
assert( sr );
1028+
SuperRegion* World::GetSuperRegionCreate( const point_int_t& org )
1029+
{
1030+
SuperRegion* sr = GetSuperRegion(org);
1031+
1032+
if( sr == NULL ) // no superregion exists! make a new one
1033+
{
1034+
sr = AddSuperRegion( org );
1035+
assert( sr );
1036+
//sr_cached = sr;
1037+
}
10191038

1020-
// cache for next time around
1021-
sr_cached = sr;
1022-
10231039
return sr;
10241040
}
10251041

1042+
10261043
void World::Extend( point3_t pt )
10271044
{
10281045
extent.x.min = std::min( extent.x.min, pt.x );

0 commit comments

Comments
 (0)