BGE: Remove old world bge.render API
[blender.git] / source / gameengine / Ketsji / KX_PythonInit.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * Initialize Python thingies.
27  */
28
29 /** \file gameengine/Ketsji/KX_PythonInit.cpp
30  *  \ingroup ketsji
31  */
32
33 #include "glew-mx.h"
34
35 #ifdef _MSC_VER
36 #  pragma warning (disable:4786)
37 #endif
38
39 #ifdef WITH_PYTHON
40 #  ifdef   _POSIX_C_SOURCE
41 #    undef _POSIX_C_SOURCE
42 #  endif
43 #  ifdef   _XOPEN_SOURCE
44 #    undef _XOPEN_SOURCE
45 #  endif
46 #  if defined(__sun) || defined(sun)
47 #    if defined(_XPG4)
48 #      undef _XPG4
49 #    endif
50 #  endif
51 #  include <Python.h>
52
53 extern "C" {
54         #  include "bpy_internal_import.h"  /* from the blender python api, but we want to import text too! */
55         #  include "py_capi_utils.h"
56         #  include "mathutils.h" // 'mathutils' module copied here so the blenderlayer can use.
57         #  include "bgl.h"
58         #  include "blf_py_api.h"
59
60         #  include "marshal.h" /* python header for loading/saving dicts */
61 }
62 #include "AUD_PyInit.h"
63
64 #endif  /* WITH_PYTHON */
65
66 #include "KX_PythonInit.h"
67
68 // directory header for py function getBlendFileList
69 #ifndef WIN32
70 #  include <dirent.h>
71 #  include <stdlib.h>
72 #else
73 #  include <io.h>
74 #  include "BLI_winstuff.h"
75 #endif
76
77 //python physics binding
78 #include "KX_PyConstraintBinding.h"
79
80 #include "KX_KetsjiEngine.h"
81 #include "KX_RadarSensor.h"
82 #include "KX_RaySensor.h"
83 #include "KX_ArmatureSensor.h"
84 #include "KX_SceneActuator.h"
85 #include "KX_GameActuator.h"
86 #include "KX_ParentActuator.h"
87 #include "KX_SCA_DynamicActuator.h"
88 #include "KX_SteeringActuator.h"
89 #include "KX_NavMeshObject.h"
90 #include "KX_MouseActuator.h"
91 #include "KX_TrackToActuator.h"
92
93 #include "SCA_IInputDevice.h"
94 #include "SCA_PropertySensor.h"
95 #include "SCA_RandomActuator.h"
96 #include "SCA_KeyboardSensor.h" /* IsPrintable, ToCharacter */
97 #include "SCA_JoystickManager.h" /* JOYINDEX_MAX */
98 #include "SCA_PythonJoystick.h"
99 #include "SCA_PythonKeyboard.h"
100 #include "SCA_PythonMouse.h"
101 #include "KX_ConstraintActuator.h"
102 #include "KX_IpoActuator.h"
103 #include "KX_SoundActuator.h"
104 #include "KX_StateActuator.h"
105 #include "BL_ActionActuator.h"
106 #include "BL_ArmatureObject.h"
107 #include "RAS_IRasterizer.h"
108 #include "RAS_ICanvas.h"
109 #include "RAS_BucketManager.h"
110 #include "RAS_2DFilterManager.h"
111 #include "MT_Vector3.h"
112 #include "MT_Point3.h"
113 #include "ListValue.h"
114 #include "InputParser.h"
115 #include "KX_Scene.h"
116
117 #include "NG_NetworkScene.h" //Needed for sendMessage()
118
119 #include "BL_Shader.h"
120 #include "BL_Action.h"
121
122 #include "KX_PyMath.h"
123
124 #include "PyObjectPlus.h"
125
126 #include "KX_PythonInitTypes.h" 
127
128 /* we only need this to get a list of libraries from the main struct */
129 #include "DNA_ID.h"
130 #include "DNA_scene_types.h"
131
132 #include "PHY_IPhysicsEnvironment.h"
133
134 extern "C" {
135 #include "BKE_main.h"
136 #include "BKE_global.h"
137 #include "BKE_library.h"
138 #include "BKE_appdir.h"
139 #include "BLI_blenlib.h"
140 #include "GPU_material.h"
141 #include "MEM_guardedalloc.h"
142 }
143
144 /* for converting new scenes */
145 #include "KX_BlenderSceneConverter.h"
146 #include "KX_LibLoadStatus.h"
147 #include "KX_MeshProxy.h" /* for creating a new library of mesh objects */
148 extern "C" {
149         #include "BKE_idcode.h"
150 }
151
152 // 'local' copy of canvas ptr, for window height/width python scripts
153
154 #ifdef WITH_PYTHON
155
156 static RAS_ICanvas* gp_Canvas = NULL;
157 static char gp_GamePythonPath[FILE_MAX] = "";
158 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
159
160 static SCA_PythonKeyboard* gp_PythonKeyboard = NULL;
161 static SCA_PythonMouse* gp_PythonMouse = NULL;
162 static SCA_PythonJoystick* gp_PythonJoysticks[JOYINDEX_MAX] = {NULL};
163 #endif // WITH_PYTHON
164
165 static KX_Scene*        gp_KetsjiScene = NULL;
166 static KX_KetsjiEngine* gp_KetsjiEngine = NULL;
167 static RAS_IRasterizer* gp_Rasterizer = NULL;
168
169
170 void KX_SetActiveScene(class KX_Scene* scene)
171 {
172         gp_KetsjiScene = scene;
173 }
174
175 class KX_Scene* KX_GetActiveScene()
176 {
177         return gp_KetsjiScene;
178 }
179
180 class KX_KetsjiEngine* KX_GetActiveEngine()
181 {
182         return gp_KetsjiEngine;
183 }
184
185 /* why is this in python? */
186 void    KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,const MT_Vector3& color)
187 {
188         if (gp_Rasterizer)
189                 gp_Rasterizer->DrawDebugLine(from,to,color);
190 }
191
192 void KX_RasterizerDrawDebugCircle(const MT_Vector3& center, const MT_Scalar radius, const MT_Vector3& color,
193                                   const MT_Vector3& normal, int nsector)
194 {
195         if (gp_Rasterizer)
196                 gp_Rasterizer->DrawDebugCircle(center, radius, color, normal, nsector);
197 }
198
199 #ifdef WITH_PYTHON
200
201 static PyObject *gp_OrigPythonSysPath= NULL;
202 static PyObject *gp_OrigPythonSysModules= NULL;
203
204 /* Macro for building the keyboard translation */
205 //#define KX_MACRO_addToDict(dict, name) PyDict_SetItemString(dict, #name, PyLong_FromLong(SCA_IInputDevice::KX_##name))
206 //#define KX_MACRO_addToDict(dict, name) PyDict_SetItemString(dict, #name, item=PyLong_FromLong(name)); Py_DECREF(item)
207 /* For the defines for types from logic bricks, we do stuff explicitly... */
208 #define KX_MACRO_addTypesToDict(dict, name, value) KX_MACRO_addTypesToDict_fn(dict, #name, value)
209 static void KX_MACRO_addTypesToDict_fn(PyObject *dict, const char *name, long value)
210 {
211         PyObject *item;
212
213         item = PyLong_FromLong(value);
214         PyDict_SetItemString(dict, name, item);
215         Py_DECREF(item);
216 }
217
218
219
220 // temporarily python stuff, will be put in another place later !
221 #include "KX_Python.h"
222 #include "SCA_PythonController.h"
223 // List of methods defined in the module
224
225 static PyObject *ErrorObject;
226
227 PyDoc_STRVAR(gPyGetRandomFloat_doc,
228 "getRandomFloat()\n"
229 "returns a random floating point value in the range [0..1]"
230 );
231 static PyObject *gPyGetRandomFloat(PyObject *)
232 {
233         return PyFloat_FromDouble(MT_random());
234 }
235
236 static PyObject *gPySetGravity(PyObject *, PyObject *value)
237 {
238         MT_Vector3 vec;
239         if (!PyVecTo(value, vec))
240                 return NULL;
241
242         if (gp_KetsjiScene)
243                 gp_KetsjiScene->SetGravity(vec);
244         
245         Py_RETURN_NONE;
246 }
247
248 PyDoc_STRVAR(gPyExpandPath_doc,
249 "expandPath(path)\n"
250 "Converts a blender internal path into a proper file system path.\n"
251 " path - the string path to convert.\n"
252 "Use / as directory separator in path\n"
253 "You can use '//' at the start of the string to define a relative path."
254 "Blender replaces that string by the directory of the current .blend or runtime file to make a full path name.\n"
255 "The function also converts the directory separator to the local file system format."
256 );
257 static PyObject *gPyExpandPath(PyObject *, PyObject *args)
258 {
259         char expanded[FILE_MAX];
260         char* filename;
261         
262         if (!PyArg_ParseTuple(args,"s:ExpandPath",&filename))
263                 return NULL;
264
265         BLI_strncpy(expanded, filename, FILE_MAX);
266         BLI_path_abs(expanded, gp_GamePythonPath);
267         return PyC_UnicodeFromByte(expanded);
268 }
269
270 PyDoc_STRVAR(gPyStartGame_doc,
271 "startGame(blend)\n"
272 "Loads the blend file"
273 );
274 static PyObject *gPyStartGame(PyObject *, PyObject *args)
275 {
276         char* blendfile;
277
278         if (!PyArg_ParseTuple(args, "s:startGame", &blendfile))
279                 return NULL;
280
281         gp_KetsjiEngine->RequestExit(KX_EXIT_REQUEST_START_OTHER_GAME);
282         gp_KetsjiEngine->SetNameNextGame(blendfile);
283
284         Py_RETURN_NONE;
285 }
286
287 PyDoc_STRVAR(gPyEndGame_doc,
288 "endGame()\n"
289 "Ends the current game"
290 );
291 static PyObject *gPyEndGame(PyObject *)
292 {
293         gp_KetsjiEngine->RequestExit(KX_EXIT_REQUEST_QUIT_GAME);
294
295         //printf("%s\n", gp_GamePythonPath);
296
297         Py_RETURN_NONE;
298 }
299
300 PyDoc_STRVAR(gPyRestartGame_doc,
301 "restartGame()\n"
302 "Restarts the current game by reloading the .blend file"
303 );
304 static PyObject *gPyRestartGame(PyObject *)
305 {
306         gp_KetsjiEngine->RequestExit(KX_EXIT_REQUEST_RESTART_GAME);
307         gp_KetsjiEngine->SetNameNextGame(gp_GamePythonPath);
308
309         Py_RETURN_NONE;
310 }
311
312 PyDoc_STRVAR(gPySaveGlobalDict_doc,
313 "saveGlobalDict()\n"
314 "Saves bge.logic.globalDict to a file"
315 );
316 static PyObject *gPySaveGlobalDict(PyObject *)
317 {
318         char marshal_path[512];
319         char *marshal_buffer = NULL;
320         unsigned int marshal_length;
321         FILE *fp = NULL;
322
323         pathGamePythonConfig(marshal_path);
324         marshal_length = saveGamePythonConfig(&marshal_buffer);
325
326         if (marshal_length && marshal_buffer)
327         {
328                 fp = fopen(marshal_path, "wb");
329
330                 if (fp)
331                 {
332                         if (fwrite(marshal_buffer, 1, marshal_length, fp) != marshal_length)
333                                 printf("Warning: could not write marshal data\n");
334
335                         fclose(fp);
336                 } else {
337                         printf("Warning: could not open marshal file\n");
338                 }
339         } else {
340                 printf("Warning: could not create marshal buffer\n");
341         }
342
343         if (marshal_buffer)
344                 delete [] marshal_buffer;
345
346         Py_RETURN_NONE;
347 }
348
349 PyDoc_STRVAR(gPyLoadGlobalDict_doc,
350 "LoadGlobalDict()\n"
351 "Loads bge.logic.globalDict from a file"
352 );
353 static PyObject *gPyLoadGlobalDict(PyObject *)
354 {
355         char marshal_path[512];
356         char *marshal_buffer = NULL;
357         size_t marshal_length;
358         FILE *fp = NULL;
359         int result;
360
361         pathGamePythonConfig(marshal_path);
362
363         fp = fopen(marshal_path, "rb");
364
365         if (fp) {
366                 // obtain file size:
367                 fseek (fp, 0, SEEK_END);
368                 marshal_length = (size_t)ftell(fp);
369                 rewind(fp);
370
371                 marshal_buffer = (char*)malloc (sizeof(char)*marshal_length);
372
373                 result = fread(marshal_buffer, 1, marshal_length, fp);
374
375                 if (result == marshal_length) {
376                         loadGamePythonConfig(marshal_buffer, marshal_length);
377                 } else {
378                         printf("Warning: could not read all of '%s'\n", marshal_path);
379                 }
380
381                 free(marshal_buffer);
382                 fclose(fp);
383         } else {
384                 printf("Warning: could not open '%s'\n", marshal_path);
385         }
386
387         Py_RETURN_NONE;
388 }
389
390 PyDoc_STRVAR(gPyGetProfileInfo_doc,
391 "getProfileInfo()\n"
392 "returns a dictionary with profiling information"
393 );
394 static PyObject *gPyGetProfileInfo(PyObject *)
395 {
396         return gp_KetsjiEngine->GetPyProfileDict();
397 }
398
399 PyDoc_STRVAR(gPySendMessage_doc,
400 "sendMessage(subject, [body, to, from])\n"
401 "sends a message in same manner as a message actuator"
402 " subject = Subject of the message"
403 " body = Message body"
404 " to = Name of object to send the message to"
405 " from = Name of object to send the string from"
406 );
407 static PyObject *gPySendMessage(PyObject *, PyObject *args)
408 {
409         char* subject;
410         char* body = (char *)"";
411         char* to = (char *)"";
412         char* from = (char *)"";
413
414         if (!PyArg_ParseTuple(args, "s|sss:sendMessage", &subject, &body, &to, &from))
415                 return NULL;
416
417         gp_KetsjiScene->GetNetworkScene()->SendMessage(to, from, subject, body);
418
419         Py_RETURN_NONE;
420 }
421
422 // this gets a pointer to an array filled with floats
423 static PyObject *gPyGetSpectrum(PyObject *)
424 {
425         PyObject *resultlist = PyList_New(512);
426
427         for (int index = 0; index < 512; index++)
428         {
429                 PyList_SET_ITEM(resultlist, index, PyFloat_FromDouble(0.0));
430         }
431
432         return resultlist;
433 }
434
435 static PyObject *gPySetLogicTicRate(PyObject *, PyObject *args)
436 {
437         float ticrate;
438         if (!PyArg_ParseTuple(args, "f:setLogicTicRate", &ticrate))
439                 return NULL;
440         
441         KX_KetsjiEngine::SetTicRate(ticrate);
442         Py_RETURN_NONE;
443 }
444
445 static PyObject *gPyGetLogicTicRate(PyObject *)
446 {
447         return PyFloat_FromDouble(KX_KetsjiEngine::GetTicRate());
448 }
449
450 static PyObject *gPySetExitKey(PyObject *, PyObject *args)
451 {
452         short exitkey;
453         if (!PyArg_ParseTuple(args, "h:setExitKey", &exitkey))
454                 return NULL;
455         KX_KetsjiEngine::SetExitKey(exitkey);
456         Py_RETURN_NONE;
457 }
458
459 static PyObject *gPyGetExitKey(PyObject *)
460 {
461         return PyLong_FromLong(KX_KetsjiEngine::GetExitKey());
462 }
463
464 static PyObject *gPySetMaxLogicFrame(PyObject *, PyObject *args)
465 {
466         int frame;
467         if (!PyArg_ParseTuple(args, "i:setMaxLogicFrame", &frame))
468                 return NULL;
469         
470         KX_KetsjiEngine::SetMaxLogicFrame(frame);
471         Py_RETURN_NONE;
472 }
473
474 static PyObject *gPyGetMaxLogicFrame(PyObject *)
475 {
476         return PyLong_FromLong(KX_KetsjiEngine::GetMaxLogicFrame());
477 }
478
479 static PyObject *gPySetMaxPhysicsFrame(PyObject *, PyObject *args)
480 {
481         int frame;
482         if (!PyArg_ParseTuple(args, "i:setMaxPhysicsFrame", &frame))
483                 return NULL;
484         
485         KX_KetsjiEngine::SetMaxPhysicsFrame(frame);
486         Py_RETURN_NONE;
487 }
488
489 static PyObject *gPyGetMaxPhysicsFrame(PyObject *)
490 {
491         return PyLong_FromLong(KX_KetsjiEngine::GetMaxPhysicsFrame());
492 }
493
494 static PyObject *gPySetPhysicsTicRate(PyObject *, PyObject *args)
495 {
496         float ticrate;
497         if (!PyArg_ParseTuple(args, "f:setPhysicsTicRate", &ticrate))
498                 return NULL;
499         
500         PHY_GetActiveEnvironment()->SetFixedTimeStep(true,ticrate);
501         Py_RETURN_NONE;
502 }
503 #if 0 // unused
504 static PyObject *gPySetPhysicsDebug(PyObject *, PyObject *args)
505 {
506         int debugMode;
507         if (!PyArg_ParseTuple(args, "i:setPhysicsDebug", &debugMode))
508                 return NULL;
509         
510         PHY_GetActiveEnvironment()->setDebugMode(debugMode);
511         Py_RETURN_NONE;
512 }
513 #endif
514
515
516 static PyObject *gPyGetPhysicsTicRate(PyObject *)
517 {
518         return PyFloat_FromDouble(PHY_GetActiveEnvironment()->GetFixedTimeStep());
519 }
520
521 static PyObject *gPyGetAverageFrameRate(PyObject *)
522 {
523         return PyFloat_FromDouble(KX_KetsjiEngine::GetAverageFrameRate());
524 }
525
526 static PyObject *gPyGetBlendFileList(PyObject *, PyObject *args)
527 {
528         char cpath[sizeof(gp_GamePythonPath)];
529         char *searchpath = NULL;
530         PyObject *list, *value;
531
532         DIR *dp;
533         struct dirent *dirp;
534
535         if (!PyArg_ParseTuple(args, "|s:getBlendFileList", &searchpath))
536                 return NULL;
537
538         list = PyList_New(0);
539         
540         if (searchpath) {
541                 BLI_strncpy(cpath, searchpath, FILE_MAX);
542                 BLI_path_abs(cpath, gp_GamePythonPath);
543         } else {
544                 /* Get the dir only */
545                 BLI_split_dir_part(gp_GamePythonPath, cpath, sizeof(cpath));
546         }
547
548         if ((dp  = opendir(cpath)) == NULL) {
549                 /* todo, show the errno, this shouldnt happen anyway if the blendfile is readable */
550                 fprintf(stderr, "Could not read directory (%s) failed, code %d (%s)\n", cpath, errno, strerror(errno));
551                 return list;
552         }
553         
554         while ((dirp = readdir(dp)) != NULL) {
555                 if (BLI_testextensie(dirp->d_name, ".blend")) {
556                         value = PyC_UnicodeFromByte(dirp->d_name);
557                         PyList_Append(list, value);
558                         Py_DECREF(value);
559                 }
560         }
561         
562         closedir(dp);
563         return list;
564 }
565
566 PyDoc_STRVAR(gPyAddScene_doc,
567 "addScene(name, [overlay])\n"
568 "Adds a scene to the game engine.\n"
569 " name = Name of the scene\n"
570 " overlay = Overlay or underlay"
571 );
572 static PyObject *gPyAddScene(PyObject *, PyObject *args)
573 {
574         char* name;
575         int overlay = 1;
576         
577         if (!PyArg_ParseTuple(args, "s|i:addScene", &name , &overlay))
578                 return NULL;
579         
580         gp_KetsjiEngine->ConvertAndAddScene(name, (overlay != 0));
581
582         Py_RETURN_NONE;
583 }
584
585 PyDoc_STRVAR(gPyGetCurrentScene_doc,
586 "getCurrentScene()\n"
587 "Gets a reference to the current scene."
588 );
589 static PyObject *gPyGetCurrentScene(PyObject *self)
590 {
591         return gp_KetsjiScene->GetProxy();
592 }
593
594 PyDoc_STRVAR(gPyGetSceneList_doc,
595 "getSceneList()\n"
596 "Return a list of converted scenes."
597 );
598 static PyObject *gPyGetSceneList(PyObject *self)
599 {
600         KX_KetsjiEngine* m_engine = KX_GetActiveEngine();
601         PyObject *list;
602         KX_SceneList* scenes = m_engine->CurrentScenes();
603         int numScenes = scenes->size();
604         int i;
605         
606         list = PyList_New(numScenes);
607         
608         for (i=0;i<numScenes;i++)
609         {
610                 KX_Scene* scene = scenes->at(i);
611                 PyList_SET_ITEM(list, i, scene->GetProxy());
612         }
613
614         return list;
615 }
616
617 static PyObject *pyPrintStats(PyObject *,PyObject *,PyObject *)
618 {
619         gp_KetsjiScene->GetSceneConverter()->PrintStats();
620         Py_RETURN_NONE;
621 }
622
623 static PyObject *pyPrintExt(PyObject *,PyObject *,PyObject *)
624 {
625 #define pprint(x) std::cout << x << std::endl;
626         bool count=0;
627         bool support=0;
628         pprint("Supported Extensions...");
629         pprint(" GL_ARB_shader_objects supported?       "<< (GLEW_ARB_shader_objects?"yes.":"no."));
630         count = 1;
631
632         support= GLEW_ARB_vertex_shader;
633         pprint(" GL_ARB_vertex_shader supported?        "<< (support?"yes.":"no."));
634         count = 1;
635         if (support) {
636                 pprint(" ----------Details----------");
637                 int max=0;
638                 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB, (GLint*)&max);
639                 pprint("  Max uniform components." << max);
640
641                 glGetIntegerv(GL_MAX_VARYING_FLOATS_ARB, (GLint*)&max);
642                 pprint("  Max varying floats." << max);
643
644                 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, (GLint*)&max);
645                 pprint("  Max vertex texture units." << max);
646         
647                 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB, (GLint*)&max);
648                 pprint("  Max combined texture units." << max);
649                 pprint("");
650         }
651
652         support=GLEW_ARB_fragment_shader;
653         pprint(" GL_ARB_fragment_shader supported?      "<< (support?"yes.":"no."));
654         count = 1;
655         if (support) {
656                 pprint(" ----------Details----------");
657                 int max=0;
658                 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS_ARB, (GLint*)&max);
659                 pprint("  Max uniform components." << max);
660                 pprint("");
661         }
662
663         support = GLEW_ARB_texture_cube_map;
664         pprint(" GL_ARB_texture_cube_map supported?     "<< (support?"yes.":"no."));
665         count = 1;
666         if (support) {
667                 pprint(" ----------Details----------");
668                 int size=0;
669                 glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB, (GLint*)&size);
670                 pprint("  Max cubemap size." << size);
671                 pprint("");
672         }
673
674         support = GLEW_ARB_multitexture;
675         count = 1;
676         pprint(" GL_ARB_multitexture supported?         "<< (support?"yes.":"no."));
677         if (support) {
678                 pprint(" ----------Details----------");
679                 int units=0;
680                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, (GLint*)&units);
681                 pprint("  Max texture units available.  " << units);
682                 pprint("");
683         }
684
685         pprint(" GL_ARB_texture_env_combine supported?  "<< (GLEW_ARB_texture_env_combine?"yes.":"no."));
686         count = 1;
687
688         if (!count)
689                 pprint("No extenstions are used in this build");
690
691         Py_RETURN_NONE;
692 }
693
694 static PyObject *gLibLoad(PyObject *, PyObject *args, PyObject *kwds)
695 {
696         KX_Scene *kx_scene= gp_KetsjiScene;
697         char *path;
698         char *group;
699         Py_buffer py_buffer;
700         py_buffer.buf = NULL;
701         char *err_str= NULL;
702         KX_LibLoadStatus *status = NULL;
703
704         short options=0;
705         int load_actions=0, verbose=0, load_scripts=1, async=0;
706
707         static const char *kwlist[] = {"path", "group", "buffer", "load_actions", "verbose", "load_scripts", "async", NULL};
708         
709         if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss|y*iiIi:LibLoad", const_cast<char**>(kwlist),
710                                                                         &path, &group, &py_buffer, &load_actions, &verbose, &load_scripts, &async))
711                 return NULL;
712
713         /* setup options */
714         if (load_actions != 0)
715                 options |= KX_BlenderSceneConverter::LIB_LOAD_LOAD_ACTIONS;
716         if (verbose != 0)
717                 options |= KX_BlenderSceneConverter::LIB_LOAD_VERBOSE;
718         if (load_scripts != 0)
719                 options |= KX_BlenderSceneConverter::LIB_LOAD_LOAD_SCRIPTS;
720         if (async != 0)
721                 options |= KX_BlenderSceneConverter::LIB_LOAD_ASYNC;
722
723         if (!py_buffer.buf)
724         {
725                 char abs_path[FILE_MAX];
726                 // Make the path absolute
727                 BLI_strncpy(abs_path, path, sizeof(abs_path));
728                 BLI_path_abs(abs_path, gp_GamePythonPath);
729
730                 if ((status=kx_scene->GetSceneConverter()->LinkBlendFilePath(abs_path, group, kx_scene, &err_str, options))) {
731                         return status->GetProxy();
732                 }
733         }
734         else
735         {
736
737                 if ((status=kx_scene->GetSceneConverter()->LinkBlendFileMemory(py_buffer.buf, py_buffer.len, path, group, kx_scene, &err_str, options)))        {
738                         PyBuffer_Release(&py_buffer);
739                         return status->GetProxy();
740                 }
741
742                 PyBuffer_Release(&py_buffer);
743         }
744         
745         if (err_str) {
746                 PyErr_SetString(PyExc_ValueError, err_str);
747                 return NULL;
748         }
749         
750         Py_RETURN_FALSE;
751 }
752
753 static PyObject *gLibNew(PyObject *, PyObject *args)
754 {
755         KX_Scene *kx_scene= gp_KetsjiScene;
756         char *path;
757         char *group;
758         const char *name;
759         PyObject *names;
760         int idcode;
761
762         if (!PyArg_ParseTuple(args,"ssO!:LibNew",&path, &group, &PyList_Type, &names))
763                 return NULL;
764         
765         if (kx_scene->GetSceneConverter()->GetMainDynamicPath(path))
766         {
767                 PyErr_SetString(PyExc_KeyError, "the name of the path given exists");
768                 return NULL;
769         }
770         
771         idcode= BKE_idcode_from_name(group);
772         if (idcode==0) {
773                 PyErr_Format(PyExc_ValueError, "invalid group given \"%s\"", group);
774                 return NULL;
775         }
776         
777         Main *maggie=BKE_main_new();
778         kx_scene->GetSceneConverter()->GetMainDynamic().push_back(maggie);
779         strncpy(maggie->name, path, sizeof(maggie->name)-1);
780         
781         /* Copy the object into main */
782         if (idcode==ID_ME) {
783                 PyObject *ret= PyList_New(0);
784                 PyObject *item;
785                 for (Py_ssize_t i= 0; i < PyList_GET_SIZE(names); i++) {
786                         name= _PyUnicode_AsString(PyList_GET_ITEM(names, i));
787                         if (name) {
788                                 RAS_MeshObject *meshobj= kx_scene->GetSceneConverter()->ConvertMeshSpecial(kx_scene, maggie, name);
789                                 if (meshobj) {
790                                         KX_MeshProxy* meshproxy = new KX_MeshProxy(meshobj);
791                                         item= meshproxy->NewProxy(true);
792                                         PyList_Append(ret, item);
793                                         Py_DECREF(item);
794                                 }
795                         }
796                         else {
797                                 PyErr_Clear(); /* wasnt a string, ignore for now */
798                         }
799                 }
800                 
801                 return ret;
802         }
803         else {
804                 PyErr_Format(PyExc_ValueError, "only \"Mesh\" group currently supported");
805                 return NULL;
806         }
807         
808         Py_RETURN_NONE;
809 }
810
811 static PyObject *gLibFree(PyObject *, PyObject *args)
812 {
813         KX_Scene *kx_scene= gp_KetsjiScene;
814         char *path;
815
816         if (!PyArg_ParseTuple(args,"s:LibFree",&path))
817                 return NULL;
818
819         if (kx_scene->GetSceneConverter()->FreeBlendFile(path))
820         {
821                 Py_RETURN_TRUE;
822         }
823         else {
824                 Py_RETURN_FALSE;
825         }
826 }
827
828 static PyObject *gLibList(PyObject *, PyObject *args)
829 {
830         vector<Main*> &dynMaggie = gp_KetsjiScene->GetSceneConverter()->GetMainDynamic();
831         int i= 0;
832         PyObject *list= PyList_New(dynMaggie.size());
833         
834         for (vector<Main*>::iterator it=dynMaggie.begin(); !(it==dynMaggie.end()); it++)
835         {
836                 PyList_SET_ITEM(list, i++, PyUnicode_FromString( (*it)->name) );
837         }
838         
839         return list;
840 }
841
842 struct PyNextFrameState pynextframestate;
843 static PyObject *gPyNextFrame(PyObject *)
844 {
845         if (pynextframestate.func == NULL) Py_RETURN_NONE;
846         if (pynextframestate.state == NULL) Py_RETURN_NONE; //should never happen; raise exception instead?
847
848         if (pynextframestate.func(pynextframestate.state)) //nonzero = stop
849         { 
850                 Py_RETURN_TRUE;
851         }
852         else // 0 = go on
853         {
854                 Py_RETURN_FALSE;
855         }
856 }
857
858
859 static struct PyMethodDef game_methods[] = {
860         {"expandPath", (PyCFunction)gPyExpandPath, METH_VARARGS, (const char *)gPyExpandPath_doc},
861         {"startGame", (PyCFunction)gPyStartGame, METH_VARARGS, (const char *)gPyStartGame_doc},
862         {"endGame", (PyCFunction)gPyEndGame, METH_NOARGS, (const char *)gPyEndGame_doc},
863         {"restartGame", (PyCFunction)gPyRestartGame, METH_NOARGS, (const char *)gPyRestartGame_doc},
864         {"saveGlobalDict", (PyCFunction)gPySaveGlobalDict, METH_NOARGS, (const char *)gPySaveGlobalDict_doc},
865         {"loadGlobalDict", (PyCFunction)gPyLoadGlobalDict, METH_NOARGS, (const char *)gPyLoadGlobalDict_doc},
866         {"sendMessage", (PyCFunction)gPySendMessage, METH_VARARGS, (const char *)gPySendMessage_doc},
867         {"getCurrentController", (PyCFunction) SCA_PythonController::sPyGetCurrentController, METH_NOARGS, SCA_PythonController::sPyGetCurrentController__doc__},
868         {"getCurrentScene", (PyCFunction) gPyGetCurrentScene, METH_NOARGS, gPyGetCurrentScene_doc},
869         {"getSceneList", (PyCFunction) gPyGetSceneList, METH_NOARGS, (const char *)gPyGetSceneList_doc},
870         {"addScene", (PyCFunction)gPyAddScene, METH_VARARGS, (const char *)gPyAddScene_doc},
871         {"getRandomFloat",(PyCFunction) gPyGetRandomFloat, METH_NOARGS, (const char *)gPyGetRandomFloat_doc},
872         {"setGravity",(PyCFunction) gPySetGravity, METH_O, (const char *)"set Gravitation"},
873         {"getSpectrum",(PyCFunction) gPyGetSpectrum, METH_NOARGS, (const char *)"get audio spectrum"},
874         {"getMaxLogicFrame", (PyCFunction) gPyGetMaxLogicFrame, METH_NOARGS, (const char *)"Gets the max number of logic frame per render frame"},
875         {"setMaxLogicFrame", (PyCFunction) gPySetMaxLogicFrame, METH_VARARGS, (const char *)"Sets the max number of logic frame per render frame"},
876         {"getMaxPhysicsFrame", (PyCFunction) gPyGetMaxPhysicsFrame, METH_NOARGS, (const char *)"Gets the max number of physics frame per render frame"},
877         {"setMaxPhysicsFrame", (PyCFunction) gPySetMaxPhysicsFrame, METH_VARARGS, (const char *)"Sets the max number of physics farme per render frame"},
878         {"getLogicTicRate", (PyCFunction) gPyGetLogicTicRate, METH_NOARGS, (const char *)"Gets the logic tic rate"},
879         {"setLogicTicRate", (PyCFunction) gPySetLogicTicRate, METH_VARARGS, (const char *)"Sets the logic tic rate"},
880         {"getPhysicsTicRate", (PyCFunction) gPyGetPhysicsTicRate, METH_NOARGS, (const char *)"Gets the physics tic rate"},
881         {"setPhysicsTicRate", (PyCFunction) gPySetPhysicsTicRate, METH_VARARGS, (const char *)"Sets the physics tic rate"},
882         {"getExitKey", (PyCFunction) gPyGetExitKey, METH_NOARGS, (const char *)"Gets the key used to exit the game engine"},
883         {"setExitKey", (PyCFunction) gPySetExitKey, METH_VARARGS, (const char *)"Sets the key used to exit the game engine"},
884         {"getAverageFrameRate", (PyCFunction) gPyGetAverageFrameRate, METH_NOARGS, (const char *)"Gets the estimated average frame rate"},
885         {"getBlendFileList", (PyCFunction)gPyGetBlendFileList, METH_VARARGS, (const char *)"Gets a list of blend files in the same directory as the current blend file"},
886         {"PrintGLInfo", (PyCFunction)pyPrintExt, METH_NOARGS, (const char *)"Prints GL Extension Info"},
887         {"PrintMemInfo", (PyCFunction)pyPrintStats, METH_NOARGS, (const char *)"Print engine statistics"},
888         {"NextFrame", (PyCFunction)gPyNextFrame, METH_NOARGS, (const char *)"Render next frame (if Python has control)"},
889         {"getProfileInfo", (PyCFunction)gPyGetProfileInfo, METH_NOARGS, gPyGetProfileInfo_doc},
890         /* library functions */
891         {"LibLoad", (PyCFunction)gLibLoad, METH_VARARGS|METH_KEYWORDS, (const char *)""},
892         {"LibNew", (PyCFunction)gLibNew, METH_VARARGS, (const char *)""},
893         {"LibFree", (PyCFunction)gLibFree, METH_VARARGS, (const char *)""},
894         {"LibList", (PyCFunction)gLibList, METH_VARARGS, (const char *)""},
895         
896         {NULL, (PyCFunction) NULL, 0, NULL }
897 };
898
899 static PyObject *gPyGetWindowHeight(PyObject *, PyObject *args)
900 {
901         return PyLong_FromLong((gp_Canvas ? gp_Canvas->GetHeight() : 0));
902 }
903
904
905
906 static PyObject *gPyGetWindowWidth(PyObject *, PyObject *args)
907 {
908         return PyLong_FromLong((gp_Canvas ? gp_Canvas->GetWidth() : 0));
909 }
910
911
912
913 // temporarility visibility thing, will be moved to rasterizer/renderer later
914 bool gUseVisibilityTemp = false;
915
916 static PyObject *gPyEnableVisibility(PyObject *, PyObject *args)
917 {
918         int visible;
919         if (!PyArg_ParseTuple(args,"i:enableVisibility",&visible))
920                 return NULL;
921         
922         gUseVisibilityTemp = (visible != 0);
923         Py_RETURN_NONE;
924 }
925
926
927
928 static PyObject *gPyShowMouse(PyObject *, PyObject *args)
929 {
930         int visible;
931         if (!PyArg_ParseTuple(args,"i:showMouse",&visible))
932                 return NULL;
933         
934         if (visible)
935         {
936                 if (gp_Canvas)
937                         gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
938         } else
939         {
940                 if (gp_Canvas)
941                         gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
942         }
943         
944         Py_RETURN_NONE;
945 }
946
947
948
949 static PyObject *gPySetMousePosition(PyObject *, PyObject *args)
950 {
951         int x,y;
952         if (!PyArg_ParseTuple(args,"ii:setMousePosition",&x,&y))
953                 return NULL;
954         
955         if (gp_Canvas)
956                 gp_Canvas->SetMousePosition(x,y);
957         
958         Py_RETURN_NONE;
959 }
960
961 static PyObject *gPySetEyeSeparation(PyObject *, PyObject *args)
962 {
963         float sep;
964         if (!PyArg_ParseTuple(args, "f:setEyeSeparation", &sep))
965                 return NULL;
966
967         if (!gp_Rasterizer) {
968                 PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setEyeSeparation(float), Rasterizer not available");
969                 return NULL;
970         }
971         
972         gp_Rasterizer->SetEyeSeparation(sep);
973         
974         Py_RETURN_NONE;
975 }
976
977 static PyObject *gPyGetEyeSeparation(PyObject *)
978 {
979         if (!gp_Rasterizer) {
980                 PyErr_SetString(PyExc_RuntimeError, "Rasterizer.getEyeSeparation(), Rasterizer not available");
981                 return NULL;
982         }
983         
984         return PyFloat_FromDouble(gp_Rasterizer->GetEyeSeparation());
985 }
986
987 static PyObject *gPySetFocalLength(PyObject *, PyObject *args)
988 {
989         float focus;
990         if (!PyArg_ParseTuple(args, "f:setFocalLength", &focus))
991                 return NULL;
992         
993         if (!gp_Rasterizer) {
994                 PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setFocalLength(float), Rasterizer not available");
995                 return NULL;
996         }
997
998         gp_Rasterizer->SetFocalLength(focus);
999         
1000         Py_RETURN_NONE;
1001 }
1002
1003 static PyObject *gPyGetFocalLength(PyObject *, PyObject *, PyObject *)
1004 {
1005         if (!gp_Rasterizer) {
1006                 PyErr_SetString(PyExc_RuntimeError, "Rasterizer.getFocalLength(), Rasterizer not available");
1007                 return NULL;
1008         }
1009         
1010         return PyFloat_FromDouble(gp_Rasterizer->GetFocalLength());
1011         
1012         Py_RETURN_NONE;
1013 }
1014
1015 static PyObject *gPyGetStereoEye(PyObject *, PyObject *, PyObject *)
1016 {
1017         int flag = RAS_IRasterizer::RAS_STEREO_LEFTEYE;
1018
1019         if (!gp_Rasterizer) {
1020                 PyErr_SetString(PyExc_RuntimeError, "Rasterizer.getStereoEye(), Rasterizer not available");
1021                 return NULL;
1022         }
1023
1024         if (gp_Rasterizer->Stereo())
1025                 flag = gp_Rasterizer->GetEye();
1026
1027         return PyLong_FromLong(flag);
1028 }
1029
1030 static PyObject *gPySetBackgroundColor(PyObject *, PyObject *value)
1031 {
1032         MT_Vector4 vec;
1033         if (!PyVecTo(value, vec))
1034                 return NULL;
1035
1036         KX_WorldInfo *wi = gp_KetsjiScene->GetWorldInfo();
1037         if (!wi->hasWorld()) {
1038                 PyErr_SetString(PyExc_RuntimeError, "bge.render.SetBackgroundColor(color), World not available");
1039                 return NULL;
1040         }
1041
1042         ShowDeprecationWarning("setBackgroundColor()", "KX_WorldInfo.background_color");
1043         wi->setBackColor((float)vec[0], (float)vec[1], (float)vec[2]);
1044
1045         Py_RETURN_NONE;
1046 }
1047
1048 static PyObject *gPyMakeScreenshot(PyObject *, PyObject *args)
1049 {
1050         char* filename;
1051         if (!PyArg_ParseTuple(args,"s:makeScreenshot",&filename))
1052                 return NULL;
1053         
1054         if (gp_Canvas)
1055         {
1056                 gp_Canvas->MakeScreenShot(filename);
1057         }
1058         
1059         Py_RETURN_NONE;
1060 }
1061
1062 static PyObject *gPyEnableMotionBlur(PyObject *, PyObject *args)
1063 {
1064         float motionblurvalue;
1065         if (!PyArg_ParseTuple(args,"f:enableMotionBlur",&motionblurvalue))
1066                 return NULL;
1067         
1068         if (!gp_Rasterizer) {
1069                 PyErr_SetString(PyExc_RuntimeError, "Rasterizer.enableMotionBlur(float), Rasterizer not available");
1070                 return NULL;
1071         }
1072         
1073         gp_Rasterizer->EnableMotionBlur(motionblurvalue);
1074         
1075         Py_RETURN_NONE;
1076 }
1077
1078 static PyObject *gPyDisableMotionBlur(PyObject *)
1079 {
1080         if (!gp_Rasterizer) {
1081                 PyErr_SetString(PyExc_RuntimeError, "Rasterizer.disableMotionBlur(), Rasterizer not available");
1082                 return NULL;
1083         }
1084         
1085         gp_Rasterizer->DisableMotionBlur();
1086         
1087         Py_RETURN_NONE;
1088 }
1089
1090 static int getGLSLSettingFlag(const char *setting)
1091 {
1092         if (strcmp(setting, "lights") == 0)
1093                 return GAME_GLSL_NO_LIGHTS;
1094         else if (strcmp(setting, "shaders") == 0)
1095                 return GAME_GLSL_NO_SHADERS;
1096         else if (strcmp(setting, "shadows") == 0)
1097                 return GAME_GLSL_NO_SHADOWS;
1098         else if (strcmp(setting, "ramps") == 0)
1099                 return GAME_GLSL_NO_RAMPS;
1100         else if (strcmp(setting, "nodes") == 0)
1101                 return GAME_GLSL_NO_NODES;
1102         else if (strcmp(setting, "extra_textures") == 0)
1103                 return GAME_GLSL_NO_EXTRA_TEX;
1104         else
1105                 return -1;
1106 }
1107
1108 static PyObject *gPySetGLSLMaterialSetting(PyObject *,
1109                                            PyObject *args,
1110                                            PyObject *)
1111 {
1112         GlobalSettings *gs= gp_KetsjiEngine->GetGlobalSettings();
1113         char *setting;
1114         int enable, flag, sceneflag;
1115
1116         if (!PyArg_ParseTuple(args,"si:setGLSLMaterialSetting",&setting,&enable))
1117                 return NULL;
1118         
1119         flag = getGLSLSettingFlag(setting);
1120         
1121         if (flag == -1) {
1122                 PyErr_SetString(PyExc_ValueError, "Rasterizer.setGLSLMaterialSetting(string): glsl setting is not known");
1123                 return NULL;
1124         }
1125
1126         sceneflag= gs->glslflag;
1127         
1128         if (enable)
1129                 gs->glslflag &= ~flag;
1130         else
1131                 gs->glslflag |= flag;
1132
1133         /* temporarily store the glsl settings in the scene for the GLSL materials */
1134         GameData *gm= &(gp_KetsjiScene->GetBlenderScene()->gm);
1135         gm->flag = gs->glslflag;
1136
1137         /* display lists and GLSL materials need to be remade */
1138         if (sceneflag != gs->glslflag) {
1139                 GPU_materials_free();
1140                 if (gp_KetsjiEngine) {
1141                         KX_SceneList *scenes = gp_KetsjiEngine->CurrentScenes();
1142                         KX_SceneList::iterator it;
1143
1144                         for (it=scenes->begin(); it!=scenes->end(); it++)
1145                                 if ((*it)->GetBucketManager()) {
1146                                         (*it)->GetBucketManager()->ReleaseDisplayLists();
1147                                         (*it)->GetBucketManager()->ReleaseMaterials();
1148                                 }
1149                 }
1150         }
1151
1152         Py_RETURN_NONE;
1153 }
1154
1155 static PyObject *gPyGetGLSLMaterialSetting(PyObject *,
1156                                            PyObject *args,
1157                                            PyObject *)
1158 {
1159         GlobalSettings *gs= gp_KetsjiEngine->GetGlobalSettings();
1160         char *setting;
1161         int enabled = 0, flag;
1162
1163         if (!PyArg_ParseTuple(args,"s:getGLSLMaterialSetting",&setting))
1164                 return NULL;
1165         
1166         flag = getGLSLSettingFlag(setting);
1167         
1168         if (flag == -1) {
1169                 PyErr_SetString(PyExc_ValueError, "Rasterizer.getGLSLMaterialSetting(string): glsl setting is not known");
1170                 return NULL;
1171         }
1172
1173         enabled = ((gs->glslflag & flag) != 0);
1174         return PyLong_FromLong(enabled);
1175 }
1176
1177 #define KX_BLENDER_MULTITEX_MATERIAL    1
1178 #define KX_BLENDER_GLSL_MATERIAL                2
1179
1180 static PyObject *gPySetMaterialType(PyObject *,
1181                                     PyObject *args,
1182                                     PyObject *)
1183 {
1184         GlobalSettings *gs= gp_KetsjiEngine->GetGlobalSettings();
1185         int type;
1186
1187         if (!PyArg_ParseTuple(args,"i:setMaterialType",&type))
1188                 return NULL;
1189
1190         if (type == KX_BLENDER_GLSL_MATERIAL)
1191                 gs->matmode= GAME_MAT_GLSL;
1192         else if (type == KX_BLENDER_MULTITEX_MATERIAL)
1193                 gs->matmode= GAME_MAT_MULTITEX;
1194         else {
1195                 PyErr_SetString(PyExc_ValueError, "Rasterizer.setMaterialType(int): material type is not known");
1196                 return NULL;
1197         }
1198
1199         Py_RETURN_NONE;
1200 }
1201
1202 static PyObject *gPyGetMaterialType(PyObject *)
1203 {
1204         GlobalSettings *gs= gp_KetsjiEngine->GetGlobalSettings();
1205         int flag;
1206
1207         if (gs->matmode == GAME_MAT_GLSL)
1208                 flag = KX_BLENDER_GLSL_MATERIAL;
1209         else
1210                 flag = KX_BLENDER_MULTITEX_MATERIAL;
1211         
1212         return PyLong_FromLong(flag);
1213 }
1214
1215 static PyObject *gPySetAnisotropicFiltering(PyObject *, PyObject *args)
1216 {
1217         short level;
1218
1219         if (!PyArg_ParseTuple(args, "h:setAnisotropicFiltering", &level))
1220                 return NULL;
1221
1222         if (level != 1 && level != 2 && level != 4 && level != 8 && level != 16) {
1223                 PyErr_SetString(PyExc_ValueError, "Rasterizer.setAnisotropicFiltering(level): Expected value of 1, 2, 4, 8, or 16 for value");
1224                 return NULL;
1225         }
1226
1227         gp_Rasterizer->SetAnisotropicFiltering(level);
1228
1229         Py_RETURN_NONE;
1230 }
1231
1232 static PyObject *gPyGetAnisotropicFiltering(PyObject *, PyObject *args)
1233 {
1234         return PyLong_FromLong(gp_Rasterizer->GetAnisotropicFiltering());
1235 }
1236
1237 static PyObject *gPyDrawLine(PyObject *, PyObject *args)
1238 {
1239         PyObject *ob_from;
1240         PyObject *ob_to;
1241         PyObject *ob_color;
1242
1243         if (!gp_Rasterizer) {
1244                 PyErr_SetString(PyExc_RuntimeError, "Rasterizer.drawLine(obFrom, obTo, color): Rasterizer not available");
1245                 return NULL;
1246         }
1247
1248         if (!PyArg_ParseTuple(args,"OOO:drawLine",&ob_from,&ob_to,&ob_color))
1249                 return NULL;
1250
1251         MT_Vector3 from;
1252         MT_Vector3 to;
1253         MT_Vector3 color;
1254         if (!PyVecTo(ob_from, from))
1255                 return NULL;
1256         if (!PyVecTo(ob_to, to))
1257                 return NULL;
1258         if (!PyVecTo(ob_color, color))
1259                 return NULL;
1260
1261         gp_Rasterizer->DrawDebugLine(from,to,color);
1262         
1263         Py_RETURN_NONE;
1264 }
1265
1266 static PyObject *gPySetWindowSize(PyObject *, PyObject *args)
1267 {
1268         int width, height;
1269         if (!PyArg_ParseTuple(args, "ii:resize", &width, &height))
1270                 return NULL;
1271
1272         gp_Canvas->ResizeWindow(width, height);
1273         Py_RETURN_NONE;
1274 }
1275
1276 static PyObject *gPySetFullScreen(PyObject *, PyObject *value)
1277 {
1278         gp_Canvas->SetFullScreen(PyObject_IsTrue(value));
1279         Py_RETURN_NONE;
1280 }
1281
1282 static PyObject *gPyGetFullScreen(PyObject *)
1283 {
1284         return PyBool_FromLong(gp_Canvas->GetFullScreen());
1285 }
1286
1287 static PyObject *gPySetMipmapping(PyObject *, PyObject *args)
1288 {
1289         int val = 0;
1290
1291         if (!PyArg_ParseTuple(args, "i:setMipmapping", &val))
1292                 return NULL;
1293
1294         if (val < 0 || val > RAS_IRasterizer::RAS_MIPMAP_MAX) {
1295                 PyErr_SetString(PyExc_ValueError, "Rasterizer.setMipmapping(val): invalid mipmaping option");
1296                 return NULL;
1297         }
1298
1299         if (!gp_Rasterizer) {
1300                 PyErr_SetString(PyExc_RuntimeError, "Rasterizer.setMipmapping(val): Rasterizer not available");
1301                 return NULL;
1302         }
1303
1304         gp_Rasterizer->SetMipmapping((RAS_IRasterizer::MipmapOption)val);
1305         Py_RETURN_NONE;
1306 }
1307
1308 static PyObject *gPyGetMipmapping(PyObject *)
1309 {
1310         if (!gp_Rasterizer) {
1311                 PyErr_SetString(PyExc_RuntimeError, "Rasterizer.getMipmapping(): Rasterizer not available");
1312                 return NULL;
1313         }
1314         return PyLong_FromLong(gp_Rasterizer->GetMipmapping());
1315 }
1316
1317 static PyObject *gPySetVsync(PyObject *, PyObject *args)
1318 {
1319         int interval;
1320
1321         if (!PyArg_ParseTuple(args, "i:setVsync", &interval))
1322                 return NULL;
1323
1324         if (interval < 0 || interval > VSYNC_ADAPTIVE) {
1325                 PyErr_SetString(PyExc_ValueError, "Rasterizer.setVsync(value): value must be VSYNC_OFF, VSYNC_ON, or VSYNC_ADAPTIVE");
1326                 return NULL;
1327         }
1328
1329         if (interval == VSYNC_ADAPTIVE)
1330                 interval = -1;
1331         gp_Canvas->SetSwapInterval((interval == VSYNC_ON) ? 1 : 0);
1332         Py_RETURN_NONE;
1333 }
1334
1335 static PyObject *gPyGetVsync(PyObject *)
1336 {
1337         int interval = 0;
1338         gp_Canvas->GetSwapInterval(interval);
1339         return PyLong_FromLong(interval);
1340 }
1341
1342 static PyObject *gPyShowFramerate(PyObject *, PyObject *args)
1343 {
1344         int visible;
1345         if (!PyArg_ParseTuple(args,"i:showFramerate",&visible))
1346                 return NULL;
1347
1348         if (visible && gp_KetsjiEngine)
1349                 gp_KetsjiEngine->SetShowFramerate(true);
1350         else
1351                 gp_KetsjiEngine->SetShowFramerate(false);
1352
1353         Py_RETURN_NONE;
1354 }
1355
1356 static PyObject *gPyShowProfile(PyObject *, PyObject *args)
1357 {
1358         int visible;
1359         if (!PyArg_ParseTuple(args,"i:showProfile",&visible))
1360                 return NULL;
1361
1362         if (visible && gp_KetsjiEngine)
1363                 gp_KetsjiEngine->SetShowProfile(true);
1364         else
1365                 gp_KetsjiEngine->SetShowProfile(false);
1366
1367         Py_RETURN_NONE;
1368 }
1369
1370 static PyObject *gPyShowProperties(PyObject *, PyObject *args)
1371 {
1372         int visible;
1373         if (!PyArg_ParseTuple(args,"i:showProperties",&visible))
1374                 return NULL;
1375
1376         if (visible && gp_KetsjiEngine)
1377                 gp_KetsjiEngine->SetShowProperties(true);
1378         else
1379                 gp_KetsjiEngine->SetShowProperties(false);
1380
1381         Py_RETURN_NONE;
1382 }
1383
1384 static PyObject *gPyAutoDebugList(PyObject *, PyObject *args)
1385 {
1386         int add;
1387         if (!PyArg_ParseTuple(args,"i:autoAddProperties",&add))
1388                 return NULL;
1389
1390         if (add && gp_KetsjiEngine)
1391                 gp_KetsjiEngine->SetAutoAddDebugProperties(true);
1392         else
1393                 gp_KetsjiEngine->SetAutoAddDebugProperties(false);
1394
1395         Py_RETURN_NONE;
1396 }
1397
1398 static PyObject *gPyClearDebugList(PyObject *)
1399 {
1400         if (gp_KetsjiScene)
1401                 gp_KetsjiScene->RemoveAllDebugProperties();
1402
1403         Py_RETURN_NONE;
1404 }
1405
1406 PyDoc_STRVAR(Rasterizer_module_documentation,
1407 "This is the Python API for the game engine of Rasterizer"
1408 );
1409
1410 static struct PyMethodDef rasterizer_methods[] = {
1411         {"getWindowWidth",(PyCFunction) gPyGetWindowWidth,
1412          METH_VARARGS, "getWindowWidth doc"},
1413         {"getWindowHeight",(PyCFunction) gPyGetWindowHeight,
1414          METH_VARARGS, "getWindowHeight doc"},
1415         {"makeScreenshot",(PyCFunction)gPyMakeScreenshot,
1416          METH_VARARGS, "make Screenshot doc"},
1417         {"enableVisibility",(PyCFunction) gPyEnableVisibility,
1418          METH_VARARGS, "enableVisibility doc"},
1419         {"showMouse",(PyCFunction) gPyShowMouse,
1420          METH_VARARGS, "showMouse(bool visible)"},
1421         {"setMousePosition",(PyCFunction) gPySetMousePosition,
1422          METH_VARARGS, "setMousePosition(int x,int y)"},
1423         {"setBackgroundColor",(PyCFunction)gPySetBackgroundColor,METH_O,"set Background Color (rgb)"},
1424         {"enableMotionBlur",(PyCFunction)gPyEnableMotionBlur,METH_VARARGS,"enable motion blur"},
1425         {"disableMotionBlur",(PyCFunction)gPyDisableMotionBlur,METH_NOARGS,"disable motion blur"},
1426
1427         {"setEyeSeparation", (PyCFunction) gPySetEyeSeparation, METH_VARARGS, "set the eye separation for stereo mode"},
1428         {"getEyeSeparation", (PyCFunction) gPyGetEyeSeparation, METH_NOARGS, "get the eye separation for stereo mode"},
1429         {"setFocalLength", (PyCFunction) gPySetFocalLength, METH_VARARGS, "set the focal length for stereo mode"},
1430         {"getFocalLength", (PyCFunction) gPyGetFocalLength, METH_VARARGS, "get the focal length for stereo mode"},
1431         {"getStereoEye", (PyCFunction) gPyGetStereoEye, METH_VARARGS, "get the current stereoscopy eye being rendered"},
1432         {"setMaterialMode",(PyCFunction) gPySetMaterialType,
1433          METH_VARARGS, "set the material mode to use for OpenGL rendering"},
1434         {"getMaterialMode",(PyCFunction) gPyGetMaterialType,
1435          METH_NOARGS, "get the material mode being used for OpenGL rendering"},
1436         {"setGLSLMaterialSetting",(PyCFunction) gPySetGLSLMaterialSetting,
1437          METH_VARARGS, "set the state of a GLSL material setting"},
1438         {"getGLSLMaterialSetting",(PyCFunction) gPyGetGLSLMaterialSetting,
1439          METH_VARARGS, "get the state of a GLSL material setting"},
1440         {"setAnisotropicFiltering", (PyCFunction) gPySetAnisotropicFiltering,
1441          METH_VARARGS, "set the anisotropic filtering level (must be one of 1, 2, 4, 8, 16)"},
1442         {"getAnisotropicFiltering", (PyCFunction) gPyGetAnisotropicFiltering,
1443          METH_VARARGS, "get the anisotropic filtering level"},
1444         {"drawLine", (PyCFunction) gPyDrawLine,
1445          METH_VARARGS, "draw a line on the screen"},
1446         {"setWindowSize", (PyCFunction) gPySetWindowSize, METH_VARARGS, ""},
1447         {"setFullScreen", (PyCFunction) gPySetFullScreen, METH_O, ""},
1448         {"getFullScreen", (PyCFunction) gPyGetFullScreen, METH_NOARGS, ""},
1449         {"setMipmapping", (PyCFunction) gPySetMipmapping, METH_VARARGS, ""},
1450         {"getMipmapping", (PyCFunction) gPyGetMipmapping, METH_NOARGS, ""},
1451         {"setVsync", (PyCFunction) gPySetVsync, METH_VARARGS, ""},
1452         {"getVsync", (PyCFunction) gPyGetVsync, METH_NOARGS, ""},
1453         {"showFramerate",(PyCFunction) gPyShowFramerate, METH_VARARGS, "show or hide the framerate"},
1454         {"showProfile",(PyCFunction) gPyShowProfile, METH_VARARGS, "show or hide the profile"},
1455         {"showProperties",(PyCFunction) gPyShowProperties, METH_VARARGS, "show or hide the debug properties"},
1456         {"autoDebugList",(PyCFunction) gPyAutoDebugList, METH_VARARGS, "enable or disable auto adding debug properties to the debug  list"},
1457         {"clearDebugList",(PyCFunction) gPyClearDebugList, METH_NOARGS, "clears the debug property list"},
1458         { NULL, (PyCFunction) NULL, 0, NULL }
1459 };
1460
1461
1462
1463 PyDoc_STRVAR(GameLogic_module_documentation,
1464 "This is the Python API for the game engine of bge.logic"
1465 );
1466
1467 static struct PyModuleDef GameLogic_module_def = {
1468         {}, /* m_base */
1469         "GameLogic",  /* m_name */
1470         GameLogic_module_documentation,  /* m_doc */
1471         0,  /* m_size */
1472         game_methods,  /* m_methods */
1473         0,  /* m_reload */
1474         0,  /* m_traverse */
1475         0,  /* m_clear */
1476         0,  /* m_free */
1477 };
1478
1479 PyMODINIT_FUNC initGameLogicPythonBinding()
1480 {
1481         PyObject *m;
1482         PyObject *d;
1483         PyObject *item; /* temp PyObject *storage */
1484
1485         gUseVisibilityTemp=false;
1486
1487         PyObjectPlus::ClearDeprecationWarning(); /* Not that nice to call here but makes sure warnings are reset between loading scenes */
1488
1489         m = PyModule_Create(&GameLogic_module_def);
1490         PyDict_SetItemString(PySys_GetObject("modules"), GameLogic_module_def.m_name, m);
1491
1492
1493         // Add some symbolic constants to the module
1494         d = PyModule_GetDict(m);
1495         
1496         // can be overwritten later for gameEngine instances that can load new blend files and re-initialize this module
1497         // for now its safe to make sure it exists for other areas such as the web plugin
1498         
1499         PyDict_SetItemString(d, "globalDict", item=PyDict_New()); Py_DECREF(item);
1500
1501         // Add keyboard and mouse attributes to this module
1502         MT_assert(!gp_PythonKeyboard);
1503         gp_PythonKeyboard = new SCA_PythonKeyboard(gp_KetsjiEngine->GetKeyboardDevice());
1504         PyDict_SetItemString(d, "keyboard", gp_PythonKeyboard->NewProxy(true));
1505
1506         MT_assert(!gp_PythonMouse);
1507         gp_PythonMouse = new SCA_PythonMouse(gp_KetsjiEngine->GetMouseDevice(), gp_Canvas);
1508         PyDict_SetItemString(d, "mouse", gp_PythonMouse->NewProxy(true));
1509
1510         PyObject* joylist = PyList_New(JOYINDEX_MAX);
1511         for (int i=0; i<JOYINDEX_MAX; ++i) {
1512                 SCA_Joystick *joy = SCA_Joystick::GetInstance(i);
1513                 PyObject *item;
1514
1515                 if (joy && joy->Connected()) {
1516                         gp_PythonJoysticks[i] = new SCA_PythonJoystick(joy);
1517                         item = gp_PythonJoysticks[i]->NewProxy(true);
1518                 }
1519                 else {
1520                         if (joy) {
1521                                 joy->ReleaseInstance();
1522                         }
1523                         item = Py_None;
1524                 }
1525
1526                 Py_INCREF(item);
1527                 PyList_SET_ITEM(joylist, i, item);
1528         }
1529         PyDict_SetItemString(d, "joysticks", joylist);
1530
1531         ErrorObject = PyUnicode_FromString("GameLogic.error");
1532         PyDict_SetItemString(d, "error", ErrorObject);
1533         Py_DECREF(ErrorObject);
1534         
1535         // XXXX Add constants here
1536         /* To use logic bricks, we need some sort of constants. Here, we associate */
1537         /* constants and sumbolic names. Add them to dictionary d.                 */
1538
1539         /* 1. true and false: needed for everyone                                  */
1540         KX_MACRO_addTypesToDict(d, KX_TRUE,  SCA_ILogicBrick::KX_TRUE);
1541         KX_MACRO_addTypesToDict(d, KX_FALSE, SCA_ILogicBrick::KX_FALSE);
1542
1543         /* 2. Property sensor                                                      */
1544         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_EQUAL,      SCA_PropertySensor::KX_PROPSENSOR_EQUAL);
1545         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_NOTEQUAL,   SCA_PropertySensor::KX_PROPSENSOR_NOTEQUAL);
1546         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_INTERVAL,   SCA_PropertySensor::KX_PROPSENSOR_INTERVAL);
1547         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_CHANGED,    SCA_PropertySensor::KX_PROPSENSOR_CHANGED);
1548         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_EXPRESSION, SCA_PropertySensor::KX_PROPSENSOR_EXPRESSION);
1549         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_LESSTHAN,   SCA_PropertySensor::KX_PROPSENSOR_LESSTHAN);
1550         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_GREATERTHAN, SCA_PropertySensor::KX_PROPSENSOR_GREATERTHAN);
1551
1552         /* 3. Constraint actuator                                                  */
1553         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCX);
1554         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCY);
1555         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCZ);
1556         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTX);
1557         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTY);
1558         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTZ);
1559         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPX);
1560         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPY);
1561         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRPZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRPZ);
1562         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNX);
1563         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNY);
1564         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DIRNZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DIRNZ);
1565         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIX);
1566         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIY);
1567         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ORIZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ORIZ);
1568         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHPX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHPX);
1569         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHPY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHPY);
1570         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHPZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHPZ);
1571         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHNX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHNX);
1572         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHNY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHNY);
1573         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_FHNZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_FHNZ);
1574         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_NORMAL, KX_ConstraintActuator::KX_ACT_CONSTRAINT_NORMAL);
1575         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_MATERIAL, KX_ConstraintActuator::KX_ACT_CONSTRAINT_MATERIAL);
1576         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_PERMANENT, KX_ConstraintActuator::KX_ACT_CONSTRAINT_PERMANENT);
1577         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DISTANCE, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DISTANCE);
1578         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCAL, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCAL);
1579         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_DOROTFH, KX_ConstraintActuator::KX_ACT_CONSTRAINT_DOROTFH);
1580
1581         /* 4. Random distribution types                                            */
1582         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_CONST,      SCA_RandomActuator::KX_RANDOMACT_BOOL_CONST);
1583         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_UNIFORM,    SCA_RandomActuator::KX_RANDOMACT_BOOL_UNIFORM);
1584         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_BERNOUILLI, SCA_RandomActuator::KX_RANDOMACT_BOOL_BERNOUILLI);
1585         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_CONST,       SCA_RandomActuator::KX_RANDOMACT_INT_CONST);
1586         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_UNIFORM,     SCA_RandomActuator::KX_RANDOMACT_INT_UNIFORM);
1587         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_POISSON,     SCA_RandomActuator::KX_RANDOMACT_INT_POISSON);
1588         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_CONST,     SCA_RandomActuator::KX_RANDOMACT_FLOAT_CONST);
1589         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_UNIFORM,   SCA_RandomActuator::KX_RANDOMACT_FLOAT_UNIFORM);
1590         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NORMAL,    SCA_RandomActuator::KX_RANDOMACT_FLOAT_NORMAL);
1591         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL, SCA_RandomActuator::KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL);
1592
1593         /* 5. Sound actuator                                                      */
1594         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYSTOP,              KX_SoundActuator::KX_SOUNDACT_PLAYSTOP);
1595         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYEND,               KX_SoundActuator::KX_SOUNDACT_PLAYEND);
1596         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPSTOP,              KX_SoundActuator::KX_SOUNDACT_LOOPSTOP);
1597         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPEND,               KX_SoundActuator::KX_SOUNDACT_LOOPEND);
1598         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL,     KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL);
1599         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP,     KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP);
1600
1601         /* 6. Action actuator                                                                                                      */
1602         KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PLAY,        ACT_ACTION_PLAY);
1603         KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PINGPONG,    ACT_ACTION_PINGPONG);
1604         KX_MACRO_addTypesToDict(d, KX_ACTIONACT_FLIPPER,     ACT_ACTION_FLIPPER);
1605         KX_MACRO_addTypesToDict(d, KX_ACTIONACT_LOOPSTOP,    ACT_ACTION_LOOP_STOP);
1606         KX_MACRO_addTypesToDict(d, KX_ACTIONACT_LOOPEND,     ACT_ACTION_LOOP_END);
1607         KX_MACRO_addTypesToDict(d, KX_ACTIONACT_PROPERTY,    ACT_ACTION_FROM_PROP);
1608         
1609         /* 7. GL_BlendFunc */
1610         KX_MACRO_addTypesToDict(d, BL_ZERO, GL_ZERO);
1611         KX_MACRO_addTypesToDict(d, BL_ONE, GL_ONE);
1612         KX_MACRO_addTypesToDict(d, BL_SRC_COLOR, GL_SRC_COLOR);
1613         KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
1614         KX_MACRO_addTypesToDict(d, BL_DST_COLOR, GL_DST_COLOR);
1615         KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_DST_COLOR);
1616         KX_MACRO_addTypesToDict(d, BL_SRC_ALPHA, GL_SRC_ALPHA);
1617         KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1618         KX_MACRO_addTypesToDict(d, BL_DST_ALPHA, GL_DST_ALPHA);
1619         KX_MACRO_addTypesToDict(d, BL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
1620         KX_MACRO_addTypesToDict(d, BL_SRC_ALPHA_SATURATE, GL_SRC_ALPHA_SATURATE);
1621
1622
1623         /* 8. UniformTypes */
1624         KX_MACRO_addTypesToDict(d, SHD_TANGENT, BL_Shader::SHD_TANGENT);
1625         KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX, BL_Shader::MODELVIEWMATRIX);
1626         KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_TRANSPOSE, BL_Shader::MODELVIEWMATRIX_TRANSPOSE);
1627         KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_INVERSE, BL_Shader::MODELVIEWMATRIX_INVERSE);
1628         KX_MACRO_addTypesToDict(d, MODELVIEWMATRIX_INVERSETRANSPOSE, BL_Shader::MODELVIEWMATRIX_INVERSETRANSPOSE);
1629         KX_MACRO_addTypesToDict(d, MODELMATRIX, BL_Shader::MODELMATRIX);
1630         KX_MACRO_addTypesToDict(d, MODELMATRIX_TRANSPOSE, BL_Shader::MODELMATRIX_TRANSPOSE);
1631         KX_MACRO_addTypesToDict(d, MODELMATRIX_INVERSE, BL_Shader::MODELMATRIX_INVERSE);
1632         KX_MACRO_addTypesToDict(d, MODELMATRIX_INVERSETRANSPOSE, BL_Shader::MODELMATRIX_INVERSETRANSPOSE);
1633         KX_MACRO_addTypesToDict(d, VIEWMATRIX, BL_Shader::VIEWMATRIX);
1634         KX_MACRO_addTypesToDict(d, VIEWMATRIX_TRANSPOSE, BL_Shader::VIEWMATRIX_TRANSPOSE);
1635         KX_MACRO_addTypesToDict(d, VIEWMATRIX_INVERSE, BL_Shader::VIEWMATRIX_INVERSE);
1636         KX_MACRO_addTypesToDict(d, VIEWMATRIX_INVERSETRANSPOSE, BL_Shader::VIEWMATRIX_INVERSETRANSPOSE);
1637         KX_MACRO_addTypesToDict(d, CAM_POS, BL_Shader::CAM_POS);
1638         KX_MACRO_addTypesToDict(d, CONSTANT_TIMER, BL_Shader::CONSTANT_TIMER);
1639
1640         /* 9. state actuator */
1641         KX_MACRO_addTypesToDict(d, KX_STATE1, (1<<0));
1642         KX_MACRO_addTypesToDict(d, KX_STATE2, (1<<1));
1643         KX_MACRO_addTypesToDict(d, KX_STATE3, (1<<2));
1644         KX_MACRO_addTypesToDict(d, KX_STATE4, (1<<3));
1645         KX_MACRO_addTypesToDict(d, KX_STATE5, (1<<4));
1646         KX_MACRO_addTypesToDict(d, KX_STATE6, (1<<5));
1647         KX_MACRO_addTypesToDict(d, KX_STATE7, (1<<6));
1648         KX_MACRO_addTypesToDict(d, KX_STATE8, (1<<7));
1649         KX_MACRO_addTypesToDict(d, KX_STATE9, (1<<8));
1650         KX_MACRO_addTypesToDict(d, KX_STATE10, (1<<9));
1651         KX_MACRO_addTypesToDict(d, KX_STATE11, (1<<10));
1652         KX_MACRO_addTypesToDict(d, KX_STATE12, (1<<11));
1653         KX_MACRO_addTypesToDict(d, KX_STATE13, (1<<12));
1654         KX_MACRO_addTypesToDict(d, KX_STATE14, (1<<13));
1655         KX_MACRO_addTypesToDict(d, KX_STATE15, (1<<14));
1656         KX_MACRO_addTypesToDict(d, KX_STATE16, (1<<15));
1657         KX_MACRO_addTypesToDict(d, KX_STATE17, (1<<16));
1658         KX_MACRO_addTypesToDict(d, KX_STATE18, (1<<17));
1659         KX_MACRO_addTypesToDict(d, KX_STATE19, (1<<18));
1660         KX_MACRO_addTypesToDict(d, KX_STATE20, (1<<19));
1661         KX_MACRO_addTypesToDict(d, KX_STATE21, (1<<20));
1662         KX_MACRO_addTypesToDict(d, KX_STATE22, (1<<21));
1663         KX_MACRO_addTypesToDict(d, KX_STATE23, (1<<22));
1664         KX_MACRO_addTypesToDict(d, KX_STATE24, (1<<23));
1665         KX_MACRO_addTypesToDict(d, KX_STATE25, (1<<24));
1666         KX_MACRO_addTypesToDict(d, KX_STATE26, (1<<25));
1667         KX_MACRO_addTypesToDict(d, KX_STATE27, (1<<26));
1668         KX_MACRO_addTypesToDict(d, KX_STATE28, (1<<27));
1669         KX_MACRO_addTypesToDict(d, KX_STATE29, (1<<28));
1670         KX_MACRO_addTypesToDict(d, KX_STATE30, (1<<29));
1671         
1672         /* All Sensors */
1673         KX_MACRO_addTypesToDict(d, KX_SENSOR_JUST_ACTIVATED, SCA_ISensor::KX_SENSOR_JUST_ACTIVATED);
1674         KX_MACRO_addTypesToDict(d, KX_SENSOR_ACTIVE, SCA_ISensor::KX_SENSOR_ACTIVE);
1675         KX_MACRO_addTypesToDict(d, KX_SENSOR_JUST_DEACTIVATED, SCA_ISensor::KX_SENSOR_JUST_DEACTIVATED);
1676         KX_MACRO_addTypesToDict(d, KX_SENSOR_INACTIVE, SCA_ISensor::KX_SENSOR_INACTIVE);
1677         
1678         /* Radar Sensor */
1679         KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_POS_X, KX_RadarSensor::KX_RADAR_AXIS_POS_X);
1680         KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_POS_Y, KX_RadarSensor::KX_RADAR_AXIS_POS_Y);
1681         KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_POS_Z, KX_RadarSensor::KX_RADAR_AXIS_POS_Z);
1682         KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_NEG_X, KX_RadarSensor::KX_RADAR_AXIS_NEG_X);
1683         KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_NEG_Y, KX_RadarSensor::KX_RADAR_AXIS_NEG_Y);
1684         KX_MACRO_addTypesToDict(d, KX_RADAR_AXIS_NEG_Z, KX_RadarSensor::KX_RADAR_AXIS_NEG_Z);
1685
1686         /* Ray Sensor */
1687         KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_POS_X, KX_RaySensor::KX_RAY_AXIS_POS_X);
1688         KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_POS_Y, KX_RaySensor::KX_RAY_AXIS_POS_Y);
1689         KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_POS_Z, KX_RaySensor::KX_RAY_AXIS_POS_Z);
1690         KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_NEG_X, KX_RaySensor::KX_RAY_AXIS_NEG_X);
1691         KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_NEG_Y, KX_RaySensor::KX_RAY_AXIS_NEG_Y);
1692         KX_MACRO_addTypesToDict(d, KX_RAY_AXIS_NEG_Z, KX_RaySensor::KX_RAY_AXIS_NEG_Z);
1693
1694         /* TrackTo Actuator */
1695         KX_MACRO_addTypesToDict(d, KX_TRACK_UPAXIS_POS_X, KX_TrackToActuator::KX_TRACK_UPAXIS_POS_X);
1696         KX_MACRO_addTypesToDict(d, KX_TRACK_UPAXIS_POS_Y, KX_TrackToActuator::KX_TRACK_UPAXIS_POS_Y);
1697         KX_MACRO_addTypesToDict(d, KX_TRACK_UPAXIS_POS_Z, KX_TrackToActuator::KX_TRACK_UPAXIS_POS_Z);
1698         KX_MACRO_addTypesToDict(d, KX_TRACK_TRAXIS_POS_X, KX_TrackToActuator::KX_TRACK_TRAXIS_POS_X);
1699         KX_MACRO_addTypesToDict(d, KX_TRACK_TRAXIS_POS_Y, KX_TrackToActuator::KX_TRACK_TRAXIS_POS_Y);
1700         KX_MACRO_addTypesToDict(d, KX_TRACK_TRAXIS_POS_Z, KX_TrackToActuator::KX_TRACK_TRAXIS_POS_Z);
1701         KX_MACRO_addTypesToDict(d, KX_TRACK_TRAXIS_NEG_X, KX_TrackToActuator::KX_TRACK_TRAXIS_NEG_X);
1702         KX_MACRO_addTypesToDict(d, KX_TRACK_TRAXIS_NEG_Y, KX_TrackToActuator::KX_TRACK_TRAXIS_NEG_Y);
1703         KX_MACRO_addTypesToDict(d, KX_TRACK_TRAXIS_NEG_Z, KX_TrackToActuator::KX_TRACK_TRAXIS_NEG_Z);
1704
1705         /* Dynamic actuator */
1706         KX_MACRO_addTypesToDict(d, KX_DYN_RESTORE_DYNAMICS, KX_SCA_DynamicActuator::KX_DYN_RESTORE_DYNAMICS);
1707         KX_MACRO_addTypesToDict(d, KX_DYN_DISABLE_DYNAMICS, KX_SCA_DynamicActuator::KX_DYN_DISABLE_DYNAMICS);
1708         KX_MACRO_addTypesToDict(d, KX_DYN_ENABLE_RIGID_BODY, KX_SCA_DynamicActuator::KX_DYN_ENABLE_RIGID_BODY);
1709         KX_MACRO_addTypesToDict(d, KX_DYN_DISABLE_RIGID_BODY, KX_SCA_DynamicActuator::KX_DYN_DISABLE_RIGID_BODY);
1710         KX_MACRO_addTypesToDict(d, KX_DYN_SET_MASS, KX_SCA_DynamicActuator::KX_DYN_SET_MASS);
1711
1712         /* Input & Mouse Sensor */
1713         KX_MACRO_addTypesToDict(d, KX_INPUT_NONE, SCA_InputEvent::KX_NO_INPUTSTATUS);
1714         KX_MACRO_addTypesToDict(d, KX_INPUT_JUST_ACTIVATED, SCA_InputEvent::KX_JUSTACTIVATED);
1715         KX_MACRO_addTypesToDict(d, KX_INPUT_ACTIVE, SCA_InputEvent::KX_ACTIVE);
1716         KX_MACRO_addTypesToDict(d, KX_INPUT_JUST_RELEASED, SCA_InputEvent::KX_JUSTRELEASED);
1717         
1718         KX_MACRO_addTypesToDict(d, KX_MOUSE_BUT_LEFT, SCA_IInputDevice::KX_LEFTMOUSE);
1719         KX_MACRO_addTypesToDict(d, KX_MOUSE_BUT_MIDDLE, SCA_IInputDevice::KX_MIDDLEMOUSE);
1720         KX_MACRO_addTypesToDict(d, KX_MOUSE_BUT_RIGHT, SCA_IInputDevice::KX_RIGHTMOUSE);
1721
1722         /* 2D Filter Actuator */
1723         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_ENABLED, RAS_2DFilterManager::RAS_2DFILTER_ENABLED);
1724         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_DISABLED, RAS_2DFilterManager::RAS_2DFILTER_DISABLED);
1725         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_NOFILTER, RAS_2DFilterManager::RAS_2DFILTER_NOFILTER);
1726         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_MOTIONBLUR, RAS_2DFilterManager::RAS_2DFILTER_MOTIONBLUR);
1727         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_BLUR, RAS_2DFilterManager::RAS_2DFILTER_BLUR);
1728         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_SHARPEN, RAS_2DFilterManager::RAS_2DFILTER_SHARPEN);
1729         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_DILATION, RAS_2DFilterManager::RAS_2DFILTER_DILATION);
1730         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_EROSION, RAS_2DFilterManager::RAS_2DFILTER_EROSION);
1731         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_LAPLACIAN, RAS_2DFilterManager::RAS_2DFILTER_LAPLACIAN);
1732         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_SOBEL, RAS_2DFilterManager::RAS_2DFILTER_SOBEL);
1733         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_PREWITT, RAS_2DFilterManager::RAS_2DFILTER_PREWITT);
1734         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_GRAYSCALE, RAS_2DFilterManager::RAS_2DFILTER_GRAYSCALE);
1735         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_SEPIA, RAS_2DFilterManager::RAS_2DFILTER_SEPIA);
1736         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_INVERT, RAS_2DFilterManager::RAS_2DFILTER_INVERT);
1737         KX_MACRO_addTypesToDict(d, RAS_2DFILTER_CUSTOMFILTER, RAS_2DFilterManager::RAS_2DFILTER_CUSTOMFILTER);
1738
1739         /* Sound Actuator */
1740         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYSTOP, KX_SoundActuator::KX_SOUNDACT_PLAYSTOP);
1741         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_PLAYEND, KX_SoundActuator::KX_SOUNDACT_PLAYEND);
1742         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPSTOP, KX_SoundActuator::KX_SOUNDACT_LOOPSTOP);
1743         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPEND, KX_SoundActuator:: KX_SOUNDACT_LOOPEND);
1744         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL, KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL);
1745         KX_MACRO_addTypesToDict(d, KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP, KX_SoundActuator::KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP);
1746
1747         /* State Actuator */
1748         KX_MACRO_addTypesToDict(d, KX_STATE_OP_CPY, KX_StateActuator::OP_CPY);
1749         KX_MACRO_addTypesToDict(d, KX_STATE_OP_SET, KX_StateActuator::OP_SET);
1750         KX_MACRO_addTypesToDict(d, KX_STATE_OP_CLR, KX_StateActuator::OP_CLR);
1751         KX_MACRO_addTypesToDict(d, KX_STATE_OP_NEG, KX_StateActuator::OP_NEG);
1752
1753         /* Game Actuator Modes */
1754         KX_MACRO_addTypesToDict(d, KX_GAME_LOAD, KX_GameActuator::KX_GAME_LOAD);
1755         KX_MACRO_addTypesToDict(d, KX_GAME_START, KX_GameActuator::KX_GAME_START);
1756         KX_MACRO_addTypesToDict(d, KX_GAME_RESTART, KX_GameActuator::KX_GAME_RESTART);
1757         KX_MACRO_addTypesToDict(d, KX_GAME_QUIT, KX_GameActuator::KX_GAME_QUIT);
1758         KX_MACRO_addTypesToDict(d, KX_GAME_SAVECFG, KX_GameActuator::KX_GAME_SAVECFG);
1759         KX_MACRO_addTypesToDict(d, KX_GAME_LOADCFG, KX_GameActuator::KX_GAME_LOADCFG);
1760
1761         /* Scene Actuator Modes */
1762         KX_MACRO_addTypesToDict(d, KX_SCENE_RESTART, KX_SceneActuator::KX_SCENE_RESTART);
1763         KX_MACRO_addTypesToDict(d, KX_SCENE_SET_SCENE, KX_SceneActuator::KX_SCENE_SET_SCENE);
1764         KX_MACRO_addTypesToDict(d, KX_SCENE_SET_CAMERA, KX_SceneActuator::KX_SCENE_SET_CAMERA);
1765         KX_MACRO_addTypesToDict(d, KX_SCENE_ADD_FRONT_SCENE, KX_SceneActuator::KX_SCENE_ADD_FRONT_SCENE);
1766         KX_MACRO_addTypesToDict(d, KX_SCENE_ADD_BACK_SCENE, KX_SceneActuator::KX_SCENE_ADD_BACK_SCENE);
1767         KX_MACRO_addTypesToDict(d, KX_SCENE_REMOVE_SCENE, KX_SceneActuator::KX_SCENE_REMOVE_SCENE);
1768         KX_MACRO_addTypesToDict(d, KX_SCENE_SUSPEND, KX_SceneActuator::KX_SCENE_SUSPEND);
1769         KX_MACRO_addTypesToDict(d, KX_SCENE_RESUME, KX_SceneActuator::KX_SCENE_RESUME);
1770
1771         /* Parent Actuator Modes */
1772         KX_MACRO_addTypesToDict(d, KX_PARENT_SET, KX_ParentActuator::KX_PARENT_SET);
1773         KX_MACRO_addTypesToDict(d, KX_PARENT_REMOVE, KX_ParentActuator::KX_PARENT_REMOVE);
1774
1775         /* BL_ArmatureConstraint type */
1776         KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_TRACKTO);
1777         KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_KINEMATIC, CONSTRAINT_TYPE_KINEMATIC);
1778         KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_ROTLIKE, CONSTRAINT_TYPE_ROTLIKE);
1779         KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_LOCLIKE, CONSTRAINT_TYPE_LOCLIKE);
1780         KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_MINMAX, CONSTRAINT_TYPE_MINMAX);
1781         KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_SIZELIKE, CONSTRAINT_TYPE_SIZELIKE);
1782         KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_LOCKTRACK);
1783         KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_STRETCHTO, CONSTRAINT_TYPE_STRETCHTO);
1784         KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_CLAMPTO, CONSTRAINT_TYPE_CLAMPTO);
1785         KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_TRANSFORM, CONSTRAINT_TYPE_TRANSFORM);
1786         KX_MACRO_addTypesToDict(d, CONSTRAINT_TYPE_DISTLIMIT, CONSTRAINT_TYPE_DISTLIMIT);
1787         /* BL_ArmatureConstraint ik_type */
1788         KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_COPYPOSE, CONSTRAINT_IK_COPYPOSE);
1789         KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_DISTANCE, CONSTRAINT_IK_DISTANCE);
1790         /* BL_ArmatureConstraint ik_mode */
1791         KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_MODE_INSIDE, LIMITDIST_INSIDE);
1792         KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_MODE_OUTSIDE, LIMITDIST_OUTSIDE);
1793         KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_MODE_ONSURFACE, LIMITDIST_ONSURFACE);
1794         /* BL_ArmatureConstraint ik_flag */
1795         KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_FLAG_TIP, CONSTRAINT_IK_TIP);
1796         KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_FLAG_ROT, CONSTRAINT_IK_ROT);
1797         KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_FLAG_STRETCH, CONSTRAINT_IK_STRETCH);
1798         KX_MACRO_addTypesToDict(d, CONSTRAINT_IK_FLAG_POS, CONSTRAINT_IK_POS);
1799         /* KX_ArmatureSensor type */
1800         KX_MACRO_addTypesToDict(d, KX_ARMSENSOR_STATE_CHANGED, SENS_ARM_STATE_CHANGED);
1801         KX_MACRO_addTypesToDict(d, KX_ARMSENSOR_LIN_ERROR_BELOW, SENS_ARM_LIN_ERROR_BELOW);
1802         KX_MACRO_addTypesToDict(d, KX_ARMSENSOR_LIN_ERROR_ABOVE, SENS_ARM_LIN_ERROR_ABOVE);
1803         KX_MACRO_addTypesToDict(d, KX_ARMSENSOR_ROT_ERROR_BELOW, SENS_ARM_ROT_ERROR_BELOW);
1804         KX_MACRO_addTypesToDict(d, KX_ARMSENSOR_ROT_ERROR_ABOVE, SENS_ARM_ROT_ERROR_ABOVE);
1805
1806         /* BL_ArmatureActuator type */
1807         KX_MACRO_addTypesToDict(d, KX_ACT_ARMATURE_RUN, ACT_ARM_RUN);
1808         KX_MACRO_addTypesToDict(d, KX_ACT_ARMATURE_ENABLE, ACT_ARM_ENABLE);
1809         KX_MACRO_addTypesToDict(d, KX_ACT_ARMATURE_DISABLE, ACT_ARM_DISABLE);
1810         KX_MACRO_addTypesToDict(d, KX_ACT_ARMATURE_SETTARGET, ACT_ARM_SETTARGET);
1811         KX_MACRO_addTypesToDict(d, KX_ACT_ARMATURE_SETWEIGHT, ACT_ARM_SETWEIGHT);
1812         KX_MACRO_addTypesToDict(d, KX_ACT_ARMATURE_SETINFLUENCE, ACT_ARM_SETINFLUENCE);
1813
1814         /* BL_Armature Channel rotation_mode */
1815         KX_MACRO_addTypesToDict(d, ROT_MODE_QUAT, ROT_MODE_QUAT);
1816         KX_MACRO_addTypesToDict(d, ROT_MODE_XYZ, ROT_MODE_XYZ);
1817         KX_MACRO_addTypesToDict(d, ROT_MODE_XZY, ROT_MODE_XZY);
1818         KX_MACRO_addTypesToDict(d, ROT_MODE_YXZ, ROT_MODE_YXZ);
1819         KX_MACRO_addTypesToDict(d, ROT_MODE_YZX, ROT_MODE_YZX);
1820         KX_MACRO_addTypesToDict(d, ROT_MODE_ZXY, ROT_MODE_ZXY);
1821         KX_MACRO_addTypesToDict(d, ROT_MODE_ZYX, ROT_MODE_ZYX);
1822
1823         /* Steering actuator */
1824         KX_MACRO_addTypesToDict(d, KX_STEERING_SEEK, KX_SteeringActuator::KX_STEERING_SEEK);
1825         KX_MACRO_addTypesToDict(d, KX_STEERING_FLEE, KX_SteeringActuator::KX_STEERING_FLEE);
1826         KX_MACRO_addTypesToDict(d, KX_STEERING_PATHFOLLOWING, KX_SteeringActuator::KX_STEERING_PATHFOLLOWING);
1827
1828         /* KX_NavMeshObject render mode */
1829         KX_MACRO_addTypesToDict(d, RM_WALLS, KX_NavMeshObject::RM_WALLS);
1830         KX_MACRO_addTypesToDict(d, RM_POLYS, KX_NavMeshObject::RM_POLYS);
1831         KX_MACRO_addTypesToDict(d, RM_TRIS, KX_NavMeshObject::RM_TRIS);
1832
1833         /* BL_Action play modes */
1834         KX_MACRO_addTypesToDict(d, KX_ACTION_MODE_PLAY, BL_Action::ACT_MODE_PLAY);
1835         KX_MACRO_addTypesToDict(d, KX_ACTION_MODE_LOOP, BL_Action::ACT_MODE_LOOP);
1836         KX_MACRO_addTypesToDict(d, KX_ACTION_MODE_PING_PONG, BL_Action::ACT_MODE_PING_PONG);
1837
1838         /* BL_Action blend modes */
1839         KX_MACRO_addTypesToDict(d, KX_ACTION_BLEND_BLEND, BL_Action::ACT_BLEND_BLEND);
1840         KX_MACRO_addTypesToDict(d, KX_ACTION_BLEND_ADD, BL_Action::ACT_BLEND_ADD);
1841
1842         /* Mouse Actuator object axis*/
1843         KX_MACRO_addTypesToDict(d, KX_ACT_MOUSE_OBJECT_AXIS_X, KX_MouseActuator::KX_ACT_MOUSE_OBJECT_AXIS_X);
1844         KX_MACRO_addTypesToDict(d, KX_ACT_MOUSE_OBJECT_AXIS_Y, KX_MouseActuator::KX_ACT_MOUSE_OBJECT_AXIS_Y);
1845         KX_MACRO_addTypesToDict(d, KX_ACT_MOUSE_OBJECT_AXIS_Z, KX_MouseActuator::KX_ACT_MOUSE_OBJECT_AXIS_Z);
1846
1847
1848         // Check for errors
1849         if (PyErr_Occurred())
1850         {
1851                 Py_FatalError("can't initialize module bge.logic");
1852         }
1853
1854         return m;
1855 }
1856
1857 /* Explanation of 
1858  * 
1859  * - backupPySysObjects()               : stores sys.path in gp_OrigPythonSysPath
1860  * - initPySysObjects(main)     : initializes the blendfile and library paths
1861  * - restorePySysObjects()              : restores sys.path from gp_OrigPythonSysPath
1862  * 
1863  * These exist so the current blend dir "//" can always be used to import modules from.
1864  * the reason we need a few functions for this is that python is not only used by the game engine
1865  * so we cant just add to sys.path all the time, it would leave pythons state in a mess.
1866  * It would also be incorrect since loading blend files for new levels etc would always add to sys.path
1867  * 
1868  * To play nice with blenders python, the sys.path is backed up and the current blendfile along
1869  * with all its lib paths are added to the sys path.
1870  * When loading a new blendfile, the original sys.path is restored and the new paths are added over the top.
1871  */
1872
1873 /**
1874  * So we can have external modules mixed with our blend files.
1875  */
1876 static void backupPySysObjects(void)
1877 {
1878         PyObject *sys_path= PySys_GetObject("path"); /* should never fail */
1879         PyObject *sys_mods= PySys_GetObject("modules"); /* should never fail */
1880         
1881         /* paths */
1882         Py_XDECREF(gp_OrigPythonSysPath); /* just in case its set */
1883         gp_OrigPythonSysPath = PyList_GetSlice(sys_path, 0, INT_MAX); /* copy the list */
1884         
1885         /* modules */
1886         Py_XDECREF(gp_OrigPythonSysModules); /* just in case its set */
1887         gp_OrigPythonSysModules = PyDict_Copy(sys_mods); /* copy the list */
1888         
1889 }
1890
1891 /* for initPySysObjects only,
1892  * takes a blend path and adds a scripts dir from it
1893  *
1894  * "/home/me/foo.blend" -> "/home/me/scripts"
1895  */
1896 static void initPySysObjects__append(PyObject *sys_path, const char *filename)
1897 {
1898         PyObject *item;
1899         char expanded[FILE_MAX];
1900         
1901         BLI_split_dir_part(filename, expanded, sizeof(expanded)); /* get the dir part of filename only */
1902         BLI_path_abs(expanded, gp_GamePythonPath); /* filename from lib->filename is (always?) absolute, so this may not be needed but it wont hurt */
1903         BLI_cleanup_file(gp_GamePythonPath, expanded); /* Don't use BLI_cleanup_dir because it adds a slash - BREAKS WIN32 ONLY */
1904         item = PyC_UnicodeFromByte(expanded);
1905         
1906 //      printf("SysPath - '%s', '%s', '%s'\n", expanded, filename, gp_GamePythonPath);
1907         
1908         if (PySequence_Index(sys_path, item) == -1) {
1909                 PyErr_Clear(); /* PySequence_Index sets a ValueError */
1910                 PyList_Insert(sys_path, 0, item);
1911         }
1912         
1913         Py_DECREF(item);
1914 }
1915 static void initPySysObjects(Main *maggie)
1916 {
1917         PyObject *sys_path= PySys_GetObject("path"); /* should never fail */
1918         
1919         if (gp_OrigPythonSysPath==NULL) {
1920                 /* backup */
1921                 backupPySysObjects();
1922         }
1923         else {
1924                 /* get the original sys path when the BGE started */
1925                 PyList_SetSlice(sys_path, 0, INT_MAX, gp_OrigPythonSysPath);
1926         }
1927         
1928         Library *lib= (Library *)maggie->library.first;
1929         
1930         while (lib) {
1931                 /* lib->name wont work in some cases (on win32),
1932                  * even when expanding with gp_GamePythonPath, using lib->filename is less trouble */
1933                 initPySysObjects__append(sys_path, lib->filepath);
1934                 lib= (Library *)lib->id.next;
1935         }
1936         
1937         initPySysObjects__append(sys_path, gp_GamePythonPath);
1938         
1939 //      fprintf(stderr, "\nNew Path: %d ", PyList_GET_SIZE(sys_path));
1940 //      PyObject_Print(sys_path, stderr, 0);
1941 }
1942
1943 static void restorePySysObjects(void)
1944 {
1945         if (gp_OrigPythonSysPath==NULL)
1946                 return;
1947         
1948         PyObject *sys_path= PySys_GetObject("path"); /* should never fail */
1949         PyObject *sys_mods= PySys_GetObject("modules"); /* should never fail */
1950
1951         /* paths */
1952         PyList_SetSlice(sys_path, 0, INT_MAX, gp_OrigPythonSysPath);
1953         Py_DECREF(gp_OrigPythonSysPath);
1954         gp_OrigPythonSysPath= NULL;
1955         
1956         /* modules */
1957         PyDict_Clear(sys_mods);
1958         PyDict_Update(sys_mods, gp_OrigPythonSysModules);
1959         Py_DECREF(gp_OrigPythonSysModules);
1960         gp_OrigPythonSysModules= NULL;
1961         
1962         
1963 //      fprintf(stderr, "\nRestore Path: %d ", PyList_GET_SIZE(sys_path));
1964 //      PyObject_Print(sys_path, stderr, 0);
1965 }
1966
1967 void addImportMain(struct Main *maggie)
1968 {
1969         bpy_import_main_extra_add(maggie);
1970 }
1971
1972 void removeImportMain(struct Main *maggie)
1973 {
1974         bpy_import_main_extra_remove(maggie);
1975 }
1976
1977
1978 PyDoc_STRVAR(BGE_module_documentation,
1979         "This module contains submodules for the Blender Game Engine.\n"
1980 );
1981
1982 static struct PyModuleDef BGE_module_def = {
1983         PyModuleDef_HEAD_INIT,
1984         "bge",  /* m_name */
1985         BGE_module_documentation,  /* m_doc */
1986         0,  /* m_size */
1987         NULL,  /* m_methods */
1988         NULL,  /* m_reload */
1989         NULL,  /* m_traverse */
1990         NULL,  /* m_clear */
1991         NULL,  /* m_free */
1992 };
1993
1994 PyMODINIT_FUNC initBGE(void)
1995 {
1996         PyObject *mod;
1997         PyObject *submodule;
1998         PyObject *sys_modules = PyThreadState_GET()->interp->modules;
1999
2000         mod = PyModule_Create(&BGE_module_def);
2001
2002         PyModule_AddObject(mod, "constraints", (submodule = initConstraintPythonBinding()));
2003         PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
2004         Py_INCREF(submodule);
2005
2006         PyModule_AddObject(mod, "events", (submodule = initGameKeysPythonBinding()));
2007         PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
2008         Py_INCREF(submodule);
2009
2010         PyModule_AddObject(mod, "logic", (submodule = initGameLogicPythonBinding()));
2011         PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
2012         Py_INCREF(submodule);
2013
2014         PyModule_AddObject(mod, "render", (submodule = initRasterizerPythonBinding()));
2015         PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
2016         Py_INCREF(submodule);
2017
2018         PyModule_AddObject(mod, "texture", (submodule = initVideoTexturePythonBinding()));
2019         PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
2020         Py_INCREF(submodule);
2021
2022         PyModule_AddObject(mod, "types", (submodule = initGameTypesPythonBinding()));
2023         PyDict_SetItemString(sys_modules, PyModule_GetName(submodule), submodule);
2024         Py_INCREF(submodule);
2025
2026         return mod;
2027 }
2028
2029
2030 /* minimal required blender modules to run blenderplayer */
2031 static struct _inittab bge_internal_modules[] = {
2032         {"mathutils", PyInit_mathutils},
2033         {"bgl", BPyInit_bgl},
2034         {"blf", BPyInit_blf},
2035         {"aud", AUD_initPython},
2036         {NULL, NULL}
2037 };
2038
2039 /**
2040  * Python is not initialized.
2041  * see bpy_interface.c's BPY_python_start() which shares the same functionality in blender.
2042  */
2043 PyObject *initGamePlayerPythonScripting(Main *maggie, int argc, char** argv)
2044 {
2045         /* Yet another gotcha in the py api
2046          * Cant run PySys_SetArgv more than once because this adds the
2047          * binary dir to the sys.path each time.
2048          * Id have thought python being totally restarted would make this ok but
2049          * somehow it remembers the sys.path - Campbell
2050          */
2051         static bool first_time = true;
2052         const char * const py_path_bundle = BKE_appdir_folder_id(BLENDER_SYSTEM_PYTHON, NULL);
2053
2054         /* not essential but nice to set our name */
2055         static wchar_t program_path_wchar[FILE_MAX]; /* python holds a reference */
2056         BLI_strncpy_wchar_from_utf8(program_path_wchar, BKE_appdir_program_path(), ARRAY_SIZE(program_path_wchar));
2057         Py_SetProgramName(program_path_wchar);
2058
2059         /* Update, Py3.3 resolves attempting to parse non-existing header */
2060         #if 0
2061         /* Python 3.2 now looks for '2.xx/python/include/python3.2d/pyconfig.h' to
2062          * parse from the 'sysconfig' module which is used by 'site',
2063          * so for now disable site. alternatively we could copy the file. */
2064         if (py_path_bundle != NULL) {
2065                 Py_NoSiteFlag = 1; /* inhibits the automatic importing of 'site' */
2066         }
2067         #endif
2068
2069         Py_FrozenFlag = 1;
2070
2071         /* must run before python initializes */
2072         PyImport_ExtendInittab(bge_internal_modules);
2073
2074         /* find local python installation */
2075         PyC_SetHomePath(py_path_bundle);
2076
2077         Py_Initialize();
2078         
2079         if (argv && first_time) { /* browser plugins don't currently set this */
2080                 // Until python support ascii again, we use our own.
2081                 // PySys_SetArgv(argc, argv);
2082                 int i;
2083                 PyObject *py_argv= PyList_New(argc);
2084
2085                 for (i=0; i<argc; i++)
2086                         PyList_SET_ITEM(py_argv, i, PyC_UnicodeFromByte(argv[i]));
2087
2088                 PySys_SetObject("argv", py_argv);
2089                 Py_DECREF(py_argv);
2090         }
2091
2092         /* Initialize thread support (also acquires lock) */
2093         PyEval_InitThreads();
2094
2095         bpy_import_init(PyEval_GetBuiltins());
2096
2097         bpy_import_main_set(maggie);
2098
2099         initPySysObjects(maggie);
2100
2101         /* mathutils types are used by the BGE even if we don't import them */
2102         {
2103                 PyObject *mod = PyImport_ImportModuleLevel("mathutils", NULL, NULL, NULL, 0);
2104                 Py_DECREF(mod);
2105         }
2106
2107 #ifdef WITH_AUDASPACE
2108         /* accessing a SoundActuator's sound results in a crash if aud is not initialized... */
2109         {
2110                 PyObject *mod = PyImport_ImportModuleLevel("aud", NULL, NULL, NULL, 0);
2111                 Py_DECREF(mod);
2112         }
2113 #endif
2114
2115         PyDict_SetItemString(PyImport_GetModuleDict(), "bge", initBGE());
2116
2117         first_time = false;
2118         
2119         PyObjectPlus::ClearDeprecationWarning();
2120
2121         return PyC_DefaultNameSpace(NULL);
2122 }
2123
2124 void exitGamePlayerPythonScripting()
2125 {
2126         /* Clean up the Python mouse and keyboard */
2127         delete gp_PythonKeyboard;
2128         gp_PythonKeyboard = NULL;
2129
2130         delete gp_PythonMouse;
2131         gp_PythonMouse = NULL;
2132
2133         for (int i=0; i<JOYINDEX_MAX; ++i) {
2134                 if (gp_PythonJoysticks[i]) {
2135                         delete gp_PythonJoysticks[i];
2136                         gp_PythonJoysticks[i] = NULL;
2137                 }
2138         }
2139
2140         /* since python restarts we cant let the python backup of the sys.path hang around in a global pointer */
2141         restorePySysObjects(); /* get back the original sys.path and clear the backup */
2142         
2143         Py_Finalize();
2144         bpy_import_main_set(NULL);
2145         PyObjectPlus::ClearDeprecationWarning();
2146 }
2147
2148
2149
2150 /**
2151  * Python is already initialized.
2152  */
2153 PyObject *initGamePythonScripting(Main *maggie)
2154 {
2155         /* no need to Py_SetProgramName, it was already taken care of in BPY_python_start */
2156
2157         bpy_import_main_set(maggie);
2158
2159         initPySysObjects(maggie);
2160
2161 #ifdef WITH_AUDASPACE
2162         /* accessing a SoundActuator's sound results in a crash if aud is not initialized... */
2163         {
2164                 PyObject *mod= PyImport_ImportModuleLevel("aud", NULL, NULL, NULL, 0);
2165                 Py_DECREF(mod);
2166         }
2167 #endif
2168
2169         PyDict_SetItemString(PyImport_GetModuleDict(), "bge", initBGE());
2170
2171         PyObjectPlus::NullDeprecationWarning();
2172
2173         return PyC_DefaultNameSpace(NULL);
2174 }
2175
2176 void exitGamePythonScripting()
2177 {
2178         /* Clean up the Python mouse and keyboard */
2179         delete gp_PythonKeyboard;
2180         gp_PythonKeyboard = NULL;
2181
2182         delete gp_PythonMouse;
2183         gp_PythonMouse = NULL;
2184
2185         for (int i=0; i<JOYINDEX_MAX; ++i) {
2186                 if (gp_PythonJoysticks[i]) {
2187                         delete gp_PythonJoysticks[i];
2188                         gp_PythonJoysticks[i] = NULL;
2189                 }
2190         }
2191
2192         restorePySysObjects(); /* get back the original sys.path and clear the backup */
2193         bpy_import_main_set(NULL);
2194         PyObjectPlus::ClearDeprecationWarning();
2195 }
2196
2197 /* similar to the above functions except it sets up the namespace
2198  * and other more general things */
2199 void setupGamePython(KX_KetsjiEngine* ketsjiengine, KX_Scene *startscene, Main *blenderdata,
2200                      PyObject *pyGlobalDict, PyObject **gameLogic, PyObject **gameLogic_keys, int argc, char** argv)
2201 {
2202         PyObject *modules, *dictionaryobject;
2203
2204         gp_Canvas = ketsjiengine->GetCanvas();
2205         gp_Rasterizer = ketsjiengine->GetRasterizer();
2206         gp_KetsjiEngine = ketsjiengine;
2207         gp_KetsjiScene = startscene;
2208
2209         if (argv) /* player only */
2210                 dictionaryobject= initGamePlayerPythonScripting(blenderdata, argc, argv);
2211         else
2212                 dictionaryobject= initGamePythonScripting(blenderdata);
2213
2214         ketsjiengine->SetPyNamespace(dictionaryobject);
2215
2216         modules = PyImport_GetModuleDict();
2217
2218         *gameLogic = PyDict_GetItemString(modules, "GameLogic");
2219         /* is set in initGameLogicPythonBinding so only set here if we want it to persist between scenes */
2220         if (pyGlobalDict)
2221                 PyDict_SetItemString(PyModule_GetDict(*gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
2222
2223         *gameLogic_keys = PyDict_Keys(PyModule_GetDict(*gameLogic));
2224 }
2225
2226 static struct PyModuleDef Rasterizer_module_def = {
2227         PyModuleDef_HEAD_INIT,
2228         "Rasterizer",  /* m_name */
2229         Rasterizer_module_documentation,  /* m_doc */
2230         0,  /* m_size */
2231         rasterizer_methods,  /* m_methods */
2232         0,  /* m_reload */
2233         0,  /* m_traverse */
2234         0,  /* m_clear */
2235         0,  /* m_free */
2236 };
2237
2238 PyMODINIT_FUNC initRasterizerPythonBinding()
2239 {
2240         PyObject *m;
2241         PyObject *d;
2242
2243         m = PyModule_Create(&Rasterizer_module_def);
2244         PyDict_SetItemString(PySys_GetObject("modules"), Rasterizer_module_def.m_name, m);
2245
2246
2247         // Add some symbolic constants to the module
2248         d = PyModule_GetDict(m);
2249         ErrorObject = PyUnicode_FromString("Rasterizer.error");
2250         PyDict_SetItemString(d, "error", ErrorObject);
2251         Py_DECREF(ErrorObject);
2252
2253         /* needed for get/setMaterialType */
2254         KX_MACRO_addTypesToDict(d, KX_BLENDER_MULTITEX_MATERIAL, KX_BLENDER_MULTITEX_MATERIAL);
2255         KX_MACRO_addTypesToDict(d, KX_BLENDER_GLSL_MATERIAL, KX_BLENDER_GLSL_MATERIAL);
2256
2257         KX_MACRO_addTypesToDict(d, RAS_MIPMAP_NONE, RAS_IRasterizer::RAS_MIPMAP_NONE);
2258         KX_MACRO_addTypesToDict(d, RAS_MIPMAP_NEAREST, RAS_IRasterizer::RAS_MIPMAP_NEAREST);
2259         KX_MACRO_addTypesToDict(d, RAS_MIPMAP_LINEAR, RAS_IRasterizer::RAS_MIPMAP_LINEAR);
2260
2261         /* for get/setVsync */
2262         KX_MACRO_addTypesToDict(d, VSYNC_OFF, VSYNC_OFF);
2263         KX_MACRO_addTypesToDict(d, VSYNC_ON, VSYNC_ON);
2264         KX_MACRO_addTypesToDict(d, VSYNC_ADAPTIVE, VSYNC_ADAPTIVE);
2265
2266         /* stereoscopy */
2267         KX_MACRO_addTypesToDict(d, LEFT_EYE, RAS_IRasterizer::RAS_STEREO_LEFTEYE);
2268         KX_MACRO_addTypesToDict(d, RIGHT_EYE, RAS_IRasterizer::RAS_STEREO_RIGHTEYE);
2269
2270         // XXXX Add constants here
2271
2272         // Check for errors
2273         if (PyErr_Occurred())
2274         {
2275                 Py_FatalError("can't initialize module Rasterizer");
2276         }
2277
2278         return m;
2279 }
2280
2281
2282
2283 /* ------------------------------------------------------------------------- */
2284 /* GameKeys: symbolic constants for key mapping                              */
2285 /* ------------------------------------------------------------------------- */
2286
2287 PyDoc_STRVAR(GameKeys_module_documentation,
2288 "This modules provides defines for key-codes"
2289 );
2290
2291 PyDoc_STRVAR(gPyEventToString_doc,
2292 "EventToString(event)\n"
2293 "Take a valid event from the GameKeys module or Keyboard Sensor and return a name"
2294 );
2295
2296 static PyObject *gPyEventToString(PyObject *, PyObject *value)
2297 {
2298         PyObject *mod, *dict, *key, *val, *ret = NULL;
2299         Py_ssize_t pos = 0;
2300         
2301         mod = PyImport_ImportModule( "GameKeys" );
2302         if (!mod)
2303                 return NULL;
2304         
2305         dict = PyModule_GetDict(mod);
2306         
2307         while (PyDict_Next(dict, &pos, &key, &val)) {
2308                 if (PyObject_RichCompareBool(value, val, Py_EQ)) {
2309                         ret = key;
2310                         break;
2311                 }
2312         }
2313         
2314         PyErr_Clear(); // in case there was an error clearing
2315         Py_DECREF(mod);
2316         if (!ret)       PyErr_SetString(PyExc_ValueError, "GameKeys.EventToString(int): expected a valid int keyboard event");
2317         else            Py_INCREF(ret);
2318         
2319         return ret;
2320 }
2321
2322
2323 PyDoc_STRVAR(gPyEventToCharacter_doc,
2324 "EventToCharacter(event, is_shift)\n"
2325 "Take a valid event from the GameKeys module or Keyboard Sensor and return a character"
2326 );
2327
2328 static PyObject *gPyEventToCharacter(PyObject *, PyObject *args)
2329 {
2330         int event, shift;
2331         if (!PyArg_ParseTuple(args,"ii:EventToCharacter", &event, &shift))
2332                 return NULL;
2333         
2334         if (IsPrintable(event)) {
2335                 char ch[2] = {'\0', '\0'};
2336                 ch[0] = ToCharacter(event, (bool)shift);
2337                 return PyUnicode_FromString(ch);
2338         }
2339         else {
2340                 return PyUnicode_FromString("");
2341         }
2342 }
2343
2344
2345 static struct PyMethodDef gamekeys_methods[] = {
2346         {"EventToCharacter", (PyCFunction)gPyEventToCharacter, METH_VARARGS, (const char *)gPyEventToCharacter_doc},
2347         {"EventToString", (PyCFunction)gPyEventToString, METH_O, (const char *)gPyEventToString_doc},
2348         { NULL, (PyCFunction) NULL, 0, NULL }
2349 };
2350
2351 static struct PyModuleDef GameKeys_module_def = {
2352         PyModuleDef_HEAD_INIT,
2353         "GameKeys",  /* m_name */
2354         GameKeys_module_documentation,  /* m_doc */
2355         0,  /* m_size */
2356         gamekeys_methods,  /* m_methods */
2357         0,  /* m_reload */
2358         0,  /* m_traverse */
2359         0,  /* m_clear */
2360         0,  /* m_free */
2361 };
2362
2363 PyMODINIT_FUNC initGameKeysPythonBinding()
2364 {
2365         PyObject *m;
2366         PyObject *d;
2367
2368         m = PyModule_Create(&GameKeys_module_def);
2369         PyDict_SetItemString(PySys_GetObject("modules"), GameKeys_module_def.m_name, m);
2370
2371         // Add some symbolic constants to the module
2372         d = PyModule_GetDict(m);
2373
2374         // XXXX Add constants here
2375
2376         KX_MACRO_addTypesToDict(d, AKEY, SCA_IInputDevice::KX_AKEY);
2377         KX_MACRO_addTypesToDict(d, BKEY, SCA_IInputDevice::KX_BKEY);
2378         KX_MACRO_addTypesToDict(d, CKEY, SCA_IInputDevice::KX_CKEY);
2379         KX_MACRO_addTypesToDict(d, DKEY, SCA_IInputDevice::KX_DKEY);
2380         KX_MACRO_addTypesToDict(d, EKEY, SCA_IInputDevice::KX_EKEY);
2381         KX_MACRO_addTypesToDict(d, FKEY, SCA_IInputDevice::KX_FKEY);
2382         KX_MACRO_addTypesToDict(d, GKEY, SCA_IInputDevice::KX_GKEY);
2383         KX_MACRO_addTypesToDict(d, HKEY, SCA_IInputDevice::KX_HKEY);
2384         KX_MACRO_addTypesToDict(d, IKEY, SCA_IInputDevice::KX_IKEY);
2385         KX_MACRO_addTypesToDict(d, JKEY, SCA_IInputDevice::KX_JKEY);
2386         KX_MACRO_addTypesToDict(d, KKEY, SCA_IInputDevice::KX_KKEY);
2387         KX_MACRO_addTypesToDict(d, LKEY, SCA_IInputDevice::KX_LKEY);
2388         KX_MACRO_addTypesToDict(d, MKEY, SCA_IInputDevice::KX_MKEY);
2389         KX_MACRO_addTypesToDict(d, NKEY, SCA_IInputDevice::KX_NKEY);
2390         KX_MACRO_addTypesToDict(d, OKEY, SCA_IInputDevice::KX_OKEY);
2391         KX_MACRO_addTypesToDict(d, PKEY, SCA_IInputDevice::KX_PKEY);
2392         KX_MACRO_addTypesToDict(d, QKEY, SCA_IInputDevice::KX_QKEY);
2393         KX_MACRO_addTypesToDict(d, RKEY, SCA_IInputDevice::KX_RKEY);
2394         KX_MACRO_addTypesToDict(d, SKEY, SCA_IInputDevice::KX_SKEY);
2395         KX_MACRO_addTypesToDict(d, TKEY, SCA_IInputDevice::KX_TKEY);
2396         KX_MACRO_addTypesToDict(d, UKEY, SCA_IInputDevice::KX_UKEY);
2397         KX_MACRO_addTypesToDict(d, VKEY, SCA_IInputDevice::KX_VKEY);
2398         KX_MACRO_addTypesToDict(d, WKEY, SCA_IInputDevice::KX_WKEY);
2399         KX_MACRO_addTypesToDict(d, XKEY, SCA_IInputDevice::KX_XKEY);
2400         KX_MACRO_addTypesToDict(d, YKEY, SCA_IInputDevice::KX_YKEY);
2401         KX_MACRO_addTypesToDict(d, ZKEY, SCA_IInputDevice::KX_ZKEY);
2402         
2403         KX_MACRO_addTypesToDict(d, ZEROKEY, SCA_IInputDevice::KX_ZEROKEY);
2404         KX_MACRO_addTypesToDict(d, ONEKEY, SCA_IInputDevice::KX_ONEKEY);
2405         KX_MACRO_addTypesToDict(d, TWOKEY, SCA_IInputDevice::KX_TWOKEY);
2406         KX_MACRO_addTypesToDict(d, THREEKEY, SCA_IInputDevice::KX_THREEKEY);
2407         KX_MACRO_addTypesToDict(d, FOURKEY, SCA_IInputDevice::KX_FOURKEY);
2408         KX_MACRO_addTypesToDict(d, FIVEKEY, SCA_IInputDevice::KX_FIVEKEY);
2409         KX_MACRO_addTypesToDict(d, SIXKEY, SCA_IInputDevice::KX_SIXKEY);
2410         KX_MACRO_addTypesToDict(d, SEVENKEY, SCA_IInputDevice::KX_SEVENKEY);
2411         KX_MACRO_addTypesToDict(d, EIGHTKEY, SCA_IInputDevice::KX_EIGHTKEY);
2412         KX_MACRO_addTypesToDict(d, NINEKEY, SCA_IInputDevice::KX_NINEKEY);
2413                 
2414         KX_MACRO_addTypesToDict(d, CAPSLOCKKEY, SCA_IInputDevice::KX_CAPSLOCKKEY);
2415                 
2416         KX_MACRO_addTypesToDict(d, LEFTCTRLKEY, SCA_IInputDevice::KX_LEFTCTRLKEY);
2417         KX_MACRO_addTypesToDict(d, LEFTALTKEY, SCA_IInputDevice::KX_LEFTALTKEY);
2418         KX_MACRO_addTypesToDict(d, RIGHTALTKEY, SCA_IInputDevice::KX_RIGHTALTKEY);
2419         KX_MACRO_addTypesToDict(d, RIGHTCTRLKEY, SCA_IInputDevice::KX_RIGHTCTRLKEY);
2420         KX_MACRO_addTypesToDict(d, RIGHTSHIFTKEY, SCA_IInputDevice::KX_RIGHTSHIFTKEY);
2421         KX_MACRO_addTypesToDict(d, LEFTSHIFTKEY, SCA_IInputDevice::KX_LEFTSHIFTKEY);
2422                 
2423         KX_MACRO_addTypesToDict(d, ESCKEY, SCA_IInputDevice::KX_ESCKEY);
2424         KX_MACRO_addTypesToDict(d, TABKEY, SCA_IInputDevice::KX_TABKEY);
2425         KX_MACRO_addTypesToDict(d, RETKEY, SCA_IInputDevice::KX_RETKEY);
2426         KX_MACRO_addTypesToDict(d, ENTERKEY, SCA_IInputDevice::KX_RETKEY);
2427         KX_MACRO_addTypesToDict(d, SPACEKEY, SCA_IInputDevice::KX_SPACEKEY);
2428         KX_MACRO_addTypesToDict(d, LINEFEEDKEY, SCA_IInputDevice::KX_LINEFEEDKEY);
2429         KX_MACRO_addTypesToDict(d, BACKSPACEKEY, SCA_IInputDevice::KX_BACKSPACEKEY);
2430         KX_MACRO_addTypesToDict(d, DELKEY, SCA_IInputDevice::KX_DELKEY);
2431         KX_MACRO_addTypesToDict(d, SEMICOLONKEY, SCA_IInputDevice::KX_SEMICOLONKEY);
2432         KX_MACRO_addTypesToDict(d, PERIODKEY, SCA_IInputDevice::KX_PERIODKEY);
2433         KX_MACRO_addTypesToDict(d, COMMAKEY, SCA_IInputDevice::KX_COMMAKEY);
2434         KX_MACRO_addTypesToDict(d, QUOTEKEY, SCA_IInputDevice::KX_QUOTEKEY);
2435         KX_MACRO_addTypesToDict(d, ACCENTGRAVEKEY, SCA_IInputDevice::KX_ACCENTGRAVEKEY);
2436         KX_MACRO_addTypesToDict(d, MINUSKEY, SCA_IInputDevice::KX_MINUSKEY);
2437         KX_MACRO_addTypesToDict(d, SLASHKEY, SCA_IInputDevice::KX_SLASHKEY);
2438         KX_MACRO_addTypesToDict(d, BACKSLASHKEY, SCA_IInputDevice::KX_BACKSLASHKEY);
2439         KX_MACRO_addTypesToDict(d, EQUALKEY, SCA_IInputDevice::KX_EQUALKEY);
2440         KX_MACRO_addTypesToDict(d, LEFTBRACKETKEY, SCA_IInputDevice::KX_LEFTBRACKETKEY);
2441         KX_MACRO_addTypesToDict(d, RIGHTBRACKETKEY, SCA_IInputDevice::KX_RIGHTBRACKETKEY);
2442                 
2443         KX_MACRO_addTypesToDict(d, LEFTARROWKEY, SCA_IInputDevice::KX_LEFTARROWKEY);
2444         KX_MACRO_addTypesToDict(d, DOWNARROWKEY, SCA_IInputDevice::KX_DOWNARROWKEY);
2445         KX_MACRO_addTypesToDict(d, RIGHTARROWKEY, SCA_IInputDevice::KX_RIGHTARROWKEY);
2446         KX_MACRO_addTypesToDict(d, UPARROWKEY, SCA_IInputDevice::KX_UPARROWKEY);
2447         
2448         KX_MACRO_addTypesToDict(d, PAD2 , SCA_IInputDevice::KX_PAD2);
2449         KX_MACRO_addTypesToDict(d, PAD4 , SCA_IInputDevice::KX_PAD4);
2450         KX_MACRO_addTypesToDict(d, PAD6 , SCA_IInputDevice::KX_PAD6);
2451         KX_MACRO_addTypesToDict(d, PAD8 , SCA_IInputDevice::KX_PAD8);
2452                 
2453         KX_MACRO_addTypesToDict(d, PAD1 , SCA_IInputDevice::KX_PAD1);
2454         KX_MACRO_addTypesToDict(d, PAD3 , SCA_IInputDevice::KX_PAD3);
2455         KX_MACRO_addTypesToDict(d, PAD5 , SCA_IInputDevice::KX_PAD5);
2456         KX_MACRO_addTypesToDict(d, PAD7 , SCA_IInputDevice::KX_PAD7);
2457         KX_MACRO_addTypesToDict(d, PAD9 , SCA_IInputDevice::KX_PAD9);
2458                 
2459         KX_MACRO_addTypesToDict(d, PADPERIOD, SCA_IInputDevice::KX_PADPERIOD);
2460         KX_MACRO_addTypesToDict(d, PADSLASHKEY, SCA_IInputDevice::KX_PADSLASHKEY);
2461         KX_MACRO_addTypesToDict(d, PADASTERKEY, SCA_IInputDevice::KX_PADASTERKEY);
2462                 
2463                 
2464         KX_MACRO_addTypesToDict(d, PAD0, SCA_IInputDevice::KX_PAD0);
2465         KX_MACRO_addTypesToDict(d, PADMINUS, SCA_IInputDevice::KX_PADMINUS);
2466         KX_MACRO_addTypesToDict(d, PADENTER, SCA_IInputDevice::KX_PADENTER);
2467         KX_MACRO_addTypesToDict(d, PADPLUSKEY, SCA_IInputDevice::KX_PADPLUSKEY);
2468                 
2469                 
2470         KX_MACRO_addTypesToDict(d, F1KEY,  SCA_IInputDevice::KX_F1KEY);
2471         KX_MACRO_addTypesToDict(d, F2KEY,  SCA_IInputDevice::KX_F2KEY);
2472         KX_MACRO_addTypesToDict(d, F3KEY,  SCA_IInputDevice::KX_F3KEY);
2473         KX_MACRO_addTypesToDict(d, F4KEY,  SCA_IInputDevice::KX_F4KEY);
2474         KX_MACRO_addTypesToDict(d, F5KEY,  SCA_IInputDevice::KX_F5KEY);
2475         KX_MACRO_addTypesToDict(d, F6KEY,  SCA_IInputDevice::KX_F6KEY);
2476         KX_MACRO_addTypesToDict(d, F7KEY,  SCA_IInputDevice::KX_F7KEY);
2477         KX_MACRO_addTypesToDict(d, F8KEY,  SCA_IInputDevice::KX_F8KEY);
2478         KX_MACRO_addTypesToDict(d, F9KEY,  SCA_IInputDevice::KX_F9KEY);
2479         KX_MACRO_addTypesToDict(d, F10KEY, SCA_IInputDevice::KX_F10KEY);
2480         KX_MACRO_addTypesToDict(d, F11KEY, SCA_IInputDevice::KX_F11KEY);
2481         KX_MACRO_addTypesToDict(d, F12KEY, SCA_IInputDevice::KX_F12KEY);
2482         KX_MACRO_addTypesToDict(d, F13KEY, SCA_IInputDevice::KX_F13KEY);
2483         KX_MACRO_addTypesToDict(d, F14KEY, SCA_IInputDevice::KX_F14KEY);
2484         KX_MACRO_addTypesToDict(d, F15KEY, SCA_IInputDevice::KX_F15KEY);
2485         KX_MACRO_addTypesToDict(d, F16KEY, SCA_IInputDevice::KX_F16KEY);
2486         KX_MACRO_addTypesToDict(d, F17KEY, SCA_IInputDevice::KX_F17KEY);
2487         KX_MACRO_addTypesToDict(d, F18KEY, SCA_IInputDevice::KX_F18KEY);
2488         KX_MACRO_addTypesToDict(d, F19KEY, SCA_IInputDevice::KX_F19KEY);
2489
2490         KX_MACRO_addTypesToDict(d, OSKEY, SCA_IInputDevice::KX_OSKEY);
2491                 
2492         KX_MACRO_addTypesToDict(d, PAUSEKEY,  SCA_IInputDevice::KX_PAUSEKEY);
2493         KX_MACRO_addTypesToDict(d, INSERTKEY, SCA_IInputDevice::KX_INSERTKEY);
2494         KX_MACRO_addTypesToDict(d, HOMEKEY,   SCA_IInputDevice::KX_HOMEKEY);
2495         KX_MACRO_addTypesToDict(d, PAGEUPKEY, SCA_IInputDevice::KX_PAGEUPKEY);
2496         KX_MACRO_addTypesToDict(d, PAGEDOWNKEY, SCA_IInputDevice::KX_PAGEDOWNKEY);
2497         KX_MACRO_addTypesToDict(d, ENDKEY, SCA_IInputDevice::KX_ENDKEY);
2498
2499         // MOUSE
2500         KX_MACRO_addTypesToDict(d, LEFTMOUSE, SCA_IInputDevice::KX_LEFTMOUSE);
2501         KX_MACRO_addTypesToDict(d, MIDDLEMOUSE, SCA_IInputDevice::KX_MIDDLEMOUSE);
2502         KX_MACRO_addTypesToDict(d, RIGHTMOUSE, SCA_IInputDevice::KX_RIGHTMOUSE);
2503         KX_MACRO_addTypesToDict(d, WHEELUPMOUSE, SCA_IInputDevice::KX_WHEELUPMOUSE);
2504         KX_MACRO_addTypesToDict(d, WHEELDOWNMOUSE, SCA_IInputDevice::KX_WHEELDOWNMOUSE);
2505         KX_MACRO_addTypesToDict(d, MOUSEX, SCA_IInputDevice::KX_MOUSEX);
2506         KX_MACRO_addTypesToDict(d, MOUSEY, SCA_IInputDevice::KX_MOUSEY);
2507
2508         // Check for errors
2509         if (PyErr_Occurred())
2510         {
2511                 Py_FatalError("can't initialize module GameKeys");
2512         }
2513
2514         return m;
2515 }
2516
2517 // utility function for loading and saving the globalDict
2518 int saveGamePythonConfig( char **marshal_buffer)
2519 {
2520         int marshal_length = 0;
2521         PyObject *gameLogic = PyImport_ImportModule("GameLogic");
2522         if (gameLogic) {
2523                 PyObject *pyGlobalDict = PyDict_GetItemString(PyModule_GetDict(gameLogic), "globalDict"); // Same as importing the module
2524                 if (pyGlobalDict) {
2525 #ifdef Py_MARSHAL_VERSION
2526                         PyObject *pyGlobalDictMarshal = PyMarshal_WriteObjectToString(  pyGlobalDict, 2); // Py_MARSHAL_VERSION == 2 as of Py2.5
2527 #else
2528                         PyObject *pyGlobalDictMarshal = PyMarshal_WriteObjectToString(  pyGlobalDict );
2529 #endif
2530                         if (pyGlobalDictMarshal) {
2531                                 // for testing only
2532                                 // PyObject_Print(pyGlobalDictMarshal, stderr, 0);
2533                                 char *marshal_cstring;
2534                                 
2535                                 marshal_cstring = PyBytes_AsString(pyGlobalDictMarshal); // py3 uses byte arrays
2536                                 marshal_length= PyBytes_Size(pyGlobalDictMarshal);
2537                                 *marshal_buffer = new char[marshal_length + 1];
2538                                 memcpy(*marshal_buffer, marshal_cstring, marshal_length);
2539                                 Py_DECREF(pyGlobalDictMarshal);
2540                         } else {
2541                                 printf("Error, bge.logic.globalDict could not be marshal'd\n");
2542                         }
2543                 } else {
2544                         printf("Error, bge.logic.globalDict was removed\n");
2545                 }
2546                 Py_DECREF(gameLogic);
2547         } else {
2548                 PyErr_Clear();
2549                 printf("Error, bge.logic failed to import bge.logic.globalDict will be lost\n");
2550         }
2551         return marshal_length;
2552 }
2553
2554 int loadGamePythonConfig(char *marshal_buffer, int marshal_length)
2555 {
2556         /* Restore the dict */
2557         if (marshal_buffer) {
2558                 PyObject *gameLogic = PyImport_ImportModule("GameLogic");
2559
2560                 if (gameLogic) {
2561                         PyObject *pyGlobalDict = PyMarshal_ReadObjectFromString(marshal_buffer, marshal_length);
2562                         if (pyGlobalDict) {
2563                                 PyObject *pyGlobalDict_orig = PyDict_GetItemString(PyModule_GetDict(gameLogic), "globalDict"); // Same as importing the module.
2564                                 if (pyGlobalDict_orig) {
2565                                         PyDict_Clear(pyGlobalDict_orig);
2566                                         PyDict_Update(pyGlobalDict_orig, pyGlobalDict);
2567                                 } else {
2568                                         /* this should not happen, but cant find the original globalDict, just assign it then */
2569                                         PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
2570                                 }
2571                                 Py_DECREF(gameLogic);
2572                                 Py_DECREF(pyGlobalDict);
2573                                 return 1;
2574                         } else {
2575                                 Py_DECREF(gameLogic);
2576                                 PyErr_Clear();
2577                                 printf("Error could not marshall string\n");
2578                         }
2579                 }
2580                 else {
2581                         PyErr_Clear();
2582                         printf("Error, bge.logic failed to import bge.logic.globalDict will be lost\n");
2583                 }
2584         }
2585         return 0;
2586 }
2587
2588 void pathGamePythonConfig(char *path)
2589 {
2590         int len = strlen(gp_GamePythonPathOrig); // Always use the first loaded blend filename
2591         
2592         BLI_strncpy(path, gp_GamePythonPathOrig, sizeof(gp_GamePythonPathOrig));
2593
2594         /* replace extension */
2595         if (BLI_testextensie(path, ".blend")) {
2596                 strcpy(path+(len-6), ".bgeconf");
2597         } else {
2598                 strcpy(path+len, ".bgeconf");
2599         }
2600 }
2601
2602 void setGamePythonPath(const char *path)
2603 {
2604         BLI_strncpy(gp_GamePythonPath, path, sizeof(gp_GamePythonPath));
2605         BLI_cleanup_file(NULL, gp_GamePythonPath); /* not absolutely needed but makes resolving path problems less confusing later */
2606         
2607         if (gp_GamePythonPathOrig[0] == '\0')
2608                 BLI_strncpy(gp_GamePythonPathOrig, path, sizeof(gp_GamePythonPathOrig));
2609 }
2610
2611 // we need this so while blender is open (not blenderplayer)
2612 // loading new blendfiles will reset this on starting the
2613 // engine but loading blend files within the BGE wont overwrite gp_GamePythonPathOrig
2614 void resetGamePythonPath()
2615 {
2616         gp_GamePythonPathOrig[0] = '\0';
2617 }
2618
2619 #endif // WITH_PYTHON