-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
render_utils.hpp
140 lines (118 loc) · 3.37 KB
/
render_utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
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>;
/*
* TEXTURE SETTERS =========================================================================
* Need to decide if these need their own file.
*/
inline void set_texture_alpha(texture& t, uint8_t amount)
{
SDL_SetTextureAlphaMod(t, amount);
}
inline void set_texture_blend_color(texture& t, uint8_t r, uint8_t g, uint8_t b)
{
SDL_SetTextureColorMod(t, r, g, b);
}
inline void set_texture_blend_mode(texture& t, SDL_BlendMode mode)
{
SDL_SetTextureBlendMode(t, mode);
}
/**
* Sets the texture scale quality. Note this should be called *before* a texture
* is created, since the hint has no effect on existing textures or render ops.
*
* @param value The scaling mode. Use either 'linear' or 'nearest'.
*/
inline void set_texture_scale_quality(const std::string& value)
{
SDL_SetHintWithPriority(SDL_HINT_RENDER_SCALE_QUALITY, value.c_str(), SDL_HINT_OVERRIDE);
}