[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [pygame] I'm having problems with the pygame C api
Ben Smith wrote:
> Hi Everyone,
>
> I'm having some problems with the pygame C api, which I'm using in my
> pymousetrap module.
>
> My code is getting a false value for PySurface_Check, though I can look at
> the PyObject that I'm sending to PySurface_Check and it looks like a surface.
>
> So, I decided to write the attached test program. Unfortunately, I get a
> segfault when I try to use PySurface_New so I don't get far enough to
> determine what's going on with PySurface_Check.
ah, i know the problem :]
pygame's C api actually does some "runtime linking" which means you
don't need to use the pygame libraries when linking your program.
this means you need to do a simple "import" before using any of the
functions from a pygame library.
each pygame module has a sort of "vtable" which is a list of pointers to
all the functions it contains. the macros you are using simply cast the
generic vtable function pointers and cast them to the correct prototype.
your code will work fine, you just need to setup the internal vtable
properly. so whichever pygame module you want to use functions from, you
just "import" it. you need the surface functions, so call
import_pygame_surface();
before making any calls to the functions in that module. for example,
you can also use the Rect C API functions by doing
import_pygame_rect();
this whole process may seem a bit strange to you, but once you
understand it, there are certain benefits to working like this. it is
all explained at this python tutorial...
http://www.python.org/doc/current/ext/using-cobjects.html
a couple other things on your little "quicky" program. since you created
your surface with the pygame surface constructor PySurface_NEW(), it is
a real python object, not just an SDL surface. to "free" the object you
need to reduce it's refcount "Py_DECREF(pysurf);"
another quick warning.. one thing never tested is initializing some
parts of SDL through the SDL functions and then using them through
pygame. there are many ways to likely break things in pygame, but just
think things out logically and it should work. for example, you are
calling SDL_SetVideoMode() in your code. this could cause some potential
wierdness for any pygame functions that desire information about pygame.
lastly, not all calls in pygame have a C API equivelent. i mainly just
added functions that support the different pygame object types. if you
find there's some functions that are really needed and aren't available
in the API, let me know and they can easily be added to the C API. but
before you need that, remember you can call any pygame function from C,
by using functions as python objects..
PyObject *mod, *func, *screen;
mod = PyImport_ImportModule("pygame.display");
func = PyDict_GetItemString(PyModule_GetDict(mod), "set_mode");
screen = PyObject_CallFunction(func, "(ii)", 640, 480);
Py_DECREF(mod);
anyways, you get the idea :]
if you end up stuck, i'll be glad to lend more help
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org