GML Erase Pixels Ingame?

P

Pretento

Guest
So I've done a bit of google searching and couldn't find an answer, sorry if this has already been covered somewhere. I need to erase pixels on an object's sprite in game with another object.
(the pixels touching "another object")

I have sort of a complex game mechanic based on this being possible, and I would like to know before I get too invested how one might go about this. Thanks.
 

Simon Gust

Member
Hi,
I don't exactly know what you are trying to do but using a subtractive blendmode for drawing can erease colors.
with the function draw_set_blendmode() you can change the current blendmode (bm_subtract). Just make sure to reset it afterwards immediatly (bm_normal).
 

CMAllen

Member
I don't believe you can edit pre-loaded resources at run-time. You have to make a copy of them and edit the copy, and from what I've read, GM does not handle run-time sprite editing particularly well (memory leaks abound with functions like sprite_set_alpha_from_sprite). A better (but more complex) approach is to draw your sprite to a surface, and then do as Simon Gust suggested -- set your blend_mode to bm_subtract, your draw_color to c_black, and your draw_alpha to 1.0, drawing over the regions of the surface that you want to remove. Once you've modified the surface, you can convert that to a sprite and draw it as normal (you'd need to set the mask and offset information to the originating sprite's information), or keep it as a surface and draw it as though it were a sprite by subtracting the originating sprite's x and y offset values from the coordinates that you want to draw the surface at. Note that if you don't convert to a sprite, the collision mask of any objects using the modified sprite won't reflect the changes being made to it.

An alternative to the method of editing the surface proposed above, you can also convert the surface to a buffer, buffer_seek to the pixel locations you want to edit, set the alpha channel's byte to 0, then convert the buffer back to a surface and go from there. The surface information is stored linearly, left to right, top to bottom, in 4 1-byte color channels (bgra, is the order, I believe), making finding the right pixel a very simple matter.
 
Top