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