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

Use this class to load a Sprite asynchronously. More...

#include <jngl/sprite.hpp>

Public Member Functions

 Loader (std::string filename) noexcept
 Starts a thread to load filename and returns instantly. More...
 
 ~Loader () noexcept
 Blocks until the Sprite has been loaded. More...
 
std::shared_ptr< Spriteshared () const
 Blocks until the Sprite has been loaded and returns a non-nullptr std::shared_ptr. More...
 
 operator bool () const
 Returns whether the sprite has been loaded, does NOT block. More...
 
Spriteoperator-> () const
 Blocks until the Sprite has been loaded. More...
 

Detailed Description

Use this class to load a Sprite asynchronously.

Example:

class MyGame : public jngl::Work {
void step() override {}
void draw() const override {
if (mySprite) {
mySprite->draw();
} else {
jngl::print("loading...", jngl::Vec2(0, 0));
}
}
jngl::Sprite::Loader mySprite{ "foo.webp" };
};
Use this class to load a Sprite asynchronously.
Definition: sprite.hpp:59
Two-dimensional vector.
Definition: Vec2.hpp:32
Active state of the game, e.g. a menu or the game itself.
Definition: work.hpp:13
void print(const std::string &text, jngl::Vec2 position)
Print text at position.

Definition at line 59 of file sprite.hpp.

Constructor & Destructor Documentation

◆ Loader()

Loader ( std::string  filename)
explicitnoexcept

Starts a thread to load filename and returns instantly.

Note that if the file couldn't be found this will not throw. Instead the exception will be thrown on first use by shared() or operator->().

◆ ~Loader()

~Loader ( )
noexcept

Blocks until the Sprite has been loaded.

While you won't have access to std::shared_ptr returned by shared(), the Sprite will still be loaded into JNGL's cache and the next time you create a Sprite from this filename, you'll get the same std::shared_ptr.

If shared() or operator->() haven't been called yet and the file wasn't found the destructor won't throw but use errorMessage(const std::string&).

Member Function Documentation

◆ shared()

std::shared_ptr< Sprite > shared ( ) const

Blocks until the Sprite has been loaded and returns a non-nullptr std::shared_ptr.

Exceptions
std::runtime_erroron decoding errors or file not found

◆ operator bool()

operator bool ( ) const

Returns whether the sprite has been loaded, does NOT block.

After this returned true all other methods won't block any more.

◆ operator->()

Sprite * operator-> ( ) const

Blocks until the Sprite has been loaded.

Exceptions
std::runtime_erroron decoding errors or file not found

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