HTML and CSS Reference
In-Depth Information
as “textures/wall.png” ), then the game could use a generic asset cache in memory that has the ability to release
assets if they are not being used. Different heuristics can be used to decide if an asset should be released from such a
cache, such as size. In the case of texture assets, where texture memory is limited, that cache can be used as a buffer
to limit the storage of assets that are used less frequently. Releasing these assets will involve freeing it from the texture
memory on the graphics card. Listing 2-3 shows an example of such a memory cache.
Listing 2-3. A Memory Cache with a Limited Asset Size That Prioritizes Cached Assets That Have Been Requested
Most Recently
/**
* Assumed global values:
*
* Observer - A class to notify subscribers when a callback is made.
* See https://github.com/turbulenz/turbulenz_engine for an example.
* TurbulenzEngine - Required for the setTimeout function.
* Used to make callbacks asynchronously.
* requestTexture - A function responsible for requesting the texture asset.
* drawTexture - A function that draws a given texture.
*/
/**
* AssetCache - A class to manage a cache of a fixed size.
*
* When the number of unique assets exceeds the cache size an existing asset is removed.
* The cache prioritizes cached assets that have been requested most recently.
*/
var AssetCache = (function () {
function AssetCache() {}
AssetCache.prototype.exists = function (key) {
return this.cache.hasOwnProperty(key);
};
AssetCache.prototype.isLoading = function (key) {
// See if the asset has a cache entry and if it is loading
var cachedAsset = this.cache[key];
if (cachedAsset) {
return cachedAsset.isLoading;
}
return false;
};
AssetCache.prototype.get = function (key) {
// Look for the asset in the cache
var cachedAsset = this.cache[key];
if (cachedAsset) {
// Set the current hitCounter for the asset
// This indicates it is the last requested asset
cachedAsset.cacheHit = this.hitCounter;
this.hitCounter += 1;
Search WWH ::




Custom Search