JNGL
sprite.hpp
Go to the documentation of this file.
1 // Copyright 2012-2021 Jan Niklas Hasse <[email protected]>
2 // For conditions of distribution and use, see copyright notice in LICENSE.txt
5 #pragma once
6 
7 #include "Color.hpp"
8 #include "Drawable.hpp"
9 #include "ShaderProgram.hpp"
10 #include "Vec2.hpp"
11 
12 #include <vector>
13 
14 namespace jngl {
15 
16 class Mat3;
17 class Texture;
18 struct Vertex;
19 
21 class Sprite : public Drawable {
22 public:
23  enum class LoadType {
24  NORMAL,
25  HALF,
26  THREADED,
27  };
28 
29  Sprite(const unsigned char* bytes, size_t width, size_t height);
30  explicit Sprite(const std::string& filename, LoadType loadType = LoadType::NORMAL);
31  void step() override;
32  void draw() const override;
33 
37  void draw(Mat3 modelview, const ShaderProgram* = nullptr) const;
38 
39  void draw(const ShaderProgram* shaderProgram) const;
40 
51  [[deprecated("Scale the modelview matrix instead using jngl::Mat3::scale")]]
52  void drawScaled(float xfactor, float yfactor,
53  const ShaderProgram* shaderProgram = nullptr) const;
54 
56  [[deprecated("Use new drawClipped(Vec2, Vec2) method instead")]] void
57  drawClipped(float xstart, float xend, float ystart, float yend) const;
58 
60  void drawClipped(Vec2 start, Vec2 end) const;
61 
63  void drawMesh(const std::vector<Vertex>& vertexes, const ShaderProgram* = nullptr) const;
64 
65  void setBytes(const unsigned char*);
66 
68  static const Shader& vertexShader();
69 
72 
73 private:
74  static void cleanUpRowPointers(std::vector<unsigned char*>& buf);
75  void loadTexture(int scaledWidth, int scaledHeight, const std::string& filename, bool halfLoad,
76  unsigned int format, const unsigned char* const* rowPointers,
77  const unsigned char* data = nullptr);
78  Finally LoadPNG(const std::string& filename, FILE* fp, bool halfLoad);
79  struct BMPHeader {
80  unsigned int dataOffset;
81  unsigned int headerSize;
82  int width;
83  int height;
84  unsigned short planes;
85  unsigned short bpp;
86  unsigned int compression;
87  unsigned int dataSize;
88  };
89  Finally LoadBMP(const std::string& filename, FILE* fp, bool halfLoad);
90 #ifndef NOJPEG
91  Finally LoadJPG(const std::string& filename, FILE* file, bool halfLoad);
92 #endif
93 #ifndef NOWEBP
94  Finally LoadWebP(const std::string& filename, FILE* file, bool halfLoad);
95 #endif
96 
98 };
99 
100 void draw(const std::string& filename, double x, double y);
101 
102 template <class Vect> void draw(const std::string& filename, Vect pos) {
103  draw(filename, pos.x, pos.y);
104 }
105 
109 Finally load(const std::string& filename);
110 
111 void unload(const std::string& filename);
112 
113 void unloadAll();
114 
115 void drawClipped(const std::string& filename, double xposition, double yposition, float xstart,
116  float xend, float ystart, float yend);
117 
118 void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue,
119  unsigned char alpha);
120 
121 void setSpriteColor(unsigned char red, unsigned char green, unsigned char blue);
122 
123 void setSpriteColor(Color);
124 
125 void setSpriteAlpha(unsigned char alpha);
126 
127 void pushSpriteAlpha(unsigned char alpha = 255);
128 
129 void popSpriteAlpha();
130 
131 int getWidth(const std::string& filename);
132 
133 int getHeight(const std::string& filename);
134 
135 #if __cplusplus >= 201703L
136 [[nodiscard]]
137 #endif
138 Finally
139 disableBlending();
140 
141 } // namespace jngl
Contains jngl::Color class.
Contains jngl::Drawable class.
Contains jngl::ShaderProgram class.
Contains jngl::Vec2 class.
Object representing a RGB color.
Definition: Color.hpp:25
Base class for drawable objects with a position and a rectangle size.
Definition: Drawable.hpp:12
Helper class which calls a function when being destroyed.
Definition: Finally.hpp:22
3x3 matrix
Definition: Mat3.hpp:19
Linked vertex and fragment shaders.
Fragment or vertex GLSL shader.
Definition: Shader.hpp:12
Higher-level representation of an image.
Definition: sprite.hpp:21
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 override
Called when drawing a frame.
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:71
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.
static const Shader & vertexShader()
Returns a reference to JNGL's default vertex shader used to draw textures.
void step() override
Advance object's state.
void draw(Mat3 modelview, const ShaderProgram *=nullptr) const
Draws the image centered using modelview.
Two-dimensional vector.
Definition: Vec2.hpp:35
JNGL's main namespace.
Definition: App.hpp:10
jngl::Mat3 modelview()
Returns a copy of the global ModelView matrix.
Finally load(const std::string &filename)
Starts a thread to load filename and returns a Finally which will join it.