SDL_SaveBMP() with OpenGL surfaces

I am trying to take screenshots of my OpenGL program on Linux. Gimp
failed, I guess it didn’t handle overlay or something. I quickly wrote
code

printf( "Saving screenshot... " ); fflush( stdout );
SDL_LockSurface( surf );
SDL_SaveBMP( surf, "screenshot.bmp" );
SDL_UnlockSurface( surf );
printf( "Done!\n" );

Where surf is my SDL OpenGL surface. Now, this thing simply hangs, and
when I press CTRL-C, it segfaults. Is SDL_SaveBMP() supposed to work on
OpenGL surfaces?

If not, how should I work around this? Can I create a software surface
of the same mode into which I could use glReadPixels(), so I could
finally use SDL_SaveBMP() on that surface?

– Timo Suoranta – @Timo_K_Suoranta

Is SDL_SaveBMP() supposed to work on OpenGL surfaces?

No. In fact, the screen isn’t even a real SDL surface in OpenGL mode. The
code you posted “should” crash.

If not, how should I work around this? Can I create a software surface
of the same mode into which I could use glReadPixels(), so I could
finally use SDL_SaveBMP() on that surface?

Creating a software surface for OpenGL will only enforce software
rendering, still without making the screen an SDL surface.

However, glReadPixels() should do what you want regardless, if your
OpenGL setup is functioning properly. It’s often very slow (read from
VRAM), but at least it works without rendering into a special software
surface for each screenshot, which would be even slower.

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Friday 26 April 2002 23:22, Timo K Suoranta wrote:

If not, how should I work around this? Can I create a software surface
of the same mode into which I could use glReadPixels(), so I could
finally use SDL_SaveBMP() on that surface?

There’s example code here for saving the screen to a bitmap, when using
OpenGL.

http://www.libsdl.org/pipermail/sdl/2000-September/030013.html

I am trying to take screenshots of my OpenGL program on Linux. Gimp
failed, I guess it didn’t handle overlay or something. I quickly wrote
code

printf( "Saving screenshot… " ); fflush( stdout );
SDL_LockSurface( surf );
SDL_SaveBMP( surf, “screenshot.bmp” );
SDL_UnlockSurface( surf );
printf( “Done!\n” );

Where surf is my SDL OpenGL surface. Now, this thing simply hangs, and
when I press CTRL-C, it segfaults. Is SDL_SaveBMP() supposed to work on
OpenGL surfaces?

This won’t work at all. Try this, or something close (untested):

shot = SDL_CreateRGBSurface (SDL_SWSURFACE, surf->w, surf->h, 24,

#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x00FF0000, 0x0000FF00, 0x000000FF, 0);
#else
0x000000FF, 0x0000FF00, 0x00FF0000, 0);
#endif
glReadPixels (0, 0, surf->w, surf->h, GL_BGR, GL_UNSIGNED_BYTE,
shot->pixels);
SDL_SaveBMP (“screenshot.bmp”);
SDL_FreeSurface (shot);

SDL_Surface’s work with Uint32’s, not Uint8’s. This is frustrating
because byte order matters (I was just reminded of this myself…)

Use BGR, not RGB! This works around a bug with some broken drivers which
can only return BGR, regardless of what you ask for.

If not, how should I work around this? Can I create a software surface
of the same mode into which I could use glReadPixels(), so I could
finally use SDL_SaveBMP() on that surface?

Yes, similar to the above.On Sat, Apr 27, 2002 at 12:22:46AM +0300, Timo K Suoranta wrote:


Joseph Carter Not many fishes

Flinny: black crontab magic kinda stuff :slight_smile:
Joy: does that mean people get to dance naked around bonfires
chanting strange things and waving their arms about in a silly
manner?
knghtbrd: what do you think people do at novare?

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 273 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020426/3a591d7c/attachment.pgp