soc-2008-mxcurioni: merged changes to revision 15705
[blender.git] / source / gameengine / Ketsji / KX_PythonInit.cpp
index 6f7b73ee4fac079331c2fa3ec144aed9308fd7c5..61ed8b6a8e4a8567e8e92e03dedba20c75bc514a 100644 (file)
@@ -1,15 +1,12 @@
 /**
  * $Id$
  *
- * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version. The Blender
- * Foundation also sells licenses for use in proprietary software under
- * the Blender License.  See http://www.blender.org/BL/ for information
- * about this.
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  *
  * Contributor(s): none yet.
  *
- * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ * ***** END GPL LICENSE BLOCK *****
  * Initialize Python thingies.
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "GL/glew.h"
 
-#ifdef WIN32
-#include <windows.h>
-#endif // WIN32
-#ifdef __APPLE__
-#define GL_GLEXT_LEGACY 1
-#include <OpenGL/gl.h>
-#include <OpenGL/glu.h>
-#else
-#include <GL/gl.h>
-#include <GL/glu.h>
-#endif
+#include <stdlib.h>
 
 #ifdef WIN32
 #pragma warning (disable : 4786)
@@ -73,6 +58,7 @@
 #include "SND_DeviceManager.h"
 
 #include "RAS_OpenGLRasterizer/RAS_GLExtensionManager.h"
+#include "BL_Shader.h"
 
 #include "KX_PyMath.h"
 
 //#include "BPY_extern.h"
 #endif 
 
+#include "BKE_utildefines.h"
+#include "BKE_global.h"
+#include "BLI_blenlib.h"
+
 static void setSandbox(TPythonSecurityLevel level);
 
 
@@ -113,9 +103,7 @@ void        KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,cons
 static PyObject* ErrorObject;
 STR_String gPyGetRandomFloat_doc="getRandomFloat returns a random floating point value in the range [0..1)";
 
-static PyObject* gPyGetRandomFloat(PyObject*,
-                                       PyObject*, 
-                                       PyObject*)
+static PyObject* gPyGetRandomFloat(PyObject*)
 {
        return PyFloat_FromDouble(MT_random());
 }
@@ -136,13 +124,37 @@ static PyObject* gPySetGravity(PyObject*,
        return NULL;
 }
 
+static char gPyExpandPath_doc[] =
+"(path) - Converts a blender internal path into a proper file system path.\n\
+path - the string path to convert.\n\n\
+Use / as directory separator in path\n\
+You can use '//' at the start of the string to define a relative path;\n\
+Blender replaces that string by the directory of the startup .blend or runtime\n\
+file to make a full path name (doesn't change during the game, even if you load\n\
+other .blend).\n\
+The function also converts the directory separator to the local file system format.";
+
+static PyObject* gPyExpandPath(PyObject*,
+                                                               PyObject* args, 
+                                                               PyObject*)
+{
+       char expanded[FILE_MAXDIR + FILE_MAXFILE];
+       char* filename;
+       
+       if (PyArg_ParseTuple(args,"s",&filename))
+       {
+               BLI_strncpy(expanded, filename, FILE_MAXDIR + FILE_MAXFILE);
+               BLI_convertstringcode(expanded, G.sce);
+               return PyString_FromString(expanded);
+       }
+       return NULL;
+}
+
 
 static bool usedsp = false;
 
 // this gets a pointer to an array filled with floats
-static PyObject* gPyGetSpectrum(PyObject*,
-                                                               PyObject* args, 
-                                                               PyObject*)
+static PyObject* gPyGetSpectrum(PyObject*)
 {
        SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
 
@@ -221,7 +233,7 @@ static PyObject* gPySetLogicTicRate(PyObject*,
        return NULL;
 }
 
-static PyObject* gPyGetLogicTicRate(PyObject*, PyObject*, PyObject*)
+static PyObject* gPyGetLogicTicRate(PyObject*)
 {
        return PyFloat_FromDouble(KX_KetsjiEngine::GetTicRate());
 }
@@ -257,7 +269,7 @@ static PyObject* gPySetPhysicsDebug(PyObject*,
 
 
 
-static PyObject* gPyGetPhysicsTicRate(PyObject*, PyObject*, PyObject*)
+static PyObject* gPyGetPhysicsTicRate(PyObject*)
 {
        return PyFloat_FromDouble(PHY_GetActiveEnvironment()->getFixedTimeStep());
 }
@@ -265,9 +277,7 @@ static PyObject* gPyGetPhysicsTicRate(PyObject*, PyObject*, PyObject*)
 static STR_String gPyGetCurrentScene_doc =  
 "getCurrentScene()\n"
 "Gets a reference to the current scene.\n";
-static PyObject* gPyGetCurrentScene(PyObject* self,
-                                          PyObject* args, 
-                                          PyObject* kwds)
+static PyObject* gPyGetCurrentScene(PyObject* self)
 {
        Py_INCREF(gp_KetsjiScene);
        return (PyObject*) gp_KetsjiScene;
@@ -276,17 +286,13 @@ static PyObject* gPyGetCurrentScene(PyObject* self,
 static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
 {
 #define pprint(x) std::cout << x << std::endl;
-       bgl::BL_EXTInfo ext = bgl::RAS_EXT_support;
        bool count=0;
        bool support=0;
        pprint("Supported Extensions...");
-#ifdef GL_ARB_shader_objects
-       pprint(" GL_ARB_shader_objects supported?       "<< (ext._ARB_shader_objects?"yes.":"no."));
+       pprint(" GL_ARB_shader_objects supported?       "<< (GLEW_ARB_shader_objects?"yes.":"no."));
        count = 1;
-#endif
 
-#ifdef GL_ARB_vertex_shader
-       support= ext._ARB_vertex_shader;
+       support= GLEW_ARB_vertex_shader;
        pprint(" GL_ARB_vertex_shader supported?        "<< (support?"yes.":"no."));
        count = 1;
        if(support){
@@ -305,9 +311,8 @@ static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
                pprint("  Max combined texture units." << max);
                pprint("");
        }
-#endif
-#ifdef GL_ARB_fragment_shader
-       support=ext._ARB_fragment_shader;
+
+       support=GLEW_ARB_fragment_shader;
        pprint(" GL_ARB_fragment_shader supported?      "<< (support?"yes.":"no."));
        count = 1;
        if(support){
@@ -317,9 +322,8 @@ static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
                pprint("  Max uniform components." << max);
                pprint("");
        }
-#endif
-#ifdef GL_ARB_texture_cube_map
-       support = ext._ARB_texture_cube_map;
+
+       support = GLEW_ARB_texture_cube_map;
        pprint(" GL_ARB_texture_cube_map supported?     "<< (support?"yes.":"no."));
        count = 1;
        if(support){
@@ -329,9 +333,8 @@ static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
                pprint("  Max cubemap size." << size);
                pprint("");
        }
-#endif
-#ifdef GL_ARB_multitexture
-       support = ext._ARB_multitexture;
+
+       support = GLEW_ARB_multitexture;
        count = 1;
        pprint(" GL_ARB_multitexture supported?         "<< (support?"yes.":"no."));
        if(support){
@@ -341,11 +344,10 @@ static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
                pprint("  Max texture units available.  " << units);
                pprint("");
        }
-#endif
-#ifdef GL_ARB_texture_env_combine
-       pprint(" GL_ARB_texture_env_combine supported?  "<< (ext._ARB_texture_env_combine?"yes.":"no."));
+
+       pprint(" GL_ARB_texture_env_combine supported?  "<< (GLEW_ARB_texture_env_combine?"yes.":"no."));
        count = 1;
-#endif
+
        if(!count)
                pprint("No extenstions are used in this build");
 
@@ -355,21 +357,22 @@ static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
 
 
 static struct PyMethodDef game_methods[] = {
+       {"expandPath", (PyCFunction)gPyExpandPath, METH_VARARGS, gPyExpandPath_doc},
        {"getCurrentController",
        (PyCFunction) SCA_PythonController::sPyGetCurrentController,
-       METH_VARARGS, SCA_PythonController::sPyGetCurrentController__doc__},
+       METH_NOARGS, SCA_PythonController::sPyGetCurrentController__doc__},
        {"getCurrentScene", (PyCFunction) gPyGetCurrentScene,
-       METH_VARARGS, gPyGetCurrentScene_doc.Ptr()},
+       METH_NOARGS, gPyGetCurrentScene_doc.Ptr()},
        {"addActiveActuator",(PyCFunction) SCA_PythonController::sPyAddActiveActuator,
        METH_VARARGS, SCA_PythonController::sPyAddActiveActuator__doc__},
        {"getRandomFloat",(PyCFunction) gPyGetRandomFloat,
-       METH_VARARGS,gPyGetRandomFloat_doc.Ptr()},
+       METH_NOARGS,gPyGetRandomFloat_doc.Ptr()},
        {"setGravity",(PyCFunction) gPySetGravity, METH_VARARGS,"set Gravitation"},
-       {"getSpectrum",(PyCFunction) gPyGetSpectrum, METH_VARARGS,"get audio spectrum"},
+       {"getSpectrum",(PyCFunction) gPyGetSpectrum, METH_NOARGS,"get audio spectrum"},
        {"stopDSP",(PyCFunction) gPyStopDSP, METH_VARARGS,"stop using the audio dsp (for performance reasons)"},
-       {"getLogicTicRate", (PyCFunction) gPyGetLogicTicRate, METH_VARARGS, "Gets the logic tic rate"},
+       {"getLogicTicRate", (PyCFunction) gPyGetLogicTicRate, METH_NOARGS, "Gets the logic tic rate"},
        {"setLogicTicRate", (PyCFunction) gPySetLogicTicRate, METH_VARARGS, "Sets the logic tic rate"},
-       {"getPhysicsTicRate", (PyCFunction) gPyGetPhysicsTicRate, METH_VARARGS, "Gets the physics tic rate"},
+       {"getPhysicsTicRate", (PyCFunction) gPyGetPhysicsTicRate, METH_NOARGS, "Gets the physics tic rate"},
        {"setPhysicsTicRate", (PyCFunction) gPySetPhysicsTicRate, METH_VARARGS, "Sets the physics tic rate"},
        {"PrintGLInfo", (PyCFunction)pyPrintExt, METH_NOARGS, "Prints GL Extension Info"},
        {NULL, (PyCFunction) NULL, 0, NULL }
@@ -416,7 +419,7 @@ static PyObject* gPyEnableVisibility(PyObject*,
        }
        else
        {
-         Py_Return;         
+               return NULL;
        }
    Py_Return;
 }
@@ -440,6 +443,9 @@ static PyObject* gPyShowMouse(PyObject*,
                                gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
                }
        }
+       else {
+               return NULL;
+       }
        
    Py_Return;
 }
@@ -456,6 +462,9 @@ static PyObject* gPySetMousePosition(PyObject*,
            if (gp_Canvas)
                        gp_Canvas->SetMousePosition(x,y);
        }
+       else {
+               return NULL;
+       }
        
    Py_Return;
 }
@@ -559,6 +568,9 @@ static PyObject* gPySetMistStart(PyObject*,
                        gp_Rasterizer->SetFogStart(miststart);
                }
        }
+       else {
+               return NULL;
+       }
    Py_Return;
 }
 
@@ -577,6 +589,9 @@ static PyObject* gPySetMistEnd(PyObject*,
                        gp_Rasterizer->SetFogEnd(mistend);
                }
        }
+       else {
+               return NULL;
+       }
    Py_Return;
 }
 
@@ -614,10 +629,40 @@ static PyObject* gPyMakeScreenshot(PyObject*,
                        gp_Canvas->MakeScreenShot(filename);
                }
        }
+       else {
+               return NULL;
+       }
        Py_Return;
 }
 
+static PyObject* gPyEnableMotionBlur(PyObject*,
+                                                                       PyObject* args,
+                                                                       PyObject*)
+{
+       float motionblurvalue;
+       if (PyArg_ParseTuple(args,"f",&motionblurvalue))
+       {
+               if(gp_Rasterizer)
+               {
+                       gp_Rasterizer->EnableMotionBlur(motionblurvalue);
+               }
+       }
+       else {
+               return NULL;
+       }
+       Py_Return;
+}
 
+static PyObject* gPyDisableMotionBlur(PyObject*,
+                                                                       PyObject* args,
+                                                                       PyObject*)
+{
+       if(gp_Rasterizer)
+       {
+               gp_Rasterizer->DisableMotionBlur();
+       }
+       Py_Return;
+}
 
 STR_String     gPyGetWindowHeight__doc__="getWindowHeight doc";
 STR_String     gPyGetWindowWidth__doc__="getWindowWidth doc";
@@ -644,6 +689,9 @@ static struct PyMethodDef rasterizer_methods[] = {
  {"setMistColor",(PyCFunction)gPySetMistColor,METH_VARARGS,"set Mist Color (rgb)"},
   {"setMistStart",(PyCFunction)gPySetMistStart,METH_VARARGS,"set Mist Start(rgb)"},
   {"setMistEnd",(PyCFunction)gPySetMistEnd,METH_VARARGS,"set Mist End(rgb)"},
+  {"enableMotionBlur",(PyCFunction)gPyEnableMotionBlur,METH_VARARGS,"enable motion blur"},
+  {"disableMotionBlur",(PyCFunction)gPyDisableMotionBlur,METH_VARARGS,"disable motion blur"},
+
   
   {"setEyeSeparation", (PyCFunction) gPySetEyeSeparation, METH_VARARGS, "set the eye separation for stereo mode"},
   {"getEyeSeparation", (PyCFunction) gPyGetEyeSeparation, METH_VARARGS, "get the eye separation for stereo mode"},
@@ -756,17 +804,33 @@ PyObject* initGameLogic(KX_Scene* scene) // quick hack to get gravity hook
        KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
        KX_MACRO_addTypesToDict(d, BL_SRC_ALPHA_SATURATE, GL_SRC_ALPHA_SATURATE);
 
+
+       /* 9. UniformTypes */
+       KX_MACRO_addTypesToDict(d, SHD_TANGENT, BL_Shader::SHD_TANGENT);
+       KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX, BL_Shader::MODELVIEWMATRIX);
+       KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_TRANSPOSE, BL_Shader::MODELVIEWMATRIX_TRANSPOSE);
+       KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_INVERSE, BL_Shader::MODELVIEWMATRIX_INVERSE);
+       KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_INVERSETRANSPOSE, BL_Shader::MODELVIEWMATRIX_INVERSETRANSPOSE);
+       KX_MACRO_addTypesToDict(d, MODELMATRIX, BL_Shader::MODELMATRIX);
+       KX_MACRO_addTypesToDict(d, MODELMATRIX_TRANSPOSE, BL_Shader::MODELMATRIX_TRANSPOSE);
+       KX_MACRO_addTypesToDict(d, MODELMATRIX_INVERSE, BL_Shader::MODELMATRIX_INVERSE);
+       KX_MACRO_addTypesToDict(d, MODELMATRIX_INVERSETRANSPOSE, BL_Shader::MODELMATRIX_INVERSETRANSPOSE);
+       KX_MACRO_addTypesToDict(d, VIEWMATRIX, BL_Shader::VIEWMATRIX);
+       KX_MACRO_addTypesToDict(d, VIEWMATRIX_TRANSPOSE, BL_Shader::VIEWMATRIX_TRANSPOSE);
+       KX_MACRO_addTypesToDict(d, VIEWMATRIX_INVERSE, BL_Shader::VIEWMATRIX_INVERSE);
+       KX_MACRO_addTypesToDict(d, VIEWMATRIX_INVERSETRANSPOSE, BL_Shader::VIEWMATRIX_INVERSETRANSPOSE);
+       KX_MACRO_addTypesToDict(d, CAM_POS, BL_Shader::CAM_POS);
+       KX_MACRO_addTypesToDict(d, CONSTANT_TIMER, BL_Shader::CONSTANT_TIMER);
+
        // Check for errors
        if (PyErr_Occurred())
     {
                Py_FatalError("can't initialize module GameLogic");
     }
 
-       return d;
+       return m;
 }
 
-
-
 // Python Sandbox code
 // override builtin functions import() and open()
 
@@ -802,7 +866,7 @@ PyObject *KXpy_import(PyObject *self, PyObject *args)
 
        /* quick hack for GamePython modules 
                TODO: register builtin modules properly by ExtendInittab */
-       if (!strcmp(name, "GameLogic") || !strcmp(name, "GameKeys") ||
+       if (!strcmp(name, "GameLogic") || !strcmp(name, "GameKeys") || !strcmp(name, "PhysicsConstraints") ||
                !strcmp(name, "Rasterizer")) {
                return PyImport_ImportModuleEx(name, globals, locals, fromlist);
        }
@@ -1100,3 +1164,8 @@ void PHY_SetActiveScene(class KX_Scene* scene)
 {
        gp_KetsjiScene = scene;
 }
+
+class KX_Scene* PHY_GetActiveScene()
+{
+       return gp_KetsjiScene;
+}