Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ and Python

Started by CMD Jan 8, 2007 at 12:37 PM 13 replies 2.6k views
Original Post
CMD
CMD
Greetings community. It's been a while. Anyway, I have a little problem. I want to write the entire game code in Python and link it/merge/embed with/into my engine (C++). I've read some articles/documentation on the subject, but I'm still not sure what's the best approach. What am I supposed to do? Embed Python in C++, extend C++ with Python, or the other way around? Can compiled Python modules be loaded and read by the executable? Regards
metrix
metrix
Hi,

I don't know much about what you want to do, but I do recall
SWIG. Maybe its what your looking for:

http://www.swig.org/

metrix
smr
smr
Yes, you can embed python in your application. The documentation explains how to do it the standard way.
Vorpy
Vorpy
Compiled python modules can be loaded by the embedded python interpreter. There are also functions to execute python source or bytecode from a buffer to allow the code to be stored and used in other ways if you want to use a different method for organizing the embedded code.
Kylotan
Kylotan
Both ways will work, though the recommended approach is to write the program in Python and code modules in C++ as required for performance. You'll see several threads on such matters in the Scripting Languages and Mods forum.
CMD
CMD
Thanks for replies,

Extending vs Embedding is an interesting topic. However, I couldn't find any valuable information on the net, so I'll just ask this here instead;

Should the application executable be written in C++ or rather pyc modules compiled with py2exe? In the case of py2exe, how can C++ modules be implemented? Also, would it make much sense to have all the C++ code in engine.dll, and let the Python game.exe load it?
doctorsixstring
doctorsixstring
Quote:
Original post by CMD
...I couldn't find any valuable information on the net...


Really? I found two excellent links on the first page of a Google search:

http://www.rexx.com/~dkuhlman/python_201/python_201.html#SECTION006000000000000000000
http://twistedmatrix.com/users/glyph/rant/extendit.html

Quote:
Original post by CMD
Should the application executable be written in C++ or rather pyc modules compiled with py2exe? In the case of py2exe, how can C++ modules be implemented? Also, would it make much sense to have all the C++ code in engine.dll, and let the Python game.exe load it?


As Kylotan already mentioned, the recommended approach is to write your game in pure Python, then rewrite performance-critical modules in C or C++, or better yet, Pyrex. You can then use py2exe to combine all your modules, regardless of what they were written in, into a single EXE.

- Mike
Vorpy
Vorpy
Extending python = writing python modules in another language

Embedding python = running the python interpreter from within another application

There is actually little to be gained from embedding python, besides the obvious advantage of integrating python with a project that is only using python for certain tasks. The pythonic approach to software development is to use python for everything, writing extensions only where necessary to access other systems or for performance (after it has been decided that the performance of a peice of code cannot be made satisfactory using only python and already existing extensions).

Embedding is also necessary if you want to write your own executable to control the context of the interpreter instead of using py2exe.
Luctus
Luctus
Whatever the case of extending or embedding, if you're working on interoperation between c++ and python, you most likely would want to check out Boost.Python if you haven't already. It will ease a lot (all?) of the headache of boxing/unboxing types, keeping reference counts, etc. that otherwise comes with working with the python API.
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
Kylotan
Kylotan
And to correct the apparently incorrect assumption; Py2exe is not a compiler, and doesn't produce .pyc files. It is merely a packager to allow you to distribute Python files as executables rather than requiring the Python interpreter to be installed.
CMD
CMD
In the case of extension, how is the game loop and window processing handled in a module?
Kylotan
Kylotan
Python has loops just the same as C++ does! No difference there. By 'window processing', do you mean Windows messages? You'd probably just register the WNDPROC as usual, and that function would call back into Python if required to pass any data that way.
CMD
CMD
Quote:
Original post by Kylotan
Python has loops just the same as C++ does! No difference there. By 'window processing', do you mean Windows messages? You'd probably just register the WNDPROC as usual, and that function would call back into Python if required to pass any data that way.


But how do I handle WinAPI specific functions such as WinMain and MsgProc? AFAIK, HINSTANCE, HRESULT, hWND etc, are unavailable to Python. (Using Boost.Python)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.