Hello altogether,
first of all, I am kind of new to game development, so please don’t beat me for making beginners mistakes [Wink]
Also english is not my mother-tongue. If something I write is unclear to you, please feel free to ask and I’ll try to explain what I mean
I have a tiled background where I add some objects (trees). (Here is a Screenshot: http://imgur.com/RaxKOrX.)
I can move the scene by pressing W, A, S or D.
When everything is standing still, the “Game” runs at 120 frames per second both with and without objects visible.
When I move the scene without any objects visible in it (only background), the frame rate drops to 60.
When showing some objects (which are moved with the scene, too) the frame rate drops down to 40 and I guess it would drop even further when I add more objects.
To show the objects, I use the two methods listed below.
Code:
void Map::BuildMap()
{
// Build background tiles
SDL_Rect rSourceTile;
SDL_Rect rTargetTile;
// Initialize SDL_Rects
rSourceTile.x = 0;
rSourceTile.y = 0;
rSourceTile.w = 0;
rSourceTile.h = 0;
rTargetTile.x = 0;
rTargetTile.y = 0;
rTargetTile.w = 0;
rTargetTile.h = 0;
int init_y = (int)(fOffsetY * -1) % iBackgroundTileSize;
int init_x = (int)(fOffsetX * -1) % iBackgroundTileSize;
int iSpaceToBorderRight = 0;
int iSpaceToBorderBottom = 0;
float fTmp = 0.f;
for (int y = init_y; y < iHeight; y += iBackgroundTileSize)
{
for (int x = init_x; x < iWidth; x += iBackgroundTileSize)
{
rTargetTile.x = x;
rTargetTile.y = y;
iSpaceToBorderRight = iWidth - x;
iSpaceToBorderBottom = iHeight - y;
// Correct X and Width of the tiles (especially the bordertiles)
if (x < 0)
{
fTmp = (x * -1) / ((float)iBackgroundTileSize / 100); // how much percent are the negative pixels on iBackgroundTileSize?
fTmp = fTmp * ((float)MSurfBackgroundTile->w / 100); // how much pixels are that much percent?
rSourceTile.x = (int)fTmp;
rSourceTile.w = MSurfBackgroundTile->w - (int)fTmp;
rTargetTile.x = 0;
rTargetTile.w = iBackgroundTileSize - (x * -1);
}
else if (iSpaceToBorderRight < iBackgroundTileSize)
{
fTmp = iSpaceToBorderRight / ((float)iBackgroundTileSize / 100); // how much percent are iSpaceToBorderRight pixels on iBackgroundTileSize?
fTmp = fTmp * ((float)MSurfBackgroundTile->w / 100); // how much pixels are that much percent?
rSourceTile.x = 0;
rSourceTile.w = (int)fTmp;
rTargetTile.w = iSpaceToBorderRight;
}
else
{
rSourceTile.x = 0;
rSourceTile.w = MSurfBackgroundTile->w;
rTargetTile.w = iBackgroundTileSize;
}
// Correct Y and Height of the tiles (especially the bordertiles)
if (y < 0)
{
fTmp = (y * -1) / ((float)iBackgroundTileSize / 100); // how much percent are the negative pixels on iBackgroundTileSize?
fTmp = fTmp * ((float)MSurfBackgroundTile->h / 100); // how much pixels are that much percent?
rSourceTile.y = (int)fTmp;
rSourceTile.h = MSurfBackgroundTile->h - (int)fTmp;
rTargetTile.y = 0;
rTargetTile.h = iBackgroundTileSize - (y * -1);
}
else if (iSpaceToBorderBottom < iBackgroundTileSize)
{
fTmp = iSpaceToBorderBottom / ((float)iBackgroundTileSize / 100); // how much percent are iSpaceToBorderBottom pixels on iBackgroundTileSize?
fTmp = fTmp * ((float)MSurfBackgroundTile->h / 100); // how much pixels are that much percent?
rSourceTile.y = 0;
rSourceTile.h = (int)fTmp;
rTargetTile.h = iSpaceToBorderBottom;
}
else
{
rSourceTile.y = 0;
rSourceTile.h = MSurfBackgroundTile->h;
rTargetTile.h = iBackgroundTileSize;
}
SDL_BlitScaled(MSurfBackgroundTile, &rSourceTile, MSurface, &rTargetTile);
}
}
}
void Map::BuildMapObjects()
{
int iObjectCount = vObjects.size();
MObj* pMObj;
SDL_Rect rDest;
int iMapPosX = (int)fOffsetX;
int iMapPosY = (int)fOffsetY;
for (int i = 0; i < iObjectCount; i++)
{
pMObj = vObjects.at(i);
MOSurface = pMObj->obj->GetObjectSurface();
// Only draw if the object is inside the visible area of the map
// TODO: Fix bug (disappearing object at top right border)
if ((pMObj->x >= iMapPosX && pMObj->x <= iMapPosX + iWidth &&
pMObj->y >= iMapPosY && pMObj->y <= iMapPosY + iHeight) ||
(pMObj->x + MOSurface->w >= iMapPosX && pMObj->x + MOSurface->w <= iMapPosX + iWidth &&
pMObj->y + MOSurface->h >= iMapPosY && pMObj->y + MOSurface->h <= iMapPosY + iHeight))
{
rDest.x = pMObj->x - iMapPosX;
rDest.y = pMObj->y - iMapPosY;
rDest.w = MOSurface->w;
rDest.h = MOSurface->h;
SDL_BlitSurface(MOSurface, NULL, MSurface, &rDest);
}
}
}
It would be really nice, if you could help me stabilizing the frame rate and/or optimizing my code.
Best regards,
Daniel