Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 167 vs 168 for /trunk/Source Code/Core/Kernel.cpp

Diff revisions: vs.
  @@ -15,7 +15,6 @@
15 15 #include "Dynamics/Physics/Headers/PXDevice.h"
16 16 #include "Hardware/Input/Headers/InputInterface.h"
17 17 #include "Managers/Headers/FrameListenerManager.h"
18 - #include "Rendering/Camera/Headers/FreeFlyCamera.h"
19 18 #include "Hardware/Video/Headers/RenderStateBlock.h"
20 19
21 20 #include "Rendering/Headers/ForwardRenderer.h"
  @@ -34,37 +33,32 @@
34 33 _argc(argc),
35 34 _argv(argv),
36 35 _APP(parentApp),
37 - _GFX(GFX_DEVICE), //Video
38 - _SFX(SFXDevice::getInstance()), //Audio
39 - _PFX(PHYSICS_DEVICE), //Physics
40 - _GUI(GUI::getInstance()), //Graphical User Interface
41 - _sceneMgr(SceneManager::getInstance()), //Scene Manager
42 - _shaderMgr(ShaderManager::getInstance()),//Shader Manager
43 - _camera(New FreeFlyCamera()), //Default camera
36 + _GFX(GFXDevice::getOrCreateInstance()), //Video
37 + _SFX(SFXDevice::getOrCreateInstance()), //Audio
38 + _PFX(PXDevice::getOrCreateInstance()), //Physics
39 + _GUI(GUI::getOrCreateInstance()), //Graphical User Interface
40 + _sceneMgr(SceneManager::getOrCreateInstance()), //Scene Manager
41 + _shaderMgr(ShaderManager::getOrCreateInstance()),//Shader Manager
44 42 _cameraMgr(New CameraManager()), //Camera manager
45 - _frameMgr(FrameListenerManager::getInstance()),
46 - _lightPool(LightManager::getInstance()),//Light pool
47 - _inputInterface(InputInterface::getInstance()) //Input interface
43 + _frameMgr(FrameListenerManager::getOrCreateInstance()),
44 + _lightPool(LightManager::getOrCreateInstance()),//Light pool
45 + _inputInterface(InputInterface::getOrCreateInstance()),//Input interface
46 + _activeScene(NULL)
48 47 {
48 + ResourceCache::createInstance();
49 + PostFX::createInstance();
49 50 assert(_cameraMgr != NULL);
50 51 //If camera has been updated, set a callback to inform the current scene
51 - _cameraMgr->addCameraChangeListener(DELEGATE_BIND(&SceneManager::updateCamera, //update camera
52 - DELEGATE_REF(_sceneMgr),
53 - _cameraMgr->getActiveCamera()));
54 - // force all lights to update on camera change (to keep them still actually)
55 - _camera->addUpdateListener(DELEGATE_BIND(&LightManager::update, DELEGATE_REF(_lightPool)));
56 - //As soon as a camera is added to the camera manager, the manager is responsible for cleaning it up
57 - _cameraMgr->addNewCamera("defaultCamera",_camera);
52 + _cameraMgr->addCameraChangeListener(DELEGATE_BIND(&SceneManager::updateCameras, //update camera
53 + DELEGATE_REF(_sceneMgr)));
58 54 //We have an A.I. thread, a networking thread, a PhysX thread, the main update/rendering thread
59 55 //so how many threads do we allocate for tasks? That's up to the programmer to decide for each app
60 56 //we add the A.I. thread in the same pool as it's a task. ReCast should also use this ...
61 - _mainTaskPool = New boost::threadpool::pool(THREAD_LIMIT + 1 /*A.I.*/);
57 + _mainTaskPool = New boost::threadpool::pool(Config::THREAD_LIMIT + 1 /*A.I.*/);
62 58 }
63 59
64 60 Kernel::~Kernel(){
65 61 SAFE_DELETE(_cameraMgr);
66 - _inputInterface.terminate();
67 - _inputInterface.DestroyInstance();
68 62 _mainTaskPool->wait();
69 63 SAFE_DELETE(_mainTaskPool);
70 64 }
  @@ -99,7 +93,7 @@
99 93 }
100 94
101 95 Kernel::Idle();
102 - //Restore CPU to default state: clear buffers and set default render state
96 + //Restore GPU to default state: clear buffers and set default render state
103 97 GFX_DEVICE.beginFrame();
104 98 //Get current frame-to-application speed factor
105 99 Framerate::getInstance().SetSpeedFactor();
  @@ -110,7 +104,7 @@
110 104 //Process physics
111 105 PHYSICS_DEVICE.process();
112 106 //Process the current frame
113 - _keepAlive = APP.getKernel()->MainLoopScene();
107 + _keepAlive = APP.getKernel()->MainLoopScene(evt);
114 108 //Launch the FRAME_PROCESS event (a.k.a. the frame processing has ended event)
115 109 frameMgr.createEvent(FRAME_EVENT_PROCESS,evt);
116 110 _keepAlive = frameMgr.frameRenderingQueued(evt);
  @@ -122,46 +116,50 @@
122 116 }
123 117
124 118 void Kernel::FirstLoop(){
119 + ParamHandler& par = ParamHandler::getInstance();
120 + bool shadowMappingEnabled = par.getParam<bool>("rendering.enableShadows");
121 + par.setParam("rendering.enableShadows",false);
125 122 //Skip one frame so all resources can be built while the splash screen is displayed
126 123 MainLoopApp();
127 124 Kernel::_applicationReady = true;
128 125 Framerate::getInstance().benchmark(true);
129 126 //Hide splash screen
130 - ParamHandler& par = ParamHandler::getInstance();
131 127 GFX_DEVICE.changeResolution(par.getParam<U16>("runtime.resolutionWidth"),
132 128 par.getParam<U16>("runtime.resolutionHeight"));
133 129 GFX_DEVICE.setWindowPos(10,50);
134 130 //Bind main render loop
135 131 _mainLoopCallback = DELEGATE_REF(Kernel::MainLoopApp);
132 + par.setParam("rendering.enableShadows", shadowMappingEnabled);
136 133 }
137 134
138 - const I32 SKIP_TICKS = 1000 / TICKS_PER_SECOND;
139 - bool Kernel::MainLoopScene(){
135 + static const I32 SKIP_TICKS = 1000 / Config::TICKS_PER_SECOND;
136 + bool Kernel::MainLoopScene(FrameEvent& evt){
140 137 if(_renderingPaused) {
141 138 Idle();
142 139 return true;
143 140 }
144 - //Update camera position
145 - _camera->RenderLookAt();
146 - //Set the current scene's active camera
147 - _sceneMgr.updateCamera(_camera);
141 + _activeScene = GET_ACTIVE_SCENE();
142 + _cameraMgr->tick(_currentTimeMS);
143 +
148 144 _loops = 0;
149 145 //while(_currentTimeMS > _nextGameTick && _loops < MAX_FRAMESKIP) {
150 146 // Update scene based on input
151 147 _sceneMgr.processInput();
148 +
152 149 // process all scene events
153 150 _sceneMgr.processTasks(_currentTimeMS);
154 151
155 152 //Update the scene state based on current time
156 153 _sceneMgr.updateSceneState(_currentTimeMS);
154 +
157 155 //Update physics
158 156 _PFX.update();
159 -
157 +
160 158 _nextGameTick += SKIP_TICKS;
161 159 _loops++;
162 160 //}
163 161
164 - bool sceneRenderState = presentToScreen();
162 + bool sceneRenderState = presentToScreen(evt);
165 163 // Get input events
166 164 _inputInterface.tick();
167 165 // Draw the GUI
  @@ -169,19 +167,22 @@
169 167 return sceneRenderState;
170 168 }
171 169
172 - bool Kernel::presentToScreen(){
170 + bool Kernel::presentToScreen(FrameEvent& evt){
171 + _frameMgr.createEvent(FRAME_PRERENDER_START,evt);
172 + if(!_frameMgr.framePreRenderStarted(evt)) return false;
173 +
173 174 //Prepare scene for rendering
174 175 _sceneMgr.preRender();
176 +
175 177 //perform time-sensitive shader tasks
176 178 _shaderMgr.tick(_currentTimeMS);
177 179
178 - // Inform listeners that we finished pre-rendering
179 - FrameEvent evt;
180 +
180 181 _frameMgr.createEvent(FRAME_PRERENDER_END,evt);
181 182 if(!_frameMgr.framePreRenderEnded(evt)) return false;
182 183
183 184 // Render the scene adding any post-processing effects that we have active
184 - PostFX::getInstance().render(_camera);
185 + PostFX::getInstance().render();
185 186
186 187 _sceneMgr.postRender();
187 188
  @@ -194,66 +195,83 @@
194 195 I8 windowId = -1;
195 196 I8 returnCode = 0;
196 197 ParamHandler& par = ParamHandler::getInstance();
197 - Console::getInstance().bindConsoleOutput(DELEGATE_BIND(&GUIConsole::printText, GUI::getInstance().getConsole(),_1,_2));
198 +
199 + Console::getInstance().bindConsoleOutput(DELEGATE_BIND(&GUIConsole::printText,
200 + GUI::getInstance().getConsole(),
201 + _1,_2));
198 202 //Using OpenGL for rendering as default
199 203 _GFX.setApi(OpenGL);
204 +
200 205 //Target FPS is usually 60. So all movement is capped around that value
201 - Framerate::getInstance().Init(TARGET_FRAME_RATE);
206 + Framerate::getInstance().Init(Config::TARGET_FRAME_RATE);
207 +
202 208 //Load info from XML files
203 209 std::string startupScene = XML::loadScripts(entryPoint);
210 +
204 211 //Create mem log file
205 212 std::string mem = par.getParam<std::string>("memFile");
206 - if(mem.compare("none") == 0) mem = "mem.log";
207 - _APP.setMemoryLogFile(mem);
213 + _APP.setMemoryLogFile(mem.compare("none") == 0 ? "mem.log" : mem);
214 +
208 215 PRINT_FN(Locale::get("START_RENDER_INTERFACE"));
209 216 vec2<U16> resolution(par.getParam<U16>("runtime.resolutionWidth"),
210 217 par.getParam<U16>("runtime.resolutionHeight"));
211 218 windowId = _GFX.initHardware(resolution/2,_argc,_argv);
219 +
212 220 //If we could not initialize the graphics device, exit
213 - if(windowId < 0) return windowId;
221 + if(windowId < 0)
222 + return windowId;
214 223
224 + //Load and render the splash screen
215 225 _GFX.setRenderStage(FINAL_STAGE);
216 - //Load the splash screen
217 - _GFX.changeResolution(400,300);
218 - GUISplash splashScreen("divideLogo.jpg",vec2<U16>(400,300));
219 226 _GFX.beginFrame();
220 - splashScreen.render();
227 + GUISplash("divideLogo.jpg",vec2<U16>(400,300)).render();
221 228 _GFX.endFrame();
229 +
222 230 //We start of with a forward renderer. Change it to something else on scene load ... or ... whenever
223 231 _GFX.setRenderer(New ForwardRenderer());
224 232 _GFX.registerKernel(this);
233 +
225 234 _lightPool.init();
235 +
226 236 PRINT_FN(Locale::get("START_SOUND_INTERFACE"));
227 - returnCode = _SFX.initHardware();
228 - if(returnCode < 0){
237 + if((returnCode = _SFX.initHardware()) < 0)
229 238 return returnCode;
230 - }
239 +
231 240 PRINT_FN(Locale::get("START_PHYSICS_INTERFACE"));
232 - returnCode =_PFX.initPhysics(TARGET_FRAME_RATE);
233 - if(returnCode < 0) {
241 + if((returnCode =_PFX.initPhysics(Config::TARGET_FRAME_RATE)) < 0)
234 242 return returnCode;
235 - }
243 +
236 244 PostFX::getInstance().init(resolution);
245 +
237 246 //Bind the kernel with the input interface
238 247 _inputInterface.initialize(this,par.getParam<std::string>("appTitle"),(size_t)_GFX.getHWND());
248 +
239 249 //Load default material
240 250 PRINT_FN(Locale::get("LOAD_DEFAULT_MATERIAL"));
241 251 XML::loadMaterialXML(par.getParam<std::string>("scriptLocation")+"/defaultMaterial");
252 +
242 253 //Initialize GUI with our current resolution
243 254 GUI::getInstance().init();
244 - if(!_sceneMgr.load(startupScene, resolution, _camera)){ ///< Load the scene with a default camera
255 +
256 + _sceneMgr.init();
257 +
258 + if(!_sceneMgr.load(startupScene, resolution, _cameraMgr)){ //< Load the scene
245 259 ERROR_FN(Locale::get("ERROR_SCENE_LOAD"),startupScene.c_str());
246 260 return MISSING_SCENE_DATA;
247 261 }
262 +
248 263 if(!_sceneMgr.checkLoadFlag()){
249 264 ERROR_FN(Locale::get("ERROR_SCENE_LOAD_NOT_CALLED"),startupScene.c_str());
250 265 return MISSING_SCENE_LOAD_CALL;
251 266 }
267 +
252 268 PRINT_FN(Locale::get("INITIAL_DATA_LOADED"));
269 +
253 270 PRINT_FN(Locale::get("CREATE_AI_ENTITIES_START"));
254 271 //Initialize and start the AI
255 272 _sceneMgr.initializeAI(true);
256 273 PRINT_FN(Locale::get("CREATE_AI_ENTITIES_END"));
274 + _activeScene = GET_ACTIVE_SCENE();
257 275 _GUI.cacheResolution(resolution);
258 276 _nextGameTick = GETMSTIME();
259 277 return windowId;
  @@ -262,20 +280,20 @@
262 280 void Kernel::beginLogicLoop(){
263 281 PRINT_FN(Locale::get("START_RENDER_LOOP"));
264 282 //lock the scene
265 - GET_ACTIVE_SCENE()->state()->toggleRunningState(true);
283 + GET_ACTIVE_SCENE()->state().toggleRunningState(true);
266 284 //The first loop compiles all the textures, so, do not render the first frame
267 285 _mainLoopCallback = DELEGATE_REF(Kernel::FirstLoop);
268 286 //Target FPS is define in "config.h". So all movement is capped around that value
269 287 //start rendering
270 - GFX_DEVICE.initDevice(TARGET_FRAME_RATE);
288 + GFX_DEVICE.initDevice(Config::TARGET_FRAME_RATE);
271 289 }
272 290
273 291 void Kernel::Shutdown(){
274 292 _keepAlive = false;
275 293 //release the scene
276 - GET_ACTIVE_SCENE()->state()->toggleRunningState(false);
294 + GET_ACTIVE_SCENE()->state().toggleRunningState(false);
277 295 PRINT_FN(Locale::get("STOP_GUI"));
278 - Console::getInstance().bindConsoleOutput(boost::function2<void, std::string, bool>());
296 + Console::getInstance().bindConsoleOutput(boost::function2<void, const char*, bool>());
279 297 _GUI.DestroyInstance(); ///Deactivate GUI
280 298 PRINT_FN(Locale::get("STOP_PHYSICS_INTERFACE"));
281 299 _PFX.exitPhysics();
  @@ -286,12 +304,17 @@
286 304 AIManager::getInstance().Destroy();
287 305 AIManager::getInstance().DestroyInstance();
288 306 _sceneMgr.DestroyInstance();
307 + _shaderMgr.Destroy();
308 + _lightPool.DestroyInstance();
289 309 _GFX.closeRenderer();
290 310 PRINT_FN(Locale::get("STOP_RESOURCE_CACHE"));
291 311 ResourceCache::getInstance().DestroyInstance();
292 312 PRINT_FN(Locale::get("STOP_ENGINE_OK"));
293 -
313 + _frameMgr.DestroyInstance();
314 + _shaderMgr.DestroyInstance();
294 315 PRINT_FN(Locale::get("STOP_HARDWARE"));
316 + _inputInterface.terminate();
317 + _inputInterface.DestroyInstance();
295 318 _SFX.closeAudioApi();
296 319 _SFX.DestroyInstance();
297 320 _GFX.closeRenderingApi();
  @@ -304,11 +327,7 @@
304 327 ShaderManager::getInstance().refresh();
305 328 Application& app = Application::getInstance();
306 329 //minimized
307 - if(w == 0 || h == 0){
308 - _renderingPaused = true;
309 - }else{
310 - _renderingPaused = false;
311 - }
330 + _renderingPaused = (w == 0 || h == 0);
312 331 app.setResolutionWidth(w);
313 332 app.setResolutionHeight(h);
314 333 //Update post-processing render targets and buffers
  @@ -324,65 +343,65 @@
324 343
325 344 bool Kernel::onKeyDown(const OIS::KeyEvent& key) {
326 345 if(GUI::getInstance().keyCheck(key,true)){
327 - GET_ACTIVE_SCENE()->onKeyDown(key);
346 + _activeScene->onKeyDown(key);
328 347 }
329 348 return true;
330 349 }
331 350
332 351 bool Kernel::onKeyUp(const OIS::KeyEvent& key) {
333 352 if(GUI::getInstance().keyCheck(key,false)){
334 - GET_ACTIVE_SCENE()->onKeyUp(key);
353 + _activeScene->onKeyUp(key);
335 354 }
336 355 return true;
337 356 }
338 357
339 358 bool Kernel::onMouseMove(const OIS::MouseEvent& arg) {
340 359 if(GUI::getInstance().checkItem(arg)){
341 - GET_ACTIVE_SCENE()->onMouseMove(arg);
360 + _activeScene->onMouseMove(arg);
342 361 }
343 362 return true;
344 363 }
345 364
346 365 bool Kernel::onMouseClickDown(const OIS::MouseEvent& arg,OIS::MouseButtonID button) {
347 366 if(GUI::getInstance().clickCheck(button,true)){
348 - GET_ACTIVE_SCENE()->onMouseClickDown(arg,button);
367 + _activeScene->onMouseClickDown(arg,button);
349 368 }
350 369 return true;
351 370 }
352 371
353 372 bool Kernel::onMouseClickUp(const OIS::MouseEvent& arg,OIS::MouseButtonID button) {
354 373 if(GUI::getInstance().clickCheck(button,false)){
355 - GET_ACTIVE_SCENE()->onMouseClickUp(arg,button);
374 + _activeScene->onMouseClickUp(arg,button);
356 375 }
357 376 return true;
358 377 }
359 378
360 379 bool Kernel::onJoystickMoveAxis(const OIS::JoyStickEvent& arg,I8 axis,I32 deadZone) {
361 - GET_ACTIVE_SCENE()->onJoystickMoveAxis(arg,axis,deadZone);
380 + _activeScene->onJoystickMoveAxis(arg,axis,deadZone);
362 381 return true;
363 382 }
364 383
365 384 bool Kernel::onJoystickMovePOV(const OIS::JoyStickEvent& arg,I8 pov){
366 - GET_ACTIVE_SCENE()->onJoystickMovePOV(arg,pov);
385 + _activeScene->onJoystickMovePOV(arg,pov);
367 386 return true;
368 387 }
369 388
370 389 bool Kernel::onJoystickButtonDown(const OIS::JoyStickEvent& arg,I8 button){
371 - GET_ACTIVE_SCENE()->onJoystickButtonDown(arg,button);
390 + _activeScene->onJoystickButtonDown(arg,button);
372 391 return true;
373 392 }
374 393
375 394 bool Kernel::onJoystickButtonUp(const OIS::JoyStickEvent& arg, I8 button){
376 - GET_ACTIVE_SCENE()->onJoystickButtonUp(arg,button);
395 + _activeScene->onJoystickButtonUp(arg,button);
377 396 return true;
378 397 }
379 398
380 399 bool Kernel::sliderMoved( const OIS::JoyStickEvent &arg, I8 index){
381 - GET_ACTIVE_SCENE()->sliderMoved(arg,index);
400 + _activeScene->sliderMoved(arg,index);
382 401 return true;
383 402 }
384 403
385 404 bool Kernel::vector3Moved( const OIS::JoyStickEvent &arg, I8 index){
386 - GET_ACTIVE_SCENE()->vector3Moved(arg,index);
405 + _activeScene->vector3Moved(arg,index);
387 406 return true;
388 407 }