Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Texture.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "ArrayRef.h"
30 #include "GraphicsHandle.h"
31 #include "Path.h"
32 #include "Portability.h"
33 #include "Rect.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 
44  template<>
45  struct GF_API GraphicsTrait<GraphicsTag::Texture> {
46  static void gen(int n, unsigned* resources);
47  static void del(int n, const unsigned* resources);
48  };
49 
75  class GF_API BareTexture {
76  public:
82  enum class Format {
83  Color,
84  Alpha,
85  };
86 
94  BareTexture(Format format);
95 
103  BareTexture(Format format, Vector2i size, const uint8_t *data);
104 
110  Format getFormat() const {
111  return m_format;
112  }
113 
121  unsigned getName() const {
122  return m_handle.getName();
123  }
124 
130  Vector2i getSize() const {
131  return m_size;
132  }
133 
148  void setSmooth(bool smooth = true);
149 
157  bool isSmooth() const noexcept {
158  return m_smooth;
159  }
160 
180  void setRepeated(bool repeated = true);
181 
189  bool isRepeated() const noexcept {
190  return m_repeated;
191  }
192 
209  void update(const uint8_t *data);
210 
228  void update(const uint8_t *data, const RectI& rect);
229 
236  void resize(Vector2i size, const uint8_t *data = nullptr);
237 
244  RectF computeTextureCoords(const RectI& rect) const;
245 
265  bool generateMipmap();
266 
274  static void bind(const BareTexture *texture);
275 
276  private:
277  Format m_format;
279  Vector2i m_size;
280  bool m_smooth;
281  bool m_repeated;
282  bool m_mipmap;
283  };
284 
285 
309  class GF_API Texture : public BareTexture {
310  public:
316  Texture();
317 
323  Texture(Vector2i size);
324 
330  Texture(const Image& image);
331 
337  Texture(const Path& filename);
338 
344  Texture(InputStream& stream);
345 
351  Texture(ArrayRef<uint8_t> content);
352 
368  void update(const Image& image);
369 
380  Image copyToImage() const;
381 
382  };
383 
384 
391  class GF_API AlphaTexture : public BareTexture {
392  public:
398  AlphaTexture();
399 
405  AlphaTexture(Vector2i size);
406  };
407 
408 #ifndef DOXYGEN_SHOULD_SKIP_THIS
409 }
410 #endif
411 }
412 
413 #endif // GF_TEXTURE_H
Format
Format of the texture.
Definition: Texture.h:82
bool isRepeated() const noexcept
Check if the texture is repeated or not.
Definition: Texture.h:189
Definition: GraphicsHandle.h:41
A texture for colored images.
Definition: Texture.h:309
bool isSmooth() const noexcept
Check if the smooth filter is enabled or not.
Definition: Texture.h:157
GraphicsTag
Definition: GraphicsHandle.h:34
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:75
Abstract class for custom file input streams.
Definition: Stream.h:55
ColorF Color
Instantiation of ColorBase for float
Definition: Color.h:416
Vector2i getSize() const
Return the size of the texture.
Definition: Texture.h:130
unsigned getName() const
Get the internal representation of the texture.
Definition: Texture.h:121
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:44
A texture with a single alpha channel.
Definition: Texture.h:391
Format getFormat() const
Get the format of the texture.
Definition: Texture.h:110