【问题标题】:Problems using SDL_QueryTexture through PySDL2通过 PySDL2 使用 SDL_QueryTexture 的问题
【发布时间】:2016-10-26 02:58:27
【问题描述】:

我正在使用 PySDL2,我正在编写一个在 Windows 上加载图像的小脚本,但是当我使用此函数时,我收到此错误消息“ctypes.ArgumentError: argument 4:: expected LP_c_int instance instead of int” SDL_QueryTexture”。这是我的代码:

"""Simple example for using sdl2 directly."""
import os
import sys
import ctypes
from sdl2 import *

def run():
    SDL_Init(SDL_INIT_VIDEO)
    window = SDL_CreateWindow(b"Hello World",
                                   SDL_WINDOWPOS_CENTERED,
                                   SDL_WINDOWPOS_CENTERED,
                                   459, 536, SDL_WINDOW_SHOWN)
    render = SDL_CreateRenderer(window, -1, 0)                                   
    fname = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                         "resources", "self-control.bmp")
    imageSurface = SDL_LoadBMP(fname.encode("utf-8"))
    imageTexture = SDL_CreateTextureFromSurface(render, imageSurface)
    SDL_FreeSurface(imageSurface)

    sourceRectangle = SDL_Rect()
    destinationRectangle = SDL_Rect()    
    SDL_QueryTexture(imageTexture, None, None, sourceRectangle.w, sourceRectangle.h)

    destinationRectangle.x = sourceRectangle.x = 0
    destinationRectangle.y = sourceRectangle.y = 0
    destinationRectangle.w = sourceRectangle.w
    destinationRectangle.h = sourceRectangle.h

    SDL_RenderCopy(render, imageTexture, sourceRectangle, destinationRectangle)

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

    SDL_DestroyWindow(window)
    SDL_Quit()
    return 0    

if __name__ == "__main__":
    sys.exit(run())

我知道是ctypes相关的东西,希望有人能帮帮我。

【问题讨论】:

    标签: python sdl-2 pysdl2


    【解决方案1】:

    SDL_QueryTexture 获取指向要写入结果的 int 的指针,您不能简单地在此处传递 int。解决方法类似于

    w = ctypes.c_int()
    h = ctypes.c_int()
    SDL_QueryTexture(imageTexture, None, None, w, h)
    

    然后从w.valueh.value 获取结果。

    但是你已经有了一个表面,为什么不从它读取宽度和高度呢?

    imageSurface.contents.w, imageSurface.contents.h
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      • 2016-05-17
      • 1970-01-01
      • 2020-03-30
      相关资源
      最近更新 更多