svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22205:22290
[blender.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
index d8de24c610ce9882036ce67339c6706a2e9b5cc9..086c259c4bbeefe7fd0e7585bb742951bedf7189 100644 (file)
 
 #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_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"
 
-#include "BKE_main.h"  
+#include "BKE_main.h"
 #include "BLI_blenlib.h"
 #include "BLO_readfile.h"
 #include "DNA_scene_types.h"
        /***/
 
-#include "GPU_extensions.h"
-#include "Value.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
 //XXX #include "BSE_headerbuttons.h"
-void update_for_newframe();
+#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)
 {
        ReportList reports;
@@ -111,24 +118,25 @@ static BlendFileData *load_game_data(char *filename)
        return bfd;
 }
 
-extern "C" void StartKetsjiShell(struct ScrArea *area,
-                                                                struct ARegion *ar,
-                                                                char* scenename,
-                                                                struct Main* maggie1,
-                                                                struct SpaceIpo *sipo,
-                                                                int always_use_expand_framing)
+extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int always_use_expand_framing)
 {
+       /* context values */
+       struct wmWindow *win= CTX_wm_window(C);
+       struct Scene *scene= CTX_data_scene(C);
+       struct Main* maggie1= CTX_data_main(C);
+       
+       
        int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
-       Scene *scene= NULL; // XXX give as arg
        Main* blenderdata = maggie1;
 
-       char* startscenename = scenename;
+       char* startscenename = scene->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));
+       resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
        setGamePythonPath(G.sce);
 
        // Acquire Python's GIL (global interpreter lock)
@@ -141,8 +149,8 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
 
        do
        {
-               View3D *v3d= (View3D*) area->spacedata.first;
-               RegionView3D *rv3d= (RegionView3D*) ar->regiondata;
+               View3D *v3d= CTX_wm_view3d(C);
+               RegionView3D *rv3d= CTX_wm_region_view3d(C);
 
                // get some preferences
                SYS_SystemHandle syshandle = SYS_GetSystem();
@@ -153,20 +161,20 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                bool game2ipo = (SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
                bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
                bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
-
+               bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
                // create the canvas, rasterizer and rendertools
-               RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
+               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)
+                       if (GLEW_VERSION_1_1 && !novertexarrays)
                                rasterizer = new RAS_ListRasterizer(canvas, true, true);
                        else
                                rasterizer = new RAS_ListRasterizer(canvas);
                }
-               else if (GLEW_VERSION_1_1)
+               else if (GLEW_VERSION_1_1 && !novertexarrays)
                        rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
                else
                        rasterizer = new RAS_OpenGLRasterizer(canvas);
@@ -221,13 +229,12 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                        scene->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*) rv3d->viewmat;
@@ -235,11 +242,11 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                }
                for (i = 0; i < 16; i++)
                {
-                       float *projmat_linear;                  //XXX = (float*) area->winmat;
+                       float *projmat_linear= (float*) rv3d->winmat;
                        projmat.setElem(i, projmat_linear[i]);
                }
                
-               if(v3d->persp==V3D_CAMOB) {
+               if(rv3d->persp==V3D_CAMOB) {
                        camzoom = (1.41421 + (rv3d->camzoom / 50.0));
                        camzoom *= camzoom;
                }
@@ -292,6 +299,7 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                if(blenderdata) {
                                        BLI_strncpy(G.sce, blenderdata->name, sizeof(G.sce));
                                        BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
+                                       setGamePythonPath(G.sce);
                                }
                        }
                        // else forget it, we can't find it
@@ -321,26 +329,28 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                {
                        int startFrame = blscene->r.cfra;
                        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);
+                       }
                }
-
-
-               // 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)
                {
-                       if (v3d->persp != V3D_CAMOB)
+                       if (rv3d->persp != V3D_CAMOB)
                        {
                                ketsjiengine->EnableCameraOverride(startscenename);
-                               ketsjiengine->SetCameraOverrideUseOrtho((v3d->persp == V3D_ORTHO));
+                               ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == V3D_ORTHO));
                                ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
                                ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
                                ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
+                               ketsjiengine->SetCameraOverrideLens(v3d->lens);
                        }
                        
                        // create a scene converter, create and convert the startingscene
-                       KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata,sipo, ketsjiengine);
+                       KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine);
                        ketsjiengine->SetSceneConverter(sceneconverter);
                        sceneconverter->addInitFromFrame=false;
                        if (always_use_expand_framing)
@@ -380,23 +390,22 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                        initGameKeys();
                        initPythonConstraintBinding();
                        initMathutils();
+                       initGeometry();
                        initBGL();
 #ifdef WITH_FFMPEG
                        initVideoTexture();
 #endif
 
                        //initialize Dome Settings
-                       if(blscene->r.stereomode == RAS_IRasterizer::RAS_STEREO_DOME)
-                               ketsjiengine->InitDome(blscene->r.domesize, blscene->r.domeres, blscene->r.domemode, blscene->r.domeangle, blscene->r.domeresbuf, blscene->r.dometext);
+                       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 (sceneconverter)
                        {
                                // convert and add scene
                                sceneconverter->ConvertScene(
-                                       startscenename,
                                        startscene,
                                        dictionaryobject,
-                                       keyboarddevice,
                                        rendertools,
                                        canvas);
                                ketsjiengine->AddScene(startscene);
@@ -421,7 +430,7 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                        exitrequested = ketsjiengine->GetExitCode();
                                        
                                        // kick the engine
-                                       bool render = ketsjiengine->NextFrame();
+                                       bool render = ketsjiengine->NextFrame(); // XXX 2.5 Bug, This is never true! FIXME-  Campbell
                                        
                                        if (render)
                                        {
@@ -429,26 +438,37 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                                ketsjiengine->Render();
                                        }
                                        
+                                       wm_window_process_events_nosleep(C);
+                                       
                                        // test for the ESC key
-                                       while (0) //XXX while (qtest())
+                                       //XXX while (qtest())
+                                       while(wmEvent *event= (wmEvent *)win->queue.first)
                                        {
-                                               short val
-                                               unsigned short event = 0; //XXX 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 = 0;//XXX val - scrarea_get_win_x(area);
-                                               } else if (event==MOUSEY) {
-                                                       val = 0;//XXX scrarea_get_win_height(area) - (val - scrarea_get_win_y(area)) - 1;
+                                               if (event->type==MOUSEMOVE) {
+                                                       /* Note 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);
                                        }
+                                       
                                }
                                printf("\nBlender Game Engine Finished\n\n");
                                exitstring = ketsjiengine->GetExitString();
@@ -555,11 +575,10 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
        PyGILState_Release(gilstate);
 }
 
-extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
+extern "C" void StartKetsjiShellSimulation(struct wmWindow *win,
                                                                 struct ARegion *ar,
                                                                 char* scenename,
                                                                 struct Main* maggie,
-                                                                struct SpaceIpo *sipo,
                                                                 int always_use_expand_framing)
 {
     int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
@@ -594,7 +613,7 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
                bool usemat = false;
 
                // create the canvas, rasterizer and rendertools
-               RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
+               RAS_ICanvas* canvas = new KX_BlenderCanvas(win, ar);
                //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
                RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
                RAS_IRasterizer* rasterizer = NULL;
@@ -647,18 +666,20 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
                        cframe=blscene->r.cfra;
                        startFrame = blscene->r.sfra;
                        blscene->r.cfra=startFrame;
-                       update_for_newframe();
+                       // update_for_newframe(); // XXX scene_update_for_newframe wont cut it!
                        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(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,sipo, ketsjiengine);
+                       KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie, ketsjiengine);
                        ketsjiengine->SetSceneConverter(sceneconverter);
                        sceneconverter->addInitFromFrame=true;
                        
@@ -684,6 +705,7 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
                        initGameKeys();
                        initPythonConstraintBinding();
                        initMathutils();
+                       initGeometry();
                        initBGL();
 #ifdef WITH_FFMPEG
                        initVideoTexture();
@@ -693,10 +715,8 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
                        {
                                // convert and add scene
                                sceneconverter->ConvertScene(
-                                       startscenename,
                                        startscene,
                                        dictionaryobject,
-                                       keyboarddevice,
                                        rendertools,
                                        canvas);
                                ketsjiengine->AddScene(startscene);
@@ -720,7 +740,7 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
                                        // kick the engine
                                        ketsjiengine->NextFrame();
                                    blscene->r.cfra=blscene->r.cfra+1;
-                                   update_for_newframe();
+                                   // update_for_newframe(); // XXX scene_update_for_newframe wont cut it
                                        
                                }
                                exitstring = ketsjiengine->GetExitString();
@@ -762,6 +782,16 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
                        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);