Skip to main content
gamedev.net
🔒 Locked

Python, C-Types, and constants

Started by esrix Feb 18, 2007 at 9:27 PM 3 replies 4.7k views
Original Post
esrix
esrix
Now that Python 2.5 has C-types integrated, does anyone know if they managed to enable calling predefined constants in compiled libraries? For example, in C, I can call SDL_Init with flags like SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) However, will I be able to call the flags SDL_INIT_AUDIO and SDL_INIT_VIDEO while using C-types? I couldn't find any documentation on whether or not it was fixed...
Vorpy
Vorpy
I think you'll need to define the constants within python since python has no way of knowing what data those flags correspond to. This is similiar to how python does not know the name of the dll, the name of the function, the function's calling conventions, or the function's arguments or return value in advance. The person writing the binding, whether using the C interface, the python ctypes interface, or any other interface is responsible for knowing the details of how the function is called and how associated constants should be defined.

As far as the ctypes library itself, I have no idea how well it works. If it works as it is supposed to, I see no reason why you couldn't write the SDL binding in python, including defining those constants (just look into the SDL header files to see what the value of the constants should be).

The PyGame library already provides a binding for SDL for Python 2.4, with an init function designed to automatically initialize all imported PyGame modules that have an associated SDL system which needs to be initialized, and separate init functions for such modules if you want to control their initialization yourself...
mikeman
mikeman
Quote:

For example, in C, I can call SDL_Init with flags like SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO)

However, will I be able to call the flags SDL_INIT_AUDIO and SDL_INIT_VIDEO while using C-types?


ctypes have absolutely no business knowing the value of, say, SDL_INIT_VIDEO. There is not something special about these flags, they're just some integer values. Furthermore, the value of these constants is not defined in SDL.dll. It's defined in the C header file SDL.h. If you open that, you'll see in the beginning of it:

#define	SDL_INIT_TIMER		0x00000001#define SDL_INIT_AUDIO		0x00000010#define SDL_INIT_VIDEO		0x00000020#define SDL_INIT_CDROM		0x00000100#define SDL_INIT_JOYSTICK	0x00000200#define SDL_INIT_NOPARACHUTE	0x00100000	/* Don't catch fatal signals */#define SDL_INIT_EVENTTHREAD	0x01000000	/* Not supported on all OS's */#define SDL_INIT_EVERYTHING	0x0000FFFF


so, if you do in Python:
SDL_INIT_TIMER=0x00000001SDL_INIT_AUDIO=0x00000010SDL_INIT_VIDEO=0x00000020...


...then of course you can use those flags. Otherwise, you'll have to use an SDL binding that someone else has made. Pygame is an excellent wrapper for SDL.

[Edited by - mikeman on February 19, 2007 7:43:36 AM]
bubu LV
bubu LV
Pygame has also SDL wrapper made with ctypes: http://www.pygame.org/ctypes/ (look for SDL_ctypes_... files).
esrix
esrix
Sweet! Thanks a lot, guys :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.