[Rubygame]Making transparent surfaces

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?
This is from a different project, but the issue is the same.
The reason I was sending the text to another surface was to apply an outline - I've removed the outline code for now.
Pretty much:
  1.  def draw_text(font,string,color)
  2.     size = @font[font].size_text(string)
  3.     text2 = @font[font].render(string,true,color)
  4.     surface = Surface.new [text2.w,text2.h],0,[HWSURFACE,SRCALPHA]
  5.     surface.to_display_alpha
  6.     text2.blit(surface,[0,0])
  7.     return surface
  8.   end
  9.  
  10. # other stuff . .  .
  11.     @text  = []
  12.     @text[0] = draw_text('ubuntu,16','Armour',[255,255,255])
  13.     @text_rect = []
  14.     @text_rect[0] = Rect.new(16,20,@text[0].make_rect.w,@text[0].make_rect.h)
  15.  
  16. #moar stuff . . .
  17.  
  18.     @text[0].blit($screen,@text_rect[0]) # within the draw method
  19.  
Expand to see the 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.
ZenVirZan wrote:
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.

And now this is when I shock you by revealing I've never coded in Ruby before and that I've never used RubyGame before.
Xilef wrote:
ZenVirZan wrote:
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.

And now this is when I shock you by revealing I've never coded in Ruby before and that I've never used RubyGame before.


Oh wow. :blank:
I am shocked D:

EDIT: New issue.
3 surfaces.
a = Loaded from png with transparency
b = Scratch (has been converted)
c = Scratch (has been converted)
Blitting surfaces a => screen, b => screen and c => screen works
Blitting surfaces a => b => screen and a => c => screen works
But blitting a => b => c => screen doesnt.
If two surfaces have to_display_alpha set, it doesn't show the PNG image blitted onto it.
:snip:
Double posted for some reason
ZenVirZan wrote:
3 surfaces.
a = Loaded from png with transparency
b = Scratch (has been converted)
c = Scratch (has been converted)
Blitting surfaces a => screen, b => screen and c => screen works
Blitting surfaces a => b => screen and a => c => screen works
But blitting a => b => c => screen doesnt.
If two surfaces have to_display_alpha set, it doesn't show the PNG image blitted onto it.

Could you tell me the dimensions of these surfaces and the screen you're blitting to?
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 :)

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=76908