diff --git a/src/video.cpp b/src/video.cpp index f6f23c1dbd1..8d163818e0c 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -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) { diff --git a/src/video.hpp b/src/video.hpp index 2ba0fdbea21..498d24b87e9 100644 --- a/src/video.hpp +++ b/src/video.hpp @@ -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.