doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
index 4137dc3..2424ca7 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
@@ -32,7 +32,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-#ifdef WIN32
+#if defined(WIN32) && !defined(FREE_WINDOWS)
 // don't show stl-warnings
 #pragma warning (disable:4786)
 #endif
@@ -72,11 +72,12 @@ extern "C" {
        /***/
 #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 "BKE_utildefines.h"
+
 //XXX #include "BIF_screen.h"
 //XXX #include "BIF_scrarea.h"
 
@@ -84,6 +85,7 @@ extern "C" {
 #include "BLI_blenlib.h"
 #include "BLO_readfile.h"
 #include "DNA_scene_types.h"
+#include "BKE_ipo.h"
        /***/
 
 #include "AUD_C-API.h"
@@ -120,7 +122,7 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *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);
 
 
@@ -133,16 +135,16 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
        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));
-       BLI_strncpy(oldsce, G.sce, sizeof(oldsce));
-#ifndef DISABLE_PYTHON
+       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
@@ -153,6 +155,10 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
        
        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);
@@ -166,13 +172,22 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
                bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 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(win, area_rect);
-               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;
                
@@ -213,38 +228,24 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
                ketsjiengine->SetUseFixedTime(usefixed);
                ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
 
-#ifndef DISABLE_PYTHON
+#ifdef WITH_PYTHON
                CValue::SetDeprecationWarnings(nodepwarnings);
 #endif
 
                //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){
-                       scene->lay= v3d->lay;
-                       scene->camera= v3d->camera;
+                       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*) 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==RV3D_CAMOB) {
-                       if(scene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
+                       if(startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
                                camzoom = 1.0f;
                        }
                        else {
@@ -257,7 +258,6 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
                        camzoom = 2.0;
                }
 
-               
 
                ketsjiengine->SetDrawType(v3d->drawtype);
                ketsjiengine->SetCameraZoom(camzoom);
@@ -301,10 +301,10 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
                                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));
-#ifndef DISABLE_PYTHON
-                                       setGamePythonPath(G.sce);
+#ifdef WITH_PYTHON
+                                       setGamePythonPath(G.main->name);
 #endif
                                }
                        }
@@ -315,22 +315,22 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
                        }
                }
 
-               Scene *blscene= bfd ? bfd->curscene : (Scene *)BLI_findstring(&blenderdata->scene, startscenename, offsetof(ID, name) + 2);
+               Scene *scene= bfd ? bfd->curscene : (Scene *)BLI_findstring(&blenderdata->scene, startscenename, offsetof(ID, name) + 2);
 
-               if (blscene)
+               if (scene)
                {
-                       int startFrame = blscene->r.cfra;
+                       int startFrame = scene->r.cfra;
                        ketsjiengine->SetAnimRecordMode(animation_record, 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(scene->gm.stereoflag == STEREO_ENABLED){
+                               if (scene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
+                                       rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) scene->gm.stereomode);
 
-                               rasterizer->SetEyeSeparation(blscene->gm.eyeseparation);
+                               rasterizer->SetEyeSeparation(scene->gm.eyeseparation);
                        }
 
-                       rasterizer->SetBackColor(blscene->gm.framing.col[0], blscene->gm.framing.col[1], blscene->gm.framing.col[2], 0.0f);
+                       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)
@@ -339,8 +339,8 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
                        {
                                ketsjiengine->EnableCameraOverride(startscenename);
                                ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == RV3D_ORTHO));
-                               ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
-                               ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
+                               ketsjiengine->SetCameraOverrideProjectionMatrix(MT_CmMatrix4x4(rv3d->winmat));
+                               ketsjiengine->SetCameraOverrideViewMatrix(MT_CmMatrix4x4(rv3d->viewmat));
                                ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
                                ketsjiengine->SetCameraOverrideLens(v3d->lens);
                        }
@@ -359,34 +359,41 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
 
                        if(GPU_glsl_support())
                                useglslmat = true;
-                       else if(blscene->gm.matmode == GAME_MAT_GLSL)
+                       else if(scene->gm.matmode == GAME_MAT_GLSL)
                                usemat = false;
 
-            if(usemat && (blscene->gm.matmode != GAME_MAT_TEXFACE))
+                       if(usemat && (scene->gm.matmode != GAME_MAT_TEXFACE))
                                sceneconverter->SetMaterials(true);
-                       if(useglslmat && (blscene->gm.matmode == GAME_MAT_GLSL))
+                       if(useglslmat && (scene->gm.matmode == GAME_MAT_GLSL))
                                sceneconverter->SetGLSLMaterials(true);
                                        
                        KX_Scene* startscene = new KX_Scene(keyboarddevice,
                                mousedevice,
                                networkdevice,
                                startscenename,
-                               blscene);
+                               scene,
+                               canvas);
 
-#ifndef DISABLE_PYTHON
+#ifdef WITH_PYTHON
                        // some python things
                        PyObject *gameLogic, *gameLogic_keys;
                        setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL);
-#endif // DISABLE_PYTHON
+#endif // WITH_PYTHON
 
                        //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_set3DSetting(AUD_3DS_SPEED_OF_SOUND, blscene->audio.speed_of_sound);
-                       AUD_set3DSetting(AUD_3DS_DOPPLER_FACTOR, blscene->audio.doppler_factor);
-                       AUD_set3DSetting(AUD_3DS_DISTANCE_MODEL, blscene->audio.distance_model);
+                       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)
                        {
@@ -410,14 +417,14 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
                                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
                                        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)
                                        {
@@ -425,7 +432,7 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
                                                ketsjiengine->Render();
                                        }
                                        
-                                       wm_window_process_events_nosleep(C);
+                                       wm_window_process_events_nosleep();
                                        
                                        // test for the ESC key
                                        //XXX while (qtest())
@@ -456,13 +463,16 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
                                                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
-#ifndef DISABLE_PYTHON
+#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.
@@ -484,7 +494,7 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
                                gameLogic_keys_new = NULL;
 #endif
                                ketsjiengine->StopEngine();
-#ifndef DISABLE_PYTHON
+#ifdef WITH_PYTHON
                                exitGamePythonScripting();
 #endif
                                networkdevice->Disconnect();
@@ -495,19 +505,22 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
                                sceneconverter = NULL;
                        }
 
-#ifndef DISABLE_PYTHON
+#ifdef WITH_PYTHON
                        Py_DECREF(gameLogic_keys);
                        gameLogic_keys = NULL;
 #endif
                }
                //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)
@@ -553,11 +566,14 @@ extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *c
        
        } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
        
+       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));
 
-#ifndef DISABLE_PYTHON
+#ifdef WITH_PYTHON
        Py_DECREF(pyGlobalDict);
 
        // Release Python's GIL