From f52761f6388f3456d1d3ec44f107c39b3c4f1b00 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Thu, 2 Jul 2015 15:05:59 +0200 Subject: [PATCH] Added stuff --- geo/MeshContainer.js | 55 +++++++++++++++++++++++++++++++++----------- server.js | 3 +-- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/geo/MeshContainer.js b/geo/MeshContainer.js index db36b3e..c889cc5 100644 --- a/geo/MeshContainer.js +++ b/geo/MeshContainer.js @@ -4,7 +4,7 @@ * @constructor * @memberOf geo */ -geo.MeshContainer = function(path) { +geo.MeshContainer = function(path, callback) { /** * array of each part of the mesh @@ -36,6 +36,8 @@ geo.MeshContainer = function(path) { */ this.texCoords = []; + this.callback = callback; + if (path !== undefined) { this.loadFromFile(path); @@ -51,7 +53,7 @@ geo.MeshContainer = function(path) { geo.MeshContainer.prototype.loadFromFile = function(path) { var self = this; - var data = fs.readFileSync(path, {encoding: 'utf-8'}); + fs.readFile(path, {encoding: 'utf-8'}, function(err, data) { var currentMesh; @@ -133,27 +135,54 @@ geo.MeshContainer.prototype.loadFromFile = function(path) { } + + if (typeof self.callback === 'function') { + + self.callback(); + + } + + }); + }; -var availableMeshNames = [ - '/static/data/castle/princess peaches castle (outside).obj', - '/static/data/mountain/coocoolmountain.obj', - '/static/data/whomp/Whomps Fortress.obj', - '/static/data/bobomb/bobomb battlefeild.obj', - '/static/data/sponza/sponza.obj' -]; +function trySetLoaded() { + for (var name in availableMeshNames) { + + if (availableMeshNames[name] === false) { + + return; + + } + + } + + console.log("[OBJ] All meshes are ready"); +} + +var availableMeshNames = { + '/static/data/castle/princess peaches castle (outside).obj':false, + '/static/data/mountain/coocoolmountain.obj':false, + '/static/data/whomp/Whomps Fortress.obj':false, + '/static/data/bobomb/bobomb battlefeild.obj':false, + '/static/data/sponza/sponza.obj':false +}; for (var i = 1; i < 26; i++) { - availableMeshNames.push('/static/data/spheres/' + i + '.obj'); + availableMeshNames['/static/data/spheres/' + i + '.obj'] = false; } geo.availableMeshes = {}; -for (var i = 0; i < availableMeshNames.length; i++) { +for (var name in availableMeshNames) { - var name = availableMeshNames[i]; - geo.availableMeshes[name] = new geo.MeshContainer(name.substring(1, name.length)); + (function(name) { + geo.availableMeshes[name] = new geo.MeshContainer(name.substring(1, name.length), function() { + availableMeshNames[name] = true; + trySetLoaded(); + }); + })(name); } diff --git a/server.js b/server.js index 0acbb7c..1a5d2d4 100644 --- a/server.js +++ b/server.js @@ -106,6 +106,5 @@ if ( isDev ) { // Start server http.listen(server_port, server_ip_address, function() { - if (isDev) - console.log("Server ready : now listening " + server_ip_address + ":" + server_port); + console.log("[READY] Now listening " + server_ip_address + ":" + server_port); });