[PySDL] Issue with LP_SDL_Surface / SDL_Surface |
theASDF
|
i have found out that LP_* is a ctypes thing and means (long) pointer to *
realizing this i checked the python ctypes documentation and now use the content attribute to get to the surface itself. so i guess the issue is solved, im still unsure that this is intended behavior though. |
|||||||||||
|
[PySDL] Issue with LP_SDL_Surface / SDL_Surface |
Marcus von Appen
Guest
|
On, Wed Aug 21, 2013, theASDF wrote:
This is the intended behaviour. Since SDL's C API for SDL_LoadBMP() returns a pointer to a SDL_Surface, ctypes returns a pointer (LP_*) to a SDL_Surface ctypes.Structure object. See http://docs.python.org/2/library/ctypes.html#pointers for a how to work with ctypes pointers. Be aware of the caveats, when you work with PySDL2's SDL API bindings. You have to check the input and return values properly. If sdl2.SDL_LoadBMP() fails (similar to SDL2's C API), the returned LP_SDL_Surface pointer is a null value: p_surface = SDL_LoadBMP(b"exampleimage.bmp") if not p_surface: # p_surface is NOT None, but evaluates to false # An error occured print(sdl2.SDL_GetError()) surface = p_surface.contents ... The unit tests under sdl2/test/ are a good starting point to check for examples on how to use C API bindings. If you want to use a slightly advanced, simplified way of doing things, take a look at the examples and the API documentation for the sdl2.ext module, which gives Python programmers a more comfortable access to several parts of the SDL2 API. Cheers Marcus _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||
|
theASDF
|
Thanks for the reply, and the examples. I will definitely check out the sdl2 tests, as i plan not use this as a learning opportunity and not use sdl2.ext but my own "extension" when i want more comfortable access.
A quick question though, to me it seems convenient to do something like test_surface = SDL_LoadBMP(b"exampleimage.bmp").contents as i dont really need to work with the pointer myself and the other functions seem to work fine when given the surface instead of the pointer to the surface aswell. I'm asking because in the surface.py file i can see that this is not what they expect to get. i.e: SDL_SetColorKey = _bind("SDL_SetColorKey", [POINTER(SDL_Surface), c_int, Uint32], c_int) |
|||||||||||
|
[PySDL] Issue with LP_SDL_Surface / SDL_Surface |
Marcus von Appen
Guest
|
On, Thu Aug 22, 2013, theASDF wrote:
This only works as long as the pointer is not a null value. If it is a null value, you can't access .contents without raising an exception. So, either create some safe wrappers or lend yourself code from sdl2.ext, which does a lot for you on that aspect.
The declaration above causes sdl2.SDL_SetColorKey to receive: - a ctypes pointer to a SDL_Surface ctypes.Structure object - this can be either a LP_SDL_Surface returned from another SDL function (which's the same as pointer(SDL_Surface) or POINTER(SDL_Surface)) or a POINTER(yoursurfacehere) or - for most ctypes implementations a plain SDL_Surface (beware of dragons, though) - some other ctypes values not of relevance here Cheers Marcus _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||||||
|