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-2024 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 "Rgb.hpp"
9#include "Rgba.hpp"
10#include "ShaderProgram.hpp"
11#include "Vec2.hpp"
12
13#include <future>
14#include <optional>
15#include <vector>
16
17namespace jngl {
18
19class ImageData;
20class Mat3;
21class Texture;
22struct Vertex;
23
25class Sprite : public Drawable {
26public:
27 enum class LoadType : uint8_t {
28 NORMAL,
29 HALF,
30 THREADED,
31 };
32
51 explicit Sprite(const ImageData&, double scale,
53
56 Sprite(const uint8_t* bytes, size_t width, size_t height);
57
59 explicit Sprite(const std::string& filename, LoadType loadType = LoadType::NORMAL);
60
62 void step() override;
63
65 void draw() const override;
66
84 class Loader {
85 public:
90 explicit Loader(std::string filename) noexcept;
91
100 ~Loader() noexcept;
101
105 std::shared_ptr<Sprite> shared() const;
106
110 operator bool() const; // NOLINT
111
115 Sprite* operator->() const;
116
117 private:
118 mutable std::future<std::unique_ptr<ImageData>> imageDataFuture;
119 std::string filename;
120 };
121
125 void draw(Mat3 modelview, const ShaderProgram* = nullptr) const;
126 void draw(Mat3 modelview, Alpha, const ShaderProgram* = nullptr) const;
127
131 void draw(const ShaderProgram* shaderProgram) const;
132
134 class Batch {
135 struct Impl;
137 friend class Sprite;
138
139 public:
141 ~Batch();
142 Batch(Batch&&) = default;
143 Batch& operator=(Batch&&) = default;
144 Batch(const Batch&) = delete;
145 Batch& operator=(const Batch&) = delete;
146
148 void draw(Mat3 modelview) const;
149 };
150
167 Batch batch(const ShaderProgram* shaderProgram = nullptr) const;
168
179 [[deprecated("Scale the modelview matrix instead using jngl::Mat3::scale")]]
180 void drawScaled(float xfactor, float yfactor,
181 const ShaderProgram* shaderProgram = nullptr) const;
182
184 [[deprecated("Use new drawClipped(Vec2, Vec2) method instead")]] void
185 drawClipped(float xstart, float xend, float ystart, float yend) const;
186
188 void drawClipped(Vec2 start, Vec2 end) const;
189
192 void drawMesh(const std::vector<Vertex>& vertexes, const ShaderProgram* = nullptr) const;
193
219 const ShaderProgram* = nullptr) const;
220
221 void setBytes(const unsigned char*);
222
224 static const Shader& vertexShader();
225
228
229private:
230 static void cleanUpRowPointers(std::vector<unsigned char*>& buf);
231 void loadTexture(int scaledWidth, int scaledHeight, const std::string& filename, bool halfLoad,
232 unsigned int format, const unsigned char* const* rowPointers,
233 const unsigned char* data = nullptr);
234 Finally LoadPNG(const std::string& filename, FILE* fp, bool halfLoad);
235 struct BMPHeader {
236 unsigned int dataOffset;
237 unsigned int headerSize;
238 int width;
239 int height;
240 unsigned short planes;
241 unsigned short bpp;
242 unsigned int compression;
243 unsigned int dataSize;
244 };
245 Finally LoadBMP(const std::string& filename, FILE* fp, bool halfLoad);
246#ifndef NOWEBP
247 Finally LoadWebP(const std::string& filename, FILE* file, bool halfLoad);
248#endif
249
251};
252
253void draw(const std::string& filename, double x, double y);
254
255template <class Vect> void draw(const std::string& filename, Vect pos) {
256 draw(filename, pos.x, pos.y);
257}
258
262Finally load(const std::string& filename);
263
264void unload(const std::string& filename);
265
266void unloadAll();
267
268void drawClipped(const std::string& filename, double xposition, double yposition, float xstart,
269 float xend, float ystart, float yend);
270
271void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue,
272 unsigned char alpha);
273
274void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue);
275
277void setSpriteColor(Rgb);
278
280void setSpriteColor(Rgba);
281
282void setSpriteAlpha(unsigned char alpha);
283
284void pushSpriteAlpha(unsigned char alpha = 255);
285
286void popSpriteAlpha();
287
288int getWidth(const std::string& filename);
289
290int getHeight(const std::string& filename);
291
292#if __cplusplus >= 201703L
293[[nodiscard]]
294#endif
296disableBlending();
297
298} // namespace jngl
Contains jngl::Drawable 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:12
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:134
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:84
~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:25
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:227
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.