TextureManager.js 10 KB

1
  1. var cacheEntryUniqueId=0;var TextureManager=Class.create({initialize:function(b){debugMessage(kDebugTextureManager_Initialize);var a=this;document.observe(kScriptDidDownloadEvent,function(c){a.handleScriptDidDownloadEvent(c)},false);this.script=null;this.showUrl=b;this.sceneCache={};this.numImageObjects=0;this.numLoadFailures=0;this.numOutstandingLoadRequests=0;this.textureCount=0},cacheEntryForScene:function(c,a){var b=this.sceneCache[c];if(b==null){if(a){debugMessage(kDebugTextureManager_CacheEntryForScene,"sceneIndex: "+c+" - not found, creating new entry...");this.sceneCache[c]={numPreloading:-1,numLoaded:-2,uniqueID:cacheEntryUniqueId++,imageObjects:{}};b=this.sceneCache[c]}}else{debugMessage(kDebugTextureManager_CacheEntryForScene,"sceneIndex: "+c+" - found existing entry...")}return b},debugDumpCache:function(){debugMessageAlways(kDebugShowController_DiagnosticsDump,"-");debugMessageAlways(kDebugShowController_DiagnosticsDump,"- Texture Cache");debugMessageAlways(kDebugShowController_DiagnosticsDump,"---------------");for(var f in this.sceneCache){var b=this.sceneCache[f];debugMessageAlways(kDebugShowController_DiagnosticsDump,"- sceneIndex: "+f);debugMessageAlways(kDebugShowController_DiagnosticsDump,"- numPreloading: "+b.numPreloading);debugMessageAlways(kDebugShowController_DiagnosticsDump,"- numLoaded: "+b.numLoaded);debugMessageAlways(kDebugShowController_DiagnosticsDump,"- isDownloaded: "+((this.isScenePreloaded(f))?"yes":"no"));var e=b.imageObjects;var d=0;if(e){for(var c in e){var a=e[c];debugMessageAlways(kDebugShowController_DiagnosticsDump,"- "+(d++)+": "+c+" - "+a.tagName+" - "+a.src)}}else{debugMessageAlways(kDebugShowController_DiagnosticsDump,"- imageSet for this cache entry is Undefined!")}}},scenesInCache:function(){var a="";for(var b in this.sceneCache){if(a!=""){a+=", "}a+=b}return a},getCacheStatistics:function(){var h={numScenes:0,numTextures:0,numPixels:0,minTexturesPerScene:0,maxTexturesPerScene:0,averageNumTexturesPerScene:0,minNumTexturesPerScene:0,maxNumTexturesPerScene:0};debugMessage(kDebugTextureManager_GetCacheStatistics,"begin iterating over cache...");for(var g in this.sceneCache){var b=this.sceneCache[g];debugMessage(kDebugTextureManager_GetCacheStatistics,"- scene: "+g);h.numScenes++;var f=b.imageObjects;var e=0;if(f){debugMessage(kDebugTextureManager_GetCacheStatistics,"- it has an imageSet, iterate over it...");for(var d in f){var a=f[d];var c=a.naturalHeight*a.naturalWidth;debugMessage(kDebugTextureManager_GetCacheStatistics,"- textureId: "+d+" - "+a.naturalWidth+"x"+a.naturalHeight+" = "+c);h.numPixels+=c;h.numTextures++}}}debugMessage(kDebugTextureManager_GetCacheStatistics,"- total of "+h.numTextures+" textures, "+h.numPixels+" pixels");return h},handleScriptDidDownloadEvent:function(a){this.script=a.memo.script;debugMessage(kDebugTextureManager_HandleScriptDidDownloadEvent,"filename: "+this.script.filename)},preloadScenes:function(a){debugMessage(kDebugTextureManager_PreloadScenes,"ensuring the cache contains the following scenes:");var f=0;for(var e in a){debugMessage(kDebugTextureManager_PreloadScenes,"- "+(f++)+": "+e)}debugMessage(kDebugTextureManager_PreloadScenes,"- iterating over scenes currently in cache purging those not in above list...");f=0;for(var e in this.sceneCache){var c=a.hasOwnProperty(e);debugMessage(kDebugTextureManager_PreloadScenes,"- "+(f++)+": "+e+" - "+(c?"keep":"discard"));if(c==false){var b=this.sceneCache[e];delete this.sceneCache[e]}}debugMessage(kDebugTextureManager_PreloadScenes,"- iterating over new list, loading those scene not already in the cache...");f=0;for(var e in a){var d=this.sceneCache.hasOwnProperty(e);debugMessage(kDebugTextureManager_PreloadScenes,"- "+(f++)+": "+e+" - "+(d?"already loaded":"loading..."));if(d==false){this.loadScene(e)}}},loadScene:function(l){debugMessage(kDebugTextureManager_LoadScene,"sceneIndex: "+l);kDebugPreloadScene="";if(l<0){debugMessage(kDebugTextureManager_LoadScene," - sceneIndex is less than 0, ignoring...");return}else{if(l>this.script.numScenes){debugMessage(kDebugTextureManager_LoadScene," - sceneIndex has gone off the end, ignoring...");return}}var b=this.cacheEntryForScene(l,true);var i=this.script.eventTimelines[l];var k=i.eventInitialStates;if(this.isScenePreloaded(l)){debugMessageAlways(kDebugTextureManager_LoadScene,"- scene already preloaded, ignoring...");return}else{if(b.numPreloading!=-1){debugMessageAlways(kDebugTextureManager_LoadScene," - we've already started a preload for scene "+l+", ignoring...");return}}var c=k.length;var d=b.imageObjects;b.numPreloading=c;b.numLoaded=0;for(var f=0;f<c;f++){var g=k[f];var a=g.texture;var e=gIpad?g.degrade:false;var h=this.urlForTexture(a,e);debugMessage(kDebugTextureManager_LoadScene,"- textureId: "+a+" degrade: "+(e?"yes":"no")+" textureUrl: "+h);var j=this.preloadTexture(l,a,h);d[a]=j;this.numOutstandingLoadRequests++}gShowController.displayManager.updateStatisticsDisplay()},unloadScene:function(b){debugMessage(kDebugTextureManager_UnloadScene,"sceneIndex: "+b+" removing entry for this scene from cache...");if(this.sceneCache.hasOwnProperty(b)){var a=this.sceneCache[b];delete this.sceneCache[b]}else{debugWarning(kDebugTextureManager_UnloadScene,"sceneIndex: "+b+" tried to remove entry for this scene from cache, but it did not exist.")}},isScenePreloaded:function(c){var a=this.cacheEntryForScene(c,false);if(a==null){debugMessage(kDebugTextureManager_IsScenePreloaded,"sceneIndex: "+c+" - there is no cache entry for this scene");return false}var b=(a.numPreloading==0)&&(a.numLoaded>0);debugMessage(kDebugTextureManager_IsScenePreloaded,"sceneIndex: "+c);debugMessage(kDebugTextureManager_IsScenePreloaded,"- numPreloading:"+a.numPreloading);debugMessage(kDebugTextureManager_IsScenePreloaded,"- numLoaded:"+a.numLoaded);debugMessage(kDebugTextureManager_IsScenePreloaded,"- isPreloaded:"+b);return b},preloadTexture:function(g,a,d,c){var e=null;var h=this;if(c){e=document.createElement("video")}else{e=new Image()}var b=-1;var i=this.cacheEntryForScene(g,false);if(i!=null){b=i.uniqueID}e.onerror=function(){h.textureDidNotPreload(g,a,e)};e.onload=function(){h.textureDidPreload(g,a,e,b)};e.onabort=function(){h.texturePreloadAborted(g,a,e)};if(gDebugSimulateSlowTextureDownload){e.onload=function(){h.textureDidPreload_Debug(g,a)}}if(gDebugSimulateTextureLoadFailure){this.textureCount++;if(this.textureCount%20==0){e.onload=function(){h.textureDidNotPreload_Debug(g,a)}}}var f=d+"?ts="+escape(this.script.timestamp);e.src=f;debugMessage(kDebugTextureManager_PreloadTexture,"textureId: "+a+" textureUrl: "+f);debugMessage(kDebugTextureManager_TrackTextureLifetime+"-preloadTexture","sceneIndex: "+g+" textureId: "+a+" textureUrl: "+f);return e},textureDidPreload_Debug:function(e,d,b){debugMessage(kDebugTextureManager_TextureDidPreload,"sceneIndex: "+e+" textureId: "+d+" downloaded, but pretending it took longer...");var a=this;var c=5000;setTimeout(function(){a.textureDidPreload(e,d)},c)},textureDidPreload:function(h,a,j,d){this.numOutstandingLoadRequests--;gShowController.displayManager.updateStatisticsDisplay();var c=this.cacheEntryForScene(h,false);if(c==null){debugWarning(kDebugTextureManager_TextureDidPreload,"sceneIndex: "+h+" textureId: "+a+" - there is no cache entry for this scene");return}if(c.uniqueID!=d){return}var f=this.script.textures[a];var e=this.script.scalefactor480;var i=f.height*e;var g=f.width*e;debugMessage(kDebugTextureManager_TextureDidPreload,"sceneIndex: "+h+" textureId: "+a);debugMessage(kDebugTextureManager_TextureDidPreload,"- before:");debugMessage(kDebugTextureManager_TextureDidPreload,"- numPreloading:"+c.numPreloading);debugMessage(kDebugTextureManager_TextureDidPreload,"- numLoaded:"+c.numLoaded);var b=c.imageObjects[a];if(b){c.numPreloading--;c.numLoaded++}else{debugWarning(kDebugTextureManager_TextureDidPreload,"sceneIndex: "+h+" textureId: "+a+" - there is no entry for this texture in this scene");return}debugMessage(kDebugTextureManager_TrackTextureLifetime+"-textureDidPreload","sceneIndex: "+h+" textureId: "+a+" expectedSize: "+f.width+"x"+f.height+" naturalSize: "+j.naturalWidth+"x"+j.naturalHeight+" url: "+j.src);debugMessage(kDebugTextureManager_TextureDidPreload,"- after:");debugMessage(kDebugTextureManager_TextureDidPreload,"- numPreloading:"+c.numPreloading);debugMessage(kDebugTextureManager_TextureDidPreload,"- numLoaded:"+c.numLoaded)},textureDidNotPreload_Debug:function(c,b,a){debugWarning(kDebugTextureManager_TextureDidNotPreload,"simulating texture load failure on scene: "+c+", textureId: "+b);this.textureDidNotPreload(c,b,a)},texturePreloadAborted:function(c,b,a){this.numOutstandingLoadRequests--;debugUpdateStatusDisplay();debugWarning(kDebugTextureManager_TrackTextureLifetime+"-texturePreloadAborted"," - sceneIndex: "+c+" textureId: "+b+" url: "+a.src)},textureDidNotPreload:function(c,b,a){this.numOutstandingLoadRequests--;debugUpdateStatusDisplay();debugMessageAlways(kDebugTextureManager_TrackTextureLifetime+"-textureDidNotPreload","LOAD FAILURE! - sceneIndex: "+c+" textureId: "+b);debugMessage(kDebugTextureManager_TrackTextureLifetime+"- size: "+a.naturalWidth+"x"+a.naturalHeight+" url: "+a.src);this.numLoadFailures++;return true},getImageObjectForTexture:function(d,c){var a=this.cacheEntryForScene(d,false);var b=a.imageObjects[c];return b},urlForMovie:function(a){return this.generateUrl(a,true,false)},urlForTexture:function(b,a){return this.generateUrl(b,false,a)},generateUrl:function(g,f,b){debugMessage(kDebugTextureManager_UrlForTexture,"textureId: "+g+" wantsMovieUrl: "+(f?"true":"false"));var a="";var e=this.script.textures[g];var c="";if(e!=null){if(f){c=e.movieUrl}else{c=e.url}if((c!=null)&&(c!="")){var d;if(gMode==kModeMobile){showSizeFolder="480x268/";if(f){debugMessage(kDebugTextureManager_UrlForTexture,"- this is a movie, there is no small version per se, it's all in the same m4v file...");d=false}else{if(gIpad||window.devicePixelRatio>=2){debugMessage(kDebugTextureManager_UrlForTexture,"- we're on an iPad, check if we should degrade this texture...");if(b){debugMessage(kDebugTextureManager_UrlForTexture,"- yes, this texture would put us over the limit...");d=true}else{debugMessage(kDebugTextureManager_UrlForTexture,"- no, this texture fits within the limit, use full size...");d=false}}else{debugMessage(kDebugTextureManager_UrlForTexture,"- we're on an iPhone/iPod Touch, so use small textures...");d=true}}}else{debugMessage(kDebugTextureManager_UrlForTexture,"- we're on a desktop machine, so use large textures...");d=false}a=this.showUrl+(d?"480x268/":"")+c}}debugMessage(kDebugTextureManager_UrlForTexture,"- url: "+a);return a}});