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

Started by
13 comments, last by rafaelmgn 12 years, 8 months ago
In your code SDL_DisplayFormat should return a blank surface because you haven't copied the screen surface yet.
Advertisement
SDL_BlitSurface is returning 0 (success)...
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.
Here is such a reasonably minimal example:

#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;
}

Works for me[sup]TM[/sup].
I deleted all my code, and started again. It worked... mellow.gif
I used SDL_DisplayFormat to convert the screen to another surface.

Thanks guys

This topic is closed to new replies.

Advertisement