svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22205:22290
[blender.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
index 2337ab49ee90e33e43b2a3e0054bd22e48e4cead..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"
-#include "BIF_screen.h"
-#include "BIF_scrarea.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
-#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,
-                                                                struct SpaceIpo *sipo,
-                                                                int always_use_expand_framing)
+extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int always_use_expand_framing)
 {
-       int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
+       /* 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;
        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)
@@ -130,7 +149,8 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
 
        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();
@@ -141,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);
@@ -201,34 +221,33 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
 
 
                //lock frame and camera enabled - storing global values
-               int tmp_lay= G.scene->lay;
-               Object *tmp_camera = G.scene->camera;
+               int tmp_lay= scene->lay;
+               Object *tmp_camera = scene->camera;
 
-               if (G.vd->scenelock==0){
-                       G.scene->lay= v3d->lay;
-                       G.scene->camera= v3d->camera;
+               if (v3d->scenelock==0){
+                       scene->lay= v3d->lay;
+                       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*) v3d->viewmat;
+                       float *viewmat_linear= (float*) rv3d->viewmat;
                        viewmat.setElem(i, viewmat_linear[i]);
                }
                for (i = 0; i < 16; i++)
                {
-                       float *projmat_linear = (float*) area->winmat;
+                       float *projmat_linear= (float*) rv3d->winmat;
                        projmat.setElem(i, projmat_linear[i]);
                }
                
-               if(v3d->persp==V3D_CAMOB) {
-                       camzoom = (1.41421 + (v3d->camzoom / 50.0));
+               if(rv3d->persp==V3D_CAMOB) {
+                       camzoom = (1.41421 + (rv3d->camzoom / 50.0));
                        camzoom *= camzoom;
                }
                else
@@ -312,23 +331,26 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                        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)
                {
-                       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)
@@ -368,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);
@@ -409,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)
                                        {
@@ -417,26 +438,37 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                                                ketsjiengine->Render();
                                        }
                                        
+                                       wm_window_process_events_nosleep(C);
+                                       
                                        // 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 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();
@@ -478,9 +510,9 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                        gameLogic_keys = NULL;
                }
                //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){
+                       scene->lay= tmp_lay;
+                       scene->camera= tmp_camera;
                }
 
                // set the cursor back to normal
@@ -543,10 +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;
@@ -581,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;
@@ -634,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;
                        
@@ -671,6 +705,7 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
                        initGameKeys();
                        initPythonConstraintBinding();
                        initMathutils();
+                       initGeometry();
                        initBGL();
 #ifdef WITH_FFMPEG
                        initVideoTexture();
@@ -680,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);
@@ -707,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();
@@ -749,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);