Gamedev Framework (gf)  0.8.0
A C++14 framework for 2D games
Texture.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 "Path.h"
30 #include "Portability.h"
31 #include "Rect.h"
32 #include "Vector.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
39  class Image;
40  class InputStream;
41 
67  class GF_API BareTexture {
68  public:
74  enum class Format {
75  Color,
76  Alpha,
77  };
78 
86  BareTexture(Format format);
87 
91  ~BareTexture();
92 
96  BareTexture(const BareTexture&) = delete;
97 
101  BareTexture& operator=(const BareTexture&) = delete;
102 
106  BareTexture(BareTexture&& other) noexcept;
107 
111  BareTexture& operator=(BareTexture&& other) noexcept;
112 
118  Format getFormat() const {
119  return m_format;
120  }
121 
129  unsigned getName() const {
130  return m_name;
131  }
132 
138  Vector2u getSize() const {
139  return m_size;
140  }
141 
156  void setSmooth(bool smooth = true);
157 
165  bool isSmooth() const noexcept {
166  return m_smooth;
167  }
168 
188  void setRepeated(bool repeated = true);
189 
197  bool isRepeated() const noexcept {
198  return m_repeated;
199  }
200 
217  void update(const uint8_t *data);
218 
236  void update(const uint8_t *data, const RectU& rect);
237 
244  RectF computeTextureCoords(const RectU& rect) const;
245 
265  bool generateMipmap();
266 
274  static void bind(const BareTexture *texture);
275 
276  protected:
287  bool create(Vector2u size, const uint8_t *data);
288 
289  private:
290  Format m_format;
291  unsigned m_name;
292  Vector2u m_size;
293  bool m_smooth;
294  bool m_repeated;
295  bool m_mipmap;
296  };
297 
298 
339  class GF_API Texture : public BareTexture {
340  public:
346  Texture();
347 
357  bool create(Vector2u size);
358 
370  bool loadFromImage(const Image& image);
371 
391  bool loadFromFile(const Path& filename);
392 
411  bool loadFromStream(InputStream& stream);
412 
432  bool loadFromMemory(const uint8_t *data, std::size_t length);
433 
449  void update(const Image& image);
450 
463  Image copyToImage() const;
464 
465  };
466 
467 
474  class GF_API AlphaTexture : public BareTexture {
475  public:
481  AlphaTexture();
482 
492  bool create(Vector2u size);
493  };
494 
495 #ifndef DOXYGEN_SHOULD_SKIP_THIS
496 }
497 #endif
498 }
499 
500 #endif // GF_TEXTURE_H
Format
Format of the texture.
Definition: Texture.h:74
bool isRepeated() const noexcept
Check if the texture is repeated or not.
Definition: Texture.h:197
A texture for colored images.
Definition: Texture.h:339
bool isSmooth() const noexcept
Check if the smooth filter is enabled or not.
Definition: Texture.h:165
Class for loading, manipulating and saving images.
Definition: Image.h:92
The namespace for gf classes.
Definition: Action.h:34
An image that lives in the graphic memory that can be used for drawing.
Definition: Texture.h:67
Predefined colors.
Definition: Color.h:44
Abstract class for custom file input streams.
Definition: InputStream.h:51
unsigned getName() const
Get the internal representation of the texture.
Definition: Texture.h:129
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:41
A texture with a single alpha channel.
Definition: Texture.h:474
Format getFormat() const
Get the format of the texture.
Definition: Texture.h:118
Vector2u getSize() const
Return the size of the texture.
Definition: Texture.h:138