4af5ac4d5d2f3a47502b6ac0f7b678a8ad8ebf11
[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         //this doesn't work anymore for relative paths, so use BLO_read_from_memory instead
94         //BlendFileData *bfd= BLO_read_from_file(filename, &error);
95         FILE* file = fopen(filename,"rb");
96         BlendFileData *bfd  = 0;
97         if (file)
98         {
99                 fseek(file, 0L, SEEK_END);
100                 int len= ftell(file);
101                 fseek(file, 0L, SEEK_SET);      
102                 char* filebuffer= new char[len];//MEM_mallocN(len, "text_buffer");
103                 int sizeread = fread(filebuffer,len,1,file);
104                 if (sizeread==1){
105                         bfd = BLO_read_from_memory(filebuffer, len, &error);
106                 } else {
107                         error = BRE_UNABLE_TO_READ;
108                 }
109                 fclose(file);
110                 // the memory is not released in BLO_read_from_memory, must do it here
111                 delete filebuffer;
112         } else {
113                 error = BRE_UNABLE_TO_OPEN;
114         }
115
116         if (!bfd) {
117                 printf("Loading %s failed: %s\n", filename, BLO_bre_as_string(error));
118         }
119         
120         return bfd;
121 }
122
123 extern "C" void StartKetsjiShell(struct ScrArea *area,
124                                                                  char* scenename,
125                                                                  struct Main* maggie1,
126                                                                  struct SpaceIpo *sipo,
127                                                                  int always_use_expand_framing)
128 {
129         int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
130         
131         Main* blenderdata = maggie1;
132
133         char* startscenename = scenename;
134         char pathname[160];
135         strcpy (pathname, blenderdata->name);
136         STR_String exitstring = "";
137         BlendFileData *bfd= NULL;
138
139         // Acquire Python's GIL (global interpreter lock)
140         // so we can safely run Python code and API calls
141         PyGILState_STATE gilstate = PyGILState_Ensure();
142
143         bgl::InitExtensions(true);
144
145         do
146         {
147                 View3D *v3d= (View3D*) area->spacedata.first;
148
149                 // get some preferences
150                 SYS_SystemHandle syshandle = SYS_GetSystem();
151                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
152                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
153                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
154                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
155                 bool game2ipo = (SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
156                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
157                 bool usemat = false, useglslmat = false;
158
159                 if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
160                         usemat = (SYS_GetCommandLineInt(syshandle, "blender_material", 0) != 0);
161
162                 // create the canvas, rasterizer and rendertools
163                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
164                 canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
165                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
166                 RAS_IRasterizer* rasterizer = NULL;
167                 
168                 if(displaylists) {
169                         if (GLEW_VERSION_1_1)
170                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
171                         else
172                                 rasterizer = new RAS_ListRasterizer(canvas);
173                 }
174                 else if (GLEW_VERSION_1_1)
175                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
176                 else
177                         rasterizer = new RAS_OpenGLRasterizer(canvas);
178                 
179                 // create the inputdevices
180                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
181                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
182                 
183                 // create a networkdevice
184                 NG_NetworkDeviceInterface* networkdevice = new
185                         NG_LoopBackNetworkDeviceInterface();
186                 
187                 // get an audiodevice
188                 SND_DeviceManager::Subscribe();
189                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
190                 audiodevice->UseCD();
191                 
192                 // create a ketsji/blendersystem (only needed for timing and stuff)
193                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
194                 
195                 // create the ketsjiengine
196                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
197                 
198                 // set the devices
199                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
200                 ketsjiengine->SetMouseDevice(mousedevice);
201                 ketsjiengine->SetNetworkDevice(networkdevice);
202                 ketsjiengine->SetCanvas(canvas);
203                 ketsjiengine->SetRenderTools(rendertools);
204                 ketsjiengine->SetRasterizer(rasterizer);
205                 ketsjiengine->SetNetworkDevice(networkdevice);
206                 ketsjiengine->SetAudioDevice(audiodevice);
207                 ketsjiengine->SetUseFixedTime(usefixed);
208                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
209
210                 
211         
212                 // some blender stuff
213                 MT_CmMatrix4x4 projmat;
214                 MT_CmMatrix4x4 viewmat;
215                 int i;
216                 
217                 for (i = 0; i < 16; i++)
218                 {
219                         float *viewmat_linear= (float*) v3d->viewmat;
220                         viewmat.setElem(i, viewmat_linear[i]);
221                 }
222                 for (i = 0; i < 16; i++)
223                 {
224                         float *projmat_linear = (float*) area->winmat;
225                         projmat.setElem(i, projmat_linear[i]);
226                 }
227                 
228                 float camzoom = (1.41421 + (v3d->camzoom / 50.0));
229                 camzoom *= camzoom;
230                 camzoom = 4.0 / camzoom;
231                 
232                 ketsjiengine->SetDrawType(v3d->drawtype);
233                 ketsjiengine->SetCameraZoom(camzoom);
234                 
235                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
236                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
237                 {
238                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
239                         if (bfd) BLO_blendfiledata_free(bfd);
240                         
241                         char basedpath[240];
242                         // base the actuator filename with respect
243                         // to the original file working directory
244                         if (exitstring != "")
245                                 strcpy(basedpath, exitstring.Ptr());
246
247                         BLI_convertstringcode(basedpath, pathname);
248                         bfd = load_game_data(basedpath);
249                         
250                         // if it wasn't loaded, try it forced relative
251                         if (!bfd)
252                         {
253                                 // just add "//" in front of it
254                                 char temppath[242];
255                                 strcpy(temppath, "//");
256                                 strcat(temppath, basedpath);
257                                 
258                                 BLI_convertstringcode(temppath, pathname);
259                                 bfd = load_game_data(temppath);
260                         }
261                         
262                         // if we got a loaded blendfile, proceed
263                         if (bfd)
264                         {
265                                 blenderdata = bfd->main;
266                                 startscenename = bfd->curscene->id.name + 2;
267                         }
268                         // else forget it, we can't find it
269                         else
270                         {
271                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
272                         }
273                 }
274                 
275                 Scene *blscene = NULL;
276                 if (!bfd)
277                 {
278                         blscene = (Scene*) blenderdata->scene.first;
279                         for (Scene *sce= (Scene*) blenderdata->scene.first; sce; sce= (Scene*) sce->id.next)
280                         {
281                                 if (startscenename == (sce->id.name+2))
282                                 {
283                                         blscene = sce;
284                                         break;
285                                 }
286                         }
287                 } else {
288                         blscene = bfd->curscene;
289                 }
290
291                 if (blscene)
292                 {
293                         int startFrame = blscene->r.cfra;
294                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
295                 }
296
297
298                 // Quad buffered needs a special window.
299                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
300                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
301                 
302                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
303                 {
304                         if (v3d->persp != V3D_CAMOB)
305                         {
306                                 ketsjiengine->EnableCameraOverride(startscenename);
307                                 ketsjiengine->SetCameraOverrideUseOrtho((v3d->persp == V3D_ORTHO));
308                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
309                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
310                         }
311                         
312                         // create a scene converter, create and convert the startingscene
313                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata,sipo, ketsjiengine);
314                         ketsjiengine->SetSceneConverter(sceneconverter);
315                         sceneconverter->addInitFromFrame=false;
316                         if (always_use_expand_framing)
317                                 sceneconverter->SetAlwaysUseExpandFraming(true);
318                         
319                         if(usemat)
320                                 sceneconverter->SetMaterials(true);
321                         if(useglslmat)
322                                 sceneconverter->SetGLSLMaterials(true);
323                                         
324                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
325                                 mousedevice,
326                                 networkdevice,
327                                 audiodevice,
328                                 startscenename);
329                         
330                         // some python things
331                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
332                         ketsjiengine->SetPythonDictionary(dictionaryobject);
333                         initRasterizer(rasterizer, canvas);
334                         PyObject *gameLogic = initGameLogic(startscene);
335                         initGameKeys();
336                         initPythonConstraintBinding();
337
338
339                         if (sceneconverter)
340                         {
341                                 // convert and add scene
342                                 sceneconverter->ConvertScene(
343                                         startscenename,
344                                         startscene,
345                                         dictionaryobject,
346                                         keyboarddevice,
347                                         rendertools,
348                                         canvas);
349                                 ketsjiengine->AddScene(startscene);
350                                 
351                                 // init the rasterizer
352                                 rasterizer->Init();
353                                 
354                                 // start the engine
355                                 ketsjiengine->StartEngine(true);
356                                 
357
358                                 // Set the animation playback rate for ipo's and actions
359                                 // the framerate below should patch with FPS macro defined in blendef.h
360                                 // Could be in StartEngine set the framerate, we need the scene to do this
361                                 ketsjiengine->SetAnimFrameRate( (((double) blscene->r.frs_sec) / blscene->r.frs_sec_base) );
362                                 
363                                 // the mainloop
364                                 while (!exitrequested)
365                                 {
366                                         // first check if we want to exit
367                                         exitrequested = ketsjiengine->GetExitCode();
368                                         
369                                         // kick the engine
370                                         bool render = ketsjiengine->NextFrame();
371                                         
372                                         if (render)
373                                         {
374                                                 // render the frame
375                                                 ketsjiengine->Render();
376                                         }
377                                         
378                                         // test for the ESC key
379                                         while (qtest())
380                                         {
381                                                 short val; 
382                                                 unsigned short event = extern_qread(&val);
383                                                 
384                                                 if (keyboarddevice->ConvertBlenderEvent(event,val))
385                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
386                                                 
387                                                         /* Coordinate conversion... where
388                                                         * should this really be?
389                                                 */
390                                                 if (event==MOUSEX) {
391                                                         val = val - scrarea_get_win_x(area);
392                                                 } else if (event==MOUSEY) {
393                                                         val = scrarea_get_win_height(area) - (val - scrarea_get_win_y(area)) - 1;
394                                                 }
395                                                 
396                                                 mousedevice->ConvertBlenderEvent(event,val);
397                                         }
398                                 }
399                                 exitstring = ketsjiengine->GetExitString();
400                                 
401                                 // when exiting the mainloop
402                                 dictionaryClearByHand(gameLogic);
403                                 ketsjiengine->StopEngine();
404                                 exitGamePythonScripting();
405                                 networkdevice->Disconnect();
406                         }
407                         if (sceneconverter)
408                         {
409                                 delete sceneconverter;
410                                 sceneconverter = NULL;
411                         }
412                 }
413                 // set the cursor back to normal
414                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
415                 
416                 // clean up some stuff
417                 audiodevice->StopCD();
418                 
419                 if (ketsjiengine)
420                 {
421                         delete ketsjiengine;
422                         ketsjiengine = NULL;
423                 }
424                 if (kxsystem)
425                 {
426                         delete kxsystem;
427                         kxsystem = NULL;
428                 }
429                 if (networkdevice)
430                 {
431                         delete networkdevice;
432                         networkdevice = NULL;
433                 }
434                 if (keyboarddevice)
435                 {
436                         delete keyboarddevice;
437                         keyboarddevice = NULL;
438                 }
439                 if (mousedevice)
440                 {
441                         delete mousedevice;
442                         mousedevice = NULL;
443                 }
444                 if (rasterizer)
445                 {
446                         delete rasterizer;
447                         rasterizer = NULL;
448                 }
449                 if (rendertools)
450                 {
451                         delete rendertools;
452                         rendertools = NULL;
453                 }
454                 if (canvas)
455                 {
456                         delete canvas;
457                         canvas = NULL;
458                 }
459                 SND_DeviceManager::Unsubscribe();
460         
461         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
462
463         if (bfd) BLO_blendfiledata_free(bfd);
464
465         // Release Python's GIL
466         PyGILState_Release(gilstate);
467 }
468
469 extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
470                                                                  char* scenename,
471                                                                  struct Main* maggie,
472                                                                  struct SpaceIpo *sipo,
473                                                                  int always_use_expand_framing)
474 {
475     int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
476
477         Main* blenderdata = maggie;
478
479         char* startscenename = scenename;
480         char pathname[160];
481         strcpy (pathname, maggie->name);
482         STR_String exitstring = "";
483         BlendFileData *bfd= NULL;
484
485         // Acquire Python's GIL (global interpreter lock)
486         // so we can safely run Python code and API calls
487         PyGILState_STATE gilstate = PyGILState_Ensure();
488
489         bgl::InitExtensions(true);
490
491         do
492         {
493
494                 // get some preferences
495                 SYS_SystemHandle syshandle = SYS_GetSystem();
496                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
497                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
498                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
499                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
500                 bool game2ipo = true;//(SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
501                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
502                 bool usemat = false;
503
504                 // create the canvas, rasterizer and rendertools
505                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
506                 //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
507                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
508                 RAS_IRasterizer* rasterizer = NULL;
509
510                 if(displaylists) {
511                         if (GLEW_VERSION_1_1)
512                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
513                         else
514                                 rasterizer = new RAS_ListRasterizer(canvas);
515                 }
516                 else if (GLEW_VERSION_1_1)
517                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
518                 else
519                         rasterizer = new RAS_OpenGLRasterizer(canvas);
520
521                 // create the inputdevices
522                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
523                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
524
525                 // create a networkdevice
526                 NG_NetworkDeviceInterface* networkdevice = new
527                         NG_LoopBackNetworkDeviceInterface();
528
529                 // get an audiodevice
530                 SND_DeviceManager::Subscribe();
531                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
532                 audiodevice->UseCD();
533
534                 // create a ketsji/blendersystem (only needed for timing and stuff)
535                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
536
537                 // create the ketsjiengine
538                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
539
540                 int i;
541
542                 Scene *blscene = NULL;
543                 if (!bfd)
544                 {
545                         blscene = (Scene*) maggie->scene.first;
546                         for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
547                         {
548                                 if (startscenename == (sce->id.name+2))
549                                 {
550                                         blscene = sce;
551                                         break;
552                                 }
553                         }
554                 } else {
555                         blscene = bfd->curscene;
556                 }
557         int cframe,startFrame;
558                 if (blscene)
559                 {
560                         cframe=blscene->r.cfra;
561                         startFrame = blscene->r.sfra;
562                         blscene->r.cfra=startFrame;
563                         update_for_newframe();
564                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
565                 }
566
567                 // Quad buffered needs a special window.
568                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
569                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
570
571                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
572                 {
573                         // create a scene converter, create and convert the startingscene
574                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie,sipo, ketsjiengine);
575                         ketsjiengine->SetSceneConverter(sceneconverter);
576                         sceneconverter->addInitFromFrame=true;
577                         
578                         if (always_use_expand_framing)
579                                 sceneconverter->SetAlwaysUseExpandFraming(true);
580
581                         if(usemat)
582                                 sceneconverter->SetMaterials(true);
583
584                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
585                                 mousedevice,
586                                 networkdevice,
587                                 audiodevice,
588                                 startscenename);
589                         // some python things
590                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
591                         ketsjiengine->SetPythonDictionary(dictionaryobject);
592                         initRasterizer(rasterizer, canvas);
593                         PyObject *gameLogic = initGameLogic(startscene);
594                         initGameKeys();
595                         initPythonConstraintBinding();
596
597                         if (sceneconverter)
598                         {
599                                 // convert and add scene
600                                 sceneconverter->ConvertScene(
601                                         startscenename,
602                                         startscene,
603                                         dictionaryobject,
604                                         keyboarddevice,
605                                         rendertools,
606                                         canvas);
607                                 ketsjiengine->AddScene(startscene);
608
609                                 // start the engine
610                                 ketsjiengine->StartEngine(false);
611                                 
612                                 ketsjiengine->SetUseFixedTime(true);
613                                 
614                                 ketsjiengine->SetTicRate(
615                                         (double) blscene->r.frs_sec /
616                                         (double) blscene->r.frs_sec_base);
617
618                                 // the mainloop
619                                 while ((blscene->r.cfra<=blscene->r.efra)&&(!exitrequested))
620                                 {
621                     printf("frame %i\n",blscene->r.cfra);
622                     // first check if we want to exit
623                                         exitrequested = ketsjiengine->GetExitCode();
624         
625                                         // kick the engine
626                                         ketsjiengine->NextFrame();
627                                     blscene->r.cfra=blscene->r.cfra+1;
628                                     update_for_newframe();
629                                         
630                                 }
631                                 exitstring = ketsjiengine->GetExitString();
632                         }
633                         if (sceneconverter)
634                         {
635                                 delete sceneconverter;
636                                 sceneconverter = NULL;
637                         }
638                 }
639                 blscene->r.cfra=cframe;
640                 // set the cursor back to normal
641                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
642
643                 // clean up some stuff
644                 audiodevice->StopCD();
645                 if (ketsjiengine)
646                 {
647                         delete ketsjiengine;
648                         ketsjiengine = NULL;
649                 }
650                 if (kxsystem)
651                 {
652                         delete kxsystem;
653                         kxsystem = NULL;
654                 }
655                 if (networkdevice)
656                 {
657                         delete networkdevice;
658                         networkdevice = NULL;
659                 }
660                 if (keyboarddevice)
661                 {
662                         delete keyboarddevice;
663                         keyboarddevice = NULL;
664                 }
665                 if (mousedevice)
666                 {
667                         delete mousedevice;
668                         mousedevice = NULL;
669                 }
670                 SND_DeviceManager::Unsubscribe();
671
672         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
673         if (bfd) BLO_blendfiledata_free(bfd);
674
675         // Release Python's GIL
676         PyGILState_Release(gilstate);
677 }