[SDL] How to copy the screen to another surface?

Started by
13 comments, last by rafaelmgn 12 years, 7 months ago
Hello!

I'm trying to make a copy of the actual screen to another SDL_Surface. I tried to use SDL_BlitSurface and SDL_DisplayFormat, rounded and non-rounded by callings of SDL_LockSurface and SDL_UnlockSurface, but I can't get the copy. I only get an empty surface.

Anyone can help me?
Advertisement
SDL_BlitSurface() should work, without SDL_LockSurface(). How are you allocating the destination surface? Have you tried checking the return value of SDL_BlitSurface(), and the value of SDL_GetError()?
I did the following:

SDL_Surface *mySurface, *temp;
SDL_Rect rect;

temp = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, screen->w, screen->h, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);

if (temp) {
mySurface = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
}

rect.x = 0;
rect.y = 0;

SDL_BlitSurface(screen, NULL, mySurface, &rect);


I tried to use SDL_GetError() but it didn't returned anything.
SDL_CreateRGBSurface takes 8 arguments but you only pass 7 arguments. How is that possible?

SDL_DisplayFormat creates a copy so all you should have to do to create a copy of the screen surface ismySurface = SDL_DisplayFormat(screen);
Sorry, I mistyped that line:

temp = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, screen->w, screen->h, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);


I tried to use SDL_DisplayFormat, but no result and no error... huh.gif
You aren't checking for errors in that code.
Sorry, I just put a fragment of the code, just a explanation of what I'm doing...

SDL_Surface *mySurface, *temp;
SDL_Rect rect;

temp = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, screen->w, screen->h, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);

if (temp) {
mySurface = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
}

rect.x = 0;
rect.y = 0;

SDL_BlitSurface(screen, NULL, mySurface, &rect);

if (!mySurface) {
cerr << string(SDL_GetError()) << endl;
exit(-1);
}


It pass through the 'if'... there's no error, but it gives me a black surface.
When you say no result, do you mean that SDL_DisplayFormat returns NULL?
No... it returns a black surface with the size of the screen...
You still aren't checking the return value of SDL_BlitSurface().

This topic is closed to new replies.

Advertisement