My map system has a scrolling function which takes a X and Y scroll value and then adds them onto the position of each and every tile, probably not the most optimized way to go about it, but it worked at a good 60 fps on a 96x96 map. However when I tried to load a 96x48 map into my engine, the scrolling still works, but the entire engine runs at about 10 fps.
All of my code which is involved in this cycle of my map system:
Code: Select all
//TileMap::scroll -- The function that does the actual scrolling
void TileMap::scroll(int x, int y)
{
for(int i = 0; i < tiles.size(); i++){
tiles[i]->position.x+=x;
tiles[i]->position.y+=y;
}
}
//TileMap::render -- the function that renders the map
void TileMap::render()
{
for(int i = 0; i < tiles.size(); i++)
{
renderSprite(tiles[i]->position, vector2f(scale, scale), tiles[i]->id, tiles[i]->clip, tiles[i]->dimensions);
//}
}
}
//renderSprite -- the function that renders the sprites/tiles
void renderSprite(GLfloat x, GLfloat y, GLfloat w, GLfloat h, GLuint id, GLuint clipX, GLuint clipY, GLuint clipW, GLuint clipH, GLuint sheetW, GLuint sheetH)
{
if(!glIsEnabled(GL_TEXTURE_2D))
glEnable(GL_TEXTURE_2D);
glColor3f(0, 0, 0);
glTranslatef(x, y, 0);
glBindTexture(GL_TEXTURE_2D, id);
glBegin(GL_QUADS);
glTexCoord2f(clipX/sheetW, clipY/sheetH);
glVertex2f(0, 0);
glTexCoord2f(clipX/sheetW, (clipY+clipH)/sheetH);
glVertex2f(0, h);
glTexCoord2f((clipX+clipW)/sheetW, (clipY+clipH)/sheetH);
glVertex2f(w, h);
glTexCoord2f((clipX+clipW)/sheetW, clipY/sheetH);
glVertex2f(w, 0);
glEnd();
glLoadIdentity();
}
//Tile structure
struct Tile
{
GLfloat id;
vector4f clip;
vector2f position;
vector2f dimensions;
Tile(vector2f _position, GLfloat _id, vector4f _clip, vector2f _dimensions)
{
position=_position;
id=_id;
clip=_clip;
dimensions=_dimensions;
}
};
//Code in the run function that relates to the map/rendering
TileMap* tilemap;
tilemap = new TileMap("maps/testmap3.bmp");
graphics->addTileMap(tilemap);
graphics->setActiveMap(0);
while(input->getInput())
{
currentTicks=SDL_GetTicks();
if(input->upKeyPressed())
{
tilemap->scroll(0, 8);
}
if(input->downKeyPressed())
{
tilemap->scroll(0, -8);
}
if(input->leftKeyPressed())
{
tilemap->scroll(8, 0);
}
if(input->rightKeyPressed())
{
tilemap->scroll(-8, 0);
}
graphics->render();
SDL_GL_SwapBuffers();
if(SDL_GetTicks()-currentTicks < 1000 / MAX_FPS)
{
SDL_Delay( ( 1000 / MAX_FPS ) + SDL_GetTicks()-currentTicks );
}
SDL_Delay(1);
}
Thanks in advance for any help/enlightenment anyone can give me.