cpp-sdl2
C++ header-only SDL2 wrapper
renderer.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "color.hpp"
4 #include "exception.hpp"
5 #include "rect.hpp"
6 #include "surface.hpp"
7 #include "texture.hpp"
8 
9 #include <SDL.h>
10 #include <SDL_render.h>
11 
12 #include <utility>
13 #include <vector>
14 
15 namespace sdl
16 {
18 class Renderer
19 {
20 public:
22  explicit Renderer(SDL_Renderer* renderer) : renderer_{renderer} {}
23 
25  Renderer() {}
26 
28  Renderer(Renderer&& other) noexcept { *this = std::move(other); }
29 
31  Renderer& operator=(Renderer&& other) noexcept
32  {
33  if (renderer_ != other.renderer_)
34  {
35  SDL_DestroyRenderer(renderer_);
36  renderer_ = other.renderer_;
37  other.renderer_ = nullptr;
38  }
39  return *this;
40  }
41 
43  ~Renderer() { SDL_DestroyRenderer(renderer_); }
44 
46  Renderer(Renderer const&) = delete;
47 
49  Renderer& operator=(Renderer const&) = delete;
50 
52  SDL_Renderer* ptr() const { return renderer_; }
53 
55  void get_info(SDL_RendererInfo& info) const
56  {
57  if (SDL_GetRendererInfo(renderer_, &info) != 0) throw Exception{"SDL_GetRendererInfo"};
58  }
59 
61  SDL_RendererInfo info() const
62  {
63  SDL_RendererInfo info;
64  get_info(info);
65  return info;
66  }
67 
68  // Get the current draw color
69  Color drawcolor() const
70  {
71  Color c;
72  if (SDL_GetRenderDrawColor(renderer_, &c.r, &c.g, &c.b, &c.a) != 0)
73  throw Exception{"SDL_GetRenderDrawColor"};
74  return c;
75  }
76 
78  void set_drawcolor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = SDL_ALPHA_OPAQUE) const
79  {
80  if (SDL_SetRenderDrawColor(renderer_, r, g, b, a) != 0)
81  throw Exception{"SDL_SetRenderDrawColor"};
82  }
83 
85  void set_drawcolor(Color const& c) const { set_drawcolor(c.r, c.g, c.b, c.a); }
86 
88  Rect cliprect() const
89  {
90  Rect r;
91  SDL_RenderGetClipRect(renderer_, &r);
92  return r;
93  }
94 
96  void set_cliprect(Rect const& r) const
97  {
98  if (SDL_RenderSetClipRect(renderer_, &r) != 0) throw Exception{"SDL_RenderSetClipRect"};
99  }
100 
102  bool clip_enabled() const { return SDL_RenderIsClipEnabled(renderer_); }
103 
105  void disable_clip() const
106  {
107  if (SDL_RenderSetClipRect(renderer_, nullptr) != 0)
108  throw Exception{"SDL_RenderSetClipRect"};
109  }
110 
112  bool intscale() const { return SDL_RenderGetIntegerScale(renderer_); }
113 
115  void set_intscale(bool intscale) const
116  {
117  if (SDL_RenderSetIntegerScale(renderer_, SDL_bool(intscale)) != 0)
118  throw Exception{"SDL_RenderSetIntegerScale"};
119  }
120 
122  Texture make_texture(Uint32 format, SDL_TextureAccess access, int w, int h) const
123  {
124  return Texture{renderer_, format, access, w, h};
125  }
126 
128  Texture make_texture(Uint32 format, SDL_TextureAccess access, Vec2i size) const
129  {
130  return Texture{renderer_, format, access, size.x, size.y};
131  }
132 
134  Texture make_texture(Surface const& surface) const { return Texture{renderer_, surface}; }
135 
138  Texture make_texture(std::string const& filename) const { return Texture{renderer_, filename}; }
139 
140  void render_copy(Texture const& tex, Rect const& source_rect, Rect const& dest_rect) const
141  {
142  // SDL will *not* modify the texture or the rects here, but the
143  // signature has a non-const pointer So we are forced to cast away our
144  // const ref on texture
145  SDL_RenderCopy(renderer_, const_cast<Texture&>(tex).ptr(), &source_rect, &dest_rect);
146  }
147 
149  void present() const { SDL_RenderPresent(renderer_); }
150 
152  void clear() const
153  {
154  if (SDL_RenderClear(renderer_) != 0) throw Exception{"SDL_RenderClear"};
155  }
156 
158  void clear(Color const& c) const
159  {
160  set_drawcolor(c);
161  clear();
162  }
163 
165  void draw_line(Vec2i const& pos1, Vec2i const& pos2) const
166  {
167  if (SDL_RenderDrawLine(renderer_, pos1.x, pos1.y, pos2.x, pos2.y) != 0)
168  throw Exception{"SDL_RenderDrawLine"};
169  }
170 
172  void draw_line(Vec2i const& pos1, Vec2i const& pos2, Color const& c) const
173  {
174  set_drawcolor(c);
175  draw_line(pos1, pos2);
176  }
177 
179  void draw_lines(std::vector<Vec2i> const& points) const
180  {
181  if (SDL_RenderDrawLines(renderer_, &points[0], (int)points.size()) != 0)
182  throw Exception{"SDL_RenderDrawLines"};
183  }
184 
186  void draw_lines(std::vector<Vec2i> const& points, Color const& c) const
187  {
188  set_drawcolor(c);
189  draw_lines(points);
190  }
191 
193  void draw_point(Vec2i const& point) const
194  {
195  if (SDL_RenderDrawPoint(renderer_, point.x, point.y) != 0)
196  throw Exception{"SDL_RenderDrawPoint"};
197  }
198 
200  void draw_point(Vec2i const& point, Color const& c) const
201  {
202  set_drawcolor(c);
203  draw_point(point);
204  }
205 
207  void draw_points(std::vector<Vec2i> const& points) const
208  {
209  if (SDL_RenderDrawPoints(renderer_, &points[0], (int)points.size()) != 0)
210  throw Exception{"SDL_RenderDrawPoints"};
211  }
212 
214  void draw_points(std::vector<Vec2i> const& points, Color const& c) const
215  {
216  set_drawcolor(c);
217  draw_points(points);
218  }
219 
220  void draw_ray(Vec2i const& orig, Vec2i const& ray) const { draw_line(orig, orig + ray); }
221  void draw_ray(Vec2i const& orig, Vec2i const& ray, Color const& c) const
222  {
223  draw_line(orig, orig + ray, c);
224  }
225 
227  void draw_rect(Rect const& rect) const
228  {
229  if (SDL_RenderDrawRect(renderer_, &rect) != 0) throw Exception{"SDL_RenderDrawRect"};
230  }
231 
233  void draw_rect(Rect const& rect, Color const& c) const
234  {
235  set_drawcolor(c);
236  draw_rect(rect);
237  }
238 
240  void draw_rects(std::vector<Rect> const& rects) const
241  {
242  if (SDL_RenderDrawRects(renderer_, &rects[0], (int)rects.size()) != 0)
243  throw Exception{"SDL_RenderDrawRects"};
244  }
246  void draw_rects(std::vector<Rect> const& rects, const Color& c) const
247  {
248  set_drawcolor(c);
249  draw_rects(rects);
250  }
251 
253  void fill_rect(Rect const& rect) const
254  {
255  if (SDL_RenderFillRect(renderer_, &rect) != 0) throw Exception{"SDL_RenderFillRect"};
256  }
258  void fill_rect(Rect const& rect, Color const& c) const
259  {
260  set_drawcolor(c);
261  fill_rect(rect);
262  }
263 
265  void fill_rects(std::vector<Rect> const& rects) const
266  {
267  if (SDL_RenderFillRects(renderer_, &rects[0], (int)rects.size()) != 0)
268  throw Exception{"SDL_RenderDrawRects"};
269  }
271  void fill_rects(std::vector<Rect> const& rects, Color const& c)
272  {
273  set_drawcolor(c);
274  fill_rects(rects);
275  }
276 
277 private:
279  SDL_Renderer* renderer_ = nullptr;
280 };
281 
282 } // namespace sdl
void draw_points(std::vector< Vec2i > const &points, Color const &c) const
Draw array of points with specified color.
Definition: renderer.hpp:214
void set_drawcolor(Uint8 r, Uint8 g, Uint8 b, Uint8 a=SDL_ALPHA_OPAQUE) const
Set the drawcolor from color values as bytes.
Definition: renderer.hpp:78
Texture make_texture(Uint32 format, SDL_TextureAccess access, Vec2i size) const
Make a new texture.
Definition: renderer.hpp:128
sdl::Rect, C++ wrapping of SDL_Rect
Definition: rect.hpp:11
Color drawcolor() const
Definition: renderer.hpp:69
void draw_line(Vec2i const &pos1, Vec2i const &pos2) const
Draw line between two points.
Definition: renderer.hpp:165
void fill_rect(Rect const &rect) const
Fill rectangle.
Definition: renderer.hpp:253
void get_info(SDL_RendererInfo &info) const
Populate renderinfo structure.
Definition: renderer.hpp:55
Generic templated 2D vector class.
Definition: vec2.hpp:24
void set_drawcolor(Color const &c) const
Set the drawcolor from color struct.
Definition: renderer.hpp:85
void draw_line(Vec2i const &pos1, Vec2i const &pos2, Color const &c) const
Draw line between two points with specified color.
Definition: renderer.hpp:172
Rect cliprect() const
Get the clipping rectangle.
Definition: renderer.hpp:88
void draw_ray(Vec2i const &orig, Vec2i const &ray, Color const &c) const
Definition: renderer.hpp:221
Texture make_texture(std::string const &filename) const
Make a texture from a file.
Definition: renderer.hpp:138
void draw_point(Vec2i const &point) const
Draw point.
Definition: renderer.hpp:193
Renderer(Renderer &&other) noexcept
Default move ctor.
Definition: renderer.hpp:28
void draw_lines(std::vector< Vec2i > const &points, Color const &c) const
Draw array of lines with specified color.
Definition: renderer.hpp:186
bool clip_enabled() const
Return true if clipping is enable.
Definition: renderer.hpp:102
void draw_ray(Vec2i const &orig, Vec2i const &ray) const
Definition: renderer.hpp:220
void present() const
Present renderer.
Definition: renderer.hpp:149
Class that represet a renderer texture.
Definition: texture.hpp:17
void draw_rect(Rect const &rect, Color const &c) const
Draw rectangle with specified color.
Definition: renderer.hpp:233
void fill_rect(Rect const &rect, Color const &c) const
Fill rectangle with specified color.
Definition: renderer.hpp:258
C++ wrapping around the SDL_Color structure.
Definition: color.hpp:10
Define to deactivate exception support.
Definition: color.hpp:7
bool intscale() const
Get the current integer scale.
Definition: renderer.hpp:112
void fill_rects(std::vector< Rect > const &rects) const
Fill array of rectangles.
Definition: renderer.hpp:265
void draw_rects(std::vector< Rect > const &rects, const Color &c) const
Draw array of rectangles with specified colors.
Definition: renderer.hpp:246
Texture make_texture(Uint32 format, SDL_TextureAccess access, int w, int h) const
Make a new texture.
Definition: renderer.hpp:122
Renderer()
Default ctor, create an empty renderer object.
Definition: renderer.hpp:25
void draw_point(Vec2i const &point, Color const &c) const
Draw point with specified color.
Definition: renderer.hpp:200
SDL_RendererInfo info() const
Get renderer infos.
Definition: renderer.hpp:61
Texture make_texture(Surface const &surface) const
Make a texture from a surface.
Definition: renderer.hpp:134
SDL_Renderer * renderer_
Pointer to raw SDL_Renderer.
Definition: renderer.hpp:279
SDL_Renderer * ptr() const
Return a pointer to the wrapped C SDL_Renderer.
Definition: renderer.hpp:52
void draw_rects(std::vector< Rect > const &rects) const
Draw array of rectangles.
Definition: renderer.hpp:240
void draw_lines(std::vector< Vec2i > const &points) const
Draw array of lines.
Definition: renderer.hpp:179
void draw_rect(Rect const &rect) const
Draw rectangle.
Definition: renderer.hpp:227
void draw_points(std::vector< Vec2i > const &points) const
Draw array of points.
Definition: renderer.hpp:207
allocator< U > a
Definition: simd.hpp:144
Represent an SDL_Surface.
Definition: surface.hpp:21
Renderer(SDL_Renderer *renderer)
Construct a renderer from the SDL_Renderer C object.
Definition: renderer.hpp:22
void set_cliprect(Rect const &r) const
Set the clipping rectangle.
Definition: renderer.hpp:96
void render_copy(Texture const &tex, Rect const &source_rect, Rect const &dest_rect) const
Definition: renderer.hpp:140
~Renderer()
Destroy the renderer automaticlally when object goes out of scope.
Definition: renderer.hpp:43
void clear(Color const &c) const
Clear with specified color.
Definition: renderer.hpp:158
void fill_rects(std::vector< Rect > const &rects, Color const &c)
Fill array of rectangles with specified colors.
Definition: renderer.hpp:271
void disable_clip() const
Disable the clipping rectangle.
Definition: renderer.hpp:105
void set_intscale(bool intscale) const
Set the integer scale.
Definition: renderer.hpp:115
Define to deactivate exception support.
Definition: exception.hpp:21
Renderer & operator=(Renderer &&other) noexcept
Move a renderer to this one.
Definition: renderer.hpp:31
void clear() const
Clear renderer.
Definition: renderer.hpp:152
Class that represent a SDL2 2D renderer.
Definition: renderer.hpp:18