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
57 explicit Sprite(const ImageData&, double scale,
58 std::optional<std::string_view> filename = std::nullopt, bool mipmap = false);
59
64 Sprite(const uint8_t* bytes, size_t width, size_t height, bool mipmap = false);
65
68 explicit Sprite(const std::string& filename, LoadType loadType = LoadType::NORMAL,
69 bool mipmap = false);
70
73 explicit Sprite(const std::string& filename, bool mipmap);
74
76 void step();
77
79 void draw() const;
80
98 class Loader {
99 public:
108 explicit Loader(std::string filename, bool mipmap = false) noexcept;
109
118 ~Loader() noexcept;
119
123 std::shared_ptr<Sprite> shared() const;
124
128 operator bool() const; // NOLINT
129
133 Sprite* operator->() const;
134
135 private:
136 mutable std::future<std::unique_ptr<ImageData>> imageDataFuture;
137 std::string filename;
138 bool mipmap = false;
139 };
140
144 void draw(const Mat3& modelview, const ShaderProgram* shaderProgram = nullptr) const;
145 void draw(Mat3 modelview, Alpha, const ShaderProgram* shaderProgram = nullptr) const;
146
151 void draw(Mat3 modelview, Rgba color) const;
152
170 void drawLerped(Mat3 modelview, Rgb color, float red, float green, float blue, Alpha) const;
171
175 void draw(const ShaderProgram* shaderProgram) const;
176
178 class Batch {
179 struct Impl;
181 friend class Sprite;
182
183 public:
185 ~Batch();
186 Batch(Batch&&) = default;
187 Batch& operator=(Batch&&) = default;
188 Batch(const Batch&) = delete;
189 Batch& operator=(const Batch&) = delete;
190
192 void draw(Mat3 modelview) const;
193 };
194
211 Batch batch(const ShaderProgram* shaderProgram = nullptr) const;
212
223 [[deprecated("Scale the modelview matrix instead using jngl::Mat3::scale")]]
224 void drawScaled(float xfactor, float yfactor,
225 const ShaderProgram* shaderProgram = nullptr) const;
226
228 [[deprecated("Use new drawClipped(Vec2, Vec2) method instead")]] void
229 drawClipped(float xstart, float xend, float ystart, float yend) const;
230
232 void drawClipped(Vec2 start, Vec2 end) const;
233
236 void drawMesh(const std::vector<Vertex>& vertexes, const ShaderProgram* = nullptr) const;
237
262 void drawMesh(const Mat3& modelview, const std::vector<Vertex>& vertexes,
263 const ShaderProgram* shaderProgram = nullptr) const;
264 void drawMesh(const Mat3& modelview, const std::vector<Vertex>& vertexes, jngl::Rgba color,
265 const ShaderProgram* = nullptr) const;
266
267 void setBytes(const unsigned char*);
268
270 static const Shader& vertexShader();
271
274
275 // Position and size member variables (from Drawable)
276 Vec2 getPos() const;
277 void setPos(double x, double y);
278 template <class Vect> void setPos(Vect p) {
279 setPos(p.x, p.y);
280 }
281
282 jngl::Vec2 getCenter() const;
283 void setCenter(double x, double y);
284 template <class Vect> void setCenter(Vect c) {
285 setCenter(c.x, c.y);
286 }
287
288 double getLeft() const;
289 void setLeft(double x);
290
291 double getTop() const;
292 void setTop(double y);
293
294 double getRight() const;
295 void setRight(double x);
296
297 double getBottom() const;
298 void setBottom(double y);
299
300 double getX() const;
301 void setX(double);
302
303 double getY() const;
304 void setY(double);
305
306 Vec2 getSize() const;
307
308 float getWidth() const;
309
310 float getHeight() const;
311
312 void drawBoundingBox() const;
313
314 bool contains(jngl::Vec2 point) const;
315
316private:
317 void enableMipmaps();
318 static void cleanUpRowPointers(std::vector<unsigned char*>& buf);
319 void loadTexture(int scaledWidth, int scaledHeight, const std::string& filename, bool halfLoad,
320 unsigned int format, const unsigned char* const* rowPointers,
321 const unsigned char* data = nullptr, bool mipmap = false);
322 Finally LoadPNG(const std::string& filename, FILE* fp, bool halfLoad, bool mipmap);
323 struct BMPHeader {
324 unsigned int dataOffset;
325 unsigned int headerSize;
326 int width;
327 int height;
328 unsigned short planes;
329 unsigned short bpp;
330 unsigned int compression;
331 unsigned int dataSize;
332 };
333 Finally LoadBMP(const std::string& filename, FILE* fp, bool halfLoad, bool mipmap);
334#ifndef NOWEBP
335 Finally LoadWebP(const std::string& filename, FILE* file, bool halfLoad, bool mipmap);
336#endif
337
339
340 // Position in screen coordinates
341 Vec2 position;
342
343 // Width and height in pixel, NOT screen coordinates
344 float width = 0;
345 float height = 0;
346};
347
348void draw(const std::string& filename, double x, double y);
349
350template <class Vect> void draw(const std::string& filename, Vect pos) {
351 draw(filename, pos.x, pos.y);
352}
353
357Finally load(const std::string& filename);
358
359void unload(const std::string& filename);
360
361void unloadAll();
362
363void drawClipped(const std::string& filename, double xposition, double yposition, float xstart,
364 float xend, float ystart, float yend);
365
366void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue,
367 unsigned char alpha);
368
369void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue);
370
372void setSpriteColor(Rgb);
373
375void setSpriteColor(Rgba);
376
377void setSpriteAlpha(unsigned char alpha);
378
379void pushSpriteAlpha(unsigned char alpha = 255);
380
381void popSpriteAlpha();
382
383int getWidth(const std::string& filename);
384
385int getHeight(const std::string& filename);
386
387#if __cplusplus >= 201703L
388[[nodiscard]]
389#endif
390Finally disableBlending();
391
392Finally drawOnlyIntoAlphaChannel();
393
395enum class BlendMode : uint8_t {
401 Composite,
402
406};
407
412#if __cplusplus >= 201703L
413[[nodiscard]]
414#endif
416
417} // 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:14
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:21
Object representing a RGBA color.
Definition Rgba.hpp:22
Linked vertex and fragment shaders.
Fragment or vertex GLSL shader.
Definition Shader.hpp:13
While this object is alive, don't do any other draw calls. Should never outlive its Sprite.
Definition Sprite.hpp:178
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:98
Loader(std::string filename, bool mipmap=false) 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
Sprite(const std::string &filename, LoadType loadType=LoadType::NORMAL, bool mipmap=false)
std::shared_ptr< Finally > loader
Function which actually loads the sprite.
Definition Sprite.hpp:273
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 std::string &filename, bool mipmap)
Sprite(const ImageData &, double scale, std::optional< std::string_view > filename=std::nullopt, bool mipmap=false)
Creates a Sprite from ImageData and scales it by scale.
void step()
Does nothing.
static const Shader & vertexShader()
Returns a reference to JNGL's default vertex shader used to draw textures.
Sprite(const uint8_t *bytes, size_t width, size_t height, bool mipmap=false)
The sprite data is stored as packed RGBA bytes in an array, where the size of the array needs to be c...
void drawLerped(Mat3 modelview, Rgb color, float red, float green, float blue, Alpha) const
Draws the Sprite with each color channel linearly interpolated towards color.
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.
Two-dimensional vector.
Definition Vec2.hpp:36
JNGL's main namespace.
BlendMode
How what's drawn is blended with what's already there, see setBlendMode()
Definition Sprite.hpp:395
@ Composite
Colours are blended by their alpha as usual, but alpha is composited too, rather than added up as it ...
@ Premultiplied
For colours that already have their alpha multiplied in, e.g.
Finally setBlendMode(BlendMode)
Sets how what's drawn is blended with what's already there, until the returned Finally object is dest...
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.