f4736f4c4239cfda7869452dc73a31eedf92f2c2
[blender.git] / source / gameengine / Ketsji / KX_PythonInit.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Initialize Python thingies.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #ifdef WIN32
39 #pragma warning (disable : 4786)
40 #endif //WIN32
41
42 #include "KX_PythonInit.h"
43
44 #include "SCA_IInputDevice.h"
45 #include "SCA_PropertySensor.h"
46 #include "SCA_RandomActuator.h"
47 #include "KX_ConstraintActuator.h"
48 #include "KX_IpoActuator.h"
49 #include "RAS_IRasterizer.h"
50 #include "RAS_ICanvas.h"
51 #include "MT_Vector3.h"
52 #include "MT_Point3.h"
53 #include "ListValue.h"
54 #include "KX_Scene.h"
55 #include "SND_DeviceManager.h"
56
57 #include "KX_PyMath.h"
58
59 // FIXME: Enable for access to blender python modules.  This is disabled because
60 // python has dependencies on a lot of other modules and is a pain to link.
61 //#define USE_BLENDER_PYTHON
62 #ifdef USE_BLENDER_PYTHON
63 //#include "BPY_extern.h"
64 #endif 
65
66 static void setSandbox(TPythonSecurityLevel level);
67
68
69 // 'local' copy of canvas ptr, for window height/width python scripts
70 static RAS_ICanvas* gp_Canvas = NULL;
71 static KX_Scene*        gp_KetsjiScene = NULL;
72 static RAS_IRasterizer* gp_Rasterizer = NULL;
73
74 /* Macro for building the keyboard translation */
75 //#define KX_MACRO_addToDict(dict, name) PyDict_SetItemString(dict, #name, PyInt_FromLong(SCA_IInputDevice::KX_##name))
76 #define KX_MACRO_addToDict(dict, name) PyDict_SetItemString(dict, #name, PyInt_FromLong(name))
77 /* For the defines for types from logic bricks, we do stuff explicitly... */
78 #define KX_MACRO_addTypesToDict(dict, name, name2) PyDict_SetItemString(dict, #name, PyInt_FromLong(name2))
79
80
81 // temporarily python stuff, will be put in another place later !
82 #include "KX_Python.h"
83 #include "SCA_PythonController.h"
84 // List of methods defined in the module
85
86 static PyObject* ErrorObject;
87 STR_String gPyGetRandomFloat_doc="getRandomFloat returns a random floating point value in the range [0..1)";
88
89 static PyObject* gPyGetRandomFloat(PyObject* self,
90                                                                                  PyObject* args, 
91                                                                                  PyObject* kwds)
92 {
93         return PyFloat_FromDouble(MT_random());
94 }
95
96
97
98
99
100 void GlobalConvertPythonVectorArg(PyObject* args, MT_Vector3 &pos)
101 {
102         PyObject* pylist;
103         PyArg_ParseTuple(args,"O",&pylist);
104
105         pos = MT_Vector3FromPyList(pylist);
106 }
107
108 void GlobalConvertPythonVectorArg(PyObject* args, MT_Vector4 &vec)
109 {
110         PyObject* pylist;
111         PyArg_ParseTuple(args,"O",&pylist);
112
113         vec = MT_Vector4FromPyList(pylist);
114 }
115
116
117 static PyObject* gPySetGravity(PyObject* self,
118                                                                                  PyObject* args, 
119                                                                                  PyObject* kwds)
120 {
121         MT_Vector3 vec = MT_Vector3(0., 0., 0.);
122         GlobalConvertPythonVectorArg(args, vec);
123
124         if (gp_KetsjiScene)
125                 gp_KetsjiScene->SetGravity(vec);
126         
127         Py_Return;
128 }
129
130
131 static bool usedsp = false;
132
133 // this gets a pointer to an array filled with floats
134 static PyObject* gPyGetSpectrum(PyObject* self,
135                                                                 PyObject* args, 
136                                                                 PyObject* kwds)
137 {
138         SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
139
140         PyObject* resultlist = PyList_New(512);
141
142         if (audiodevice)
143         {
144                 if (!usedsp)
145                 {
146                         audiodevice->StartUsingDSP();
147                         usedsp = true;
148                 }
149                         
150                 float* spectrum = audiodevice->GetSpectrum();
151
152                 for (int index = 0; index < 512; index++)
153                 {
154                         PyList_SetItem(resultlist, index, PyFloat_FromDouble(spectrum[index]));
155                 }
156         }
157
158         return resultlist;
159 }
160
161
162
163 static void gPyStartDSP(PyObject* self,
164                                                 PyObject* args, 
165                                                 PyObject* kwds)
166 {
167         SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
168
169         if (audiodevice)
170         {
171                 if (!usedsp)
172                 {
173                         audiodevice->StartUsingDSP();
174                         usedsp = true;
175                 }
176         }
177 }
178
179
180
181 static void gPyStopDSP(PyObject* self,
182                                            PyObject* args, 
183                                            PyObject* kwds)
184 {
185         SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
186
187         if (audiodevice)
188         {
189                 if (usedsp)
190                 {
191                         audiodevice->StopUsingDSP();
192                         usedsp = false;
193                 }
194         }
195 }
196
197
198
199 static struct PyMethodDef game_methods[] = {
200         {"getCurrentController",
201         (PyCFunction) SCA_PythonController::sPyGetCurrentController,
202         METH_VARARGS, SCA_PythonController::sPyGetCurrentController__doc__},
203         {"addActiveActuator",(PyCFunction) SCA_PythonController::sPyAddActiveActuator,
204         METH_VARARGS, SCA_PythonController::sPyAddActiveActuator__doc__},
205         {"getRandomFloat",(PyCFunction) gPyGetRandomFloat,
206         METH_VARARGS,gPyGetRandomFloat_doc.Ptr()},
207         {"setGravity",(PyCFunction) gPySetGravity, METH_VARARGS,"set Gravitation"},
208         {"getSpectrum",(PyCFunction) gPyGetSpectrum, METH_VARARGS,"get audio spectrum"},
209         {"stopDSP",(PyCFunction) gPyStopDSP, METH_VARARGS,"stop using the audio dsp (for performance reasons)"},
210         {NULL, (PyCFunction) NULL, 0, NULL }
211 };
212
213
214 static PyObject* gPyGetWindowHeight(PyObject* self, 
215                                                                                  PyObject* args, 
216                                                                                  PyObject* kwds)
217 {
218         int height = (gp_Canvas ? gp_Canvas->GetHeight() : 0);
219
220                 PyObject* heightval = PyInt_FromLong(height);
221                 return heightval;
222 }
223
224
225
226 static PyObject* gPyGetWindowWidth(PyObject* self, 
227                                                                                  PyObject* args, 
228                                                                                  PyObject* kwds)
229 {
230                 
231
232         int width = (gp_Canvas ? gp_Canvas->GetWidth() : 0);
233         
234                 PyObject* widthval = PyInt_FromLong(width);
235                 return widthval;
236 }
237
238
239
240 // temporarility visibility thing, will be moved to rasterizer/renderer later
241 bool gUseVisibilityTemp = false;
242
243 static PyObject* gPyEnableVisibility(PyObject* self, 
244                                                                                  PyObject* args, 
245                                                                                  PyObject* kwds)
246 {
247         int visible;
248         if (PyArg_ParseTuple(args,"i",&visible))
249         {
250             gUseVisibilityTemp = (visible != 0);
251         }
252         else
253         {
254           Py_Return;         
255         }
256    Py_Return;
257 }
258
259
260
261 static PyObject* gPyShowMouse(PyObject* self, 
262                                                                                  PyObject* args, 
263                                                                                  PyObject* kwds)
264 {
265         int visible;
266         if (PyArg_ParseTuple(args,"i",&visible))
267         {
268             if (visible)
269                 {
270                         if (gp_Canvas)
271                                 gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
272                 } else
273                 {
274                         if (gp_Canvas)
275                                 gp_Canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
276                 }
277         }
278         
279    Py_Return;
280 }
281
282
283
284 static PyObject* gPySetMousePosition(PyObject* self, 
285                                                                                  PyObject* args, 
286                                                                                  PyObject* kwds)
287 {
288         int x,y;
289         if (PyArg_ParseTuple(args,"ii",&x,&y))
290         {
291             if (gp_Canvas)
292                         gp_Canvas->SetMousePosition(x,y);
293         }
294         
295    Py_Return;
296 }
297
298
299
300 static PyObject* gPySetBackgroundColor(PyObject* self, 
301                                                                                  PyObject* args, 
302                                                                                  PyObject* kwds)
303 {
304         
305         MT_Vector4 vec = MT_Vector4(0., 0., 0.3, 0.);
306         GlobalConvertPythonVectorArg(args, vec);
307
308         if (gp_Canvas)
309         {
310                 gp_Rasterizer->SetBackColor(vec[0], vec[1], vec[2], vec[3]);
311         }
312    Py_Return;
313 }
314
315
316
317 static PyObject* gPySetMistColor(PyObject* self, 
318                                                                                  PyObject* args, 
319                                                                                  PyObject* kwds)
320 {
321         
322         MT_Vector3 vec = MT_Vector3(0., 0., 0.);
323         GlobalConvertPythonVectorArg(args, vec);
324
325         if (gp_Rasterizer)
326         {
327                 gp_Rasterizer->SetFogColor(vec[0], vec[1], vec[2]);
328         }
329    Py_Return;
330 }
331
332
333
334 static PyObject* gPySetMistStart(PyObject* self, 
335                                                                                  PyObject* args, 
336                                                                                  PyObject* kwds)
337 {
338
339         float miststart;
340         if (PyArg_ParseTuple(args,"f",&miststart))
341         {
342                 if (gp_Rasterizer)
343                 {
344                         gp_Rasterizer->SetFogStart(miststart);
345                 }
346         }
347    Py_Return;
348 }
349
350
351
352 static PyObject* gPySetMistEnd(PyObject* self, 
353                                                                                  PyObject* args, 
354                                                                                  PyObject* kwds)
355 {
356
357         float mistend;
358         if (PyArg_ParseTuple(args,"f",&mistend))
359         {
360                 if (gp_Rasterizer)
361                 {
362                         gp_Rasterizer->SetFogEnd(mistend);
363                 }
364         }
365    Py_Return;
366 }
367
368
369
370 static PyObject* gPyMakeScreenshot(PyObject* self,
371                                                                         PyObject* args,
372                                                                         PyObject* kwds)
373 {
374         char* filename;
375         if (PyArg_ParseTuple(args,"s",&filename))
376         {
377                 if (gp_Canvas)
378                 {
379                         gp_Canvas->MakeScreenShot(filename);
380                 }
381         }
382         Py_Return;
383 }
384
385
386
387 STR_String      gPyGetWindowHeight__doc__="getWindowHeight doc";
388 STR_String      gPyGetWindowWidth__doc__="getWindowWidth doc";
389 STR_String      gPyEnableVisibility__doc__="enableVisibility doc";
390 STR_String      gPyMakeScreenshot__doc__="make Screenshot doc";
391 STR_String      gPyShowMouse__doc__="showMouse(bool visible)";
392 STR_String      gPySetMousePosition__doc__="setMousePosition(int x,int y)";
393
394 static struct PyMethodDef rasterizer_methods[] = {
395   {"getWindowWidth",(PyCFunction) gPyGetWindowWidth,
396    METH_VARARGS, gPyGetWindowWidth__doc__.Ptr()},
397    {"getWindowHeight",(PyCFunction) gPyGetWindowHeight,
398    METH_VARARGS, gPyGetWindowHeight__doc__.Ptr()},
399   {"makeScreenshot",(PyCFunction)gPyMakeScreenshot,
400         METH_VARARGS, gPyMakeScreenshot__doc__.Ptr()},
401    {"enableVisibility",(PyCFunction) gPyEnableVisibility,
402    METH_VARARGS, gPyEnableVisibility__doc__.Ptr()},
403         {"showMouse",(PyCFunction) gPyShowMouse,
404    METH_VARARGS, gPyShowMouse__doc__.Ptr()},
405    {"setMousePosition",(PyCFunction) gPySetMousePosition,
406    METH_VARARGS, gPySetMousePosition__doc__.Ptr()},
407   {"setBackgroundColor",(PyCFunction)gPySetBackgroundColor,METH_VARARGS,"set Background Color (rgb)"},
408   {"setMistColor",(PyCFunction)gPySetMistColor,METH_VARARGS,"set Mist Color (rgb)"},
409   {"setMistStart",(PyCFunction)gPySetMistStart,METH_VARARGS,"set Mist Start(rgb)"},
410   {"setMistEnd",(PyCFunction)gPySetMistEnd,METH_VARARGS,"set Mist End(rgb)"},
411   
412   { NULL, (PyCFunction) NULL, 0, NULL }
413 };
414
415
416
417 // Initialization function for the module (*must* be called initGameLogic)
418
419 static char GameLogic_module_documentation[] =
420 "This is the Python API for the game engine of GameLogic"
421 ;
422
423 static char Rasterizer_module_documentation[] =
424 "This is the Python API for the game engine of Rasterizer"
425 ;
426
427
428
429 PyObject* initGameLogic(KX_Scene* scene) // quick hack to get gravity hook
430 {
431         PyObject* m;
432         PyObject* d;
433
434         gp_KetsjiScene = scene;
435
436         gUseVisibilityTemp=false;
437
438         // Create the module and add the functions
439         m = Py_InitModule4("GameLogic", game_methods,
440                                            GameLogic_module_documentation,
441                                            (PyObject*)NULL,PYTHON_API_VERSION);
442
443         // Add some symbolic constants to the module
444         d = PyModule_GetDict(m);
445
446         ErrorObject = PyString_FromString("GameLogic.error");
447         PyDict_SetItemString(d, "error", ErrorObject);
448
449         // XXXX Add constants here
450         /* To use logic bricks, we need some sort of constants. Here, we associate */
451         /* constants and sumbolic names. Add them to dictionary d.                 */
452
453         /* 1. true and false: needed for everyone                                  */
454         KX_MACRO_addTypesToDict(d, KX_TRUE,  SCA_ILogicBrick::KX_TRUE);
455         KX_MACRO_addTypesToDict(d, KX_FALSE, SCA_ILogicBrick::KX_FALSE);
456
457         /* 2. Property sensor                                                      */
458         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_EQUAL,      SCA_PropertySensor::KX_PROPSENSOR_EQUAL);
459         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_NOTEQUAL,   SCA_PropertySensor::KX_PROPSENSOR_NOTEQUAL);
460         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_INTERVAL,   SCA_PropertySensor::KX_PROPSENSOR_INTERVAL);
461         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_CHANGED,    SCA_PropertySensor::KX_PROPSENSOR_CHANGED);
462         KX_MACRO_addTypesToDict(d, KX_PROPSENSOR_EXPRESSION, SCA_PropertySensor::KX_PROPSENSOR_EXPRESSION);
463
464         /* 3. Constraint actuator                                                  */
465         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCX);
466         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCY);
467         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_LOCZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_LOCZ);
468         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTX, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTX);
469         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTY, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTY);
470         KX_MACRO_addTypesToDict(d, KX_CONSTRAINTACT_ROTZ, KX_ConstraintActuator::KX_ACT_CONSTRAINT_ROTZ);
471
472         /* 4. Ipo actuator, simple part                                            */
473         KX_MACRO_addTypesToDict(d, KX_IPOACT_PLAY,     KX_IpoActuator::KX_ACT_IPO_PLAY);
474         KX_MACRO_addTypesToDict(d, KX_IPOACT_PINGPONG, KX_IpoActuator::KX_ACT_IPO_PINGPONG);
475         KX_MACRO_addTypesToDict(d, KX_IPOACT_FLIPPER,  KX_IpoActuator::KX_ACT_IPO_FLIPPER);
476         KX_MACRO_addTypesToDict(d, KX_IPOACT_LOOPSTOP, KX_IpoActuator::KX_ACT_IPO_LOOPSTOP);
477         KX_MACRO_addTypesToDict(d, KX_IPOACT_LOOPEND,  KX_IpoActuator::KX_ACT_IPO_LOOPEND);
478
479         /* 5. Random distribution types                                            */
480         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_CONST,      SCA_RandomActuator::KX_RANDOMACT_BOOL_CONST);
481         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_UNIFORM,    SCA_RandomActuator::KX_RANDOMACT_BOOL_UNIFORM);
482         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_BOOL_BERNOUILLI, SCA_RandomActuator::KX_RANDOMACT_BOOL_BERNOUILLI);
483         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_CONST,       SCA_RandomActuator::KX_RANDOMACT_INT_CONST);
484         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_UNIFORM,     SCA_RandomActuator::KX_RANDOMACT_INT_UNIFORM);
485         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_INT_POISSON,     SCA_RandomActuator::KX_RANDOMACT_INT_POISSON);
486         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_CONST,     SCA_RandomActuator::KX_RANDOMACT_FLOAT_CONST);
487         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_UNIFORM,   SCA_RandomActuator::KX_RANDOMACT_FLOAT_UNIFORM);
488         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NORMAL,    SCA_RandomActuator::KX_RANDOMACT_FLOAT_NORMAL);
489         KX_MACRO_addTypesToDict(d, KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL, SCA_RandomActuator::KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL);
490
491         // Check for errors
492         if (PyErr_Occurred())
493     {
494                 Py_FatalError("can't initialize module GameLogic");
495     }
496
497         return d;
498 }
499
500
501
502 // Python Sandbox code
503 // override builtin functions import() and open()
504
505
506 PyObject *KXpy_open(PyObject *self, PyObject *args)
507 {
508         PyErr_SetString(PyExc_RuntimeError, "Sandbox: open() function disabled!\nGame Scripts should not use this function.");
509         return NULL;
510 }
511
512
513
514 PyObject *KXpy_import(PyObject *self, PyObject *args)
515 {
516         char *name;
517         PyObject *globals = NULL;
518         PyObject *locals = NULL;
519         PyObject *fromlist = NULL;
520         PyObject *l, *m, *n;
521
522         if (!PyArg_ParseTuple(args, "s|OOO:m_import",
523                 &name, &globals, &locals, &fromlist))
524             return NULL;
525
526         /* check for builtin modules */
527         m = PyImport_AddModule("sys");
528         l = PyObject_GetAttrString(m, "builtin_module_names");
529         n = PyString_FromString(name);
530         
531         if (PySequence_Contains(l, n)) {
532                 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
533         }
534
535         /* quick hack for GamePython modules 
536                 TODO: register builtin modules properly by ExtendInittab */
537         if (!strcmp(name, "GameLogic") || !strcmp(name, "GameKeys") ||
538                 !strcmp(name, "Rasterizer")) {
539                 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
540         }
541                 
542         PyErr_Format(PyExc_ImportError,
543                  "Import of external Module %.20s not allowed.", name);
544         return NULL;
545
546 }
547
548
549
550 static PyMethodDef meth_open[] = {
551         { "open", KXpy_open, METH_VARARGS,
552                 "(disabled)"}
553 };
554
555
556 static PyMethodDef meth_import[] = {
557         { "import", KXpy_import, METH_VARARGS,
558                 "our own import"}
559 };
560
561
562
563 //static PyObject *g_oldopen = 0;
564 //static PyObject *g_oldimport = 0;
565 //static int g_security = 0;
566
567
568 void setSandbox(TPythonSecurityLevel level)
569 {
570     PyObject *m = PyImport_AddModule("__builtin__");
571     PyObject *d = PyModule_GetDict(m);
572         PyObject *meth = PyCFunction_New(meth_open, NULL);
573
574         switch (level) {
575         case psl_Highest:
576                 //if (!g_security) {
577                         //g_oldopen = PyDict_GetItemString(d, "open");
578                         PyDict_SetItemString(d, "open", meth);
579                         meth = PyCFunction_New(meth_import, NULL);
580                         PyDict_SetItemString(d, "__import__", meth);
581                         //g_security = level;
582                 //}
583                 break;
584         /*
585         case psl_Lowest:
586                 if (g_security) {
587                         PyDict_SetItemString(d, "open", g_oldopen);
588                         PyDict_SetItemString(d, "__import__", g_oldimport);
589                         g_security = level;
590                 }
591         */
592         default:
593                 break;
594         }
595 }
596
597 /**
598  * Python is not initialised.
599  */
600 PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level)
601 {
602         STR_String pname = progname;
603         Py_SetProgramName(pname.Ptr());
604         Py_NoSiteFlag=1;
605         Py_FrozenFlag=1;
606         Py_Initialize();
607
608         //importBlenderModules()
609         
610         setSandbox(level);
611
612         PyObject* moduleobj = PyImport_AddModule("__main__");
613         return PyModule_GetDict(moduleobj);
614 }
615
616 void exitGamePlayerPythonScripting()
617 {
618         Py_Finalize();
619 }
620
621 /**
622  * Python is already initialized.
623  */
624 PyObject* initGamePythonScripting(const STR_String& progname, TPythonSecurityLevel level)
625 {
626         STR_String pname = progname;
627         Py_SetProgramName(pname.Ptr());
628         Py_NoSiteFlag=1;
629         Py_FrozenFlag=1;
630
631         setSandbox(level);
632
633         PyObject* moduleobj = PyImport_AddModule("__main__");
634         return PyModule_GetDict(moduleobj);
635 }
636
637
638
639 void exitGamePythonScripting()
640 {
641 }
642
643
644
645 PyObject* initRasterizer(RAS_IRasterizer* rasty,RAS_ICanvas* canvas)
646 {
647         gp_Canvas = canvas;
648         gp_Rasterizer = rasty;
649
650
651   PyObject* m;
652   PyObject* d;
653
654   // Create the module and add the functions
655   m = Py_InitModule4("Rasterizer", rasterizer_methods,
656                      Rasterizer_module_documentation,
657                      (PyObject*)NULL,PYTHON_API_VERSION);
658
659   // Add some symbolic constants to the module
660   d = PyModule_GetDict(m);
661   ErrorObject = PyString_FromString("Rasterizer.error");
662   PyDict_SetItemString(d, "error", ErrorObject);
663
664   // XXXX Add constants here
665
666   // Check for errors
667   if (PyErr_Occurred())
668     {
669       Py_FatalError("can't initialize module Rasterizer");
670     }
671
672   return d;
673 }
674
675
676
677 /* ------------------------------------------------------------------------- */
678 /* GameKeys: symbolic constants for key mapping                              */
679 /* ------------------------------------------------------------------------- */
680
681 static char GameKeys_module_documentation[] =
682 "This modules provides defines for key-codes"
683 ;
684
685
686
687 static struct PyMethodDef gamekeys_methods[] = {
688         { NULL, (PyCFunction) NULL, 0, NULL }
689 };
690
691
692
693 PyObject* initGameKeys()
694 {
695         PyObject* m;
696         PyObject* d;
697
698         // Create the module and add the functions
699         m = Py_InitModule4("GameKeys", gamekeys_methods,
700                                            GameKeys_module_documentation,
701                                            (PyObject*)NULL,PYTHON_API_VERSION);
702
703         // Add some symbolic constants to the module
704         d = PyModule_GetDict(m);
705
706         // XXXX Add constants here
707
708         KX_MACRO_addTypesToDict(d, AKEY, SCA_IInputDevice::KX_AKEY);
709         KX_MACRO_addTypesToDict(d, BKEY, SCA_IInputDevice::KX_BKEY);
710         KX_MACRO_addTypesToDict(d, CKEY, SCA_IInputDevice::KX_CKEY);
711         KX_MACRO_addTypesToDict(d, DKEY, SCA_IInputDevice::KX_DKEY);
712         KX_MACRO_addTypesToDict(d, EKEY, SCA_IInputDevice::KX_EKEY);
713         KX_MACRO_addTypesToDict(d, FKEY, SCA_IInputDevice::KX_FKEY);
714         KX_MACRO_addTypesToDict(d, GKEY, SCA_IInputDevice::KX_GKEY);
715         KX_MACRO_addTypesToDict(d, HKEY, SCA_IInputDevice::KX_HKEY);
716         KX_MACRO_addTypesToDict(d, IKEY, SCA_IInputDevice::KX_IKEY);
717         KX_MACRO_addTypesToDict(d, JKEY, SCA_IInputDevice::KX_JKEY);
718         KX_MACRO_addTypesToDict(d, KKEY, SCA_IInputDevice::KX_KKEY);
719         KX_MACRO_addTypesToDict(d, LKEY, SCA_IInputDevice::KX_LKEY);
720         KX_MACRO_addTypesToDict(d, MKEY, SCA_IInputDevice::KX_MKEY);
721         KX_MACRO_addTypesToDict(d, NKEY, SCA_IInputDevice::KX_NKEY);
722         KX_MACRO_addTypesToDict(d, OKEY, SCA_IInputDevice::KX_OKEY);
723         KX_MACRO_addTypesToDict(d, PKEY, SCA_IInputDevice::KX_PKEY);
724         KX_MACRO_addTypesToDict(d, QKEY, SCA_IInputDevice::KX_QKEY);
725         KX_MACRO_addTypesToDict(d, RKEY, SCA_IInputDevice::KX_RKEY);
726         KX_MACRO_addTypesToDict(d, SKEY, SCA_IInputDevice::KX_SKEY);
727         KX_MACRO_addTypesToDict(d, TKEY, SCA_IInputDevice::KX_TKEY);
728         KX_MACRO_addTypesToDict(d, UKEY, SCA_IInputDevice::KX_UKEY);
729         KX_MACRO_addTypesToDict(d, VKEY, SCA_IInputDevice::KX_VKEY);
730         KX_MACRO_addTypesToDict(d, WKEY, SCA_IInputDevice::KX_WKEY);
731         KX_MACRO_addTypesToDict(d, XKEY, SCA_IInputDevice::KX_XKEY);
732         KX_MACRO_addTypesToDict(d, YKEY, SCA_IInputDevice::KX_YKEY);
733         KX_MACRO_addTypesToDict(d, ZKEY, SCA_IInputDevice::KX_ZKEY);
734         
735         KX_MACRO_addTypesToDict(d, ZEROKEY, SCA_IInputDevice::KX_ZEROKEY);              
736         KX_MACRO_addTypesToDict(d, ONEKEY, SCA_IInputDevice::KX_ONEKEY);                
737         KX_MACRO_addTypesToDict(d, TWOKEY, SCA_IInputDevice::KX_TWOKEY);                
738         KX_MACRO_addTypesToDict(d, THREEKEY, SCA_IInputDevice::KX_THREEKEY);
739         KX_MACRO_addTypesToDict(d, FOURKEY, SCA_IInputDevice::KX_FOURKEY);              
740         KX_MACRO_addTypesToDict(d, FIVEKEY, SCA_IInputDevice::KX_FIVEKEY);              
741         KX_MACRO_addTypesToDict(d, SIXKEY, SCA_IInputDevice::KX_SIXKEY);                
742         KX_MACRO_addTypesToDict(d, SEVENKEY, SCA_IInputDevice::KX_SEVENKEY);
743         KX_MACRO_addTypesToDict(d, EIGHTKEY, SCA_IInputDevice::KX_EIGHTKEY);
744         KX_MACRO_addTypesToDict(d, NINEKEY, SCA_IInputDevice::KX_NINEKEY);              
745                 
746         KX_MACRO_addTypesToDict(d, CAPSLOCKKEY, SCA_IInputDevice::KX_CAPSLOCKKEY);
747                 
748         KX_MACRO_addTypesToDict(d, LEFTCTRLKEY, SCA_IInputDevice::KX_LEFTCTRLKEY);      
749         KX_MACRO_addTypesToDict(d, LEFTALTKEY, SCA_IInputDevice::KX_LEFTALTKEY);                
750         KX_MACRO_addTypesToDict(d, RIGHTALTKEY, SCA_IInputDevice::KX_RIGHTALTKEY);      
751         KX_MACRO_addTypesToDict(d, RIGHTCTRLKEY, SCA_IInputDevice::KX_RIGHTCTRLKEY);    
752         KX_MACRO_addTypesToDict(d, RIGHTSHIFTKEY, SCA_IInputDevice::KX_RIGHTSHIFTKEY);  
753         KX_MACRO_addTypesToDict(d, LEFTSHIFTKEY, SCA_IInputDevice::KX_LEFTSHIFTKEY);
754                 
755         KX_MACRO_addTypesToDict(d, ESCKEY, SCA_IInputDevice::KX_ESCKEY);
756         KX_MACRO_addTypesToDict(d, TABKEY, SCA_IInputDevice::KX_TABKEY);
757         KX_MACRO_addTypesToDict(d, RETKEY, SCA_IInputDevice::KX_RETKEY);
758         KX_MACRO_addTypesToDict(d, SPACEKEY, SCA_IInputDevice::KX_SPACEKEY);
759         KX_MACRO_addTypesToDict(d, LINEFEEDKEY, SCA_IInputDevice::KX_LINEFEEDKEY);              
760         KX_MACRO_addTypesToDict(d, BACKSPACEKEY, SCA_IInputDevice::KX_BACKSPACEKEY);
761         KX_MACRO_addTypesToDict(d, DELKEY, SCA_IInputDevice::KX_DELKEY);
762         KX_MACRO_addTypesToDict(d, SEMICOLONKEY, SCA_IInputDevice::KX_SEMICOLONKEY);
763         KX_MACRO_addTypesToDict(d, PERIODKEY, SCA_IInputDevice::KX_PERIODKEY);          
764         KX_MACRO_addTypesToDict(d, COMMAKEY, SCA_IInputDevice::KX_COMMAKEY);            
765         KX_MACRO_addTypesToDict(d, QUOTEKEY, SCA_IInputDevice::KX_QUOTEKEY);            
766         KX_MACRO_addTypesToDict(d, ACCENTGRAVEKEY, SCA_IInputDevice::KX_ACCENTGRAVEKEY);        
767         KX_MACRO_addTypesToDict(d, MINUSKEY, SCA_IInputDevice::KX_MINUSKEY);            
768         KX_MACRO_addTypesToDict(d, SLASHKEY, SCA_IInputDevice::KX_SLASHKEY);            
769         KX_MACRO_addTypesToDict(d, BACKSLASHKEY, SCA_IInputDevice::KX_BACKSLASHKEY);
770         KX_MACRO_addTypesToDict(d, EQUALKEY, SCA_IInputDevice::KX_EQUALKEY);            
771         KX_MACRO_addTypesToDict(d, LEFTBRACKETKEY, SCA_IInputDevice::KX_LEFTBRACKETKEY);        
772         KX_MACRO_addTypesToDict(d, RIGHTBRACKETKEY, SCA_IInputDevice::KX_RIGHTBRACKETKEY);      
773                 
774         KX_MACRO_addTypesToDict(d, LEFTARROWKEY, SCA_IInputDevice::KX_LEFTARROWKEY);
775         KX_MACRO_addTypesToDict(d, DOWNARROWKEY, SCA_IInputDevice::KX_DOWNARROWKEY);
776         KX_MACRO_addTypesToDict(d, RIGHTARROWKEY, SCA_IInputDevice::KX_RIGHTARROWKEY);  
777         KX_MACRO_addTypesToDict(d, UPARROWKEY, SCA_IInputDevice::KX_UPARROWKEY);                
778         
779         KX_MACRO_addTypesToDict(d, PAD2 , SCA_IInputDevice::KX_PAD2);
780         KX_MACRO_addTypesToDict(d, PAD4 , SCA_IInputDevice::KX_PAD4);
781         KX_MACRO_addTypesToDict(d, PAD6 , SCA_IInputDevice::KX_PAD6);
782         KX_MACRO_addTypesToDict(d, PAD8 , SCA_IInputDevice::KX_PAD8);
783                 
784         KX_MACRO_addTypesToDict(d, PAD1 , SCA_IInputDevice::KX_PAD1);
785         KX_MACRO_addTypesToDict(d, PAD3 , SCA_IInputDevice::KX_PAD3);
786         KX_MACRO_addTypesToDict(d, PAD5 , SCA_IInputDevice::KX_PAD5);
787         KX_MACRO_addTypesToDict(d, PAD7 , SCA_IInputDevice::KX_PAD7);
788         KX_MACRO_addTypesToDict(d, PAD9 , SCA_IInputDevice::KX_PAD9);
789                 
790         KX_MACRO_addTypesToDict(d, PADPERIOD, SCA_IInputDevice::KX_PADPERIOD);
791         KX_MACRO_addTypesToDict(d, PADSLASHKEY, SCA_IInputDevice::KX_PADSLASHKEY);
792         KX_MACRO_addTypesToDict(d, PADASTERKEY, SCA_IInputDevice::KX_PADASTERKEY);
793                 
794                 
795         KX_MACRO_addTypesToDict(d, PAD0, SCA_IInputDevice::KX_PAD0);
796         KX_MACRO_addTypesToDict(d, PADMINUS, SCA_IInputDevice::KX_PADMINUS);
797         KX_MACRO_addTypesToDict(d, PADENTER, SCA_IInputDevice::KX_PADENTER);
798         KX_MACRO_addTypesToDict(d, PADPLUSKEY, SCA_IInputDevice::KX_PADPLUSKEY);
799                 
800                 
801         KX_MACRO_addTypesToDict(d, F1KEY , SCA_IInputDevice::KX_F1KEY);
802         KX_MACRO_addTypesToDict(d, F2KEY , SCA_IInputDevice::KX_F2KEY);
803         KX_MACRO_addTypesToDict(d, F3KEY , SCA_IInputDevice::KX_F3KEY);
804         KX_MACRO_addTypesToDict(d, F4KEY , SCA_IInputDevice::KX_F4KEY);
805         KX_MACRO_addTypesToDict(d, F5KEY , SCA_IInputDevice::KX_F5KEY);
806         KX_MACRO_addTypesToDict(d, F6KEY , SCA_IInputDevice::KX_F6KEY);
807         KX_MACRO_addTypesToDict(d, F7KEY , SCA_IInputDevice::KX_F7KEY);
808         KX_MACRO_addTypesToDict(d, F8KEY , SCA_IInputDevice::KX_F8KEY);
809         KX_MACRO_addTypesToDict(d, F9KEY , SCA_IInputDevice::KX_F9KEY);
810         KX_MACRO_addTypesToDict(d, F10KEY, SCA_IInputDevice::KX_F10KEY);
811         KX_MACRO_addTypesToDict(d, F11KEY, SCA_IInputDevice::KX_F11KEY);
812         KX_MACRO_addTypesToDict(d, F12KEY, SCA_IInputDevice::KX_F12KEY);
813                 
814         KX_MACRO_addTypesToDict(d, PAUSEKEY, SCA_IInputDevice::KX_PAUSEKEY);
815         KX_MACRO_addTypesToDict(d, INSERTKEY, SCA_IInputDevice::KX_INSERTKEY);
816         KX_MACRO_addTypesToDict(d, HOMEKEY , SCA_IInputDevice::KX_HOMEKEY);
817         KX_MACRO_addTypesToDict(d, PAGEUPKEY, SCA_IInputDevice::KX_PAGEUPKEY);
818         KX_MACRO_addTypesToDict(d, PAGEDOWNKEY, SCA_IInputDevice::KX_PAGEDOWNKEY);
819         KX_MACRO_addTypesToDict(d, ENDKEY, SCA_IInputDevice::KX_ENDKEY);
820
821
822         // Check for errors
823         if (PyErr_Occurred())
824     {
825                 Py_FatalError("can't initialize module GameKeys");
826     }
827
828         return d;
829 }
830
831 void PHY_SetActiveScene(class KX_Scene* scene)
832 {
833         gp_KetsjiScene = scene;
834 }