-
Notifications
You must be signed in to change notification settings - Fork 8
Graphics with Lua
Computers also have programmable screen sessions, that have ids one through eight. You can toggle between them using ^1
, all the way up to ^8
in your computer. As you can tell, most of them remain unused, except for ^1
, which is your terminal instance, and ^8
, which is used for view
and edit
. It's a good idea to avoid trying to set the components for these two sessions, since it would probably break something.
Screen pixels are also represented as integers, which correspond to color values for maps in minecraft. You can check out how these color values work here: http://minecraft.gamepedia.com/Map_item_format#Map_colors
Frames are the safe, easy way to draw to a screen session. To create a buffer for adding your own frames to, use the following:
buffer = screenBuffer(index)
Where index
is the index of the screen session you want to bind to. This will return nil
if the screen is already allocated.
The buffer
object that is returned is your way of interacting with this component. There's multiple functions you can use from it:
-
buffer:update(id)
Sets the frame of this buffer and updates the component. More on this below. -
buffer:poll()
Reads text input from this component, similar to the globalread()
function, except it only reads the text that has been already sent. It will returnnil
if no text has been added since the last time it was polled. -
buffer:pollCoords()
Reads aninteraction
object from the buffer. This object has ax()
,y()
, andname()
method to get the x coordinate, y coordinate, and the name of the player who interacted, respectively.nil
is returned if the buffer has not been interacted since the last time is was polled, and should be called in a loop untilnil
is returned frompollCoords()
. -
buffer:destroy()
Destroys the buffer and frees the screen index it belonged to. You do not need to do this at the end of your program, it will do this for you.
Once you're created your buffer, you can use switchSession(index)
to change the index of the screen.
Now, when you want to update the frame of your buffer, you will need to create a frame
object, whose ID you will need to pass to the buffer:update(id)
method. This is done using the screenFrame()
global function, which will return a frame object, with the following functions:
-
frame:set(x, y, c)
Sets the pixel at(x,y)
to the colorc
(integer). -
frame:fill(c)
Covers the screen with the given color. -
frame:write(x, y, text)
Writes the given text on the screen. Color codes can be used. -
frame:box(x, y, w, h, c)
Draws a box at(x,y)
with the given width and height, using the colorc
. -
frame:len(text)
Returns the length (in pixels) of the text. Ignores color codes. -
frame:id()
Returns the ID of the frame -
frame:getWidth()
Returns the width of the frame -
frame:getHeight()
Returns the height of the frame
Modify to your heart's content, and then send it to your buffer using buffer:update(frame:id())
.
Note: since 1.7-alpha, you cannot make buffer:update(id)
calls less than 100ms apart. If you try doing so, the function will wait until 100ms has passed. This does not mean there is a fixed framerate, the disply is still updated on demand, but instead means the display cannot be updated at more than 10Hz via Lua code.
There used to be a tutorial here on custom/threaded rendering with Lua, but it has been since removed in version 1.0