This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
billysback's profile picture

[API] ART - Advanced art utils [v1.1]

Started by billysback, 10 November 2012 - 09:41 AM
billysback #1
Posted 10 November 2012 - 10:41 AM

Art API is essentially a advanced (in my opinon) version of paintutilis.

First, lets just strait off list the functions:
SpoilergetPixelIndex(table)
-for in-API function use, returns the index of the pixel in table format, not metatable format.

clear()
-clears the terminal of all colors and clears the pixel table, resets the API.

getPixelByLoc(x, y)
-returns this index of a pixel in table format at the x and y coordinate, the last found one.

setPixel(x, y, bcolor, tcolor, ch, id, index)
-adds a new pixel in table format and add it to the storage, it also returns this pixel.
-bcolor is background color to set the terminal to when the pixel is drawn (nil for none)
-tcolor is text color for the pixel
-ch is the character
-id is mainly for use of in-API functions, nil to ignore it
-index is mainly for use of in-API functions, nil to ignore it.

setPixelByData(x, y, data)
-data is a table containing: bcolor, tcolor, ch, id, index in that order.

drawLine(x1, y1, x2, y2, data)
-returns a table containing pixels in table format.

drawLineP(p1, p2, data)
-same as drawLine() but p1 is a table containing x, y and p2 is a table containing x, y of point 2

drawSquare(corner1, corner2, corner3, corner4, data)
-draws line between corners

drawCircle(center, radius, data, fill)
-draws a circle around the center.

drawPolygon(points, data)
-draws a polygon by joining up each point with a line

<ALL DRAW FUNCTIONS RETURN A PIXEL TABLE IN TABLE FORMAT>

createPixelArray(pixs)
-returns a pixel array based off the pixel table in table format (pixs)

createPixel(x, y, bcolor, tcolor, character)
-returns a pixel in pixel format using x, y, bcolor, tcolor, character.

createPixelByLoc(x, y)
-returns a pixel in pixel format using the x, y to look at stored pixels in table format to create a pixel

It adds two "objects" or metatables through this API, Pixel and Pixelarray.
Here are the Pixel functions:
Spoilerpixel:getID() - returns the id of the pixel
pixel:getX() - returns the x of the pixel
pixel:getY() - returns the y of the pixel
pixel:getBackgroundColor() - returns the color of the pixel
pixel:getTextColor() - returns the color of the pixel
pixel:getChar() - returns the character of the pixel
pixel:setX(x) - sets the pixel x to x
pixel:setY(y) - sets the pixel y to y
pixel:setBackgroundColor(color) - sets the pixel background color to color
pixel:setTextColor(color) - sets the pixel background color to color
pixel:setChar(character) - sets the pixel character to character
pixel:updatePixel() - updates the pixel to store it in table format.
pixel:draw() - draws the pixel
Here are the Pixel Array functions:
Spoilerpixelarray:getPixels()
-returns the table of pixels

pixelarray:getPixel(id)
-returns a pixel inside the pixelarray which has this ID

pixelarray:getPixelByLoc(x, y)
-returns a pixel inside the pixelarray which has the X as x and the Y as y

pixelarray:draw()
-draws all the pixels in the pixelarray

pixelarray:updatePixels()
-updates all the pixels

pixelarray:merge(pixelarray)
-merges the pixelarray in to the pixelarray the function is called through

pixelarray:addPixels(pixs)
-adds pixels in pixel format the the pixelarray

Download (Pastebin):
Spoilerhttp://pastebin.com/ERf5QAkK

Thats pretty much it, it's reasonably simple but make some things, such as games, easier to manipulate and easier to make graphical programs in general.

Heres a small example game I made using the art API as an example:
SpoilerDownload:
Spoilerhttp://pastebin.com/ERf5QAkK

Heres a Screenshot:
Spoiler

Controls:
arrow keys to move around the "village", up to 6 houses will spawn, you can walk around and look at these houses.

Details:
Walking past the edge of your current map view will walk you one that way in the real map if it can;
To change the map size or the amount of houses go to line 6 and 7 for map width and height, change those values, line 66 change the math.random(6) to math.random(number_of_houses).

There are now people as well, walk in to the people to talk to them.

Changelog:
Spoilerv1.0: initial release
v1.1: bug fixes, fixed draw functions.

Thanks!
Leo Verto #2
Posted 10 November 2012 - 12:25 PM
Impressive, am I allowed to use it to make a paint program?
billysback #3
Posted 10 November 2012 - 09:05 PM
Sure, use it for whatever just credit me :unsure:/>/>
billysback #4
Posted 11 November 2012 - 01:45 AM
UPDATE:
updated to version 1.1, numerous bug fixed, took drawLine from paintutilis because mine wasn't working and fixed draw functions.
Pretty much every function is tested now, and all bugs I have found have been fixed.