• 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!

Drawing part of a sprite with circles

D

Deleted member 467

Guest
I was looking through the manual to see if there is anything like a draw_sprite_part_circle(); and found nothing like that (much to my disappointment) and did some research on what other people have done regarding this.
So far, it looks like not too many people have had to do something like this :|

Essentially what I'm asking is how could I draw a part of a sprite within a circle?

Example of what I want to achieve:

upload_2016-10-12_9-15-58.png
 
C

CedSharp

Guest
This is super easy to achieve with surfaces.
Create a surface of the size of your rectangle,
make the surface completly transparent,
draw your circle on it.

Then, draw your rectangle on it using the correct blending
in order to reverse alpha but keep your color.

Here is a piece of code in the draw event ( I set 'surface = -1;' in the create event )
Code:
if( !surface_exists( surface ) ) {
    surface = surface_create( 64, 256 );
    surface_set_target( surface );
        draw_clear_alpha( 0,0 );
        draw_set_color( $FFFFFF ); // white
        draw_circle( 64, 256, 48, false );
      
        draw_set_blend_mode_ext( bm_dest_alpha, bm_src_color );
        draw_set_color( $FF0000 ); // blue
        draw_rectangle( 0,0,64,256,false );
        draw_set_blend_mode( bm_normal );
    surface_reset_target();
}

draw_surface( surface, room_width/2 - 32, room_height/2 - 128 );

// remove this to view effect
// show lines around surface
draw_set_color( c_black );
draw_rectangle( room_width/2 - 32, room_height/2 - 128,room_width/2 + 32, room_height/2 + 128, true );
Here is the result:


And without the draw_rectangle and the end:


Of course, if you do this using a sprite instead of a circle, you can have a mask act like a "stamp",
and if you use a sprite instead of the rectangle, you can successfully draw only part of a sprite, using any shape ( not just circle ) that you want!

Regards,
CedSharp
 
D

Deleted member 467

Guest
just out of curiousity, what would this effect be good for?
It's to be used for an item in my game. Essentially you have a sword that casts a circle of darkness around it, and a new tile/object I'll be making today is invisible unless the darkness from the sword is in contact with it. Pretty much going to use it for a few puzzles and such.
 
F

fxokz

Guest
It's to be used for an item in my game. Essentially you have a sword that casts a circle of darkness around it, and a new tile/object I'll be making today is invisible unless the darkness from the sword is in contact with it. Pretty much going to use it for a few puzzles and such.
Awesome.
 
D

Deleted member 467

Guest
@CedSharp The code works for the most part (after I made it so it fits my game). Only problem is after drawing my circular sprite and my sword object is destroyed the circle is still there. I'm sure I'll find a way to fix it, though :) Thanks again for the help.

Edit: fixed the bug, I made a dumb
 
C

CedSharp

Guest
@CedSharp The code works for the most part (after I made it so it fits my game). Only problem is after drawing my circular sprite and my sword object is destroyed the circle is still there. I'm sure I'll find a way to fix it, though :) Thanks again for the help.

Edit: fixed the bug, I made a dumb
Glad to help :D
 
P

p_consti

Guest
Hey there,

I know this post is old and stuff, but I have a problem with your code...
When I execute your code, everything runs normal, how it's supposed to do.
But when I execute my modified version, I don't get a transparent part but a white part (alpha wrong?)

Here's what I did (I try to draw a rectangle without a halfcircle):
Code:
if (!surface_exists(srf)) {
    srf = surface_create(room_width, room_height);
    surface_set_target(srf);
    
    draw_clear_alpha(0, 0);
    draw_set_color(c_blue);
    draw_rectangle(0, 0, room_width/2, room_height, 0);
    draw_set_blend_mode_ext(bm_dest_alpha, bm_src_color);
    draw_set_color(c_white);
    draw_circle(room_width/2, room_height/2, 150, 0);
    draw_set_blend_mode(bm_normal);
    
    surface_reset_target();

}

draw_surface(srf, 0, 0);
 
C

CedSharp

Guest
Hey there,

I know this post is old and stuff, but I have a problem with your code...
When I execute your code, everything runs normal, how it's supposed to do.
But when I execute my modified version, I don't get a transparent part but a white part (alpha wrong?)

Here's what I did (I try to draw a rectangle without a halfcircle):
Code:
if (!surface_exists(srf)) {
    srf = surface_create(room_width, room_height);
    surface_set_target(srf);
   
    draw_clear_alpha(0, 0);
    draw_set_color(c_blue);
    draw_rectangle(0, 0, room_width/2, room_height, 0);
    draw_set_blend_mode_ext(bm_dest_alpha, bm_src_color);
    draw_set_color(c_white);
    draw_circle(room_width/2, room_height/2, 150, 0);
    draw_set_blend_mode(bm_normal);
   
    surface_reset_target();

}

draw_surface(srf, 0, 0);
I've already answered you outside of the forums, but I'll answer for anyone else who might have this issue.
What @p_consti was trying to do is not to restrict a drawing to a shape, but rather he wanted to draw "holes" or
"erase" what was on the surface ( he's trying to erase a circle from the rectangle )

To do that, you can simply use
Code:
draw_set_blend_mode( bm_subtract )
which is a Game Maker constant
that doest exactly that.

Hope this helps
 

OnLashoc

Member
I know this is thread resurrection but looking through the forums for this exact thing but the code wont work as written as draw_set_blend_mode_ext isn't recognized by GMS 2 and I need a circle not a rectangle.

Im trying to pass an image under a circle and only reveal what is shown within the circle, and nothing outside of the circle. Any help would be greatly appreciated!
 

FoxyOfJungle

Kazan Games
I know this is thread resurrection but looking through the forums for this exact thing but the code wont work as written as draw_set_blend_mode_ext isn't recognized by GMS 2 and I need a circle not a rectangle.

Im trying to pass an image under a circle and only reveal what is shown within the circle, and nothing outside of the circle. Any help would be greatly appreciated!
 
I know this is thread resurrection but looking through the forums for this exact thing but the code wont work as written as draw_set_blend_mode_ext isn't recognized by GMS 2 and I need a circle not a rectangle.

Im trying to pass an image under a circle and only reveal what is shown within the circle, and nothing outside of the circle. Any help would be greatly appreciated!
That function was renamed to gpu_set_blendmode_ext().
 
Top