DrawSurface with transparency not working [SOLVED]

Started by Lt. Smash, Sat 03/09/2011 16:40:46

Previous topic - Next topic

Lt. Smash

Hey guys!
I've a very noobish question. I have this
Code: ags

spriteBlackLayer = DynamicSprite.Create(320, 200, true);
spriteLightLayer = DynamicSprite.Create(320, 200, true);
	
surfaceBlackLayer = spriteBlackLayer.GetDrawingSurface();
surfaceLightLayer = spriteLightLayer.GetDrawingSurface();
	
surfaceBlackLayer.Clear(0);
surfaceLightLayer.DrawSurface(surfaceBlackLayer, 50);  // 50% transparency

object[1].Graphic = spriteLightLayer.Graphic;


But why the hell does DrawSurface doesn't work with any other transparency than 0? When I pass 0, object[1] becomes pitch black as it should; passing anything above (e.g. 1, 20, 60) results in a 100% transparent object[1].

Some info: Game is 32bit,  Object[1] has no image as default (image = 0).

I hope someone can help me here...

Calin Leafshade

This is a known issue that has plagued mankind for decades.

Drawing surfaces can't blend alpha channels so if you want to draw things with different transparencies then you need to use the AGSBlend plugin.

Lt. Smash

Ahaaaaaa... Thanks for the quick answer :)

I'll go and try your plugin...

monkey0506

That's actually not the reason for this issue. Even though your game is 32-bit, unless you've explicitly replaced sprite slot 0, then that graphic is actually an 8-bit image. Transparency doesn't work with 8-bit images, even if your game itself is 32-bit.

As Calin said, the DrawingSurface functions cannot blend/merge alpha channels, but the result would be that the resultant image would be flattened, with no alpha channel. This typically would result in your image having a pink haze where the alpha channel should have been (since the alpha channels are flattened against the AGS transparency color, which is magenta, RGB(255, 0, 255)).

If neither of your images have an alpha channel then you do not need the AGSBlend plugin. If only one of the images has an alpha channel, the resultant image is the same size as the image with the alpha channel, and the resultant image can have the same alpha transparency as the original image, then you can just use the DrawingSurface functions and then use DynamicSprite.CopyTransparencyMask on the resultant image, so again, you would not need the AGSBlend plugin.

The only time you need the AGSBlend plugin is if you specifically need to merge two or more alpha channels together at run-time.

Calin Leafshade

What has sprite slot 0 got to do with it?

He doesnt reference any sprites from the cache.

Atelier


Calin Leafshade

thats still not using sprite 0 for anything. Thats just the sprite he replaces.

monkey0506

You're right. Huh. Sorry I must have confused myself about him using sprite slot 0, when he wasn't using it during the drawing.

However, he also never indicated that both of these images have alpha channels that need to be merged. He's essentially tinting a magenta-transparent sprite. The hasAlphaChannel parameter to DynamicSprite.Create doesn't actually seem necessary based on the code being supplied. It shouldn't matter though since the resultant image would be flattened (after the non-existent alpha channel was applied during drawing), and would have no alpha channel anyway.

So, the fact remains that the AGSBlend plugin isn't relevant to the code snippets that were provided since there was never any alpha channels to be merged (based on the code provided).

What seems to be the likely culprit for this problem is that he's never releasing the DrawingSurface, so it's probably using an outdated cached version of the surface instead of the most recent one.

Code: ags
spriteBlackLayer = DynamicSprite.Create(320, 200);
spriteLightLayer = DynamicSprite.Create(320, 200);
	
surfaceBlackLayer = spriteBlackLayer.GetDrawingSurface();
surfaceLightLayer = spriteLightLayer.GetDrawingSurface();
	
surfaceBlackLayer.Clear(0);
surfaceLightLayer.DrawSurface(surfaceBlackLayer, 50);  // 50% transparency

surfaceBlackLayer.Release();
surfaceLightLayer.Release();

object[1].Graphic = spriteLightLayer.Graphic;

SMF spam blocked by CleanTalk