Blender V2.61 - r43446

KX_PythonInit.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  * Initialize Python thingies.
00027  */
00028 
00034 #include "GL/glew.h"
00035 
00036 #if defined(WIN32) && !defined(FREE_WINDOWS)
00037 #pragma warning (disable : 4786)
00038 #endif //WIN32
00039 
00040 #ifdef WITH_PYTHON
00041 
00042 #ifdef _POSIX_C_SOURCE
00043 #undef _POSIX_C_SOURCE
00044 #endif
00045 
00046 #ifdef _XOPEN_SOURCE
00047 #undef _XOPEN_SOURCE
00048 #endif
00049 
00050 #if defined(__sun) || defined(sun) 
00051 #if defined(_XPG4) 
00052 #undef _XPG4 
00053 #endif 
00054 #endif 
00055 
00056 #include <Python.h>
00057 
00058 extern "C" {
00059     #include "bpy_internal_import.h"  /* from the blender python api, but we want to import text too! */
00060     #include "py_capi_utils.h"
00061     #include "mathutils.h" // 'mathutils' module copied here so the blenderlayer can use.
00062     #include "bgl.h"
00063     #include "blf_py_api.h"
00064 
00065     #include "marshal.h" /* python header for loading/saving dicts */
00066 }
00067 
00068 #include "AUD_PyInit.h"
00069 
00070 #endif
00071 
00072 #include "KX_PythonInit.h"
00073 
00074 // directory header for py function getBlendFileList
00075 #ifndef WIN32
00076   #include <dirent.h>
00077   #include <stdlib.h>
00078 #else
00079   #include <io.h>
00080   #include "BLI_winstuff.h"
00081 #endif
00082 
00083 //python physics binding
00084 #include "KX_PyConstraintBinding.h"
00085 
00086 #include "KX_KetsjiEngine.h"
00087 #include "KX_RadarSensor.h"
00088 #include "KX_RaySensor.h"
00089 #include "KX_ArmatureSensor.h"
00090 #include "KX_SceneActuator.h"
00091 #include "KX_GameActuator.h"
00092 #include "KX_ParentActuator.h"
00093 #include "KX_SCA_DynamicActuator.h"
00094 #include "KX_SteeringActuator.h"
00095 #include "KX_NavMeshObject.h"
00096 
00097 #include "SCA_IInputDevice.h"
00098 #include "SCA_PropertySensor.h"
00099 #include "SCA_RandomActuator.h"
00100 #include "SCA_KeyboardSensor.h" /* IsPrintable, ToCharacter */
00101 #include "SCA_PythonKeyboard.h"
00102 #include "SCA_PythonMouse.h"
00103 #include "KX_ConstraintActuator.h"
00104 #include "KX_IpoActuator.h"
00105 #include "KX_SoundActuator.h"
00106 #include "KX_StateActuator.h"
00107 #include "BL_ActionActuator.h"
00108 #include "BL_ArmatureObject.h"
00109 #include "RAS_IRasterizer.h"
00110 #include "RAS_ICanvas.h"
00111 #include "RAS_BucketManager.h"
00112 #include "RAS_2DFilterManager.h"
00113 #include "MT_Vector3.h"
00114 #include "MT_Point3.h"
00115 #include "ListValue.h"
00116 #include "InputParser.h"
00117 #include "KX_Scene.h"
00118 
00119 #include "NG_NetworkScene.h" //Needed for sendMessage()
00120 
00121 #include "BL_Shader.h"
00122 #include "BL_Action.h"
00123 
00124 #include "KX_PyMath.h"
00125 
00126 #include "PyObjectPlus.h"
00127 
00128 #include "KX_PythonInitTypes.h" 
00129 
00130 /* we only need this to get a list of libraries from the main struct */
00131 #include "DNA_ID.h"
00132 #include "DNA_scene_types.h"
00133 
00134 #include "PHY_IPhysicsEnvironment.h"
00135 #include "BKE_main.h"
00136 #include "BKE_utildefines.h"
00137 #include "BKE_global.h"
00138 #include "BLI_blenlib.h"
00139 #include "GPU_material.h"
00140 #include "MEM_guardedalloc.h"
00141 
00142 /* for converting new scenes */
00143 #include "KX_BlenderSceneConverter.h"
00144 #include "KX_MeshProxy.h" /* for creating a new library of mesh objects */
00145 extern "C" {
00146     #include "BKE_idcode.h"
00147 }
00148 
00149 #include "NG_NetworkScene.h" //Needed for sendMessage()
00150 
00151 // 'local' copy of canvas ptr, for window height/width python scripts
00152 
00153 #ifdef WITH_PYTHON
00154 
00155 static RAS_ICanvas* gp_Canvas = NULL;
00156 static char gp_GamePythonPath[FILE_MAX] = "";
00157 static char gp_GamePythonPathOrig[FILE_MAX] = ""; // not super happy about this, but we need to remember the first loaded file for the global/dict load save
00158 
00159 static SCA_PythonKeyboard* gp_PythonKeyboard = NULL;
00160 static SCA_PythonMouse* gp_PythonMouse = NULL;
00161 #endif // WITH_PYTHON
00162 
00163 static KX_Scene*    gp_KetsjiScene = NULL;
00164 static KX_KetsjiEngine* gp_KetsjiEngine = NULL;
00165 static RAS_IRasterizer* gp_Rasterizer = NULL;
00166 
00167 
00168 void KX_SetActiveScene(class KX_Scene* scene)
00169 {
00170     gp_KetsjiScene = scene;
00171 }
00172 
00173 class KX_Scene* KX_GetActiveScene()
00174 {
00175     return gp_KetsjiScene;
00176 }
00177 
00178 class KX_KetsjiEngine* KX_GetActiveEngine()
00179 {
00180     return gp_KetsjiEngine;
00181 }
00182 
00183 /* why is this in python? */
00184 void    KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,const MT_Vector3& color)
00185 {
00186     if (gp_Rasterizer)
00187         gp_Rasterizer->DrawDebugLine(from,to,color);
00188 }
00189 
00190 void    KX_RasterizerDrawDebugCircle(const MT_Vector3& center, const MT_Scalar radius, const MT_Vector3& color,
00191                                      const MT_Vector3& normal, int nsector)
00192 {
00193     if (gp_Rasterizer)
00194         gp_Rasterizer->DrawDebugCircle(center, radius, color, normal, nsector);
00195 }
00196 
00197 #ifdef WITH_PYTHON
00198 
00199 static PyObject *gp_OrigPythonSysPath= NULL;
00200 static PyObject *gp_OrigPythonSysModules= NULL;
00201 
00202 /* Macro for building the keyboard translation */
00203 //#define KX_MACRO_addToDict(dict, name) PyDict_SetItemString(dict, #name, PyLong_FromSsize_t(SCA_IInputDevice::KX_##name))
00204 #define KX_MACRO_addToDict(dict, name) PyDict_SetItemString(dict, #name, item=PyLong_FromSsize_t(name)); Py_DECREF(item)
00205 /* For the defines for types from logic bricks, we do stuff explicitly... */
00206 #define KX_MACRO_addTypesToDict(dict, name, name2) PyDict_SetItemString(dict, #name, item=PyLong_FromSsize_t(name2)); Py_DECREF(item)
00207 
00208 
00209 // temporarily python stuff, will be put in another place later !
00210 #include "KX_Python.h"
00211 #include "SCA_PythonController.h"
00212 // List of methods defined in the module
00213 
00214 static PyObject* ErrorObject;
00215 static const char *gPyGetRandomFloat_doc="getRandomFloat returns a random floating point value in the range [0..1]";
00216 
00217 static PyObject* gPyGetRandomFloat(PyObject*)
00218 {
00219     return PyFloat_FromDouble(MT_random());
00220 }
00221 
00222 static PyObject* gPySetGravity(PyObject*, PyObject* value)
00223 {
00224     MT_Vector3 vec;
00225     if (!PyVecTo(value, vec))
00226         return NULL;
00227 
00228     if (gp_KetsjiScene)
00229         gp_KetsjiScene->SetGravity(vec);
00230     
00231     Py_RETURN_NONE;
00232 }
00233 
00234 static char gPyExpandPath_doc[] =
00235 "(path) - Converts a blender internal path into a proper file system path.\n\
00236 path - the string path to convert.\n\n\
00237 Use / as directory separator in path\n\
00238 You can use '//' at the start of the string to define a relative path;\n\
00239 Blender replaces that string by the directory of the startup .blend or runtime\n\
00240 file to make a full path name (doesn't change during the game, even if you load\n\
00241 other .blend).\n\
00242 The function also converts the directory separator to the local file system format.";
00243 
00244 static PyObject* gPyExpandPath(PyObject*, PyObject* args)
00245 {
00246     char expanded[FILE_MAX];
00247     char* filename;
00248     
00249     if (!PyArg_ParseTuple(args,"s:ExpandPath",&filename))
00250         return NULL;
00251 
00252     BLI_strncpy(expanded, filename, FILE_MAX);
00253     BLI_path_abs(expanded, gp_GamePythonPath);
00254     return PyUnicode_DecodeFSDefault(expanded);
00255 }
00256 
00257 static char gPyStartGame_doc[] =
00258 "startGame(blend)\n\
00259 Loads the blend file";
00260 
00261 static PyObject* gPyStartGame(PyObject*, PyObject* args)
00262 {
00263     char* blendfile;
00264 
00265     if (!PyArg_ParseTuple(args, "s:startGame", &blendfile))
00266         return NULL;
00267 
00268     gp_KetsjiEngine->RequestExit(KX_EXIT_REQUEST_START_OTHER_GAME);
00269     gp_KetsjiEngine->SetNameNextGame(blendfile);
00270 
00271     Py_RETURN_NONE;
00272 }
00273 
00274 static char gPyEndGame_doc[] =
00275 "endGame()\n\
00276 Ends the current game";
00277 
00278 static PyObject* gPyEndGame(PyObject*)
00279 {
00280     gp_KetsjiEngine->RequestExit(KX_EXIT_REQUEST_QUIT_GAME);
00281 
00282     //printf("%s\n", gp_GamePythonPath);
00283 
00284     Py_RETURN_NONE;
00285 }
00286 
00287 static char gPyRestartGame_doc[] =
00288 "restartGame()\n\
00289 Restarts the current game by reloading the .blend file";
00290 
00291 static PyObject* gPyRestartGame(PyObject*)
00292 {
00293     gp_KetsjiEngine->RequestExit(KX_EXIT_REQUEST_RESTART_GAME);
00294     gp_KetsjiEngine->SetNameNextGame(gp_GamePythonPath);
00295 
00296     Py_RETURN_NONE;
00297 }
00298 
00299 static char gPySaveGlobalDict_doc[] =
00300     "saveGlobalDict()\n"
00301     "Saves bge.logic.globalDict to a file";
00302 
00303 static PyObject* gPySaveGlobalDict(PyObject*)
00304 {
00305     char marshal_path[512];
00306     char *marshal_buffer = NULL;
00307     unsigned int marshal_length;
00308     FILE *fp = NULL;
00309 
00310     pathGamePythonConfig(marshal_path);
00311     marshal_length = saveGamePythonConfig(&marshal_buffer);
00312 
00313     if (marshal_length && marshal_buffer)
00314     {
00315         fp = fopen(marshal_path, "wb");
00316 
00317         if (fp)
00318         {
00319             if (fwrite(marshal_buffer, 1, marshal_length, fp) != marshal_length)
00320                 printf("Warning: could not write marshal data\n");
00321 
00322             fclose(fp);
00323         } else {
00324             printf("Warning: could not open marshal file\n");
00325         }
00326     } else {
00327         printf("Warning: could not create marshal buffer\n");
00328     }
00329 
00330     if (marshal_buffer)
00331         delete [] marshal_buffer;
00332 
00333     Py_RETURN_NONE;
00334 }
00335 
00336 static char gPyLoadGlobalDict_doc[] =
00337     "LoadGlobalDict()\n"
00338     "Loads bge.logic.globalDict from a file";
00339 
00340 static PyObject* gPyLoadGlobalDict(PyObject*)
00341 {
00342     char marshal_path[512];
00343     char *marshal_buffer = NULL;
00344     size_t marshal_length;
00345     FILE *fp = NULL;
00346     int result;
00347 
00348     pathGamePythonConfig(marshal_path);
00349 
00350     fp = fopen(marshal_path, "rb");
00351 
00352     if (fp) {
00353         // obtain file size:
00354         fseek (fp, 0, SEEK_END);
00355         marshal_length = (size_t)ftell(fp);
00356         rewind(fp);
00357 
00358         marshal_buffer = (char*)malloc (sizeof(char)*marshal_length);
00359 
00360         result = fread(marshal_buffer, 1, marshal_length, fp);
00361 
00362         if (result == marshal_length) {
00363             loadGamePythonConfig(marshal_buffer, marshal_length);
00364         } else {
00365             printf("Warning: could not read all of '%s'\n", marshal_path);
00366         }
00367 
00368         free(marshal_buffer);
00369         fclose(fp);
00370     } else {
00371         printf("Warning: could not open '%s'\n", marshal_path);
00372     }
00373 
00374     Py_RETURN_NONE;
00375 }
00376 
00377 static char gPySendMessage_doc[] = 
00378 "sendMessage(subject, [body, to, from])\n\
00379 sends a message in same manner as a message actuator\
00380 subject = Subject of the message\
00381 body = Message body\
00382 to = Name of object to send the message to\
00383 from = Name of object to send the string from";
00384 
00385 static PyObject* gPySendMessage(PyObject*, PyObject* args)
00386 {
00387     char* subject;
00388     char* body = (char *)"";
00389     char* to = (char *)"";
00390     char* from = (char *)"";
00391 
00392     if (!PyArg_ParseTuple(args, "s|sss:sendMessage", &subject, &body, &to, &from))
00393         return NULL;
00394 
00395     gp_KetsjiScene->GetNetworkScene()->SendMessage(to, from, subject, body);
00396 
00397     Py_RETURN_NONE;
00398 }
00399 
00400 // this gets a pointer to an array filled with floats
00401 static PyObject* gPyGetSpectrum(PyObject*)
00402 {
00403     PyObject* resultlist = PyList_New(512);
00404 
00405     for (int index = 0; index < 512; index++)
00406     {
00407         PyList_SET_ITEM(resultlist, index, PyFloat_FromDouble(0.0));
00408     }
00409 
00410     return resultlist;
00411 }
00412 
00413 static PyObject* gPySetLogicTicRate(PyObject*, PyObject* args)
00414 {
00415     float ticrate;
00416     if (!PyArg_ParseTuple(args, "f:setLogicTicRate", &ticrate))
00417         return NULL;
00418     
00419     KX_KetsjiEngine::SetTicRate(ticrate);
00420     Py_RETURN_NONE;
00421 }
00422 
00423 static PyObject* gPyGetLogicTicRate(PyObject*)
00424 {
00425     return PyFloat_FromDouble(KX_KetsjiEngine::GetTicRate());
00426 }
00427 
00428 static PyObject* gPySetExitKey(PyObject*, PyObject* args)
00429 {
00430     short exitkey;
00431     if (!PyArg_ParseTuple(args, "h:setExitKey", &exitkey))
00432         return NULL;
00433     KX_KetsjiEngine::SetExitKey(exitkey);
00434     Py_RETURN_NONE;
00435 }
00436 
00437 static PyObject* gPyGetExitKey(PyObject*)
00438 {
00439     return PyLong_FromSsize_t(KX_KetsjiEngine::GetExitKey());
00440 }
00441 
00442 static PyObject* gPySetMaxLogicFrame(PyObject*, PyObject* args)
00443 {
00444     int frame;
00445     if (!PyArg_ParseTuple(args, "i:setMaxLogicFrame", &frame))
00446         return NULL;
00447     
00448     KX_KetsjiEngine::SetMaxLogicFrame(frame);
00449     Py_RETURN_NONE;
00450 }
00451 
00452 static PyObject* gPyGetMaxLogicFrame(PyObject*)
00453 {
00454     return PyLong_FromSsize_t(KX_KetsjiEngine::GetMaxLogicFrame());
00455 }
00456 
00457 static PyObject* gPySetMaxPhysicsFrame(PyObject*, PyObject* args)
00458 {
00459     int frame;
00460     if (!PyArg_ParseTuple(args, "i:setMaxPhysicsFrame", &frame))
00461         return NULL;
00462     
00463     KX_KetsjiEngine::SetMaxPhysicsFrame(frame);
00464     Py_RETURN_NONE;
00465 }
00466 
00467 static PyObject* gPyGetMaxPhysicsFrame(PyObject*)
00468 {
00469     return PyLong_FromSsize_t(KX_KetsjiEngine::GetMaxPhysicsFrame());
00470 }
00471 
00472 static PyObject* gPySetPhysicsTicRate(PyObject*, PyObject* args)
00473 {
00474     float ticrate;
00475     if (!PyArg_ParseTuple(args, "f:setPhysicsTicRate", &ticrate))
00476         return NULL;
00477     
00478     PHY_GetActiveEnvironment()->setFixedTimeStep(true,ticrate);
00479     Py_RETURN_NONE;
00480 }
00481 #if 0 // unused
00482 static PyObject* gPySetPhysicsDebug(PyObject*, PyObject* args)
00483 {
00484     int debugMode;
00485     if (!PyArg_ParseTuple(args, "i:setPhysicsDebug", &debugMode))
00486         return NULL;
00487     
00488     PHY_GetActiveEnvironment()->setDebugMode(debugMode);
00489     Py_RETURN_NONE;
00490 }
00491 #endif
00492 
00493 
00494 static PyObject* gPyGetPhysicsTicRate(PyObject*)
00495 {
00496     return PyFloat_FromDouble(PHY_GetActiveEnvironment()->getFixedTimeStep());
00497 }
00498 
00499 static PyObject* gPyGetAverageFrameRate(PyObject*)
00500 {
00501     return PyFloat_FromDouble(KX_KetsjiEngine::GetAverageFrameRate());
00502 }
00503 
00504 static PyObject* gPyGetBlendFileList(PyObject*, PyObject* args)
00505 {
00506     char cpath[sizeof(gp_GamePythonPath)];
00507     char *searchpath = NULL;
00508     PyObject* list, *value;
00509 
00510     DIR *dp;
00511     struct dirent *dirp;
00512 
00513     if (!PyArg_ParseTuple(args, "|s:getBlendFileList", &searchpath))
00514         return NULL;
00515 
00516     list = PyList_New(0);
00517     
00518     if (searchpath) {
00519         BLI_strncpy(cpath, searchpath, FILE_MAX);
00520         BLI_path_abs(cpath, gp_GamePythonPath);
00521     } else {
00522         /* Get the dir only */
00523         BLI_split_dir_part(gp_GamePythonPath, cpath, sizeof(cpath));
00524     }
00525 
00526     if((dp  = opendir(cpath)) == NULL) {
00527         /* todo, show the errno, this shouldnt happen anyway if the blendfile is readable */
00528         fprintf(stderr, "Could not read directoty (%s) failed, code %d (%s)\n", cpath, errno, strerror(errno));
00529         return list;
00530     }
00531     
00532     while ((dirp = readdir(dp)) != NULL) {
00533         if (BLI_testextensie(dirp->d_name, ".blend")) {
00534             value= PyUnicode_DecodeFSDefault(dirp->d_name);
00535             PyList_Append(list, value);
00536             Py_DECREF(value);
00537         }
00538     }
00539     
00540     closedir(dp);
00541     return list;
00542 }
00543 
00544 static char gPyAddScene_doc[] = 
00545 "addScene(name, [overlay])\n\
00546 adds a scene to the game engine\n\
00547 name = Name of the scene\n\
00548 overlay = Overlay or underlay";
00549 static PyObject* gPyAddScene(PyObject*, PyObject* args)
00550 {
00551     char* name;
00552     int overlay = 1;
00553     
00554     if (!PyArg_ParseTuple(args, "s|i:addScene", &name , &overlay))
00555         return NULL;
00556     
00557     gp_KetsjiEngine->ConvertAndAddScene(name, (overlay != 0));
00558 
00559     Py_RETURN_NONE;
00560 }
00561 
00562 static const char *gPyGetCurrentScene_doc =
00563 "getCurrentScene()\n"
00564 "Gets a reference to the current scene.\n";
00565 static PyObject* gPyGetCurrentScene(PyObject* self)
00566 {
00567     return gp_KetsjiScene->GetProxy();
00568 }
00569 
00570 static const char *gPyGetSceneList_doc =
00571 "getSceneList()\n"
00572 "Return a list of converted scenes.\n";
00573 static PyObject* gPyGetSceneList(PyObject* self)
00574 {
00575     KX_KetsjiEngine* m_engine = KX_GetActiveEngine();
00576     PyObject* list;
00577     KX_SceneList* scenes = m_engine->CurrentScenes();
00578     int numScenes = scenes->size();
00579     int i;
00580     
00581     list = PyList_New(numScenes);
00582     
00583     for (i=0;i<numScenes;i++)
00584     {
00585         KX_Scene* scene = scenes->at(i);
00586         PyList_SET_ITEM(list, i, scene->GetProxy());
00587     }
00588 
00589     return list;
00590 }
00591 
00592 static PyObject *pyPrintStats(PyObject *,PyObject *,PyObject *)
00593 {
00594     gp_KetsjiScene->GetSceneConverter()->PrintStats();
00595     Py_RETURN_NONE;
00596 }
00597 
00598 static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
00599 {
00600 #define pprint(x) std::cout << x << std::endl;
00601     bool count=0;
00602     bool support=0;
00603     pprint("Supported Extensions...");
00604     pprint(" GL_ARB_shader_objects supported?       "<< (GLEW_ARB_shader_objects?"yes.":"no."));
00605     count = 1;
00606 
00607     support= GLEW_ARB_vertex_shader;
00608     pprint(" GL_ARB_vertex_shader supported?        "<< (support?"yes.":"no."));
00609     count = 1;
00610     if(support){
00611         pprint(" ----------Details----------");
00612         int max=0;
00613         glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB, (GLint*)&max);
00614         pprint("  Max uniform components." << max);
00615 
00616         glGetIntegerv(GL_MAX_VARYING_FLOATS_ARB, (GLint*)&max);
00617         pprint("  Max varying floats." << max);
00618 
00619         glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, (GLint*)&max);
00620         pprint("  Max vertex texture units." << max);
00621     
00622         glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB, (GLint*)&max);
00623         pprint("  Max combined texture units." << max);
00624         pprint("");
00625     }
00626 
00627     support=GLEW_ARB_fragment_shader;
00628     pprint(" GL_ARB_fragment_shader supported?      "<< (support?"yes.":"no."));
00629     count = 1;
00630     if(support){
00631         pprint(" ----------Details----------");
00632         int max=0;
00633         glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS_ARB, (GLint*)&max);
00634         pprint("  Max uniform components." << max);
00635         pprint("");
00636     }
00637 
00638     support = GLEW_ARB_texture_cube_map;
00639     pprint(" GL_ARB_texture_cube_map supported?     "<< (support?"yes.":"no."));
00640     count = 1;
00641     if(support){
00642         pprint(" ----------Details----------");
00643         int size=0;
00644         glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB, (GLint*)&size);
00645         pprint("  Max cubemap size." << size);
00646         pprint("");
00647     }
00648 
00649     support = GLEW_ARB_multitexture;
00650     count = 1;
00651     pprint(" GL_ARB_multitexture supported?         "<< (support?"yes.":"no."));
00652     if(support){
00653         pprint(" ----------Details----------");
00654         int units=0;
00655         glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, (GLint*)&units);
00656         pprint("  Max texture units available.  " << units);
00657         pprint("");
00658     }
00659 
00660     pprint(" GL_ARB_texture_env_combine supported?  "<< (GLEW_ARB_texture_env_combine?"yes.":"no."));
00661     count = 1;
00662 
00663     if(!count)
00664         pprint("No extenstions are used in this build");
00665 
00666     Py_RETURN_NONE;
00667 }
00668 
00669 static PyObject *gLibLoad(PyObject*, PyObject* args, PyObject* kwds)
00670 {
00671     KX_Scene *kx_scene= gp_KetsjiScene;
00672     char *path;
00673     char *group;
00674     Py_buffer py_buffer;
00675     py_buffer.buf = NULL;
00676     char *err_str= NULL;
00677 
00678     short options=0;
00679     int load_actions=0, verbose=0;
00680 
00681     static const char *kwlist[] = {"path", "group", "buffer", "load_actions", "verbose", NULL};
00682     
00683     if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss|y*ii:LibLoad", const_cast<char**>(kwlist),
00684                                     &path, &group, &py_buffer, &load_actions, &verbose))
00685         return NULL;
00686 
00687     /* setup options */
00688     if (load_actions != 0)
00689         options |= KX_BlenderSceneConverter::LIB_LOAD_LOAD_ACTIONS;
00690     if (verbose != 0)
00691         options |= KX_BlenderSceneConverter::LIB_LOAD_VERBOSE;
00692 
00693     if (!py_buffer.buf)
00694     {
00695         char abs_path[FILE_MAX];
00696         // Make the path absolute
00697         BLI_strncpy(abs_path, path, sizeof(abs_path));
00698         BLI_path_abs(abs_path, gp_GamePythonPath);
00699 
00700         if(kx_scene->GetSceneConverter()->LinkBlendFilePath(abs_path, group, kx_scene, &err_str, options)) {
00701             Py_RETURN_TRUE;
00702         }
00703     }
00704     else
00705     {
00706 
00707         if(kx_scene->GetSceneConverter()->LinkBlendFileMemory(py_buffer.buf, py_buffer.len, path, group, kx_scene, &err_str, options))  {
00708             PyBuffer_Release(&py_buffer);
00709             Py_RETURN_TRUE;
00710         }
00711 
00712         PyBuffer_Release(&py_buffer);
00713     }
00714     
00715     if(err_str) {
00716         PyErr_SetString(PyExc_ValueError, err_str);
00717         return NULL;
00718     }
00719     
00720     Py_RETURN_FALSE;
00721 }
00722 
00723 static PyObject *gLibNew(PyObject*, PyObject* args)
00724 {
00725     KX_Scene *kx_scene= gp_KetsjiScene;
00726     char *path;
00727     char *group;
00728     const char *name;
00729     PyObject *names;
00730     int idcode;
00731 
00732     if (!PyArg_ParseTuple(args,"ssO!:LibNew",&path, &group, &PyList_Type, &names))
00733         return NULL;
00734     
00735     if(kx_scene->GetSceneConverter()->GetMainDynamicPath(path))
00736     {
00737         PyErr_SetString(PyExc_KeyError, "the name of the path given exists");
00738         return NULL;
00739     }
00740     
00741     idcode= BKE_idcode_from_name(group);
00742     if(idcode==0) {
00743         PyErr_Format(PyExc_ValueError, "invalid group given \"%s\"", group);
00744         return NULL;
00745     }
00746     
00747     Main *maggie= (Main *)MEM_callocN( sizeof(Main), "BgeMain");
00748     kx_scene->GetSceneConverter()->GetMainDynamic().push_back(maggie);
00749     strncpy(maggie->name, path, sizeof(maggie->name)-1);
00750     
00751     /* Copy the object into main */
00752     if(idcode==ID_ME) {
00753         PyObject *ret= PyList_New(0);
00754         PyObject *item;
00755         for(Py_ssize_t i= 0; i < PyList_GET_SIZE(names); i++) {
00756             name= _PyUnicode_AsString(PyList_GET_ITEM(names, i));
00757             if(name) {
00758                 RAS_MeshObject *meshobj= kx_scene->GetSceneConverter()->ConvertMeshSpecial(kx_scene, maggie, name);
00759                 if(meshobj) {
00760                     KX_MeshProxy* meshproxy = new KX_MeshProxy(meshobj);
00761                     item= meshproxy->NewProxy(true);
00762                     PyList_Append(ret, item);
00763                     Py_DECREF(item);
00764                 }
00765             }
00766             else {
00767                 PyErr_Clear(); /* wasnt a string, ignore for now */
00768             }
00769         }
00770         
00771         return ret;
00772     }
00773     else {
00774         PyErr_Format(PyExc_ValueError, "only \"Mesh\" group currently supported");
00775         return NULL;
00776     }
00777     
00778     Py_RETURN_NONE;
00779 }
00780 
00781 static PyObject *gLibFree(PyObject*, PyObject* args)
00782 {
00783     KX_Scene *kx_scene= gp_KetsjiScene;
00784     char *path;
00785 
00786     if (!PyArg_ParseTuple(args,"s:LibFree",&path))
00787         return NULL;
00788 
00789     if (kx_scene->GetSceneConverter()->FreeBlendFile(path))
00790     {
00791         Py_RETURN_TRUE;
00792     }
00793     else {
00794         Py_RETURN_FALSE;
00795     }
00796 }
00797 
00798 static PyObject *gLibList(PyObject*, PyObject* args)
00799 {
00800     vector<Main*> &dynMaggie = gp_KetsjiScene->GetSceneConverter()->GetMainDynamic();
00801     int i= 0;
00802     PyObject *list= PyList_New(dynMaggie.size());
00803     
00804     for (vector<Main*>::iterator it=dynMaggie.begin(); !(it==dynMaggie.end()); it++)
00805     {
00806         PyList_SET_ITEM(list, i++, PyUnicode_FromString( (*it)->name) );
00807     }
00808     
00809     return list;
00810 }
00811 
00812 static struct PyMethodDef game_methods[] = {
00813     {"expandPath", (PyCFunction)gPyExpandPath, METH_VARARGS, (const char *)gPyExpandPath_doc},
00814     {"startGame", (PyCFunction)gPyStartGame, METH_VARARGS, (const char *)gPyStartGame_doc},
00815     {"endGame", (PyCFunction)gPyEndGame, METH_NOARGS, (const char *)gPyEndGame_doc},
00816     {"restartGame", (PyCFunction)gPyRestartGame, METH_NOARGS, (const char *)gPyRestartGame_doc},
00817     {"saveGlobalDict", (PyCFunction)gPySaveGlobalDict, METH_NOARGS, (const char *)gPySaveGlobalDict_doc},
00818     {"loadGlobalDict", (PyCFunction)gPyLoadGlobalDict, METH_NOARGS, (const char *)gPyLoadGlobalDict_doc},
00819     {"sendMessage", (PyCFunction)gPySendMessage, METH_VARARGS, (const char *)gPySendMessage_doc},
00820     {"getCurrentController", (PyCFunction) SCA_PythonController::sPyGetCurrentController, METH_NOARGS, SCA_PythonController::sPyGetCurrentController__doc__},
00821     {"getCurrentScene", (PyCFunction) gPyGetCurrentScene, METH_NOARGS, gPyGetCurrentScene_doc},
00822     {"getSceneList", (PyCFunction) gPyGetSceneList, METH_NOARGS, (const char *)gPyGetSceneList_doc},
00823     {"addScene", (PyCFunction)gPyAddScene, METH_VARARGS, (const char *)gPyAddScene_doc},
00824     {"getRandomFloat",(PyCFunction) gPyGetRandomFloat, METH_NOARGS, (const char *)gPyGetRandomFloat_doc},
00825     {"setGravity",(PyCFunction) gPySetGravity, METH_O, (const char *)"set Gravitation"},
00826     {"getSpectrum",(PyCFunction) gPyGetSpectrum, METH_NOARGS, (const char *)"get audio spectrum"},
00827     {"getMaxLogicFrame", (PyCFunction) gPyGetMaxLogicFrame, METH_NOARGS, (const char *)"Gets the max number of logic frame per render frame"},
00828     {"setMaxLogicFrame", (PyCFunction) gPySetMaxLogicFrame, METH_VARARGS, (const char *)"Sets the max number of logic frame per render frame"},
00829     {"getMaxPhysicsFrame", (PyCFunction) gPyGetMaxPhysicsFrame, METH_NOARGS, (const char *)"Gets the max number of physics frame per render frame"},
00830     {"setMaxPhysicsFrame", (PyCFunction) gPySetMaxPhysicsFrame, METH_VARARGS, (const char *)"Sets the max number of physics farme per render frame"},
00831     {"getLogicTicRate", (PyCFunction) gPyGetLogicTicRate, METH_NOARGS, (const char *)"Gets the logic tic rate"},
00832     {"setLogicTicRate", (PyCFunction) gPySetLogicTicRate, METH_VARARGS, (const char *)"Sets the logic tic rate"},
00833     {"getPhysicsTicRate", (PyCFunction) gPyGetPhysicsTicRate, METH_NOARGS, (const char *)"Gets the physics tic rate"},
00834     {"setPhysicsTicRate", (PyCFunction) gPySetPhysicsTicRate, METH_VARARGS, (const char *)"Sets the physics tic rate"},
00835     {"getExitKey", (PyCFunction) gPyGetExitKey, METH_NOARGS, (const char *)"Gets the key used to exit the game engine"},
00836     {"setExitKey", (PyCFunction) gPySetExitKey, METH_VARARGS, (const char *)"Sets the key used to exit the game engine"},
00837     {"getAverageFrameRate", (PyCFunction) gPyGetAverageFrameRate, METH_NOARGS, (const char *)"Gets the estimated average frame rate"},
00838     {"getBlendFileList", (PyCFunction)gPyGetBlendFileList, METH_VARARGS, (const char *)"Gets a list of blend files in the same directory as the current blend file"},
00839     {"PrintGLInfo", (PyCFunction)pyPrintExt, METH_NOARGS, (const char *)"Prints GL Extension Info"},
00840     {"PrintMemInfo", (PyCFunction)pyPrintStats, METH_NOARGS, (const char *)"Print engine statistics"},
00841     
00842     /* library functions */
00843     {"LibLoad", (PyCFunction)gLibLoad, METH_VARARGS|METH_KEYWORDS, (const char *)""},
00844     {"LibNew", (PyCFunction)gLibNew, METH_VARARGS, (const char *)""},
00845     {"LibFree", (PyCFunction)gLibFree, METH_VARARGS, (const char *)""},
00846     {"LibList", (PyCFunction)gLibList, METH_VARARGS, (const char *)""},
00847     
00848     {NULL, (PyCFunction) NULL, 0, NULL }
00849 };
00850 
00851 static PyObject* gPyGetWindowHeight(PyObject*, PyObject* args)
00852 {
00853     return PyLong_FromSsize_t((gp_Canvas ? gp_Canvas->GetHeight() : 0));
00854 }
00855 
00856 
00857 
00858 static PyObject* gPyGetWindowWidth(PyObject*, PyObject* args)
00859 {
00860     return PyLong_FromSsize_t((gp_Canvas ? gp_Canvas->GetWidth() : 0));
00861 }
00862 
00863 
00864 
00865 // temporarility visibility thing, will be moved to rasterizer/renderer later
00866 bool gUseVisibilityTemp = false;
00867 
00868 static PyObject* gPyEnableVisibility(PyObject*, PyObject* args)
00869 {
00870     int visible;
00871     if (!PyArg_ParseTuple(args,"i:enableVisibility",&visible))
00872         return NULL;
00873     
00874     gUseVisibilityTemp = (visible != 0);
00875     Py_RETURN_NONE;
00876 }
00877 
00878 
00879 
00880 static PyObject* gPyShowMouse(PyObject*, PyObject* args)
00881 {
00882     int visible;
00883     if (!PyArg_ParseTuple(args,"i:showMouse",&visible))
00884         return NULL;
00885     
00886     if (visible)
00887     {
00888         if (gp_Canvas)
00889             gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
00890     } else
00891     {
00892         if (gp_Canvas)
00893             gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
00894     }
00895     
00896     Py_RETURN_NONE;
00897 }
00898 
00899 
00900 
00901 static PyObject* gPySetMousePosition(PyObject*, PyObject* args)
00902 {
00903     int x,y;
00904     if (!PyArg_ParseTuple(args,"ii:setMousePosition",&x,&y))
00905         return NULL;
00906     
00907     if (gp_Canvas)
00908         gp_Canvas->SetMousePosition(x,y);
00909     
00910     Py_RETURN_NONE;
00911 }
00912 
00913 static PyObject* gPySetEyeSeparation(PyObject*, PyObject* args)
00914 {
00915     float sep;
00916     if (!PyArg_ParseTuple(args, "f:setEyeSeparation", &sep))
00917         return NULL;
00918 
00919     if (!gp_Rasterizer) {
00920         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setEyeSeparation(float), Rasterizer not available");
00921         return NULL;
00922     }
00923     
00924     gp_Rasterizer->SetEyeSeparation(sep);
00925     
00926     Py_RETURN_NONE;
00927 }
00928 
00929 static PyObject* gPyGetEyeSeparation(PyObject*)
00930 {
00931     if (!gp_Rasterizer) {
00932         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.getEyeSeparation(), Rasterizer not available");
00933         return NULL;
00934     }
00935     
00936     return PyFloat_FromDouble(gp_Rasterizer->GetEyeSeparation());
00937 }
00938 
00939 static PyObject* gPySetFocalLength(PyObject*, PyObject* args)
00940 {
00941     float focus;
00942     if (!PyArg_ParseTuple(args, "f:setFocalLength", &focus))
00943         return NULL;
00944     
00945     if (!gp_Rasterizer) {
00946         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setFocalLength(float), Rasterizer not available");
00947         return NULL;
00948     }
00949 
00950     gp_Rasterizer->SetFocalLength(focus);
00951     
00952     Py_RETURN_NONE;
00953 }
00954 
00955 static PyObject* gPyGetFocalLength(PyObject*, PyObject*, PyObject*)
00956 {
00957     if (!gp_Rasterizer) {
00958         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.getFocalLength(), Rasterizer not available");
00959         return NULL;
00960     }
00961     
00962     return PyFloat_FromDouble(gp_Rasterizer->GetFocalLength());
00963     
00964     Py_RETURN_NONE;
00965 }
00966 
00967 static PyObject* gPySetBackgroundColor(PyObject*, PyObject* value)
00968 {
00969     
00970     MT_Vector4 vec;
00971     if (!PyVecTo(value, vec))
00972         return NULL;
00973     
00974     if (gp_Canvas)
00975     {
00976         gp_Rasterizer->SetBackColor((float)vec[0], (float)vec[1], (float)vec[2], (float)vec[3]);
00977     }
00978 
00979     KX_WorldInfo *wi = gp_KetsjiScene->GetWorldInfo();
00980     if (wi->hasWorld())
00981         wi->setBackColor((float)vec[0], (float)vec[1], (float)vec[2]);
00982 
00983     Py_RETURN_NONE;
00984 }
00985 
00986 
00987 
00988 static PyObject* gPySetMistColor(PyObject*, PyObject* value)
00989 {
00990     
00991     MT_Vector3 vec;
00992     if (!PyVecTo(value, vec))
00993         return NULL;
00994     
00995     if (!gp_Rasterizer) {
00996         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setMistColor(color), Rasterizer not available");
00997         return NULL;
00998     }   
00999     gp_Rasterizer->SetFogColor((float)vec[0], (float)vec[1], (float)vec[2]);
01000     
01001     Py_RETURN_NONE;
01002 }
01003 
01004 static PyObject* gPyDisableMist(PyObject*)
01005 {
01006     
01007     if (!gp_Rasterizer) {
01008         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setMistColor(color), Rasterizer not available");
01009         return NULL;
01010     }   
01011     gp_Rasterizer->DisableFog();
01012     
01013     Py_RETURN_NONE;
01014 }
01015 
01016 static PyObject* gPySetMistStart(PyObject*, PyObject* args)
01017 {
01018 
01019     float miststart;
01020     if (!PyArg_ParseTuple(args,"f:setMistStart",&miststart))
01021         return NULL;
01022     
01023     if (!gp_Rasterizer) {
01024         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setMistStart(float), Rasterizer not available");
01025         return NULL;
01026     }
01027     
01028     gp_Rasterizer->SetFogStart(miststart);
01029     
01030     Py_RETURN_NONE;
01031 }
01032 
01033 
01034 
01035 static PyObject* gPySetMistEnd(PyObject*, PyObject* args)
01036 {
01037 
01038     float mistend;
01039     if (!PyArg_ParseTuple(args,"f:setMistEnd",&mistend))
01040         return NULL;
01041     
01042     if (!gp_Rasterizer) {
01043         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setMistEnd(float), Rasterizer not available");
01044         return NULL;
01045     }
01046     
01047     gp_Rasterizer->SetFogEnd(mistend);
01048     
01049     Py_RETURN_NONE;
01050 }
01051 
01052 
01053 static PyObject* gPySetAmbientColor(PyObject*, PyObject* value)
01054 {
01055     
01056     MT_Vector3 vec;
01057     if (!PyVecTo(value, vec))
01058         return NULL;
01059     
01060     if (!gp_Rasterizer) {
01061         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setAmbientColor(color), Rasterizer not available");
01062         return NULL;
01063     }   
01064     gp_Rasterizer->SetAmbientColor((float)vec[0], (float)vec[1], (float)vec[2]);
01065     
01066     Py_RETURN_NONE;
01067 }
01068 
01069 
01070 
01071 
01072 static PyObject* gPyMakeScreenshot(PyObject*, PyObject* args)
01073 {
01074     char* filename;
01075     if (!PyArg_ParseTuple(args,"s:makeScreenshot",&filename))
01076         return NULL;
01077     
01078     if (gp_Canvas)
01079     {
01080         gp_Canvas->MakeScreenShot(filename);
01081     }
01082     
01083     Py_RETURN_NONE;
01084 }
01085 
01086 static PyObject* gPyEnableMotionBlur(PyObject*, PyObject* args)
01087 {
01088     float motionblurvalue;
01089     if (!PyArg_ParseTuple(args,"f:enableMotionBlur",&motionblurvalue))
01090         return NULL;
01091     
01092     if (!gp_Rasterizer) {
01093         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.enableMotionBlur(float), Rasterizer not available");
01094         return NULL;
01095     }
01096     
01097     gp_Rasterizer->EnableMotionBlur(motionblurvalue);
01098     
01099     Py_RETURN_NONE;
01100 }
01101 
01102 static PyObject* gPyDisableMotionBlur(PyObject*)
01103 {
01104     if (!gp_Rasterizer) {
01105         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.disableMotionBlur(), Rasterizer not available");
01106         return NULL;
01107     }
01108     
01109     gp_Rasterizer->DisableMotionBlur();
01110     
01111     Py_RETURN_NONE;
01112 }
01113 
01114 static int getGLSLSettingFlag(const char *setting)
01115 {
01116     if(strcmp(setting, "lights") == 0)
01117         return GAME_GLSL_NO_LIGHTS;
01118     else if(strcmp(setting, "shaders") == 0)
01119         return GAME_GLSL_NO_SHADERS;
01120     else if(strcmp(setting, "shadows") == 0)
01121         return GAME_GLSL_NO_SHADOWS;
01122     else if(strcmp(setting, "ramps") == 0)
01123         return GAME_GLSL_NO_RAMPS;
01124     else if(strcmp(setting, "nodes") == 0)
01125         return GAME_GLSL_NO_NODES;
01126     else if(strcmp(setting, "extra_textures") == 0)
01127         return GAME_GLSL_NO_EXTRA_TEX;
01128     else
01129         return -1;
01130 }
01131 
01132 static PyObject* gPySetGLSLMaterialSetting(PyObject*,
01133                                             PyObject* args,
01134                                             PyObject*)
01135 {
01136     GlobalSettings *gs= gp_KetsjiEngine->GetGlobalSettings();
01137     char *setting;
01138     int enable, flag, sceneflag;
01139 
01140     if (!PyArg_ParseTuple(args,"si:setGLSLMaterialSetting",&setting,&enable))
01141         return NULL;
01142     
01143     flag = getGLSLSettingFlag(setting);
01144     
01145     if  (flag==-1) {
01146         PyErr_SetString(PyExc_ValueError, "Rasterizer.setGLSLMaterialSetting(string): glsl setting is not known");
01147         return NULL;
01148     }
01149 
01150     sceneflag= gs->glslflag;
01151     
01152     if (enable)
01153         gs->glslflag &= ~flag;
01154     else
01155         gs->glslflag |= flag;
01156 
01157     /* display lists and GLSL materials need to be remade */
01158     if(sceneflag != gs->glslflag) {
01159         GPU_materials_free();
01160         if(gp_KetsjiEngine) {
01161             KX_SceneList *scenes = gp_KetsjiEngine->CurrentScenes();
01162             KX_SceneList::iterator it;
01163 
01164             for(it=scenes->begin(); it!=scenes->end(); it++)
01165                 if((*it)->GetBucketManager()) {
01166                     (*it)->GetBucketManager()->ReleaseDisplayLists();
01167                     (*it)->GetBucketManager()->ReleaseMaterials();
01168                 }
01169         }
01170     }
01171 
01172     Py_RETURN_NONE;
01173 }
01174 
01175 static PyObject* gPyGetGLSLMaterialSetting(PyObject*, 
01176                                      PyObject* args, 
01177                                      PyObject*)
01178 {
01179     GlobalSettings *gs= gp_KetsjiEngine->GetGlobalSettings();
01180     char *setting;
01181     int enabled = 0, flag;
01182 
01183     if (!PyArg_ParseTuple(args,"s:getGLSLMaterialSetting",&setting))
01184         return NULL;
01185     
01186     flag = getGLSLSettingFlag(setting);
01187     
01188     if  (flag==-1) {
01189         PyErr_SetString(PyExc_ValueError, "Rasterizer.getGLSLMaterialSetting(string): glsl setting is not known");
01190         return NULL;
01191     }
01192 
01193     enabled = ((gs->glslflag & flag) != 0);
01194     return PyLong_FromSsize_t(enabled);
01195 }
01196 
01197 #define KX_TEXFACE_MATERIAL             0
01198 #define KX_BLENDER_MULTITEX_MATERIAL    1
01199 #define KX_BLENDER_GLSL_MATERIAL        2
01200 
01201 static PyObject* gPySetMaterialType(PyObject*,
01202                                     PyObject* args,
01203                                     PyObject*)
01204 {
01205     GlobalSettings *gs= gp_KetsjiEngine->GetGlobalSettings();
01206     int type;
01207 
01208     if (!PyArg_ParseTuple(args,"i:setMaterialType",&type))
01209         return NULL;
01210 
01211     if(type == KX_BLENDER_GLSL_MATERIAL)
01212         gs->matmode= GAME_MAT_GLSL;
01213     else if(type == KX_BLENDER_MULTITEX_MATERIAL)
01214         gs->matmode= GAME_MAT_MULTITEX;
01215     else if(type == KX_TEXFACE_MATERIAL)
01216         gs->matmode= GAME_MAT_TEXFACE;
01217     else {
01218         PyErr_SetString(PyExc_ValueError, "Rasterizer.setMaterialType(int): material type is not known");
01219         return NULL;
01220     }
01221 
01222     Py_RETURN_NONE;
01223 }
01224 
01225 static PyObject* gPyGetMaterialType(PyObject*)
01226 {
01227     GlobalSettings *gs= gp_KetsjiEngine->GetGlobalSettings();
01228     int flag;
01229 
01230     if(gs->matmode == GAME_MAT_GLSL)
01231         flag = KX_BLENDER_GLSL_MATERIAL;
01232     else if(gs->matmode == GAME_MAT_MULTITEX)
01233         flag = KX_BLENDER_MULTITEX_MATERIAL;
01234     else
01235         flag = KX_TEXFACE_MATERIAL;
01236     
01237     return PyLong_FromSsize_t(flag);
01238 }
01239 
01240 static PyObject* gPySetAnisotropicFiltering(PyObject*, PyObject* args)
01241 {
01242     short level;
01243 
01244     if (!PyArg_ParseTuple(args, "h:setAnisotropicFiltering", &level))
01245         return NULL;
01246 
01247     if (level != 1 && level != 2 && level != 4 && level != 8 && level != 16) {
01248         PyErr_SetString(PyExc_ValueError, "Rasterizer.setAnisotropicFiltering(level): Expected value of 1, 2, 4, 8, or 16 for value");
01249         return NULL;
01250     }
01251 
01252     gp_Rasterizer->SetAnisotropicFiltering(level);
01253 
01254     Py_RETURN_NONE;
01255 }
01256 
01257 static PyObject* gPyGetAnisotropicFiltering(PyObject*, PyObject* args)
01258 {
01259     return PyLong_FromLong(gp_Rasterizer->GetAnisotropicFiltering());
01260 }
01261 
01262 static PyObject* gPyDrawLine(PyObject*, PyObject* args)
01263 {
01264     PyObject* ob_from;
01265     PyObject* ob_to;
01266     PyObject* ob_color;
01267 
01268     if (!gp_Rasterizer) {
01269         PyErr_SetString(PyExc_RuntimeError, "Rasterizer.drawLine(obFrom, obTo, color): Rasterizer not available");
01270         return NULL;
01271     }
01272 
01273     if (!PyArg_ParseTuple(args,"OOO:drawLine",&ob_from,&ob_to,&ob_color))
01274         return NULL;
01275 
01276     MT_Vector3 from;
01277     MT_Vector3 to;
01278     MT_Vector3 color;
01279     if (!PyVecTo(ob_from, from))
01280         return NULL;
01281     if (!PyVecTo(ob_to, to))
01282         return NULL;
01283     if (!PyVecTo(ob_color, color))
01284         return NULL;
01285 
01286     gp_Rasterizer->DrawDebugLine(from,to,color);
01287     
01288     Py_RETURN_NONE;
01289 }
01290 
01291 static struct PyMethodDef rasterizer_methods[] = {
01292   {"getWindowWidth",(PyCFunction) gPyGetWindowWidth,
01293    METH_VARARGS, "getWindowWidth doc"},
01294    {"getWindowHeight",(PyCFunction) gPyGetWindowHeight,
01295    METH_VARARGS, "getWindowHeight doc"},
01296   {"makeScreenshot",(PyCFunction)gPyMakeScreenshot,
01297     METH_VARARGS, "make Screenshot doc"},
01298    {"enableVisibility",(PyCFunction) gPyEnableVisibility,
01299    METH_VARARGS, "enableVisibility doc"},
01300     {"showMouse",(PyCFunction) gPyShowMouse,
01301    METH_VARARGS, "showMouse(bool visible)"},
01302    {"setMousePosition",(PyCFunction) gPySetMousePosition,
01303    METH_VARARGS, "setMousePosition(int x,int y)"},
01304   {"setBackgroundColor",(PyCFunction)gPySetBackgroundColor,METH_O,"set Background Color (rgb)"},
01305     {"setAmbientColor",(PyCFunction)gPySetAmbientColor,METH_O,"set Ambient Color (rgb)"},
01306  {"disableMist",(PyCFunction)gPyDisableMist,METH_NOARGS,"turn off mist"},
01307  {"setMistColor",(PyCFunction)gPySetMistColor,METH_O,"set Mist Color (rgb)"},
01308   {"setMistStart",(PyCFunction)gPySetMistStart,METH_VARARGS,"set Mist Start(rgb)"},
01309   {"setMistEnd",(PyCFunction)gPySetMistEnd,METH_VARARGS,"set Mist End(rgb)"},
01310   {"enableMotionBlur",(PyCFunction)gPyEnableMotionBlur,METH_VARARGS,"enable motion blur"},
01311   {"disableMotionBlur",(PyCFunction)gPyDisableMotionBlur,METH_NOARGS,"disable motion blur"},
01312 
01313   
01314   {"setEyeSeparation", (PyCFunction) gPySetEyeSeparation, METH_VARARGS, "set the eye separation for stereo mode"},
01315   {"getEyeSeparation", (PyCFunction) gPyGetEyeSeparation, METH_NOARGS, "get the eye separation for stereo mode"},
01316   {"setFocalLength", (PyCFunction) gPySetFocalLength, METH_VARARGS, "set the focal length for stereo mode"},
01317   {"getFocalLength", (PyCFunction) gPyGetFocalLength, METH_VARARGS, "get the focal length for stereo mode"},
01318   {"setMaterialMode",(PyCFunction) gPySetMaterialType,
01319    METH_VARARGS, "set the material mode to use for OpenGL rendering"},
01320   {"getMaterialMode",(PyCFunction) gPyGetMaterialType,
01321    METH_NOARGS, "get the material mode being used for OpenGL rendering"},
01322   {"setGLSLMaterialSetting",(PyCFunction) gPySetGLSLMaterialSetting,
01323    METH_VARARGS, "set the state of a GLSL material setting"},
01324   {"getGLSLMaterialSetting",(PyCFunction) gPyGetGLSLMaterialSetting,
01325    METH_VARARGS, "get the state of a GLSL material setting"},
01326   {"setAnisotropicFiltering", (PyCFunction) gPySetAnisotropicFiltering,
01327   METH_VARARGS, "set the anisotropic filtering level (must be one of 1, 2, 4, 8, 16)"},
01328   {"getAnisotropicFiltering", (PyCFunction) gPyGetAnisotropicFiltering,
01329   METH_VARARGS, "get the anisotropic filtering level"},
01330   {"drawLine", (PyCFunction) gPyDrawLine,
01331    METH_VARARGS, "draw a line on the screen"},
01332   { NULL, (PyCFunction) NULL, 0, NULL }
01333 };
01334 
01335 // Initialization function for the module (*must* be called initGameLogic)
01336 
01337 static char GameLogic_module_documentation[] =
01338 "This is the Python API for the game engine of bge.logic"
01339 ;
01340 
01341 static char Rasterizer_module_documentation[] =
01342 "This is the Python API for the game engine of Rasterizer"
01343 ;
01344 
01345 static struct PyModuleDef GameLogic_module_def = {
01346     {}, /* m_base */
01347     "GameLogic",  /* m_name */
01348     GameLogic_module_documentation,  /* m_doc */
01349     0,  /* m_size */
01350     game_methods,  /* m_methods */
01351     0,  /* m_reload */
01352     0,  /* m_traverse */
01353     0,  /* m_clear */
01354     0,  /* m_free */
01355 };
01356 
01357 PyObject* initGameLogic(KX_KetsjiEngine *engine, KX_Scene* scene) // quick hack to get gravity hook
01358 {
01359     PyObject* m;
01360     PyObject* d;
01361     PyObject* item; /* temp PyObject* storage */
01362     
01363     gp_KetsjiEngine = engine;
01364     gp_KetsjiScene = scene;
01365 
01366     gUseVisibilityTemp=false;
01367     
01368     PyObjectPlus::ClearDeprecationWarning(); /* Not that nice to call here but makes sure warnings are reset between loading scenes */
01369     
01370     /* Use existing module where possible
01371      * be careful not to init any runtime vars after this */
01372     m = PyImport_ImportModule( "GameLogic" );
01373     if(m) {
01374         Py_DECREF(m);
01375         return m;
01376     }
01377     else {
01378         PyErr_Clear();
01379         // Create the module and add the functions  
01380         m = PyModule_Create(&GameLogic_module_def);
01381         PyDict_SetItemString(PySys_GetObject("modules"), GameLogic_module_def.m_name, m);
01382     }
01383     
01384     // Add some symbolic constants to the module
01385     d = PyModule_GetDict(m);
01386     
01387     // can be overwritten later for gameEngine instances that can load new blend files and re-initialize this module
01388     // for now its safe to make sure it exists for other areas such as the web plugin
01389     
01390     PyDict_SetItemString(d, "globalDict", item=PyDict_New()); Py_DECREF(item);
01391 
01392     // Add keyboard and mouse attributes to this module
01393     MT_assert(!gp_PythonKeyboard);
01394     gp_PythonKeyboard = new SCA_PythonKeyboard(gp_KetsjiEngine->GetKeyboardDevice());
01395     PyDict_SetItemString(d, "keyboard", gp_PythonKeyboard->NewProxy(true));
01396 
01397     MT_assert(!gp_PythonMouse);
01398     gp_PythonMouse = new SCA_PythonMouse(gp_KetsjiEngine->GetMouseDevice(), gp_Canvas);
01399     PyDict_SetItemString(d, "mouse", gp_PythonMouse->NewProxy(true));
01400 
01401     ErrorObject = PyUnicode_FromString("GameLogic.error");
01402     PyDict_SetItemString(d, "error", ErrorObject);
01403     Py_DECREF(ErrorObject);
01404     
01405     // XXXX Add constants here
01406     /* To use logic bricks, we need some sort of constants. Here, we associate */
01407     /* constants and sumbolic names. Add them to dictionary d.                 */
01408 
01409     /* 1. true and false: needed for everyone                                  */
01410     KX_MACRO_addTypesToDict(d, KX_TRUE,  SCA_ILogicBrick::KX_TRUE);
01411     KX_MACRO_addTypesToDict(d, KX_FALSE, SCA_ILogicBrick::KX_FALSE);
01412 
01413     /* 2. Property sensor                                                      */
01414     KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_EQUAL,      SCA_PropertySensor::KX_PROPSENSOR_EQUAL);
01415     KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_NOTEQUAL,   SCA_PropertySensor::KX_PROPSENSOR_NOTEQUAL);
01416     KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_INTERVAL,   SCA_PropertySensor::KX_PROPSENSOR_INTERVAL);
01417     KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_CHANGED,    SCA_PropertySensor::KX_PROPSENSOR_CHANGED);
01418     KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_EXPRESSION, SCA_PropertySensor::KX_PROPSENSOR_EXPRESSION);
01419 
01420     /* 3. Constraint actuator                                                  */
01421     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCX);
01422     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCY);
01423     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCZ);
01424     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTX);
01425     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTY);
01426     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTZ);
01427     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPX);
01428     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPY);
01429     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPZ);
01430     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNX);
01431     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNY);
01432     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNZ);
01433     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIX);
01434     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIY);
01435     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIZ);
01436     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHPX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHPX);
01437     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHPY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHPY);
01438     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHPZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHPZ);
01439     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHNX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHNX);
01440     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHNY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHNY);
01441     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHNZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHNZ);
01442     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_NORMAL, KX_ConstraintActuator::KX_ACT_CONSTRAINT_NORMAL);
01443     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_MATERIAL, KX_ConstraintActuator::KX_ACT_CONSTRAINT_MATERIAL);
01444     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_PERMANENT, KX_ConstraintActuator::KX_ACT_CONSTRAINT_PERMANENT);
01445     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DISTANCE, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DISTANCE);
01446     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCAL, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCAL);
01447     KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DOROTFH, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DOROTFH);
01448 
01449     /* 4. Ipo actuator, simple part                                            */
01450     KX_MACRO_addTypesToDict(d, KX_IPOACT_PLAY,     KX_IpoActuator::KX_ACT_IPO_PLAY);
01451     KX_MACRO_addTypesToDict(d, KX_IPOACT_PINGPONG, KX_IpoActuator::KX_ACT_IPO_PINGPONG);
01452     KX_MACRO_addTypesToDict(d, KX_IPOACT_FLIPPER,  KX_IpoActuator::KX_ACT_IPO_FLIPPER);
01453     KX_MACRO_addTypesToDict(d, KX_IPOACT_LOOPSTOP, KX_IpoActuator::KX_ACT_IPO_LOOPSTOP);
01454     KX_MACRO_addTypesToDict(d, KX_IPOACT_LOOPEND,  KX_IpoActuator::KX_ACT_IPO_LOOPEND);
01455     KX_MACRO_addTypesToDict(d, KX_IPOACT_FROM_PROP,KX_IpoActuator::KX_ACT_IPO_FROM_PROP);
01456 
01457     /* 5. Random distribution types                                            */
01458     KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_CONST,      SCA_RandomActuator::KX_RANDOMACT_BOOL_CONST);
01459     KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_UNIFORM,    SCA_RandomActuator::KX_RANDOMACT_BOOL_UNIFORM);
01460     KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_BERNOUILLI, SCA_RandomActuator::KX_RANDOMACT_BOOL_BERNOUILLI);
01461     KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_CONST,       SCA_RandomActuator::KX_RANDOMACT_INT_CONST);
01462     KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_UNIFORM,     SCA_RandomActuator::KX_RANDOMACT_INT_UNIFORM);
01463     KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_POISSON,     SCA_RandomActuator::KX_RANDOMACT_INT_POISSON);
01464     KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_CONST,     SCA_RandomActuator::KX_RANDOMACT_FLOAT_CONST);
01465     KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_UNIFORM,   SCA_RandomActuator::KX_RANDOMACT_FLOAT_UNIFORM);
01466     KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NORMAL,    SCA_RandomActuator::KX_RANDOMACT_FLOAT_NORMAL);
01467     KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL, SCA_RandomActuator::KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL);
01468 
01469     /* 6. Sound actuator                                                      */
01470     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYSTOP,              KX_SoundActuator::KX_SOUNDACT_PLAYSTOP);
01471     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYEND,               KX_SoundActuator::KX_SOUNDACT_PLAYEND);
01472     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPSTOP,              KX_SoundActuator::KX_SOUNDACT_LOOPSTOP);
01473     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPEND,               KX_SoundActuator::KX_SOUNDACT_LOOPEND);
01474     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL,     KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL);
01475     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP,     KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP);
01476 
01477     /* 7. Action actuator                                                      */
01478     KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PLAY,        ACT_ACTION_PLAY);
01479     KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PINGPONG,    ACT_ACTION_PINGPONG);
01480     KX_MACRO_addTypesToDict(d, KX_ACTIONACT_FLIPPER,     ACT_ACTION_FLIPPER);
01481     KX_MACRO_addTypesToDict(d, KX_ACTIONACT_LOOPSTOP,    ACT_ACTION_LOOP_STOP);
01482     KX_MACRO_addTypesToDict(d, KX_ACTIONACT_LOOPEND,     ACT_ACTION_LOOP_END);
01483     KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PROPERTY,    ACT_ACTION_FROM_PROP);
01484     
01485     /*8. GL_BlendFunc */
01486     KX_MACRO_addTypesToDict(d, BL_ZERO, GL_ZERO);
01487     KX_MACRO_addTypesToDict(d, BL_ONE, GL_ONE);
01488     KX_MACRO_addTypesToDict(d, BL_SRC_COLOR, GL_SRC_COLOR);
01489     KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
01490     KX_MACRO_addTypesToDict(d, BL_DST_COLOR, GL_DST_COLOR);
01491     KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_DST_COLOR);
01492     KX_MACRO_addTypesToDict(d, BL_SRC_ALPHA, GL_SRC_ALPHA);
01493     KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
01494     KX_MACRO_addTypesToDict(d, BL_DST_ALPHA, GL_DST_ALPHA);
01495     KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
01496     KX_MACRO_addTypesToDict(d, BL_SRC_ALPHA_SATURATE, GL_SRC_ALPHA_SATURATE);
01497 
01498 
01499     /* 9. UniformTypes */
01500     KX_MACRO_addTypesToDict(d, SHD_TANGENT, BL_Shader::SHD_TANGENT);
01501     KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX, BL_Shader::MODELVIEWMATRIX);
01502     KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_TRANSPOSE, BL_Shader::MODELVIEWMATRIX_TRANSPOSE);
01503     KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_INVERSE, BL_Shader::MODELVIEWMATRIX_INVERSE);
01504     KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_INVERSETRANSPOSE, BL_Shader::MODELVIEWMATRIX_INVERSETRANSPOSE);
01505     KX_MACRO_addTypesToDict(d, MODELMATRIX, BL_Shader::MODELMATRIX);
01506     KX_MACRO_addTypesToDict(d, MODELMATRIX_TRANSPOSE, BL_Shader::MODELMATRIX_TRANSPOSE);
01507     KX_MACRO_addTypesToDict(d, MODELMATRIX_INVERSE, BL_Shader::MODELMATRIX_INVERSE);
01508     KX_MACRO_addTypesToDict(d, MODELMATRIX_INVERSETRANSPOSE, BL_Shader::MODELMATRIX_INVERSETRANSPOSE);
01509     KX_MACRO_addTypesToDict(d, VIEWMATRIX, BL_Shader::VIEWMATRIX);
01510     KX_MACRO_addTypesToDict(d, VIEWMATRIX_TRANSPOSE, BL_Shader::VIEWMATRIX_TRANSPOSE);
01511     KX_MACRO_addTypesToDict(d, VIEWMATRIX_INVERSE, BL_Shader::VIEWMATRIX_INVERSE);
01512     KX_MACRO_addTypesToDict(d, VIEWMATRIX_INVERSETRANSPOSE, BL_Shader::VIEWMATRIX_INVERSETRANSPOSE);
01513     KX_MACRO_addTypesToDict(d, CAM_POS, BL_Shader::CAM_POS);
01514     KX_MACRO_addTypesToDict(d, CONSTANT_TIMER, BL_Shader::CONSTANT_TIMER);
01515 
01516     /* 10 state actuator */
01517     KX_MACRO_addTypesToDict(d, KX_STATE1, (1<<0));
01518     KX_MACRO_addTypesToDict(d, KX_STATE2, (1<<1));
01519     KX_MACRO_addTypesToDict(d, KX_STATE3, (1<<2));
01520     KX_MACRO_addTypesToDict(d, KX_STATE4, (1<<3));
01521     KX_MACRO_addTypesToDict(d, KX_STATE5, (1<<4));
01522     KX_MACRO_addTypesToDict(d, KX_STATE6, (1<<5));
01523     KX_MACRO_addTypesToDict(d, KX_STATE7, (1<<6));
01524     KX_MACRO_addTypesToDict(d, KX_STATE8, (1<<7));
01525     KX_MACRO_addTypesToDict(d, KX_STATE9, (1<<8));
01526     KX_MACRO_addTypesToDict(d, KX_STATE10, (1<<9));
01527     KX_MACRO_addTypesToDict(d, KX_STATE11, (1<<10));
01528     KX_MACRO_addTypesToDict(d, KX_STATE12, (1<<11));
01529     KX_MACRO_addTypesToDict(d, KX_STATE13, (1<<12));
01530     KX_MACRO_addTypesToDict(d, KX_STATE14, (1<<13));
01531     KX_MACRO_addTypesToDict(d, KX_STATE15, (1<<14));
01532     KX_MACRO_addTypesToDict(d, KX_STATE16, (1<<15));
01533     KX_MACRO_addTypesToDict(d, KX_STATE17, (1<<16));
01534     KX_MACRO_addTypesToDict(d, KX_STATE18, (1<<17));
01535     KX_MACRO_addTypesToDict(d, KX_STATE19, (1<<18));
01536     KX_MACRO_addTypesToDict(d, KX_STATE20, (1<<19));
01537     KX_MACRO_addTypesToDict(d, KX_STATE21, (1<<20));
01538     KX_MACRO_addTypesToDict(d, KX_STATE22, (1<<21));
01539     KX_MACRO_addTypesToDict(d, KX_STATE23, (1<<22));
01540     KX_MACRO_addTypesToDict(d, KX_STATE24, (1<<23));
01541     KX_MACRO_addTypesToDict(d, KX_STATE25, (1<<24));
01542     KX_MACRO_addTypesToDict(d, KX_STATE26, (1<<25));
01543     KX_MACRO_addTypesToDict(d, KX_STATE27, (1<<26));
01544     KX_MACRO_addTypesToDict(d, KX_STATE28, (1<<27));
01545     KX_MACRO_addTypesToDict(d, KX_STATE29, (1<<28));
01546     KX_MACRO_addTypesToDict(d, KX_STATE30, (1<<29));
01547     
01548     /* All Sensors */
01549     KX_MACRO_addTypesToDict(d, KX_SENSOR_JUST_ACTIVATED, SCA_ISensor::KX_SENSOR_JUST_ACTIVATED);
01550     KX_MACRO_addTypesToDict(d, KX_SENSOR_ACTIVE, SCA_ISensor::KX_SENSOR_ACTIVE);
01551     KX_MACRO_addTypesToDict(d, KX_SENSOR_JUST_DEACTIVATED, SCA_ISensor::KX_SENSOR_JUST_DEACTIVATED);
01552     KX_MACRO_addTypesToDict(d, KX_SENSOR_INACTIVE, SCA_ISensor::KX_SENSOR_INACTIVE);
01553     
01554     /* Radar Sensor */
01555     KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_POS_X, KX_RadarSensor::KX_RADAR_AXIS_POS_X);
01556     KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_POS_Y, KX_RadarSensor::KX_RADAR_AXIS_POS_Y);
01557     KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_POS_Z, KX_RadarSensor::KX_RADAR_AXIS_POS_Z);
01558     KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_NEG_X, KX_RadarSensor::KX_RADAR_AXIS_NEG_Y);
01559     KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_NEG_Y, KX_RadarSensor::KX_RADAR_AXIS_NEG_X);
01560     KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_NEG_Z, KX_RadarSensor::KX_RADAR_AXIS_NEG_Z);
01561 
01562     /* Ray Sensor */
01563     KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_POS_X, KX_RaySensor::KX_RAY_AXIS_POS_X);
01564     KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_POS_Y, KX_RaySensor::KX_RAY_AXIS_POS_Y);
01565     KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_POS_Z, KX_RaySensor::KX_RAY_AXIS_POS_Z);
01566     KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_NEG_X, KX_RaySensor::KX_RAY_AXIS_NEG_Y);
01567     KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_NEG_Y, KX_RaySensor::KX_RAY_AXIS_NEG_X);
01568     KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_NEG_Z, KX_RaySensor::KX_RAY_AXIS_NEG_Z);
01569 
01570     /* Dynamic actuator */
01571     KX_MACRO_addTypesToDict(d, KX_DYN_RESTORE_DYNAMICS, KX_SCA_DynamicActuator::KX_DYN_RESTORE_DYNAMICS);
01572     KX_MACRO_addTypesToDict(d, KX_DYN_DISABLE_DYNAMICS, KX_SCA_DynamicActuator::KX_DYN_DISABLE_DYNAMICS);
01573     KX_MACRO_addTypesToDict(d, KX_DYN_ENABLE_RIGID_BODY, KX_SCA_DynamicActuator::KX_DYN_ENABLE_RIGID_BODY);
01574     KX_MACRO_addTypesToDict(d, KX_DYN_DISABLE_RIGID_BODY, KX_SCA_DynamicActuator::KX_DYN_DISABLE_RIGID_BODY);
01575     KX_MACRO_addTypesToDict(d, KX_DYN_SET_MASS, KX_SCA_DynamicActuator::KX_DYN_SET_MASS);
01576 
01577     /* Input & Mouse Sensor */
01578     KX_MACRO_addTypesToDict(d, KX_INPUT_NONE, SCA_InputEvent::KX_NO_INPUTSTATUS);
01579     KX_MACRO_addTypesToDict(d, KX_INPUT_JUST_ACTIVATED, SCA_InputEvent::KX_JUSTACTIVATED);
01580     KX_MACRO_addTypesToDict(d, KX_INPUT_ACTIVE, SCA_InputEvent::KX_ACTIVE);
01581     KX_MACRO_addTypesToDict(d, KX_INPUT_JUST_RELEASED, SCA_InputEvent::KX_JUSTRELEASED);
01582     
01583     KX_MACRO_addTypesToDict(d, KX_MOUSE_BUT_LEFT, SCA_IInputDevice::KX_LEFTMOUSE);
01584     KX_MACRO_addTypesToDict(d, KX_MOUSE_BUT_MIDDLE, SCA_IInputDevice::KX_MIDDLEMOUSE);
01585     KX_MACRO_addTypesToDict(d, KX_MOUSE_BUT_RIGHT, SCA_IInputDevice::KX_RIGHTMOUSE);
01586 
01587     /* 2D Filter Actuator */
01588     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_ENABLED, RAS_2DFilterManager::RAS_2DFILTER_ENABLED);
01589     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_DISABLED, RAS_2DFilterManager::RAS_2DFILTER_DISABLED);
01590     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_NOFILTER, RAS_2DFilterManager::RAS_2DFILTER_NOFILTER);
01591     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_MOTIONBLUR, RAS_2DFilterManager::RAS_2DFILTER_MOTIONBLUR);
01592     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_BLUR, RAS_2DFilterManager::RAS_2DFILTER_BLUR);
01593     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_SHARPEN, RAS_2DFilterManager::RAS_2DFILTER_SHARPEN);
01594     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_DILATION, RAS_2DFilterManager::RAS_2DFILTER_DILATION);
01595     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_EROSION, RAS_2DFilterManager::RAS_2DFILTER_EROSION);
01596     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_LAPLACIAN, RAS_2DFilterManager::RAS_2DFILTER_LAPLACIAN);
01597     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_SOBEL, RAS_2DFilterManager::RAS_2DFILTER_SOBEL);
01598     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_PREWITT, RAS_2DFilterManager::RAS_2DFILTER_PREWITT);
01599     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_GRAYSCALE, RAS_2DFilterManager::RAS_2DFILTER_GRAYSCALE);
01600     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_SEPIA, RAS_2DFilterManager::RAS_2DFILTER_SEPIA);
01601     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_INVERT, RAS_2DFilterManager::RAS_2DFILTER_INVERT);
01602     KX_MACRO_addTypesToDict(d, RAS_2DFILTER_CUSTOMFILTER, RAS_2DFilterManager::RAS_2DFILTER_CUSTOMFILTER);
01603 
01604     /* Sound Actuator */
01605     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYSTOP, KX_SoundActuator::KX_SOUNDACT_PLAYSTOP);
01606     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYEND, KX_SoundActuator::KX_SOUNDACT_PLAYEND);
01607     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPSTOP, KX_SoundActuator::KX_SOUNDACT_LOOPSTOP);
01608     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPEND, KX_SoundActuator:: KX_SOUNDACT_LOOPEND);
01609     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL, KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL);
01610     KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP, KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP);
01611 
01612     /* State Actuator */
01613     KX_MACRO_addTypesToDict(d, KX_STATE_OP_CPY, KX_StateActuator::OP_CPY);
01614     KX_MACRO_addTypesToDict(d, KX_STATE_OP_SET, KX_StateActuator::OP_SET);
01615     KX_MACRO_addTypesToDict(d, KX_STATE_OP_CLR, KX_StateActuator::OP_CLR);
01616     KX_MACRO_addTypesToDict(d, KX_STATE_OP_NEG, KX_StateActuator::OP_NEG);
01617 
01618     /* Game Actuator Modes */
01619     KX_MACRO_addTypesToDict(d, KX_GAME_LOAD, KX_GameActuator::KX_GAME_LOAD);
01620     KX_MACRO_addTypesToDict(d, KX_GAME_START, KX_GameActuator::KX_GAME_START);
01621     KX_MACRO_addTypesToDict(d, KX_GAME_RESTART, KX_GameActuator::KX_GAME_RESTART);
01622     KX_MACRO_addTypesToDict(d, KX_GAME_QUIT, KX_GameActuator::KX_GAME_QUIT);
01623     KX_MACRO_addTypesToDict(d, KX_GAME_SAVECFG, KX_GameActuator::KX_GAME_SAVECFG);
01624     KX_MACRO_addTypesToDict(d, KX_GAME_LOADCFG, KX_GameActuator::KX_GAME_LOADCFG);
01625 
01626     /* Scene Actuator Modes */
01627     KX_MACRO_addTypesToDict(d, KX_SCENE_RESTART, KX_SceneActuator::KX_SCENE_RESTART);
01628     KX_MACRO_addTypesToDict(d, KX_SCENE_SET_SCENE, KX_SceneActuator::KX_SCENE_SET_SCENE);
01629     KX_MACRO_addTypesToDict(d, KX_SCENE_SET_CAMERA, KX_SceneActuator::KX_SCENE_SET_CAMERA);
01630     KX_MACRO_addTypesToDict(d, KX_SCENE_ADD_FRONT_SCENE, KX_SceneActuator::KX_SCENE_ADD_FRONT_SCENE);
01631     KX_MACRO_addTypesToDict(d, KX_SCENE_ADD_BACK_SCENE, KX_SceneActuator::KX_SCENE_ADD_BACK_SCENE);
01632     KX_MACRO_addTypesToDict(d, KX_SCENE_REMOVE_SCENE, KX_SceneActuator::KX_SCENE_REMOVE_SCENE);
01633     KX_MACRO_addTypesToDict(d, KX_SCENE_SUSPEND, KX_SceneActuator::KX_SCENE_SUSPEND);
01634     KX_MACRO_addTypesToDict(d, KX_SCENE_RESUME, KX_SceneActuator::KX_SCENE_RESUME);
01635 
01636     /* Parent Actuator Modes */
01637     KX_MACRO_addTypesToDict(d, KX_PARENT_SET, KX_ParentActuator::KX_PARENT_SET);
01638     KX_MACRO_addTypesToDict(d, KX_PARENT_REMOVE, KX_ParentActuator::KX_PARENT_REMOVE);
01639 
01640     /* BL_ArmatureConstraint type */
01641     KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_TRACKTO);
01642     KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_KINEMATIC, CONSTRAINT_TYPE_KINEMATIC);
01643     KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_ROTLIKE, CONSTRAINT_TYPE_ROTLIKE);
01644     KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_LOCLIKE, CONSTRAINT_TYPE_LOCLIKE);
01645     KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_MINMAX, CONSTRAINT_TYPE_MINMAX);
01646     KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_SIZELIKE, CONSTRAINT_TYPE_SIZELIKE);
01647     KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_LOCKTRACK);
01648     KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_STRETCHTO, CONSTRAINT_TYPE_STRETCHTO);
01649     KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_CLAMPTO, CONSTRAINT_TYPE_CLAMPTO);
01650     KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_TRANSFORM, CONSTRAINT_TYPE_TRANSFORM);
01651     KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_DISTLIMIT, CONSTRAINT_TYPE_DISTLIMIT);
01652     /* BL_ArmatureConstraint ik_type */
01653     KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_COPYPOSE, CONSTRAINT_IK_COPYPOSE);
01654     KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_DISTANCE, CONSTRAINT_IK_DISTANCE);
01655     /* BL_ArmatureConstraint ik_mode */
01656     KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_MODE_INSIDE, LIMITDIST_INSIDE);
01657     KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_MODE_OUTSIDE, LIMITDIST_OUTSIDE);
01658     KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_MODE_ONSURFACE, LIMITDIST_ONSURFACE);
01659     /* BL_ArmatureConstraint ik_flag */
01660     KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_FLAG_TIP, CONSTRAINT_IK_TIP);
01661     KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_FLAG_ROT, CONSTRAINT_IK_ROT);
01662     KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_FLAG_STRETCH, CONSTRAINT_IK_STRETCH);
01663     KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_FLAG_POS, CONSTRAINT_IK_POS);
01664     /* KX_ArmatureSensor type */
01665     KX_MACRO_addTypesToDict(d, KX_ARMSENSOR_STATE_CHANGED, SENS_ARM_STATE_CHANGED);
01666     KX_MACRO_addTypesToDict(d, KX_ARMSENSOR_LIN_ERROR_BELOW, SENS_ARM_LIN_ERROR_BELOW);
01667     KX_MACRO_addTypesToDict(d, KX_ARMSENSOR_LIN_ERROR_ABOVE, SENS_ARM_LIN_ERROR_ABOVE);
01668     KX_MACRO_addTypesToDict(d, KX_ARMSENSOR_ROT_ERROR_BELOW, SENS_ARM_ROT_ERROR_BELOW);
01669     KX_MACRO_addTypesToDict(d, KX_ARMSENSOR_ROT_ERROR_ABOVE, SENS_ARM_ROT_ERROR_ABOVE);
01670 
01671     /* BL_ArmatureActuator type */
01672     KX_MACRO_addTypesToDict(d, KX_ACT_ARMATURE_RUN, ACT_ARM_RUN);
01673     KX_MACRO_addTypesToDict(d, KX_ACT_ARMATURE_ENABLE, ACT_ARM_ENABLE);
01674     KX_MACRO_addTypesToDict(d, KX_ACT_ARMATURE_DISABLE, ACT_ARM_DISABLE);
01675     KX_MACRO_addTypesToDict(d, KX_ACT_ARMATURE_SETTARGET, ACT_ARM_SETTARGET);
01676     KX_MACRO_addTypesToDict(d, KX_ACT_ARMATURE_SETWEIGHT, ACT_ARM_SETWEIGHT);
01677 
01678     /* BL_Armature Channel rotation_mode */
01679     KX_MACRO_addTypesToDict(d, ROT_MODE_QUAT, ROT_MODE_QUAT);
01680     KX_MACRO_addTypesToDict(d, ROT_MODE_XYZ, ROT_MODE_XYZ);
01681     KX_MACRO_addTypesToDict(d, ROT_MODE_XZY, ROT_MODE_XZY);
01682     KX_MACRO_addTypesToDict(d, ROT_MODE_YXZ, ROT_MODE_YXZ);
01683     KX_MACRO_addTypesToDict(d, ROT_MODE_YZX, ROT_MODE_YZX);
01684     KX_MACRO_addTypesToDict(d, ROT_MODE_ZXY, ROT_MODE_ZXY);
01685     KX_MACRO_addTypesToDict(d, ROT_MODE_ZYX, ROT_MODE_ZYX);
01686 
01687     /* Steering actuator */
01688     KX_MACRO_addTypesToDict(d, KX_STEERING_SEEK, KX_SteeringActuator::KX_STEERING_SEEK);
01689     KX_MACRO_addTypesToDict(d, KX_STEERING_FLEE, KX_SteeringActuator::KX_STEERING_FLEE);
01690     KX_MACRO_addTypesToDict(d, KX_STEERING_PATHFOLLOWING, KX_SteeringActuator::KX_STEERING_PATHFOLLOWING);
01691 
01692     /* KX_NavMeshObject render mode */
01693     KX_MACRO_addTypesToDict(d, RM_WALLS, KX_NavMeshObject::RM_WALLS);
01694     KX_MACRO_addTypesToDict(d, RM_POLYS, KX_NavMeshObject::RM_POLYS);
01695     KX_MACRO_addTypesToDict(d, RM_TRIS, KX_NavMeshObject::RM_TRIS);
01696 
01697     /* BL_Action play modes */
01698     KX_MACRO_addTypesToDict(d, KX_ACTION_MODE_PLAY, BL_Action::ACT_MODE_PLAY);
01699     KX_MACRO_addTypesToDict(d, KX_ACTION_MODE_LOOP, BL_Action::ACT_MODE_LOOP);
01700     KX_MACRO_addTypesToDict(d, KX_ACTION_MODE_PING_PONG, BL_Action::ACT_MODE_PING_PONG);
01701 
01702     // Check for errors
01703     if (PyErr_Occurred())
01704     {
01705         Py_FatalError("can't initialize module bge.logic");
01706     }
01707 
01708     return m;
01709 }
01710 
01711 /* Explanation of 
01712  * 
01713  * - backupPySysObjects()       : stores sys.path in gp_OrigPythonSysPath
01714  * - initPySysObjects(main) : initializes the blendfile and library paths
01715  * - restorePySysObjects()      : restores sys.path from gp_OrigPythonSysPath
01716  * 
01717  * These exist so the current blend dir "//" can always be used to import modules from.
01718  * the reason we need a few functions for this is that python is not only used by the game engine
01719  * so we cant just add to sys.path all the time, it would leave pythons state in a mess.
01720  * It would also be incorrect since loading blend files for new levels etc would alwasy add to sys.path
01721  * 
01722  * To play nice with blenders python, the sys.path is backed up and the current blendfile along
01723  * with all its lib paths are added to the sys path.
01724  * When loading a new blendfile, the original sys.path is restored and the new paths are added over the top.
01725  */
01726 
01730 static void backupPySysObjects(void)
01731 {
01732     PyObject *sys_path= PySys_GetObject("path"); /* should never fail */
01733     PyObject *sys_mods= PySys_GetObject("modules"); /* should never fail */
01734     
01735     /* paths */
01736     Py_XDECREF(gp_OrigPythonSysPath); /* just incase its set */
01737     gp_OrigPythonSysPath = PyList_GetSlice(sys_path, 0, INT_MAX); /* copy the list */
01738     
01739     /* modules */
01740     Py_XDECREF(gp_OrigPythonSysModules); /* just incase its set */
01741     gp_OrigPythonSysModules = PyDict_Copy(sys_mods); /* copy the list */
01742     
01743 }
01744 
01745 /* for initPySysObjects only,
01746  * takes a blend path and adds a scripts dir from it
01747  *
01748  * "/home/me/foo.blend" -> "/home/me/scripts"
01749  */
01750 static void initPySysObjects__append(PyObject *sys_path, const char *filename)
01751 {
01752     PyObject *item;
01753     char expanded[FILE_MAX];
01754     
01755     BLI_split_dir_part(filename, expanded, sizeof(expanded)); /* get the dir part of filename only */
01756     BLI_path_abs(expanded, gp_GamePythonPath); /* filename from lib->filename is (always?) absolute, so this may not be needed but it wont hurt */
01757     BLI_cleanup_file(gp_GamePythonPath, expanded); /* Dont use BLI_cleanup_dir because it adds a slash - BREAKS WIN32 ONLY */
01758     item= PyUnicode_DecodeFSDefault(expanded);
01759     
01760 //  printf("SysPath - '%s', '%s', '%s'\n", expanded, filename, gp_GamePythonPath);
01761     
01762     if(PySequence_Index(sys_path, item) == -1) {
01763         PyErr_Clear(); /* PySequence_Index sets a ValueError */
01764         PyList_Insert(sys_path, 0, item);
01765     }
01766     
01767     Py_DECREF(item);
01768 }
01769 static void initPySysObjects(Main *maggie)
01770 {
01771     PyObject *sys_path= PySys_GetObject("path"); /* should never fail */
01772     
01773     if (gp_OrigPythonSysPath==NULL) {
01774         /* backup */
01775         backupPySysObjects();
01776     }
01777     else {
01778         /* get the original sys path when the BGE started */
01779         PyList_SetSlice(sys_path, 0, INT_MAX, gp_OrigPythonSysPath);
01780     }
01781     
01782     Library *lib= (Library *)maggie->library.first;
01783     
01784     while(lib) {
01785         /* lib->name wont work in some cases (on win32),
01786          * even when expanding with gp_GamePythonPath, using lib->filename is less trouble */
01787         initPySysObjects__append(sys_path, lib->filepath);
01788         lib= (Library *)lib->id.next;
01789     }
01790     
01791     initPySysObjects__append(sys_path, gp_GamePythonPath);
01792     
01793 //  fprintf(stderr, "\nNew Path: %d ", PyList_GET_SIZE(sys_path));
01794 //  PyObject_Print(sys_path, stderr, 0);
01795 }
01796 
01797 static void restorePySysObjects(void)
01798 {
01799     if (gp_OrigPythonSysPath==NULL)
01800         return;
01801     
01802     PyObject *sys_path= PySys_GetObject("path"); /* should never fail */
01803     PyObject *sys_mods= PySys_GetObject("modules"); /* should never fail */
01804 
01805     /* paths */
01806     PyList_SetSlice(sys_path, 0, INT_MAX, gp_OrigPythonSysPath);
01807     Py_DECREF(gp_OrigPythonSysPath);
01808     gp_OrigPythonSysPath= NULL;
01809     
01810     /* modules */
01811     PyDict_Clear(sys_mods);
01812     PyDict_Update(sys_mods, gp_OrigPythonSysModules);
01813     Py_DECREF(gp_OrigPythonSysModules);
01814     gp_OrigPythonSysModules= NULL;  
01815     
01816     
01817 //  fprintf(stderr, "\nRestore Path: %d ", PyList_GET_SIZE(sys_path));
01818 //  PyObject_Print(sys_path, stderr, 0);
01819 }
01820 
01821 // Copied from bpy_interface.c
01822 static struct _inittab bge_internal_modules[]= {
01823     {(char *)"mathutils", PyInit_mathutils},
01824     {(char *)"bgl", BPyInit_bgl},
01825     {(char *)"blf", BPyInit_blf},
01826     {(char *)"aud", AUD_initPython},
01827     {NULL, NULL}
01828 };
01829 
01834 PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level, Main *maggie, int argc, char** argv)
01835 {
01836     /* Yet another gotcha in the py api
01837      * Cant run PySys_SetArgv more then once because this adds the
01838      * binary dir to the sys.path each time.
01839      * Id have thought python being totally restarted would make this ok but
01840      * somehow it remembers the sys.path - Campbell
01841      */
01842     static bool first_time = true;
01843     
01844 #if 0 // TODO - py3
01845     STR_String pname = progname;
01846     Py_SetProgramName(pname.Ptr());
01847 #endif
01848     Py_NoSiteFlag=1;
01849     Py_FrozenFlag=1;
01850 
01851     /* must run before python initializes */
01852     PyImport_ExtendInittab(bge_internal_modules);
01853 
01854     /* find local python installation */
01855     PyC_SetHomePath(BLI_get_folder(BLENDER_SYSTEM_PYTHON, NULL));
01856 
01857     Py_Initialize();
01858     
01859     if(argv && first_time) { /* browser plugins dont currently set this */
01860         // Until python support ascii again, we use our own.
01861         // PySys_SetArgv(argc, argv);
01862         int i;
01863         PyObject *py_argv= PyList_New(argc);
01864 
01865         for (i=0; i<argc; i++)
01866             PyList_SET_ITEM(py_argv, i, PyC_UnicodeFromByte(argv[i]));
01867 
01868         PySys_SetObject("argv", py_argv);
01869         Py_DECREF(py_argv);
01870     }
01871 
01872     bpy_import_init(PyEval_GetBuiltins());
01873 
01874     /* mathutils types are used by the BGE even if we dont import them */
01875     {
01876         PyObject *mod= PyImport_ImportModuleLevel((char *)"mathutils", NULL, NULL, NULL, 0);
01877         Py_DECREF(mod);
01878     }
01879 
01880     initPyTypes();
01881     
01882     bpy_import_main_set(maggie);
01883     
01884     initPySysObjects(maggie);
01885     
01886     first_time = false;
01887     
01888     PyObjectPlus::ClearDeprecationWarning();
01889 
01890     return PyC_DefaultNameSpace(NULL);
01891 }
01892 
01893 void exitGamePlayerPythonScripting()
01894 {   
01895     /* Clean up the Python mouse and keyboard */
01896     delete gp_PythonKeyboard;
01897     gp_PythonKeyboard = NULL;
01898 
01899     delete gp_PythonMouse;
01900     gp_PythonMouse = NULL;
01901 
01902     /* since python restarts we cant let the python backup of the sys.path hang around in a global pointer */
01903     restorePySysObjects(); /* get back the original sys.path and clear the backup */
01904     
01905     Py_Finalize();
01906     bpy_import_main_set(NULL);
01907     PyObjectPlus::ClearDeprecationWarning();
01908 }
01909 
01910 
01911 
01915 PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level, Main *maggie)
01916 {
01917 #if 0 // XXX TODO Py3
01918     STR_String pname = progname;
01919     Py_SetProgramName(pname.Ptr());
01920 #endif
01921     Py_NoSiteFlag=1;
01922     Py_FrozenFlag=1;
01923 
01924     initPyTypes();
01925     
01926     bpy_import_main_set(maggie);
01927     
01928     initPySysObjects(maggie);
01929 
01930     PyObjectPlus::NullDeprecationWarning();
01931 
01932     return PyC_DefaultNameSpace(NULL);
01933 }
01934 
01935 void exitGamePythonScripting()
01936 {
01937     /* Clean up the Python mouse and keyboard */
01938     delete gp_PythonKeyboard;
01939     gp_PythonKeyboard = NULL;
01940 
01941     delete gp_PythonMouse;
01942     gp_PythonMouse = NULL;
01943 
01944     restorePySysObjects(); /* get back the original sys.path and clear the backup */
01945     bpy_import_main_set(NULL);
01946     PyObjectPlus::ClearDeprecationWarning();
01947 }
01948 
01949 /* similar to the above functions except it sets up the namespace
01950  * and other more general things */
01951 void setupGamePython(KX_KetsjiEngine* ketsjiengine, KX_Scene* startscene, Main *blenderdata, PyObject * pyGlobalDict, PyObject **gameLogic, PyObject **gameLogic_keys, int argc, char** argv)
01952 {
01953     PyObject* dictionaryobject;
01954 
01955     if(argv) /* player only */
01956         dictionaryobject= initGamePlayerPythonScripting("Ketsji", psl_Lowest, blenderdata, argc, argv);
01957     else
01958         dictionaryobject= initGamePythonScripting("Ketsji", psl_Lowest, blenderdata);
01959 
01960     ketsjiengine->SetPyNamespace(dictionaryobject);
01961     initRasterizer(ketsjiengine->GetRasterizer(), ketsjiengine->GetCanvas());
01962     *gameLogic = initGameLogic(ketsjiengine, startscene);
01963 
01964     /* is set in initGameLogic so only set here if we want it to persist between scenes */
01965     if(pyGlobalDict)
01966         PyDict_SetItemString(PyModule_GetDict(*gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
01967 
01968     *gameLogic_keys = PyDict_Keys(PyModule_GetDict(*gameLogic));
01969 
01970     initGameKeys();
01971     initPythonConstraintBinding();
01972     initVideoTexture();
01973 
01974     /* could be done a lot more nicely, but for now a quick way to get bge.* working */
01975     PyRun_SimpleString("sys = __import__('sys');"
01976                        "mod = sys.modules['bge'] = type(sys)('bge');"
01977                        "mod.__dict__.update({'logic':__import__('GameLogic'), "
01978                                             "'render':__import__('Rasterizer'), "
01979                                             "'events':__import__('GameKeys'), "
01980                                             "'constraints':__import__('PhysicsConstraints'), "
01981                                             "'types':__import__('GameTypes'), "
01982                                             "'texture':__import__('VideoTexture')});"
01983                        );
01984 }
01985 
01986 static struct PyModuleDef Rasterizer_module_def = {
01987     {}, /* m_base */
01988     "Rasterizer",  /* m_name */
01989     Rasterizer_module_documentation,  /* m_doc */
01990     0,  /* m_size */
01991     rasterizer_methods,  /* m_methods */
01992     0,  /* m_reload */
01993     0,  /* m_traverse */
01994     0,  /* m_clear */
01995     0,  /* m_free */
01996 };
01997 
01998 PyObject* initRasterizer(RAS_IRasterizer* rasty,RAS_ICanvas* canvas)
01999 {
02000     gp_Canvas = canvas;
02001     gp_Rasterizer = rasty;
02002 
02003 
02004     PyObject* m;
02005     PyObject* d;
02006     PyObject* item;
02007 
02008     /* Use existing module where possible
02009   * be careful not to init any runtime vars after this */
02010     m = PyImport_ImportModule( "Rasterizer" );
02011     if(m) {
02012         Py_DECREF(m);
02013         return m;
02014     }
02015     else {
02016         PyErr_Clear();
02017 
02018         // Create the module and add the functions
02019         m = PyModule_Create(&Rasterizer_module_def);
02020         PyDict_SetItemString(PySys_GetObject("modules"), Rasterizer_module_def.m_name, m);
02021     }
02022 
02023     // Add some symbolic constants to the module
02024     d = PyModule_GetDict(m);
02025     ErrorObject = PyUnicode_FromString("Rasterizer.error");
02026     PyDict_SetItemString(d, "error", ErrorObject);
02027     Py_DECREF(ErrorObject);
02028 
02029     /* needed for get/setMaterialType */
02030     KX_MACRO_addTypesToDict(d, KX_TEXFACE_MATERIAL, KX_TEXFACE_MATERIAL);
02031     KX_MACRO_addTypesToDict(d, KX_BLENDER_MULTITEX_MATERIAL, KX_BLENDER_MULTITEX_MATERIAL);
02032     KX_MACRO_addTypesToDict(d, KX_BLENDER_GLSL_MATERIAL, KX_BLENDER_GLSL_MATERIAL);
02033 
02034     // XXXX Add constants here
02035 
02036     // Check for errors
02037     if (PyErr_Occurred())
02038     {
02039         Py_FatalError("can't initialize module Rasterizer");
02040     }
02041 
02042     return d;
02043 }
02044 
02045 
02046 
02047 /* ------------------------------------------------------------------------- */
02048 /* GameKeys: symbolic constants for key mapping                              */
02049 /* ------------------------------------------------------------------------- */
02050 
02051 static char GameKeys_module_documentation[] =
02052 "This modules provides defines for key-codes"
02053 ;
02054 
02055 static char gPyEventToString_doc[] =
02056 "EventToString(event) - Take a valid event from the GameKeys module or Keyboard Sensor and return a name"
02057 ;
02058 
02059 static PyObject* gPyEventToString(PyObject*, PyObject* value)
02060 {
02061     PyObject* mod, *dict, *key, *val, *ret = NULL;
02062     Py_ssize_t pos = 0;
02063     
02064     mod = PyImport_ImportModule( "GameKeys" );
02065     if (!mod)
02066         return NULL;
02067     
02068     dict = PyModule_GetDict(mod);
02069     
02070     while (PyDict_Next(dict, &pos, &key, &val)) {
02071         if (PyObject_RichCompareBool(value, val, Py_EQ)) {
02072             ret = key;
02073             break;
02074         }
02075     }
02076     
02077     PyErr_Clear(); // incase there was an error clearing
02078     Py_DECREF(mod);
02079     if (!ret)   PyErr_SetString(PyExc_ValueError, "GameKeys.EventToString(int): expected a valid int keyboard event");
02080     else        Py_INCREF(ret);
02081     
02082     return ret;
02083 }
02084 
02085 static char gPyEventToCharacter_doc[] =
02086 "EventToCharacter(event, is_shift) - Take a valid event from the GameKeys module or Keyboard Sensor and return a character"
02087 ;
02088 
02089 static PyObject* gPyEventToCharacter(PyObject*, PyObject* args)
02090 {
02091     int event, shift;
02092     if (!PyArg_ParseTuple(args,"ii:EventToCharacter", &event, &shift))
02093         return NULL;
02094     
02095     if(IsPrintable(event)) {
02096         char ch[2] = {'\0', '\0'};
02097         ch[0] = ToCharacter(event, (bool)shift);
02098         return PyUnicode_FromString(ch);
02099     }
02100     else {
02101         return PyUnicode_FromString("");
02102     }
02103 }
02104 
02105 
02106 static struct PyMethodDef gamekeys_methods[] = {
02107     {"EventToCharacter", (PyCFunction)gPyEventToCharacter, METH_VARARGS, (const char *)gPyEventToCharacter_doc},
02108     {"EventToString", (PyCFunction)gPyEventToString, METH_O, (const char *)gPyEventToString_doc},
02109     { NULL, (PyCFunction) NULL, 0, NULL }
02110 };
02111 
02112 static struct PyModuleDef GameKeys_module_def = {
02113     {}, /* m_base */
02114     "GameKeys",  /* m_name */
02115     GameKeys_module_documentation,  /* m_doc */
02116     0,  /* m_size */
02117     gamekeys_methods,  /* m_methods */
02118     0,  /* m_reload */
02119     0,  /* m_traverse */
02120     0,  /* m_clear */
02121     0,  /* m_free */
02122 };
02123 
02124 PyObject* initGameKeys()
02125 {
02126     PyObject* m;
02127     PyObject* d;
02128     PyObject* item;
02129     
02130     /* Use existing module where possible */
02131     m = PyImport_ImportModule( "GameKeys" );
02132     if(m) {
02133         Py_DECREF(m);
02134         return m;
02135     }
02136     else {
02137         PyErr_Clear();
02138     
02139         // Create the module and add the functions
02140         m = PyModule_Create(&GameKeys_module_def);
02141         PyDict_SetItemString(PySys_GetObject("modules"), GameKeys_module_def.m_name, m);
02142     }
02143 
02144     // Add some symbolic constants to the module
02145     d = PyModule_GetDict(m);
02146 
02147     // XXXX Add constants here
02148 
02149     KX_MACRO_addTypesToDict(d, AKEY, SCA_IInputDevice::KX_AKEY);
02150     KX_MACRO_addTypesToDict(d, BKEY, SCA_IInputDevice::KX_BKEY);
02151     KX_MACRO_addTypesToDict(d, CKEY, SCA_IInputDevice::KX_CKEY);
02152     KX_MACRO_addTypesToDict(d, DKEY, SCA_IInputDevice::KX_DKEY);
02153     KX_MACRO_addTypesToDict(d, EKEY, SCA_IInputDevice::KX_EKEY);
02154     KX_MACRO_addTypesToDict(d, FKEY, SCA_IInputDevice::KX_FKEY);
02155     KX_MACRO_addTypesToDict(d, GKEY, SCA_IInputDevice::KX_GKEY);
02156     KX_MACRO_addTypesToDict(d, HKEY, SCA_IInputDevice::KX_HKEY);
02157     KX_MACRO_addTypesToDict(d, IKEY, SCA_IInputDevice::KX_IKEY);
02158     KX_MACRO_addTypesToDict(d, JKEY, SCA_IInputDevice::KX_JKEY);
02159     KX_MACRO_addTypesToDict(d, KKEY, SCA_IInputDevice::KX_KKEY);
02160     KX_MACRO_addTypesToDict(d, LKEY, SCA_IInputDevice::KX_LKEY);
02161     KX_MACRO_addTypesToDict(d, MKEY, SCA_IInputDevice::KX_MKEY);
02162     KX_MACRO_addTypesToDict(d, NKEY, SCA_IInputDevice::KX_NKEY);
02163     KX_MACRO_addTypesToDict(d, OKEY, SCA_IInputDevice::KX_OKEY);
02164     KX_MACRO_addTypesToDict(d, PKEY, SCA_IInputDevice::KX_PKEY);
02165     KX_MACRO_addTypesToDict(d, QKEY, SCA_IInputDevice::KX_QKEY);
02166     KX_MACRO_addTypesToDict(d, RKEY, SCA_IInputDevice::KX_RKEY);
02167     KX_MACRO_addTypesToDict(d, SKEY, SCA_IInputDevice::KX_SKEY);
02168     KX_MACRO_addTypesToDict(d, TKEY, SCA_IInputDevice::KX_TKEY);
02169     KX_MACRO_addTypesToDict(d, UKEY, SCA_IInputDevice::KX_UKEY);
02170     KX_MACRO_addTypesToDict(d, VKEY, SCA_IInputDevice::KX_VKEY);
02171     KX_MACRO_addTypesToDict(d, WKEY, SCA_IInputDevice::KX_WKEY);
02172     KX_MACRO_addTypesToDict(d, XKEY, SCA_IInputDevice::KX_XKEY);
02173     KX_MACRO_addTypesToDict(d, YKEY, SCA_IInputDevice::KX_YKEY);
02174     KX_MACRO_addTypesToDict(d, ZKEY, SCA_IInputDevice::KX_ZKEY);
02175     
02176     KX_MACRO_addTypesToDict(d, ZEROKEY, SCA_IInputDevice::KX_ZEROKEY);      
02177     KX_MACRO_addTypesToDict(d, ONEKEY, SCA_IInputDevice::KX_ONEKEY);        
02178     KX_MACRO_addTypesToDict(d, TWOKEY, SCA_IInputDevice::KX_TWOKEY);        
02179     KX_MACRO_addTypesToDict(d, THREEKEY, SCA_IInputDevice::KX_THREEKEY);
02180     KX_MACRO_addTypesToDict(d, FOURKEY, SCA_IInputDevice::KX_FOURKEY);      
02181     KX_MACRO_addTypesToDict(d, FIVEKEY, SCA_IInputDevice::KX_FIVEKEY);      
02182     KX_MACRO_addTypesToDict(d, SIXKEY, SCA_IInputDevice::KX_SIXKEY);        
02183     KX_MACRO_addTypesToDict(d, SEVENKEY, SCA_IInputDevice::KX_SEVENKEY);
02184     KX_MACRO_addTypesToDict(d, EIGHTKEY, SCA_IInputDevice::KX_EIGHTKEY);
02185     KX_MACRO_addTypesToDict(d, NINEKEY, SCA_IInputDevice::KX_NINEKEY);      
02186         
02187     KX_MACRO_addTypesToDict(d, CAPSLOCKKEY, SCA_IInputDevice::KX_CAPSLOCKKEY);
02188         
02189     KX_MACRO_addTypesToDict(d, LEFTCTRLKEY, SCA_IInputDevice::KX_LEFTCTRLKEY);  
02190     KX_MACRO_addTypesToDict(d, LEFTALTKEY, SCA_IInputDevice::KX_LEFTALTKEY);        
02191     KX_MACRO_addTypesToDict(d, RIGHTALTKEY, SCA_IInputDevice::KX_RIGHTALTKEY);  
02192     KX_MACRO_addTypesToDict(d, RIGHTCTRLKEY, SCA_IInputDevice::KX_RIGHTCTRLKEY);    
02193     KX_MACRO_addTypesToDict(d, RIGHTSHIFTKEY, SCA_IInputDevice::KX_RIGHTSHIFTKEY);  
02194     KX_MACRO_addTypesToDict(d, LEFTSHIFTKEY, SCA_IInputDevice::KX_LEFTSHIFTKEY);
02195         
02196     KX_MACRO_addTypesToDict(d, ESCKEY, SCA_IInputDevice::KX_ESCKEY);
02197     KX_MACRO_addTypesToDict(d, TABKEY, SCA_IInputDevice::KX_TABKEY);
02198     KX_MACRO_addTypesToDict(d, RETKEY, SCA_IInputDevice::KX_RETKEY);
02199     KX_MACRO_addTypesToDict(d, ENTERKEY, SCA_IInputDevice::KX_RETKEY);
02200     KX_MACRO_addTypesToDict(d, SPACEKEY, SCA_IInputDevice::KX_SPACEKEY);
02201     KX_MACRO_addTypesToDict(d, LINEFEEDKEY, SCA_IInputDevice::KX_LINEFEEDKEY);      
02202     KX_MACRO_addTypesToDict(d, BACKSPACEKEY, SCA_IInputDevice::KX_BACKSPACEKEY);
02203     KX_MACRO_addTypesToDict(d, DELKEY, SCA_IInputDevice::KX_DELKEY);
02204     KX_MACRO_addTypesToDict(d, SEMICOLONKEY, SCA_IInputDevice::KX_SEMICOLONKEY);
02205     KX_MACRO_addTypesToDict(d, PERIODKEY, SCA_IInputDevice::KX_PERIODKEY);      
02206     KX_MACRO_addTypesToDict(d, COMMAKEY, SCA_IInputDevice::KX_COMMAKEY);        
02207     KX_MACRO_addTypesToDict(d, QUOTEKEY, SCA_IInputDevice::KX_QUOTEKEY);        
02208     KX_MACRO_addTypesToDict(d, ACCENTGRAVEKEY, SCA_IInputDevice::KX_ACCENTGRAVEKEY);    
02209     KX_MACRO_addTypesToDict(d, MINUSKEY, SCA_IInputDevice::KX_MINUSKEY);        
02210     KX_MACRO_addTypesToDict(d, SLASHKEY, SCA_IInputDevice::KX_SLASHKEY);        
02211     KX_MACRO_addTypesToDict(d, BACKSLASHKEY, SCA_IInputDevice::KX_BACKSLASHKEY);
02212     KX_MACRO_addTypesToDict(d, EQUALKEY, SCA_IInputDevice::KX_EQUALKEY);        
02213     KX_MACRO_addTypesToDict(d, LEFTBRACKETKEY, SCA_IInputDevice::KX_LEFTBRACKETKEY);    
02214     KX_MACRO_addTypesToDict(d, RIGHTBRACKETKEY, SCA_IInputDevice::KX_RIGHTBRACKETKEY);  
02215         
02216     KX_MACRO_addTypesToDict(d, LEFTARROWKEY, SCA_IInputDevice::KX_LEFTARROWKEY);
02217     KX_MACRO_addTypesToDict(d, DOWNARROWKEY, SCA_IInputDevice::KX_DOWNARROWKEY);
02218     KX_MACRO_addTypesToDict(d, RIGHTARROWKEY, SCA_IInputDevice::KX_RIGHTARROWKEY);  
02219     KX_MACRO_addTypesToDict(d, UPARROWKEY, SCA_IInputDevice::KX_UPARROWKEY);        
02220     
02221     KX_MACRO_addTypesToDict(d, PAD2 , SCA_IInputDevice::KX_PAD2);
02222     KX_MACRO_addTypesToDict(d, PAD4 , SCA_IInputDevice::KX_PAD4);
02223     KX_MACRO_addTypesToDict(d, PAD6 , SCA_IInputDevice::KX_PAD6);
02224     KX_MACRO_addTypesToDict(d, PAD8 , SCA_IInputDevice::KX_PAD8);
02225         
02226     KX_MACRO_addTypesToDict(d, PAD1 , SCA_IInputDevice::KX_PAD1);
02227     KX_MACRO_addTypesToDict(d, PAD3 , SCA_IInputDevice::KX_PAD3);
02228     KX_MACRO_addTypesToDict(d, PAD5 , SCA_IInputDevice::KX_PAD5);
02229     KX_MACRO_addTypesToDict(d, PAD7 , SCA_IInputDevice::KX_PAD7);
02230     KX_MACRO_addTypesToDict(d, PAD9 , SCA_IInputDevice::KX_PAD9);
02231         
02232     KX_MACRO_addTypesToDict(d, PADPERIOD, SCA_IInputDevice::KX_PADPERIOD);
02233     KX_MACRO_addTypesToDict(d, PADSLASHKEY, SCA_IInputDevice::KX_PADSLASHKEY);
02234     KX_MACRO_addTypesToDict(d, PADASTERKEY, SCA_IInputDevice::KX_PADASTERKEY);
02235         
02236         
02237     KX_MACRO_addTypesToDict(d, PAD0, SCA_IInputDevice::KX_PAD0);
02238     KX_MACRO_addTypesToDict(d, PADMINUS, SCA_IInputDevice::KX_PADMINUS);
02239     KX_MACRO_addTypesToDict(d, PADENTER, SCA_IInputDevice::KX_PADENTER);
02240     KX_MACRO_addTypesToDict(d, PADPLUSKEY, SCA_IInputDevice::KX_PADPLUSKEY);
02241         
02242         
02243     KX_MACRO_addTypesToDict(d, F1KEY , SCA_IInputDevice::KX_F1KEY);
02244     KX_MACRO_addTypesToDict(d, F2KEY , SCA_IInputDevice::KX_F2KEY);
02245     KX_MACRO_addTypesToDict(d, F3KEY , SCA_IInputDevice::KX_F3KEY);
02246     KX_MACRO_addTypesToDict(d, F4KEY , SCA_IInputDevice::KX_F4KEY);
02247     KX_MACRO_addTypesToDict(d, F5KEY , SCA_IInputDevice::KX_F5KEY);
02248     KX_MACRO_addTypesToDict(d, F6KEY , SCA_IInputDevice::KX_F6KEY);
02249     KX_MACRO_addTypesToDict(d, F7KEY , SCA_IInputDevice::KX_F7KEY);
02250     KX_MACRO_addTypesToDict(d, F8KEY , SCA_IInputDevice::KX_F8KEY);
02251     KX_MACRO_addTypesToDict(d, F9KEY , SCA_IInputDevice::KX_F9KEY);
02252     KX_MACRO_addTypesToDict(d, F10KEY, SCA_IInputDevice::KX_F10KEY);
02253     KX_MACRO_addTypesToDict(d, F11KEY, SCA_IInputDevice::KX_F11KEY);
02254     KX_MACRO_addTypesToDict(d, F12KEY, SCA_IInputDevice::KX_F12KEY);
02255     KX_MACRO_addTypesToDict(d, F13KEY, SCA_IInputDevice::KX_F13KEY);
02256     KX_MACRO_addTypesToDict(d, F14KEY, SCA_IInputDevice::KX_F14KEY);
02257     KX_MACRO_addTypesToDict(d, F15KEY, SCA_IInputDevice::KX_F15KEY);
02258     KX_MACRO_addTypesToDict(d, F16KEY, SCA_IInputDevice::KX_F16KEY);
02259     KX_MACRO_addTypesToDict(d, F17KEY, SCA_IInputDevice::KX_F17KEY);
02260     KX_MACRO_addTypesToDict(d, F18KEY, SCA_IInputDevice::KX_F18KEY);
02261     KX_MACRO_addTypesToDict(d, F19KEY, SCA_IInputDevice::KX_F19KEY);
02262         
02263     KX_MACRO_addTypesToDict(d, PAUSEKEY, SCA_IInputDevice::KX_PAUSEKEY);
02264     KX_MACRO_addTypesToDict(d, INSERTKEY, SCA_IInputDevice::KX_INSERTKEY);
02265     KX_MACRO_addTypesToDict(d, HOMEKEY , SCA_IInputDevice::KX_HOMEKEY);
02266     KX_MACRO_addTypesToDict(d, PAGEUPKEY, SCA_IInputDevice::KX_PAGEUPKEY);
02267     KX_MACRO_addTypesToDict(d, PAGEDOWNKEY, SCA_IInputDevice::KX_PAGEDOWNKEY);
02268     KX_MACRO_addTypesToDict(d, ENDKEY, SCA_IInputDevice::KX_ENDKEY);
02269 
02270     // MOUSE
02271     KX_MACRO_addTypesToDict(d, LEFTMOUSE, SCA_IInputDevice::KX_LEFTMOUSE);
02272     KX_MACRO_addTypesToDict(d, MIDDLEMOUSE, SCA_IInputDevice::KX_MIDDLEMOUSE);
02273     KX_MACRO_addTypesToDict(d, RIGHTMOUSE, SCA_IInputDevice::KX_RIGHTMOUSE);
02274     KX_MACRO_addTypesToDict(d, WHEELUPMOUSE, SCA_IInputDevice::KX_WHEELUPMOUSE);
02275     KX_MACRO_addTypesToDict(d, WHEELDOWNMOUSE, SCA_IInputDevice::KX_WHEELDOWNMOUSE);
02276     KX_MACRO_addTypesToDict(d, MOUSEX, SCA_IInputDevice::KX_MOUSEX);
02277     KX_MACRO_addTypesToDict(d, MOUSEY, SCA_IInputDevice::KX_MOUSEY);
02278 
02279     // Check for errors
02280     if (PyErr_Occurred())
02281     {
02282         Py_FatalError("can't initialize module GameKeys");
02283     }
02284 
02285     return d;
02286 }
02287 
02288 // utility function for loading and saving the globalDict
02289 int saveGamePythonConfig( char **marshal_buffer)
02290 {
02291     int marshal_length = 0;
02292     PyObject* gameLogic = PyImport_ImportModule("GameLogic");
02293     if (gameLogic) {
02294         PyObject* pyGlobalDict = PyDict_GetItemString(PyModule_GetDict(gameLogic), "globalDict"); // Same as importing the module
02295         if (pyGlobalDict) {
02296 #ifdef Py_MARSHAL_VERSION   
02297             PyObject* pyGlobalDictMarshal = PyMarshal_WriteObjectToString(  pyGlobalDict, 2); // Py_MARSHAL_VERSION == 2 as of Py2.5
02298 #else
02299             PyObject* pyGlobalDictMarshal = PyMarshal_WriteObjectToString(  pyGlobalDict ); 
02300 #endif
02301             if (pyGlobalDictMarshal) {
02302                 // for testing only
02303                 // PyObject_Print(pyGlobalDictMarshal, stderr, 0);
02304                 char *marshal_cstring;
02305                 
02306                 marshal_cstring = PyBytes_AsString(pyGlobalDictMarshal); // py3 uses byte arrays
02307                 marshal_length= PyBytes_Size(pyGlobalDictMarshal);
02308                 *marshal_buffer = new char[marshal_length + 1];
02309                 memcpy(*marshal_buffer, marshal_cstring, marshal_length);
02310                 Py_DECREF(pyGlobalDictMarshal);
02311             } else {
02312                 printf("Error, bge.logic.globalDict could not be marshal'd\n");
02313             }
02314         } else {
02315             printf("Error, bge.logic.globalDict was removed\n");
02316         }
02317         Py_DECREF(gameLogic);
02318     } else {
02319         PyErr_Clear();
02320         printf("Error, bge.logic failed to import bge.logic.globalDict will be lost\n");
02321     }
02322     return marshal_length;
02323 }
02324 
02325 int loadGamePythonConfig(char *marshal_buffer, int marshal_length)
02326 {
02327     /* Restore the dict */
02328     if (marshal_buffer) {
02329         PyObject* gameLogic = PyImport_ImportModule("GameLogic");
02330 
02331         if (gameLogic) {
02332             PyObject* pyGlobalDict = PyMarshal_ReadObjectFromString(marshal_buffer, marshal_length);
02333             if (pyGlobalDict) {
02334                 PyObject* pyGlobalDict_orig = PyDict_GetItemString(PyModule_GetDict(gameLogic), "globalDict"); // Same as importing the module.
02335                 if (pyGlobalDict_orig) {
02336                     PyDict_Clear(pyGlobalDict_orig);
02337                     PyDict_Update(pyGlobalDict_orig, pyGlobalDict);
02338                 } else {
02339                     /* this should not happen, but cant find the original globalDict, just assign it then */
02340                     PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
02341                 }
02342                 Py_DECREF(gameLogic);
02343                 Py_DECREF(pyGlobalDict);
02344                 return 1;
02345             } else {
02346                 Py_DECREF(gameLogic);
02347                 PyErr_Clear();
02348                 printf("Error could not marshall string\n");
02349             }
02350         } else {
02351             PyErr_Clear();
02352             printf("Error, bge.logic failed to import bge.logic.globalDict will be lost\n");
02353         }   
02354     }
02355     return 0;
02356 }
02357 
02358 void pathGamePythonConfig(char *path)
02359 {
02360     int len = strlen(gp_GamePythonPathOrig); // Always use the first loaded blend filename
02361     
02362     BLI_strncpy(path, gp_GamePythonPathOrig, sizeof(gp_GamePythonPathOrig));
02363 
02364     /* replace extension */
02365     if (BLI_testextensie(path, ".blend")) {
02366         strcpy(path+(len-6), ".bgeconf");
02367     } else {
02368         strcpy(path+len, ".bgeconf");
02369     }
02370 }
02371 
02372 void setGamePythonPath(const char *path)
02373 {
02374     BLI_strncpy(gp_GamePythonPath, path, sizeof(gp_GamePythonPath));
02375     BLI_cleanup_file(NULL, gp_GamePythonPath); /* not absolutely needed but makes resolving path problems less confusing later */
02376     
02377     if (gp_GamePythonPathOrig[0] == '\0')
02378         BLI_strncpy(gp_GamePythonPathOrig, path, sizeof(gp_GamePythonPathOrig));
02379 }
02380 
02381 // we need this so while blender is open (not blenderplayer)
02382 // loading new blendfiles will reset this on starting the
02383 // engine but loading blend files within the BGE wont overwrite gp_GamePythonPathOrig
02384 void resetGamePythonPath()
02385 {
02386     gp_GamePythonPathOrig[0] = '\0';
02387 }
02388 
02389 #endif // WITH_PYTHON