diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/runtime.js')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/runtime.js | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/runtime.js b/js/helper-classes/RDGE/src/core/script/runtime.js new file mode 100644 index 00000000..8d8fdf38 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/runtime.js | |||
@@ -0,0 +1,237 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No 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 | // runtime globals | ||
8 | g_Engine = new Engine(); | ||
9 | g_width = 0; | ||
10 | g_height = 0; | ||
11 | g_cam = null; | ||
12 | g_camMoveSpeed = 25.0; | ||
13 | gl = null; //webGL handle | ||
14 | g_worldObjects = []; | ||
15 | g_shaderMan = null | ||
16 | g_defaultTex = null; | ||
17 | g_alphaTex = null; | ||
18 | g_hiQu = true; | ||
19 | g_meshMan = null; | ||
20 | |||
21 | /* | ||
22 | * RDGEState a RDGEstate is an interface that is defined by the user and called by the engine | ||
23 | */ | ||
24 | function RDGEState() | ||
25 | { | ||
26 | this.init = function() | ||
27 | { | ||
28 | |||
29 | } | ||
30 | |||
31 | this.update = function(dt) | ||
32 | { | ||
33 | |||
34 | } | ||
35 | |||
36 | this.draw = function() | ||
37 | { | ||
38 | |||
39 | } | ||
40 | |||
41 | this.resize = function() | ||
42 | { | ||
43 | |||
44 | } | ||
45 | |||
46 | this.shutdown = function() | ||
47 | { | ||
48 | |||
49 | } | ||
50 | |||
51 | this.onComplete = function() | ||
52 | { | ||
53 | |||
54 | } | ||
55 | } | ||
56 | |||
57 | /* | ||
58 | * Calling this makes sure the passed in run state has all the functions | ||
59 | * that are required, adding dummy functions where needed | ||
60 | */ | ||
61 | function validateUserState( userState ) | ||
62 | { | ||
63 | if(!userState.init) | ||
64 | { | ||
65 | userState.init = function(){}; | ||
66 | } | ||
67 | if(!userState.update) | ||
68 | { | ||
69 | userState.update = function(dt) | ||
70 | { | ||
71 | var currentScene = g_Engine.getContext().currentScene; | ||
72 | currentScene = g_Engine.getScene(currentScene); | ||
73 | |||
74 | if(currentScene != null) | ||
75 | currentScene.update(dt); | ||
76 | } | ||
77 | } | ||
78 | if(!userState.draw) | ||
79 | { | ||
80 | userState.draw = function() | ||
81 | { | ||
82 | var currentScene = g_Engine.getContext().currentScene; | ||
83 | currentScene = g_Engine.getScene(currentScene); | ||
84 | |||
85 | if(currentScene==null) | ||
86 | return; | ||
87 | |||
88 | currentScene.render(); | ||
89 | } | ||
90 | } | ||
91 | if(!userState.resize) | ||
92 | { | ||
93 | userState.resize = function(){}; | ||
94 | } | ||
95 | if(!userState.shutdown) | ||
96 | { | ||
97 | userState.shutdown = function(){}; | ||
98 | } | ||
99 | if(!userState.onComplete) | ||
100 | { | ||
101 | userState.onComplete = function(){}; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | /* | ||
106 | * Used to start the RDGE engine, pass the initState and runState, both of which are RDGEState objects | ||
107 | * initState is used to asynchronously load scene data while allowing you to render and update if needed | ||
108 | * runState is used clear the execution path for regular rendering and updating once loading is complete | ||
109 | * @param initState - the initialization state, false if you don't want to use one | ||
110 | * @param runState - the run state | ||
111 | */ | ||
112 | function RDGEStart(canvasOrID) | ||
113 | { | ||
114 | var canvas = canvasOrID; | ||
115 | |||
116 | if (typeof(canvasOrID) === "string") | ||
117 | canvas = document.getElementById(canvasOrID); | ||
118 | |||
119 | if (!canvas) | ||
120 | return; | ||
121 | |||
122 | g_Engine.registerCanvas(canvas); | ||
123 | |||
124 | canvas.task = new RDGETask(canvas, true); | ||
125 | |||
126 | if (!g_shaderMan) | ||
127 | g_shaderMan = new ShaderManager(); | ||
128 | |||
129 | if (!g_meshMan) | ||
130 | g_meshMan = new MeshManager(); | ||
131 | |||
132 | // start rdge | ||
133 | if (!g_Engine.initializeComplete) | ||
134 | g_Engine.init(); | ||
135 | } | ||
136 | |||
137 | function RDGEStop() | ||
138 | { | ||
139 | if(RDGEShutdown != undefined) | ||
140 | { | ||
141 | RDGEShutdown(); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | // the runtime interface | ||
146 | function IRuntime() | ||
147 | { | ||
148 | this.init = null; // called when state is pushed on the stack | ||
149 | this.ReInit = null; // called when state above is popped from stack | ||
150 | this.Resize = null; // called every tick to setup the viewport/projection | ||
151 | this.Update = null; // called every tick to update scene | ||
152 | this.Draw = null; // called every tick to draw scene | ||
153 | this.Shutdown = null; // called when state is popped from stack | ||
154 | } | ||
155 | |||
156 | // add the connection Pool's to this list for auto polling | ||
157 | g_poolList = []; | ||
158 | function ConnPoll() | ||
159 | { | ||
160 | var len = g_poolList.length; | ||
161 | for(var i = 0; i < len; ++i) | ||
162 | { | ||
163 | g_poolList[i].Poll(); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | /* RDGE Task */ | ||
168 | RDGERequestAnimationFrame = (function() { | ||
169 | return window.requestAnimationFrame || | ||
170 | window.webkitRequestAnimationFrame || | ||
171 | window.mozRequestAnimationFrame || | ||
172 | window.oRequestAnimationFrame || | ||
173 | window.msRequestAnimationFrame || | ||
174 | function(/* function FrameRequestCallback */callback, /* DOMElement Element */element) { | ||
175 | window.setTimeout(callback, 1000 / 60); | ||
176 | }; | ||
177 | })(); | ||
178 | |||
179 | RDGETask = (function() { | ||
180 | var tasks = {}; | ||
181 | return function(canvas, startNow) { | ||
182 | this.id = canvas.id; | ||
183 | this.currTime = 0.0; | ||
184 | this.lastTime = 0.0; | ||
185 | this.running = false; | ||
186 | this.context = null; | ||
187 | |||
188 | if (!canvas) { | ||
189 | return; | ||
190 | } | ||
191 | |||
192 | this.context = g_Engine.ctxMan.handleToObject(canvas.rdgeCtxHandle); | ||
193 | |||
194 | tasks[this.id] = function() { | ||
195 | if (!self.running) { | ||
196 | return; | ||
197 | } | ||
198 | |||
199 | self.currTime = new Date().getTime(); | ||
200 | var dt = (self.currTime - self.lastTime) / 1000.0; | ||
201 | |||
202 | self.step(dt); | ||
203 | |||
204 | RDGERequestAnimationFrame(tasks[self.id], canvas); | ||
205 | |||
206 | self.lastTime = self.currTime; | ||
207 | } | ||
208 | |||
209 | this.start = function() { | ||
210 | this.running = true; | ||
211 | this.currTime = new Date().getTime(); | ||
212 | this.lastTime = this.currTime; | ||
213 | tasks[this.id](); | ||
214 | } | ||
215 | |||
216 | this.stop = function() { | ||
217 | this.running = false; | ||
218 | } | ||
219 | |||
220 | this.kill = function() { | ||
221 | this.running = false; | ||
222 | tasks[this.id] = null; | ||
223 | } | ||
224 | |||
225 | this.step = function(dt) { | ||
226 | contextManager.currentCtx = this.context; | ||
227 | this.context.fpsTracker.sample(); | ||
228 | this.context.ctxStateManager.tick(dt); | ||