e78b996391ea59ccfce450ddfb810e9fbf4f791f
[blender-staging.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * Blender's Ketsji startpoint
27  */
28
29 /** \file gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp
30  *  \ingroup blroutines
31  */
32
33
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37
38 #ifdef _MSC_VER
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 #include "KX_PythonMain.h"
58
59 #include "RAS_GLExtensionManager.h"
60 #include "RAS_OpenGLRasterizer.h"
61 #include "RAS_ListRasterizer.h"
62
63 #include "NG_LoopBackNetworkDeviceInterface.h"
64
65 #include "BL_System.h"
66
67 #include "GPU_extensions.h"
68 #include "Value.h"
69
70
71 extern "C" {
72         #include "DNA_view3d_types.h"
73         #include "DNA_screen_types.h"
74         #include "DNA_userdef_types.h"
75         #include "DNA_scene_types.h"
76         #include "DNA_windowmanager_types.h"
77
78         #include "BKE_global.h"
79         #include "BKE_report.h"
80         #include "BKE_ipo.h"
81         #include "BKE_main.h"
82         #include "BKE_context.h"
83
84         /* avoid c++ conflict with 'new' */
85         #define new _new
86         #include "BKE_screen.h"
87         #undef new
88
89         #include "MEM_guardedalloc.h"
90
91         #include "BLI_blenlib.h"
92         #include "BLO_readfile.h"
93
94         #include "../../blender/windowmanager/WM_types.h"
95         #include "../../blender/windowmanager/wm_window.h"
96         #include "../../blender/windowmanager/wm_event_system.h"
97 }
98
99 #ifdef WITH_AUDASPACE
100 #  include "AUD_C-API.h"
101 #  include "AUD_I3DDevice.h"
102 #  include "AUD_IDevice.h"
103 #endif
104
105 static BlendFileData *load_game_data(char *filename)
106 {
107         ReportList reports;
108         BlendFileData *bfd;
109         
110         BKE_reports_init(&reports, RPT_STORE);
111         bfd= BLO_read_from_file(filename, &reports);
112
113         if (!bfd) {
114                 printf("Loading %s failed: ", filename);
115                 BKE_reports_print(&reports, RPT_ERROR);
116         }
117
118         BKE_reports_clear(&reports);
119
120         return bfd;
121 }
122
123 static int BL_KetsjiNextFrame(KX_KetsjiEngine *ketsjiengine, bContext *C, wmWindow *win, Scene *scene, ARegion *ar,
124                               KX_BlenderKeyboardDevice* keyboarddevice, KX_BlenderMouseDevice* mousedevice, int draw_letterbox)
125 {
126         int exitrequested;
127
128         // first check if we want to exit
129         exitrequested = ketsjiengine->GetExitCode();
130
131         // kick the engine
132         bool render = ketsjiengine->NextFrame();
133
134         if (render) {
135                 if (draw_letterbox) {
136                         // Clear screen to border color
137                         // We do this here since we set the canvas to be within the frames. This means the engine
138                         // itself is unaware of the extra space, so we clear the whole region for it.
139                         glClearColor(scene->gm.framing.col[0], scene->gm.framing.col[1], scene->gm.framing.col[2], 1.0f);
140                         glViewport(ar->winrct.xmin, ar->winrct.ymin,
141                                    BLI_rcti_size_x(&ar->winrct), BLI_rcti_size_y(&ar->winrct));
142                         glClear(GL_COLOR_BUFFER_BIT);
143                 }
144
145                 // render the frame
146                 ketsjiengine->Render();
147         }
148
149         wm_window_process_events_nosleep();
150
151         // test for the ESC key
152         //XXX while (qtest())
153         while (wmEvent *event= (wmEvent *)win->queue.first) {
154                 short val = 0;
155                 //unsigned short event = 0; //XXX extern_qread(&val);
156
157                 if (keyboarddevice->ConvertBlenderEvent(event->type,event->val))
158                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
159
160                 /* Coordinate conversion... where
161                  * should this really be?
162                  */
163                 if (event->type == MOUSEMOVE) {
164                         /* Note, not nice! XXX 2.5 event hack */
165                         val = event->x - ar->winrct.xmin;
166                         mousedevice->ConvertBlenderEvent(MOUSEX, val);
167
168                         val = ar->winy - (event->y - ar->winrct.ymin) - 1;
169                         mousedevice->ConvertBlenderEvent(MOUSEY, val);
170                 }
171                 else {
172                         mousedevice->ConvertBlenderEvent(event->type,event->val);
173                 }
174
175                 BLI_remlink(&win->queue, event);
176                 wm_event_free(event);
177         }
178
179         if (win != CTX_wm_window(C)) {
180                 exitrequested= KX_EXIT_REQUEST_OUTSIDE; /* window closed while bge runs */
181         }
182         return exitrequested;
183 }
184
185 struct BL_KetsjiNextFrameState {
186         struct KX_KetsjiEngine* ketsjiengine;
187         struct bContext *C;
188         struct wmWindow* win;
189         struct Scene* scene;
190         struct ARegion *ar;
191         KX_BlenderKeyboardDevice* keyboarddevice;
192         KX_BlenderMouseDevice* mousedevice;
193         int draw_letterbox;
194 } ketsjinextframestate;
195
196 static int BL_KetsjiPyNextFrame(void *state0)
197 {
198         BL_KetsjiNextFrameState *state = (BL_KetsjiNextFrameState *) state0;
199         return BL_KetsjiNextFrame(
200                 state->ketsjiengine, 
201                 state->C, 
202                 state->win, 
203                 state->scene, 
204                 state->ar,
205                 state->keyboarddevice, 
206                 state->mousedevice, 
207                 state->draw_letterbox);
208 }
209
210 extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *cam_frame, int always_use_expand_framing)
211 {
212         /* context values */
213         struct wmWindow *win= CTX_wm_window(C);
214         struct Scene *startscene= CTX_data_scene(C);
215         struct Main* maggie1= CTX_data_main(C);
216
217
218         RAS_Rect area_rect;
219         area_rect.SetLeft(cam_frame->xmin);
220         area_rect.SetBottom(cam_frame->ymin);
221         area_rect.SetRight(cam_frame->xmax);
222         area_rect.SetTop(cam_frame->ymax);
223
224         int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
225         Main* blenderdata = maggie1;
226
227         char* startscenename = startscene->id.name+2;
228         char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE];
229         STR_String exitstring = "";
230         BlendFileData *bfd= NULL;
231
232         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
233         BLI_strncpy(oldsce, G.main->name, sizeof(oldsce));
234 #ifdef WITH_PYTHON
235         resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
236         setGamePythonPath(G.main->name);
237
238         // Acquire Python's GIL (global interpreter lock)
239         // so we can safely run Python code and API calls
240         PyGILState_STATE gilstate = PyGILState_Ensure();
241         
242         PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
243 #endif
244         
245         bgl::InitExtensions(true);
246
247         // VBO code for derived mesh is not compatible with BGE (couldn't find why), so disable
248         int disableVBO = (U.gameflags & USER_DISABLE_VBO);
249         U.gameflags |= USER_DISABLE_VBO;
250
251         // Globals to be carried on over blender files
252         GlobalSettings gs;
253         gs.matmode= startscene->gm.matmode;
254         gs.glslflag= startscene->gm.flag;
255
256         do
257         {
258                 View3D *v3d= CTX_wm_view3d(C);
259                 RegionView3D *rv3d= CTX_wm_region_view3d(C);
260
261                 // get some preferences
262                 SYS_SystemHandle syshandle = SYS_GetSystem();
263                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
264                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
265                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
266                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
267                 bool animation_record = (SYS_GetCommandLineInt(syshandle, "animation_record", 0) != 0);
268                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
269 #ifdef WITH_PYTHON
270                 bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
271 #endif
272                 // bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
273                 bool mouse_state = startscene->gm.flag & GAME_SHOW_MOUSE;
274                 bool restrictAnimFPS = startscene->gm.flag & GAME_RESTRICT_ANIM_UPDATES;
275
276                 if (animation_record) usefixed= false; /* override since you don't want to run full-speed for sim recording */
277
278                 // create the canvas, rasterizer and rendertools
279                 RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect, ar);
280                 
281                 // default mouse state set on render panel
282                 if (mouse_state)
283                         canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
284                 else
285                         canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
286                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
287                 RAS_IRasterizer* rasterizer = NULL;
288                 
289                 //Don't use displaylists with VBOs
290                 //If auto starts using VBOs, make sure to check for that here
291                 if (displaylists && startscene->gm.raster_storage != RAS_STORE_VBO)
292                         rasterizer = new RAS_ListRasterizer(canvas, true, startscene->gm.raster_storage);
293                 else
294                         rasterizer = new RAS_OpenGLRasterizer(canvas, startscene->gm.raster_storage);
295                 
296                 // create the inputdevices
297                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
298                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
299                 
300                 // create a networkdevice
301                 NG_NetworkDeviceInterface* networkdevice = new
302                         NG_LoopBackNetworkDeviceInterface();
303
304                 //
305                 // create a ketsji/blendersystem (only needed for timing and stuff)
306                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
307                 
308                 // create the ketsjiengine
309                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
310                 
311                 // set the devices
312                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
313                 ketsjiengine->SetMouseDevice(mousedevice);
314                 ketsjiengine->SetNetworkDevice(networkdevice);
315                 ketsjiengine->SetCanvas(canvas);
316                 ketsjiengine->SetRenderTools(rendertools);
317                 ketsjiengine->SetRasterizer(rasterizer);
318                 ketsjiengine->SetUseFixedTime(usefixed);
319                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
320                 ketsjiengine->SetRestrictAnimationFPS(restrictAnimFPS);
321                 KX_KetsjiEngine::SetExitKey(ConvertKeyCode(startscene->gm.exitkey));
322
323                 //set the global settings (carried over if restart/load new files)
324                 ketsjiengine->SetGlobalSettings(&gs);
325
326 #ifdef WITH_PYTHON
327                 CValue::SetDeprecationWarnings(nodepwarnings);
328 #endif
329
330                 //lock frame and camera enabled - storing global values
331                 int tmp_lay= startscene->lay;
332                 Object *tmp_camera = startscene->camera;
333
334                 if (v3d->scenelock==0) {
335                         startscene->lay= v3d->lay;
336                         startscene->camera= v3d->camera;
337                 }
338
339                 // some blender stuff
340                 float camzoom;
341                 int draw_letterbox = 0;
342                 
343                 if (rv3d->persp==RV3D_CAMOB) {
344                         if (startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
345                                 camzoom = 1.0f;
346                                 draw_letterbox = 1;
347                         }
348                         else {
349                                 camzoom = 1.0f / BKE_screen_view3d_zoom_to_fac(rv3d->camzoom);
350                         }
351                 }
352                 else {
353                         camzoom = 2.0;
354                 }
355
356
357                 ketsjiengine->SetDrawType(v3d->drawtype);
358                 ketsjiengine->SetCameraZoom(camzoom);
359                 
360                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
361                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
362                 {
363                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
364                         if (bfd) BLO_blendfiledata_free(bfd);
365                         
366                         char basedpath[FILE_MAX];
367                         // base the actuator filename with respect
368                         // to the original file working directory
369
370                         if (exitstring != "")
371                                 strcpy(basedpath, exitstring.Ptr());
372
373                         // load relative to the last loaded file, this used to be relative
374                         // to the first file but that makes no sense, relative paths in
375                         // blend files should be relative to that file, not some other file
376                         // that happened to be loaded first
377                         BLI_path_abs(basedpath, pathname);
378                         bfd = load_game_data(basedpath);
379                         
380                         // if it wasn't loaded, try it forced relative
381                         if (!bfd)
382                         {
383                                 // just add "//" in front of it
384                                 char temppath[242];
385                                 strcpy(temppath, "//");
386                                 strcat(temppath, basedpath);
387                                 
388                                 BLI_path_abs(temppath, pathname);
389                                 bfd = load_game_data(temppath);
390                         }
391                         
392                         // if we got a loaded blendfile, proceed
393                         if (bfd)
394                         {
395                                 blenderdata = bfd->main;
396                                 startscenename = bfd->curscene->id.name + 2;
397
398                                 if (blenderdata) {
399                                         BLI_strncpy(G.main->name, blenderdata->name, sizeof(G.main->name));
400                                         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
401 #ifdef WITH_PYTHON
402                                         setGamePythonPath(G.main->name);
403 #endif
404                                 }
405                         }
406                         // else forget it, we can't find it
407                         else
408                         {
409                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
410                         }
411                 }
412
413                 Scene *scene= bfd ? bfd->curscene : (Scene *)BLI_findstring(&blenderdata->scene, startscenename, offsetof(ID, name) + 2);
414
415                 if (scene)
416                 {
417                         int startFrame = scene->r.cfra;
418                         ketsjiengine->SetAnimRecordMode(animation_record, startFrame);
419                         
420                         // Quad buffered needs a special window.
421                         if (scene->gm.stereoflag == STEREO_ENABLED) {
422                                 if (scene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
423                                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) scene->gm.stereomode);
424
425                                 rasterizer->SetEyeSeparation(scene->gm.eyeseparation);
426                         }
427
428                         rasterizer->SetBackColor(scene->gm.framing.col[0], scene->gm.framing.col[1], scene->gm.framing.col[2], 0.0f);
429                 }
430                 
431                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
432                 {
433                         if (rv3d->persp != RV3D_CAMOB)
434                         {
435                                 ketsjiengine->EnableCameraOverride(startscenename);
436                                 ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == RV3D_ORTHO));
437                                 ketsjiengine->SetCameraOverrideProjectionMatrix(MT_CmMatrix4x4(rv3d->winmat));
438                                 ketsjiengine->SetCameraOverrideViewMatrix(MT_CmMatrix4x4(rv3d->viewmat));
439                                 if (rv3d->persp == RV3D_ORTHO)
440                                 {
441                                         ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
442                                 }
443                                 else
444                                 {
445                                         ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
446                                 }
447                                 ketsjiengine->SetCameraOverrideLens(v3d->lens);
448                         }
449                         
450                         // create a scene converter, create and convert the startingscene
451                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine);
452                         ketsjiengine->SetSceneConverter(sceneconverter);
453                         sceneconverter->addInitFromFrame=false;
454                         if (always_use_expand_framing)
455                                 sceneconverter->SetAlwaysUseExpandFraming(true);
456
457                         bool usemat = false, useglslmat = false;
458
459                         if (GLEW_ARB_multitexture && GLEW_VERSION_1_1)
460                                 usemat = true;
461
462                         if (GPU_glsl_support())
463                                 useglslmat = true;
464                         else if (gs.matmode == GAME_MAT_GLSL)
465                                 usemat = false;
466
467                         if (usemat && (gs.matmode != GAME_MAT_TEXFACE))
468                                 sceneconverter->SetMaterials(true);
469                         if (useglslmat && (gs.matmode == GAME_MAT_GLSL))
470                                 sceneconverter->SetGLSLMaterials(true);
471                         if (scene->gm.flag & GAME_NO_MATERIAL_CACHING)
472                                 sceneconverter->SetCacheMaterials(false);
473                                         
474                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
475                                 mousedevice,
476                                 networkdevice,
477                                 startscenename,
478                                 scene,
479                                 canvas);
480
481 #ifdef WITH_PYTHON
482                         // some python things
483                         PyObject *gameLogic, *gameLogic_keys;
484                         setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL);
485 #endif // WITH_PYTHON
486
487                         //initialize Dome Settings
488                         if (scene->gm.stereoflag == STEREO_DOME)
489                                 ketsjiengine->InitDome(scene->gm.dome.res, scene->gm.dome.mode, scene->gm.dome.angle, scene->gm.dome.resbuf, scene->gm.dome.tilt, scene->gm.dome.warptext);
490
491                         // initialize 3D Audio Settings
492                         AUD_I3DDevice* dev = AUD_get3DDevice();
493                         if (dev)
494                         {
495                                 dev->setSpeedOfSound(scene->audio.speed_of_sound);
496                                 dev->setDopplerFactor(scene->audio.doppler_factor);
497                                 dev->setDistanceModel(AUD_DistanceModel(scene->audio.distance_model));
498                         }
499
500                         // from see blender.c:
501                         // FIXME: this version patching should really be part of the file-reading code,
502                         // but we still get too many unrelated data-corruption crashes otherwise...
503                         if (blenderdata->versionfile < 250)
504                                 do_versions_ipos_to_animato(blenderdata);
505
506                         if (sceneconverter)
507                         {
508                                 // convert and add scene
509                                 sceneconverter->ConvertScene(
510                                         startscene,
511                                         rendertools,
512                                         canvas);
513                                 ketsjiengine->AddScene(startscene);
514                                 
515                                 // init the rasterizer
516                                 rasterizer->Init();
517                                 
518                                 // start the engine
519                                 ketsjiengine->StartEngine(true);
520                                 
521
522                                 // Set the animation playback rate for ipo's and actions
523                                 // the framerate below should patch with FPS macro defined in blendef.h
524                                 // Could be in StartEngine set the framerate, we need the scene to do this
525                                 ketsjiengine->SetAnimFrameRate(FPS);
526                                 
527 #ifdef WITH_PYTHON
528                                 char *python_main = NULL;
529                                 pynextframestate.state = NULL;
530                                 pynextframestate.func = NULL;
531                                 python_main = KX_GetPythonMain(scene);
532
533                                 // the mainloop
534                                 printf("\nBlender Game Engine Started\n");
535                                 if (python_main) {
536                                         char *python_code = KX_GetPythonCode(blenderdata, python_main);
537                                         if (python_code) {
538                                                 ketsjinextframestate.ketsjiengine = ketsjiengine;
539                                                 ketsjinextframestate.C = C;
540                                                 ketsjinextframestate.win = win;
541                                                 ketsjinextframestate.scene = scene;
542                                                 ketsjinextframestate.ar = ar;
543                                                 ketsjinextframestate.keyboarddevice = keyboarddevice;
544                                                 ketsjinextframestate.mousedevice = mousedevice;
545                                                 ketsjinextframestate.draw_letterbox = draw_letterbox;
546                         
547                                                 pynextframestate.state = &ketsjinextframestate;
548                                                 pynextframestate.func = &BL_KetsjiPyNextFrame;
549                                                 printf("Yielding control to Python script '%s'...\n", python_main);
550                                                 PyRun_SimpleString(python_code);
551                                                 printf("Exit Python script '%s'\n", python_main);
552                                                 MEM_freeN(python_code);
553                                         }
554                                 }
555                                 else
556 #endif  /* WITH_PYTHON */
557                                 {
558                                         while (!exitrequested)
559                                         {
560                                                 exitrequested = BL_KetsjiNextFrame(ketsjiengine, C, win, scene, ar, keyboarddevice, mousedevice, draw_letterbox);
561                                         }
562                                 }
563                                 printf("Blender Game Engine Finished\n");
564                                 exitstring = ketsjiengine->GetExitString();
565 #ifdef WITH_PYTHON
566                                 if (python_main) MEM_freeN(python_main);
567 #endif  /* WITH_PYTHON */
568
569                                 gs = *(ketsjiengine->GetGlobalSettings());
570
571                                 // when exiting the mainloop
572 #ifdef WITH_PYTHON
573                                 // Clears the dictionary by hand:
574                                 // This prevents, extra references to global variables
575                                 // inside the GameLogic dictionary when the python interpreter is finalized.
576                                 // which allows the scene to safely delete them :)
577                                 // see: (space.c)->start_game
578                                 
579                                 //PyDict_Clear(PyModule_GetDict(gameLogic));
580                                 
581                                 // Keep original items, means python plugins will autocomplete members
582                                 PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic));
583                                 const Py_ssize_t numitems= PyList_GET_SIZE(gameLogic_keys_new);
584                                 Py_ssize_t listIndex;
585                                 for (listIndex=0; listIndex < numitems; listIndex++) {
586                                         PyObject *item = PyList_GET_ITEM(gameLogic_keys_new, listIndex);
587                                         if (!PySequence_Contains(gameLogic_keys, item)) {
588                                                 PyDict_DelItem( PyModule_GetDict(gameLogic), item);
589                                         }
590                                 }
591                                 Py_DECREF(gameLogic_keys_new);
592                                 gameLogic_keys_new = NULL;
593 #endif
594                                 ketsjiengine->StopEngine();
595 #ifdef WITH_PYTHON
596                                 exitGamePythonScripting();
597 #endif
598                                 networkdevice->Disconnect();
599                         }
600                         if (sceneconverter)
601                         {
602                                 delete sceneconverter;
603                                 sceneconverter = NULL;
604                         }
605
606 #ifdef WITH_PYTHON
607                         Py_DECREF(gameLogic_keys);
608                         gameLogic_keys = NULL;
609 #endif
610                 }
611                 //lock frame and camera enabled - restoring global values
612                 if (v3d->scenelock==0) {
613                         startscene->lay= tmp_lay;
614                         startscene->camera= tmp_camera;
615                 }
616
617                 if (exitrequested != KX_EXIT_REQUEST_OUTSIDE)
618                 {
619                         // set the cursor back to normal
620                         canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
621                 }
622                 
623                 // clean up some stuff
624                 if (ketsjiengine)
625                 {
626                         delete ketsjiengine;
627                         ketsjiengine = NULL;
628                 }
629                 if (kxsystem)
630                 {
631                         delete kxsystem;
632                         kxsystem = NULL;
633                 }
634                 if (networkdevice)
635                 {
636                         delete networkdevice;
637                         networkdevice = NULL;
638                 }
639                 if (keyboarddevice)
640                 {
641                         delete keyboarddevice;
642                         keyboarddevice = NULL;
643                 }
644                 if (mousedevice)
645                 {
646                         delete mousedevice;
647                         mousedevice = NULL;
648                 }
649                 if (rasterizer)
650                 {
651                         delete rasterizer;
652                         rasterizer = NULL;
653                 }
654                 if (rendertools)
655                 {
656                         delete rendertools;
657                         rendertools = NULL;
658                 }
659                 if (canvas)
660                 {
661                         delete canvas;
662                         canvas = NULL;
663                 }
664
665                 // stop all remaining playing sounds
666                 AUD_getDevice()->stopAll();
667         
668         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
669         
670         if (!disableVBO)
671                 U.gameflags &= ~USER_DISABLE_VBO;
672
673         if (bfd) BLO_blendfiledata_free(bfd);
674
675         BLI_strncpy(G.main->name, oldsce, sizeof(G.main->name));
676
677 #ifdef WITH_PYTHON
678         Py_DECREF(pyGlobalDict);
679
680         // Release Python's GIL
681         PyGILState_Release(gilstate);
682 #endif
683
684 }