How’s this? (Quick and dirty…)
SDL_bool
Screenshot (char *name)
{
SDL_Surface *scr;
Uint8 *buf;
size_t imgsize, writesize;
FILE *f;
scr = SDL_GetVideoSurface ();
imgsize = scr->w * scr->h * 3 + 18 + 26;
buf = calloc (1, imgsize);
if (!buf)
return SDL_FALSE;
/* Fill in the necessary bits of the header - RTF Targa spec! */
buf[2] = 0x02; /* F3, uncompressed truecolor */
buf[12] = (scr->w & 255); /* F5.3, width, little endian */
buf[13] = (scr->w >> 8);
buf[14] = (scr->h & 255); /* F5.4, height, little endian */
buf[15] = (scr->h >> 8);
buf[16] = 24; /* F5.5, bpp */
// Fill in the pixel data
glReadPixels (0, 0, scr->w, scr->h, GL_BGR, GL_UNSIGNED_BYTE,
&buf[18]);
/* Fill in the image footer and file ID */
strcpy (&buf[imgsize - 18], "TRUEVISION-XFILE.");
/* Spit out the file */
f = fopen (name, "wb");
if (!f)
{
free (buf);
return SDL_FALSE;
}
writesize = fwrite (buf, imgsize, 1, f);
free (buf);
fclose (f);
if (writesize == imgsize)
return SDL_TRUE;
return SDL_FALSE;
}
Caveats: I didn’t even TRY to explain the Targa header fields. If you
want to know what they are, go to wotsit and download the Targa 2.0 spec.
Note that the two high bits of Field 5.6 have been assigned a use, but
AFAIK not by Truevision, and I’ve never seen a reference implementation
which used them… Targa supports a lot of optional fields which I don’t
set in this function.
Why not read the pixels into another SDL_Surface? Three reasons: SDL with
or without SDL_image can only write Windows BMP, glReadPixels’ output is
bottom-to-top as all OpenGL image data is (as anyone who has tried to
upload SDL_Surfaces as textures has already discovered for themselves),
and it’s positively confusing to go back and forth between the pixel
format namings used by SDL which uses Uint32s for pixel data (and
therefore cares about endianness) and OpenGL which doesn’t. If you want
to make it work with a SDL_Surface, you’ll have to flip the image line by
line and keep in mind that on little-endian machines, what OpenGL calls
BGR is RGB when you’re talking to SDL…
One more note about using GL_BGR - it requires OpenGL 1.2. It exists as
an extension in, as far as I can tell, all Win32 OpenGL drivers. BGR and
BGRA are the native formats used by Win32 and in fact DirectX only
supports BGRA (which IMO just as foolishly as SDL, is called “ARGB”…)
Still, it’s the only format that Targa understands, and certain drivers
(such as 3dfx in Linux until the most recent versions) will return BGR
data from glReadPixels even if you specified GL_RGB. You can either check
for the extension or 1.2 and cope gracefully when it’s not present or you
can just assume it works and expect anyone for whom it doesn’t to complain
to their card’s chipset maker about crappy drivers which are more than 4
years out of date. I never see complaints when games demand DirectX 7 or
nowadays 8, but I always get someone whining when I expect OpenGL 1.2,
especially on Win32. Go figure.On Sun, Jun 23, 2002 at 01:13:24AM -0300, Danny Angelo Carminati Grein wrote:
| > Why when I try and grab a screenshot of an SDL window (under Linux) do
| > I get the following when I press the “grab” button in ‘xv’?
|
| Is this window OpenGL, by chance?
And how to take a screen shot with SDL OpenGL Surface?
–
Joseph Carter If this sig were funny…
$you = new YOU;
honk() if $you->love(perl)
– Seen on Slashdot
-------------- 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/20020623/734b52d0/attachment.pgp