Win32, COM, DLLs and DirectX
howard at eegsoftware.com
Tue Oct 22 21:08:01 CEST 2002
"GambiT" <gambit at yourwap.com> wrote in
news:3dafef7a$0$1367$626a54ce at news.free.fr:
> Hi everyone,
> I am quite new to Python, and as a game developper I am looking
> forward using it in development tools (like graphical editors and so
> on). I know useful graphical libraries like OpenGL and SDL are
> available (not mentionning GUIs) from Python code, but I wanted to do
> a little check by myself about DirectX.
> Talking about a specific tool, it can be possible to think about all
> DirectX features I need and use a DLL to be loaded in Python to call
> them. But for the moment I wanted to know how deep into DirectX I can
> dive from Python.
> First thing I tried is COM objects, or I would better say DirectX Type
> Librairy for Visual Basic. I do not quite understand really what is
> the difference between this and an usual COM object, but it seems I
> cannot access DirectX features this way. At best, I can obtain a
> IDirectx8 interface object, but no further (as DirectX methods are
> stored in vtables I cannot access and it seems other intrefaces do not
> support IDispatch - then I cannot instantiate any other type of
> Then I looked at dynwin and calldll stuff, and loaded DX DLLs. Methods
> are accessible this way, but as there is no type support for DX
> objetcs this way, I cannot do much with returned objects (for example
> I can create a device from DX top-level interface loaded from the DLL,
> but then I cannot access the device object's methods)
> I read on the pycom-dev ML that someone has coded the Invoke() and
> InvokeTypes() methods for PyITypeInfo, and making win32com.client
> support vtable interfaces. I am not there yet, as I do not have any
> experience of Python in-depth coding.
> Could someone give me some advice, or someone has maybe run into the
> same problem. Any kind of help would be appreciated.
First problem: the managment of DirectX state (variables, sequencing,
etc.) is difficult (directly) in Python when you are using dynamic objects
whose creation/destruction implies many things happening at once.
My solution to the problem was to use two pieces:
CDX is a (C++) thin (open source) wrapper over DirectX that manages many
of the details needed for DirectX (Note: only the 2D stuff really
works well). This is a game library and is fast and available.
This is a Python wrapper around (most) of CDX. It was originally
written by Jonathon Riehl and extended by me several years ago.
It uses (fairly recent versions of) SWIG to make a DLL and python
Using these two pieces, I can create DirectX windows, draw, track
keyboard/mouse actions, play sounds, etc. from Python scripts. Most of the
work I had to do (which the CDX developers kindly placed back in the CDX
source code) had to do with the issues of destructor sequencing (how to
delete DirectX parts in the correct order to avoid segment faults).
I use it prototype graphics displays/actions for programs which have to be
rewritten in C++. The Python code (and CDX calls) translate directly
(well, semicolons, parentheses, and Python objects aside).
pycdx is not currently posted anywhere (Did I say that I am behind schedule
everywhere ?) but could be.
More information about the Python-list