Slow Tile Scrolling

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
BlobOfFailure
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Sat Nov 27, 2010 10:38 am
Programming Language of Choice: C++

Slow Tile Scrolling

Post 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.
User avatar
Light-Dark
Dreamcast Developer
Dreamcast Developer
Posts: 307
Joined: Sun Mar 13, 2011 7:57 pm
Current Project: 2D RPG & NES Platformer
Favorite Gaming Platforms: NES,SNES,N64,Genesis,Dreamcast,PC,Xbox360
Programming Language of Choice: C/++
Location: Canada

Re: Slow Tile Scrolling

Post 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
<tpw_rules> LightDark: java is a consequence of inverse moore's law: every 18 months, the average program will be twice as slow. therefore, computers always run at the same percevied speed. java's invention was a monumental step
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Slow Tile Scrolling

Post 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.
User avatar
BlobOfFailure
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Sat Nov 27, 2010 10:38 am
Programming Language of Choice: C++

Re: Slow Tile Scrolling

Post 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.
Post Reply