• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Alpha Circle + Surface. Can't get sprite_set_alpha_from_sprite Manual example to work.

Hello,

First of all, its great to see the GMC back up and running again! Well done to all those involved.

Now the issue. I'm trying to achieve a simple alpha circle effect in GMS. The effect in question is one where a black and white circle sprite (with the whitest part in the center gradually becoming darker moving outward) is applied to a sprite and the whitest parts represent an alpha of close to 1 while the blackest parts represent an alpha close to 0 when applied to a particular sprite. In GM8, this was done easily with the sprite_set_from_alpha function but in GMS, we are required to use surfaces.

I know how surfaces work in theory, but getting them to work, particularly with respect to the above effect, is proving to be difficult. I used the manual's example as close to possible but couldn't get it to work after several attempts and looking for examples/tutorials.

Here's what I have (note that the resource names have been copied from the manual's example):

Create event:

spr_create = true;

Draw Event:

if spr_create
{
spr_create = false;
var surf, spr;
surf = surface_create(sprite_get_width(spr_Explosion_Alpha), sprite_get_height(spr_Explosion_Alpha));
surface_set_target(surf);
draw_clear_alpha(c_black, 0);
draw_sprite(spr_Gradient, 0, 0, 0);
sprite_index = sprite_create_from_surface(surf, 0, 0, sprite_width, sprite_height, false, false, 0,0) //sprite_get_xoffset(spr_Gradient), sprite_get_xoffset(spr_Gradient));
draw_clear_alpha(c_black, 0);
draw_sprite(spr_Explosion_Alpha, 0, 0,0) //sprite_get_xoffset(spr_Gradient), sprite_get_xoffset(spr_Gradient));
spr = sprite_create_from_surface(surf, 0, 0, sprite_width, sprite_height, false, false, 0,0) //sprite_get_xoffset(spr_Gradient), sprite_get_xoffset(spr_Gradient));
surface_reset_target();
surface_free(surf);
sprite_set_alpha_from_sprite(sprite_index, spr);
sprite_delete(spr);
}
draw_sprite(sprite_index, 0, 0, 0);

Room End:

sprite_delete(sprite_index);

NOTE: spr_Explosion_Alpha is intended to be the sprite to which the alpha circle applies and spr_Gradient is the alpha sprite itself.

Despite all this, all I get is a blank background. I'm not quite sure what the problem is. I've changed the clear alpha values, x and y values, sprite names etc. but I keep getting a blank screen. Any help to get the desired effect to work would be appreciated.

Thanks!
 
A

Aura

Guest
Using a surface isn't mandatory. You're simply creating a temporary sprite which holds the mixed effect of the two sprites. You can simply do this as well:

Code:
sprite_set_alpha_from_sprite(spr_Explosion_Alpha, spr_Gradient);
 
Thank you Aura.

In GM:S, if I put that function only, GM throws an error about that function not being applicable to permanent resources. It says in the manual that we need to either use the sprite_create_from_screen function (obsolete) or the surface one.

If using a surface is not required, could you please explain how this can be done without GM giving an error about using a permanent resource (In a GM8 example, the above code simply went in the Create Event and it worked, but in GMS, it gives an error)?
 
A

Aura

Guest
Ah yes, it can't be applied to permanent resources, so you'd have to create a dynamic sprite. Draw the sprite to a surface, create a sprite out of it, delete the surface and apply sprite_set_alpha_from_sprite() to this sprite. :)
 
Thank you again Aura.

I'm trying to do just that but am running into issues with the effect not showing (I simply get a blank screen). The above code was my attempt after several tries. I am still looking for a tutorial or example of how this can be done.

Also, just to get a clearer idea of the effect I'm hoping for, the 3 images below should help. The first and second images are the main sprite and alpha mask sprites respectively and the last image is the intended effect. This was done in GM 8 without issues but I cannot seem to replicate the effect in GM:S.
untitled.PNG untitled.PNG untitled.PNG
 
A

Aura

Guest
This should be a pretty simple one.

Create
Code:
spr_create = true;
spr = spr_gradient;
spr2 = spr_alpha;
Draw
Code:
if (spr_create)
{
    spr_create = false;
    var surf = surface_create(sprite_get_width(spr_gradient), sprite_get_height(spr_gradient));
    surface_set_target(surf);
    draw_clear_alpha(c_black, 0);
    draw_sprite(spr_gradient, 0, 0, 0);
    surface_reset_target();
    spr = sprite_create_from_surface(surf, 0, 0, sprite_get_width(spr_gradient), sprite_get_height(spr_gradient), false, false, 0, 0);
    surface_set_target(surf);
    draw_clear_alpha(c_black, 0);
    draw_sprite(spr_alpha, 0, 0, 0);
    surface_reset_target();
    spr2 = sprite_create_from_surface(surf, 0, 0, sprite_get_width(spr_alpha), sprite_get_height(spr_alpha), false, false, 0, 0);
    surface_free(surf);
    sprite_set_alpha_from_sprite(spr, spr2);
    sprite_delete(spr2);
}
Delete the spr sprite in the Room End event. It is the sprite with the effect you want. Let me know if it doesn't work! :)
 
Thank you once again Aura!

While the above code does not seem to work (I'm not sure if I'm doing something wrong on my end, still got the blank screen), I found an example which allowed me to achieve the effect. Here is the code for it below in case you'd like to compare it:

Create:

Code:
{
// Create both of the surfaces
su1=surface_create(room_width,room_height)
//if su1<0 exit;
su2=surface_create(room_width,room_height)
//if su2<0 exit;

// Draw on surface one
surface_set_target(su1);
draw_sprite(spr_gradient,0,x,y)
surface_reset_target();

// Draw on surface two
surface_set_target(su2);
draw_sprite(spr_texture,0,x,y);
surface_reset_target();
// Create sprites out of the surfaces
sp1=sprite_create_from_surface(su1,0,0,room_width,room_height,0,0,64,64)//1,0,0);
sp2=sprite_create_from_surface(su2,0,0,room_width,room_height,0,0,244,162)//1,0,0);
sprite_set_alpha_from_sprite(sp2,sp1);
}
Draw:

Code:
draw_sprite(sp2,0,x,y);
Though it appears to work, there is still one small issue. The below image is from the manual:

untitled.PNG

Is it possible to move around the drawing coordinates of "spr" (alpha channel sprite with the "10") after having applied the fused effect, while keeping the original "ind" sprite's coordinates unchanged (the red and purple sprite, not the fused one)? I tried doing this by drawing sp1's (surface which has the alpha channel sprite applied on it) coordinates on mouse_x and mouse_y but that didn't work. Right now, with the draw_sprite(sp2,0,x,y) line, if I replace x and y with mouse_x and mouse_y, the entire fused sprite will move accordingly, but I'd like to keep the main sprite unchanged while the alpha gradient moves.

In any case, thank you Aura for your efforts. Much appreciated!
 
Top