(Py)SDL2:绘制纹理多边形

时间:2015-05-07 11:28:27

标签: python textures sdl-2 texture-mapping pysdl2

PySDL2 version: 0.9.3
SDL2 version: 2.0.3

我正在尝试使用sdl_gfx将此图像渲染为PySDL2中多边形上的纹理

enter image description here

但它的显示完全失真,可以在SDL窗口的右下角看到:

enter image description here

我有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()

以上示例脚本只输出:

enter image description here

我发现使用ctype转换和SDL2工作非常令人生畏,而且我很高兴我做到了这一点,但我似乎无法自己解决这个问题。 。有人知道我犯了哪个错误,或者有人能指出我正确的方向吗?

正如旁注,我知道PySDL具有用于渲染图像的工厂功能,并且这些功能非常好,但我真的希望得到多边形的纹理选项。

2 个答案:

答案 0 :(得分:2)

我发现这只是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。

答案 1 :(得分:0)

您的问题是缺少事件循环实现。 TestEventProcessor不处理基于纹理的窗口更新,而是处理纯软件缓冲区。相反,你想要的是:

## RENDER THE POLYGON WITH TEXTURE
sdl2.sdlgfx.texturedPolygon(renderer.renderer, vx, vy, n,texture.surface, 0, 0)

running = True
while running:
    events = sdl2.ext.get_events()
    for event in events:
        if event.type == sdl2.SDL_QUIT:
            running = False
            break
    # Add some time step here
    renderer.present()

sdl2.ext.quit()

查看gfxdrawing.py示例,了解一个简单的实现。