Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: KodiakBehr on Mon 04/01/2016 03:14:52

Title: Drawing an expanding unfilled circle using dynamic sprites.
Post by: KodiakBehr on Mon 04/01/2016 03:14:52
Hi guys,

I could use some help.  I'm trying to use dynamic sprites to draw an unfilled circle whose radius continuously expands until a set point.  I have only been successful in drawing a filled circle.  Is there any obvious way to accomplish what I'm attempting?

The code, for what it's worth, follows:

Code (ags) Select

scanarea = DynamicSprite.Create(800, 600, true);
btnDrawScanZone.Clickable=false;
DrawingSurface *surface = scanarea.GetDrawingSurface();
surface.Clear();
surface.DrawingColor = 2016;
surface.DrawCircle(400, 300, scan_radius);
surface.Release();
btnDrawScanZone.NormalGraphic = scanarea.Graphic;

Title: Re: Drawing an expanding unfilled circle using dynamic sprites.
Post by: Gilbert on Mon 04/01/2016 05:53:39
I don't remember whether you can use colour 0 to draw stuff on a surface to make it transparent, if this is the case, one simple way is to draw a smaller circle filled with colour 0 inside the original circle to puncture it, otherwise you may whip up a function like:

Code (ags) Select

function DrawHollowCircle(DrawingSurface* surface, int ox, int oy, int radius){
  int dd, ii=0;
  int limit = FloatToInt(IntToFloat(radius)/Maths.Sqrt(2.0));
  while(ii<=limit){
    dd = FloatToInt(Maths.Sqrt(IntToFloat(radius*radius - ii*ii)));
    surface.DrawPixel(ox-ii,oy-dd);
    surface.DrawPixel(ox+ii,oy-dd);
    surface.DrawPixel(ox-ii,oy+dd);
    surface.DrawPixel(ox+ii,oy+dd);
    surface.DrawPixel(ox-dd,oy-ii);
    surface.DrawPixel(ox+dd,oy-ii);
    surface.DrawPixel(ox-dd,oy+ii);
    surface.DrawPixel(ox+dd,oy+ii);
    ii++;
  }
}

Edit: This should now work.
Edited again: Just saw the original DrawCircle() function doesn't include the colour either, so I just exercised the colour parameter for simplicity.
Title: Re: Drawing an expanding unfilled circle using dynamic sprites.
Post by: Monsieur OUXX on Mon 04/01/2016 09:57:38
The advantage of using the "circle with color zero onto a temporary sprite" technique is the HUGE performance gain compared to drawing the pixels one by one.
Title: Re: Drawing an expanding unfilled circle using dynamic sprites.
Post by: KodiakBehr on Mon 04/01/2016 14:32:59
I was surprised to discover that drawing a transparent circle within an opaque circle worked wonderfully.  In the end, because it needed to be transparent, not opaque black, I had to set the color to -1.

The end result code looked like this.

Code (ags) Select

scanarea = DynamicSprite.Create(500, 500, true);
btnDrawScanZone.Clickable=false;
DrawingSurface *surface = scanarea.GetDrawingSurface();
surface.Clear();
surface.DrawingColor = 2016;
surface.DrawCircle(250, 250, scan_radius);
surface.DrawingColor = -1;
surface.DrawCircle(250, 250, scan_radius-2);
surface.Release();
btnDrawScanZone.NormalGraphic = scanarea.Graphic;


Thank you both!
Title: Re: Drawing an expanding unfilled circle using dynamic sprites.
Post by: Crimson Wizard on Mon 04/01/2016 18:52:43
I must point out that for safety and compatibility reasons it is recommended to use "COLOR_TRANSPARENT" constant as transparent color value (instead of -1).
This is because hypothetically its actual value may be changed in some future release.
Title: Re: Drawing an expanding unfilled circle using dynamic sprites.
Post by: KodiakBehr on Mon 04/01/2016 22:37:53
Never would have occurred to me.  Thank you!
Title: Re: Drawing an expanding unfilled circle using dynamic sprites.
Post by: Monsieur OUXX on Tue 05/01/2016 10:35:02
Shouldn't the second circle be drawn at (251,251) ?
Title: Re: Drawing an expanding unfilled circle using dynamic sprites.
Post by: Snarky on Tue 05/01/2016 12:03:12
No, the coordinates are for the center, and obviously both circles need to have the same center.