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

Re: [pygame] C access to SDL surfaces



Dan Krol wrote:
My name is Dan. I'm currently developing a 2d engine, and I was
wondering if I could get some help from this list. I was going to use
Python only for some parts of it, but for prototyping I started making
the whole thing in Python, and thus Pygame. Now I'm thinking I might
just want to make most of the final version in Python, and just
optimize things in C (if it's fast enough, why not?), including
continuing to use Pygame.


Pygame has a complete C api that can be used to pass objects back and forth between Python. Your C code will need to link to Python and SDL. Then include the pygame.h

After that, working with Pygame objects in C is no different than working with any other Python object in C. This is exactly what you want, but there's not much documentation on it all. the Pygame header file isn't too big though and includes many more notes. It will look funky funky because it uses Python runtime linker.

The code should look similar to this...

#include <pygame.h>
import_pygame_surface();  //  Only once at startup time


// Create an SDL Surface and convert to a Python object
SDL_Surface* sdlSurface = SDL_CreateSurface(0, 640, 480, 32, ...);
PyObject* pySurface = PySurface_New(sdlSurface);


// Get the SDL surface from Python
SDL_Surface *surf;
PyObject *anyPyObject = ...;
if (PySurface_Check(anyPyObject) {
    surf = PySurface_AsSurface(anyPyObject);
} else {
    surf = NULL;
}


You'll find functions for converting between surfaces, rects,