Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

    //Dynamically drawn each time

    //Hero Start Position


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 == "")

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

temp = SDL_LoadBMP(writable);
sprite = SDL_DisplayFormat(temp);
// free the string after using it
delete[] writable;
share|improve this question
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. – Roy T. Dec 22 '12 at 12:06
The delay happens after the full array is rendered. i could make it 1000 jsut to see the final image for 1 second – Jonathan O Dec 22 '12 at 12:07
Why not just draw as fast as possible? Have you timed how long the loop takes? How many miliseconds? – Roy T. Dec 22 '12 at 13:04
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. – Josh Petrie Dec 22 '12 at 14:56
up vote 6 down vote accepted

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.

share|improve this answer
Ok cool. Ill try that. How can i reuse the image instead of reloading it? Store it as a private in my Sprite.h? – Jonathan O Dec 22 '12 at 12:08
SDL_LoadBMP(c_str(ImagePath)) Like this? – Jonathan O Dec 22 '12 at 12:09
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? :) – Liosan Dec 22 '12 at 12:10
temp is stored as a private variable – Jonathan O Dec 22 '12 at 12:10
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? – Jonathan O Dec 22 '12 at 12:13

For loading images only once, check out 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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.