简体   繁体   English

通过PySDL2使用SDL_QueryTexture的问题

[英]Problems using SDL_QueryTexture through PySDL2

I am using PySDL2 and I am coding a little script that load a image on a windows but I am getting this error message "ctypes.ArgumentError: argument 4: : expected LP_c_int instance instead of int" when i use this function "SDL_QueryTexture". 我正在使用PySDL2,正在编写一个小的脚本,将一个图像加载到Windows上,但是当我使用此函数“ SDL_QueryTexture”时,出现此错误消息“ ctypes.ArgumentError:参数4 ::预期的LP_c_int实例而不是int”。 This is my code: 这是我的代码:

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

I know is something related to ctypes, i hope someone can help me. 我知道与ctypes有关,希望有人能帮助我。

SDL_QueryTexture gets pointers to ints to write result to, you cannot simply pass int here. SDL_QueryTexture获取指向要写入结果的int的指针,您不能在此处简单地传递int。 Workaround would be something like 解决方法将类似于

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

And then getting result from w.value and h.value . 然后从w.valueh.value获取结果。

However you already have a surface, why not just read width and height from it? 但是,您已经有了一个曲面,为什么不从中读取宽度和高度呢?

imageSurface.contents.w, imageSurface.contents.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM