python - SDL_Surface/LP_SDL_Surface 的 PySDL2 问题

标签 python sdl geometry-surface pysdl2

我用 python 3.3 和 PySDL2 0.5 运行 win7。创建曲面时(无论使用何种方法),我得到的是 LP_SDL_Surface 而不是 SDL_Surface。 LP_SDL_Surface 缺少您期望它具有的任何方法和属性。这是使用 documentation 中示例代码的问题:

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())

回溯是:

Traceback (most recent call last):
File "C:/.../test.py", line 35, in <module>
sys.exit(main())
File "C:/.../test.py", line 17, in main
print(image.h)
AttributeError: 'LP_SDL_Surface' object has no attribute 'h'

Google 搜索“LP_SDL_Surface”得到 0 (!) 个结果。

最佳答案

如果您使用较低级别的 SDL 方法(例如 sdl2.SDL_LoadBMP),您将不得不处理 ctypes 转换、引用 (byref) 和取消引用指针 ( .contents, .value).

因此对于特定问题,正如您已经评论过的那样,使用 print(image.contents.h) 就足够了。

pysdl2 (sdl2.ext) 提供了一些更高级别的类和方法,但是,如果需要,它们可以为您完成大部分转换。下面的代码无需触及 ctypes 即可实现相同的目标:

import os
os.environ["PYSDL2_DLL_PATH"] = os.path.dirname(os.path.abspath(__file__))

import sys
import sdl2
import sdl2.ext

def main():
    sdl2.ext.init()
    window = sdl2.ext.Window(
        title="Hello World!", size=(592, 460), flags=sdl2.SDL_WINDOW_SHOWN,
        position=(sdl2.SDL_WINDOWPOS_CENTERED, sdl2.SDL_WINDOWPOS_CENTERED))

    window.show()

    renderer = sdl2.ext.Renderer(window)
    factory = sdl2.ext.SpriteFactory(sdl2.ext.TEXTURE, renderer=renderer)
    spriterenderer = factory.create_sprite_render_system(window)

    image = factory.from_image("exampleimage.bmp")
    print(image.size[1])  # image.size = (w, h)

    running = True
    while running:
        for event in sdl2.ext.get_events():
            if event.type == sdl2.SDL_QUIT:
                running = False
                break
        spriterenderer.render(image)

    sdl2.ext.quit()
    return 0

if __name__ == '__main__':
    sys.exit(main())

它还利用纹理渲染,使用硬件加速,而不是表面 block 传输(基于软件)。

最后,使用更高级别的 sdl2.ext 您还可以实例化类(而不是必须自己编写一个全新的 Sprite 类),如 sdl2.ext.sprite.TextureSprite,并实现一个h属性:

class TextureSprite(sdl2.ext.TextureSprite):

    @property
    def h(self):
        """The height of the TextureSprite."""
        return self.size[1]

    @property
    def w(self):
        """The width of the TextureSprite."""
        return self.size[0]

关于python - SDL_Surface/LP_SDL_Surface 的 PySDL2 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18323908/

相关文章:

c++ - 如何获取SDL_CreateWindow定义

c++ - SDLSurface 的 OpenGL 纹理太暗

interpolation - 在高度图上插入点

python - Pygame 表面力学

python - bson.json_util 日期时间编码和解码最佳实践

python - QApplication 实例已经存在

C++ 错误 : request for member '...' in 'grmanager' which is of non-class type 'GraphicsManager'

python - Heroku 工作进程和时钟进程如何定价?

python - Tar --exclude 在 popen(shell=False) 时不起作用

c++ - 我找不到此函数中的内存泄漏 (SDL/OpenGL)