doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
index b200fc8..2424ca7 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * $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,
- * 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.
  * Blender's Ketsji startpoint
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #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
 #include "RAS_ListRasterizer.h"
 
 #include "NG_LoopBackNetworkDeviceInterface.h"
-#include "SND_DeviceManager.h"
 
 #include "SYS_System.h"
 
-       /***/
+#include "GPU_extensions.h"
+#include "Value.h"
+
 
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+       /***/
 #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_utildefines.h"
-#include "BIF_screen.h"
-#include "BIF_scrarea.h"
+#include "BKE_report.h"
+
 
-#include "BKE_main.h"  
+//XXX #include "BIF_screen.h"
+//XXX #include "BIF_scrarea.h"
+
+#include "BKE_main.h"
 #include "BLI_blenlib.h"
 #include "BLO_readfile.h"
 #include "DNA_scene_types.h"
+#include "BKE_ipo.h"
        /***/
 
-#include "GPU_extensions.h"
-#include "Value.h"
+#include "AUD_C-API.h"
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-#include "BSE_headerbuttons.h"
-void update_for_newframe();
+//XXX #include "BSE_headerbuttons.h"
+#include "BKE_context.h"
+#include "../../blender/windowmanager/WM_types.h"
+#include "../../blender/windowmanager/wm_window.h"
+#include "../../blender/windowmanager/wm_event_system.h"
 #ifdef __cplusplus
 }
 #endif
 
-static BlendFileData *load_game_data(char *filename) {
-       BlendReadError error;
-       BlendFileData *bfd= BLO_read_from_file(filename, &error);
+
+static BlendFileData *load_game_data(char *filename)
+{
+       ReportList reports;
+       BlendFileData *bfd;
+       
+       BKE_reports_init(&reports, RPT_STORE);
+       bfd= BLO_read_from_file(filename, &reports);
+
        if (!bfd) {
-               printf("Loading %s failed: %s\n", filename, BLO_bre_as_string(error));
+               printf("Loading %s failed: ", filename);
+               BKE_reports_print(&reports, RPT_ERROR);
        }
+
+       BKE_reports_clear(&reports);
+
        return bfd;
 }
 
-extern "C" void StartKetsjiShell(struct ScrArea *area,
-                                                                char* scenename,
-                                                                struct Main* maggie1,
-                                                                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);
+       struct Scene *startscene= CTX_data_scene(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;
 
-       char* startscenename = scenename;
+       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));
-       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
-       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 */
+#endif
        
        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= (View3D*) area->spacedata.first;
+               View3D *v3d= CTX_wm_view3d(C);
+               RegionView3D *rv3d= CTX_wm_region_view3d(C);
 
                // get some preferences
                SYS_SystemHandle syshandle = SYS_GetSystem();
@@ -138,13 +170,24 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                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);
+#ifdef WITH_PYTHON
                bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
+#endif
                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
-               RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
-               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;
                
@@ -166,19 +209,8 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                // create a networkdevice
                NG_NetworkDeviceInterface* networkdevice = new
                        NG_LoopBackNetworkDeviceInterface();
-               
-               //
-               SYS_SystemHandle hSystem = SYS_GetSystem();
-               bool noaudio = SYS_GetCommandLineInt(hSystem,"noaudio",0);
 
-               if (noaudio)/*(noaudio) intrr: disable game engine audio (openal) */
-                       SND_DeviceManager::SetDeviceType(snd_e_dummydevice);
-
-               // get an audiodevice
-               SND_DeviceManager::Subscribe();
-               SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
-               audiodevice->UseCD();
-               
+               //
                // create a ketsji/blendersystem (only needed for timing and stuff)
                KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
                
@@ -193,49 +225,40 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                ketsjiengine->SetRenderTools(rendertools);
                ketsjiengine->SetRasterizer(rasterizer);
                ketsjiengine->SetNetworkDevice(networkdevice);
-               ketsjiengine->SetAudioDevice(audiodevice);
                ketsjiengine->SetUseFixedTime(usefixed);
                ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
 
+#ifdef WITH_PYTHON
                CValue::SetDeprecationWarnings(nodepwarnings);
-
+#endif
 
                //lock frame and camera enabled - storing global values
-               int tmp_lay= G.scene->lay;
-               Object *tmp_camera = G.scene->camera;
+               int tmp_lay= startscene->lay;
+               Object *tmp_camera = startscene->camera;
 
-               if (G.vd->scenelock==0){
-                       G.scene->lay= v3d->lay;
-                       G.scene->camera= v3d->camera;
+               if (v3d->scenelock==0){
+                       startscene->lay= v3d->lay;
+                       startscene->camera= v3d->camera;
                }
 
-       
                // some blender stuff
-               MT_CmMatrix4x4 projmat;
-               MT_CmMatrix4x4 viewmat;
                float camzoom;
-               int i;
-               
-               for (i = 0; i < 16; i++)
-               {
-                       float *viewmat_linear= (float*) v3d->viewmat;
-                       viewmat.setElem(i, viewmat_linear[i]);
-               }
-               for (i = 0; i < 16; i++)
-               {
-                       float *projmat_linear = (float*) area->winmat;
-                       projmat.setElem(i, projmat_linear[i]);
-               }
                
-               if(v3d->persp==V3D_CAMOB) {
-                       camzoom = (1.41421 + (v3d->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 = 4.0 / camzoom;
-               
                ketsjiengine->SetDrawType(v3d->drawtype);
                ketsjiengine->SetCameraZoom(camzoom);
                
@@ -256,7 +279,7 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                        // 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
@@ -267,7 +290,7 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                strcpy(temppath, "//");
                                strcat(temppath, basedpath);
                                
-                               BLI_convertstringcode(temppath, pathname);
+                               BLI_path_abs(temppath, pathname);
                                bfd = load_game_data(temppath);
                        }
                        
@@ -278,9 +301,11 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                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));
-                                       setGamePythonPath(G.sce);
+#ifdef WITH_PYTHON
+                                       setGamePythonPath(G.main->name);
+#endif
                                }
                        }
                        // else forget it, we can't find it
@@ -289,41 +314,33 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                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.
-                       if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
-                               rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.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 (v3d->persp != V3D_CAMOB)
+                       if (rv3d->persp != RV3D_CAMOB)
                        {
                                ketsjiengine->EnableCameraOverride(startscenename);
-                               ketsjiengine->SetCameraOverrideUseOrtho((v3d->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);
                        }
@@ -340,51 +357,49 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                        if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
                                usemat = true;
 
-                       if(GPU_extensions_minimum_support())
+                       if(GPU_glsl_support())
                                useglslmat = true;
-                       else if(G.fileflags & G_FILE_GAME_MAT_GLSL)
+                       else if(scene->gm.matmode == GAME_MAT_GLSL)
                                usemat = false;
 
-            if(usemat && (G.fileflags & G_FILE_GAME_MAT))
+                       if(usemat && (scene->gm.matmode != GAME_MAT_TEXFACE))
                                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,
-                               audiodevice,
                                startscenename,
-                               blscene);
-                       
+                               scene,
+                               canvas);
+
+#ifdef WITH_PYTHON
                        // 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
-                       if(blscene->r.stereomode == RAS_IRasterizer::RAS_STEREO_DOME)
-                               ketsjiengine->InitDome(blscene->r.domeres, blscene->r.domemode, blscene->r.domeangle, blscene->r.domeresbuf, blscene->r.dometilt, blscene->r.dometext);
+                       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,
-                                       dictionaryobject,
                                        rendertools,
                                        canvas);
                                ketsjiengine->AddScene(startscene);
@@ -399,10 +414,10 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                // 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
-                               printf("\nBlender Game Engine Started\n\n");
+                               printf("\nBlender Game Engine Started\n");
                                while (!exitrequested)
                                {
                                        // first check if we want to exit
@@ -417,33 +432,47 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                                ketsjiengine->Render();
                                        }
                                        
+                                       wm_window_process_events_nosleep();
+                                       
                                        // test for the ESC key
-                                       while (qtest())
+                                       //XXX while (qtest())
+                                       while(wmEvent *event= (wmEvent *)win->queue.first)
                                        {
-                                               short val
-                                               unsigned short event = extern_qread(&val);
+                                               short val = 0;
+                                               //unsigned short event = 0; //XXX extern_qread(&val);
                                                
-                                               if (keyboarddevice->ConvertBlenderEvent(event,val))
+                                               if (keyboarddevice->ConvertBlenderEvent(event->type,event->val))
                                                        exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
                                                
                                                        /* Coordinate conversion... where
                                                        * should this really be?
                                                */
-                                               if (event==MOUSEX) {
-                                                       val = val - scrarea_get_win_x(area);
-                                               } else if (event==MOUSEY) {
-                                                       val = scrarea_get_win_height(area) - (val - scrarea_get_win_y(area)) - 1;
+                                               if (event->type==MOUSEMOVE) {
+                                                       /* Note, not nice! XXX 2.5 event hack */
+                                                       val = event->x - ar->winrct.xmin;
+                                                       mousedevice->ConvertBlenderEvent(MOUSEX, val);
+                                                       
+                                                       val = ar->winy - (event->y - ar->winrct.ymin) - 1;
+                                                       mousedevice->ConvertBlenderEvent(MOUSEY, val);
+                                               }
+                                               else {
+                                                       mousedevice->ConvertBlenderEvent(event->type,event->val);
                                                }
                                                
-                                               mousedevice->ConvertBlenderEvent(event,val);
+                                               BLI_remlink(&win->queue, 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
-                               
+#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.
@@ -463,9 +492,11 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                }
                                Py_DECREF(gameLogic_keys_new);
                                gameLogic_keys_new = NULL;
-                               
+#endif
                                ketsjiengine->StopEngine();
+#ifdef WITH_PYTHON
                                exitGamePythonScripting();
+#endif
                                networkdevice->Disconnect();
                        }
                        if (sceneconverter)
@@ -473,22 +504,25 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                delete sceneconverter;
                                sceneconverter = NULL;
                        }
-                       
+
+#ifdef WITH_PYTHON
                        Py_DECREF(gameLogic_keys);
                        gameLogic_keys = NULL;
+#endif
                }
                //lock frame and camera enabled - restoring global values
-               if (G.vd->scenelock==0){
-                       G.scene->lay= tmp_lay;
-                       G.scene->camera= tmp_camera;
+               if (v3d->scenelock==0){
+                       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
-               audiodevice->StopCD();
-               
                if (ketsjiengine)
                {
                        delete ketsjiengine;
@@ -528,239 +562,22 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                {
                        delete canvas;
                        canvas = NULL;
-               }
-               SND_DeviceManager::Unsubscribe();
+                }
        
        } 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);
 
-       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);
-}
-
-extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
-                                                                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(area);
-               //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();
-
-               // get an audiodevice
-               SND_DeviceManager::Subscribe();
-               SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
-               audiodevice->UseCD();
-
-               // 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();
-                       ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
-               }
-
-               // Quad buffered needs a special window.
-               if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
-                       rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.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,
-                               audiodevice,
-                               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
 
-                       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();
-                                       
-                               }
-                               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
-               audiodevice->StopCD();
-               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;
-               }
-               SND_DeviceManager::Unsubscribe();
-
-       } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
-
-       // Release Python's GIL
-       PyGILState_Release(gilstate);
 }