Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Knox on Fri 06/05/2011 18:14:56

Title: Drawing Anti-Aliased lines smoothly with a mouse
Post by: Knox on Fri 06/05/2011 18:14:56
Im just trying to do a basic pencil function for free-hand drawing with AGS. I looked here for code snippets:

http://www.adventuregamestudio.co.uk/yabb/index.php?topic=41620.0 (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=41620.0)
http://www.adventuregamestudio.co.uk/yabb/index.php?topic=39846.0 (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=39846.0)



  if (bIsDrawing == true)
  {
    if (Mouse.IsButtonDown(eMouseLeft) == true)
    {
      DrawingSurface *DrawSurface = Room.GetDrawingSurfaceForBackground();
 
      float fMouseX = IntToFloat(mouse.x);
      float fMouseY = IntToFloat(mouse.y);
      DrawAntialiasedLine(DrawSurface, fMouseX, fMouseY, fMouseX*1.2, fMouseY*1.2, 0); <--what do I put here for smooth lines?

      DrawSurface.Release();
    }
    else bIsDrawing = false;
  }



To make the mouse draw a nice anti-aliased line smoothly with hand gestures to simulate a pencil is what Im going for. Im almost there, but I think Im getting the math wrong for the second "x" and "y" values...(I tried varations without luck because I dont understand what Im doing there...just pluggin arbitrary operators to see what happens.)
Title: Re: Drawing Anti-Aliased lines smoothly with a mouse
Post by: Knox on Fri 06/05/2011 18:32:48
Ok I think I got it!

(but using Wyz' brushLine instead of the AntiAlias Line code)


function repeatedly_execute()
{              
 if (bIsDrawing == true)
 {
   if (Mouse.IsButtonDown(eMouseLeft) == true)
   {
     int iPenColor = 2716; //blue pen ...2718 black pen ...2720 red pen
     DrawingSurface *DrawSurface = Room.GetDrawingSurfaceForBackground();
     DrawSurface.BrushLine(mouse.x, mouse.y, iPrevX, iPrevY, iPenColor, 7.0);
     iPrevX = mouse.x;
     iPrevY = mouse.y;
   }
   else bIsDrawing = false;
 }
}

function on_mouse_click(MouseButton button)
{
 if (button == eMouseLeft)
 {
   bIsDrawing = true;
   iPrevX = mouse.x;
   iPrevY = mouse.y;
 }
}

Is this a good way?
Title: Re: Drawing Anti-Aliased lines smoothly with a mouse
Post by: Knox on Fri 06/05/2011 19:32:25
Ok, since I got it to work on the background, Im now trying to draw onto a gui instead of the background...but I keep on making AGS crash...how do I draw onto the gNotePad gui? It should save pen drawing onto the gNotePad.BackgroundImage, but this is not working:


function repeatedly_execute()
{              
if (bIsDrawing == true)
 {
   if (Mouse.IsButtonDown(eMouseLeft) == true)
   {
     //Display("Draw!");
     int iPenColor = 2716; //Blue pen
     //int iPenColor = 2718; //Black pen
     //int iPenColor = 2720; //Red pen
     //int iPenColor = 3853; //Highlighter
     
     //A:
     //DrawingSurface *DrawSurface = Room.GetDrawingSurfaceForBackground();
     
     //B:
     DynamicSprite *dsNotePad = DynamicSprite.CreateFromExistingSprite(gNotePad.BackgroundGraphic);
     //DynamicSprite *dsNotePad = DynamicSprite.CreateFromExistingSprite(3854);
     DrawingSurface *DrawSurface = dsNotePad.GetDrawingSurface();

     DrawSurface.BrushLine(mouse.x, mouse.y, iPrevX, iPrevY, iPenColor, 7.0);
     DrawSurface.Release();
     
     //B:
     gNotePad.BackgroundGraphic = dsNotePad.Graphic;
     //Wait(120);
     //dsNotePad.Delete();

     iPrevX = mouse.x;
     iPrevY = mouse.y;
   }
   else bIsDrawing = false;
}

function on_mouse_click(MouseButton button)
{
 if (button == eMouseLeft)
 {
   bIsDrawing = true;
   iPrevX = mouse.x;
   iPrevY = mouse.y;
 }
}




**UPDATE: OK, I need to declare the dynamic sprites outside the function, sorry guys for my posts!!

QuoteIn order to use DynamicSprites as button or GUI backgrounds, they have to be defined outside any function (otherwise they are destroyed as soon as the function finishes, ending the element that uses them up at displaying sprite 0/the blue cup).