I recently decided to revamp an old 2D engine I wrote ages ago.
Before writing new code (for one, I wanted to convert the 2D blits to OpenGL and use textured quads), I decided it would be wiser to integrate Python to quickly write scripts to
- Define Levels
- Define Sprites and Animations
- Script enemies
- etc…
Unfortunately, integrating Python proved to be a harder task than I expected. I first took a look at SWIG but I didn’t like that it required conversion files… Boost.Python seemed a bit better as it seems to handle all the glue code internally so that you can simply build your C++ and automatically have your classes and functions available in Python.
After following the advise of Jon Skinner I decided to go with the “latest” builds of both Python (3.3.0) and Boost (1.52.0). This meant that I couldn’t use prepackaged versions of Boost, I would have to build it myself.
Building Python was a charm, it’s a very simple process.
Boost on the other hand, is not so simple. After fighting a lot with bjam to compile Python using the instructions in this tutorial, I decided to try a few things and simplify the problem. As described in the stack overflow question, things got hairy, but I decided to simplify things even more. I would pass as little arguments as bjam as possible and see what would happen. To my surprise, simplifying my user-config.jam to:
using python : 3.3 : C:\\Development\\Python-3.3.0\\PCBuild\\python.exe
: C:\\Development\\Python-3.3.0\\Include C:\\Development\\Python-3.3.0\\PC
: C:\\Development\\Python-3.3.0\\PCBuild : ;
And changing the command line to:
bjam --user-config=user-config.jam --with-python
simply rebuild all the targets and gave me what I was looking for!
Now, with Python and Boost.Python built… I was ready to write a test project to make sure everything worked as I expected.
Starting with the simplest program I could have, I went with:
int main(int argc, _TCHAR* argv[])
{
Py_Initialize();
Py_Finalize();
return 0;
}
Unfortunately, I didn’t get the results was expecting…
Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'
What is this ‘encodings’ you are talking about? Unless I skipped it while reading, I only realized while looking at my Sublime Text 2 install that Jon had a python26.zip file in the main folder of the application… after further inspection, this zip file contained all the core Python scripts. I therefore created a python33.zip containing the contents of my Python-3.3.0\Lib folder and it worked without a hitch!
I decided to follow SiCrane’s article on gamedev.net to have a simple example to follow. I simply added the two following lines to my very simple program:
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
And bam! Access violation when detaching the boost python dll…
To fix this, I simply add to define
BOOST_PYTHON_STATIC_LIB
for my project.
I’m continuing my experiments and will keep you posted with my progress.