LoginLogin
Might make SBS readonly: thread

Turtle library + Anti-aliased line library

Root / Submissions / [.]

niconiiCreated:
Download:Q3V3SX6D
Version:1.0.1Size:9 KB
TURTLE.LIB is a library for drawing turtle graphics, as in the Logo programming language or Python's built-in turtle library. Basically, you control a pen or "turtle" that can move forward, backward, or turn left or right by some angle, and as it moves, it draws on the screen. Turtle graphics are more of an educational tool than something with practical use, but they can be fun to play around with. ALINE.LIB is a library that can draw anti-aliased lines. Like the other AA line library on this site, it is an implementation of Xiaolin Wu's line algorithm. However, this one allows for different colored lines, and simulates translucency for edges when overlapping other graphics, rather than always fading the edges to black. That said, it is still quite slow, and is most likely slower than the other due to these additional features. Since TURTLE.LIB depends on ALINE.LIB, both have been packaged together.

Instructions:

To use turtle library:
EXEC "PRG1:TURTLE.LIB"
To use AA line library:
EXEC "PRG1:ALINE.LIB"
More documentation on usage is included in the respective libraries.

Be careful about SWAPping function arguments, since the type depends on the value that is passed, not the variable name. If you pass an int and a float, it will just swap their values, not their types, so you'll lose some precision.

Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?

Replying to:HTV04
Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?

Replying to:HTV04
Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?
It doesn’t have color support, so I can’t make transparent lines that match the lines being drawn.

Replying to:HTV04
Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?
The reason it doesn't work is because to simulate translucency, ALINE blends with whatever's underneath. Trying to draw an ALINE with color 0 over another one won't erase it, it will actually blend it with a black line (because ALINE ignores the alpha channel of the color given), darkening it a bit. (Even if it were changed to take the alpha channel into account, a transparent line wouldn't erase what's underneath, it'd simply draw nothing at all.) I've updated the library by adding a new command, ELINE. You can use it like this:
'Draw a red anti-aliased line
ALINE 100,100,250,200,#RED
'Erase it
ELINE 100,100,250,200,0

Replying to:HTV04
Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?
Thank you so much! ELINE fixed the problem.

Replying to:HTV04
Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?
Now, I should warn you, I was a bit surprised to learn you're using this for a real-time game like Solid Gunner Complete. I mean, hey, if it manages to perform well enough, that's great, but just keep in mind that ALINE is way slower than GLINE, so just be aware that it could lead to slowdowns. This benchmark should illustrate that:
EXEC "PRG1:ALINE.LIB"

'Try making these smaller to generate shorter lines
W=400
H=240

ACLS
AVG=0
NUM=0
WHILE TRUE
 LINES=0
 FRAME=MAINCNT
 WHILE MAINCNT==FRAME
  X0=RND(W):Y0=RND(H)
  X1=RND(W):Y1=RND(H)
  C=RND(&HFFFFFF) OR &HFF000000
  'Try replacing ALINE with ELINE or GLINE
  ALINE X0,Y0,X1,Y1,C
  INC LINES
 WEND
 AVG=(NUM*AVG+LINES)/(NUM+1)
 INC NUM
 CLS
 COLOR #TWHITE,#TBLACK
 ?AVG
 COLOR #TWHITE,0
WEND
This generates random lines on the screen and prints how many lines on average are drawn per frame. On my New 3DS:
  • ALINE averages about 1.6 lines per frame.
  • ELINE averages about 10.6 lines per frame.
  • GLINE averages a whopping 1385 lines per frame.
If I clock it down to old 3DS speeds using CFW:
  • ALINE averages about 1.0 lines per frame.
  • ELINE averages about 2.1 lines per frame.
  • GLINE averages about 297 lines per frame.
Now, it's a little better than it looks, since longer lines are more expensive than shorter ones, so modifying the benchmark to use a 50x50 region rather than 400x240 can get you up to about 10 lines per frame for ALINE on New 3DS. The huge difference in performance between ALINE and GLINE is just a consequence of the fact that GLINE uses native code to draw its lines, while ALINE is forced to use SmileBASIC, with relatively slow commands like GSPOIT (for color blending) and GPSET.

I wonder if it would be faster to GSAVE the region where the line will be drawn, modify the array, and then GLOAD it back.

Replying to:HTV04
Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?
I did some thorough testing and it seems to work fine. Solid Gunner isn’t really a resource-heavy game, and only two lines are being drawn on screen, so there shouldn’t be any slowdowns. By the way, I also tested your anti-aliasing library on GAME2RPG. The title screen obviously took some time to draw all the lines, but it didn’t take too long. However, the tunnels rendered pretty fast. Needless to say, the game looked a lot better. You did a really good job on this anti-aliasing library, and I may use it for future projects, if that’s okay.

Replying to:HTV04
Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?
Sure, use it for whatever you want.

Replying to:HTV04
Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?
@niconi, your test results are not very accurate since the coords are always random. You could fix this by making the coords constant or using RANDOMIZE so the coords are deterministic:
RANDOMIZE 0, whatever
'should be a constant too
EDIT lol I said "inaccurate" instead of "accurate", making it a double negative oof

Replying to:HTV04
Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?
Good thing it's not very inaccurate

Replying to:HTV04
Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?
it's actually VERY inaccurate, since the slowest speed for drawing a line a(0,0,400,240) can be 433x slower than the fastest b(x,y,x,y) speed: b will draw 5 pixels a will draw 2161 pixels(i think), considering AA is basically a one radius blur (using probability)

Replying to:HTV04
Hey, I’m planning to implement this in Solid Gunner Complete as an option for the line drawn between the ships. However, replacing the GLINEs with ALINEs causes the lines drawn by the game to stay on screen, even though the should be removed by the transparent ALINE. Is there a way to fix this?
I know that I can't take no more It ain't no lie, I wanna see you out that door Baby, bye, bye, bye...
It's interesting that you mention probability. "using probability..." the tests will measure the AVERAGE drawing time. 'splaining is now illegal.

Can you port this to SmileBASIC 4? I would like to use the library there, but I don’t know how to port it myself.

Replying to:HTV04
Can you port this to SmileBASIC 4? I would like to use the library there, but I don’t know how to port it myself.
It looks like it's compatible with SmileBASIC 4, so you can probably just download it. Just replace...
  • All USE commands with similar EXEC commands (don't have to change syntax)
  • All LOAD "PRGx:NAME",FLAG% commands with similar LOAD "NAME",x commands
  • All RGBREAD commands with RGB commands (don't have to change syntax)
  • All TRUE and FALSE system variables with similar #TRUE and #FALSE constants
  • All #[COLOR] constants with similar #C_[COLOR] constants
Sounds complicated? It isn't! I did this in like 2 minutes (even easier if you have a keyboard) and now I have a pretty anti-aliased tree on my screen.

Replying to:HTV04
Can you port this to SmileBASIC 4? I would like to use the library there, but I don’t know how to port it myself.
Thanks!

Replying to:HTV04
Can you port this to SmileBASIC 4? I would like to use the library there, but I don’t know how to port it myself.
Here's the official SB4 port: 42EKEKKAV Although simply porting it from SB3 is easy enough, some changes have been made for the SB4 version, due to SB4 now having actual alpha capabilities. For instance, ALINE now takes the alpha of the color you give it into account. Some functions have been removed or made private due to being made obsolete. For example, APSET is no longer part of the API, since you can just use:
GPSET X,Y,C,#G_ALPHA2
There's also a nice little demo program if you run MAIN.PRG.