Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带有SDL_曲面/LP_SDL_曲面的PySDL2问题_Python_Sdl_Geometry Surface_Pysdl2 - Fatal编程技术网

Python 带有SDL_曲面/LP_SDL_曲面的PySDL2问题

Python 带有SDL_曲面/LP_SDL_曲面的PySDL2问题,python,sdl,geometry-surface,pysdl2,Python,Sdl,Geometry Surface,Pysdl2,我用Python3.3和PySDL2 0.5运行win7。创建曲面时(无论采用何种方法),我都会得到一个LP_SDL_曲面,而不是SDL_曲面。LP_SDL_曲面缺少您期望它具有的任何方法和属性。下面是使用以下示例代码的问题: 回溯是: Traceback (most recent call last): File "C:/.../test.py", line 35, in <module> sys.exit(main()) File "C:/.../test.py", line 1

我用Python3.3和PySDL2 0.5运行win7。创建曲面时(无论采用何种方法),我都会得到一个LP_SDL_曲面,而不是SDL_曲面。LP_SDL_曲面缺少您期望它具有的任何方法和属性。下面是使用以下示例代码的问题:

回溯是:

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'
回溯(最近一次呼叫最后一次):
文件“C:/…/test.py”,第35行,在
sys.exit(main())
文件“C:/…/test.py”,第17行,在main中
打印(图像h)
AttributeError:“LP_SDL_曲面”对象没有属性“h”

谷歌搜索“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())
它还使用纹理渲染,使用硬件加速,而不是表面光点(基于软件)

最后,使用更高级别的
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]

我知道LP_*是一个ctypes对象,表示指向*的(长)指针。意识到这一点,我查看了pythonctypes文档,现在使用content属性来访问表面本身。所以我想问题已经解决了,但我仍然不确定这是否是故意的行为。你能详细说明你的解决方案吗?我也有这个问题。使用
surface.contents
会给我一个空指针访问错误。当然,或者更好,请参阅我在sdl论坛上从Marcus von Appen那里得到的解释:@user2697966您可以将其作为答案。
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]