2015-05-07 107 views
2
PySDL2 version: 0.9.3 
SDL2 version: 2.0.3 

我试图用sdl_gfx(PY)SDL2:绘制纹理多边形

enter image description here

但其显示完全扭曲渲染这个形象在PySDL2多边形纹理,如在右下角可以看出在SDL窗口:

enter image description here

我有this python program在我测试的人l我在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

我觉得这一切很艰巨与SDL2以及所有类型的转换一起工作,我非常高兴我获得了这么多,但我似乎无法自己解决这个问题。有人知道我在哪一步犯了错误,或者有谁能指出我的方向是正确的?作为一个旁注,我知道PySDL具有渲染图像的工厂函数,而且工作得很好,但我真的想要获得多边形的纹理选项。

回答

1

我发现这只是C/DLL级别底层sdl2_gfx库中的一个bug。 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。

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例如,对于一个简单的实现。

+0

感谢您的回复!遗憾的是,在实现这个事件循环之后,我仍然得到了与纹理相同的乱码显示。奇怪的是,图像的显示等工作,因为我也认为这是作为引擎盖下的纹理渲染完成的? –