Jibble

Author Topic: MODULE: DrawAntialiased v1.1  (Read 9611 times)

Kweepa

  • Mutated Guano Deviser
    • Best Innovation Award Winner 2009, for his modules and plugins
    • Kweepa worked on one or more games that won an AGS Award!
    •  
    • Kweepa worked on one or more games that was nominated for an AGS Award!
MODULE: DrawAntialiased v1.1
« on: 12 Jan 2010, 08:43 »


Allows you to draw antialiased lines and circles onto a drawing surface, like so:
Code: [Select]
DrawingSurface *surf = Room.GetDrawingSurfaceForBackground();
surf.DrawAntialiasedLine(x1, y1, x2, y2, transparency);
surf.DrawAntialiasedCircle(x, y, radius, transparency);
surf.DrawAntialiasedFilledCircle(x, y, radius, transparency);
surf.Release();

Demo in here:
http://www.kweepa.org/step/ags/tech/RopeTest.zip (900k)

Download here:
http://www.kweepa.org/step/ags/tech/DrawAntialiased.zip (2k)

For AGS 3.x.
Note: requires a 16 or 32 bit game and a 16 or 32 bit surface (for example, when drawing to the background, you must have imported a 16 or 32 bit image as the background). If you don't do this, the lines and circles will look blocky and the game will run slowly.

Enjoy!
Steve

Version History:
v1.1 Name change; added circles; extended DrawingSurface
v1.0 Lines only
« Last Edit: 01 Feb 2018, 15:56 by Kweepa »
Still waiting for Purity of the Surf II

SSH

  • Flying round the world at the speed of haggis
    • I can help with scripting
    • SSH worked on one or more games that won an AGS Award!
    •  
    • SSH worked on one or more games that was nominated for an AGS Award!
Re: MODULE: AntialiasedLine v1.0
« Reply #1 on: 12 Jan 2010, 09:09 »
Pretty cool, but why does Roger have a rope coming out of THERE exactly?  :=

Layabout

  • Mittens Baron
  • Brogan
    • Layabout worked on one or more games that won an AGS Award!
    •  
    • Layabout worked on one or more games that was nominated for an AGS Award!
Re: MODULE: AntialiasedLine v1.0
« Reply #2 on: 12 Jan 2010, 09:50 »
Wow, the rope test is really really impressive. I saw the rope 'demo' in the other thread, but combined with AA, it's superb.

Just a quick question, would it be possible to use the rope in the example to be affected by 'systems' such as wind. It would be cool to have it simulate power lines in a room without having to worry about animating something so tedious. (I know it's a bit off topic, but hey!)
I am Jean-Pierre.

ThreeOhFour

  • AGS Baker
  • Wild colonial boy.
    • Lifetime Achievement Award Winner
    • ThreeOhFour worked on one or more games that won an AGS Award!
    •  
    • ThreeOhFour worked on one or more games that was nominated for an AGS Award!
Re: MODULE: AntialiasedLine v1.0
« Reply #3 on: 12 Jan 2010, 10:29 »
Holy crap Steve!

That's the most bestest thing I ever saw  :o :D

Re: MODULE: AntialiasedLine v1.0
« Reply #4 on: 12 Jan 2010, 13:25 »
Very nice work. I'll have to take a closer look at the rope physics script later today - I was wanting to integrate more physics puzzles in my next game but never thought of using rope. I'm guessing I'll have to look into how to model gravity on objects hanging from the end of the rope too.

monkey0506

  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
    • monkey0506 worked on one or more games that won an AGS Award!
    •  
    • monkey0506 worked on one or more games that was nominated for an AGS Award!
Re: MODULE: AntialiasedLine v1.0
« Reply #5 on: 12 Jan 2010, 16:07 »
Very nice demo. My question isn't about that though...

Why take a DrawingSurface as a parameter instead of using an extender method like:

Code: [Select]
import void DrawAntiAliasedLine(this DrawingSurface*, int x1, int y1, int x2, int y2, int transparency);

DrawingSurface *surface = Room.GetDrawingSurfaceForBackground();
surface.DrawAntialiasedLine(x1, y1, x2, y2, transparency);
surface.Release();

No technical difference except the fact that it provides automatic parameter validation of the DrawingSurface and prevents the possibility of null pointer references. :=

From an aesthetic viewpoint it would group the function together with the rest of the DrawingSurface functions and make it more streamlined with the built-in code. You already said it's for AGS 3.0+.

But in any case, very nice work, as usual. :=

Kweepa

  • Mutated Guano Deviser
    • Best Innovation Award Winner 2009, for his modules and plugins
    • Kweepa worked on one or more games that won an AGS Award!
    •  
    • Kweepa worked on one or more games that was nominated for an AGS Award!
Re: MODULE: AntialiasedLine v1.0
« Reply #6 on: 12 Jan 2010, 17:07 »
Extender what now? :=
Sounds like a good idea. I'll do that.

RE: the rope comments above.
Easy to apply wind. Just like gravity.
Harder to add weights and make it look good. Add stronger force to the end of the rope. Need to shorten the rope to make it less stretchy, and maybe add another (invisible) direct link from the start of the rope to the end to reduce stretching some more. Or do more constraint iterations (but then it gets slow). Perhaps a different physics approach would be better.
« Last Edit: 12 Jan 2010, 17:11 by SteveMcCrea »
Still waiting for Purity of the Surf II

Kweepa

  • Mutated Guano Deviser
    • Best Innovation Award Winner 2009, for his modules and plugins
    • Kweepa worked on one or more games that won an AGS Award!
    •  
    • Kweepa worked on one or more games that was nominated for an AGS Award!
Re: MODULE: DrawAntialiased v1.1
« Reply #7 on: 13 Jan 2010, 01:01 »
Bump for new version:
:= Name change
:= Added circles and filled circles
:= Changed to extend DrawingSurface, as suggested by the monkey
Still waiting for Purity of the Surf II

abstauber

  • Cavefish
  • Mittens Knight
  • still mowing the lawn
    • abstauber worked on one or more games that won an AGS Award!
    •  
    • abstauber worked on one or more games that was nominated for an AGS Award!
Re: MODULE: DrawAntialiased v1.1
« Reply #8 on: 13 Jan 2010, 07:58 »
 :o
Amazing as always!
Great job!

Danman

  • Dan the man with the plan.
    • I can help with AGS tutoring
    • I can help with making music
    • I can help with scripting
    • I can help with story design
Re: MODULE: DrawAntialiased v1.1
« Reply #9 on: 13 Jan 2010, 13:41 »
Great I have been wanting for something like this to play with. Good work.



monkey0506

  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
    • monkey0506 worked on one or more games that won an AGS Award!
    •  
    • monkey0506 worked on one or more games that was nominated for an AGS Award!
Re: MODULE: DrawAntialiased v1.1
« Reply #10 on: 13 Jan 2010, 13:58 »
Hey Steve, I was thinking of using this module for something, but I need the lines to be thicker than 1 pixel. Any possibility you could add an optional thickness parameter like the built-in DrawingSurface.DrawLine function has? :D Thanks!!

Kweepa

  • Mutated Guano Deviser
    • Best Innovation Award Winner 2009, for his modules and plugins
    • Kweepa worked on one or more games that won an AGS Award!
    •  
    • Kweepa worked on one or more games that was nominated for an AGS Award!
Re: MODULE: DrawAntialiased v1.1
« Reply #11 on: 13 Jan 2010, 18:54 »
Hmm, not so easy.
As a workaround you could draw the line multiple times with 1px offsets.
Code: [Select]
 float dy = (x2 - x1);
  float dx = (y2 - y1);
  float len = Maths.Sqrt(dx*dx + dy*dy);
  
  if (len > 0.0001)
  {
    dy = dy/len;
    dx = dx/len; // EDIT - FORGOT THIS LINE
    float offset = -0.5*IntToFloat(thickness-1);
    int i = 0;
    while (i < thickness)
    {
      surf.DrawAntialiasedLine(x1 + offset*dx, y1 + offset*dy, x2 + offset*dx, y2 + offset*dy);
      offset = offset + 1.0;
      i++;
    }
  }
Which is obviously pretty slow :(
« Last Edit: 13 Jan 2010, 20:12 by SteveMcCrea »
Still waiting for Purity of the Surf II

Re: MODULE: DrawAntialiased v1.1
« Reply #12 on: 13 Jan 2010, 19:34 »
I guess it depends on the effect you want (i.e. should the anti-aliased area also grow or just the width of the opaque pixels). If you just want a thick solid line with soft edges, it should be possible to do the opaque pixels either using DrawLine repeatedly or drawing two triangles (for very wide lines) - and then draw two anti-aliased lines, one on each side of the aliased line.

tzachs

  • Mittens Vassal
  • AGS Baker
  • Parking Goat- games that goats like!
    • I can help with translating
    • tzachs worked on one or more games that won an AGS Award!
    •  
    • tzachs worked on one or more games that was nominated for an AGS Award!
Re: MODULE: DrawAntialiased v1.1
« Reply #13 on: 24 Feb 2010, 23:27 »
I had a puzzle with a rope in my game, I originally thought of manually animating the whole thing, but then I remembered this module, and it:
A. Looks much more impressive than what I had planned.
B. Took a lot less time.
C. Saved my life!

So I guess I just wanted to say thanks for this great module!

Dualnames

  • Local Moderator
  • Rottwheelers
  • Pretty Badass
    • Dualnames worked on one or more games that won an AGS Award!
    •  
    • Dualnames worked on one or more games that was nominated for an AGS Award!
Re: MODULE: DrawAntialiased v1.1
« Reply #14 on: 25 Feb 2010, 20:38 »
D. Also helps with the ladies! ;)
No more military army stuff. I'm alive and back.

Re: MODULE: DrawAntialiased v1.1
« Reply #15 on: 18 May 2010, 05:30 »
Hey Steve,

Using the rope test demo, is there any chance of being able to remove those AA gaps between the vertices of the rope joints, having it solid?
Like the center pixel always being 100% no matter what.

I've studied the code as much as I can without frying my brain.  It's gotta be the worst thing about using other people's code.
Heck, it even took me an hour just to make it draw on a sprite for an object instead of the background.  Now I can use it anywhere, even above the chosen characters and objects.  Even works with foreground walkbehinds.  Yay!

Edit: It doesn't show up all that badly on the black backgrounds, but really badly on colored backgrounds.  Like this:

(background is totally ripped from some random adventure game)

I understand why it's doing it, it's using floats for the vertices and judging the transparency on the edges by that.  What is the easiest way to make it convert to Int before it calculates the AA?  Obviously the visual of the 'rope' won't be as smooth anymore with ints, but at least it'll be consistent in opacity.

Thanks.

Hmm.. Now that I think about it, having the 33 rope points in int form, it'll still have AA issues between the two points.
So perhaps the only solution would be to increase the AA to 3 pixels wide/high in the areas in needs it based on the point before and after..  Basically how the photoshop line tool works, always with a strong center of the line.
How could I do this?
« Last Edit: 18 May 2010, 06:06 by Ryan Timothy »

Kweepa

  • Mutated Guano Deviser
    • Best Innovation Award Winner 2009, for his modules and plugins
    • Kweepa worked on one or more games that won an AGS Award!
    •  
    • Kweepa worked on one or more games that was nominated for an AGS Award!
Re: MODULE: DrawAntialiased v1.1
« Reply #16 on: 18 May 2010, 15:14 »
Since it's your birthday, I decided to take a look at this.
The gaps in the rope are more exaggerated because you have rendered the rope to a sprite first, so the alpha channel is on/off rather than smooth, and any pixels that are less than 50% won't render at all.
I'm out of time to look into this further - gotta go to work - but it may be possible to retain the alpha by drawing single pixel images of varying alpha to the sprite rather than a single pixel image of varying transparency.
Take a look at the "plot" function in AntiAliasedLine.asc and try replacing
gSurfaceToDrawOn.DrawImage(y,x,gPixel.Graphic,t);
with
gSurfaceToDrawOn.DrawImage(y,x,gPixel[t/4]);
where gPixel is an array of sprites of varying alpha (you'd need to create these in a paint package and load them in, then set the array up before drawing any lines).

Perhaps someone like monkey can help you further since it's your birthday and all :)
Still waiting for Purity of the Surf II

Re: MODULE: DrawAntialiased v1.1
« Reply #17 on: 18 May 2010, 17:06 »
Since it's your birthday, I decided to take a look at this.
That's the only reason I decided to ask last night.  Now get it working!!   ;D

Edit:  I removed the last portion of this post because I've got it working! Yay!  It suddenly hit me, you're drawing the gPixel at different transparencies sometimes in the same X,Y location - like layers.  I kept clearing the X,Y pixel of the sprite to match the background every time it went to draw a pixel.  So that's why they appeared as light AA in those broken connections.

Right on.  Thanks for this awesome module, you're my hero Steve.
« Last Edit: 18 May 2010, 18:26 by Ryan Timothy »

Monsieur OUXX

  • Mittens Vassal
  • Cavefish
  • Mittens Half Initiate
    • I can help with proof reading
    • I can help with translating
    • I can help with voice acting
    • Monsieur OUXX worked on one or more games that won an AGS Award!
    •  
    • Monsieur OUXX worked on one or more games that was nominated for an AGS Award!
Re: MODULE: DrawAntialiased v1.1
« Reply #18 on: 06 Jul 2010, 11:16 »
Steve, may I ask you if you based your implementation on Xiaolin Wu's algorithm?

Do you know if there are algorithms out there that are more "space consuming"* and less "time-consuming"* ?
The more the better.

* as they say in algorithmics dialect


 

Kweepa

  • Mutated Guano Deviser
    • Best Innovation Award Winner 2009, for his modules and plugins
    • Kweepa worked on one or more games that won an AGS Award!
    •  
    • Kweepa worked on one or more games that was nominated for an AGS Award!
Re: MODULE: DrawAntialiased v1.1
« Reply #19 on: 07 Jul 2010, 00:37 »
I did, yes - as it says in the source code :)
I doubt you'd get significant savings from another algorithm. Specifically, I doubt you'd get more than a 50% speedup, without making significant assumptions. If you have some assumptions (for example, start and end are always going to be on exact pixels, maximum line length is X, ...) then for example you could precache some lines and just pick a precached line.
Still waiting for Purity of the Surf II