Allow draw::set_render_target to switch to the main render buffer.

This is done by passing it an empty texture.
This commit is contained in:
Tommy 2023-10-25 16:22:18 +13:00
parent 0eada04f55
commit 08144affdf
4 changed files with 24 additions and 6 deletions

View File

@ -577,7 +577,7 @@ draw::render_target_setter::render_target_setter(const texture& t)
, viewport_()
{
// Validate we can render to this texture.
assert(t.get_access() == SDL_TEXTUREACCESS_TARGET);
assert(!t || t.get_access() == SDL_TEXTUREACCESS_TARGET);
if (!renderer()) {
WRN_D << "can't set render target with null renderer";
@ -587,7 +587,11 @@ draw::render_target_setter::render_target_setter(const texture& t)
target_ = video::get_render_target();
SDL_RenderGetViewport(renderer(), &viewport_);
video::force_render_target(t);
if (t) {
video::force_render_target(t);
} else {
video::reset_render_target();
}
}
draw::render_target_setter::~render_target_setter()
@ -602,7 +606,11 @@ draw::render_target_setter::~render_target_setter()
draw::render_target_setter draw::set_render_target(const texture& t)
{
DBG_D << "setting render target to "
<< t.w() << 'x' << t.h() << " texture";
if (t) {
DBG_D << "setting render target to "
<< t.w() << 'x' << t.h() << " texture";
} else {
DBG_D << "setting render target to main render buffer";
}
return draw::render_target_setter(t);
}

View File

@ -446,7 +446,9 @@ private:
* SDL_TEXTUREACCESS_TARGET access mode.
*
* @param t The new render target. This must be a texture created
* with SDL_TEXTUREACCESS_TARGET.
* with SDL_TEXTUREACCESS_TARGET, or an empty texture.
* If empty, it will set the render target to Wesnoth's
* primary render buffer.
* @returns A render_target_setter object. When this object is
* destroyed the render target will be restored to
* whatever it was before this call.

View File

@ -522,6 +522,11 @@ void clear_render_target()
force_render_target({});
}
void reset_render_target()
{
force_render_target(render_texture_);
}
texture get_render_target()
{
// This should always be up-to-date, but assert for sanity.
@ -564,7 +569,7 @@ void render_screen()
SDL_RenderPresent(*window);
// Reset the render target to the render texture.
force_render_target(render_texture_);
reset_render_target();
}
surface read_pixels(SDL_Rect* r)

View File

@ -290,6 +290,9 @@ void force_render_target(const texture& t);
/** Reset the render target to the main window / screen. */
void clear_render_target();
/** Reset the render target to the primary render buffer. */
void reset_render_target();
/** Get the current render target.
*
* Will return an empty texture if the render target is the underlying