pythonsdl-2rendererpysdl2

How do you access pySDL2 SoftwareSpriteRenderSystem Blendmode?


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.


Solution

  • Since you need or want sprites with solid colors, that contain an alpha channel, blending will only be relevant, if you actually render things on you screen and is not relevant, when you are creating textures.

    The blend flags from your initial question are only supported, if you do not use software-based rendering. This means that you must not used a SpriteFactory that provides a SoftwareSpriteRenderSystem, but a SpriteFactory with a TEXTURE sprite type.

    A brief example to create a texture-based renderer and factory for a window:

    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)
    

    Note that you will receive TextureSprite objects from the SpriteFactory, which do not allow you to access pixels in the same way as SoftwareSprite or SDL_Surface objects would do.

    To create a TextureSprite with an alpha channel, make sure that you use 32 bpp and an alpha mask in your factory.from_color() call.

    # 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