The Battle for Wesnoth  1.19.8+dev
texture.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "sdl/point.hpp"
18 #include "sdl/rect.hpp"
19 
20 #include <SDL2/SDL_hints.h>
21 #include <SDL2/SDL_render.h>
22 
23 #include <memory>
24 
25 class surface;
26 struct color_t;
27 
28 /**
29  * Wrapper class to encapsulate creation and management of an SDL_Texture.
30  * Supports free creation and creation from a surface.
31  */
32 class texture
33 {
34 public:
35  /** Default ctor. Texture will be a nullptr. */
36  texture() = default;
37 
38  // No other standard constructors need to be defined. See: Rule of Zero.
39  // However if you are implementing one... See: Rule of Five.
40  // There is potential to optimize copy and move by ignoring src_,
41  // however it is unlikely that this is a significant burden as-is.
42 
43  /** Assigns the given texture to this one. */
44  explicit texture(SDL_Texture* txt);
45 
46  /**
47  * Construct a texture from a surface.
48  *
49  * @param surf The surface to copy.
50  * @param linear_interpolation If true this texture will use linear
51  * interpolation when drawing. Otherwise
52  * nearest-neighbour interpolation is used.
53  * This does not affect texture creation,
54  * only later application.
55  */
56  explicit texture(const surface& surf, bool linear_interpolation = false);
57 
58  /** Construct a texture of the specified size and access type. */
59  texture(int w, int h, SDL_TextureAccess access);
60 
61 
62  /********************/
63  /* raw texture info */
64  /********************/
65 
66  /** Small wrapper that queries metadata about the provided texture. */
67  struct info
68  {
69  explicit info(SDL_Texture* t);
70 
71  uint32_t format;
72  int access;
74  };
75 
76  /** Queries metadata about the texture, such as its dimensions. */
77  const info get_info() const
78  {
79  return info(*this);
80  }
81 
82  /** The internal texture format. Equivalent to get_info().format. */
83  uint32_t get_format() const;
84 
85  /** The texture access mode. Equivalent to get_info().access. */
86  int get_access() const;
87 
88  /** The raw internal texture size. Equivalent to get_info().size. */
89  point get_raw_size() const;
90 
91 
92  /*************/
93  /* draw size */
94  /*************/
95 
96  /**
97  * The draw-space width of the texture, in pixels.
98  *
99  * This will usually be the real texture width in pixels, but may
100  * differ in some situations. For high-DPI text, for example,
101  * it will usually be equal to get_info().size.x / pixel_scale.
102  */
103  int w() const { return size_.x; }
104 
105  /**
106  * The draw-space height of the texture, in pixels.
107  *
108  * This will usually be the real texture height in pixels, but may
109  * differ in some situations. For high-DPI text, for example,
110  * it will usually be equal to get_info().size.y / pixel_scale.
111  */
112  int h() const { return size_.y; }
113 
114  /**
115  * The size of the texture in draw-space.
116  *
117  * This may differ from the raw texture size. To get that in stead,
118  * use get_info() or get_raw_size().
119  */
120  point draw_size() const { return size_; }
121 
122  /** Set the intended width of the texture, in draw-space. */
123  void set_draw_width(int w) { size_.x = w; }
124 
125  /** Set the intended height of the texture, in draw-space. */
126  void set_draw_height(int h) { size_.y = h; }
127 
128  /** Set the intended size of the texture, in draw-space. */
129  void set_draw_size(int w, int h) { size_ = {w, h}; }
130  void set_draw_size(const point& size) { size_ = size; }
131 
132 
133  /*****************/
134  /* source region */
135  /*****************/
136 
137  /**
138  * A pointer to a rect indicating the source region of the underlying
139  * SDL_Texture to be used when drawing.
140  *
141  * If null, the whole SDL_Texture should be used.
142  *
143  * It should generally not be queried directly. Set it using set_src()
144  * or set_src_raw(). Clear it using clear_src().
145  */
146  const rect* src() const { return has_src_ ? &src_ : nullptr; }
147 
148  /**
149  * Set the source region of the texture used for drawing operations.
150  *
151  * This function operates in draw-space, and will be clipped to
152  * {0, 0, w(), h()}.
153  */
154  void set_src(const rect& r);
155 
156  /**
157  * Set the source region of the texture used for drawing operations.
158  *
159  * This function operates in texture-space, and will be clipped to
160  * {0, 0, get_raw_size().x, get_raw_size().y}.
161  */
162  void set_src_raw(const rect& r);
163 
164  /** Clear the source region. */
165  void clear_src() { has_src_ = false; }
166 
167 
168  /**********************/
169  /* texture properties */
170  /**********************/
171 
172  /** Alpha modifier. Multiplies alpha when drawing. */
173  void set_alpha_mod(uint8_t alpha);
174  uint8_t get_alpha_mod() const;
175 
176  /** Blend mode. Modifies how draw operations are applied. */
177  void set_blend_mode(SDL_BlendMode mode);
178  SDL_BlendMode get_blend_mode() const;
179 
180  /** Colour modifier. Multiplies each colour component when drawing. */
181  void set_color_mod(uint8_t r, uint8_t g, uint8_t b);
182  void set_color_mod(const color_t& c);
183  color_t get_color_mod() const;
184 
185  /** Releases ownership of the managed texture and resets the ptr to null. */
186  void reset();
187 
188  /** Releases ownership of the managed texture and creates a new one. */
189  void reset(int width, int height, SDL_TextureAccess access);
190 
191  /** Replaces ownership of the managed texture with the given one. */
192  void assign(SDL_Texture* t);
193 
194  SDL_Texture* get() const { return texture_.get(); }
195 
196  /** Texture comparisons explicitly only care about the pointer value. */
197  bool operator==(const texture& t) const { return get() == t.get(); }
198 
199  operator SDL_Texture*() const
200  {
201  return texture_.get();
202  }
203 
204  explicit operator bool() const
205  {
206  return texture_ != nullptr;
207  }
208 
209 private:
210  void finalize();
211 
212  std::shared_ptr<SDL_Texture> texture_ = nullptr;
213  point size_{0, 0};
214  bool has_src_ = false; /**< true iff the source rect is valid */
215  rect src_; /**< uninitialized by default. */
216 };
217 
218 /**
219  * Sets the texture scale quality. Note this should be called *before* a texture
220  * is created, since the hint has no effect on existing textures or render ops.
221  *
222  * @param value The scaling mode. Use either "linear" or "nearest".
223  */
224 inline void set_texture_scale_quality(const char* value)
225 {
226  SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, value);
227 }
double t
Definition: astarsearch.cpp:63
double g
Definition: astarsearch.cpp:63
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
uint8_t get_alpha_mod() const
Definition: texture.cpp:135
int w() const
The draw-space width of the texture, in pixels.
Definition: texture.hpp:103
rect src_
uninitialized by default.
Definition: texture.hpp:215
SDL_BlendMode get_blend_mode() const
Definition: texture.cpp:174
int get_access() const
The texture access mode.
Definition: texture.cpp:94
uint32_t get_format() const
The internal texture format.
Definition: texture.cpp:89
void set_draw_width(int w)
Set the intended width of the texture, in draw-space.
Definition: texture.hpp:123
std::shared_ptr< SDL_Texture > texture_
Definition: texture.hpp:212
void set_blend_mode(SDL_BlendMode mode)
Blend mode.
Definition: texture.cpp:167
bool has_src_
true iff the source rect is valid
Definition: texture.hpp:214
void finalize()
Definition: texture.cpp:84
bool operator==(const texture &t) const
Texture comparisons explicitly only care about the pointer value.
Definition: texture.hpp:197
void reset()
Releases ownership of the managed texture and resets the ptr to null.
Definition: texture.cpp:184
void set_src(const rect &r)
Set the source region of the texture used for drawing operations.
Definition: texture.cpp:104
texture()=default
Default ctor.
void set_draw_height(int h)
Set the intended height of the texture, in draw-space.
Definition: texture.hpp:126
void set_draw_size(const point &size)
Definition: texture.hpp:130
point draw_size() const
The size of the texture in draw-space.
Definition: texture.hpp:120
point size_
Definition: texture.hpp:213
void set_draw_size(int w, int h)
Set the intended size of the texture, in draw-space.
Definition: texture.hpp:129
point get_raw_size() const
The raw internal texture size.
Definition: texture.cpp:99
const rect * src() const
A pointer to a rect indicating the source region of the underlying SDL_Texture to be used when drawin...
Definition: texture.hpp:146
int h() const
The draw-space height of the texture, in pixels.
Definition: texture.hpp:112
void assign(SDL_Texture *t)
Replaces ownership of the managed texture with the given one.
Definition: texture.cpp:215
void set_alpha_mod(uint8_t alpha)
Alpha modifier.
Definition: texture.cpp:128
color_t get_color_mod() const
Definition: texture.cpp:157
void clear_src()
Clear the source region.
Definition: texture.hpp:165
void set_src_raw(const rect &r)
Set the source region of the texture used for drawing operations.
Definition: texture.cpp:121
SDL_Texture * get() const
Definition: texture.hpp:194
const info get_info() const
Queries metadata about the texture, such as its dimensions.
Definition: texture.hpp:77
void set_color_mod(uint8_t r, uint8_t g, uint8_t b)
Colour modifier.
Definition: texture.cpp:150
logger & info()
Definition: log.cpp:319
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
Contains the SDL_Rect helper code.
surface surf
Image.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:49
Small wrapper that queries metadata about the provided texture.
Definition: texture.hpp:68
uint32_t format
Definition: texture.hpp:71
point size
Definition: texture.hpp:73
info(SDL_Texture *t)
Definition: texture.cpp:222
mock_char c
void set_texture_scale_quality(const char *value)
Sets the texture scale quality.
Definition: texture.hpp:224
#define b