Thursday, November 17, 2011

GameMaker - get sprite's alpha channel

In some cases you may want to retrieve alpha channel of chosen sprite, to either apply it to other sprite, use for drawing, or in some other way.

The following script helps this:
/*
    sprite_get_alpha(sprite): sprite
    Returns alpha channel version of specified sprite
    Uses: sprites, surfaces, d3d fog settings
    Notes: modifies d3d fog settings.
*/

var w, h, b, t, l, i, s, c;
w = sprite_get_width(argument0)
h = sprite_get_height(argument0)
l = sprite_get_xoffset(argument0)
t = sprite_get_yoffset(argument0)
b = surface_create(w, h)
c = sprite_get_number(argument0)
surface_set_target(b)
d3d_set_fog(true, c_white, 0, 0)
for (i = 0; i < c; i += 1) {
    draw_clear(c_black)
    draw_sprite(argument0, i, l, t)
    if (i == 0)
    then s = sprite_create_from_surface(b, 0, 0, w, h, false, false, l, t)
    else sprite_add_from_surface(s, b, 0, 0, w, h, false, false)
}
d3d_set_fog(false, 0, 0, 0)
surface_reset_target()
surface_free(b)
return s

No comments:

Post a Comment