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:
Code:
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?