GML How to save a primitive (ie trianglefan) to sprite_index?

mikix

Member
When I make a polygon, I want to save it to sprite_index so that I can use it for drawing with my paint app. Is there any way to make it possible to save the polygon mask to sprite_index?
 
I'm not completely sure what you mean by "save the polygon to sprite_index", but you can draw the polygon to a surface and then create a sprite from this surface, which you can then assign to sprite_index.

GML:
// Create a surface big enough to contain your polygon
var w = 200, h = 200;
var surf = surface_create(w,h);

// Target the surface and draw the polygon on a cleared background
surface_set_target(surf);
     draw_clear_alpha(c_black, 0);
    // Draw your polygon here
surface_reset_target();

// Create a sprite from the surface -- you should look up the function to see what argument values you want
spr_polygon = sprite_create_from_surface(surf, 0, 0, w, h, false, false, 0, 0);

// Now we don't need the surface anymore, so delete it
surface_free(surf);

// Assign the sprite to sprite_index
sprite_index = spr_polygon;

// Or draw the sprite
draw_sprite(spr_polygon, 0, x, y);

// ...
// Then when you don't need the sprite anymore, to avoid memory leak:
sprite_delete(spr_polygon)
 

mikix

Member
Hi. It seems to work, however I changed sprite_create_from_surface to surface_save_part. Sprite_create_from_surface didn't work. I have a screenshot that shows that it is semi-working. It knows where to copy itself to the sprite_index with the x and y values correctly. However, I want the painted things on the other surface (global.final_surf) to be on the sprite_index, but with the var surf's correct values.

Screenshot_10.png
 

mikix

Member
An update. I was forgetting about on what object to put the sprite_index, sorry. I also made it capture the whole polygon and making it closer to the mouse. Next step will be to make it actually copy the drawings and not only the polygon.

Screenshot_11.png
 
Top