JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
Sprite Class Reference

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)
 Creates a Sprite from ImageData and scales it by scale.
 
 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 calculated as width * height * 4.
 
 Sprite (const std::string &filename, LoadType loadType=LoadType::NORMAL)
 
void step () override
 Does nothing.
 
void draw () const override
 Draws the Sprite, centered by default.
 
void draw (Mat3 modelview, const ShaderProgram *=nullptr) const
 Draws the image centered using modelview.
 
void draw (Mat3 modelview, Alpha, const ShaderProgram *=nullptr) const
 
void draw (Mat3 modelview, Rgba color) const
 Draws the sprite but multiplies each pixel's color with 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 (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.
 
void setBytes (const unsigned char *)
 
- Public Member Functions inherited from Drawable
 Drawable (const Drawable &)=default
 
Drawableoperator= (const Drawable &)=default
 
 Drawable (Drawable &&)=default
 
Drawableoperator= (Drawable &&)=default
 
virtual void setPos (double x, double y)
 Sets the position of the top-left of the Drawable.
 
template<class Vect >
void setPos (Vect p)
 Sets the position of the top-left of the Drawable.
 
jngl::Vec2 getCenter () const
 Returns the position of the center of the Drawable.
 
virtual void setCenter (double x, double y)
 Centers the Sprite at (x, y)
 
template<class Vect >
void setCenter (Vect c)
 Centers the Sprite at c (e.g. jngl::Vec2)
 
double getLeft () const
 Returns the distance from the left side of the screen.
 
void setLeft (double x)
 
double getTop () const
 Returns the distance from the top of the screen.
 
void setTop (double y)
 
double getRight () const
 Returns the distance from the right side of the screen.
 
void setRight (double x)
 
double getBottom () const
 Returns the distance from the bottom of the screen.
 
void setBottom (double y)
 
double getX () const
 
void setX (double)
 
double getY () const
 
void setY (double)
 
Vec2 getSize () const
 Returns {width, height} in screen coordinates.
 
float getWidth () const
 Returns the width in screen coordinates.
 
float getHeight () const
 Returns the height in screen coordinates.
 
void drawBoundingBox () const
 Draws a red box around the Drawable.
 
bool contains (jngl::Vec2 point) const
 Returns whether point is inside the bounding box.
 

Static Public Member Functions

static const ShadervertexShader ()
 Returns a reference to JNGL's default vertex shader used to draw textures.
 

Public Attributes

std::shared_ptr< Finallyloader
 Function which actually loads the sprite.
 

Additional Inherited Members

- Protected Attributes inherited from Drawable
Vec2 position
 
float width = 0
 
float height = 0
 

Detailed Description

Higher-level representation of an image.

Definition at line 25 of file sprite.hpp.

Inheritance diagram for Sprite:
[legend]
Collaboration diagram for Sprite:
[legend]

Member Enumeration Documentation

◆ LoadType

enum class LoadType : uint8_t
strong

Definition at line 27 of file sprite.hpp.

Constructor & Destructor Documentation

◆ Sprite() [1/2]

Sprite ( const ImageData & ,
double scale,
std::optional< std::string_view > filename = std::nullopt )
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:

auto img = jngl::ImageData::load("foo.png");
auto sprite2 = std::make_unique<jngl::Sprite>("foo.png");
assert(std::lround(sprite1.getWidth()) == std::lround(sprite2.getWidth()));
static std::unique_ptr< ImageData > load(const std::string &filename, double scaleHint=1.)
Passing a filename will load the specified filename.
double getScaleFactor()
Size of one screen pixel in actual pixels.
T lround(T... args)

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.

◆ Sprite() [2/2]

Sprite ( const std::string & filename,
LoadType loadType = LoadType::NORMAL )
explicit
Deprecated
Use Loader instead

Member Function Documentation

◆ step()

void step ( )
overridevirtual

Does nothing.

Implements Drawable.

◆ draw() [1/4]

void draw ( ) const
overridevirtual

Draws the Sprite, centered by default.

Implements Drawable.

◆ draw() [2/4]

void draw ( Mat3 modelview,
const ShaderProgram * = nullptr ) const

Draws the image centered using modelview.

Parameters
shaderProgramPassing nullptr uses the default.

◆ draw() [3/4]

void draw ( Mat3 modelview,
Rgba color ) const

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.

◆ draw() [4/4]

void draw ( const ShaderProgram * shaderProgram) const

Draws the sprite using the specified shader program.

Parameters
shaderProgramA pointer to the ShaderProgram object to use for drawing.

◆ batch()

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:

void MyWork::draw() const {
auto batch = mySprite.batch();
for (jngl::Vec2 position : positions) {
}
}
void draw(Mat3 modelview) const
Draws the Sprite which created this Batch centered using modelview.
Batch batch(const ShaderProgram *shaderProgram=nullptr) const
Allows to draw the Sprite multiple times at different locations in an efficient way.
Two-dimensional vector.
Definition Vec2.hpp:36
jngl::Mat3 modelview()
Returns a copy of the global ModelView matrix.
void translate(double x, double y)
Multiplies the global ModelView matrix with a translation matrix.
Parameters
shaderProgramPassing nullptr uses the default.

◆ drawScaled()

void drawScaled ( float xfactor,
float yfactor,
const ShaderProgram * shaderProgram = nullptr ) const

Draws the image scaled by xfactor and yfactor

Parameters
xfactorScale width by this factor
yfactorScale height by this factor
shaderProgramPassing 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.

Deprecated
Scale the modelview matrix instead using jngl::Mat3::scale

◆ drawClipped()

void drawClipped ( float xstart,
float xend,
float ystart,
float yend ) const
Deprecated
Use new drawClipped(Vec2, Vec2) method instead

◆ drawMesh()

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.

Example:

// mySprite is a Texture Atlas with two images next to each other.
// First image:
vertexes.emplace_back( 0, 0, 0, 0); // triangle 1
vertexes.emplace_back(10, 0, 0.5, 0);
vertexes.emplace_back(10, 10, 0.5, 1);
vertexes.emplace_back(10, 10, 0.5, 1); // triangle 2
vertexes.emplace_back( 0, 10, 0, 1);
vertexes.emplace_back( 0, 0, 0, 0);
// Second image:
vertexes.emplace_back(30, 0, 0.5, 0); // triangle 1
vertexes.emplace_back(40, 0, 1, 0);
vertexes.emplace_back(40, 10, 1, 1);
vertexes.emplace_back(40, 10, 1, 1); // triangle 2
vertexes.emplace_back(30, 10, 0.5, 1);
vertexes.emplace_back(30, 0, 0.5, 0);
mySprite.drawMesh(vertexes);
Parameters
shaderProgramPassing nullptr uses the default.

Member Data Documentation

◆ loader

Function which actually loads the sprite.

Definition at line 233 of file sprite.hpp.


The documentation for this class was generated from the following file: