veandco / go-sdl2

SDL2 binding for Go
https://godoc.org/github.com/veandco/go-sdl2
BSD 3-Clause "New" or "Revised" License
2.21k stars 221 forks source link

Alpha not working for big?? images #431

Closed Matias-Barrios closed 5 years ago

Matias-Barrios commented 5 years ago

I have a weird issue when trying to add alpha to the background of my game. I have this function :

// Get a texture from a window, a renderer and a path
func GetTexture(w *sdl.Window, r *sdl.Renderer, path string, alpha uint8) *sdl.Texture {
    surface, err := w.GetSurface()
    if err != nil {
        panic(err)
    }
    surface.FillRect(nil, 0)

    surfaceImg, err := img.Load(path)
    if err != nil {
        log.Fatalf("Failed to load PNG: %s\n", err)
        os.Exit(4)
    }
    surfaceImg.SetAlphaMod(alpha)
    textureImg, err := r.CreateTextureFromSurface(surfaceImg)
    if err != nil {
        log.Fatalf("Failed to create texture: %s\n", err)
        os.Exit(5)
    }
    surfaceImg.Free()
    return textureImg
}

The problem is, it seems to make textures transparent only when they are small (??). for instance I can make each small block in my game transparent but it does not seem to affect the background, although both are drawn in the same way.

veeableful commented 5 years ago

Hi @Matias-Barrios, could you please create a very minimal example that demonstrate this issue? That would really help me in figuring out the issue. Thanks!

Matias-Barrios commented 5 years ago

Hi @veeableful . I have created this small repo which only draws a background and a block :+1: https://github.com/Matias-Barrios/transparency_bug

You can clone and play around with these two lines : https://github.com/Matias-Barrios/transparency_bug/blob/master/main.go#L32-L33

You will see that although the transparency of the block changes the background is not affected. Please let me know if this is minimalist enough

veeableful commented 5 years ago

Hi @Matias-Barrios, it seems like the texture needs to set a blend mode via textureImg.SetBlendMode(sdl.BLENDMODE_BLEND) so any image can be made translucent. Otherwise, it seems like the image needs to be saved with RGBA color type (based on quick investigation using fotoforensics.com on the images).

veeableful commented 5 years ago

Oh and thank you very much for the mini example! That was super helpful! I think if I tried to recreate the image, I wouldn't have had the same problem as the image I created for assisting with the investigation already had RGBA color type :smiley:

Matias-Barrios commented 5 years ago

@veeableful understood. Thanks for digging into this. I'll try to add the changes and contact you back in case of issues. Awesome! :)