Results 1 to 5 of 5

Thread: Screenshot with OpenGL

  1. #1

    Screenshot with OpenGL

    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?
    Freeze Development | Elysion Game Framework | Twitter: @Stoney_FD
    Check out my new book: Irrlicht 1.7.1 Realtime 3D Engine Beginner's Guide (It's C++ flavored though)

    Programmer: A device for converting coffein into software.

  2. #2
    SDL_SaveBMP(tmpSurface, PChar(Filename));

    Shouldn't it be like this?
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  3. #3
    Quote Originally Posted by JSoftware View Post
    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
    Freeze Development | Elysion Game Framework | Twitter: @Stoney_FD
    Check out my new book: Irrlicht 1.7.1 Realtime 3D Engine Beginner's Guide (It's C++ flavored though)

    Programmer: A device for converting coffein into software.

  4. #4
    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

  5. #5
    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.
    Freeze Development | Elysion Game Framework | Twitter: @Stoney_FD
    Check out my new book: Irrlicht 1.7.1 Realtime 3D Engine Beginner's Guide (It's C++ flavored though)

    Programmer: A device for converting coffein into software.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •