[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Mixing Python and C in games



On Friday 17 February 2006 10:15, Sami Hangaslammi wrote:
> I've coded some simple extensions using Pyrex in the past, but I've
> mostly been putting off the effort of learning to mix Python and C
> properly. So I was wondering what experiences do people here have, and

ok.

i dont have too much experience of this .. started one job thing some 1,5years 
ago with plain c and sdl (a scroller thing), with the idea that i then use 
that c module from a python app that does networking, scheduling etc., and 
that worked ok, but i needed to add all sorts of features so i ended up with 
making the whole thing just in pygame. works good..

> what would you recommend. My goal is mostly to rewrite the inner loops
> in my Pygext framework for event handling, rendering and collisions.

does the event handling work so that you can just map py (or c?) funcs to 
things? i dont know if it needs to be in c. rendering and collisions sure..

> So I'm wonder how to best e.g. access Pygame surfaces and OpenGL
> functions directly to lose some overhead, and how easy it is to take
> advantage of, for example, the fast dict and set implementations in
> Python (my event manager uses a lot of set objects internally, and I'd
> rather not write my own set implementation in C).

well the Py C api seems quite nice, like there is PyDict_GetItem etc.

i have quite a lot of experience with it now from extending the Blender API, 
which is written in plain C with the Python.h provided functions .. well 
similar to pygame, in a way.

> Pyrex is my main candidate, as I'm the most familiar with it, and
> creating extension classes for my purposes seems pretty easy. I just
> have to figure out how to convert all usages of set and dict object
> from my Python code to Pyrex. I've been reading Boost::Python

that i dont know. if pyrex was perfect, would it not you just do the normal 
set([1,2,3]) - other_set ..business and generate those to the equivalent PyC 
function calls?

> about it. If I understood correctly, its main purpose is to interface
> with existing C-libraries, and it wouldn't be so well suited for
> rewriting small bits of exisiting Python code in C.

yah, i think this is true of both boost and swig.

> So, if any of you have used Pyrex for example to optimize inner loops
> in your games, I would appreciate some example code and pointers.

there are two pyrex-written python opengl engines, you know? soya3d and 
spineless.. soya3d goes i think even too far in writing everything in pyrex, 
on the one hand things like SDL (it does not use pygame), but even 
Shape.Get() implementation that can open blender to convert a .blend to 
soya-data first.. spineless is much different, uses pygame for sdl and 
implements more things in py, and just some things in pyrex. soya3d is 
complete, spineless incomplete and even abandoned? but nicer code afaik.

> Sami Hangaslammi

just a 2cents
~Toni