加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – SDL_Surface / LP_SDL_Surface的PySDL2问题

发布时间:2020-12-20 13:36:44 所属栏目:Python 来源:网络整理
导读:我用 python 3.3和PySDL2 0.5运行win7.在创建曲面时(无论采用何种方法),我得到的是LP_SDL_Surface而不是SDL_Surface. LP_SDL_Surface缺少您期望它拥有的任何方法和属性.以下是使用 documentation中的示例代码的问题: import osos.environ["PYSDL2_DLL_PATH"
我用 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,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'

谷歌搜索“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())

它还使用纹理渲染,使用硬件加速,而不是表面blitting(基于软件).

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读