doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
index 4efdf04890eced99aeae0bfc9d457e161f3bb222..2424ca712f41ae7985cd0806ee2c66429a17c01d 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
  * Blender's Ketsji startpoint
  */
 
  * Blender's Ketsji startpoint
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #include <signal.h>
 #include <stdlib.h>
 #include <signal.h>
 #include <stdlib.h>
+#include <stdio.h>
 
 
-#ifdef WIN32
+#if defined(WIN32) && !defined(FREE_WINDOWS)
 // don't show stl-warnings
 #pragma warning (disable:4786)
 #endif
 // don't show stl-warnings
 #pragma warning (disable:4786)
 #endif
@@ -75,11 +72,12 @@ extern "C" {
        /***/
 #include "DNA_view3d_types.h"
 #include "DNA_screen_types.h"
        /***/
 #include "DNA_view3d_types.h"
 #include "DNA_screen_types.h"
+#include "DNA_userdef_types.h"
 #include "DNA_windowmanager_types.h"
 #include "BKE_global.h"
 #include "BKE_report.h"
 
 #include "DNA_windowmanager_types.h"
 #include "BKE_global.h"
 #include "BKE_report.h"
 
-#include "BKE_utildefines.h"
+
 //XXX #include "BIF_screen.h"
 //XXX #include "BIF_scrarea.h"
 
 //XXX #include "BIF_screen.h"
 //XXX #include "BIF_scrarea.h"
 
@@ -87,8 +85,11 @@ extern "C" {
 #include "BLI_blenlib.h"
 #include "BLO_readfile.h"
 #include "DNA_scene_types.h"
 #include "BLI_blenlib.h"
 #include "BLO_readfile.h"
 #include "DNA_scene_types.h"
+#include "BKE_ipo.h"
        /***/
 
        /***/
 
+#include "AUD_C-API.h"
+
 //XXX #include "BSE_headerbuttons.h"
 #include "BKE_context.h"
 #include "../../blender/windowmanager/WM_types.h"
 //XXX #include "BSE_headerbuttons.h"
 #include "BKE_context.h"
 #include "../../blender/windowmanager/WM_types.h"
@@ -117,35 +118,47 @@ static BlendFileData *load_game_data(char *filename)
        return bfd;
 }
 
        return bfd;
 }
 
-extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int always_use_expand_framing)
+extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *cam_frame, int always_use_expand_framing)
 {
        /* context values */
        struct wmWindow *win= CTX_wm_window(C);
 {
        /* context values */
        struct wmWindow *win= CTX_wm_window(C);
-       struct Scene *scene= CTX_data_scene(C);
+       struct Scene *startscene= CTX_data_scene(C);
        struct Main* maggie1= CTX_data_main(C);
        struct Main* maggie1= CTX_data_main(C);
-       
-       
+
+
+       RAS_Rect area_rect;
+       area_rect.SetLeft(cam_frame->xmin);
+       area_rect.SetBottom(cam_frame->ymin);
+       area_rect.SetRight(cam_frame->xmax);
+       area_rect.SetTop(cam_frame->ymax);
+
        int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
        Main* blenderdata = maggie1;
 
        int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
        Main* blenderdata = maggie1;
 
-       char* startscenename = scene->id.name+2;
+       char* startscenename = startscene->id.name+2;
        char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE];
        STR_String exitstring = "";
        BlendFileData *bfd= NULL;
 
        BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
        char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE];
        STR_String exitstring = "";
        BlendFileData *bfd= NULL;
 
        BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
-       BLI_strncpy(oldsce, G.sce, sizeof(oldsce));
+       BLI_strncpy(oldsce, G.main->name, sizeof(oldsce));
+#ifdef WITH_PYTHON
        resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
        resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
-       setGamePythonPath(G.sce);
+       setGamePythonPath(G.main->name);
 
        // Acquire Python's GIL (global interpreter lock)
        // so we can safely run Python code and API calls
        PyGILState_STATE gilstate = PyGILState_Ensure();
        
        PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
 
        // Acquire Python's GIL (global interpreter lock)
        // so we can safely run Python code and API calls
        PyGILState_STATE gilstate = PyGILState_Ensure();
        
        PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
+#endif
        
        bgl::InitExtensions(true);
 
        
        bgl::InitExtensions(true);
 
+       // VBO code for derived mesh is not compatible with BGE (couldn't find why), so disable
+       int disableVBO = (U.gameflags & USER_DISABLE_VBO);
+       U.gameflags |= USER_DISABLE_VBO;
+
        do
        {
                View3D *v3d= CTX_wm_view3d(C);
        do
        {
                View3D *v3d= CTX_wm_view3d(C);
@@ -157,13 +170,24 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
                bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
                bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
                bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
                bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
                bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
-               bool game2ipo = (SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
+               bool animation_record = (SYS_GetCommandLineInt(syshandle, "animation_record", 0) != 0);
                bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
                bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
+#ifdef WITH_PYTHON
                bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
                bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
+#endif
                bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
                bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
+               bool mouse_state = startscene->gm.flag & GAME_SHOW_MOUSE;
+
+               if(animation_record) usefixed= true; /* override since you's always want fixed time for sim recording */
+
                // create the canvas, rasterizer and rendertools
                // create the canvas, rasterizer and rendertools
-               RAS_ICanvas* canvas = new KX_BlenderCanvas(win, ar);
-               canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
+               RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect, ar);
+               
+               // default mouse state set on render panel
+               if (mouse_state)
+                       canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
+               else
+                       canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
                RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
                RAS_IRasterizer* rasterizer = NULL;
                
                RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
                RAS_IRasterizer* rasterizer = NULL;
                
@@ -204,44 +228,37 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                ketsjiengine->SetUseFixedTime(usefixed);
                ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
 
                ketsjiengine->SetUseFixedTime(usefixed);
                ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
 
+#ifdef WITH_PYTHON
                CValue::SetDeprecationWarnings(nodepwarnings);
                CValue::SetDeprecationWarnings(nodepwarnings);
-
+#endif
 
                //lock frame and camera enabled - storing global values
 
                //lock frame and camera enabled - storing global values
-               int tmp_lay= scene->lay;
-               Object *tmp_camera = scene->camera;
+               int tmp_lay= startscene->lay;
+               Object *tmp_camera = startscene->camera;
 
                if (v3d->scenelock==0){
 
                if (v3d->scenelock==0){
-                       scene->lay= v3d->lay;
-                       scene->camera= v3d->camera;
+                       startscene->lay= v3d->lay;
+                       startscene->camera= v3d->camera;
                }
 
                // some blender stuff
                }
 
                // some blender stuff
-               MT_CmMatrix4x4 projmat;
-               MT_CmMatrix4x4 viewmat;
                float camzoom;
                float camzoom;
-               int i;
-
-               for (i = 0; i < 16; i++)
-               {
-                       float *viewmat_linear= (float*) rv3d->viewmat;
-                       viewmat.setElem(i, viewmat_linear[i]);
-               }
-               for (i = 0; i < 16; i++)
-               {
-                       float *projmat_linear= (float*) rv3d->winmat;
-                       projmat.setElem(i, projmat_linear[i]);
-               }
                
                
-               if(rv3d->persp==V3D_CAMOB) {
-                       camzoom = (1.41421 + (rv3d->camzoom / 50.0));
-                       camzoom *= camzoom;
+               if(rv3d->persp==RV3D_CAMOB) {
+                       if(startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
+                               camzoom = 1.0f;
+                       }
+                       else {
+                               camzoom = (1.41421 + (rv3d->camzoom / 50.0));
+                               camzoom *= camzoom;
+                               camzoom = 4.0 / camzoom;
+                       }
                }
                }
-               else
+               else {
                        camzoom = 2.0;
                        camzoom = 2.0;
+               }
+
 
 
-               camzoom = 4.0 / camzoom;
-               
                ketsjiengine->SetDrawType(v3d->drawtype);
                ketsjiengine->SetCameraZoom(camzoom);
                
                ketsjiengine->SetDrawType(v3d->drawtype);
                ketsjiengine->SetCameraZoom(camzoom);
                
@@ -262,7 +279,7 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                        // to the first file but that makes no sense, relative paths in
                        // blend files should be relative to that file, not some other file
                        // that happened to be loaded first
                        // to the first file but that makes no sense, relative paths in
                        // blend files should be relative to that file, not some other file
                        // that happened to be loaded first
-                       BLI_convertstringcode(basedpath, pathname);
+                       BLI_path_abs(basedpath, pathname);
                        bfd = load_game_data(basedpath);
                        
                        // if it wasn't loaded, try it forced relative
                        bfd = load_game_data(basedpath);
                        
                        // if it wasn't loaded, try it forced relative
@@ -273,7 +290,7 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                                strcpy(temppath, "//");
                                strcat(temppath, basedpath);
                                
                                strcpy(temppath, "//");
                                strcat(temppath, basedpath);
                                
-                               BLI_convertstringcode(temppath, pathname);
+                               BLI_path_abs(temppath, pathname);
                                bfd = load_game_data(temppath);
                        }
                        
                                bfd = load_game_data(temppath);
                        }
                        
@@ -284,9 +301,11 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                                startscenename = bfd->curscene->id.name + 2;
 
                                if(blenderdata) {
                                startscenename = bfd->curscene->id.name + 2;
 
                                if(blenderdata) {
-                                       BLI_strncpy(G.sce, blenderdata->name, sizeof(G.sce));
+                                       BLI_strncpy(G.main->name, blenderdata->name, sizeof(G.main->name));
                                        BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
                                        BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
-                                       setGamePythonPath(G.sce);
+#ifdef WITH_PYTHON
+                                       setGamePythonPath(G.main->name);
+#endif
                                }
                        }
                        // else forget it, we can't find it
                                }
                        }
                        // else forget it, we can't find it
@@ -295,43 +314,33 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                                exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
                        }
                }
                                exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
                        }
                }
-               
-               Scene *blscene = NULL;
-               if (!bfd)
-               {
-                       blscene = (Scene*) blenderdata->scene.first;
-                       for (Scene *sce= (Scene*) blenderdata->scene.first; sce; sce= (Scene*) sce->id.next)
-                       {
-                               if (startscenename == (sce->id.name+2))
-                               {
-                                       blscene = sce;
-                                       break;
-                               }
-                       }
-               } else {
-                       blscene = bfd->curscene;
-               }
 
 
-               if (blscene)
+               Scene *scene= bfd ? bfd->curscene : (Scene *)BLI_findstring(&blenderdata->scene, startscenename, offsetof(ID, name) + 2);
+
+               if (scene)
                {
                {
-                       int startFrame = blscene->r.cfra;
-                       ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
+                       int startFrame = scene->r.cfra;
+                       ketsjiengine->SetAnimRecordMode(animation_record, startFrame);
                        
                        // Quad buffered needs a special window.
                        
                        // Quad buffered needs a special window.
-                       if(blscene->gm.stereoflag == STEREO_ENABLED){
-                               if (blscene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
-                                       rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->gm.stereomode);
+                       if(scene->gm.stereoflag == STEREO_ENABLED){
+                               if (scene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
+                                       rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) scene->gm.stereomode);
+
+                               rasterizer->SetEyeSeparation(scene->gm.eyeseparation);
                        }
                        }
+
+                       rasterizer->SetBackColor(scene->gm.framing.col[0], scene->gm.framing.col[1], scene->gm.framing.col[2], 0.0f);
                }
                
                if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
                {
                }
                
                if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
                {
-                       if (rv3d->persp != V3D_CAMOB)
+                       if (rv3d->persp != RV3D_CAMOB)
                        {
                                ketsjiengine->EnableCameraOverride(startscenename);
                        {
                                ketsjiengine->EnableCameraOverride(startscenename);
-                               ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == V3D_ORTHO));
-                               ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
-                               ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
+                               ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == RV3D_ORTHO));
+                               ketsjiengine->SetCameraOverrideProjectionMatrix(MT_CmMatrix4x4(rv3d->winmat));
+                               ketsjiengine->SetCameraOverrideViewMatrix(MT_CmMatrix4x4(rv3d->viewmat));
                                ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
                                ketsjiengine->SetCameraOverrideLens(v3d->lens);
                        }
                                ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
                                ketsjiengine->SetCameraOverrideLens(v3d->lens);
                        }
@@ -348,50 +357,49 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                        if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
                                usemat = true;
 
                        if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
                                usemat = true;
 
-                       if(GPU_extensions_minimum_support())
+                       if(GPU_glsl_support())
                                useglslmat = true;
                                useglslmat = true;
-                       else if(G.fileflags & G_FILE_GAME_MAT_GLSL)
+                       else if(scene->gm.matmode == GAME_MAT_GLSL)
                                usemat = false;
 
                                usemat = false;
 
-            if(usemat && (G.fileflags & G_FILE_GAME_MAT))
+                       if(usemat && (scene->gm.matmode != GAME_MAT_TEXFACE))
                                sceneconverter->SetMaterials(true);
                                sceneconverter->SetMaterials(true);
-                       if(useglslmat && (G.fileflags & G_FILE_GAME_MAT_GLSL))
+                       if(useglslmat && (scene->gm.matmode == GAME_MAT_GLSL))
                                sceneconverter->SetGLSLMaterials(true);
                                        
                        KX_Scene* startscene = new KX_Scene(keyboarddevice,
                                mousedevice,
                                networkdevice,
                                startscenename,
                                sceneconverter->SetGLSLMaterials(true);
                                        
                        KX_Scene* startscene = new KX_Scene(keyboarddevice,
                                mousedevice,
                                networkdevice,
                                startscenename,
-                               blscene);
-                       
+                               scene,
+                               canvas);
+
+#ifdef WITH_PYTHON
                        // some python things
                        // some python things
-                       PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata);
-                       ketsjiengine->SetPythonDictionary(dictionaryobject);
-                       initRasterizer(rasterizer, canvas);
-                       PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);
-                       PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
-                       PyObject *gameLogic_keys = PyDict_Keys(PyModule_GetDict(gameLogic));
-                       PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module.
-                       
-                       initGameKeys();
-                       initPythonConstraintBinding();
-                       initMathutils();
-                       initGeometry();
-                       initBGL();
-#ifdef WITH_FFMPEG
-                       initVideoTexture();
-#endif
+                       PyObject *gameLogic, *gameLogic_keys;
+                       setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL);
+#endif // WITH_PYTHON
 
                        //initialize Dome Settings
 
                        //initialize Dome Settings
-                       if(blscene->gm.stereoflag == STEREO_DOME)
-                               ketsjiengine->InitDome(blscene->gm.dome.res, blscene->gm.dome.mode, blscene->gm.dome.angle, blscene->gm.dome.resbuf, blscene->gm.dome.tilt, blscene->gm.dome.warptext);
+                       if(scene->gm.stereoflag == STEREO_DOME)
+                               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);
+
+                       // initialize 3D Audio Settings
+                       AUD_setSpeedOfSound(scene->audio.speed_of_sound);
+                       AUD_setDopplerFactor(scene->audio.doppler_factor);
+                       AUD_setDistanceModel(AUD_DistanceModel(scene->audio.distance_model));
+
+                       // from see blender.c:
+                       // FIXME: this version patching should really be part of the file-reading code,
+                       // but we still get too many unrelated data-corruption crashes otherwise...
+                       if (blenderdata->versionfile < 250)
+                               do_versions_ipos_to_animato(blenderdata);
 
                        if (sceneconverter)
                        {
                                // convert and add scene
                                sceneconverter->ConvertScene(
                                        startscene,
 
                        if (sceneconverter)
                        {
                                // convert and add scene
                                sceneconverter->ConvertScene(
                                        startscene,
-                                       dictionaryobject,
                                        rendertools,
                                        canvas);
                                ketsjiengine->AddScene(startscene);
                                        rendertools,
                                        canvas);
                                ketsjiengine->AddScene(startscene);
@@ -406,17 +414,17 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                                // Set the animation playback rate for ipo's and actions
                                // the framerate below should patch with FPS macro defined in blendef.h
                                // Could be in StartEngine set the framerate, we need the scene to do this
                                // Set the animation playback rate for ipo's and actions
                                // the framerate below should patch with FPS macro defined in blendef.h
                                // Could be in StartEngine set the framerate, we need the scene to do this
-                               ketsjiengine->SetAnimFrameRate( (((double) blscene->r.frs_sec) / blscene->r.frs_sec_base) );
+                               ketsjiengine->SetAnimFrameRate(FPS);
                                
                                // the mainloop
                                
                                // the mainloop
-                               printf("\nBlender Game Engine Started\n\n");
+                               printf("\nBlender Game Engine Started\n");
                                while (!exitrequested)
                                {
                                        // first check if we want to exit
                                        exitrequested = ketsjiengine->GetExitCode();
                                        
                                        // kick the engine
                                while (!exitrequested)
                                {
                                        // first check if we want to exit
                                        exitrequested = ketsjiengine->GetExitCode();
                                        
                                        // kick the engine
-                                       bool render = ketsjiengine->NextFrame(); // XXX 2.5 Bug, This is never true! FIXME-  Campbell
+                                       bool render = ketsjiengine->NextFrame();
                                        
                                        if (render)
                                        {
                                        
                                        if (render)
                                        {
@@ -424,7 +432,7 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                                                ketsjiengine->Render();
                                        }
                                        
                                                ketsjiengine->Render();
                                        }
                                        
-                                       wm_window_process_events_nosleep(C);
+                                       wm_window_process_events_nosleep();
                                        
                                        // test for the ESC key
                                        //XXX while (qtest())
                                        
                                        // test for the ESC key
                                        //XXX while (qtest())
@@ -440,7 +448,7 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                                                        * should this really be?
                                                */
                                                if (event->type==MOUSEMOVE) {
                                                        * should this really be?
                                                */
                                                if (event->type==MOUSEMOVE) {
-                                                       /* Note nice! XXX 2.5 event hack */
+                                                       /* Note, not nice! XXX 2.5 event hack */
                                                        val = event->x - ar->winrct.xmin;
                                                        mousedevice->ConvertBlenderEvent(MOUSEX, val);
                                                        
                                                        val = event->x - ar->winrct.xmin;
                                                        mousedevice->ConvertBlenderEvent(MOUSEX, val);
                                                        
@@ -455,13 +463,16 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                                                wm_event_free(event);
                                        }
                                        
                                                wm_event_free(event);
                                        }
                                        
+                                       if(win != CTX_wm_window(C)) {
+                                               exitrequested= KX_EXIT_REQUEST_OUTSIDE; /* window closed while bge runs */
+                                       }
                                }
                                }
-                               printf("\nBlender Game Engine Finished\n\n");
+                               printf("Blender Game Engine Finished\n");
                                exitstring = ketsjiengine->GetExitString();
 
 
                                // when exiting the mainloop
                                exitstring = ketsjiengine->GetExitString();
 
 
                                // when exiting the mainloop
-                               
+#ifdef WITH_PYTHON
                                // Clears the dictionary by hand:
                                // This prevents, extra references to global variables
                                // inside the GameLogic dictionary when the python interpreter is finalized.
                                // Clears the dictionary by hand:
                                // This prevents, extra references to global variables
                                // inside the GameLogic dictionary when the python interpreter is finalized.
@@ -481,9 +492,11 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                                }
                                Py_DECREF(gameLogic_keys_new);
                                gameLogic_keys_new = NULL;
                                }
                                Py_DECREF(gameLogic_keys_new);
                                gameLogic_keys_new = NULL;
-                               
+#endif
                                ketsjiengine->StopEngine();
                                ketsjiengine->StopEngine();
+#ifdef WITH_PYTHON
                                exitGamePythonScripting();
                                exitGamePythonScripting();
+#endif
                                networkdevice->Disconnect();
                        }
                        if (sceneconverter)
                                networkdevice->Disconnect();
                        }
                        if (sceneconverter)
@@ -491,18 +504,23 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
                                delete sceneconverter;
                                sceneconverter = NULL;
                        }
                                delete sceneconverter;
                                sceneconverter = NULL;
                        }
-                       
+
+#ifdef WITH_PYTHON
                        Py_DECREF(gameLogic_keys);
                        gameLogic_keys = NULL;
                        Py_DECREF(gameLogic_keys);
                        gameLogic_keys = NULL;
+#endif
                }
                //lock frame and camera enabled - restoring global values
                if (v3d->scenelock==0){
                }
                //lock frame and camera enabled - restoring global values
                if (v3d->scenelock==0){
-                       scene->lay= tmp_lay;
-                       scene->camera= tmp_camera;
+                       startscene->lay= tmp_lay;
+                       startscene->camera= tmp_camera;
                }
 
                }
 
-               // set the cursor back to normal
-               canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
+               if(exitrequested != KX_EXIT_REQUEST_OUTSIDE)
+               {
+                       // set the cursor back to normal
+                       canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
+               }
                
                // clean up some stuff
                if (ketsjiengine)
                
                // clean up some stuff
                if (ketsjiengine)
@@ -548,229 +566,18 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int alw
        
        } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
        
        
        } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
        
-       Py_DECREF(pyGlobalDict);
-       
+       if (!disableVBO)
+               U.gameflags &= ~USER_DISABLE_VBO;
+
        if (bfd) BLO_blendfiledata_free(bfd);
 
        if (bfd) BLO_blendfiledata_free(bfd);
 
-       BLI_strncpy(G.sce, oldsce, sizeof(G.sce));
+       BLI_strncpy(G.main->name, oldsce, sizeof(G.main->name));
+
+#ifdef WITH_PYTHON
+       Py_DECREF(pyGlobalDict);
 
        // Release Python's GIL
        PyGILState_Release(gilstate);
 
        // Release Python's GIL
        PyGILState_Release(gilstate);
-}
-
-extern "C" void StartKetsjiShellSimulation(struct wmWindow *win,
-                                                                struct ARegion *ar,
-                                                                char* scenename,
-                                                                struct Main* maggie,
-                                                                int always_use_expand_framing)
-{
-    int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
-
-       Main* blenderdata = maggie;
-
-       char* startscenename = scenename;
-       char pathname[FILE_MAXDIR+FILE_MAXFILE];
-       STR_String exitstring = "";
-
-       BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
-
-       // Acquire Python's GIL (global interpreter lock)
-       // so we can safely run Python code and API calls
-       PyGILState_STATE gilstate = PyGILState_Ensure();
-
-       bgl::InitExtensions(true);
-
-       do
-       {
-
-               // get some preferences
-               SYS_SystemHandle syshandle = SYS_GetSystem();
-               /*
-               bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
-               bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
-               bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
-               bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
-               */
-               bool game2ipo = true;//(SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
-               bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
-               bool usemat = false;
-
-               // create the canvas, rasterizer and rendertools
-               RAS_ICanvas* canvas = new KX_BlenderCanvas(win, ar);
-               //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
-               RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
-               RAS_IRasterizer* rasterizer = NULL;
-
-               if(displaylists) {
-                       if (GLEW_VERSION_1_1)
-                               rasterizer = new RAS_ListRasterizer(canvas, true, true);
-                       else
-                               rasterizer = new RAS_ListRasterizer(canvas);
-               }
-               else if (GLEW_VERSION_1_1)
-                       rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
-               else
-                       rasterizer = new RAS_OpenGLRasterizer(canvas);
-
-               // create the inputdevices
-               KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
-               KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
-
-               // create a networkdevice
-               NG_NetworkDeviceInterface* networkdevice = new
-                       NG_LoopBackNetworkDeviceInterface();
-
-               // create a ketsji/blendersystem (only needed for timing and stuff)
-               KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
-
-               // create the ketsjiengine
-               KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
-
-               Scene *blscene = NULL;
-
-               blscene = (Scene*) maggie->scene.first;
-               for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
-               {
-                       if (startscenename == (sce->id.name+2))
-                       {
-                               blscene = sce;
-                               break;
-                       }
-               }
-
-        int cframe = 1, startFrame;
-               if (blscene)
-               {
-                       cframe=blscene->r.cfra;
-                       startFrame = blscene->r.sfra;
-                       blscene->r.cfra=startFrame;
-                       // update_for_newframe(); // XXX scene_update_for_newframe wont cut it!
-                       ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
-               }
-
-               // Quad buffered needs a special window.
-               if(blscene->gm.stereoflag == STEREO_ENABLED){
-                       if (blscene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
-                               rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->gm.stereomode);
-               }
-
-               if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
-               {
-                       // create a scene converter, create and convert the startingscene
-                       KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie, ketsjiengine);
-                       ketsjiengine->SetSceneConverter(sceneconverter);
-                       sceneconverter->addInitFromFrame=true;
-                       
-                       if (always_use_expand_framing)
-                               sceneconverter->SetAlwaysUseExpandFraming(true);
-
-                       if(usemat)
-                               sceneconverter->SetMaterials(true);
-
-                       KX_Scene* startscene = new KX_Scene(keyboarddevice,
-                               mousedevice,
-                               networkdevice,
-                               startscenename,
-                               blscene);
-
-                       // some python things
-                       PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata);
-                       ketsjiengine->SetPythonDictionary(dictionaryobject);
-                       initRasterizer(rasterizer, canvas);
-                       PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);
-                       PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module
-                       initGameKeys();
-                       initPythonConstraintBinding();
-                       initMathutils();
-                       initGeometry();
-                       initBGL();
-#ifdef WITH_FFMPEG
-                       initVideoTexture();
 #endif
 
 #endif
 
-                       if (sceneconverter)
-                       {
-                               // convert and add scene
-                               sceneconverter->ConvertScene(
-                                       startscene,
-                                       dictionaryobject,
-                                       rendertools,
-                                       canvas);
-                               ketsjiengine->AddScene(startscene);
-
-                               // start the engine
-                               ketsjiengine->StartEngine(false);
-                               
-                               ketsjiengine->SetUseFixedTime(true);
-                               
-                               ketsjiengine->SetTicRate(
-                                       (double) blscene->r.frs_sec /
-                                       (double) blscene->r.frs_sec_base);
-
-                               // the mainloop
-                               while ((blscene->r.cfra<=blscene->r.efra)&&(!exitrequested))
-                               {
-                    printf("frame %i\n",blscene->r.cfra);
-                    // first check if we want to exit
-                                       exitrequested = ketsjiengine->GetExitCode();
-       
-                                       // kick the engine
-                                       ketsjiengine->NextFrame();
-                                   blscene->r.cfra=blscene->r.cfra+1;
-                                   // update_for_newframe(); // XXX scene_update_for_newframe wont cut it
-                                       
-                               }
-                               exitstring = ketsjiengine->GetExitString();
-                       }
-                       if (sceneconverter)
-                       {
-                               delete sceneconverter;
-                               sceneconverter = NULL;
-                       }
-               }
-               blscene->r.cfra=cframe;
-               // set the cursor back to normal
-               canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
-
-               // clean up some stuff
-               if (ketsjiengine)
-               {
-                       delete ketsjiengine;
-                       ketsjiengine = NULL;
-               }
-               if (kxsystem)
-               {
-                       delete kxsystem;
-                       kxsystem = NULL;
-               }
-               if (networkdevice)
-               {
-                       delete networkdevice;
-                       networkdevice = NULL;
-               }
-               if (keyboarddevice)
-               {
-                       delete keyboarddevice;
-                       keyboarddevice = NULL;
-               }
-               if (mousedevice)
-               {
-                       delete mousedevice;
-                       mousedevice = NULL;
-               }
-               if (rasterizer)
-               {
-                       delete rasterizer;
-                       rasterizer = NULL;
-               }
-               if (rendertools)
-               {
-                       delete rendertools;
-                       rendertools = NULL;
-                }
-
-       } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
-
-       // Release Python's GIL
-       PyGILState_Release(gilstate);
 }
 }