PySDL2 version: 0.9.3
SDL2 version: 2.0.3
我正在尝试使用 sdl_gfx 在 PySDL2 中将此图像渲染为多边形上的纹理
但它的显示完全失真,如 SDL 窗口右下角所示:
我有this python program我在其中测试了我在 a FrameBuffer class 中实现的所有 sdlgfx 绘图函数负责绘图和渲染。它们都运行良好,除了抗锯齿多边形(中间的绿色六边形,但这是另一个问题)和纹理多边形。
为了提供更基本的脚本,我按照以下步骤绘制了一个带纹理的多边形:
# Initialize SDL2 and window
import sdl2
import sdl2.ext
import sdl2.sdlgfx
import ctypes
sdl2.ext.init()
window = sdl2.ext.Window(size=(800,600))
window.show()
# Create renderer and factories
renderer = sdl2.ext.Renderer(window)
renderer.clear(0)
renderer.present()
# Create sprite factory to create textures with later
texture_factory = sdl2.ext.SpriteFactory(renderer=renderer)
# Create sprite factory to create surfaces with later
surface_factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE)
# Determine path to image to use as texture
RESOURCES = sdl2.ext.Resources(__file__, "LSD/resources")
image_path = RESOURCES.get_path("Memory.jpeg")
# set polygon coordinates
x = 100
row4 = 470
vx = [x, x+200, x+200, x]
vy = [row4-50, row4-50, row4+50, row4+50]
# Calculate the length of the vectors (which should be the same for x and y)
n = len(vx)
# Make sure all coordinates are integers
vx = map(int,vx)
vy = map(int,vy)
# Cast the list to the appropriate ctypes vectors reabable by
# the sdlgfx polygon functions
vx = ctypes.cast((sdl2.Sint16*n)(*vx), ctypes.POINTER(sdl2.Sint16))
vy = ctypes.cast((sdl2.Sint16*n)(*vy), ctypes.POINTER(sdl2.Sint16))
# Load the image on an SoftwareSprite
# The underlying surface is available at SoftwareSprite.surface
texture = surface_factory.from_image(image_path)
## RENDER THE POLYGON WITH TEXTURE
sdl2.sdlgfx.texturedPolygon(renderer.renderer, vx, vy, n,\
texture.surface, 0, 0)
# Swap buffers
renderer.present()
# Handle window close events
processor = sdl2.ext.TestEventProcessor()
processor.run(window)
sdl2.ext.quit()
上面的示例脚本只输出:
我发现使用 SDL2 进行 ctype 转换和所有操作都非常令人生畏,我很高兴我能走到这一步,但我似乎无法独自解决这个问题。有谁知道我在哪一步犯了错误,或者谁能指出我正确的方向?
作为旁注,我知道 PySDL 具有渲染图像的工厂函数,这些函数工作得很好,但我真的想让多边形的纹理选项也能工作。
最佳答案
我发现这只是 C/DLL 级别的底层 sdl2_gfx 库中的一个错误。 sdl2_gfx 的自制版本是 1.0.0,而版本 1.0.1(2014 年 6 月 15 日)已经发布。我在 Windows 和 Ubuntu 上测试了它,我在这两个平台上安装了 sdl2_gfx 1.0.1 并且纹理多边形绘制正确(尽管偏移参数的使用对我来说仍然有点可疑)。
底线:如果您想使用带纹理的多边形,请不要使用 sdl2_gfx 1.0.0,因为它在那里不起作用。尝试获取 v1.0.1。
关于python - (Py)SDL2 : Drawing a textured polygon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30099777/