Help needed: flickering / not responding when blitting multiple times


I’m starting with SDL (version 1.2.11 under win XP SP2). I have implemented a simple program to paint the screen of a different color 100 times. To do this I use an additional surface which pixels are modified in a loop and then I blit both surfaces. This is repeated 100 times, but I have the following problem:

If I let everything run without limiting the speed I get a lot of flickering (actually using software surfaces, but tried with hardware, double buffer, etc and could not get rid of the flickering ever) so I decided to introduce a delay for each frame. Unfortunately when I do so my program keeps running ok but after a couple of frames the corresponding window appears as Not Responding and it gets blank.

The code is as simple as the following:

#include “SDL/SDL.h”
#include <windows.h>
using namespace std;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface screen = NULL;
SDL_Surface image = NULL;
Uint32 imagePixels[SCREEN_TOTAL_PIXELS];
void apply_surface( int x, int y, SDL_Surface
source, SDL_Surface
destination ) {
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
int main( int argc, char* args[] ) {
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) {
return 1;
//Set up the screen
int framesShown = 0;
int i;
SDL_PixelFormat *screenPixelFormat = screen->format;
// create the image surface from the pixels setted above
image = SDL_CreateRGBSurfaceFrom(imagePixels, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, screen->pitch, 0, 0, 0, 0);
while (framesShown<100) {
cout << "Frame: " << framesShown << “\n”;
for(i = 0; i < SCREEN_TOTAL_PIXELS; i++) {
imagePixels[i] = 1000000 + (100 * framesShown);
// apply the image to the screen
apply_surface( 0, 0, image, screen );
//Update the screen
if( SDL_Flip( screen ) == -1 ) {
return 1;
int j = 0;
SDL_FreeSurface( image );
//Quit SDL
return 0;

Any ideas please?

Thanks in advanced,

Martin G---------------------------------
Pregunt?. Respond?. Descubr?.
Todo lo que quer?as saber, y lo que ni imaginabas,
est? en Yahoo! Respuestas (Beta).
Probalo ya!