Programming Macaw OS

C reference: the window and drawing

These calls come from #include <macaw.h>. They open your window, draw into it, and work with pictures, sprites and maps. A picture is a number: 0 means there isn't one, and for drawinto it means the window itself. The Drawing chapter explains the Lua calls these are built on, and the C chapter shows how a C program fits together.

The window

window

void window(int w, int h, const char *title)

Says what size your window should be and what its title bar says. Call it in main, and it takes the place of Lua's WINDOW table.

realtime

void realtime(int on)

Makes your window draw every frame, which games and animations need. Without it, _draw runs only when something may have changed.

windowscale

void windowscale(int scale)

Shows every pixel of your window bigger, for pixel art. Call it in main.

resizable

void resizable(int on)

Says whether people can resize your window with its size box. Windows can be resized unless you say otherwise.

redraw

void redraw(void)

Asks for your window to be drawn again on the next frame. Call it from _update when something changes what the window shows, such as a timer or a download.

It takes nothing.

close

void close(void)

Closes your window, asking first if it has unsaved work.

It takes nothing.

settitle

void settitle(const char *title)

Changes the words in your window's title bar while the program runs.

windowmin

void windowmin(int w, int h)

Sets the smallest size people can resize your window to. Call it in main, before the window opens.

windowcolors

void windowcolors(int colours)

Asks for the screen to show this many colours while your program runs. Call it in main; the person is asked when the window opens, and if they don't allow it, your program closes.

windowcursor

void windowcursor(int on)

Shows or hides the pointer while it's over your window. Hide it when you draw your own pointer.

fullscreen

void fullscreen(int on)

Asks to make your window fill the screen, or puts it back. Asking shows the person a question, and if they don't allow it, your program closes. Leaving full screen never asks.

focused

int focused(void)

Tells you whether your window is the one in front, for example to pause a game when it isn't.

It takes nothing.

Returns 1 if your window is in front, or 0 if it isn't.

Drawing

cls

void cls(int colour)

Fills the whole window, or the picture you're drawing into, with one colour. It stays inside the clip rectangle, and the camera doesn't move it.

pset

void pset(int x, int y, int colour)

Sets one pixel to a colour.

pget

int pget(int x, int y)

Reads the colour of one pixel of the window, or of the picture you're drawing into.

Returns the pixel's colour, from 0 to 255, or 0 if the pixel is outside the window.

line

void line(int x0, int y0, int x1, int y1, int colour)

Draws a straight line from one point to another.

rect

void rect(int x, int y, int w, int h, int colour)

Draws the outline of a rectangle, one pixel thick.

rectfill

void rectfill(int x, int y, int w, int h, int colour)

Fills a rectangle with one colour.

circ

void circ(int x, int y, int r, int colour)

Draws the outline of a circle.

circfill

void circfill(int x, int y, int r, int colour)

Draws a filled circle.

tri

void tri(int x0, int y0, int x1, int y1, int x2, int y2, int colour)

Draws a filled triangle between three corners.

text

void text(const char *s, int x, int y, int colour)

Writes text in the built-in font. Each character takes a cell 6 pixels wide and 9 high, and \n starts a new line.

textwidth

int textwidth(const char *s)

Measures how wide some text would be, so you can line it up before you write it.

Returns its width in pixels: 6 for each character, or the widest line's width if it has more than one.

clip

void clip(int x, int y, int w, int h)

Limits drawing to a rectangle, so nothing is drawn outside it. The camera doesn't move the rectangle.

noclip

void noclip(void)

Lets you draw anywhere in the window, or the picture you're drawing into, again after clip.

It takes nothing.

camera

void camera(int x, int y)

Moves everything drawn after it by -x, -y. Use it to scroll a world that's bigger than the window, and camera(0, 0) to put it back.

box

void box(int x, int y, int w, int h, int fill, int outline)

Draws a filled rectangle with an outline in another colour.

centertext

void centertext(const char *s, int x, int y, int colour)

Writes text centred on a point across, which is handy for titles and buttons.

shadowtext

void shadowtext(const char *s, int x, int y, int colour, int shadow)

Writes text with a shadow one pixel down and to the right, so it stands out on a busy background.

textscaled

void textscaled(const char *s, int x, int y, int colour, int scale)

Writes text with every pixel of the font made bigger.

wraptext

int wraptext(const char *s, int columns)

Breaks text into lines short enough to fit, breaking at spaces where it can. Read the lines afterwards with wrapline.

Returns how many lines there are.

wrapline

int wrapline(int i, char *buf, int size)

Gives you one of the lines the last wraptext made.

Returns the whole line's length in characters, or -1 if there's no line i.

Colours

pal

void pal(int colour, int r, int g, int b)

Changes what one colour number looks like, everywhere on the screen, until you put it back with palreset.

palreset

void palreset(void)

Puts every colour back to how it normally looks, after pal.

It takes nothing.

drawpal

void drawpal(int from, int to)

From now on, anything you draw in one colour is drawn in another instead. Use it to flash a sprite white when it's hit, for example.

drawpalreset

void drawpalreset(void)

Stops swapping colours, so every colour draws as itself again.

It takes nothing.

nearestcolor

int nearestcolor(int r, int g, int b)

Finds which of the 256 colours is closest to a mix of red, green and blue, as they look now.

Returns the nearest colour number, from 0 to 255.

The window's memory

screenmem

int screenmem(void)

Gives you the address of your window's picture in memory, so you can write pixels into it directly. Each pixel is one byte holding its colour: the pixel at x, y is at the address plus y * screenwidth() + x.

It takes nothing.

Returns the address. Turn it into a pointer with (unsigned char *)screenmem().

screenwidth

int screenwidth(void)

Tells you how wide your window's picture is. Ask again after the window is resized, because it gets a new picture.

It takes nothing.

Returns the width in pixels.

screenheight

int screenheight(void)

Tells you how tall your window's picture is. Ask again after the window is resized.

It takes nothing.

Returns the height in pixels.

The screen

display_width

int display_width(void)

Tells you how wide the whole screen is now.

It takes nothing.

Returns the screen's width in pixels.

display_height

int display_height(void)

Tells you how tall the whole screen is now.

It takes nothing.

Returns the screen's height in pixels.

display_current

void display_current(int *w, int *h, int *colours)

Tells you the screen's size and how many colours it shows, all in one call.

display_sizes

int display_sizes(void)

Finds out which sizes the screen can be. Read each one afterwards with display_size.

It takes nothing.

Returns how many sizes there are.

display_size

int display_size(int i, int *w, int *h)

Gives you one of the sizes the last display_sizes found. Call display_sizes first.

Returns 1 if there's a size i, or 0 if there isn't.

display_request

void display_request(int w, int h, int colours, void (*fn)(int ok))

Asks for the screen to be a different size or show a different number of colours. The person is asked first, and if they don't allow it, your program closes. The screen goes back when your program quits.

Pictures

newpicture

int newpicture(int w, int h)

Makes a new, empty picture that you can draw into or draw from. Every pixel starts as colour 0.

Returns the picture's number, or 0 if the size is out of range.

loadpicture

int loadpicture(const char *path)

Loads a picture from a file: a .pxl saved by Paint, or a .png.

Returns the picture's number, or 0 if the file couldn't be read.

freepicture

void freepicture(int p)

Lets go of a picture you've finished with. Don't use its number afterwards: that stops your program with an error.

picturewidth

int picturewidth(int p)

Tells you how wide a picture is.

Returns its width in pixels.

pictureheight

int pictureheight(int p)

Tells you how tall a picture is.

Returns its height in pixels.

drawpicture

void drawpicture(int p, int x, int y, int key)

Draws a whole picture into the window, or into the picture you're drawing into.

drawpart

void drawpart(int p, int sx, int sy, int sw, int sh, int x, int y, int key)

Draws one rectangle of a picture, such as a single frame from a sheet of them.

drawinto

void drawinto(int p)

Sends all drawing into a picture instead of your window, so you can build something once and draw it many times. It also undoes any clip.

drawpictureex

void drawpictureex(int p, int x, int y, int key, int scale, int flipx, int flipy)

Draws a whole picture, made bigger or flipped.

drawpartex

void drawpartex(int p, int sx, int sy, int sw, int sh, int x, int y, int key, int scale, int flipx, int flipy)

Draws one rectangle of a picture, made bigger or flipped.

savepicture

int savepicture(int p, const char *path)

Saves a picture as a .pxl file, which Paint can open.

Returns 1 if it was saved, or 0 if it couldn't be.

parsepicture

int parsepicture(const char **rows, int n, const char *chars, const int *colours)

Makes a picture from rows of text, one character for each pixel. It's a quick way to draw small things in your code.

Returns the picture's number, or 0 if it couldn't be made. It's as wide as the longest row.

decodepicture

int decodepicture(const void *png, int n, int maxw, int maxh, int background, int sprite, int *w, int *h)

Makes a picture from PNG data in memory, such as a file you've read or something downloaded. It's shrunk to fit if it's too big.

Returns the picture's number, or 0 if the data isn't a PNG it can read.

decodepicturefile

int decodepicturefile(const char *path, int maxw, int maxh, int background, int sprite, int *w, int *h)

The same as decodepicture, but reads the PNG from a file.

Returns the picture's number, or 0 if the file couldn't be read as a PNG.

importpicture

int importpicture(const void *png, int n, int maxw, int maxh, int *w, int *h)

Brings in a PNG made elsewhere, ready for editing: each pixel becomes the nearest of the 256 colours, and see-through parts become colour 0. It's made smaller only if it doesn't fit.

Returns the picture's number, or 0 if the data isn't a PNG it can read.

importpicturefile

int importpicturefile(const char *path, int maxw, int maxh, int *w, int *h)

The same as importpicture, but reads the PNG from a file.

Returns the picture's number, or 0 if the file couldn't be read as a PNG.

pictureget

int pictureget(int p, int x, int y)

Reads the colour of one pixel of a picture.

Returns the pixel's colour, from 0 to 255, or 0 if it's outside the picture.

pictureset

void pictureset(int p, int x, int y, int colour)

Sets one pixel of a picture to a colour. A pixel outside the picture is left alone.

picturesize

void picturesize(int p, int *w, int *h)

Tells you a picture's width and height in one call.

pictureclear

void pictureclear(int p, int colour)

Fills a whole picture with one colour.

picturecopy

int picturecopy(int p)

Makes a new picture that's the same as another one, so you can change the copy and keep the first.

Returns the new picture's number.

picturegetrgb

void picturegetrgb(int p, int x, int y, int *r, int *g, int *b)

Reads a pixel's true colour as red, green and blue. That's what shows when the screen is in thousands of colours.

picturesetrgb

void picturesetrgb(int p, int x, int y, int r, int g, int b)

Sets a pixel's true colour, which shows when the screen is in thousands of colours. The pixel's colour number becomes the nearest of the 256.

Sprites and the map

spr

void spr(int n, int x, int y)

Draws one 16 x 16 sprite from your program's sprite sheet, gfx.pxl. Colour 0 in it is see-through.

sprex

void sprex(int n, int x, int y, int w, int h, int flipx, int flipy)

Draws a block of sprites together, flipped if you like. A 2 x 2 block is a 32 x 32 sprite made of four.

sspr

void sspr(int sx, int sy, int sw, int sh, int x, int y, int scale, int flipx, int flipy)

Draws any rectangle of the sprite sheet, given in pixels rather than sprites.

fget

int fget(int n)

Reads a sprite's flags, from gfx.flags. Flags mean whatever you decide, such as flag 0 for a solid tile.

Returns all 8 flags as one number, from 0 to 255: flag f is set if (fget(n) >> f) & 1 is 1.

fset

void fset(int n, int flags)

Sets all 8 of a sprite's flags at once, while your program runs.

map

void map(int cx, int cy, int x, int y, int cw, int ch, const char *layer)

Draws tiles from your program's map, map.map, each one a 16 x 16 sprite. Only the tiles inside the window are drawn, and empty tiles (0) aren't drawn at all.

mget

int mget(int x, int y, const char *layer)

Reads one tile of the map.

Returns the tile's sprite number, or 0 if it's empty, off the map, or there's no such layer.

mset

void mset(int x, int y, int n, const char *layer)

Changes one tile of the map while your program runs. A tile off the map is left alone.

mapwidth

int mapwidth(const char *layer)

Tells you how many tiles across a map layer is.

Returns its width in tiles, or 0 if there's no such layer.

mapheight

int mapheight(const char *layer)

Tells you how many tiles down a map layer is.

Returns its height in tiles, or 0 if there's no such layer.

maplayers

int maplayers(void)

Tells you how many layers your map has.

It takes nothing.

Returns how many layers there are.

maplayername

int maplayername(int i, char *buf, int size)

Gives you the name of one of the map's layers, to pass to map or mget.

Returns the whole name's length, or 0 if there's no layer i.

Sprites as pictures

sprimage

int sprimage(int n, int w, int h)

Copies a sprite, or a block of them, into a picture of its own. Use it to scale a sprite or use it as a 3D texture.

Returns the new picture's number, or 0 if it couldn't be made.

spritesheet

int spritesheet(void)

Gives you your program's whole sprite sheet as a picture, 256 x 256 pixels. Whatever you draw into it shows in spr and map straight away.

It takes nothing.

Returns the sprite sheet's picture number. It's the same number each time.

Maps made elsewhere

importmap

int importmap(const char *path)

Reads a map made in another program: a Tiled map (.tmx or .json) or a CSV of tile numbers. It keeps at most 8 layers.

Returns a number for the imported map, or 0 if it couldn't be read.

importmapimage

int importmapimage(const char *path, int tilesize)

Reads a picture of a whole level (a PNG) and cuts it into tiles. Each different tile becomes a sprite, numbered 1, 2, 3 in the order they're first met, and see-through tiles are 0.

Returns a number for the imported map, or 0 if it couldn't be read.

imap_layers

int imap_layers(int m)

Tells you how many layers an imported map has.

Returns how many layers it has.

imap_layername

int imap_layername(int m, int layer, char *buf, int size)

Gives you the name of one of an imported map's layers.

Returns the whole name's length, or -1 if there's no such layer.

imap_size

int imap_size(int m, int layer, int *w, int *h)

Tells you how big one layer of an imported map is, in tiles.

Returns 1 if there's such a layer, or 0 if there isn't.

imap_get

int imap_get(int m, int layer, int x, int y)

Reads one tile of an imported map.

Returns the tile's sprite number, or 0 if it's empty, off the map, or there's no such layer.

imap_picture

int imap_picture(int m)

Gives you a 256 x 256 picture of the imported map's tiles, each in its sprite's place, laid out like a sprite sheet.

Returns the picture's number, or 0 if the map brought no tiles with it.

imap_used

int imap_used(int m, int sprite)

Tells you whether the imported map's tiles fill a sprite, so you know which places in the sheet they take.

Returns 1 if the map's tiles fill that sprite, or 0 if they don't.

imap_notes

int imap_notes(int m)

Tells you how many notes the import left, about what was left out or changed. Read each one with imap_note.

Returns how many notes there are.

imap_note

int imap_note(int m, int i, char *buf, int size)

Gives you one of the import's notes, as text you can show the person.

Returns the whole note's length, or -1 if there's no note i.

imap_free

void imap_free(int m)

Lets go of an imported map you've finished with. A picture you got from imap_picture stays until you freepicture it.