CUGL 3.0
Cornell University Game Library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
cugl::graphics::SpriteMeshLoader Class Reference

#include <CUSpriteMeshLoader.h>

Inheritance diagram for cugl::graphics::SpriteMeshLoader:
cugl::Loader< SpriteMesh > cugl::BaseLoader

Public Member Functions

 SpriteMeshLoader ()
 
void dispose () override
 
- Public Member Functions inherited from cugl::Loader< SpriteMesh >
 Loader ()
 
std::shared_ptr< SpriteMesh > get (const std::string key) const
 
std::shared_ptr< SpriteMesh > operator[] (const std::string key) const
 
size_t loadCount () const override
 
size_t inFlight () const override
 
void enqueue (const std::string key)
 
void unloadAll () override
 
virtual std::vector< std::string > keys () override
 
bool purgeKey (const std::string key) override
 
bool verify (const std::string key) const override
 
- Public Member Functions inherited from cugl::BaseLoader
 BaseLoader ()
 
 ~BaseLoader ()
 
virtual void dispose ()
 
virtual bool init ()
 
virtual bool init (const std::shared_ptr< ThreadPool > &threads)
 
std::shared_ptr< BaseLoadergetHook ()
 
std::shared_ptr< ThreadPoolgetThreadPool () const
 
void setThreadPool (const std::shared_ptr< ThreadPool > &threads)
 
void setManager (AssetManager *manager)
 
const AssetManagergetManager () const
 
void setJsonKey (const std::string key)
 
const std::string getJsonKey () const
 
void setPriority (Uint32 priority)
 
const Uint32 getPriority () const
 
bool load (const std::string key, const std::string source)
 
bool load (const std::shared_ptr< JsonValue > &json)
 
void loadAsync (const std::string key, const std::string source, LoaderCallback callback)
 
void loadAsync (const std::shared_ptr< JsonValue > &json, LoaderCallback callback)
 
bool unload (const std::string key)
 
bool unload (const std::shared_ptr< JsonValue > &json)
 
virtual void unloadAll ()
 
virtual std::vector< std::string > keys ()
 
bool contains (const std::string key) const
 
void reserve (size_t amount)
 
virtual size_t loadCount () const
 
size_t waitCount () const
 
virtual size_t inFlight () const
 
bool complete () const
 
float progress () const
 

Static Public Member Functions

static std::shared_ptr< SpriteMeshLoaderalloc ()
 
static std::shared_ptr< SpriteMeshLoaderalloc (const std::shared_ptr< ThreadPool > &threads)
 

Protected Member Functions

std::shared_ptr< SpriteMeshpreload (const std::string key, const std::string source)
 
std::shared_ptr< SpriteMeshpreload (const std::shared_ptr< JsonValue > &json)
 
bool materialize (const std::string key, const std::shared_ptr< SpriteMesh > &mesh, LoaderCallback callback)
 
virtual bool read (const std::string key, const std::string source, LoaderCallback callback, bool async) override
 
virtual bool read (const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async) override
 
- Protected Member Functions inherited from cugl::BaseLoader
virtual bool read (const std::string key, const std::string source, LoaderCallback callback, bool async)
 
virtual bool read (const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async)
 
virtual bool purgeKey (const std::string key)
 
virtual bool purgeJson (const std::shared_ptr< JsonValue > &json)
 
virtual bool verify (const std::string key) const
 

Additional Inherited Members

- Protected Attributes inherited from cugl::Loader< SpriteMesh >
std::unordered_map< std::string, std::shared_ptr< SpriteMesh > > _assets
 
std::unordered_set< std::string > _queue
 
- Protected Attributes inherited from cugl::BaseLoader
std::string _jsonKey
 
Uint32 _priority
 
size_t _reserved
 
std::shared_ptr< ThreadPool_loader
 
AssetManager_manager
 

Detailed Description

This class is a specific implementation of Loader<SpriteMesh>

This asset loader allows us to allocate sprite meshes from a JSON specification. The format of this json data is the same as that required by SpriteMesh#allocWithData.

Note that this implementation uses a two phase loading system. First, it loads as much of the asset as possible without using OpenGL. This allows us to load the model in a separate thread. It then finishes off the remainder of asset loading using Application#schedule. This is a good template for asset loaders in general.

As with all of our loaders, this loader is designed to be attached to an asset manager. Use the method getHook() to get the appropriate pointer for attaching the loader.

Constructor & Destructor Documentation

◆ SpriteMeshLoader()

cugl::graphics::SpriteMeshLoader::SpriteMeshLoader ( )
inline

Creates a new, uninitialized sprite mesh loader

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate a loader on the heap, use one of the static constructors instead.

Member Function Documentation

◆ alloc() [1/2]

static std::shared_ptr< SpriteMeshLoader > cugl::graphics::SpriteMeshLoader::alloc ( )
inlinestatic

Returns a newly allocated sprite mesh loader.

This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the OpenGL context must be active. Attempts to load an asset before this method is called will fail.

This loader will have no associated threads. That means any asynchronous loading will fail until a thread is provided via setThreadPool.

Returns
a newly allocated sprite mesh loader.

◆ alloc() [2/2]

static std::shared_ptr< SpriteMeshLoader > cugl::graphics::SpriteMeshLoader::alloc ( const std::shared_ptr< ThreadPool > &  threads)
inlinestatic

Returns a newly allocated sprite mesh loader.

This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the OpenGL context must be active. Attempts to load an asset before this method is called will fail.

Parameters
threadsThe thread pool for asynchronous loading
Returns
a newly allocated sprite mesh loader.

◆ dispose()

void cugl::graphics::SpriteMeshLoader::dispose ( )
inlineoverridevirtual

Disposes all resources and assets of this loader

Once the loader is disposed, any attempts to load a new asset will fail. You must reinitialize the loader to begin loading assets again.

Reimplemented from cugl::BaseLoader.

◆ materialize()

bool cugl::graphics::SpriteMeshLoader::materialize ( const std::string  key,
const std::shared_ptr< SpriteMesh > &  mesh,
LoaderCallback  callback 
)
protected

Creates an OpenGL buffer for the mesh, and assigns it the given key.

This method finishes the asset loading started in preload. This step is not safe to be done in a separate thread. Instead, it takes place in the main CUGL thread via Application#schedule.

This method supports an optional callback function which reports whether the asset was successfully materialized.

Parameters
keyThe key to access the asset after loading
meshThe mesh to materialize
callbackAn optional callback for asynchronous loading
Returns
true if materialization was successful

◆ preload() [1/2]

std::shared_ptr< SpriteMesh > cugl::graphics::SpriteMeshLoader::preload ( const std::shared_ptr< JsonValue > &  json)
protected

Loads the portion of this asset that is safe to load outside the main thread.

It is not safe to create an OpenGL buffer in a separate thread. However, it is safe to create a SpriteMesh, so long as it does not have a graphics buffer. Hence this method does the maximum amount of work that can be done in asynchronous mesh loading.

Parameters
jsonThe JSON entry specifying the asset
Returns
the incomplete sprite mesh

◆ preload() [2/2]

std::shared_ptr< SpriteMesh > cugl::graphics::SpriteMeshLoader::preload ( const std::string  key,
const std::string  source 
)
protected

Loads the portion of this asset that is safe to load outside the main thread.

It is not safe to create an OpenGL buffer in a separate thread. However, it is safe to create a SpriteMesh, so long as it does not have a graphics buffer. Hence this method does the maximum amount of work that can be done in asynchronous mesh loading.

Parameters
keyThe key to access the asset after loading
sourceThe pathname to the asset
Returns
the incomplete sprite mesh

◆ read() [1/2]

virtual bool cugl::graphics::SpriteMeshLoader::read ( const std::shared_ptr< JsonValue > &  json,
LoaderCallback  callback,
bool  async 
)
overrideprotectedvirtual

Internal method to support asset loading.

This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.

This method will split the loading across the preload and materialize methods. This ensures that asynchronous loading is safe.

This version of read provides support for JSON directories. A sprite mesh directory entry is either an array or a JSON object (or the name of a file containing either of these), satisfying the specification of SpriteMesh#allocWithData.

Parameters
jsonThe directory entry for the asset
callbackAn optional callback for asynchronous loading
asyncWhether the asset was loaded asynchronously
Returns
true if the asset was successfully loaded

Reimplemented from cugl::BaseLoader.

◆ read() [2/2]

virtual bool cugl::graphics::SpriteMeshLoader::read ( const std::string  key,
const std::string  source,
LoaderCallback  callback,
bool  async 
)
overrideprotectedvirtual

Internal method to support asset loading.

This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.

This method will split the loading across the preload and materialize methods. This ensures that asynchronous loading is safe.

A mesh file is a JSON file, whose contents satisfy the specification of SpriteMesh#allocWithData.

Parameters
keyThe key to access the asset after loading
sourceThe pathname to the asset
callbackAn optional callback for asynchronous loading
asyncWhether the asset was loaded asynchronously
Returns
true if the asset was successfully loaded

Reimplemented from cugl::BaseLoader.


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