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-2026 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 {
27public:
28 enum class LoadType : uint8_t {
29 NORMAL,
30 HALF,
31 THREADED,
32 };
33
52 explicit Sprite(const ImageData&, double scale,
53 std::optional<std::string_view> filename = std::nullopt);
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();
64
66 void draw() const;
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(const Mat3& modelview, const ShaderProgram* shaderProgram = nullptr) const;
127 void draw(Mat3 modelview, Alpha, const ShaderProgram* 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* shaderProgram = nullptr) const;
227 void drawMesh(const 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
238 // Position and size member variables (from Drawable)
239 Vec2 getPos() const;
240 void setPos(double x, double y);
241 template <class Vect> void setPos(Vect p) {
242 setPos(p.x, p.y);
243 }
244
245 jngl::Vec2 getCenter() const;
246 void setCenter(double x, double y);
247 template <class Vect> void setCenter(Vect c) {
248 setCenter(c.x, c.y);
249 }
250
251 double getLeft() const;
252 void setLeft(double x);
253
254 double getTop() const;
255 void setTop(double y);
256
257 double getRight() const;
258 void setRight(double x);
259
260 double getBottom() const;
261 void setBottom(double y);
262
263 double getX() const;
264 void setX(double);
265
266 double getY() const;
267 void setY(double);
268
269 Vec2 getSize() const;
270
271 float getWidth() const;
272
273 float getHeight() const;
274
275 void drawBoundingBox() const;
276
277 bool contains(jngl::Vec2 point) const;
278
279private:
280 static void cleanUpRowPointers(std::vector<unsigned char*>& buf);
281 void loadTexture(int scaledWidth, int scaledHeight, const std::string& filename, bool halfLoad,
282 unsigned int format, const unsigned char* const* rowPointers,
283 const unsigned char* data = nullptr);
284 Finally LoadPNG(const std::string& filename, FILE* fp, bool halfLoad);
285 struct BMPHeader {
286 unsigned int dataOffset;
287 unsigned int headerSize;
288 int width;
289 int height;
290 unsigned short planes;
291 unsigned short bpp;
292 unsigned int compression;
293 unsigned int dataSize;
294 };
295 Finally LoadBMP(const std::string& filename, FILE* fp, bool halfLoad);
296#ifndef NOWEBP
297 Finally LoadWebP(const std::string& filename, FILE* file, bool halfLoad);
298#endif
299
301
302 // Position in screen coordinates
303 Vec2 position;
304
305 // Width and height in pixel, NOT screen coordinates
306 float width = 0;
307 float height = 0;
308};
309
310void draw(const std::string& filename, double x, double y);
311
312template <class Vect> void draw(const std::string& filename, Vect pos) {
313 draw(filename, pos.x, pos.y);
314}
315
319Finally load(const std::string& filename);
320
321void unload(const std::string& filename);
322
323void unloadAll();
324
325void drawClipped(const std::string& filename, double xposition, double yposition, float xstart,
326 float xend, float ystart, float yend);
327
328void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue,
329 unsigned char alpha);
330
331void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue);
332
334void setSpriteColor(Rgb);
335
337void setSpriteColor(Rgba);
338
339void setSpriteAlpha(unsigned char alpha);
340
341void pushSpriteAlpha(unsigned char alpha = 255);
342
343void popSpriteAlpha();
344
345int getWidth(const std::string& filename);
346
347int getHeight(const std::string& filename);
348
349#if __cplusplus >= 201703L
350[[nodiscard]]
351#endif
352Finally disableBlending();
353
354Finally drawOnlyIntoAlphaChannel();
355
356} // 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, 0 meaning invisible, 1 fully visible.
Definition Alpha.hpp:12
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:16
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 draw() const
Draws the Sprite, centered by default.
Batch batch(const ShaderProgram *shaderProgram=nullptr) const
Allows to draw the Sprite multiple times at different locations in an efficient way.
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...
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...
void step()
Does nothing.
static const Shader & vertexShader()
Returns a reference to JNGL's default vertex shader used to draw textures.
void drawMesh(const Mat3 &modelview, const std::vector< Vertex > &vertexes, const ShaderProgram *shaderProgram=nullptr) const
Draws a list of triangles with the sprite's texture on it, ignores the Sprite's position.
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.