[SDL] How to copy the screen to another surface?
In your code SDL_DisplayFormat should return a blank surface because you haven't copied the screen surface yet.
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:
Works for me[sup]TM[/sup].
#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].
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement