renpy / pygame_sdl2

Reimplementation of portions of the pygame API using SDL2.
GNU Lesser General Public License v2.1
328 stars 66 forks source link

Magenta borders around png with alpha channel (renderer) #65

Closed thomanimation closed 6 years ago

thomanimation commented 7 years ago

Thank you renpytom.

Today I switched from blitting surfaces to using the Renderer and saw amazing improvement in FPS.


image = pygame.image.load(filename).convert_alpha()
image_texture = renderer.load_texture(image)
image_sprite = pygame.render.Sprite(image_texture)
image_sprite.render((x,y))

Now I've stumbled upon a small issue, as seen here:

pygamesdl2_transparent

There's a magenta border around every PNG with transparency and I'd like to get rid of it.

A JPG or a PNG with non-transparent background displays without the magenta border. Also, there was no magenta border before switching to the renderer method: http://www.thomanimation.com/game/

I'm using pygame_sdl2 v. 2.0.0 (available in the Ubuntu Xenial Repo) Unfortunately I couldn't access the page http://nightly.renpy.org/pygame_sdl2/ (404)

bitcraft commented 7 years ago

You could try padding all your sprites with a 1 or 2 pixel transparent border.

leotada commented 7 years ago

Anything fix that?

eshikafe commented 7 years ago

This can be fixed by implementing the following changes in the pygame_sdl2/render.pyx file:

if DEBUG_DRAW_BBOX:
                # TODO: Adjust for rotation.
                self.adjust_rect(&dest_rect, &self.bounding_box, &real_dest)

                SDL_SetRenderDrawColor(tex.renderer, 0x00, 0x00, 0x00, 0x00)
                #SDL_RenderDrawRect(tex.renderer, &real_dest)

SDL_SetRenderDrawColor (tex.renderer, 0xFF,0x00,0xFF,0xFF) actually creates a magenta brush while SDL_RenderDrawRect draws a rect around the borders using the colour specified in SDL_SetRenderDrawColor.

So change the colour to black, with transparency, and don't draw a rect around the borders, otherwise, you will get black borders around each sprite. Compile the code from source and test.

I will try and submit a PR for this.

ghost commented 7 years ago

Is this fixed? I encountered the same thing when i made one of my TextureNode/texture a sprite because i wanted to be able to rotate it.

texture = obj.game.r.load_texture(image) ---> texture = pg.render.Sprite(texture) <---

tested eshikafe fix and it works