Re: reading pixels from a TARGET texture
"rtrussell" <[email protected]>
| Newsgroups | gmane.comp.lib.sdl |
|---|---|
| Message-ID | <[email protected]> |
Sanette wrote:
> What is the best way to obtain this pixel array ?
You can use SDL_RenderReadPixels but there are a couple of problems with that approach:
The API is buggy. For example with some renderers it will return the image upside-down (https://bugzilla.libsdl.org/show_bug.cgi?id=2740)!
The API can be very slow, especially if the target texture is significantly larger than the window.
To work around the first issue, and to ameliorate the second, I use this slightly more complex method (when the rectangle you want to read is no larger than the window):
Code:
int RenderReadPixels (SDL_Renderer* renderer, const SDL_Rect* rect,
Uint32 format, void* pixels, int pitch)
{
int result ;
SDL_Rect dst = {0, 0, rect->w, rect->h} ;
SDL_Texture *tex = SDL_GetRenderTarget (renderer) ;
SDL_SetRenderTarget (renderer, NULL) ;
SDL_RenderCopy (renderer, tex, rect, &dst) ;
result = SDL_RenderReadPixels (renderer, &dst, format, pixels, pitch) ;
SDL_SetRenderTarget (renderer, tex) ;
return result ;
}
Richard.
_______________________________________________
SDL mailing list
[email protected]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org