PDA

View Full Version : Screenshot with OpenGL



Stoney
21-11-2010, 06:14 PM
I am currently on polishing up my Ludum Dare #17 and I'm stuck with a function on how to make a screenshot from within my game. I'm using SDL + OpenGL btw.

My function looks like this:


procedure TakeScreenshot(Filename: String);
var
tmpSurface: PSDL_Surface;
rmask, gmask, bmask, amask: Uint32;
SBits: Integer;
begin

if ExtractFileExt(Filename) <> '.bmp' then
begin
Filename := Filename + '.bmp';
end;

if SDL_BYTEORDER = SDL_BIG_ENDIAN then
begin
rmask:=$ff000000;
gmask:=$00ff0000;
bmask:=$0000ff00;
amask := 0;
end else
begin
rmask:=$000000ff;
gmask:=$0000ff00;
bmask:=$00ff0000;
amask := 0;
end;

tmpSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Self.Width, Self.Height, 24, rmask, gmask, bmask, amask);

if not Assigned(tmpSurface) then Exit;

glReadPixels(0, 0, Self.Width, Self.Height, GL_RGB, GL_UNSIGNED_BYTE, tmpSurface^.pixels);

SDL_SaveBMP(SDL_Surface, PChar(Filename));

SDL_FreeSurface(tmpSurface);
end;
(Self.Width and Self.Height returns the current width and height of my SDL window.)
Each time I call this function I get an Access Violation and I'm not really sure why. Can someone help me?

JSoftware
21-11-2010, 06:28 PM
SDL_SaveBMP(tmpSurface, PChar(Filename));

Shouldn't it be like this?

Stoney
21-11-2010, 06:45 PM
SDL_SaveBMP(tmpSurface, PChar(Filename));

Shouldn't it be like this?
You're right, thanks. I missed that somehow.
It's still not working though :(

Srki_82
21-11-2010, 11:16 PM
Which line of code is throwing Access Violation? I guess it's the glReadPixels line.
My guess is that you must lock surface before writting pixels using SDL_LockSurface (http://www.libsdl.org/docs/html/sdllocksurface.html)

Stoney
22-11-2010, 01:05 AM
Thank you, Srki_82.
Locking and unlocking the surface did the trick when saving the surface as a BMP. I'm still having trouble when saving the surface as a TGA, but BMP is enough for me at the moment.