0
\$\begingroup\$

I have a basic 20x20 array that stores the map for my game. It either has a 1 or 0 to say a path and a non path. When I draw it on the screen, It seems to take to long to draw one at a time and looks like a snake is being loaded on the screen. I need them drawing faster.

Set up the screen array

for(int i=0; i<sizeOfArray; i++)
    {
        for(int j=0; j<sizeOfArray; j++)
        {
            if (LevelOne[i][j]==0)//Path
            {
                screenChunks[i][j].Set(i*40,j*40,"Path.bmp", screen);
            }
            if (LevelOne[i][j]==1)//Wall
            {
                screenChunks[i][j].Set(i*40,j*40,"Wall.bmp", screen);
            }
            if (LevelOne[i][j]==2) //Hero
            {
                screenChunks[i][j].Set(i*40,j*40,"Hero.bmp", screen);
            }
        }
    }

Draw the sprites

for(int i=0; i<sizeOfArray; i++)
    {
        for(int j=0; j<sizeOfArray; j++)
        {
            screenChunks[i][j].Draw();
        }
    }

    //Dynamically drawn each time
    SDL_FillRect(screen,0,0);

    //Hero Start Position
    hero.Draw();

    SDL_Delay(60);

Sprite.cpp Draw Method

void Sprite::Draw()
{
SDL_Rect offset;

//Give the offsets to the rectangle
offset.x = PosX;
offset.y = PosY;

//Blit the surface
SDL_BlitSurface( sprite, NULL, Screen, &offset );

Height = offset.h;
Width = offset.w;

SDL_UpdateRect(Screen, 0,0,0,0);

}

Sprite Setter

void Sprite::Set(int posX, int posY, string imagePath, SDL_Surface *screen)
{
PosX = posX;
PosY = posY;
ImagePath = imagePath;
Screen = screen;

if(ImagePath == "")
    return;

char * writable = new char [ImagePath.size()+1];
strcpy (writable, ImagePath.c_str());

temp = SDL_LoadBMP(writable);
sprite = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
// free the string after using it
delete[] writable;
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ It's only 400 sprites so it shouldn't be too slow. Is that ` SDL_Delay(60);` supposed to be there? Sprite Batching might be helpful but the problem is probably somewhere else with so few sprites. \$\endgroup\$
    – Roy T.
    Dec 22, 2012 at 12:06
  • \$\begingroup\$ The delay happens after the full array is rendered. i could make it 1000 jsut to see the final image for 1 second \$\endgroup\$
    – joncodo
    Dec 22, 2012 at 12:07
  • \$\begingroup\$ Why not just draw as fast as possible? Have you timed how long the loop takes? How many miliseconds? \$\endgroup\$
    – Roy T.
    Dec 22, 2012 at 13:04
  • 3
    \$\begingroup\$ What does your profiler tell you? If you haven't used a profiler, get one and do so. It's far better than guesswork at determining where performance issues are in your code. \$\endgroup\$
    – user1430
    Dec 22, 2012 at 14:56

2 Answers 2

6
\$\begingroup\$

Loading an image from disk every time you need to draw it is a bad idea. Terrible, in fact. Try loading once and reusing it.

Also, please have a look at calling SDL_UpdateRect(Screen, 0,0,0,0); on every sprite draw...

BTW, if you're doing the whole "writable" thing because ImagePath is an std::string and SDL_LoadBMP accepts char*, then you should google the c_str() method.

\$\endgroup\$
9
  • \$\begingroup\$ Ok cool. Ill try that. How can i reuse the image instead of reloading it? Store it as a private in my Sprite.h? \$\endgroup\$
    – joncodo
    Dec 22, 2012 at 12:08
  • \$\begingroup\$ SDL_LoadBMP(c_str(ImagePath)) Like this? \$\endgroup\$
    – joncodo
    Dec 22, 2012 at 12:09
  • \$\begingroup\$ For instance. I don't see where the temp variable is declared, so maybe you are already doing that. As for c_str()... just search C++ c_str() for an example, ok? :) \$\endgroup\$
    – Liosan
    Dec 22, 2012 at 12:10
  • \$\begingroup\$ temp is stored as a private variable \$\endgroup\$
    – joncodo
    Dec 22, 2012 at 12:10
  • \$\begingroup\$ However I load the image for each sprite. Is there a way to load it once for all images of type wall? Should I make a class called wall to inherit from this? \$\endgroup\$
    – joncodo
    Dec 22, 2012 at 12:13
1
\$\begingroup\$

For loading images only once, check out https://stackoverflow.com/questions/1729699/how-do-i-correctly-use-sdl-freesurface-when-dealing-with-a-vector-of-surfaces which introduces the idea of an image_cache.

I've expanded on this on my blog here and then again once I'd updated my design to break up the class into smaller more manageable pieces here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .