CVideo: add functions to read pixels back from the render target.

This commit is contained in:
Tommy 2022-05-03 16:51:29 +12:00
parent fca99c9f6f
commit 5877371d70
2 changed files with 61 additions and 0 deletions

View File

@ -41,6 +41,7 @@
static lg::log_domain log_display("display");
#define LOG_DP LOG_STREAM(info, log_display)
#define ERR_DP LOG_STREAM(err, log_display)
#define DBG_DP LOG_STREAM(debug, log_display)
CVideo* CVideo::singleton_ = nullptr;
@ -520,6 +521,30 @@ void CVideo::render_screen()
}
}
surface CVideo::read_pixels(SDL_Rect* r)
{
SDL_Rect d = draw_area();
SDL_Rect r_clipped = d;
if (r) {
r_clipped = sdl::intersect_rects(*r, d);
if (r_clipped != *r) {
DBG_DP << "modifying pixel read area\n"
<< " from " << *r << "\n"
<< " to " << r_clipped << std::endl;
*r = r_clipped;
}
}
SDL_Rect o = to_output(r_clipped);
surface s(o.w, o.h);
SDL_RenderReadPixels(*window, &o, s->format->format, s->pixels, s->pitch);
return s;
}
texture CVideo::read_texture(SDL_Rect* r)
{
return texture(read_pixels(r));
}
void CVideo::lock_updates(bool value)
{
if(value == true) {

View File

@ -312,6 +312,42 @@ public:
/** Returns a reference to the drawing surface. */
surface& getDrawingSurface();
/**
* Copy back a portion of the render target that is already drawn.
*
* This area is specified in draw coordinates, not render coordinates.
* Thus the size of the retrieved surface may not match the size of r.
*
* If not null, r will be automatically clipped to the drawing area.
*
* Note: This is a very slow function! Its use should be phased out
* for everything except maybe screenshots.
*
* @param r The portion of the render target to retrieve, in
* draw coordinates.
* If not null, this will be modified to reflect the
* portion of the draw area that has been returned.
*/
surface read_pixels(SDL_Rect* r = nullptr);
/**
* Copy a portion of the render target to another texture.
*
* This area is specified in draw coordinates, not render coordinates.
* Thus the size of the retrieved texture may not match the size of r.
*
* If not null, r will be automatically clipped to the drawing area.
*
* Note: This is a very slow function! Its use should be phased out
* for everything except maybe screenshots.
*
* @param r The portion of the render target to retrieve, in
* draw coordinates.
* If not null, this will be modified to reflect the
* portion of the draw area that has been returned.
*/
texture read_texture(SDL_Rect* r = nullptr);
/**
* Stop the screen being redrawn. Anything that happens while the updates are locked will
* be hidden from the user's view.