JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
sprite.hpp
Go to the documentation of this file.
1// Copyright 2012-2025 Jan Niklas Hasse <jhasse@bixense.com>
2// For conditions of distribution and use, see copyright notice in LICENSE.txt
5#pragma once
6
7#include "Drawable.hpp"
8#include "Finally.hpp"
9#include "Rgb.hpp"
10#include "Rgba.hpp"
11#include "ShaderProgram.hpp"
12#include "Vec2.hpp"
13
14#include <future>
15#include <optional>
16#include <vector>
17
18namespace jngl {
19
20class ImageData;
21class Mat3;
22class Texture;
23struct Vertex;
24
26class Sprite : public Drawable {
27public:
28 enum class LoadType : uint8_t {
29 NORMAL,
30 HALF,
31 THREADED,
32 };
33
52 explicit Sprite(const ImageData&, double scale,
54
57 Sprite(const uint8_t* bytes, size_t width, size_t height);
58
60 explicit Sprite(const std::string& filename, LoadType loadType = LoadType::NORMAL);
61
63 void step() override;
64
66 void draw() const override;
67
85 class Loader {
86 public:
91 explicit Loader(std::string filename) noexcept;
92
101 ~Loader() noexcept;
102
106 std::shared_ptr<Sprite> shared() const;
107
111 operator bool() const; // NOLINT
112
116 Sprite* operator->() const;
117
118 private:
119 mutable std::future<std::unique_ptr<ImageData>> imageDataFuture;
120 std::string filename;
121 };
122
126 void draw(Mat3 modelview, const ShaderProgram* = nullptr) const;
127 void draw(Mat3 modelview, Alpha, const ShaderProgram* = nullptr) const;
128
133 void draw(Mat3 modelview, Rgba color) const;
134
138 void draw(const ShaderProgram* shaderProgram) const;
139
141 class Batch {
142 struct Impl;
144 friend class Sprite;
145
146 public:
148 ~Batch();
149 Batch(Batch&&) = default;
150 Batch& operator=(Batch&&) = default;
151 Batch(const Batch&) = delete;
152 Batch& operator=(const Batch&) = delete;
153
155 void draw(Mat3 modelview) const;
156 };
157
174 Batch batch(const ShaderProgram* shaderProgram = nullptr) const;
175
186 [[deprecated("Scale the modelview matrix instead using jngl::Mat3::scale")]]
187 void drawScaled(float xfactor, float yfactor,
188 const ShaderProgram* shaderProgram = nullptr) const;
189
191 [[deprecated("Use new drawClipped(Vec2, Vec2) method instead")]] void
192 drawClipped(float xstart, float xend, float ystart, float yend) const;
193
195 void drawClipped(Vec2 start, Vec2 end) const;
196
199 void drawMesh(const std::vector<Vertex>& vertexes, const ShaderProgram* = nullptr) const;
200
226 const ShaderProgram* = nullptr) const;
227
228 void setBytes(const unsigned char*);
229
231 static const Shader& vertexShader();
232
235
236private:
237 static void cleanUpRowPointers(std::vector<unsigned char*>& buf);
238 void loadTexture(int scaledWidth, int scaledHeight, const std::string& filename, bool halfLoad,
239 unsigned int format, const unsigned char* const* rowPointers,
240 const unsigned char* data = nullptr);
241 Finally LoadPNG(const std::string& filename, FILE* fp, bool halfLoad);
242 struct BMPHeader {
243 unsigned int dataOffset;
244 unsigned int headerSize;
245 int width;
246 int height;
247 unsigned short planes;
248 unsigned short bpp;
249 unsigned int compression;
250 unsigned int dataSize;
251 };
252 Finally LoadBMP(const std::string& filename, FILE* fp, bool halfLoad);
253#ifndef NOWEBP
254 Finally LoadWebP(const std::string& filename, FILE* file, bool halfLoad);
255#endif
256
258};
259
260void draw(const std::string& filename, double x, double y);
261
262template <class Vect> void draw(const std::string& filename, Vect pos) {
263 draw(filename, pos.x, pos.y);
264}
265
269Finally load(const std::string& filename);
270
271void unload(const std::string& filename);
272
273void unloadAll();
274
275void drawClipped(const std::string& filename, double xposition, double yposition, float xstart,
276 float xend, float ystart, float yend);
277
278void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue,
279 unsigned char alpha);
280
281void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue);
282
284void setSpriteColor(Rgb);
285
287void setSpriteColor(Rgba);
288
289void setSpriteAlpha(unsigned char alpha);
290
291void pushSpriteAlpha(unsigned char alpha = 255);
292
293void popSpriteAlpha();
294
295int getWidth(const std::string& filename);
296
297int getHeight(const std::string& filename);
298
299#if __cplusplus >= 201703L
300[[nodiscard]]
301#endif
303disableBlending();
304
305} // namespace jngl
Contains jngl::Drawable class.
Contains jngl::Finally class.
Contains jngl::Rgb class.
Contains jngl::Rgba class.
Contains jngl::ShaderProgram class.
Contains jngl::Vec2 class.
Object representing only the Alpha Channel in an RGBA color.
Definition Alpha.hpp:11
Base class for drawable objects with a position and a rectangle size.
Definition Drawable.hpp:12
float getWidth() const
Returns the width in screen coordinates.
float getHeight() const
Returns the height in screen coordinates.
Helper class which calls a function when being destroyed.
Definition Finally.hpp:22
Containing the pixel data of an image file.
Definition ImageData.hpp:13
3x3 matrix
Definition Mat3.hpp:18
Object representing a RGB color, new version of jngl::Color (which will be deprecated in the future)
Definition Rgb.hpp:15
Object representing a RGBA color.
Definition Rgba.hpp:22
Linked vertex and fragment shaders.
Fragment or vertex GLSL shader.
Definition Shader.hpp:12
While this object is alive, don't do any other draw calls. Should never outlive its Sprite.
Definition sprite.hpp:141
void draw(Mat3 modelview) const
Draws the Sprite which created this Batch centered using modelview.
Use this class to load a Sprite asynchronously.
Definition sprite.hpp:85
~Loader() noexcept
Blocks until the Sprite has been loaded.
Loader(std::string filename) noexcept
Starts a thread to load filename and returns instantly.
std::shared_ptr< Sprite > shared() const
Blocks until the Sprite has been loaded and returns a non-nullptr std::shared_ptr.
Higher-level representation of an image.
Definition sprite.hpp:26
void drawClipped(Vec2 start, Vec2 end) const
Draw a cutout of the sprite. drawClipped({0, 0}, {1, 1}) would draw it normally.
void drawMesh(Mat3 modelview, const std::vector< Vertex > &vertexes, const ShaderProgram *=nullptr) const
Draws a list of triangles with the sprite's texture on it, ignores the Sprite's position.
Batch batch(const ShaderProgram *shaderProgram=nullptr) const
Allows to draw the Sprite multiple times at different locations in an efficient way.
void draw() const override
Draws the Sprite, centered by default.
void drawScaled(float xfactor, float yfactor, const ShaderProgram *shaderProgram=nullptr) const
Draws the image scaled by xfactor and yfactor
std::shared_ptr< Finally > loader
Function which actually loads the sprite.
Definition sprite.hpp:234
void drawClipped(float xstart, float xend, float ystart, float yend) const
void drawMesh(const std::vector< Vertex > &vertexes, const ShaderProgram *=nullptr) const
Draws a list of triangles with the sprite's texture on it using the global modelview from jngl::model...
Sprite(const uint8_t *bytes, size_t width, size_t height)
The sprite data is stored as packed RGBA bytes in an array, where the size of the array needs to be c...
static const Shader & vertexShader()
Returns a reference to JNGL's default vertex shader used to draw textures.
void step() override
Does nothing.
Sprite(const ImageData &, double scale, std::optional< std::string_view > filename=std::nullopt)
Creates a Sprite from ImageData and scales it by scale.
Sprite(const std::string &filename, LoadType loadType=LoadType::NORMAL)
Two-dimensional vector.
Definition Vec2.hpp:36
JNGL's main namespace.
void scale(double factor)
Multiplies the global ModelView matrix by a scaling matrix.
Finally load(const std::string &filename)
Starts a thread to load filename and returns a Finally which will join it.
jngl::Mat3 modelview()
Returns a copy of the global ModelView matrix.