[PySDL] Issue with LP_SDL_Surface / SDL_Surface

Im running win7 with python 3.3 and PySDL2 0.5. When creating surfaces (no matter what method) i get an LP_SDL_Surface instead of a SDL_Surface. The LP_SDL_Surface lacks any of the methods and attribute you would expect it to have. Here is the issue using example code from the documentation:

Code:
import os
os.environ[“PYSDL2_DLL_PATH”] = os.path.dirname(os.path.abspath(file))

import sys
import ctypes
from sdl2 import *

def main():
SDL_Init(SDL_INIT_VIDEO)
window = SDL_CreateWindow(b"Hello World",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
592, 460, SDL_WINDOW_SHOWN)
windowsurface = SDL_GetWindowSurface(window)

image = SDL_LoadBMP(b"exampleimage.bmp")
SDL_BlitSurface(image, None, windowsurface, None)
print(image.h)

SDL_UpdateWindowSurface(window)
SDL_FreeSurface(image)

running = True
event = SDL_Event()
while running:
    while SDL_PollEvent(ctypes.byref(event)) != 0:
        if event.type == SDL_QUIT:
            running = False
            break

SDL_DestroyWindow(window)
SDL_Quit()
return 0

if name == “main”:
sys.exit(main())

and the traceback is:

Code:
Traceback (most recent call last):
File “C:/…/test.py”, line 35, in
sys.exit(main())
File “C:/…/test.py”, line 17, in main
print(image.h)
AttributeError: ‘LP_SDL_Surface’ object has no attribute ‘h’

A google search for “LP_SDL_Surface” brings 0 (!) results. I am am completely lost and not able to find out whats going wrong here, any help is appreciated.

i have =http://stackoverflow.com/questions/1413851/expected-lp-c-double-instance-instead-of-c-double-array-python-ctypes-errorfound 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.

i have found out (http://stackoverflow.com/questions/1413851/expected-lp-c-double-instance-instead-of-c-double-array-python-ctypes-error) 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.

On, Wed Aug 21, 2013, theASDF wrote:

i have =http://stackoverflow.com/questions/1413851/expected-lp-c-double-instance-instead-of-c-double-array-python-ctypes-errorfound 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.

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
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20130822/3017b1ec/attachment.pgp

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)

On, Thu Aug 22, 2013, theASDF wrote:

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

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.

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)

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
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20130822/46da3bfa/attachment.pgp