mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2025-10-08 07:11:16 +00:00
Fix some small performance bottlenecks:
- Vector interpolation. Also removed unused interpolation trigger events. - Game::collideCircleWithGrid() - GridRender::onRender() (removed Andrew's hack, use fixed map file!) - make use of glVertex3i() & memchr() in GridRender
This commit is contained in:
parent
99e32e0ee7
commit
2d7eeb4781
12 changed files with 183 additions and 345 deletions
|
@ -9092,9 +9092,8 @@ void Avatar::onUpdate(float dt)
|
|||
|
||||
|
||||
int hw = collideCircle;
|
||||
Vector fix;
|
||||
|
||||
if (dsq->game->collideCircleWithGrid(position, hw, &fix))
|
||||
if (dsq->game->collideCircleWithGrid(position, hw))
|
||||
{
|
||||
if (dsq->game->lastCollideTileType == OT_HURT
|
||||
&& dsq->continuity.getWorldType() != WT_SPIRIT
|
||||
|
|
|
@ -206,7 +206,6 @@ void CollideEntity::updateMovement(float dt)
|
|||
|
||||
const int hw = collideRadius;
|
||||
bool freeRange = false;
|
||||
Vector fix;
|
||||
|
||||
if (isv(EV_COLLIDELEVEL,1))
|
||||
{
|
||||
|
@ -216,7 +215,7 @@ void CollideEntity::updateMovement(float dt)
|
|||
bool doesFreeRange = !isPullable();
|
||||
if (doesFreeRange)
|
||||
{
|
||||
if (dsq->game->collideCircleWithGrid(position, hw, &fix))
|
||||
if (dsq->game->collideCircleWithGrid(position, hw))
|
||||
{
|
||||
// starting in a collision state
|
||||
freeRange = true;
|
||||
|
@ -232,7 +231,7 @@ void CollideEntity::updateMovement(float dt)
|
|||
{
|
||||
if (getState() == STATE_PUSH)
|
||||
{
|
||||
if (!freeRange && dsq->game->collideCircleWithGrid(position, hw, &fix))
|
||||
if (!freeRange && dsq->game->collideCircleWithGrid(position, hw))
|
||||
{
|
||||
position = lastPosition;
|
||||
collided = true;
|
||||
|
@ -241,7 +240,7 @@ void CollideEntity::updateMovement(float dt)
|
|||
}
|
||||
else
|
||||
{
|
||||
if (!freeRange && ((!canLeaveWater && !isUnderWater() && wasUnderWater) || dsq->game->collideCircleWithGrid(position, hw, &fix)))
|
||||
if (!freeRange && ((!canLeaveWater && !isUnderWater() && wasUnderWater) || dsq->game->collideCircleWithGrid(position, hw)))
|
||||
{
|
||||
position = lastPosition;
|
||||
onHitWall();
|
||||
|
|
|
@ -11161,47 +11161,72 @@ Vector Game::getClosestPointOnLine(Vector a, Vector b, Vector p)
|
|||
return a + V;
|
||||
}
|
||||
|
||||
bool Game::collideCircleWithGrid(Vector position, int r, Vector *fill)
|
||||
bool Game::collideCircleWithGrid(const Vector& position, int r)
|
||||
{
|
||||
Vector tile = position;
|
||||
TileVector t(tile);
|
||||
tile.x = t.x;
|
||||
tile.y = t.y;
|
||||
TileVector t(position);
|
||||
|
||||
float hsz = TILE_SIZE/2;
|
||||
int xrange=1,yrange=1;
|
||||
xrange = (r/TILE_SIZE)+1;
|
||||
yrange = (r/TILE_SIZE)+1;
|
||||
const float hsz = TILE_SIZE/2;
|
||||
const int xrange = (r/TILE_SIZE)+1;
|
||||
const int yrange = (r/TILE_SIZE)+1;
|
||||
|
||||
for (int x = tile.x-xrange; x <= tile.x+xrange; x++)
|
||||
// quick early check if out of bounds
|
||||
const int xstart = t.x-xrange;
|
||||
const int ystart = t.y-yrange;
|
||||
if (xstart < 0 || ystart < 0)
|
||||
{
|
||||
for (int y = tile.y-yrange; y <= tile.y+yrange; y++)
|
||||
lastCollideTileType = (ObsType)1;
|
||||
lastCollidePosition = TileVector::worldVector(xstart, ystart);
|
||||
return true;
|
||||
}
|
||||
|
||||
const int r2 = sqr(r);
|
||||
const int xmax = t.x+xrange;
|
||||
const int ymax = t.y+yrange;
|
||||
|
||||
for (int x = xstart; x <= xmax; x++)
|
||||
{
|
||||
if (x >= MAX_GRID)
|
||||
{
|
||||
int v = this->getGrid(TileVector(x, y));
|
||||
lastCollideTileType = (ObsType)1;
|
||||
lastCollidePosition = TileVector::worldVector(x, ystart);
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int y = ystart; y <= ymax; y++)
|
||||
{
|
||||
if (y >= MAX_GRID)
|
||||
{
|
||||
lastCollideTileType = (ObsType)1;
|
||||
lastCollidePosition = TileVector::worldVector(x, y);
|
||||
return true;
|
||||
}
|
||||
|
||||
int v = getGridRaw(x, y); // known to be in bounds
|
||||
if (v != 0)
|
||||
{
|
||||
//if (tile.x == x && tile.y == y) return true;
|
||||
TileVector t(x, y);
|
||||
lastCollidePosition = t.worldVector();
|
||||
//if (tile.x == x && tile.y == y) return true;
|
||||
lastCollidePosition = TileVector::worldVector(x, y);
|
||||
float rx = (x*TILE_SIZE)+TILE_SIZE/2;
|
||||
float ry = (y*TILE_SIZE)+TILE_SIZE/2;
|
||||
|
||||
float rSqr;
|
||||
lastCollideTileType = (ObsType)v;
|
||||
|
||||
rSqr = sqr(position.x - (rx+hsz)) + sqr(position.y - (ry+hsz));
|
||||
if (rSqr < sqr(r)) return true;
|
||||
float yp = sqr(position.y - (ry+hsz));
|
||||
float xp = sqr(position.x - (rx+hsz));
|
||||
|
||||
rSqr = sqr(position.x - (rx-hsz)) + sqr(position.y - (ry+hsz));
|
||||
if (rSqr < sqr(r)) return true;
|
||||
if (xp + yp < r2)
|
||||
return true;
|
||||
|
||||
rSqr = sqr(position.x - (rx-hsz)) + sqr(position.y - (ry-hsz));
|
||||
if (rSqr < sqr(r)) return true;
|
||||
float xm = sqr(position.x - (rx-hsz));
|
||||
|
||||
rSqr = sqr(position.x - (rx+hsz)) + sqr(position.y - (ry-hsz));
|
||||
if (rSqr < sqr(r)) return true;
|
||||
if (xm + yp < r2)
|
||||
return true;
|
||||
|
||||
float ym = sqr(position.y - (ry-hsz));
|
||||
if (xm + ym < r2)
|
||||
return true;
|
||||
|
||||
if (xp < ym)
|
||||
return true;
|
||||
|
||||
if (position.x > rx-hsz && position.x < rx+hsz)
|
||||
{
|
||||
|
@ -11211,7 +11236,6 @@ bool Game::collideCircleWithGrid(Vector position, int r, Vector *fill)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
if (position.y > ry-hsz && position.y < ry+hsz)
|
||||
{
|
||||
if (fabsf(rx - position.x) < r+hsz)
|
||||
|
@ -11226,7 +11250,7 @@ bool Game::collideCircleWithGrid(Vector position, int r, Vector *fill)
|
|||
return false;
|
||||
}
|
||||
|
||||
bool Game::collideBoxWithGrid(Vector position, int hw, int hh)
|
||||
bool Game::collideBoxWithGrid(const Vector& position, int hw, int hh)
|
||||
{
|
||||
Vector tile = position;
|
||||
TileVector t(tile);
|
||||
|
|
|
@ -636,7 +636,8 @@ public:
|
|||
|
||||
std::string getSelectedChoice() { return selectedChoice; }
|
||||
|
||||
int getGrid(const TileVector &tile);
|
||||
int getGrid(const TileVector &tile) const;
|
||||
int getGridRaw(unsigned int x, unsigned int y) const;
|
||||
const signed char *getGridColumn(int tileX);
|
||||
void setGrid(const TileVector &tile, int v);
|
||||
bool isObstructed(const TileVector &tile, int t = -1);
|
||||
|
@ -669,8 +670,8 @@ public:
|
|||
|
||||
void registerSporeDrop(const Vector &pos, int t);
|
||||
|
||||
bool collideBoxWithGrid(Vector position, int w, int h);
|
||||
bool collideCircleWithGrid(Vector position, int r, Vector *fill=0);
|
||||
bool collideBoxWithGrid(const Vector& position, int w, int h);
|
||||
bool collideCircleWithGrid(const Vector& position, int r);
|
||||
|
||||
bool collideHairVsCircle(Entity *a, int num, const Vector &pos2, int radius, float perc=0);
|
||||
|
||||
|
@ -1214,7 +1215,13 @@ extern Game *game;
|
|||
// INLINE FUNCTIONS
|
||||
|
||||
inline
|
||||
int Game::getGrid(const TileVector &tile)
|
||||
int Game::getGridRaw(unsigned int x, unsigned int y) const
|
||||
{
|
||||
return grid[x][y];
|
||||
}
|
||||
|
||||
inline
|
||||
int Game::getGrid(const TileVector &tile) const
|
||||
{
|
||||
if (tile.x < 0 || tile.x >= MAX_GRID || tile.y < 0 || tile.y >= MAX_GRID) return 1;
|
||||
return grid[tile.x][tile.y];
|
||||
|
|
|
@ -39,6 +39,31 @@ void GridRender::onUpdate(float dt)
|
|||
if (obsType != OT_BLACK) { blendEnabled = true; }
|
||||
}
|
||||
|
||||
inline static void doRenderGrid(int x, int startCol, int endCol)
|
||||
{
|
||||
const int drawx1 = x*TILE_SIZE;
|
||||
const int drawx2 = (x+1)*TILE_SIZE;
|
||||
const int drawy1 = startCol*TILE_SIZE;
|
||||
const int drawy2 = (endCol+1)*TILE_SIZE;
|
||||
|
||||
#ifdef BBGE_BUILD_OPENGL
|
||||
glBegin(GL_QUADS);
|
||||
glVertex3i(drawx1, drawy2, 0.0f);
|
||||
glVertex3i(drawx2, drawy2, 0.0f);
|
||||
glVertex3i(drawx2, drawy1, 0.0f);
|
||||
glVertex3i(drawx1, drawy1, 0.0f);
|
||||
glEnd();
|
||||
#endif
|
||||
|
||||
#ifdef BBGE_BUILD_DIRECTX
|
||||
core->blitD3DVerts(0,
|
||||
drawx1, drawy1,
|
||||
drawx2, drawy1,
|
||||
drawx2, drawy2,
|
||||
drawx1, drawy2);
|
||||
#endif
|
||||
}
|
||||
|
||||
void GridRender::onRender()
|
||||
{
|
||||
switch(obsType)
|
||||
|
@ -59,7 +84,7 @@ void GridRender::onRender()
|
|||
break;
|
||||
}
|
||||
|
||||
const int obsType = int(this->obsType);
|
||||
int obsType = this->obsType;
|
||||
Vector camPos = core->cameraPos;
|
||||
camPos.x -= core->getVirtualOffX() * (core->invGlobalScale);
|
||||
const TileVector ct(camPos);
|
||||
|
@ -77,57 +102,43 @@ void GridRender::onRender()
|
|||
startY = 0;
|
||||
if (endY >= MAX_GRID)
|
||||
endY = MAX_GRID-1;
|
||||
for (int x = startX; x <= endX; x++)
|
||||
for (int x = startX; x <= endX; ++x)
|
||||
{
|
||||
const signed char *gridColumn = dsq->game->getGridColumn(x);
|
||||
int startCol = -1, endCol;
|
||||
for (int y = startY; y <= endY; y++)
|
||||
int startCol = -1, y;
|
||||
|
||||
// fast-forward to next drawable byte
|
||||
if(const signed char *next = (const signed char*)memchr(gridColumn + startY, obsType, endY - startY + 1)) // find next byte with correct obs type
|
||||
{
|
||||
int v = gridColumn[y];
|
||||
// HACK: Don't draw the leftmost or rightmost column of
|
||||
// black tiles (otherwise they "leak out" around the
|
||||
// edges of the Sun Temple). --achurch
|
||||
if (v == OT_BLACK && ((dsq->game->getGridColumn(x-1))[y] != OT_BLACK || (dsq->game->getGridColumn(x+1))[y] != OT_BLACK))
|
||||
v = OT_EMPTY;
|
||||
y = next - gridColumn; // will get incremented right away, which is okay, because we alrady set startCol
|
||||
startCol = y;
|
||||
}
|
||||
else
|
||||
continue; // nothing do draw in this column
|
||||
|
||||
if (v == obsType && startCol == -1)
|
||||
for ( ; y < endY; ++y)
|
||||
{
|
||||
if (gridColumn[y] != obsType)
|
||||
{
|
||||
startCol = y;
|
||||
}
|
||||
else if ((v != obsType || y == endY) && startCol != -1)
|
||||
{
|
||||
endCol = y;
|
||||
if (v != obsType)
|
||||
endCol--;
|
||||
|
||||
const float drawx1 = x*TILE_SIZE;
|
||||
const float drawx2 = (x+1)*TILE_SIZE;
|
||||
const float drawy1 = startCol*TILE_SIZE;
|
||||
const float drawy2 = (endCol+1)*TILE_SIZE;
|
||||
|
||||
#ifdef BBGE_BUILD_OPENGL
|
||||
glBegin(GL_QUADS);
|
||||
glVertex3f(drawx1, drawy2, 0.0f);
|
||||
glVertex3f(drawx2, drawy2, 0.0f);
|
||||
glVertex3f(drawx2, drawy1, 0.0f);
|
||||
glVertex3f(drawx1, drawy1, 0.0f);
|
||||
glEnd();
|
||||
#endif
|
||||
|
||||
#ifdef BBGE_BUILD_DIRECTX
|
||||
core->blitD3DVerts(0,
|
||||
drawx1, drawy1,
|
||||
drawx2, drawy1,
|
||||
drawx2, drawy2,
|
||||
drawx1, drawy2);
|
||||
#endif
|
||||
startCol = -1;
|
||||
doRenderGrid(x, startCol, y - 1);
|
||||
|
||||
// fast-forward to next drawable byte
|
||||
if(const signed char *next = (const signed char*)memchr(gridColumn + y, obsType, endY - y)) // find next byte with correct obs type
|
||||
{
|
||||
y = next - gridColumn; // will get incremented right away, which is okay, because we alrady set startCol
|
||||
startCol = y;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (y == endY)
|
||||
{
|
||||
doRenderGrid(x, startCol, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SongLineRender::SongLineRender()
|
||||
{
|
||||
followCamera = 1;
|
||||
|
|
|
@ -46,11 +46,7 @@ void Segmented::destroySegments(float life)
|
|||
for (int i = 0; i < segments.size(); i++)
|
||||
{
|
||||
segments[i]->setLife(life);
|
||||
segments[i]->setDecayRate(1.0);
|
||||
|
||||
//segments[i]->setLife(1.0);
|
||||
//segments[i]->setDecayRate(1.0/life);
|
||||
//segments[i]->setDecayRate(1.0/life);
|
||||
segments[i]->setDecayRate(1.0f);
|
||||
segments[i]->fadeAlphaWithLife = true;
|
||||
}
|
||||
segments.clear();
|
||||
|
@ -84,7 +80,7 @@ void Segmented::updateSegment(int i, const Vector &diff)
|
|||
|
||||
float angle;
|
||||
MathFunctions::calculateAngleBetweenVectorsInDegrees(Vector(0,0,0), diff, angle);
|
||||
segments[i]->rotation.interpolateTo(Vector(0,0,angle), 0.2);
|
||||
segments[i]->rotation.interpolateTo(Vector(0,0,angle), 0.2f);
|
||||
}
|
||||
|
||||
void Segmented::updateAlpha(float a)
|
||||
|
@ -105,46 +101,25 @@ void Segmented::warpSegments(const Vector &position)
|
|||
|
||||
void Segmented::updateSegments(const Vector &position, bool reverse)
|
||||
{
|
||||
/*
|
||||
if (lastPositions.empty())
|
||||
{
|
||||
for (int i = 0; i < segments.size(); i++)
|
||||
{
|
||||
segments[i]->position = position;
|
||||
}
|
||||
lastPositions.resize(numSegments);
|
||||
for (int i = 0; i < numSegments; i++)
|
||||
{
|
||||
lastPositions.push_back(position);
|
||||
}
|
||||
}
|
||||
*/
|
||||
const int top = segments.size()-1;
|
||||
Vector lastPosition = position;
|
||||
const Vector *lastPosition = &position;
|
||||
if (!reverse)
|
||||
{
|
||||
for (int i = 0; i <= top; i++)
|
||||
{
|
||||
const Vector diff = lastPosition - segments[i]->position;
|
||||
const Vector diff = *lastPosition - segments[i]->position;
|
||||
updateSegment(i, diff);
|
||||
lastPosition = segments[i]->position;
|
||||
lastPosition = &segments[i]->position;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = top; i >= 0; i--)
|
||||
{
|
||||
const Vector diff = lastPosition - segments[i]->position;
|
||||
const Vector diff = *lastPosition - segments[i]->position;
|
||||
updateSegment(i, diff);
|
||||
lastPosition = segments[i]->position;
|
||||
lastPosition = &segments[i]->position;
|
||||
}
|
||||
}
|
||||
/*
|
||||
for (int i = lastPositions.size()-1; i > 0; i--)
|
||||
{
|
||||
lastPositions[i] = lastPositions[i-1];
|
||||
}
|
||||
lastPositions[0] = position;
|
||||
*/
|
||||
}
|
||||
|
||||
|
|
|
@ -37,14 +37,19 @@ public:
|
|||
|
||||
TileVector() : x(0),y(0) {}
|
||||
|
||||
Vector worldVector() const
|
||||
inline Vector worldVector() const
|
||||
{
|
||||
return worldVector(x, y);
|
||||
}
|
||||
|
||||
inline static Vector worldVector(int x, int y)
|
||||
{
|
||||
return Vector(x*TILE_SIZE+TILE_SIZE/2, y*TILE_SIZE+TILE_SIZE/2);
|
||||
}
|
||||
|
||||
bool isZero() const
|
||||
inline bool isZero() const
|
||||
{
|
||||
return (x==0 && y==0);
|
||||
return !(x | y);
|
||||
}
|
||||
|
||||
int x,y;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue