Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Texture.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_TEXTURE_H
25 #define GF_TEXTURE_H
26 
27 #include <cstdint>
28 
29 #include "GraphicsApi.h"
30 #include "GraphicsHandle.h"
31 #include "Path.h"
32 #include "Rect.h"
33 #include "Span.h"
34 #include "Vector.h"
35 
36 namespace gf {
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
38 inline namespace v1 {
39 #endif
40 
41  class Image;
42  class InputStream;
43 
48  template<>
49  struct GF_GRAPHICS_API GraphicsTrait<GraphicsTag::Texture> {
50  static void gen(int n, unsigned* resources);
51  static void del(int n, const unsigned* resources);
52  };
53 
79  class GF_GRAPHICS_API BareTexture {
80  public:
86  enum class Format {
87  Color,
88  Alpha,
89  };
90 
98  BareTexture(Format format);
99 
107  BareTexture(Format format, Vector2i size, const uint8_t *data);
108 
114  Format getFormat() const {
115  return m_format;
116  }
117 
125  unsigned getName() const {
126  return m_handle.getName();
127  }
128 
134  Vector2i getSize() const {
135  return m_size;
136  }
137 
152  void setSmooth(bool smooth = true);
153 
161  bool isSmooth() const noexcept {
162  return m_smooth;
163  }
164 
184  void setRepeated(bool repeated = true);
185 
193  bool isRepeated() const noexcept {
194  return m_repeated;
195  }
196 
213  void update(const uint8_t *data);
214 
232  void update(const uint8_t *data, const RectI& rect);
233 
240  void resize(Vector2i size, const uint8_t *data = nullptr);
241 
248  RectF computeTextureCoords(const RectI& rect) const;
249 
269  bool generateMipmap();
270 
278  static void bind(const BareTexture *texture);
279 
280  private:
281  Format m_format;
283  Vector2i m_size;
284  bool m_smooth;
285  bool m_repeated;
286  bool m_mipmap;
287  };
288 
289 
313  class GF_GRAPHICS_API Texture : public BareTexture {
314  public:
320  Texture();
321 
327  Texture(Vector2i size);
328 
334  Texture(const Image& image);
335 
341  Texture(const Path& filename);
342 
348  Texture(InputStream& stream);
349 
355  Texture(Span<const uint8_t> content);
356 
372  void update(const Image& image);
373 
384  Image copyToImage() const;
385 
386  };
387 
388 
395  class GF_GRAPHICS_API AlphaTexture : public BareTexture {
396  public:
402  AlphaTexture();
403 
409  AlphaTexture(Vector2i size);
410  };
411 
412 #ifndef DOXYGEN_SHOULD_SKIP_THIS
413 }
414 #endif
415 }
416 
417 #endif // GF_TEXTURE_H
Format
Format of the texture.
Definition: Texture.h:86
ColorF Color
Instantiation of ColorBase for float
Definition: Color.h:415
bool isRepeated() const noexcept
Check if the texture is repeated or not.
Definition: Texture.h:193
A trait to handle creation and deletion of GPU resources.
Definition: GraphicsHandle.h:48
A texture for colored images.
Definition: Texture.h:313
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
bool isSmooth() const noexcept
Check if the smooth filter is enabled or not.
Definition: Texture.h:161
Class for loading, manipulating and saving images.
Definition: Image.h:80
The namespace for gf classes.
Definition: Action.h:35
An image that lives in the graphic memory that can be used for drawing.
Definition: Texture.h:79
Abstract class for custom file input streams.
Definition: Stream.h:54
Vector2i getSize() const
Return the size of the texture.
Definition: Texture.h:134
unsigned getName() const
Get the internal representation of the texture.
Definition: Texture.h:125
A texture with a single alpha channel.
Definition: Texture.h:395
Format getFormat() const
Get the format of the texture.
Definition: Texture.h:114
GraphicsTag
A tag to represent various GPU resources.
Definition: GraphicsHandle.h:37