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
TextureLoader Class Reference

#include <CUTextureLoader.h>

Inheritance diagram for TextureLoader:
Loader< Texture2D > BaseLoader

Classes

class  Coordinator
 

Public Member Functions

void start () override
 
void stop () override
 
size_t waitCount () const override
 
Texture2D * load (std::string key, std::string source) override
 
Texture2D * load (std::string key, std::string source, const Texture2D::TexParams &params)
 
void loadAsync (std::string key, std::string source) override
 
void loadAsync (std::string key, std::string source, const Texture2D::TexParams &params)
 
void unload (std::string key) override
 
void unloadAll () override
 
const Texture2D::TexParams & getDefaultParameters () const
 
void setDefaultParameters (const Texture2D::TexParams &params)
 
- Public Member Functions inherited from Loader< Texture2D >
virtual bool contains (std::string key) const
 
virtual Texture2D * get (std::string key) const
 
Texture2D * operator[] (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 TextureLoadercreate ()
 

Public Attributes

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

Protected Member Functions

void allocate (std::string key, Texture2D *texture, const Texture2D::TexParams &params)
 

Protected Attributes

Texture2D::TexParams _default
 
std::unordered_set< std::string > _tqueue
 
- Protected Attributes inherited from Loader< Texture2D >
std::unordered_map< std::string, Texture2D * > _assets
 
- Protected Attributes inherited from BaseLoader
bool _active
 

Static Protected Attributes

static Coordinator_gCoordinator = nullptr
 

Detailed Description

Class is a implementation of Loader<Texture2D>

This asset loader allows us to allocate texture objects from the associated image files. Technically, a texture should be identified by both its source file and its texture parameters. However, Cocos2D does not allow you to to do that – each texture can be only loaded once. Refactoring this would be a lot more work than we are comfortable with. Hence changeing the texture parameters for any texture object will change it across all references.

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.

Texture objects are uniquely identified by their image file. Attempt to load a image file a second time, even under a new key, will return a reference to the same texture object, even if different parameters are used.

Member Function Documentation

void TextureLoader::allocate ( std::string  key,
Texture2D *  texture,
const Texture2D::TexParams &  params 
)
protected

A function to create a new texture 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 texture information
textureThe texture to associate with the key
paramsThe texture parameters to initialize the texture
TextureLoader * TextureLoader::create ( )
static

Creates a new TextureLoader.

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

Returns
an autoreleased TextureLoader object
const Texture2D::TexParams& TextureLoader::getDefaultParameters ( ) const
inline

Returns the default texture parameters

Any font processed by this loader will use these settings unless otherwise specified.

Returns
the default texture parameters
Texture2D* TextureLoader::load ( std::string  key,
std::string  source 
)
inlineoverridevirtual

Loads a texture and assigns it to the given key.

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

The texture will be initialized with the default texture parameters.

Parameters
keyThe key to access the texture after loading
sourceThe pathname to the texture image file

the loaded texture

Returns
the loaded texture

Reimplemented from Loader< Texture2D >.

Texture2D * TextureLoader::load ( std::string  key,
std::string  source,
const Texture2D::TexParams &  params 
)

Loads a texture and assigns it to the given key.

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

Parameters
keyThe key to access the texture after loading
sourceThe pathname to the texture image file
paramsThe texture parameters for initialization

the loaded texture

Returns
the loaded texture
void TextureLoader::loadAsync ( std::string  key,
std::string  source 
)
inlineoverridevirtual

Adds a new texture to the loading queue.

The texture 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 texture until it is complete again.

The texture will be initialized with the default texture parameters.

Parameters
keyThe key to access the texture after loading
sourceThe pathname to the texture image file

the texture upon loading

Reimplemented from BaseLoader.

void TextureLoader::loadAsync ( std::string  key,
std::string  source,
const Texture2D::TexParams &  params 
)

Adds a new texture to the loading queue.

The texture 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 texture until it is complete again.

Parameters
keyThe key to access the texture after loading
sourceThe pathname to the texture image file
paramsThe texture parameters for initialization

the texture upon loading

void TextureLoader::setDefaultParameters ( const Texture2D::TexParams &  params)
inline

Sets the default texture parameters

Any font processed by this loader will use these settings unless otherwise specified.

Parameters
paramsthe default texture parameters
void TextureLoader::start ( )
overridevirtual

Starts this resource loader.

This method bootstraps the loader with any initial resources that it needs to load assets. Attempts to load an asset before this method is called will fail.

By separating this call from the constructor, this allows us to construct loaders and attach them to the AssetManager before we are ready to load assets.

Reimplemented from BaseLoader.

void TextureLoader::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 TextureLoader::unload ( std::string  key)
overridevirtual

Unloads the texture for the given key.

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

Parameters
keythe key referencing the texture

the texture for the given key

Reimplemented from BaseLoader.

void TextureLoader::unloadAll ( )
overridevirtual

Unloads all assets present in this loader.

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

all loaded textures

Reimplemented from BaseLoader.

size_t TextureLoader::waitCount ( ) const
inlineoverridevirtual

Returns the number of textures waiting to load.

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

Returns
the number of textures waiting to load.

Reimplemented from BaseLoader.

Member Data Documentation

Texture2D::TexParams TextureLoader::_default
protected

The default texture parameters

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

The static coordinator singleton

std::unordered_set<std::string> TextureLoader::_tqueue
protected

The textures we are expecting that are not yet loaded


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