Thanks for Dennis Jenkins for sending a code that exports the window
to a BMP file.
My problem is that the following statement:
SDL_Surface *screen = SDL_GetVideoSurface ();
just simply doesn’t work. After this statement the screen variable is
NULL.
Is that because I’m using MFC? How can I get the video surface of an
MFC program? Or what am I doing wrong?
I only have a menu at the top of the window. By clicking on “Export
bmp” I’d like to save the whole surface to a BMP file. This is what the
source code is. This code always displays the “Cannot get the video
surface” error message" What am I doing wrong??
Thanks for your help !!!
void CMainFrame::OnExportbmpExport ()
{
//Initalize SDL
#ifdef _DEBUG
SDL_Init (SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
#else
SDL_Init (SDL_INIT_VIDEO);
#endif
//Get SDL Surface
SDL_Surface *screen = SDL_GetVideoSurface ();
SDL_Surface *temp;
unsigned char * pixels;
int i;
if (screen != NULL)
{
if (!(screen->flags & SDL_OPENGL)) {
SDL_SaveBMP (temp, "a.bmp");
}
temp = SDL_CreateRGBSurface (SDL_SWSURFACE, screen->w,
screen->h, 24,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x000000FF, 0x0000FF00, 0x00FF0000, 0
#else
0x00FF0000, 0x0000FF00, 0x000000FF, 0
#endif
);
pixels = (unsigned char *) malloc (3 * (screen->w) * (screen->h));
if (pixels == NULL){
SDL_FreeSurface (temp);
}
glReadPixels ( 0,
0, screen->w,
screen->h,
GL_RGB,
GL_UNSIGNED_BYTE,
pixels);
for (i = 0; i < screen->h; i++){
memcpy (((char *) temp->pixels) + temp->pitch * i
, pixels + 3 * (screen->w) *
((screen->h) - i - 1)
, (screen->w) * 3);
}
free (pixels);
SDL_SaveBMP (temp, "a.bmp");
free (pixels);
SDL_FreeSurface (temp);
}
else
MessageBox (“Unable to get the video surface.”
, “Error”
, MB_OK | MB_ICONERROR);
// Shutdown SDL
SDL_Quit ();
}