views:

58

answers:

1

I'm sketching on some fluid dynamics in Python. After a while, I'm looking for a bit more speed, so I rewrote the actual logic in C and put up some Python bindings (using SWIG).

My problem now is that I don't how to render it in a good way. The logic is run pixel by pixel so pixels are what I want to track and render.

Python gives my a TypeError if I try to make a function in the C lib that accepts a SDL_Surface*, I was probably a bit naive to think that PyGame mapped that easily directly to SDL. Python also seems unsure what to do if I make the C libs "init" return an SDL_Surface*.

What is a good way to do this? It wouldn't be a problem if I would just render everything in the C lib. But I want to put on some GUI there (using Python). The C lib already keeps track of which pixels are "dirty". Should I expose that list and let Python loop through it, call a function for every dirty pixel? Seems bad, since those kind of huge loops are the exact reason I wanted to rewrite parts of the app in C.

And before anyone suggests it, boost.python is a bit heavy to install right now (since I'm on Windows), so I'll just stick to SWIG for the moment (unless anyone has a clever way to install "just" boost.python?).

I'm hoping for a silver bullet here. How to make a C lib, running SDL, share a render target with Python, running PyGame?

A: 

Have you tried something like the following to get SDL_Surface* from python object?

PySurfaceObject *obj;
SDL_Surface *surf;
if (!PyArg_ParseTuple(args, 'O!', &PySurface_Type, &obj) {
    return NULL; # or other action for error
}
surf = PySurface_AsSurface(obj);
Denis Otkidach
I ended up creating the surface in Python, and making the C lib return a three item tuple (x,y,paletteindex) of the pixels in need of repainting. I actually got quite good performance with that, I assume its because Im using PyGame (SDL), good bindings apparently. Although I thought the calls would be more expensive.
mizipzor
`PySurface_AsSurface` is just a macro for struct member access, so it's very chip.
Denis Otkidach