(Py)SDL2:绘制带纹理的多边形
(Py)SDL2: Drawing a textured polygon
PySDL2 version: 0.9.3
SDL2 version: 2.0.3
我正在尝试使用 sdl_gfx
在 PySDL2 中将此图像渲染为多边形上的纹理
但是它的显示是完全扭曲的,在 SDL 的右下角可以看到 window:
我有 this python program in which I test all sdlgfx drawing functions that I implemented in a FrameBuffer class 负责绘图和渲染。它们都工作良好,除了抗锯齿多边形(中间的绿色六边形,但这是另一个问题)和纹理多边形。
为了提供更基本的脚本,我按照以下步骤绘制了带纹理的多边形:
# 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 有渲染图像的工厂函数,而且这些函数工作得很好,但我真的想让多边形的纹理选项也能工作。
您的问题是缺少事件循环实现。 TestEventProcessor
不处理基于纹理的 window 更新,而是纯软件缓冲区。相反,您想要的是:
## 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
示例以获得简单的实现。
我发现这只是 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.
PySDL2 version: 0.9.3
SDL2 version: 2.0.3
我正在尝试使用 sdl_gfx
在 PySDL2 中将此图像渲染为多边形上的纹理但是它的显示是完全扭曲的,在 SDL 的右下角可以看到 window:
我有 this python program in which I test all sdlgfx drawing functions that I implemented in a FrameBuffer class 负责绘图和渲染。它们都工作良好,除了抗锯齿多边形(中间的绿色六边形,但这是另一个问题)和纹理多边形。
为了提供更基本的脚本,我按照以下步骤绘制了带纹理的多边形:
# 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 有渲染图像的工厂函数,而且这些函数工作得很好,但我真的想让多边形的纹理选项也能工作。
您的问题是缺少事件循环实现。 TestEventProcessor
不处理基于纹理的 window 更新,而是纯软件缓冲区。相反,您想要的是:
## 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
示例以获得简单的实现。
我发现这只是 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.