Merge from trunk 16122-16307
[blender.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Blender's Ketsji startpoint
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <signal.h>
36 #include <stdlib.h>
37
38 #ifdef WIN32
39 // don't show stl-warnings
40 #pragma warning (disable:4786)
41 #endif
42
43 #include "GL/glew.h"
44
45 #include "KX_BlenderGL.h"
46 #include "KX_BlenderCanvas.h"
47 #include "KX_BlenderKeyboardDevice.h"
48 #include "KX_BlenderMouseDevice.h"
49 #include "KX_BlenderRenderTools.h"
50 #include "KX_BlenderSystem.h"
51 #include "BL_Material.h"
52
53 #include "KX_KetsjiEngine.h"
54 #include "KX_BlenderSceneConverter.h"
55 #include "KX_PythonInit.h"
56 #include "KX_PyConstraintBinding.h"
57
58 #include "RAS_GLExtensionManager.h"
59 #include "RAS_OpenGLRasterizer.h"
60 #include "RAS_VAOpenGLRasterizer.h"
61 #include "RAS_ListRasterizer.h"
62
63 #include "NG_LoopBackNetworkDeviceInterface.h"
64 #include "SND_DeviceManager.h"
65
66 #include "SYS_System.h"
67
68         /***/
69
70 #include "DNA_view3d_types.h"
71 #include "DNA_screen_types.h"
72 #include "BKE_global.h"
73 #include "BIF_screen.h"
74 #include "BIF_scrarea.h"
75
76 #include "BKE_main.h"   
77 #include "BLI_blenlib.h"
78 #include "BLO_readfile.h"
79 #include "DNA_scene_types.h"
80         /***/
81
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85 #include "BSE_headerbuttons.h"
86 void update_for_newframe();
87 #ifdef __cplusplus
88 }
89 #endif
90
91 static BlendFileData *load_game_data(char *filename) {
92         BlendReadError error;
93         BlendFileData *bfd= BLO_read_from_file(filename, &error);
94         if (!bfd) {
95                 printf("Loading %s failed: %s\n", filename, BLO_bre_as_string(error));
96         }
97         return bfd;
98 }
99
100 extern "C" void StartKetsjiShell(struct ScrArea *area,
101                                                                  char* scenename,
102                                                                  struct Main* maggie1,
103                                                                  struct SpaceIpo *sipo,
104                                                                  int always_use_expand_framing)
105 {
106         int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
107         
108         Main* blenderdata = maggie1;
109
110         char* startscenename = scenename;
111         char pathname[160];
112         strcpy (pathname, blenderdata->name);
113         STR_String exitstring = "";
114         BlendFileData *bfd= NULL;
115
116         // Acquire Python's GIL (global interpreter lock)
117         // so we can safely run Python code and API calls
118         PyGILState_STATE gilstate = PyGILState_Ensure();
119         
120         PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
121         
122         bgl::InitExtensions(true);
123
124         do
125         {
126                 View3D *v3d= (View3D*) area->spacedata.first;
127
128                 // get some preferences
129                 SYS_SystemHandle syshandle = SYS_GetSystem();
130                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
131                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
132                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
133                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
134                 bool game2ipo = (SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
135                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
136                 bool usemat = false, useglslmat = false;
137
138                 if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
139                         usemat = (SYS_GetCommandLineInt(syshandle, "blender_material", 0) != 0);
140
141                 // create the canvas, rasterizer and rendertools
142                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
143                 canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
144                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
145                 RAS_IRasterizer* rasterizer = NULL;
146                 
147                 if(displaylists) {
148                         if (GLEW_VERSION_1_1)
149                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
150                         else
151                                 rasterizer = new RAS_ListRasterizer(canvas);
152                 }
153                 else if (GLEW_VERSION_1_1)
154                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
155                 else
156                         rasterizer = new RAS_OpenGLRasterizer(canvas);
157                 
158                 // create the inputdevices
159                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
160                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
161                 
162                 // create a networkdevice
163                 NG_NetworkDeviceInterface* networkdevice = new
164                         NG_LoopBackNetworkDeviceInterface();
165                 
166                 // get an audiodevice
167                 SND_DeviceManager::Subscribe();
168                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
169                 audiodevice->UseCD();
170                 
171                 // create a ketsji/blendersystem (only needed for timing and stuff)
172                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
173                 
174                 // create the ketsjiengine
175                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
176                 
177                 // set the devices
178                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
179                 ketsjiengine->SetMouseDevice(mousedevice);
180                 ketsjiengine->SetNetworkDevice(networkdevice);
181                 ketsjiengine->SetCanvas(canvas);
182                 ketsjiengine->SetRenderTools(rendertools);
183                 ketsjiengine->SetRasterizer(rasterizer);
184                 ketsjiengine->SetNetworkDevice(networkdevice);
185                 ketsjiengine->SetAudioDevice(audiodevice);
186                 ketsjiengine->SetUseFixedTime(usefixed);
187                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
188
189                 
190         
191                 // some blender stuff
192                 MT_CmMatrix4x4 projmat;
193                 MT_CmMatrix4x4 viewmat;
194                 int i;
195                 
196                 for (i = 0; i < 16; i++)
197                 {
198                         float *viewmat_linear= (float*) v3d->viewmat;
199                         viewmat.setElem(i, viewmat_linear[i]);
200                 }
201                 for (i = 0; i < 16; i++)
202                 {
203                         float *projmat_linear = (float*) area->winmat;
204                         projmat.setElem(i, projmat_linear[i]);
205                 }
206                 
207                 float camzoom = (1.41421 + (v3d->camzoom / 50.0));
208                 camzoom *= camzoom;
209                 camzoom = 4.0 / camzoom;
210                 
211                 ketsjiengine->SetDrawType(v3d->drawtype);
212                 ketsjiengine->SetCameraZoom(camzoom);
213                 
214                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
215                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
216                 {
217                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
218                         if (bfd) BLO_blendfiledata_free(bfd);
219                         
220                         char basedpath[240];
221                         // base the actuator filename with respect
222                         // to the original file working directory
223                         if (exitstring != "")
224                                 strcpy(basedpath, exitstring.Ptr());
225
226                         BLI_convertstringcode(basedpath, pathname);
227                         bfd = load_game_data(basedpath);
228                         
229                         // if it wasn't loaded, try it forced relative
230                         if (!bfd)
231                         {
232                                 // just add "//" in front of it
233                                 char temppath[242];
234                                 strcpy(temppath, "//");
235                                 strcat(temppath, basedpath);
236                                 
237                                 BLI_convertstringcode(temppath, pathname);
238                                 bfd = load_game_data(temppath);
239                         }
240                         
241                         // if we got a loaded blendfile, proceed
242                         if (bfd)
243                         {
244                                 blenderdata = bfd->main;
245                                 startscenename = bfd->curscene->id.name + 2;
246                         }
247                         // else forget it, we can't find it
248                         else
249                         {
250                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
251                         }
252                 }
253                 
254                 Scene *blscene = NULL;
255                 if (!bfd)
256                 {
257                         blscene = (Scene*) blenderdata->scene.first;
258                         for (Scene *sce= (Scene*) blenderdata->scene.first; sce; sce= (Scene*) sce->id.next)
259                         {
260                                 if (startscenename == (sce->id.name+2))
261                                 {
262                                         blscene = sce;
263                                         break;
264                                 }
265                         }
266                 } else {
267                         blscene = bfd->curscene;
268                 }
269
270                 if (blscene)
271                 {
272                         int startFrame = blscene->r.cfra;
273                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
274                 }
275
276
277                 // Quad buffered needs a special window.
278                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
279                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
280                 
281                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
282                 {
283                         if (v3d->persp != V3D_CAMOB)
284                         {
285                                 ketsjiengine->EnableCameraOverride(startscenename);
286                                 ketsjiengine->SetCameraOverrideUseOrtho((v3d->persp == V3D_ORTHO));
287                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
288                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
289                         }
290                         
291                         // create a scene converter, create and convert the startingscene
292                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata,sipo, ketsjiengine);
293                         ketsjiengine->SetSceneConverter(sceneconverter);
294                         sceneconverter->addInitFromFrame=false;
295                         if (always_use_expand_framing)
296                                 sceneconverter->SetAlwaysUseExpandFraming(true);
297                         
298                         if(usemat)
299                                 sceneconverter->SetMaterials(true);
300                         if(useglslmat)
301                                 sceneconverter->SetGLSLMaterials(true);
302                                         
303                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
304                                 mousedevice,
305                                 networkdevice,
306                                 audiodevice,
307                                 startscenename);
308                         
309                         // some python things
310                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
311                         ketsjiengine->SetPythonDictionary(dictionaryobject);
312                         initRasterizer(rasterizer, canvas);
313                         PyObject *gameLogic = initGameLogic(startscene);
314                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module.
315                         PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
316                         initGameKeys();
317                         initPythonConstraintBinding();
318                         initMathutils();
319
320                         if (sceneconverter)
321                         {
322                                 // convert and add scene
323                                 sceneconverter->ConvertScene(
324                                         startscenename,
325                                         startscene,
326                                         dictionaryobject,
327                                         keyboarddevice,
328                                         rendertools,
329                                         canvas);
330                                 ketsjiengine->AddScene(startscene);
331                                 
332                                 // init the rasterizer
333                                 rasterizer->Init();
334                                 
335                                 // start the engine
336                                 ketsjiengine->StartEngine(true);
337                                 
338
339                                 // Set the animation playback rate for ipo's and actions
340                                 // the framerate below should patch with FPS macro defined in blendef.h
341                                 // Could be in StartEngine set the framerate, we need the scene to do this
342                                 ketsjiengine->SetAnimFrameRate( (((double) blscene->r.frs_sec) / blscene->r.frs_sec_base) );
343                                 
344                                 // the mainloop
345                                 while (!exitrequested)
346                                 {
347                                         // first check if we want to exit
348                                         exitrequested = ketsjiengine->GetExitCode();
349                                         
350                                         // kick the engine
351                                         bool render = ketsjiengine->NextFrame();
352                                         
353                                         if (render)
354                                         {
355                                                 // render the frame
356                                                 ketsjiengine->Render();
357                                         }
358                                         
359                                         // test for the ESC key
360                                         while (qtest())
361                                         {
362                                                 short val; 
363                                                 unsigned short event = extern_qread(&val);
364                                                 
365                                                 if (keyboarddevice->ConvertBlenderEvent(event,val))
366                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
367                                                 
368                                                         /* Coordinate conversion... where
369                                                         * should this really be?
370                                                 */
371                                                 if (event==MOUSEX) {
372                                                         val = val - scrarea_get_win_x(area);
373                                                 } else if (event==MOUSEY) {
374                                                         val = scrarea_get_win_height(area) - (val - scrarea_get_win_y(area)) - 1;
375                                                 }
376                                                 
377                                                 mousedevice->ConvertBlenderEvent(event,val);
378                                         }
379                                 }
380                                 exitstring = ketsjiengine->GetExitString();
381                                 
382                                 // when exiting the mainloop
383                                 
384                                 // Clears the dictionary by hand:
385                                 // This prevents, extra references to global variables
386                                 // inside the GameLogic dictionary when the python interpreter is finalized.
387                                 // which allows the scene to safely delete them :)
388                                 // see: (space.c)->start_game
389                                 PyDict_Clear(PyModule_GetDict(gameLogic));
390                                 PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict);
391                                 
392                                 ketsjiengine->StopEngine();
393                                 exitGamePythonScripting();
394                                 networkdevice->Disconnect();
395                         }
396                         if (sceneconverter)
397                         {
398                                 delete sceneconverter;
399                                 sceneconverter = NULL;
400                         }
401                 }
402                 // set the cursor back to normal
403                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
404                 
405                 // clean up some stuff
406                 audiodevice->StopCD();
407                 
408                 if (ketsjiengine)
409                 {
410                         delete ketsjiengine;
411                         ketsjiengine = NULL;
412                 }
413                 if (kxsystem)
414                 {
415                         delete kxsystem;
416                         kxsystem = NULL;
417                 }
418                 if (networkdevice)
419                 {
420                         delete networkdevice;
421                         networkdevice = NULL;
422                 }
423                 if (keyboarddevice)
424                 {
425                         delete keyboarddevice;
426                         keyboarddevice = NULL;
427                 }
428                 if (mousedevice)
429                 {
430                         delete mousedevice;
431                         mousedevice = NULL;
432                 }
433                 if (rasterizer)
434                 {
435                         delete rasterizer;
436                         rasterizer = NULL;
437                 }
438                 if (rendertools)
439                 {
440                         delete rendertools;
441                         rendertools = NULL;
442                 }
443                 if (canvas)
444                 {
445                         delete canvas;
446                         canvas = NULL;
447                 }
448                 SND_DeviceManager::Unsubscribe();
449         
450         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
451
452         if (bfd) BLO_blendfiledata_free(bfd);
453
454         // Release Python's GIL
455         PyGILState_Release(gilstate);
456 }
457
458 extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
459                                                                  char* scenename,
460                                                                  struct Main* maggie,
461                                                                  struct SpaceIpo *sipo,
462                                                                  int always_use_expand_framing)
463 {
464     int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
465
466         Main* blenderdata = maggie;
467
468         char* startscenename = scenename;
469         char pathname[160];
470         strcpy (pathname, maggie->name);
471         STR_String exitstring = "";
472         BlendFileData *bfd= NULL;
473
474         // Acquire Python's GIL (global interpreter lock)
475         // so we can safely run Python code and API calls
476         PyGILState_STATE gilstate = PyGILState_Ensure();
477
478         bgl::InitExtensions(true);
479
480         do
481         {
482
483                 // get some preferences
484                 SYS_SystemHandle syshandle = SYS_GetSystem();
485                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
486                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
487                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
488                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
489                 bool game2ipo = true;//(SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
490                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
491                 bool usemat = false;
492
493                 // create the canvas, rasterizer and rendertools
494                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
495                 //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
496                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
497                 RAS_IRasterizer* rasterizer = NULL;
498
499                 if(displaylists) {
500                         if (GLEW_VERSION_1_1)
501                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
502                         else
503                                 rasterizer = new RAS_ListRasterizer(canvas);
504                 }
505                 else if (GLEW_VERSION_1_1)
506                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
507                 else
508                         rasterizer = new RAS_OpenGLRasterizer(canvas);
509
510                 // create the inputdevices
511                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
512                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
513
514                 // create a networkdevice
515                 NG_NetworkDeviceInterface* networkdevice = new
516                         NG_LoopBackNetworkDeviceInterface();
517
518                 // get an audiodevice
519                 SND_DeviceManager::Subscribe();
520                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
521                 audiodevice->UseCD();
522
523                 // create a ketsji/blendersystem (only needed for timing and stuff)
524                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
525
526                 // create the ketsjiengine
527                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
528
529                 int i;
530
531                 Scene *blscene = NULL;
532                 if (!bfd)
533                 {
534                         blscene = (Scene*) maggie->scene.first;
535                         for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
536                         {
537                                 if (startscenename == (sce->id.name+2))
538                                 {
539                                         blscene = sce;
540                                         break;
541                                 }
542                         }
543                 } else {
544                         blscene = bfd->curscene;
545                 }
546         int cframe,startFrame;
547                 if (blscene)
548                 {
549                         cframe=blscene->r.cfra;
550                         startFrame = blscene->r.sfra;
551                         blscene->r.cfra=startFrame;
552                         update_for_newframe();
553                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
554                 }
555
556                 // Quad buffered needs a special window.
557                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
558                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
559
560                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
561                 {
562                         // create a scene converter, create and convert the startingscene
563                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie,sipo, ketsjiengine);
564                         ketsjiengine->SetSceneConverter(sceneconverter);
565                         sceneconverter->addInitFromFrame=true;
566                         
567                         if (always_use_expand_framing)
568                                 sceneconverter->SetAlwaysUseExpandFraming(true);
569
570                         if(usemat)
571                                 sceneconverter->SetMaterials(true);
572
573                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
574                                 mousedevice,
575                                 networkdevice,
576                                 audiodevice,
577                                 startscenename);
578                         // some python things
579                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
580                         ketsjiengine->SetPythonDictionary(dictionaryobject);
581                         initRasterizer(rasterizer, canvas);
582                         PyObject *gameLogic = initGameLogic(startscene);
583                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module
584                         initGameKeys();
585                         initPythonConstraintBinding();
586                         initMathutils();
587
588                         if (sceneconverter)
589                         {
590                                 // convert and add scene
591                                 sceneconverter->ConvertScene(
592                                         startscenename,
593                                         startscene,
594                                         dictionaryobject,
595                                         keyboarddevice,
596                                         rendertools,
597                                         canvas);
598                                 ketsjiengine->AddScene(startscene);
599
600                                 // start the engine
601                                 ketsjiengine->StartEngine(false);
602                                 
603                                 ketsjiengine->SetUseFixedTime(true);
604                                 
605                                 ketsjiengine->SetTicRate(
606                                         (double) blscene->r.frs_sec /
607                                         (double) blscene->r.frs_sec_base);
608
609                                 // the mainloop
610                                 while ((blscene->r.cfra<=blscene->r.efra)&&(!exitrequested))
611                                 {
612                     printf("frame %i\n",blscene->r.cfra);
613                     // first check if we want to exit
614                                         exitrequested = ketsjiengine->GetExitCode();
615         
616                                         // kick the engine
617                                         ketsjiengine->NextFrame();
618                                     blscene->r.cfra=blscene->r.cfra+1;
619                                     update_for_newframe();
620                                         
621                                 }
622                                 exitstring = ketsjiengine->GetExitString();
623                         }
624                         if (sceneconverter)
625                         {
626                                 delete sceneconverter;
627                                 sceneconverter = NULL;
628                         }
629                 }
630                 blscene->r.cfra=cframe;
631                 // set the cursor back to normal
632                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
633
634                 // clean up some stuff
635                 audiodevice->StopCD();
636                 if (ketsjiengine)
637                 {
638                         delete ketsjiengine;
639                         ketsjiengine = NULL;
640                 }
641                 if (kxsystem)
642                 {
643                         delete kxsystem;
644                         kxsystem = NULL;
645                 }
646                 if (networkdevice)
647                 {
648                         delete networkdevice;
649                         networkdevice = NULL;
650                 }
651                 if (keyboarddevice)
652                 {
653                         delete keyboarddevice;
654                         keyboarddevice = NULL;
655                 }
656                 if (mousedevice)
657                 {
658                         delete mousedevice;
659                         mousedevice = NULL;
660                 }
661                 SND_DeviceManager::Unsubscribe();
662
663         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
664         if (bfd) BLO_blendfiledata_free(bfd);
665
666         // Release Python's GIL
667         PyGILState_Release(gilstate);
668 }