cpp-sdl2
C++ header-only SDL2 wrapper
texture.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "color.hpp"
4 #include "exception.hpp"
5 #include "pixel.hpp"
6 #include "rect.hpp"
7 #include "surface.hpp"
8 #include "vec2.hpp"
9 
10 #include <SDL_render.h>
11 
12 #include <string>
13 
14 namespace sdl
15 {
17 class Texture
18 {
19 public:
21  class Lock
22  {
23  friend class Texture;
24 
25  public:
27  Pixel at(Vec2i const& pos) const { return at(pos.x, pos.y); }
29  Pixel at(size_t x, size_t y) const
30  {
31  return Pixel{
32  static_cast<Uint8*>(pixels_) + (y * pitch_) + (x * format_->BytesPerPixel),
33  *format_};
34  }
35 
37  Pixel operator[](Vec2i const& pos) const { return at(pos.x, pos.y); }
38 
41  {
42  SDL_UnlockTexture(texture_);
43  SDL_FreeFormat(format_);
44  }
45 
46  private:
48  Lock(SDL_Texture* texture, SDL_Rect const* rect) : texture_{texture}
49  {
50  if (SDL_LockTexture(texture_, rect, &pixels_, &pitch_) != 0)
51  {
52  throw Exception{"SDL_LockTexture"};
53  }
54 
55  Uint32 f;
56  SDL_QueryTexture(texture_, &f, nullptr, nullptr, nullptr);
57  format_ = SDL_AllocFormat(f);
58 
59  if (!format_) throw Exception{"SDL_AllocFormat"};
60  }
61 
63  SDL_Texture* texture_;
65  void* pixels_;
67  int pitch_;
69  SDL_PixelFormat* format_;
70  };
71 
73  explicit Texture(SDL_Texture* t) : texture_{t} {};
74 
76  SDL_Texture* ptr() const { return texture_; }
77 
79  Texture(SDL_Renderer* render, Uint32 format, SDL_TextureAccess access, int w, int h)
80  : Texture{SDL_CreateTexture(render, format, access, w, h)}
81  {
82  if (!texture_) throw Exception{"SDL_CreateTexture"};
83  }
84 
86  Texture() = default;
87 
89  Texture(SDL_Renderer* render, Uint32 format, SDL_TextureAccess access, Vec2i size)
90  : Texture{render, format, access, size.x, size.y}
91  {
92  }
93 
95  Texture(SDL_Renderer* render, Surface const& surface)
96  : Texture{SDL_CreateTextureFromSurface(render, surface.ptr())}
97  {
98  if (!texture_) throw Exception{"SDL_CreateTextureFromSurface"};
99  }
100 
102  Texture(SDL_Renderer* render, std::string const& filename) : Texture{render, Surface{filename}}
103  {
104  }
105 
107  Texture(Texture&& other) noexcept { *this = std::move(other); }
108 
110  Texture& operator=(Texture&& other) noexcept
111  {
112  if (texture_ != other.texture_)
113  {
114  SDL_DestroyTexture(texture_);
115  texture_ = other.texture_;
116  other.texture_ = nullptr;
117  }
118 
119  return *this;
120  }
121 
123  Texture(Texture const&) = delete;
125  Texture& operator=(Texture const&) = delete;
126 
128  ~Texture() { SDL_DestroyTexture(texture_); }
129 
131  void set_blendmode(SDL_BlendMode const& bm) const
132  {
133  if (SDL_SetTextureBlendMode(texture_, bm) != 0) throw Exception{"SDL_SetTextureBlendMode"};
134  }
136  SDL_BlendMode blendmode() const
137  {
138  SDL_BlendMode bm;
139  if (SDL_GetTextureBlendMode(texture_, &bm) != 0) throw Exception{"SDL_GetTextureBlendMode"};
140  return bm;
141  }
142 
144  void set_colormod(Color const& color) const { set_colormod(color.r, color.g, color.b); }
146  void set_colormod(Uint8 r, Uint8 g, Uint8 b) const
147  {
148  if (SDL_SetTextureColorMod(texture_, r, g, b) != 0)
149  throw Exception{"SDL_SetTextureColorMod"};
150  }
151 
153  Color colormod() const
154  {
155  Color c;
156  if (SDL_GetTextureColorMod(texture_, &c.r, &c.g, &c.b) != 0)
157  throw Exception{"SDL_SetTextureColorMod"};
158  return c;
159  }
160 
162  void set_alphamod(Uint8 alpha) const
163  {
164  if (SDL_SetTextureAlphaMod(texture_, alpha) != 0) throw Exception{"SDL_SetTextureAlphaMod"};
165  }
167  Uint8 alphamod() const
168  {
169  Uint8 alpha;
170  if (SDL_GetTextureAlphaMod(texture_, &alpha) != 0)
171  throw Exception{"SDL_GetTextureAlphaMod"};
172  return alpha;
173  }
174 
176  void set_coloralphamod(Uint8 r, Uint8 g, Uint8 b, Uint8 a) const
177  {
178  set_colormod(r, g, b);
179  set_alphamod(a);
180  }
182  void set_coloralphamod(Color const& c) const
183  {
184  set_colormod(c);
185  set_alphamod(c.a);
186  }
189  {
190  auto c = colormod();
191  c.a = alphamod();
192  return c;
193  }
194 
196  Uint32 format() const
197  {
198  Uint32 f;
199  if (SDL_QueryTexture(texture_, &f, nullptr, nullptr, nullptr) != 0)
200  throw Exception{"SDL_QueryTexture"};
201  return f;
202  }
203 
205  int access() const
206  {
207  int a;
208  if (SDL_QueryTexture(texture_, nullptr, &a, nullptr, nullptr) != 0)
209  throw Exception{"SDL_QueryTexture"};
210  return a;
211  }
212 
214  Vec2i size() const
215  {
216  Vec2i s;
217  if (SDL_QueryTexture(texture_, nullptr, nullptr, &s.x, &s.y) != 0)
218  throw Exception{"SDL_QueryTexture"};
219  return s;
220  }
221 
223  [[nodiscard]] Lock lock() { return Lock{texture_, nullptr}; }
224 
226  [[nodiscard]] Lock lock(Rect const& rect) { return Lock{texture_, &rect}; }
227 
228 private:
229  SDL_Texture* texture_ = nullptr;
230 };
231 
232 } // namespace sdl
SDL_Texture * ptr() const
Get SDL_Texture pointer from texture.
Definition: texture.hpp:76
void set_colormod(Color const &color) const
Set colormod.
Definition: texture.hpp:144
Texture(SDL_Renderer *render, Surface const &surface)
Create texture from surface.
Definition: texture.hpp:95
sdl::Rect, C++ wrapping of SDL_Rect
Definition: rect.hpp:11
void set_alphamod(Uint8 alpha) const
Set alphamod.
Definition: texture.hpp:162
Generic templated 2D vector class.
Definition: vec2.hpp:24
Texture(SDL_Renderer *render, Uint32 format, SDL_TextureAccess access, Vec2i size)
Create texture.
Definition: texture.hpp:89
Color colormod() const
Get colormod.
Definition: texture.hpp:153
void * pixels_
pointer to pixel array
Definition: texture.hpp:65
Lock(SDL_Texture *texture, SDL_Rect const *rect)
private ctor to create a lock. Lock are created by Texture class
Definition: texture.hpp:48
int access() const
Access texture.
Definition: texture.hpp:205
Color coloralphamod() const
Get coloralphamod.
Definition: texture.hpp:188
~Texture()
Destroy texture when it&#39;s not in use anymore.
Definition: texture.hpp:128
Pixel operator[](Vec2i const &pos) const
Get pixel at location with operator[].
Definition: texture.hpp:37
Uint32 format() const
Get texture format.
Definition: texture.hpp:196
Class that represet a renderer texture.
Definition: texture.hpp:17
SDL_Texture * texture_
pointer to raw texure
Definition: texture.hpp:63
Texture(SDL_Renderer *render, Uint32 format, SDL_TextureAccess access, int w, int h)
Create texture.
Definition: texture.hpp:79
SDL_PixelFormat * format_
Pixel format.
Definition: texture.hpp:69
C++ wrapping around the SDL_Color structure.
Definition: color.hpp:10
Pixel at(size_t x, size_t y) const
Get pixel at given location on texture.
Definition: texture.hpp:29
Define to deactivate exception support.
Definition: color.hpp:7
Texture & operator=(Texture &&other) noexcept
Move texture into this one.
Definition: texture.hpp:110
friend class Texture
Definition: texture.hpp:23
int pitch_
Pixel pitch.
Definition: texture.hpp:67
Vec2i size() const
Get texture size.
Definition: texture.hpp:214
void set_coloralphamod(Uint8 r, Uint8 g, Uint8 b, Uint8 a) const
Set coloralphamod.
Definition: texture.hpp:176
Lock object that permit to access pixels directly on texture.
Definition: texture.hpp:21
Represent a 4 channel pixel.
Definition: pixel.hpp:10
Lock lock(Rect const &rect)
lock texture rect
Definition: texture.hpp:226
Texture(SDL_Texture *t)
Construct texture from C SDL_Texture object.
Definition: texture.hpp:73
~Lock()
Automatically unlock texture.
Definition: texture.hpp:40
allocator< U > a
Definition: simd.hpp:144
SDL_Surface * ptr() const
Get C SDL_Surface object.
Definition: surface.hpp:144
Uint8 alphamod() const
Set alphamod.
Definition: texture.hpp:167
Represent an SDL_Surface.
Definition: surface.hpp:21
void set_coloralphamod(Color const &c) const
Set coloralphamod.
Definition: texture.hpp:182
Pixel at(Vec2i const &pos) const
Get pixel at given location on texture.
Definition: texture.hpp:27
Texture(SDL_Renderer *render, std::string const &filename)
Create texture from file.
Definition: texture.hpp:102
SDL_BlendMode blendmode() const
Get texture blend mode.
Definition: texture.hpp:136
void set_blendmode(SDL_BlendMode const &bm) const
Set texture blend mode.
Definition: texture.hpp:131
Define to deactivate exception support.
Definition: exception.hpp:21
Texture(Texture &&other) noexcept
Move texture into this one.
Definition: texture.hpp:107
Lock lock()
lock texture for direct access to content
Definition: texture.hpp:223
void set_colormod(Uint8 r, Uint8 g, Uint8 b) const
Set colormod.
Definition: texture.hpp:146