Moved renderer RAII helpers to their own file and added ones for clip rect and viewport
Do note that setting an empty rect in render_raii_rect_setter_base is not the same as passing nullptr. If an empty rect is passed to SDL_RenderGetClipRect, for example, you can end up with nothing drawing since everything is being clipped.
This commit is contained in:
parent
a710d6d457
commit
81a62bf49c
5 changed files with 110 additions and 35 deletions
|
@ -1009,6 +1009,7 @@
|
|||
<Unit filename="../../src/sdl/point.cpp" />
|
||||
<Unit filename="../../src/sdl/point.hpp" />
|
||||
<Unit filename="../../src/sdl/rect.cpp" />
|
||||
<Unit filename="../../src/sdl/render_utils.hpp" />
|
||||
<Unit filename="../../src/sdl/surface.cpp" />
|
||||
<Unit filename="../../src/sdl/surface.hpp" />
|
||||
<Unit filename="../../src/sdl/texture.cpp" />
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
#include "gui/core/log.hpp"
|
||||
#include "gui/widgets/helper.hpp"
|
||||
#include "sdl/rect.hpp"
|
||||
#include "sdl/render_utils.hpp"
|
||||
#include "video.hpp"
|
||||
#include "wml_exception.hpp"
|
||||
|
||||
|
|
108
src/sdl/render_utils.hpp
Normal file
108
src/sdl/render_utils.hpp
Normal file
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
Copyright (C) 2017 the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "sdl/rect.hpp"
|
||||
#include "sdl/texture.hpp"
|
||||
#include "video.hpp"
|
||||
|
||||
#include <SDL_rect.h>
|
||||
#include <SDL_render.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
/**
|
||||
* Sets the renderer output target to the specified texture.
|
||||
* @todo: add support for restoring last target.
|
||||
*/
|
||||
class render_target_setter
|
||||
{
|
||||
public:
|
||||
explicit render_target_setter(texture& t)
|
||||
: renderer_(CVideo::get_singleton().get_renderer())
|
||||
{
|
||||
if(renderer_) {
|
||||
// Validate we can render to this texture.
|
||||
assert(t.get_info().access == SDL_TEXTUREACCESS_TARGET);
|
||||
|
||||
SDL_SetRenderTarget(renderer_, t);
|
||||
}
|
||||
}
|
||||
|
||||
~render_target_setter()
|
||||
{
|
||||
if(renderer_) {
|
||||
SDL_SetRenderTarget(renderer_, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
};
|
||||
|
||||
using sdl_rect_getter = void (*)(SDL_Renderer*, SDL_Rect*);
|
||||
using sdl_rect_setter = int (*)(SDL_Renderer*, const SDL_Rect*);
|
||||
|
||||
/**
|
||||
* Base class for renderer RAII helpers that operate on SDL_Rects.
|
||||
*
|
||||
* @tparam G Getter function. Will fetch the current applicable rect.
|
||||
* That will be restored as the applicable state once this object is destroyed.
|
||||
* @tparam S Setter function.
|
||||
*/
|
||||
template<sdl_rect_getter G, sdl_rect_setter S>
|
||||
class render_raii_rect_setter_base
|
||||
{
|
||||
public:
|
||||
explicit render_raii_rect_setter_base(SDL_Rect* rect)
|
||||
: operate_(rect != nullptr)
|
||||
, last_rect_()
|
||||
, renderer_(CVideo::get_singleton().get_renderer())
|
||||
{
|
||||
if(renderer_ && operate_) {
|
||||
(*G)(renderer_, &last_rect_);
|
||||
(*S)(renderer_, rect);
|
||||
}
|
||||
}
|
||||
|
||||
~render_raii_rect_setter_base()
|
||||
{
|
||||
if(renderer_ && operate_) {
|
||||
if(last_rect_ != sdl::empty_rect) {
|
||||
(*S)(renderer_, &last_rect_);
|
||||
} else {
|
||||
(*S)(renderer_, nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const bool operate_;
|
||||
SDL_Rect last_rect_;
|
||||
SDL_Renderer* renderer_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the renderer clip rect.
|
||||
*/
|
||||
using render_clip_rect_setter = render_raii_rect_setter_base<
|
||||
&SDL_RenderGetClipRect,
|
||||
&SDL_RenderSetClipRect>;
|
||||
|
||||
/**
|
||||
* Sets the renderer viewport rect.
|
||||
*/
|
||||
using render_viewport_setter = render_raii_rect_setter_base<
|
||||
&SDL_RenderGetViewport,
|
||||
&SDL_RenderSetViewport>;
|
|
@ -17,9 +17,6 @@
|
|||
#include "sdl/surface.hpp"
|
||||
#include "video.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
static lg::log_domain log_sdl("SDL");
|
||||
#define ERR_SDL LOG_STREAM(err, log_sdl)
|
||||
|
||||
|
@ -112,21 +109,3 @@ texture::info::info(const texture& t)
|
|||
{
|
||||
SDL_QueryTexture(t, &format, &access, &w, &h);
|
||||
}
|
||||
|
||||
render_target_setter::render_target_setter(texture& t)
|
||||
: renderer_(CVideo::get_singleton().get_renderer())
|
||||
{
|
||||
if(renderer_) {
|
||||
// Validate we can render to this texture.
|
||||
assert(t.get_info().access == SDL_TEXTUREACCESS_TARGET);
|
||||
|
||||
SDL_SetRenderTarget(renderer_, t);
|
||||
}
|
||||
}
|
||||
|
||||
render_target_setter::~render_target_setter()
|
||||
{
|
||||
if(renderer_) {
|
||||
SDL_SetRenderTarget(renderer_, nullptr);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -80,17 +80,3 @@ private:
|
|||
|
||||
SDL_Texture* texture_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Small RAII helper class to temporarily set the renderer target to a texture.
|
||||
*/
|
||||
class render_target_setter
|
||||
{
|
||||
public:
|
||||
explicit render_target_setter(texture& t);
|
||||
|
||||
~render_target_setter();
|
||||
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue