|
JNGL
Easy to use cross-platform 2D game library
|
Higher-level representation of an image. More...
#include <jngl/Sprite.hpp>
Classes | |
| class | Batch |
| While this object is alive, don't do any other draw calls. Should never outlive its Sprite. More... | |
| class | Loader |
| Use this class to load a Sprite asynchronously. More... | |
Public Types | |
| enum class | LoadType : uint8_t { NORMAL , HALF , THREADED } |
Public Member Functions | |
| 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. | |
| 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 calculated as width * height * 4. | |
| Sprite (const std::string &filename, LoadType loadType=LoadType::NORMAL, bool mipmap=false) | |
| Sprite (const std::string &filename, bool mipmap) | |
| void | step () |
| Does nothing. | |
| void | draw () const |
| Draws the Sprite, centered by default. | |
| void | draw (const Mat3 &modelview, const ShaderProgram *shaderProgram=nullptr) const |
| Draws the image centered using modelview. | |
| void | draw (Mat3 modelview, Alpha, const ShaderProgram *shaderProgram=nullptr) const |
| void | draw (Mat3 modelview, Rgba color) const |
| Draws the sprite but multiplies each pixel's color with color. | |
| 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 | draw (const ShaderProgram *shaderProgram) const |
| Draws the sprite using the specified shader program. | |
| 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 | |
| void | drawClipped (float xstart, float xend, float ystart, float yend) const |
| void | drawClipped (Vec2 start, Vec2 end) const |
| Draw a cutout of the sprite. drawClipped({0, 0}, {1, 1}) would draw it normally. | |
| 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::modelview() | |
| 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. | |
| void | drawMesh (const Mat3 &modelview, const std::vector< Vertex > &vertexes, jngl::Rgba color, const ShaderProgram *=nullptr) const |
| void | setBytes (const unsigned char *) |
| Vec2 | getPos () const |
| void | setPos (double x, double y) |
| template<class Vect > | |
| void | setPos (Vect p) |
| jngl::Vec2 | getCenter () const |
| void | setCenter (double x, double y) |
| template<class Vect > | |
| void | setCenter (Vect c) |
| double | getLeft () const |
| void | setLeft (double x) |
| double | getTop () const |
| void | setTop (double y) |
| double | getRight () const |
| void | setRight (double x) |
| double | getBottom () const |
| void | setBottom (double y) |
| double | getX () const |
| void | setX (double) |
| double | getY () const |
| void | setY (double) |
| Vec2 | getSize () const |
| float | getWidth () const |
| float | getHeight () const |
| void | drawBoundingBox () const |
| bool | contains (jngl::Vec2 point) const |
| void | setTextureFilter (TextureFilter minification, TextureFilter magnification) |
| Sets how the texture is sampled when the sprite is drawn smaller than its pixel size (minification) and bigger (magnification). | |
Static Public Member Functions | |
| static const Shader & | vertexShader () |
| Returns a reference to JNGL's default vertex shader used to draw textures. | |
Public Attributes | |
| std::shared_ptr< Finally > | loader |
| Function which actually loads the sprite. | |
Higher-level representation of an image.
Definition at line 27 of file Sprite.hpp.
|
strong |
Definition at line 29 of file Sprite.hpp.
|
explicit |
Creates a Sprite from ImageData and scales it by scale.
When you want to draw an ImageData that hasn't been scaled, make sure to pass JNGL's scale factor so that it appears as the same size independent of the resolution of the user.
Example:
You may pass a filename, then JNGL will use that as a key for its internal texture cache, meaning that if there's already a file loaded with that name, it won't upload the passed ImageData to the GPU again. There is one texture per filename: once mipmap is true for that name, every later load shares the mipmapped texture.
| mipmap | Generate mipmaps so the sprite filters cleanly when drawn smaller than its pixel size. Minification is trilinear. On OpenGL ES 2.0 the image's width and height must both be powers of two. |
| 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 calculated as width * height * 4.
| mipmap | Generate mipmaps for minification. See Sprite(const ImageData&, double, ...). |
|
explicit |
| mipmap | Generate mipmaps for minification. See Sprite(const ImageData&, double, ...). |
|
explicit |
| mipmap | Generate mipmaps for minification. See Sprite(const ImageData&, double, ...). |
| void draw | ( | const Mat3 & | modelview, |
| const ShaderProgram * | shaderProgram = nullptr ) const |
Draws the image centered using modelview.
| shaderProgram | Passing nullptr uses the default. |
Draws the sprite but multiplies each pixel's color with color.
If the sprite is mostly white, this will make it appear in the specified color. If it's black, nothing will change.
Draws the Sprite with each color channel linearly interpolated towards color.
| red | 0.0f (red channel unchanged) ... 1.0f (red channel of color) |
| green | 0.0f (green channel unchanged) ... 1.0f (green channel of color) |
| blue | 0.0f (blue channel unchanged) ... 1.0f (blue channel of color) |
Unlike draw(Mat3, Rgba), which multiplies, this also works for black Sprites. The color set by jngl::setSpriteColor is ignored.
| void draw | ( | const ShaderProgram * | shaderProgram | ) | const |
Draws the sprite using the specified shader program.
| shaderProgram | A pointer to the ShaderProgram object to use for drawing. |
| Batch batch | ( | const ShaderProgram * | shaderProgram = nullptr | ) | const |
Allows to draw the Sprite multiple times at different locations in an efficient way.
This is pureley a performance optimisation. If it's fast enough, use Sprite::draw instead.
Example:
| shaderProgram | Passing nullptr uses the default. |
| void drawScaled | ( | float | xfactor, |
| float | yfactor, | ||
| const ShaderProgram * | shaderProgram = nullptr ) const |
Draws the image scaled by xfactor and yfactor
| xfactor | Scale width by this factor |
| yfactor | Scale height by this factor |
| shaderProgram | Passing nullptr uses the default. |
drawScaled(1, 1) would draw it normally. You can pass negative values to flip the image. For example drawScaled(-1, 1) would draw the image horizontally flipped.
| void drawClipped | ( | float | xstart, |
| float | xend, | ||
| float | ystart, | ||
| float | yend ) const |
| 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.
Example:
| shaderProgram | Passing nullptr uses the default. |
|
inline |
Definition at line 279 of file Sprite.hpp.
|
inline |
Definition at line 285 of file Sprite.hpp.
| void setTextureFilter | ( | TextureFilter | minification, |
| TextureFilter | magnification ) |
Sets how the texture is sampled when the sprite is drawn smaller than its pixel size (minification) and bigger (magnification).
With mipmaps, minification also says how they're sampled. All sprites of the same texture, e.g. of the same file, share this.
| std::shared_ptr<Finally> loader |
Function which actually loads the sprite.
Definition at line 274 of file Sprite.hpp.