I'm developing a map editing application, and all is going well, except for blitting transparent surfaces from scratch to the screen.
If i blit a .png with transparency, it merges them fine - the sprite on the background.
But if I blit a surface (Surface.new([width;height])), it leaves a black box, rather than the transparency.
How to I get the alpha channels to merge correctly from Surfaces made from scratch?
You need to use surface to_display_alpha after creating the surface with surface::new
Even when using #to_display_alpha, the issue still persists, with and without the SRCALPHA flag.
Want to post some of your code?
You need to assign a surface to surface.to_display_alpha and blit that, you should be able to write:
surface = surface.to_display_alpha
and it should work, the reason is that it's not a self-modifying function it has a return value of a new surface.
Its merging now, thank you :D
Its a pity they don't make it more noticeable in the documentation - It's such a simple mistake.
:snip:
Double posted for some reason
surface 'a' is the tileset png file, alpha included.
surface 'b' is a 32x32 tile, blitted to from the tileset image.
surface 'c' is a 1024x800 layer, and has the surface 'a's blitted to it to make the map. (#to_display_alpha is applied)
'screen' is a 1280x800 Screen instance.
If I put #to_display_alpha on surface b, it doesn't show up.
If I omit it, it does show, but the alpha is replaced with black.
Why are you using surface B at all? You should use clipping to blit a 32x32 section of A straight to C and do any modifications there to save processing speed.
You can do that with the third blit parameter which is a rectangle, instead of having only the x and y position you can add in the width and height of the blit, using surfaces to clip is very inefficient as you're copying entire blocks of ram to a fresh set of memory twice.
$a.blit($c,[destinationX,destinationY],[tileX,tileY,32,32])
I have a grid class, which has a method #get_tile(id)
It returns a 32x32 tile surface from the tileset, depending on the id.
It already uses the source rect function of #blit to get the tile onto surface b to begin with.
Even though b may not be required, i'd still like to know for future reference.
I don't think I can help any further then, I generally don't blit through more than two surfaces, it's probably a problem with preserving the alpha channel (Perhaps it's being overwritten when blitting from B to C?), it would be a Rubygame specific bug (And from looking at the git page, it's full of them)
I managed to fix it somehow.
I removed the SRCALPHA flag from the loaded tileset .png and it works now.
Thanks for your help, Xilef :)