Page 1 of 1

Slow Tile Scrolling

Posted: Mon Feb 20, 2012 9:12 pm
by BlobOfFailure
In the engine I am currently working on I've encountered a slight bug. Not anything desperately hindering development, but it is certainly causing a drastic drop in engine performance.

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);

        }
And yet again I would like to state, I did not change any of the code, just the dimensions of the map to a lower amount.

Thanks in advance for any help/enlightenment anyone can give me.

Re: Slow Tile Scrolling

Posted: Tue Feb 21, 2012 1:14 am
by Light-Dark
Thats your problem, your moving EVERYTHING at once its going to slow down, im assuming your using an api where you can designate a 'camera' if so just make a camera and have its x and y change instead of every entity i guarentee you will experince no low FPS or slow down.

EDIT: it appears your using SDL & openGL so a camera is 100% viable and you should look into that :P

Re: Slow Tile Scrolling

Posted: Tue Feb 21, 2012 7:30 am
by Falco Girgis
Holy Jesus fuck!

So every tile of yours has a position? It's a free-standing entity? Then you don't have a tile engine, homie.

A tile engine is a 2D array of indeces that lookup into tile tables. They don't need a position, because their position is always the 2D array index * tile size. What is the point of separately storing a position if your tiles are uniform?

No wonder it's slow as shit. You're doing 100x more work than you should have to to scroll.

Look into the Tiles: Entities or something else? Topic. We discusses this very issue.

Re: Slow Tile Scrolling

Posted: Tue Feb 21, 2012 8:56 am
by BlobOfFailure
Light-Dark, thanks for helping me speed up my map scrolling. That seemed to do the trick, it works a lot faster now.
Falco, thanks for enlightening me on the proper way to make a tile engine.