Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Texture.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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
36namespace gf {
37#ifndef DOXYGEN_SHOULD_SKIP_THIS
38inline 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
99
107 BareTexture(Format format, Vector2i size, const uint8_t *data);
108
115 return m_format;
116 }
117
125 unsigned getName() const {
126 return m_handle.getName();
127 }
128
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
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:
321
328
334 Texture(const Image& image);
335
342 Texture(const Image& image, const RectI& area);
343
349 Texture(const Path& filename);
350
357 Texture(const Path& filename, const RectI& area);
358
365
372 Texture(InputStream& stream, const RectI& area);
373
380
387 Texture(Span<const uint8_t> content, const RectI& area);
388
404 void update(const Image& image);
405
417
418 };
419
420
427 class GF_GRAPHICS_API AlphaTexture : public BareTexture {
428 public:
435
442 };
443
444#ifndef DOXYGEN_SHOULD_SKIP_THIS
445}
446#endif
447}
448
449#endif // GF_TEXTURE_H
A texture with a single alpha channel.
Definition: Texture.h:427
AlphaTexture()
Constructor.
AlphaTexture(Vector2i size)
Create the texture.
An image that lives in the graphic memory that can be used for drawing.
Definition: Texture.h:79
RectF computeTextureCoords(const RectI &rect) const
Compute normalized texture coordinates.
bool generateMipmap()
Generate a mipmap using the current texture data.
void setRepeated(bool repeated=true)
Enable or disable repeating.
unsigned getName() const
Get the internal representation of the texture.
Definition: Texture.h:125
Format getFormat() const
Get the format of the texture.
Definition: Texture.h:114
BareTexture(Format format, Vector2i size, const uint8_t *data)
Create the texture.
void setSmooth(bool smooth=true)
Enable or disable the smooth filter.
void update(const uint8_t *data, const RectI &rect)
Update a part of the texture from an array of pixels.
void update(const uint8_t *data)
Update the whole texture from an array of pixels.
Format
Format of the texture.
Definition: Texture.h:86
void resize(Vector2i size, const uint8_t *data=nullptr)
Resize a texture.
static void bind(const BareTexture *texture)
Bind a texture for rendering.
bool isSmooth() const noexcept
Check if the smooth filter is enabled or not.
Definition: Texture.h:161
bool isRepeated() const noexcept
Check if the texture is repeated or not.
Definition: Texture.h:193
BareTexture(Format format)
Constructor.
Vector2i getSize() const
Return the size of the texture.
Definition: Texture.h:134
Class for loading, manipulating and saving images.
Definition: Image.h:81
Abstract class for custom file input streams.
Definition: Stream.h:54
A texture for colored images.
Definition: Texture.h:313
Image copyToImage() const
Copy the texture pixels to an image.
Texture(Vector2i size)
Create the texture.
Texture()
Constructor.
void update(const Image &image)
Update the texture from an image.
Texture(InputStream &stream)
Load the texture from a custom stream.
Texture(const Path &filename)
Load the texture from a file on disk.
Texture(InputStream &stream, const RectI &area)
Load the texture from a sub-area of a custom stream.
Texture(Span< const uint8_t > content)
Load the texture from a file in memory.
Texture(const Image &image)
Load the texture from an image.
Texture(const Image &image, const RectI &area)
Load the texture from a sub-area of an image.
Texture(const Path &filename, const RectI &area)
Load the texture from a sub-area of a file on disk.
Texture(Span< const uint8_t > content, const RectI &area)
Load the texture from a sub-area of a file in memory.
ColorF Color
Instantiation of ColorBase for float
Definition: Color.h:415
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
GraphicsTag
A tag to represent various GPU resources.
Definition: GraphicsHandle.h:37
The namespace for gf classes.
static void del(int n, const unsigned *resources)
static void gen(int n, unsigned *resources)
A trait to handle creation and deletion of GPU resources.
Definition: GraphicsHandle.h:48