pythonsdl-2pysdl2

How to pass SDL_Surface to SDL_LockTexture with PySDL2?


I'm trying to pass SDL_Surface members to SDL_LockTexture:

texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height)
surface = SDL_CreateRGBSurface(0, width, height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000)
SDL_LockTexture(texture, 0, ctypes.byref(surface.contents.pixels), ctypes.byref(surface.contents.pitch))

TypeError: byref() argument must be a ctypes instance, not 'int'

SDL_Surface is defined here: surface.py

...
class SDL_Surface(Structure):
_fields_ = [
            ...
            ("pitch", c_int),
            ("pixels", c_void_p),
            ...
...

SDL_LockTexture is defined here: render.py

SDL_LockTexture = _bind("SDL_LockTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(c_void_p), POINTER(c_int)], c_int)

After calling SDL_CreateRGBSurface the debugger says that both the pixels and pitch members of the surface are of type int. What am I doing wrong?


Solution

  • I was having the same problem, trying to pass the pixels into PyOpenGL functions. For some reason the field is stored as an int. You need to cast it to a void pointer. Try:

    SDL_LockTexture(texture, 0, ctypes.byref(ctypes.c_void_p(surface.contents.pixels)), ctypes.byref(ctypes.c_int(surface.contents.pitch))