From e36138258021ec167f82c6d1651910d01e8de9d3 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Fri, 27 Oct 2017 10:19:00 +0200 Subject: [PATCH] delayed image creation for storage system --- src/game-lib-system-storage.js | 72 +++++++++++----------------------- 1 file changed, 23 insertions(+), 49 deletions(-) diff --git a/src/game-lib-system-storage.js b/src/game-lib-system-storage.js index 5b68b27..a07144e 100644 --- a/src/game-lib-system-storage.js +++ b/src/game-lib-system-storage.js @@ -377,7 +377,7 @@ GameLib.System.Storage.prototype.loadComponent = function(apiUrl, toProcess, inc if (clientErrorCallback) { clientErrorCallback({ - message : error.messsage || 'JSON parse error' + message : error.message || 'JSON parse error' }) } @@ -618,8 +618,7 @@ GameLib.System.Storage.prototype.loadComponent = function(apiUrl, toProcess, inc var progress = 0; if (progressEvent.total !== 0) { - progress = Number(progressEvent.loaded / progressEvent.total); - progress *= 100; + progress = Math.round(Number(progressEvent.loaded / progressEvent.total) * 100); } if (onComponentProgress) { @@ -839,9 +838,7 @@ GameLib.System.Storage.prototype.processBlenderData = function(data) { * Process all images - we have to load them in addition to creating their runtime components */ data.images.map(function(imageData){ - - var image = GameLib.D3.Image.FromObject(this.graphics, imageData); - + var image = GameLib.D3.Image.FromObject(this.graphics, imageData); GameLib.Event.Emit( GameLib.Event.COMPONENT_CREATED, { @@ -855,9 +852,7 @@ GameLib.System.Storage.prototype.processBlenderData = function(data) { * Process all textures */ data.textures.map(function(textureData){ - - var texture = GameLib.D3.Texture.FromObject(this.graphics, textureData); - + var texture = GameLib.D3.Texture.FromObject(this.graphics, textureData); GameLib.Event.Emit( GameLib.Event.COMPONENT_CREATED, { @@ -885,9 +880,7 @@ GameLib.System.Storage.prototype.processBlenderData = function(data) { * Now process all meshes */ data.meshes.map(function(meshData){ - - var mesh = GameLib.D3.Mesh.FromObject(this.graphics, meshData); - + var mesh = GameLib.D3.Mesh.FromObject(this.graphics, meshData); GameLib.Event.Emit( GameLib.Event.COMPONENT_CREATED, { @@ -992,52 +985,34 @@ GameLib.System.Storage.prototype.loadImage = function(data) { xhr.onload = function() { + var objectUrl = false; + + var url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4QoWEQMQBXD4hQAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAABRSURBVGje7c8xDQAwCAAwmA3koA/PU8FB0jpo1nRc9uI4AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBgX0fjEoBa8xN1z4AAAAASUVORK5CYII='; + try { if (this.response.type !== 'application/json') { - var url = window.URL.createObjectURL(this.response); - } else { - if (onError) { - - GameLib.Event.Emit( - GameLib.Event.IMAGE_NOT_FOUND, - { - image : image - } - ); - - onError(image, {message:'Image not found'}); - - return; - } + url = window.URL.createObjectURL(this.response); + objectUrl = true; } } catch (error) { - if (onError) { - - GameLib.Event.Emit( - GameLib.Event.IMAGE_NOT_FOUND, - { - image : image - } - ); - - onError(image, {message:'Image not found'}); - return; - } + /** + * Do Nothing + */ } + var img = document.createElement('img'); - img.onload = function() { + img.onload = function () { - window.URL.revokeObjectURL(url); + if (objectUrl) { + window.URL.revokeObjectURL(url); + } image.instance = img; - image.publish( - GameLib.Event.IMAGE_INSTANCE_CREATED, - { - image: image - } - ); + image.delayed = false; + + image.createInstance(); if (onLoaded) { onLoaded(image, data.createTexture); @@ -1052,8 +1027,7 @@ GameLib.System.Storage.prototype.loadImage = function(data) { var progress = 0; if (progressEvent.total !== 0) { - progress = Number(progressEvent.loaded / progressEvent.total); - progress *= 100; + progress = Math.round(Number(progressEvent.loaded / progressEvent.total) * 100); } if (onProgress) {