T
Thorn
Guest
Hello! I have written a simple script that will return an array containing the outline of a sprite. I'm very excited about it and I just felt like sharing it with you guys. I'm fairly new to the community, but I figured the best place to post this would be in the section called "programing".
scr_get_sprite_outline:
Only run this once and store it in an array.
It's a little dirty, and if you guys have any thoughts on improving I am more than happy to receive them.
After getting the array you'll have to draw it out, so I made this too:
scr_draw_sprite_outline:
you provide the object that the outline is going around and the outline array you got from scr_get_sprite_outline().
both scripts have to be in the draw event. If I'm posting this in the wrong place, or shouldn't be posting it at all, please let me know so that I won't make the same mistake next time.
Cheers!
EDIT: forgot to mention the use. I use it to show that an item is selected, but it can be used for a lot of things.
scr_get_sprite_outline:
Code:
var sprite = argument[0];
var sprite_w = sprite_get_width(sprite);
var sprite_h = sprite_get_height(sprite);
var pixel_buffer = buffer_create(sprite_w*sprite_h*4, buffer_grow, 1);
var surf = surface_create(sprite_w, sprite_h);
var outline = array_create(0);
surface_set_target(surf);
draw_sprite(sprite, 0, sprite_get_xoffset(sprite), sprite_get_yoffset(sprite));
buffer_get_surface(pixel_buffer, surf, 0, 0, 0);
for(var x_=0; x_ < sprite_w; ++x_){
for(var y_=0; y_ < sprite_h; ++y_){
var a = buffer_peek(pixel_buffer, (x_ + sprite_w * y_) * 4 + 3, buffer_u8);
if(a != 0)
{
if(x_ == 0 or buffer_peek(pixel_buffer, ((x_-1) + sprite_w * y_) * 4 + 3, buffer_u8) == 0){
var last_index = array_length_1d(outline);
outline[last_index] = array_create(2);
array_set(outline[last_index], 0, x_-1);
array_set(outline[last_index], 1, y_);
}
if(x_ == sprite_w-1 or buffer_peek(pixel_buffer, ((x_+1) + sprite_w * y_) * 4 + 3, buffer_u8) == 0){
var last_index = array_length_1d(outline);
outline[last_index] = array_create(2);
array_set(outline[last_index], 0, x_+1);
array_set(outline[last_index], 1, y_);
}
if(y_ == 0 or buffer_peek(pixel_buffer, (x_ + sprite_w * (y_-1)) * 4 + 3, buffer_u8) == 0){
var last_index = array_length_1d(outline);
outline[last_index] = array_create(2);
array_set(outline[last_index], 0, x_);
array_set(outline[last_index], 1, y_-1);
}
if(y_ == sprite_h-1 or buffer_peek(pixel_buffer, (x_ + sprite_w * (y_+1)) * 4 + 3, buffer_u8) == 0){
var last_index = array_length_1d(outline);
outline[last_index] = array_create(2);
array_set(outline[last_index], 0, x_);
array_set(outline[last_index], 1, y_+1);
}
}
}
}
surface_reset_target();
return outline;
It's a little dirty, and if you guys have any thoughts on improving I am more than happy to receive them.
After getting the array you'll have to draw it out, so I made this too:
scr_draw_sprite_outline:
Code:
/// @arg {real} object
/// @arg {real} outline
var object = argument0;
var outline = argument1;
draw_set_color(c_yellow);
for(var i=0; i < array_length_1d(outline); ++i){
var x_ = array_get(outline[i], 0) + object.x - object.sprite_xoffset;
var y_ = array_get(outline[i], 1) + object.y - object.sprite_yoffset;
draw_rectangle(x_, y_, x_, y_, false);
}
both scripts have to be in the draw event. If I'm posting this in the wrong place, or shouldn't be posting it at all, please let me know so that I won't make the same mistake next time.
Cheers!
EDIT: forgot to mention the use. I use it to show that an item is selected, but it can be used for a lot of things.
Last edited by a moderator: