26 |
#include "game.h" |
#include "game.h" |
27 |
#include <iostream> |
#include <iostream> |
28 |
|
|
29 |
PyObject * data::globals; |
PyObject *data::globals; |
30 |
PyObject * python::module; |
PyObject *python::module; |
31 |
|
|
32 |
|
// defined in py_adonthell_wrap.cc |
33 |
|
PyObject *pass_instance (void *instance, const char* class_name); |
34 |
|
|
35 |
using namespace std; |
using namespace std; |
36 |
|
|
42 |
Py_Initialize (); |
Py_Initialize (); |
43 |
} |
} |
44 |
|
|
45 |
|
/** |
46 |
|
* Stop Python |
47 |
|
*/ |
48 |
void python::cleanup () |
void python::cleanup () |
49 |
{ |
{ |
50 |
// Cleanup the global namespace of python interpreter |
// Cleanup the global namespace of python interpreter |
123 |
// Make a C++ instance available to Python |
// Make a C++ instance available to Python |
124 |
PyObject *python::pass_instance (void *instance, const char *class_name) |
PyObject *python::pass_instance (void *instance, const char *class_name) |
125 |
{ |
{ |
126 |
char class_ptr[256]; |
return pass_instance (instance, class_name); |
|
char class_addr[256]; |
|
|
char *buffer = class_addr; |
|
|
|
|
|
// Construct the python shadow class matching the "instance" class |
|
|
strcat (strcpy (class_ptr, class_name), "Ptr"); |
|
|
|
|
|
// Construct SWIG's representation of the "instance" pointer |
|
|
*(buffer++) = '_'; |
|
|
buffer = ptr_to_string (buffer, &instance, sizeof (void *)); |
|
|
strcpy (buffer, "_p_"); |
|
|
strcpy (buffer+3, class_name); |
|
|
|
|
|
// Now create the Python object corresponding to "instance" |
|
|
PyObject *cls = PyDict_GetItemString (data::globals, class_ptr); |
|
|
PyObject *arg = Py_BuildValue ("(s)", class_addr); |
|
|
PyObject *res = PyObject_CallObject (cls, arg); |
|
|
|
|
|
#ifdef PY_DEBUG |
|
|
show_traceback (); |
|
|
#endif |
|
|
|
|
|
// Clean up |
|
|
Py_DECREF (arg); |
|
|
|
|
|
// Voila: "res" is 'identical' to "instance" :) |
|
|
return res; |
|
|
} |
|
|
|
|
|
// Convert a pointer to a string (like SWIG 1.3.7 does) |
|
|
char *python::ptr_to_string (char *c, void *ptr, int sz) |
|
|
{ |
|
|
static char hex[17] = "0123456789abcdef"; |
|
|
int i; |
|
|
unsigned char *u = (unsigned char *) ptr; |
|
|
register unsigned char uu; |
|
|
|
|
|
for (i = 0; i < sz; i++,u++) |
|
|
{ |
|
|
uu = *u; |
|
|
*(c++) = hex[(uu & 0xf0) >> 4]; |
|
|
*(c++) = hex[uu & 0xf]; |
|
|
} |
|
|
|
|
|
return c; |
|
127 |
} |
} |
128 |
|
|
129 |
PyObject * python::get_tuple (igzstream & file) |
PyObject * python::get_tuple (igzstream & file) |