2

I'm using SDL in windowed mode.

Given a large sprite, say 1024 x 640, is it faster to blit the entire image into severals small tiles, say 32 x 32 px, than to blit the whole image at once ?

I have to blit a background on the screen (1024 * 640) in windowed mode @ 30 FPS (at least). I think that double buffering and hardware surface cannot be used when windowed... For now, I blit the whole sprite at once, but the CPU consumption seems to be very high for a simple background blitting.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Arnaud G
  • 139
  • 1
  • 1
  • 4
  • 1
    Don't forget to [convert your surfaces to the display format](http://www.libsdl.org/docs/html/sdldisplayformat.html), otherwise SDL will do behind-the-scenes conversions for *each and every blit*. – genpfault Nov 15 '11 at 17:09
  • I did. But, blit still are CPU-greedy ... – Arnaud G Nov 18 '11 at 11:33
  • @ArnaudG I know this is quite old, but how do you compute the CPU consumption? What framerate are you getting? – emartel Nov 16 '12 at 21:46

1 Answers1

0

First off make sure that the image is in a small file type format like "PNG". Second when blitting the image make sure that you are not adding the transparency color to the background image is it is pointless and takes up a LOT of cpu cycles. Here's an example of code I've written a long time ago as a wrapper for image loading

SDL_Surface* altSDL::load_image(std::string filename)  
{
    SDL_Surface* loadedImage = NULL;

    SDL_Surface* optimizedImage = NULL;

    loadedImage = IMG_Load( filename.c_str() );

    if( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );        
        SDL_FreeSurface( loadedImage );
    }
    else
    {
        Failure* fail;
        fail = Failure::getInstance();
        fail->failLog(filename);
    }

    if( optimizedImage != NULL )
    {
        Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xFF, 0, 0xFF );
        SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
    }

    return optimizedImage;
}

In this code you will be able to pass all your images into this to be optimized very seamlessly, but in the case of background/Large images you would need to make sure that this last block of code is changed to something more along the lines of something like this:

//Added some Sudo code

    if( loadedImage != NULL && !isBigImage)
            {
                optimizedImage = SDL_DisplayFormat( loadedImage );        
                SDL_FreeSurface( loadedImage );
            }
            else if(!isBigImage)
            {
                Failure* fail;
                fail = Failure::getInstance();
                fail->failLog(filename);
            }

the isBigImage is the sudo code i added and is a parameter passed in; in certain cases.This will make all your images optimized, smaller(Should make sure they are png files) and will make it so the transparency is only added to small image files so your background does not solw your fps