/* Copyright (c) 2012, Motorola Mobility LLC. All Rights Reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Motorola Mobility LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ var RDGE = RDGE || {}; /* * Manage state instances */ RDGE.stateManager = function () { // a stack of states this.stateStack = []; // number of states on the stack this.stateTop = undefined; // the states of the context this.RDGEInitState = null; this.RDGERunState = null; this.currentState = function () { if(this.stateTop != undefined) return this.stateStack[this.stateTop]; return null; }; /* * Push new IRuntime state - engine executes the new state */ this.PushState = function (state, flags) { if (state != null && typeof state.Init == 'function') { if(this.stateTop != undefined) this.stateStack[this.stateTop].LeaveState(); if(flags == undefined || flags != "noInit") state.Init(); this.stateTop = this.stateStack.push(state) - 1; } }; /* * Remove IRuntime state from stack, engine executes previous state */ this.PopState = function () { state = this.stateStack.pop(); if (state != null) { state.Shutdown(); } this.stateTop = this.stateTop > 0 ? this.stateTop - 1 : 0; if (this.stateStack[this.stateTop]) { this.stateStack[this.stateTop].ReInit(); } }; /* * Remove all states from the stack */ this.PopAll = function () { while (this.stateStack[this.stateTop] != null) { this.PopState(); } }; this.tick = function (dt) { if (this.stateStack[this.stateTop] != null) { this.stateStack[this.stateTop].Update(dt); this.stateStack[this.stateTop].Resize(); this.stateStack[this.stateTop].Draw(); } }; }; RDGE.Engine = function () { this._assetPath = "assets/"; // map of scene graphs to names this.sceneMap = []; // number of states on the stack this.stateTop = undefined; // size of the browser window this.lastWindowWidth = window.innerWidth; this.lastWindowHeight = window.innerHeight; this.defaultContext = null; this.lightManager = null; clearColor = [0.0, 0.0, 0.0, 0.0]; this.initializeComplete = false; this.RDGECanvas = null; /* * a map of canvas names to renderer */ this.canvasToRendererMap = {}; /* * states to canvas map - maps a state stack to the canvas context it belongs to */ this.canvasNameToStateStack = {}; /* * the list of context's that are active */ this.canvasCtxList = []; /* * regex object to verify runtime object is not some sort of exploit */ invalidObj = new RegExp("([()]|function)"); isValidObj = function (name) { // do a quick test make sure user isn't trying to execute a function if (invalidObj.test(name)) { window.console.error("invalid object name passed to RDGE, " + name + " - looks like a function"); return false; } return true; }; /* * The context definition - every context shares these parameters */ contextDef = function () { this.id = null; this.renderer = null; this.ctxStateManager = null; this.startUpState = null; this.sceneGraphMap = []; this.currentScene = null; this.getScene = function () { return this.sceneGraphMap[this.currentScene]; } this.debug = { 'frameCounter': 0, 'mat4CallCount': 0 } }; // maintains the contexts contextManager = new RDGE.objectManager(); this.ctxMan = contextManager; // the context currently being updated contextManager.currentCtx = null; contextManager._addObject = contextManager.addObject; contextManager.contextMap = {}; contextManager.addObject = function (context) { this.contextMap[context.id] = context; return this._addObject(context); }; contextManager.start = function () { var len = this.objects.length; for (var i = 0; i < len; ++i) { // set the current context contextManager.currentCtx = this.objects[i]; this.objects[i].ctxStateManager.PushState(this.objects[i].startUpState); } }; contextManager.forEach = function (cb) { var len = this.objects.length; for (var i = 0; i < len; ++i) { cb(this.objects[i]); } }; this.getContext = function (optCanvasID) { if (!optCanvasID) { return contextManager.currentCtx; } else { return contextManager.contextMap[optCanvasID]; } }; this.clearContext = function (canvasID) { contextManager.contextMap[canvasID] = undefined; }; /* * give the contextID (canvas id) of the context to set */ this.setContext = function (contextID) { contextManager.currentCtx = contextManager.contextMap[contextID]; }; this.tickContext = function (contextID) { var savedCtx = contextManager.currentCtx; contextManager.currentCtx = contextManager.contextMap[contextID]; this.objects[i].ctxStateManager.tick(dt); contextManager.currentCtx = savedCtx; }; this.setAssetPath = function (path) { this._assetPath = path.slice(); }; this.remapAssetFolder = function (url) { var searchStr = "assets/"; var index = url.indexOf(searchStr); var rtnPath = url; if (index >= 0) { rtnPath = url.substr(index + searchStr.length); rtnPath = this._assetPath + rtnPath; } return rtnPath; }; }; /* * Initialize the RDGE web engine */ RDGE.Engine.prototype.init = function (userInitState, userRunState, canvasObject) { this.GlInit(canvasObject); globalParamFuncSet = function (param) { this.data = param.data; this.type =param.type; this.set = function (v) { var len = this.data ? this.data.length : 0; for(var i=0;i