Sign in to follow this  
rafaelmgn

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

Recommended Posts

rafaelmgn    100
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?

Share this post


Link to post
Share on other sites
rip-off    10979
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()?

Share this post


Link to post
Share on other sites
rafaelmgn    100
I did the following:

[code]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);[/code]

I tried to use SDL_GetError() but it didn't returned anything.

Share this post


Link to post
Share on other sites
Wooh    1088
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 is[code]mySurface = SDL_DisplayFormat(screen);[/code]

Share this post


Link to post
Share on other sites
rafaelmgn    100
Sorry, I mistyped that line:

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

I tried to use SDL_DisplayFormat, but no result and no error... [img]http://public.gamedev.net/public/style_emoticons/default/huh.gif[/img]

Share this post


Link to post
Share on other sites
rafaelmgn    100
Sorry, I just put a fragment of the code, just a explanation of what I'm doing...

[code]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);
}[/code]

It pass through the 'if'... there's no error, but it gives me a black surface.

Share this post


Link to post
Share on other sites
Wooh    1088
In your code SDL_DisplayFormat should return a blank surface because you haven't copied the screen surface yet.

Share this post


Link to post
Share on other sites
Wooh    1088
It is probably easier if you make a testcase: A small program that compiles and have the same problem you want to show. Because now we are only guessing.

Share this post


Link to post
Share on other sites
rip-off    10979
Here is such a reasonably minimal example:
[code]
#include "SDL.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

int main(int, char **)
{
std::srand(static_cast<unsigned>(std::time(0)));

if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "Failed to initialise SDL: " << SDL_GetError() << '\n';
return 1;
}

std::atexit(&SDL_Quit);

SDL_Surface *screen = SDL_SetVideoMode(400, 400, 0, 0);
if(!screen)
{
std::cerr << "Failed to initialise SDL: " << SDL_GetError() << '\n';
return 1;
}

bool running = true;
while(running)
{
SDL_Event event;
if(SDL_WaitEvent(&event))
{
if(event.type == SDL_QUIT)
{
running = false;
}
else if((event.type == SDL_KEYUP) && (event.key.keysym.sym == SDLK_ESCAPE))
{
running = false;
}
}

SDL_FillRect(screen, NULL, 0);

for(int i = 0 ; i < 10 ; ++i)
{
SDL_Rect r;
r.w = ((std::rand() % 5) + 1) * 10;
r.h = ((std::rand() % 5) + 1) * 10;
r.x = std::rand() % (screen->w - r.w);
r.y = std::rand() % (screen->h - r.h);
SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 0xff * ((i % 3) == 0), 0xff * ((i % 3) == 1), 0xff * ((i % 3) == 2)));
}

SDL_Flip(screen);
}

SDL_Surface *copy = SDL_DisplayFormat(screen);
if(!copy)
{
std::cerr << "Failed to copy surface: " << SDL_GetError() << '\n';
return 1;
}

if(SDL_SaveBMP(copy, "out.bmp") != 0)
{
std::cerr << "Failed to save output: " << SDL_GetError() << '\n';
return 1;
}

std::cout << "Success!\n";
return 0;
}
[/code]
Works for me[sup]TM[/sup].

Share this post


Link to post
Share on other sites
rafaelmgn    100
I deleted all my code, and started again. It worked... [img]http://public.gamedev.net/public/style_emoticons/default/mellow.gif[/img]
I used SDL_DisplayFormat to convert the screen to another surface.

Thanks guys

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this