Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::core::Preloader< Resource > Class Template Reference

Loads a list of resources in the background, telling how far it is. More...

#include <aegis2d/core/preloader.hpp>

Public Member Functions

 Preloader (ResourceCache< Resource > &cache, std::vector< std::string > names)
 Starts loading at once, in its own thread.
 
 Preloader (const Preloader &)=delete
 
Preloader & operator= (const Preloader &)=delete
 
 Preloader (Preloader &&)=delete
 
Preloader & operator= (Preloader &&)=delete
 
 ~Preloader ()=default
 Waits for the loading thread.
 
std::size_t total () const noexcept
 Names to load in all.
 
std::size_t processed () const noexcept
 Names dealt with so far, loaded or failed.
 
float progress () const noexcept
 How far the loading is.
 
bool done () const noexcept
 Tells whether every name was dealt with.
 
void wait ()
 Waits until every name is dealt with.
 
std::vector< PreloadFailure > failures () const
 The resources that could not be loaded.
 
ResourceCache< Resource >::Handle handle (std::string_view name) const
 The kept handle of a loaded name.
 
void release ()
 Lets go of the kept handles, once the game holds its own.
 

Detailed Description

template<typename Resource>
class aegis2d::core::Preloader< Resource >

Loads a list of resources in the background, telling how far it is.

The preloader starts its own thread at once: each name is loaded through the cache (ResourceCache's calls are safe between threads) and its handle kept, so the resources stay cached until release(). The game thread draws a loading screen from progress() and done(); a loader's exception is recorded in failures() and the rest still loads. The cache must outlive the preloader, whose destructor waits for the thread.

core::Preloader loading{images, {"hero.png", "tiles.png", "boss.png"}};
// Each frame of the loading screen:
draw_progress_bar(renderer, loading.progress());
if (loading.done()) { start_the_game(); }
Loads a list of resources in the background, telling how far it is.
Definition preloader.hpp:42

Constructor & Destructor Documentation

◆ Preloader() [1/3]

template<typename Resource >
aegis2d::core::Preloader< Resource >::Preloader ( ResourceCache< Resource > &  cache,
std::vector< std::string >  names 
)
inline

Starts loading at once, in its own thread.

Parameters
cacheCache the resources are loaded through, and stay in.
namesNames to load, in this order; an empty list is done at once.

◆ Preloader() [2/3]

template<typename Resource >
aegis2d::core::Preloader< Resource >::Preloader ( const Preloader< Resource > &  )
delete

◆ Preloader() [3/3]

template<typename Resource >
aegis2d::core::Preloader< Resource >::Preloader ( Preloader< Resource > &&  )
delete

◆ ~Preloader()

template<typename Resource >
aegis2d::core::Preloader< Resource >::~Preloader ( )
default

Waits for the loading thread.

Member Function Documentation

◆ done()

template<typename Resource >
bool aegis2d::core::Preloader< Resource >::done ( ) const
inlinenoexcept

Tells whether every name was dealt with.

Returns
true once the list is through; failures() then says what went wrong.

◆ failures()

template<typename Resource >
std::vector< PreloadFailure > aegis2d::core::Preloader< Resource >::failures ( ) const
inline

The resources that could not be loaded.

Returns
Their names and reasons, in loading order.

◆ handle()

template<typename Resource >
ResourceCache< Resource >::Handle aegis2d::core::Preloader< Resource >::handle ( std::string_view  name) const
inline

The kept handle of a loaded name.

Parameters
nameA name of the list.
Returns
Its handle, or nothing while it is not loaded (or failed).

◆ operator=() [1/2]

template<typename Resource >
Preloader & aegis2d::core::Preloader< Resource >::operator= ( const Preloader< Resource > &  )
delete

◆ operator=() [2/2]

template<typename Resource >
Preloader & aegis2d::core::Preloader< Resource >::operator= ( Preloader< Resource > &&  )
delete

◆ processed()

template<typename Resource >
std::size_t aegis2d::core::Preloader< Resource >::processed ( ) const
inlinenoexcept

Names dealt with so far, loaded or failed.

Returns
From 0 to total().

◆ progress()

template<typename Resource >
float aegis2d::core::Preloader< Resource >::progress ( ) const
inlinenoexcept

How far the loading is.

Returns
From 0 to 1; 1 for an empty list.

◆ release()

template<typename Resource >
void aegis2d::core::Preloader< Resource >::release ( )
inline

Lets go of the kept handles, once the game holds its own.

Resources nothing else uses are freed, as ResourceCache does.

◆ total()

template<typename Resource >
std::size_t aegis2d::core::Preloader< Resource >::total ( ) const
inlinenoexcept

Names to load in all.

Returns
The size of the list given to the constructor.

◆ wait()

template<typename Resource >
void aegis2d::core::Preloader< Resource >::wait ( )
inline

Waits until every name is dealt with.


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