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
225 void drawMesh(const Mat3& modelview, const std::vector<Vertex>& vertexes,
226 const ShaderProgram* = nullptr) const;
227 void drawMesh(Mat3 modelview, const std::vector<Vertex>& vertexes, jngl::Rgba color,
228 const ShaderProgram* = nullptr) const;
229
230 void setBytes(const unsigned char*);
231
233 static const Shader& vertexShader();
234
237
238private:
239 static void cleanUpRowPointers(std::vector<unsigned char*>& buf);
240 void loadTexture(int scaledWidth, int scaledHeight, const std::string& filename, bool halfLoad,
241 unsigned int format, const unsigned char* const* rowPointers,
242 const unsigned char* data = nullptr);
243 Finally LoadPNG(const std::string& filename, FILE* fp, bool halfLoad);
244 struct BMPHeader {
245 unsigned int dataOffset;
246 unsigned int headerSize;
247 int width;
248 int height;
249 unsigned short planes;
250 unsigned short bpp;
251 unsigned int compression;
252 unsigned int dataSize;
253 };
254 Finally LoadBMP(const std::string& filename, FILE* fp, bool halfLoad);
255#ifndef NOWEBP
256 Finally LoadWebP(const std::string& filename, FILE* file, bool halfLoad);
257#endif
258
260};
261
262void draw(const std::string& filename, double x, double y);
263
264template <class Vect> void draw(const std::string& filename, Vect pos) {
265 draw(filename, pos.x, pos.y);
266}
267
271Finally load(const std::string& filename);
272
273void unload(const std::string& filename);
274
275void unloadAll();
276
277void drawClipped(const std::string& filename, double xposition, double yposition, float xstart,
278 float xend, float ystart, float yend);
279
280void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue,
281 unsigned char alpha);
282
283void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue);
284
286void setSpriteColor(Rgb);
287
289void setSpriteColor(Rgba);
290
291void setSpriteAlpha(unsigned char alpha);
292
293void pushSpriteAlpha(unsigned char alpha = 255);
294
295void popSpriteAlpha();
296
297int getWidth(const std::string& filename);
298
299int getHeight(const std::string& filename);
300
301#if __cplusplus >= 201703L
302[[nodiscard]]
303#endif
305disableBlending();
306
307Finally drawOnlyIntoAlphaChannel();
308
309} // 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:15
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.
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:236
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...
void drawMesh(const 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.
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.