Merged 15170:15635 from trunk (no conflicts or even merges)
[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                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module.
336                         initGameKeys();
337                         initPythonConstraintBinding();
338
339
340                         if (sceneconverter)
341                         {
342                                 // convert and add scene
343                                 sceneconverter->ConvertScene(
344                                         startscenename,
345                                         startscene,
346                                         dictionaryobject,
347                                         keyboarddevice,
348                                         rendertools,
349                                         canvas);
350                                 ketsjiengine->AddScene(startscene);
351                                 
352                                 // init the rasterizer
353                                 rasterizer->Init();
354                                 
355                                 // start the engine
356                                 ketsjiengine->StartEngine(true);
357                                 
358
359                                 // Set the animation playback rate for ipo's and actions
360                                 // the framerate below should patch with FPS macro defined in blendef.h
361                                 // Could be in StartEngine set the framerate, we need the scene to do this
362                                 ketsjiengine->SetAnimFrameRate( (((double) blscene->r.frs_sec) / blscene->r.frs_sec_base) );
363                                 
364                                 // the mainloop
365                                 while (!exitrequested)
366                                 {
367                                         // first check if we want to exit
368                                         exitrequested = ketsjiengine->GetExitCode();
369                                         
370                                         // kick the engine
371                                         bool render = ketsjiengine->NextFrame();
372                                         
373                                         if (render)
374                                         {
375                                                 // render the frame
376                                                 ketsjiengine->Render();
377                                         }
378                                         
379                                         // test for the ESC key
380                                         while (qtest())
381                                         {
382                                                 short val; 
383                                                 unsigned short event = extern_qread(&val);
384                                                 
385                                                 if (keyboarddevice->ConvertBlenderEvent(event,val))
386                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
387                                                 
388                                                         /* Coordinate conversion... where
389                                                         * should this really be?
390                                                 */
391                                                 if (event==MOUSEX) {
392                                                         val = val - scrarea_get_win_x(area);
393                                                 } else if (event==MOUSEY) {
394                                                         val = scrarea_get_win_height(area) - (val - scrarea_get_win_y(area)) - 1;
395                                                 }
396                                                 
397                                                 mousedevice->ConvertBlenderEvent(event,val);
398                                         }
399                                 }
400                                 exitstring = ketsjiengine->GetExitString();
401                                 
402                                 // when exiting the mainloop
403                                 
404                                 // Clears the dictionary by hand:
405                                 // This prevents, extra references to global variables
406                                 // inside the GameLogic dictionary when the python interpreter is finalized.
407                                 // which allows the scene to safely delete them :)
408                                 // see: (space.c)->start_game
409                                 PyDict_Clear(PyModule_GetDict(gameLogic));
410                                 
411                                 ketsjiengine->StopEngine();
412                                 exitGamePythonScripting();
413                                 networkdevice->Disconnect();
414                         }
415                         if (sceneconverter)
416                         {
417                                 delete sceneconverter;
418                                 sceneconverter = NULL;
419                         }
420                 }
421                 // set the cursor back to normal
422                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
423                 
424                 // clean up some stuff
425                 audiodevice->StopCD();
426                 
427                 if (ketsjiengine)
428                 {
429                         delete ketsjiengine;
430                         ketsjiengine = NULL;
431                 }
432                 if (kxsystem)
433                 {
434                         delete kxsystem;
435                         kxsystem = NULL;
436                 }
437                 if (networkdevice)
438                 {
439                         delete networkdevice;
440                         networkdevice = NULL;
441                 }
442                 if (keyboarddevice)
443                 {
444                         delete keyboarddevice;
445                         keyboarddevice = NULL;
446                 }
447                 if (mousedevice)
448                 {
449                         delete mousedevice;
450                         mousedevice = NULL;
451                 }
452                 if (rasterizer)
453                 {
454                         delete rasterizer;
455                         rasterizer = NULL;
456                 }
457                 if (rendertools)
458                 {
459                         delete rendertools;
460                         rendertools = NULL;
461                 }
462                 if (canvas)
463                 {
464                         delete canvas;
465                         canvas = NULL;
466                 }
467                 SND_DeviceManager::Unsubscribe();
468         
469         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
470
471         if (bfd) BLO_blendfiledata_free(bfd);
472
473         // Release Python's GIL
474         PyGILState_Release(gilstate);
475 }
476
477 extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
478                                                                  char* scenename,
479                                                                  struct Main* maggie,
480                                                                  struct SpaceIpo *sipo,
481                                                                  int always_use_expand_framing)
482 {
483     int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
484
485         Main* blenderdata = maggie;
486
487         char* startscenename = scenename;
488         char pathname[160];
489         strcpy (pathname, maggie->name);
490         STR_String exitstring = "";
491         BlendFileData *bfd= NULL;
492
493         // Acquire Python's GIL (global interpreter lock)
494         // so we can safely run Python code and API calls
495         PyGILState_STATE gilstate = PyGILState_Ensure();
496
497         bgl::InitExtensions(true);
498
499         do
500         {
501
502                 // get some preferences
503                 SYS_SystemHandle syshandle = SYS_GetSystem();
504                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
505                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
506                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
507                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
508                 bool game2ipo = true;//(SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
509                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
510                 bool usemat = false;
511
512                 // create the canvas, rasterizer and rendertools
513                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
514                 //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
515                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
516                 RAS_IRasterizer* rasterizer = NULL;
517
518                 if(displaylists) {
519                         if (GLEW_VERSION_1_1)
520                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
521                         else
522                                 rasterizer = new RAS_ListRasterizer(canvas);
523                 }
524                 else if (GLEW_VERSION_1_1)
525                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
526                 else
527                         rasterizer = new RAS_OpenGLRasterizer(canvas);
528
529                 // create the inputdevices
530                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
531                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
532
533                 // create a networkdevice
534                 NG_NetworkDeviceInterface* networkdevice = new
535                         NG_LoopBackNetworkDeviceInterface();
536
537                 // get an audiodevice
538                 SND_DeviceManager::Subscribe();
539                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
540                 audiodevice->UseCD();
541
542                 // create a ketsji/blendersystem (only needed for timing and stuff)
543                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
544
545                 // create the ketsjiengine
546                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
547
548                 int i;
549
550                 Scene *blscene = NULL;
551                 if (!bfd)
552                 {
553                         blscene = (Scene*) maggie->scene.first;
554                         for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
555                         {
556                                 if (startscenename == (sce->id.name+2))
557                                 {
558                                         blscene = sce;
559                                         break;
560                                 }
561                         }
562                 } else {
563                         blscene = bfd->curscene;
564                 }
565         int cframe,startFrame;
566                 if (blscene)
567                 {
568                         cframe=blscene->r.cfra;
569                         startFrame = blscene->r.sfra;
570                         blscene->r.cfra=startFrame;
571                         update_for_newframe();
572                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
573                 }
574
575                 // Quad buffered needs a special window.
576                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
577                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
578
579                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
580                 {
581                         // create a scene converter, create and convert the startingscene
582                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie,sipo, ketsjiengine);
583                         ketsjiengine->SetSceneConverter(sceneconverter);
584                         sceneconverter->addInitFromFrame=true;
585                         
586                         if (always_use_expand_framing)
587                                 sceneconverter->SetAlwaysUseExpandFraming(true);
588
589                         if(usemat)
590                                 sceneconverter->SetMaterials(true);
591
592                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
593                                 mousedevice,
594                                 networkdevice,
595                                 audiodevice,
596                                 startscenename);
597                         // some python things
598                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
599                         ketsjiengine->SetPythonDictionary(dictionaryobject);
600                         initRasterizer(rasterizer, canvas);
601                         PyObject *gameLogic = initGameLogic(startscene);
602                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module
603                         initGameKeys();
604                         initPythonConstraintBinding();
605
606                         if (sceneconverter)
607                         {
608                                 // convert and add scene
609                                 sceneconverter->ConvertScene(
610                                         startscenename,
611                                         startscene,
612                                         dictionaryobject,
613                                         keyboarddevice,
614                                         rendertools,
615                                         canvas);
616                                 ketsjiengine->AddScene(startscene);
617
618                                 // start the engine
619                                 ketsjiengine->StartEngine(false);
620                                 
621                                 ketsjiengine->SetUseFixedTime(true);
622                                 
623                                 ketsjiengine->SetTicRate(
624                                         (double) blscene->r.frs_sec /
625                                         (double) blscene->r.frs_sec_base);
626
627                                 // the mainloop
628                                 while ((blscene->r.cfra<=blscene->r.efra)&&(!exitrequested))
629                                 {
630                     printf("frame %i\n",blscene->r.cfra);
631                     // first check if we want to exit
632                                         exitrequested = ketsjiengine->GetExitCode();
633         
634                                         // kick the engine
635                                         ketsjiengine->NextFrame();
636                                     blscene->r.cfra=blscene->r.cfra+1;
637                                     update_for_newframe();
638                                         
639                                 }
640                                 exitstring = ketsjiengine->GetExitString();
641                         }
642                         if (sceneconverter)
643                         {
644                                 delete sceneconverter;
645                                 sceneconverter = NULL;
646                         }
647                 }
648                 blscene->r.cfra=cframe;
649                 // set the cursor back to normal
650                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
651
652                 // clean up some stuff
653                 audiodevice->StopCD();
654                 if (ketsjiengine)
655                 {
656                         delete ketsjiengine;
657                         ketsjiengine = NULL;
658                 }
659                 if (kxsystem)
660                 {
661                         delete kxsystem;
662                         kxsystem = NULL;
663                 }
664                 if (networkdevice)
665                 {
666                         delete networkdevice;
667                         networkdevice = NULL;
668                 }
669                 if (keyboarddevice)
670                 {
671                         delete keyboarddevice;
672                         keyboarddevice = NULL;
673                 }
674                 if (mousedevice)
675                 {
676                         delete mousedevice;
677                         mousedevice = NULL;
678                 }
679                 SND_DeviceManager::Unsubscribe();
680
681         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
682         if (bfd) BLO_blendfiledata_free(bfd);
683
684         // Release Python's GIL
685         PyGILState_Release(gilstate);
686 }