Gamedev Framework (gf)  0.14.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  RectF computeTextureCoords(const RectI& rect) const;
237 
257  bool generateMipmap();
258 
266  static void bind(const BareTexture *texture);
267 
268  private:
269  Format m_format;
271  Vector2i m_size;
272  bool m_smooth;
273  bool m_repeated;
274  bool m_mipmap;
275  };
276 
277 
301  class GF_API Texture : public BareTexture {
302  public:
308  Texture();
309 
315  Texture(Vector2i size);
316 
322  Texture(const Image& image);
323 
329  Texture(const Path& filename);
330 
336  Texture(InputStream& stream);
337 
343  Texture(ArrayRef<uint8_t> content);
344 
360  void update(const Image& image);
361 
372  Image copyToImage() const;
373 
374  };
375 
376 
383  class GF_API AlphaTexture : public BareTexture {
384  public:
390  AlphaTexture();
391 
397  AlphaTexture(Vector2i size);
398  };
399 
400 #ifndef DOXYGEN_SHOULD_SKIP_THIS
401 }
402 #endif
403 }
404 
405 #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:301
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:54
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:383
Format getFormat() const
Get the format of the texture.
Definition: Texture.h:110