Programming Macaw OS

Your first program

Make one

Open Terminal (it's on the desktop, or in the macaw menu) and type:

new first.lua

This makes /home/first.lua from a template and opens it in Code. Press Ctrl+R (Cmd+R on a Mac) to run it. You get a window with an orange ball that you move with the arrow keys. Change something and press Ctrl+R again, and the window starts over with your change. Ctrl+S saves the file.

Terminal can start other kinds of program too. newcart mygame makes a game cart, newapp myapp.lua makes an app with buttons, and new3d my3d.lua makes a program with a 3D scene in it.

What a program looks like

WINDOW = { w = 240, h = 160, title = "Hello" }

local x = 120

function _init()
  -- once, when the program starts
end

function _update()
  -- 60 times a second: read the keys, move things
  if btn("left") then x = x - 1 end
  if btn("right") then x = x + 1 end
end

function _draw()
  -- draw the window
  cls(colors.navy)
  text("Hello, Macaw!", 8, 8, colors.yellow)
  circfill(x, 80, 8, colors.orange)
end

When the program starts, the file runs once from top to bottom. Anything at the top level, like local x = 120, is set up before _init is called. After that, Macaw OS calls these functions:

FunctionWhen it's called
_init()Once, after the file has run and the window is open
_update(dt)Every frame, about 60 times a second, while the program runs. dt is the seconds since the last one (see below)
_draw()When the window needs drawing (see below)
_resize(w, h)When someone resizes the window. Its new size is w x h
_close()When the window is closing. Save anything you need to here
_drop(paths, x, y)When files are dragged onto your window, at x, y
_display(w, h, colours)When the screen changes size or number of colours

You only need the ones you use. A program with just _draw works fine.

Frames don't always come exactly 60 times a second. A slower computer, or a busy moment, makes some of them take longer. To move something at the same speed whatever happens, move it by its speed times dt, the seconds since the last _update. deltatime() gives you the same number anywhere in your program.

local x, speed = 120, 60      -- speed: pixels a second

function _update(dt)
  if btn("right") then x = x + speed * dt end
end

dt is never more than a tenth of a second, so after a pause nothing jumps a long way.

The window

WINDOW describes the window you want. Every field is optional:

FieldWhat it does
w, hThe size of the inside of the window, in pixels
titleThe words in the title bar (settitle(s) changes them later)
scaleShows every pixel 2, 3 or 4 times as big, if it fits on the screen
realtimetrue draws every frame, which games need (see below)
resizablefalse stops people resizing the window
minw, minhThe smallest size the window can be resized to
fullscreentrue asks to start the window full screen
colors8, 256 or "thousands" asks for the screen in that many colours while your program runs
cursorfalse hides the pointer over the window, for when you draw your own

The top-left pixel of the window is 0, 0. x goes right and y goes down. If someone resizes the window, WINDOW.w and WINDOW.h change to match and _resize is called.

When _draw runs

Most programs only change when someone does something, so Macaw OS doesn't redraw every window every frame. _draw runs while someone is using your window, after they choose a menu item, answer a dialog or drop something on it, and every so often anyway.

When it goes wrong

If your program has a mistake, you hear a squawk and its window shows This program crashed., with what went wrong and the line it happened on. Fix it in Code and press Ctrl+R again.

A program can't freeze Macaw OS. If it runs for a long time without finishing, because of a loop that never ends or a big file it's loading, it's paused every 30 milliseconds and the rest of the desktop carries on. See busy programs.

Keeping things

Your program can read and write files in /home:

fs.write("/home/hello-score.txt", tostring(score))
local saved = tonumber(fs.read("/home/hello-score.txt") or "") or 0

fs.read returns nil if the file isn't there yet, so the or gives you a value to start with. See Files and the system for the other file calls.

Next

Make a game builds a whole game one step at a time. Or go straight to drawing, input and sound or 3D.