Cornell Cocos
Cornell Extensions to Cocos2d
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
GenericBaseLoader Class Reference

#include <CUGenericLoader.h>

Inheritance diagram for GenericBaseLoader:
Loader< Asset > BaseLoader

Classes

class  Coordinator
 

Public Member Functions

void start () override
 
void stop () override
 
size_t waitCount () const override
 
Assetload (std::string key, std::string source) override
 
Assetload (std::string key, Asset *asset)
 
void loadAsync (std::string key, std::string source) override
 
void loadAsync (std::string key, Asset *asset)
 
void unload (std::string key) override
 
void unloadAll () override
 
- Public Member Functions inherited from Loader< Asset >
virtual bool contains (std::string key) const
 
virtual Assetget (std::string key) const
 
Assetoperator[] (std::string key) const
 
virtual size_t loadCount () const override
 
- Public Member Functions inherited from BaseLoader
bool isActive () const
 
bool isComplete () const
 
float progress () const
 
CC_CONSTRUCTOR_ACCESS _active (false)
 
virtual ~BaseLoader ()
 

Static Public Member Functions

static GenericBaseLoadercreate ()
 

Public Attributes

CC_CONSTRUCTOR_ACCESS __pad0__: GenericBaseLoader() : Loader<Asset>() {} virtual ~GenericBaseLoader() { if (_active) { stop()
 
- Public Attributes inherited from BaseLoader
CC_CONSTRUCTOR_ACCESS __pad0__: BaseLoader() : Ref()
 

Protected Member Functions

void allocate (std::string key, Asset *asset)
 

Protected Attributes

std::unordered_set< std::string > _aqueue
 
- Protected Attributes inherited from Loader< Asset >
std::unordered_map< std::string, Asset * > _assets
 
- Protected Attributes inherited from BaseLoader
bool _active
 

Static Protected Attributes

static Coordinator_gCoordinator = nullptr
 

Detailed Description

Class is a implementation of Loader<Asset>

This asset loader allows us to allocate generic from the associated source files. As with all of our loaders, this loader is designed to be attached to a scene. This is the natural was to do things, as Cocos2d is scene based. However, its asset loading is typically done through the director, which is global. This makes is hard to determine when it is safe to unload an asset. Even though the current scene many not need it, it may be used by another active scene. Unloading the asset would corrupt that scene.

This loader solves this problem by have a static coordinator behind the scenes. This coordinate is shared across all loader instances. It decides When an asset is truly ready to be unloaded. We assume that all instances are run only in the Director thread.

This class should not be created directly. Instead, it should be wrapped by an instance of GenericLoader<T>.

Member Function Documentation

void GenericBaseLoader::allocate ( std::string  key,
Asset asset 
)
protected

A function to create a new asset from a filename.

This method should be part of a C++11 closure so that it can be used by the thread pool for asynchronous loading. When done, it will safely update the data structures of this loader.

Parameters
keyThe key for the asset information
assetThe asset to associate with the key
GenericBaseLoader * GenericBaseLoader::create ( )
static

Creates a new GenericBaseLoader.

This constructor does not start the asset loader. It simply creates an object for the asset loader so that it can be attached to the asset manager. Call start() when you are ready to start using it.

Returns
an autoreleased assetLoader object
Asset* GenericBaseLoader::load ( std::string  key,
std::string  source 
)
inlineoverridevirtual

This method is disabled, as the GenericLoader<T> will wrap it with the correct version of the method.

Reimplemented from Loader< Asset >.

Asset * GenericBaseLoader::load ( std::string  key,
Asset asset 
)

Loads a asset and assigns it to the given key.

Parameter asset is a partially created asset object. It has the file set, but has not loaded any resources. This method calls the load() method, (in either the main or a separate thread). It returns a reference to the same asset object, now with resources loaded.

The asset will be loaded synchronously. It will be available immediately. This method should be limited to those times in which an asset is really necessary immediately, such as for a loading screen.

Parameters
keyThe key to access the asset after loading
sourceThe pathname to the asset file

the loaded asset

Returns
the loaded asset
void GenericBaseLoader::loadAsync ( std::string  key,
std::string  source 
)
inlineoverridevirtual

This method is disabled, as the GenericLoader<T> will wrap it with the correct version of the method.

Reimplemented from BaseLoader.

void GenericBaseLoader::loadAsync ( std::string  key,
Asset asset 
)

Adds a new asset to the loading queue.

Parameter asset is a partially created asset object. It has the file set, but has not loaded any resources. This method calls the load() method, (in either the main or a separate thread). It returns a reference to the same asset object, now with resources loaded.

The asset will be loaded asynchronously. When it is finished loading, it will be added to this loader, and accessible under the given key. This method will mark the loading process as not complete, even if it was completed previously. It is not safe to access the loaded asset until it is complete again.

Parameters
keyThe key to access the asset after loading
sourceThe pathname to the asset file

the asset upon loading

void GenericBaseLoader::start ( )
overridevirtual

Starts this asset loader.

Any assets loaded by this loader will be immediately released by the loader. However, an asset may still be available if it is attached to another loader. The asset loader is backed by a central coordinator that allows the sharing of assets.

Once the loader is stopped, any attempts to load a new asset will fail. You must call start() to begin loading assets again.

Reimplemented from BaseLoader.

void GenericBaseLoader::stop ( )
overridevirtual

Stops this resource loader removing all assets.

Any assets loaded by this loader will be immediately released by the loader. However, an asset may still be available if it is attached to another loader.

Once the loader is stopped, any attempts to load a new asset will fail. You must call start() to begin loading assets again.

Reimplemented from BaseLoader.

void GenericBaseLoader::unload ( std::string  key)
overridevirtual

Unloads the asset for the given key.

This method simply unloads the asset for the scene associated with this loader. The asset will not be deleted or removed from memory until it is removed from all instances of assetLoader.

Parameters
keythe key referencing the asset

the asset for the given key

Reimplemented from BaseLoader.

void GenericBaseLoader::unloadAll ( )
overridevirtual

Unloads all assets present in this loader.

This method simply unloads the assets for the scene associated with this loader. The assets will not be deleted or removed from memory until they are removed from all instances of assetLoader.

all loaded assets

Reimplemented from BaseLoader.

size_t GenericBaseLoader::waitCount ( ) const
inlineoverridevirtual

Returns the number of assets waiting to load.

This is a rough way to determine how many assets are still pending. A asset is pending if it has been loaded asychronously, and the loading process has not yet finished. This method counts each asset equally regardless of the memory requirements of the format.

Returns
the number of assets waiting to load.

Reimplemented from BaseLoader.

Member Data Documentation

std::unordered_set<std::string> GenericBaseLoader::_aqueue
protected

The assets we are expecting that are not yet loaded

NS_CC_BEGIN GenericBaseLoader::Coordinator * GenericBaseLoader::_gCoordinator = nullptr
staticprotected

The static coordinator singleton


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