[SOLVED] Is it possible to freehand draw with the mouse?

Started by Héctor Bometón, Mon 20/07/2020 17:02:05

Previous topic - Next topic

Héctor Bometón

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!

Crimson Wizard

Something like this:
Code: ags

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();
     }
}

Héctor Bometón


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?

Khris

Sure; for a rectangular blackboard you can do a simple coordinate check.
Code: ags
  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
  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.

Héctor Bometón


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

Thanks again!

Héctor Bometón


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"?

Khris

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

Héctor Bometón

Once again: thank you!

I am amazed about how simple the code is!

SMF spam blocked by CleanTalk