The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Save Image From Render
alejandroicom


Joined: 27 Aug 2014
Posts: 4
Hi, with the function SDL_SaveBMP I can save a image from a Surface exists a function that allows me save a image from a Render. I need do a ScreenShoot.

Thanks!
Save Image From Render
Jeffrey Carpenter
Guest

Hello there,

AFAIK, SDL2 does not have a public function available to do this for us
magically -- perhaps because it is so high-level (app-level / specific) -- but that's fine, because we can do it ourselves easily using the SDL2 Renderer
API (save to use with OpenGL, even).

// Psuedo-code for saving a screen shot of your SDL_Window contents; sloppily
copied from my game engine, nomlib, nom::RenderWindow && nom::Renderer class interfaces @ https://github.com/i8degrees/nomlib
{
SDL_Rect clip;
void* pixels = nullptr;

SDL_Rect output_size; // width & height in pixels

if( SDL_GetRendererOutputSize ( this->renderer(), &output_size.w, &output_size.h ) != 0 )
{
NOM_LOG_ERR( NOM, SDL_GetError() );
return Size2i::null;
}

// In order to accurately capture our window, with respect to aspect ratio,
// such as the case of us using device independent scaling [1], we must
// calculate the true bounding dimensions of our rendering target.
clip.x = 0;
clip.y = 0;
clip.w = output_size.w;
clip.h = output_size.h;

pixels = static_cast<uint32*> ( malloc( clip.w * clip.h * 4 ) );

if ( SDL_RenderReadPixels( this->renderer(),
// Use calculated bounding dimensions;
// Pass null to dump the pixels of the entire
// render target
&clip,
// Use the most optimal pixel format;
// Pass zero here to obtain the pixel format of
// the render target
// Either use a filled RenderInfo struct to obtain
// optimal texture format,
// ...or... cheat! in the mean time and use this
SDL_PIXELFORMAT_ARGB8888,
// Allocated pointer to be filled in with pixels
// from render target
pixels,
// Pitch of our pixels pointer
clip.w * 4 ) != 0 )
{
NOM_LOG_ERR( NOM, SDL_GetError() );
return nullptr;
}

int bpp = 0; // bits per pixel
uint32 red_mask = 0;
uint32 green_mask = 0;
uint32 blue_mask = 0;
uint32 alpha_mask = 0;

RendererInfo caps = this->caps(); // Pixel format
Image screenshot; // SDL_Surface*

// Width & height of target in pixels
SDL_Rect render_size = output_size;

if ( SDL_BOOL( SDL_PixelFormatEnumToMasks ( caps.optimal_texture_format(), &bpp, &red_mask, &green_mask, &blue_mask, &alpha_mask ) ) != true )
{
NOM_LOG_ERR( NOM, SDL_GetError() );
return false;
}

screenshot.initialize( pixels, renderer_size.w, renderer_size.h, bpp, (renderer_size.w * 4), red_mask, green_mask, blue_mask, alpha_mask );

// This is a C++ wrapper for ( pixels, width, height, bits_per_pixel, pitch, Rmask, Gmask, Bmask, Amask )

// Note that you may also use SDL_SavePNG & friends here, too!
if( SDL_SaveBMP( screenshot(), "screenshot.bmp" ) != 0 )
{
// Handle err
}

// Success!
// NOM_LOG_INFO( NOM, "The screen-shot file is saved at: ", filename );
}

Let me know if you have any questions. Good luck!

Cheers,
Jeffrey Carpenter

On 2014/10/ 02, at 7:49, alejandroicom wrote:

Quote:
Hi, with the function SDL_SaveBMP I can save a image from a Surface exists a function that allows me save a image from a Render. I need do a ScreenShoot.

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Naith


Joined: 03 Jul 2014
Posts: 158
Here´s a short function that creates a screenshot file (*.bmp) from a renderer. Hope it works the way you want. Smile

Code:

Code:

   // Create an empty RGB surface that will be used to create the screenshot bmp file
   SDL_Surface* pScreenShot = SDL_CreateRGBSurface(0, WINDOW_WIDTH, WINDOW_HEIGHT, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);

   if(pScreenShot)
   {
      // Read the pixels from the current render target and save them onto the surface
      SDL_RenderReadPixels(m_pRenderer, NULL, SDL_GetWindowPixelFormat(m_pWindow), pScreenShot->pixels, pScreenShot->pitch);

      // Create the bmp screenshot file
      SDL_SaveBMP(pScreenShot, "Screenshot.bmp");

      // Destroy the screenshot surface
      SDL_FreeSurface(pScreenShot);
   }

Save Image From Render
Pallav Nawani


Joined: 19 May 2011
Posts: 122
Location: Dehradun, India
If you use Software Renderer than this can probably work. There is no way to get pixel data from textures, unfortunately. Software Renderer however, uses a SDL_Surface, which you can directly pass to SDL_SaveBMP().

Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768


On Thu, Oct 2, 2014 at 6:19 PM, alejandroicom wrote:
Quote:
Hi, with the function SDL_SaveBMP I can save a image from a Surface exists a function that allows me save a image from a Render. I need do a ScreenShoot.

Thanks!


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Save Image From Rende
alejandroicom


Joined: 27 Aug 2014
Posts: 4
Works fine! thanks!