0

The problem is very simple. I want to change the Blendmode for my SDL2 Renderer in Python, but I don't know how to access the Renderer.

The PySDL2 Documentation here states that you can define colors with an Alpha channel:

class sdl2.ext.Renderer

blendmode: The blend mode used for drawing operations (fill and line). This can be a value of

. SDL_BLENDMODE_NONE for no blending

. SDL_BLENDMODE_BLEND for alpha blending

. SDL_BLENDMODE_ADD for additive color blending

. SDL_BLENDMODE_MOD for multiplied color blending

All I need is to set the Blendmode to SDL_BLENDMODE_BLEND so that I can use the Alpha channels. My problem is that I'm using a SoftwareSpriteRenderSystem

self.Renderer = self.SpriteFactory.create_sprite_render_system(self.Window)

There isn't any clear way to change the blendmode here. I can try doing the following:

SDL_SetRenderDrawBlendMode(self.Renderer,SDL_BLENDMODE_BLEND)

But this returns:

ArgumentError: argument 1: <type 'exceptions.TypeError'>: expected LP_SDL_Renderer instance instead of SoftwareSpriteRenderSystem

Could anyone help me out please? I'd just like to be able to get some transparent Sprites (from_rect sprites), but it's proving difficult because the SDL_renderer is inaccessible.

4

1 回答 1

1

由于您需要或想要包含 Alpha 通道的纯色精灵,因此只有在您实际在屏幕上渲染内容并且在创建纹理时不相关时,混合才会相关。

如果您不使用基于软件的渲染,则仅支持您最初问题中的混合标志。这意味着您不能使用SpriteFactory提供 a 的 a SoftwareSpriteRenderSystem,而是使用SpriteFactory带有TEXTUREsprite 类型的 a 。

为窗口创建基于纹理的渲染器和工厂的简短示例:

window = sdl2.ext.Window("Test", size=(800,600))
renderer = sdl2.ext.Renderer(window)
#     use renderer.blendmode = ... to change the blend mode
factory = sdl2.ext.SpriteFactory(sdl2.ext.TEXTURE, renderer=renderer)
rsystem = factory.create_sprite_render_system(window)

请注意,您将从 接收TextureSprite对象SpriteFactory,这不允许SoftwareSprite您以与对象相同的方式访问像素SDL_Surface

要创建TextureSprite带有 Alpha 通道的通道,请确保在factory.from_color()通话中使用 32 bpp 和 Alpha 掩码。

# Create a solid, quadratic, red texture of 100px in size, using a RGBA color layout.
factory.from_color(0xFF000000, size=(100, 100),
                   masks=(0xFF000000,           # red channel
                          0x00FF0000,           # green channel
                          0x0000FF00,           # blue channel
                          0x000000FF))          # alpha channel
于 2014-09-01T13:14:19.090 回答