aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/engine.js
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/helper-classes/RDGE/src/core/script/engine.js
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/engine.js')
-rw-r--r--js/helper-classes/RDGE/src/core/script/engine.js482
1 files changed, 482 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/engine.js b/js/helper-classes/RDGE/src/core/script/engine.js
new file mode 100644
index 00000000..6fe964c7
--- /dev/null
+++ b/js/helper-classes/RDGE/src/core/script/engine.js
@@ -0,0 +1,482 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7
8/*
9 * Manage state instances
10 */
11stateManager = function()
12{
13 // a stack of states
14 this.stateStack = [];
15
16 // number of states on the stack
17 this.stateTop = undefined;
18
19 // the states of the context
20 this.RDGEInitState = null;
21 this.RDGERunState = null;
22
23 this.currentState = function()
24 {
25 if(this.stateTop != undefined)
26 return this.stateStack[this.stateTop];
27
28 return null;
29 }
30
31 /*
32 * Push new IRuntime state - engine executes the new state
33 */
34 this.PushState = function(state, flags)
35 {
36 if(state!=null && typeof state.Init == 'function')
37 {
38 if(this.stateTop != undefined)
39 this.stateStack[this.stateTop].LeaveState();
40
41 if(flags == undefined || flags != "noInit")
42 state.Init();
43
44 this.stateTop = this.stateStack.push(state) - 1;
45 }
46 }
47
48 /*
49 * Remove IRuntime state from stack, engine executes previous state
50 */
51 this.PopState = function()
52 {
53 state = this.stateStack.pop();
54 if(state!=null)
55 {
56 state.Shutdown();
57 }
58
59 this.stateTop = this.stateTop > 0 ? this.stateTop - 1 : 0;
60
61 if(this.stateStack[this.stateTop])
62 {
63 this.stateStack[this.stateTop].ReInit();
64 }
65 }
66
67 /*
68 * Remove all states from the stack
69 */
70 this.PopAll = function()
71 {
72 while(this.stateStack[this.stateTop] != null)
73 {
74 this.PopState();
75 }
76 }
77
78 this.tick = function(dt)
79 {
80 if(this.stateStack[this.stateTop]!=null)
81 {
82 this.stateStack[this.stateTop].Update(dt);
83 this.stateStack[this.stateTop].Resize();
84 this.stateStack[this.stateTop].Draw();
85 }
86 }
87}
88
89g_enableBenchmarks = true;
90function Engine()
91{
92 // map of scene graphs to names
93 this.sceneMap = [];
94
95 // number of states on the stack
96 this.stateTop = undefined;
97
98 // size of the browser window
99 this.lastWindowWidth = window.innerWidth;
100 this.lastWindowHeight = window.innerHeight;
101
102 this.defaultContext = null;
103
104 this.lightManager = null;
105
106 clearColor = [0.0, 0.0, 0.0, 0.0];
107
108 panelObjectManager = new objectManager();
109
110 this.initializeComplete = false;
111
112 this.RDGECanvas = null;
113
114 /*
115 * a map of canvas names to renderer
116 */
117 this.canvasToRendererMap = {};
118
119 /*
120 * states to canvas map - maps a state stack to the canvas context it belongs to
121 */
122 this.canvasNameToStateStack = {};
123
124 /*
125 * the list of context's that are active
126 */
127 this.canvasCtxList = [];
128
129 /*
130 * regex object to verify runtime object is not some sort of exploit
131 */
132 invalidObj = new RegExp("([()]|function)");
133
134 isValidObj = function( name )
135 {
136 // do a quick test make sure user isn't trying to execute a function
137 if(invalidObj.test(name))
138 {
139 window.console.error("invalid object name passed to RDGE, " + name + " - looks like a function");
140 return false;
141 }
142
143 return true;
144 }
145
146 /*
147 * The context definition - every context shares these parameters
148 */
149 contextDef = function()
150 {
151 this.id = null;
152 this.renderer = null;
153 this.ctxStateManager = null;
154 this.startUpState = null;
155 this.sceneGraphMap = [];
156 this.currentScene = null;
157 this.getScene = function()
158 {
159 return this.sceneGraphMap[this.currentScene];
160 }
161 this.debug =
162 {
163 'frameCounter' : 0,
164 'mat4CallCount': 0
165 }
166 }
167
168 // maintains the contexts
169 contextManager = new objectManager();
170 this.ctxMan = contextManager;
171
172 // the context currently being updated
173 contextManager.currentCtx = null;
174
175 contextManager._addObject = contextManager.addObject;
176 contextManager.contextMap = {};
177
178 contextManager.addObject = function( context )
179 {
180 this.contextMap[context.id] = context;
181 return this._addObject(context);
182 }
183
184 contextManager.start = function()
185 {
186 var len = this.objects.length;
187 for(var i = 0; i < len; ++i)
188 {
189 // set the current context
190 contextManager.currentCtx = this.objects[i];
191 this.objects[i].ctxStateManager.PushState(this.objects[i].startUpState);
192 }
193 }
194
195 contextManager.forEach = function(cb)
196 {
197 var len = this.objects.length;
198 for(var i = 0; i < len; ++i)
199 {
200 cb(this.objects[i]);
201 }
202 }
203
204 this.getContext = function( optCanvasID )
205 {
206 if(!optCanvasID)
207 {
208 return contextManager.currentCtx;
209 }
210 else
211 {
212 return contextManager.contextMap[optCanvasID];
213 }
214 }
215
216 /*
217 * give the contextID (canvas id) of the context to set
218 */
219 this.setContext = function( contextID )
220 {
221 contextManager.currentCtx = contextManager.contextMap[contextID];