Forums Archived

The forums are being archived. See this topic for more information.

Generating Transparent Surfaces

Get help and support with Rubygame

Generating Transparent Surfaces

Postby indspenceable » Fri Nov 12, 2010 6:21 pm


I'm trying to generate reversed animations for my game. So, given a spritesheet with 16x16 tiles, which is loaded on a surface, I'm blitting each tile to a temporary surface, scaling that surface by [-1,1], then blitting that to the copy surface. Logically, makes sense - and its working.

However, I'm having an issue generating completely transparent surfaces. I load the initial spritesheet from a png, and its working fine. However, I don't want to use a colorkey (because I have no idea what colors are going to be used in the sprites, at this point; ideally I can find some way to do this without a colorkey) if I can avoid it. But, basically, is there anyway to generate an entirely transparent surface, which I can then blit onto and keep the transparency in whatever places there aren't yet pixels? At this point, its defaulting to black background, and everything I've tried seems to keep at least some sort of background there.
Posts: 8
Joined: Fri Jun 18, 2010 7:47 am

Re: Generating Transparent Surfaces

Postby Cyrus » Fri Nov 19, 2010 12:14 am

There's a way you can "paint" a surface transparent. First you ensure the newly created surface have 4 channels (I used to call #to_display_alpha because of an old little Rubygame bug, but I don't really know if its already fixed =), then you just #fill it with [0,0,0,0] .

The problem here is related to the #blit behavior that came from SDL. When you blit another surface onto this transparent one, the per-pixel alpha value isn't changed, so you'll see nothing since the original value (zero) is kept for all pixels (although the other channels do change after the blit).

The only way I could overcome this "issue" (if I got it right) was by disabling alpha blending on the source surface (in your case, the spritesheet). You can do this by calling #set_alpha and passing a second parameter with a value of zero. Like this:

Code: Select all
spritesheet.set_alpha 0,0

Now, when you blit each tile of the spritesheet, it will copy all four channels to the destination surface, including the transparent pixels (you won't even need to paint the destination surface transparent before, since the pixels' alpha value will be changed too).

Just a note: if, for some reason, you want to re-enable the alpha blending effect, just reset the per-surface alpha value with:

Code: Select all
sprites.set_alpha 255
Posts: 4
Joined: Thu Apr 02, 2009 8:19 pm
Location: Paraíba - Brazil

Return to Help & Support

Who is online

Users browsing this forum: No registered users and 1 guest