The Battle for Wesnoth  1.19.7+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;
73  int w;
74  int h;
75  };
76 
77  /** Queries metadata about the texture, such as its dimensions. */
78  const info get_info() const
79  {
80  return info(*this);
81  }
82 
83  /** The internal texture format. Equivalent to get_info().format. */
84  uint32_t get_format() const;
85 
86  /** The texture access mode. Equivalent to get_info().access. */
87  int get_access() const;
88 
89  /** The raw internal texture size.
90  * Equivalent to point{get_info().w, get_info().h} */
91  point get_raw_size() const;
92 
93 
94  /*************/
95  /* draw size */
96  /*************/
97 
98  /**
99  * The draw-space width of the texture, in pixels.
100  *
101  * This will usually be the real texture width in pixels, but may
102  * differ in some situations. For high-DPI text, for example,
103  * it will usually be equal to get_info().w / pixel_scale.
104  */
105  int w() const { return w_; }
106 
107  /**
108  * The draw-space height of the texture, in pixels.
109  *
110  * This will usually be the real texture height in pixels, but may
111  * differ in some situations. For high-DPI text, for example,
112  * it will usually be equal to get_info().h / pixel_scale.
113  */
114  int h() const { return h_; }
115 
116  /**
117  * The size of the texture in draw-space.
118  *
119  * This may differ from the raw texture size. To get that in stead,
120  * use get_info() or get_raw_size().
121  */
122  point draw_size() const { return {w_, h_}; }
123 
124  /** Set the intended width of the texture, in draw-space. */
125  void set_draw_width(int w) { w_ = w; }
126 
127  /** Set the intended height of the texture, in draw-space. */
128  void set_draw_height(int h) { h_ = h; }
129 
130  /** Set the intended size of the texture, in draw-space. */
131  void set_draw_size(int w, int h) { w_ = w; h_ = h; }
132  void set_draw_size(const point& p);
133 
134 
135  /*****************/
136  /* source region */
137  /*****************/
138 
139  /**
140  * A pointer to a rect indicating the source region of the underlying
141  * SDL_Texture to be used when drawing.
142  *
143  * If null, the whole SDL_Texture should be used.
144  *
145  * It should generally not be queried directly. Set it using set_src()
146  * or set_src_raw(). Clear it using clear_src().
147  */
148  const rect* src() const { return has_src_ ? &src_ : nullptr; }
149 
150  /**
151  * Set the source region of the texture used for drawing operations.
152  *
153  * This function operates in draw-space, and will be clipped to
154  * {0, 0, w(), h()}.
155  */
156  void set_src(const rect& r);
157 
158  /**
159  * Set the source region of the texture used for drawing operations.
160  *
161  * This function operates in texture-space, and will be clipped to
162  * {0, 0, get_raw_size().x, get_raw_size().y}.
163  */
164  void set_src_raw(const rect& r);
165 
166  /** Clear the source region. */
167  void clear_src() { has_src_ = false; }
168 
169 
170  /**********************/
171  /* texture properties */
172  /**********************/
173 
174  /** Alpha modifier. Multiplies alpha when drawing. */
175  void set_alpha_mod(uint8_t alpha);
176  uint8_t get_alpha_mod() const;
177 
178  /** Blend mode. Modifies how draw operations are applied. */
179  void set_blend_mode(SDL_BlendMode);
180  SDL_BlendMode get_blend_mode() const;
181 
182  /** Colour modifier. Multiplies each colour component when drawing. */
183  void set_color_mod(uint8_t r, uint8_t g, uint8_t b);
184  void set_color_mod(color_t);
185  color_t get_color_mod() const;
186 
187  /** Releases ownership of the managed texture and resets the ptr to null. */
188  void reset();
189 
190  /** Releases ownership of the managed texture and creates a new one. */
191  void reset(int width, int height, SDL_TextureAccess access);
192 
193  /** Replaces ownership of the managed texture with the given one. */
194  void assign(SDL_Texture* t);
195 
196  SDL_Texture* get() const { return texture_.get(); }
197 
198  /** Texture comparisons explicitly only care about the pointer value. */
199  bool operator==(const texture& t) const { return get() == t.get(); }
200 
201  operator SDL_Texture*() const
202  {
203  return texture_.get();
204  }
205 
206  explicit operator bool() const
207  {
208  return texture_ != nullptr;
209  }
210 
211 private:
212  void finalize();
213 
214  std::shared_ptr<SDL_Texture> texture_ = nullptr;
215  int w_ = 0;
216  int h_ = 0;
217  bool has_src_ = false; /**< true iff the source rect is valid */
218  rect src_; /**< uninitialized by default. */
219 };
220 
221 /**
222  * Sets the texture scale quality. Note this should be called *before* a texture
223  * is created, since the hint has no effect on existing textures or render ops.
224  *
225  * @param value The scaling mode. Use either "linear" or "nearest".
226  */
227 inline void set_texture_scale_quality(const char* value)
228 {
229  SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, value);
230 }
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:158
int w() const
The draw-space width of the texture, in pixels.
Definition: texture.hpp:105
rect src_
uninitialized by default.
Definition: texture.hpp:218
SDL_BlendMode get_blend_mode() const
Definition: texture.cpp:198
int get_access() const
The texture access mode.
Definition: texture.cpp:101
uint32_t get_format() const
The internal texture format.
Definition: texture.cpp:90
void set_draw_width(int w)
Set the intended width of the texture, in draw-space.
Definition: texture.hpp:125
std::shared_ptr< SDL_Texture > texture_
Definition: texture.hpp:214
void set_blend_mode(SDL_BlendMode)
Blend mode.
Definition: texture.cpp:191
bool has_src_
true iff the source rect is valid
Definition: texture.hpp:217
void finalize()
Definition: texture.cpp:85
bool operator==(const texture &t) const
Texture comparisons explicitly only care about the pointer value.
Definition: texture.hpp:199
void reset()
Releases ownership of the managed texture and resets the ptr to null.
Definition: texture.cpp:208
void set_src(const rect &r)
Set the source region of the texture used for drawing operations.
Definition: texture.cpp:129
int w_
Definition: texture.hpp:215
texture()=default
Default ctor.
void set_draw_height(int h)
Set the intended height of the texture, in draw-space.
Definition: texture.hpp:128
point draw_size() const
The size of the texture in draw-space.
Definition: texture.hpp:122
void set_draw_size(int w, int h)
Set the intended size of the texture, in draw-space.
Definition: texture.hpp:131
point get_raw_size() const
The raw internal texture size.
Definition: texture.cpp:112
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:148
int h() const
The draw-space height of the texture, in pixels.
Definition: texture.hpp:114
void assign(SDL_Texture *t)
Replaces ownership of the managed texture with the given one.
Definition: texture.cpp:239
void set_alpha_mod(uint8_t alpha)
Alpha modifier.
Definition: texture.cpp:151
color_t get_color_mod() const
Definition: texture.cpp:181
void clear_src()
Clear the source region.
Definition: texture.hpp:167
void set_src_raw(const rect &r)
Set the source region of the texture used for drawing operations.
Definition: texture.cpp:144
SDL_Texture * get() const
Definition: texture.hpp:196
const info get_info() const
Queries metadata about the texture, such as its dimensions.
Definition: texture.hpp:78
void set_color_mod(uint8_t r, uint8_t g, uint8_t b)
Colour modifier.
Definition: texture.cpp:174
int h_
Definition: texture.hpp:216
logger & info()
Definition: log.cpp:319
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:47
Small wrapper that queries metadata about the provided texture.
Definition: texture.hpp:68
uint32_t format
Definition: texture.hpp:71
info(SDL_Texture *t)
Definition: texture.cpp:251
mock_party p
void set_texture_scale_quality(const char *value)
Sets the texture scale quality.
Definition: texture.hpp:227
#define b