Jump to content




Surface API 1.6.2

api

61 replies to this topic

#21 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 16 April 2015 - 06:32 PM

View PostCreator, on 16 April 2015 - 06:06 PM, said:

View Postawsumben13, on 16 March 2015 - 10:14 PM, said:

This is by far the most useful API I've seen on the forums. I'm taking serious design tips from it, and I'm annoyed I didn't have these things implemented from the start of writing Nova. Rotation would be incredible to have for animations.

One quick question... How did you implement :drawTriangle()? Surely you're using the scanline method, and if so, it's not any harder to implement a :drawPolygon() function.

@awesumben every post that is about gui you ask for a triangle drawing function and a polygon drawing function. What is the math behind circles triangles and polygons in general?
Well it's mostly scanline algorithms, but I use the bresenham line algorithm and then a filling algorithm because it's pixel perfect.
It may not be the fastest way of drawing, but it doesn't have any artifacts.
Here are a few sources that may help you if you're interested in real scanline algorithms:
Sofware Rasterization Algorithms for drawing filled triangles
Triangle fillers
Polygon Fill Teaching Tool
Circle and ellipse drawing algorithm - Math Open Reference

#22 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 16 April 2015 - 06:50 PM

Thanks a lot ;)

#23 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 16 April 2015 - 06:51 PM

View PostCreator, on 16 April 2015 - 06:50 PM, said:

Thanks a lot ;)
No problem :D
If you want to learn even more, you can search for "<shape> fill algorithm" on Google.
I recommend learning about the Bresenham Line algorihm.
There is also lua version of the algorithm.

Edited by CrazedProgrammer, 16 April 2015 - 06:54 PM.


#24 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 25 April 2015 - 09:52 PM

I've made a small fix for the Surface API.
You couldn't save images in NFP (built-in paint) format because the colorToHex function was renamed to _colorToHex.
You can download the fix with the same download as before or you can replace colorToHex with _colorToHex on line 205.
I'm sorry for not noticing the problem before.
If you encounter any problems then please leave a reply.

#25 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 27 April 2015 - 03:09 PM

CrazedProgrammer this looks like you programmed in GameMaker becouse of the functions, OR you made this up!

Edited by lauriszz123, 27 April 2015 - 03:10 PM.


#26 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 27 April 2015 - 04:24 PM

View Postlauriszz123, on 27 April 2015 - 03:09 PM, said:

CrazedProgrammer this looks like you programmed in GameMaker becouse of the functions, OR you made this up!
I did program in GameMaker xD
I really like how its drawing functions are laid out and that's why the names are so similar.

Edited by CrazedProgrammer, 27 April 2015 - 06:36 PM.


#27 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 27 April 2015 - 07:10 PM

I've updated the Surface API to version 1.4.0!
I made these changes and additions to the API:
- Completely rewritten and optimized
- New version numbering and variable: surface.version
- Added surf:drawRoundRect and surf:fillRoundRect

The new version numbering works like this: 1.4.0
1 - Major: Completely different, probably no backwards compatibility.
4 - Minor: A couple of new features, backwards compatible.
0 - Patch: Bug fixes / optimalizations
The new variable, surface.version, is a string that represents the version, for example "1.4.0".

Also, it is 2150 bytes smaller than version 1.3 :P/>

You can download the new version from the same pastebin as before:
pastebin get 5YWfPd8Z surface
If you want to download version 1.3 for some reason you can still do that:
pastebin get ajFj7yNK surface

Edited by CrazedProgrammer, 02 May 2015 - 11:15 AM.


#28 Exerro

  • Members
  • 801 posts

Posted 27 April 2015 - 09:58 PM

As I've said before, this is great, although I think the 'drawRoundRect' function is a bit over-exaggerated (thought it might have some control over corner radius). With the exception of that, I'd love to see a :termRedirect() function that returns an object you can term.redirect() to, and arcs (drawn using either 2 points and 2 angles, or a circle centre and 2 angles) - yeah, I'm thinking pie charts...

Edited by awsumben13, 27 April 2015 - 09:58 PM.


#29 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 27 April 2015 - 10:25 PM

Surface is like paintutils or opengl not window api. It is like comparing tcp ip and http

#30 syfygirl

  • Members
  • 94 posts
  • LocationSomewhere in my mind, to which only makes sense to me.

Posted 27 April 2015 - 10:49 PM

oh man could you imagine a 3d engine pluggin to render objects to sprites.

#31 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 28 April 2015 - 05:55 PM

Sorry, I made a typo in my last post: surface:drawFilledRect should be surface:fillRoundRect.
I've edited it by now.

View Postawsumben13, on 27 April 2015 - 09:58 PM, said:

As I've said before, this is great, although I think the 'drawRoundRect' function is a bit over-exaggerated (thought it might have some control over corner radius). With the exception of that, I'd love to see a :termRedirect() function that returns an object you can term.redirect() to, and arcs (drawn using either 2 points and 2 angles, or a circle centre and 2 angles) - yeah, I'm thinking pie charts...
Very good suggestions, thanks a lot!
I'll make these functions in the next update (1.5.0):
- surf:getTerm (Returns an object that you can term:redirect to. When you draw on that object it draws on the surface.)
- surf:drawPie
- surf:fillPie
- surf:drawArc
- surf:drawRoundedRect (like surf:drawRoundRect but you can specify the border radius)
- surf:fillRoundedRect

The surf:drawArc function will take a circle centre and 2 angles, not 2 points and 2 angles.
I'll release it on Sunday (may 3rd).

Edited by CrazedProgrammer, 28 April 2015 - 06:08 PM.


#32 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 28 April 2015 - 06:00 PM

View PostCreator, on 27 April 2015 - 10:25 PM, said:

Surface is like paintutils or opengl not window api. It is like comparing tcp ip and http
It is indeed like comparing HTTP and TCP/IP.
HTTP requires TCP/IP to work, but when you use HTTP you don't have to worry about TCP/IP.
The same goes for the Surface API.
The Surface API requires (actually it doesn't if you don't use surf:render) the Term API to work, but when you use the Surface API you don't have to worry about the Term API.

#33 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 28 April 2015 - 06:06 PM

View Postsyfyguy13, on 27 April 2015 - 10:49 PM, said:

oh man could you imagine a 3d engine pluggin to render objects to sprites.
I could imagine it, because it really is possible.
Maybe I'll even make something like it in the future.
The surf:fillTriangle function is a pretty good triangle filler, so you don't have to worry about that.

The complicated thing is the 3D to 2D transformations.
I've tried multiple times but maybe I'm not smart enough to understand 3D Perspective Projection :P

#34 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 01 May 2015 - 02:40 PM

View PostCrazedProgrammer, on 28 April 2015 - 05:55 PM, said:

I'll release it on Sunday (may 3rd).
Scrap that, I'll release it now!

I've updated the Surface API to version 1.5.0!
I've made these changes and additions to the API:
- Added surf:getBounds
- Added surf:copy
- Added surf:getTerm
- Added surf:drawRoundedRect and surf:fillRoundedRect
- Added surf:drawArc
- Added surf:drawPie and surf:fillPie
- Added surf:shader
- Added surf:shift
- Fixed a couple bugs

With the new surf:getTerm function, you can get a term object (which is fully implemented and you can term.redirect to it) from a surface
and then draw another surface on that term object using surf:render and it will be drawn on the original surface!
Now that is some inception shit!

You can download the new version from the same pastebin as before:
pastebin get 5YWfPd8Z surface
If you want to download version 1.4.0 for some reason you can still do that:
pastebin get vm1y156i surface

A big thanks to awsumben13 for giving great suggestions!

If you've got any bug reports, suggestions or questions then please leave a reply.

Edited by CrazedProgrammer, 01 May 2015 - 04:51 PM.


#35 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 01 May 2015 - 05:50 PM

I've made a small fix (version 1.5.1).
The surf:drawArc, surf:drawPie and surf:fillPie didn't rotate left with a bigger angle, but they rotated right.
I've fixed it by now.
You can download version 1.5.1 from the same pastebin as before:
pastebin get 5YWfPd8Z surface

#36 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 02 May 2015 - 12:28 PM

I've made some optimizations (version 1.5.2).
Some functions like math.sin are localized, and I've made a colors table so that I don't have to use string.format("%x", math.log(color) / math.log(2)).
Because of this, the drawing speed has increased by 80% (using term.blit).
You can now draw to the largest possible display (164 x 81) 20 times per second (the fastest speed possible) without any lag (at least on my PC).
You can download version 1.5.2 from the same pastebin as before:
pastebin get 5YWfPd8Z surface

#37 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 02 May 2015 - 01:42 PM

View PostCrazedProgrammer, on 02 May 2015 - 12:28 PM, said:

20 times per second (the fastest speed possible)

Odds are it'll be capable of even faster speeds than that; that's just the maximum amount of time-slices ComputerCraft lets you measure out of every second, so you won't want to exceed it.

Depending on your buffering strategy, term.blit() allows you to draw pretty much anything at about the same speed as term.clear(). Beats me how fast that actually is, but you can see how code written to take advantage of it can really do some impressive things.

#38 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 02 May 2015 - 03:47 PM

View PostBomb Bloke, on 02 May 2015 - 01:42 PM, said:

View PostCrazedProgrammer, on 02 May 2015 - 12:28 PM, said:

20 times per second (the fastest speed possible)

Odds are it'll be capable of even faster speeds than that; that's just the maximum amount of time-slices ComputerCraft lets you measure out of every second, so you won't want to exceed it.

Depending on your buffering strategy, term.blit() allows you to draw pretty much anything at about the same speed as term.clear(). Beats me how fast that actually is, but you can see how code written to take advantage of it can really do some impressive things.
It's a shame that you can't draw at anything above 20fps, but that's still enough to make impressive things.
Maybe I'll make a second visual demo to show how good it can draw to big screens.

I've fixed a BIG bug (version 1.5.3).
The surf:render function was broken for any ComputerCraft version below 1.74pre20 (a beta).
It was broken since 1.4.0 for all surfaces and 1.3 for transparent ones.
I've fixed it now.
The surf:render function now returns how many draw calls were made to the display object.
You can download version 1.5.3 from the same pastebin as before:
pastebin get 5YWfPd8Z surface

Edited by CrazedProgrammer, 02 May 2015 - 05:05 PM.


#39 クデル

  • Members
  • 349 posts

Posted 06 May 2015 - 10:32 PM

This helped me out a lot, prevented a lot of hassle which is always great. Just a suggestion, it would be pretty neat if you override some of the variables in the colours api. If were people to try to use colour on a non-advanced display it would just use black and white alternatives rather than crashing.

#40 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 09 May 2015 - 07:18 PM

Quote

This helped me out a lot, prevented a lot of hassle which is always great. Just a suggestion, it would be pretty neat if you override some of the variables in the colours api. If were people to try to use colour on a non-advanced display it would just use black and white alternatives rather than crashing.
Thanks!
You can still draw surfaces with only black and white colors on normal computers.
Programs that are made for color displays are not supposed to run on normal computers, mostly because of the absence of mouse events.
That's why I won't make a fix for it, but you can make a shader using the new surf:shader function to make a surface black and white if the user uses a normal computer:
if not term.isColor() then
  surf:shader(function (c, b, t) if b ~= colors.white then b = colors.black t = colors.white else b = colors.white t = colors.black end return c, b, t end)
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users