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