Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Héctor Bometón on Mon 20/07/2020 17:02:05

Title: [SOLVED] Is it possible to freehand draw with the mouse?
Post by: Héctor Bometón on Mon 20/07/2020 17:02:05
Is it possible to freehand draw with the mouse? Just like a pen/brush tool.

I have a blackboard on one of my backbrounds and I was wondering if I could make that "area" paintable when you use the chalk on it.

I can do without that, but it defiinitely would be funny!

Thank you!
Title: Re: Is it possible to freehand draw with the mouse?
Post by: Crimson Wizard on Mon 20/07/2020 17:21:14
Something like this:
Code (ags) Select

function room_RepExec()
{
     if (Mouse.IsButtonDown(eMouseLeft))
     {
          DrawingSurface* ds = Room.GetDrawingSurfaceForBackground();
          ds.DrawingColor = Game.GetColorFromRGB(255,255,255);
          ds.DrawRectangle(mouse.x - 2, mouse.y - 2, mouse.x + 2, mouse.y + 2);
          ds.Release();
     }
}
Title: Re: Is it possible to freehand draw with the mouse?
Post by: Héctor Bometón on Mon 20/07/2020 18:01:54

That was easy! Thank you very much.

Now I'm getting greedy... Would it be possible to specify which surfaces you can draw onto and which ones not? Like... Using regions or whatever?
Title: Re: Is it possible to freehand draw with the mouse?
Post by: Khris on Mon 20/07/2020 18:15:31
Sure; for a rectangular blackboard you can do a simple coordinate check.
Code (ags) Select
  if (mouse.x < 123 || mouse.x > 234) return; // exit function if mouse is outside bounds, same for y

For an irregular shape you can use a region:
Code (ags) Select
  if (Region.GetAtScreenXY(mouse.x, mouse.y) != region[1]) return; // exit if mouse is outside region #1

You'd put either line(s) anywhere before the drawing commands.
Title: Re: Is it possible to freehand draw with the mouse?
Post by: Héctor Bometón on Mon 20/07/2020 18:25:38

Of course! Pretty obvious! I should stop and think a little before asking.

Thanks again!
Title: Re: [SOLVED] Is it possible to freehand draw with the mouse?
Post by: Héctor Bometón on Tue 21/07/2020 11:27:37

Ok, I want to take it further... What if I want to check if a certain area has being written on?

For example: I want to make a puzzel where you cross out a swastika gfraffiti. How could I know if x% of that region is covered with "paint"?
Title: Re: [SOLVED] Is it possible to freehand draw with the mouse?
Post by: Khris on Tue 21/07/2020 12:44:41
A simple way is to count the pixels while you're doing the drawing. Assuming that you're drawing single white pixels, you can check if the pixel is already white before drawing it onto the surface. If it isn't, count up an integer variable.

Code (ags) Select
  int x = mouse.x, y = mouse.y;
  if (ds.GetPixel(x, y) != 15) {
    ds.DrawPixel(x, y);
    paintedPixels++;
  }


The percentage can now be calculated using  (paintedPixels * 100) / totalPixels
Title: Re: [SOLVED] Is it possible to freehand draw with the mouse?
Post by: Héctor Bometón on Tue 21/07/2020 14:11:58
Once again: thank you!

I am amazed about how simple the code is!