Save Image From Render |
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:
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||
|
Naith
|
Here´s a short function that creates a screenshot file (*.bmp) from a renderer. Hope it works the way you want.
Code:
|
|||||||||||||
|
Save Image From Render |
Pallav Nawani
|
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:
|
|||||||||||||
|
Save Image From Rende |
alejandroicom
|
Works fine! thanks!
|
|||||||||||
|