Programming Macaw OS

C reference: levels

These calls come from #include <macawlevel.h>. They let a C program open, draw, walk round and even build the levels you make in Brushwork, the level editor. A level is a number: openlevel, loadlevel and newlevel each give you one, and 0 means it didn't work. They work the same way as Lua's level calls, which Levels explains in more detail, and C tells you about C on Macaw OS.

A few things to know

The structs

levelcam is a camera, for water and reflection probes:

levelviewfn is the type of a function you write for water and probes to call. They call it when they need the view from a camera drawn: a mirrored camera under the water for a reflection, or one of the six views round a probe. Your function sets camera3d to the camera and draws the level above the height it's given (with leveldrawabove(level, above)), plus anything else that should show. It returns nothing. Where a call takes one, you can pass NULL, and then it draws just the level. Its arguments are:

levellightinfo is a lamp or an area light, as levellight gives them and levelbake takes them. An area light is a glowing rectangle that lights one way.

Opening, saving and building

openlevel

int openlevel(const char *path)

Reads a .lvl file, builds it, and puts on its baked light from the .lit file next to it (and its baked probes from the .prb file, if the light went on). This is the usual way to start: the level is ready to draw and bump into straight away.

Returns the level's number, or 0 if it couldn't be opened (levelerror says why).

loadlevel

int loadlevel(const char *path)

Reads a .lvl file into a level table without building it. Use it when you want to look at or change a level before building it, or don't need to draw it at all.

Returns the level's number, or 0 if it couldn't be read (levelerror says why).

newlevel

int newlevel(void)

Makes an empty level, with no brushes or entities, for building from code. Its settings start as an ambient of 0.08, a bounce of 1 and a luxel of 0.5.

It takes nothing.

Returns the new level's number.

savelevel

int savelevel(int level, const char *path)

Writes a level's table to a .lvl file, which Brushwork can open. It doesn't save the baked light: levellightdata gives you that to keep yourself. Brush corners are written to 4 significant figures and entity numbers to 6, so keep your sizes simple.

Returns 1 if it was saved, or 0 if it couldn't be (levelerror says why).

buildlevel

int buildlevel(int level)

Builds a level's table into a level that draws and collides. Call it after loadlevel or newlevel, and again whenever you've changed the brushes. It makes the level's water and probes afresh from its table, and the new build has no baked light yet.

Returns 1 if it was built, or 0 if it couldn't be (levelerror says why).

freelevel

void freelevel(int level)

Lets go of a level and everything built for it. After this the number means nothing, so don't use it again.

levelerror

int levelerror(char *buf, int size)

Gives the message from the last call that didn't work, such as a file that wasn't there.

Returns the length of the whole message.

levelbuilt

int levelbuilt(int level)

Tells you whether a level has been built, so whether it can draw and collide yet.

Returns 1 if it's built, or 0 if not.

levellit

int levellit(int level)

Tells you whether a level has baked light on it, from its .lit file, levelsetlight or a finished bake.

Returns 1 if it's lit, or 0 if it has plain shading.

levelpath

int levelpath(int level, char *buf, int size)

Gives the file a level was opened or loaded from.

Returns the length of the file name, or 0 for a level from newlevel.

levellitpath

int levellitpath(const char *path, char *buf, int size)

Gives the file a level's baked light is kept in: the same name with .lit in place of .lvl.

Returns the length of the .lit file's name.

levelprobepath

int levelprobepath(const char *path, char *buf, int size)

Gives the file a level's baked reflection probes are kept in: the same name with .prb in place of .lvl.

Returns the length of the .prb file's name.

Drawing

These draw a built level with camera3d's camera, unless they say otherwise.

leveldrawbsp

int leveldrawbsp(int level, int *faces)

Draws only the parts of the level the camera can see, working them out through the level's BSP tree and portals (the way it's cut into pieces). This is the fast way, and the one to use each frame.

Returns how many leaves (pieces of the level) were drawn.

leveldraw

void leveldraw(int level)

Draws all of the level. With a graphics card this is quick too, since the card keeps the whole level.

leveldrawabove

void leveldrawabove(int level, double y)

Draws only the part of the level above a height. Use it in a levelviewfn, to leave out what's under the water.

leveldrawgpu

void leveldrawgpu(int level)

Draws all of the level with the graphics chip's camera, viewport and fog (from <macawgpu.h>) instead of camera3d's.

leveldrawgpuabove

void leveldrawgpuabove(int level, double y)

Draws the part of the level above a height, with the graphics chip's camera.

Bumping into it

These work on a built level. Water isn't solid, so they go straight through it.

leveltracebox

double leveltracebox(int level, double ax, double ay, double az, double bx, double by, double bz, double hx, double hy, double hz, double *nx, double *ny, double *nz, int *hit)

Moves a box from one place towards another and tells you how far it gets before it hits something. Use it to move a player or a ball: it stops just short of the wall.

Returns how far it got, from 0 (not at all) to 1 (all the way).

leveltrace

double leveltrace(int level, double x0, double y0, double z0, double x1, double y1, double z1)

Follows a line from one point to another and tells you how far it gets before it hits something. Use it to see whether one place can see another.

Returns how far along it got, from 0 to 1, where 1 means it hit nothing.

levelraycast

double levelraycast(int level, double x, double y, double z, double dx, double dy, double dz)

Sends a ray from a point in a direction and finds where it first goes into a brush. It tries every brush, so it works straight after building. A ray that starts inside a brush doesn't count that one.

Returns t, so the spot it hits is x + t * dx, y + t * dy, z + t * dz, or -1 if it hits nothing. If the direction is one unit long, t is the distance.

levelboxsolid

int levelboxsolid(int level, double x0, double y0, double z0, double x1, double y1, double z1)

Tells you whether a box touches anything solid. The corners can be given in either order.

Returns 1 if the box touches something solid, or 0 if it's in open space.

levelboxinside

int levelboxinside(int level, double x, double y, double z, double hx, double hy, double hz)

Tells you whether a box is inside a brush, by more than just touching it. Use it to check a player hasn't ended up stuck in a wall.

Returns 1 if the box is inside something, or 0 if not.

Water

In Brushwork you can make brushes into water. Water isn't solid, and its top is a surface that reflects. A built level has a list of water surfaces, which you draw after the level. Each needs its reflection drawn first, with waterreflect, or baked once with waterbake.

watercount

int watercount(int level)

Tells you how many water surfaces a level has to draw: those built from its water brushes, and any you've added with newwater.

Returns how many water surfaces there are (they count from 0).

newwater

int newwater(int level, double x0, double z0, double x1, double z1, double y)

Adds a water surface of your own: a flat rectangle at a height. buildlevel makes the list again from the level's table, so add yours after building.

Returns the new water's number, or -1 if one of the numbers wasn't a proper number.

waterinfo

void waterinfo(int level, int i, double *x0, double *z0, double *x1, double *z1, double *y)

Gives where a water surface is.

waterreflect

void waterreflect(int level, int i, const levelcam *cam, levelviewfn draw, void *user)

Draws a water's live reflection: the level is drawn again from a camera mirrored under the water, into the water's own picture. Call it before you clear the screen and draw the scene, then waterdraw after. It does nothing if the water's reflections are off.

waterdraw

void waterdraw(int level, int i, const levelcam *cam, double t, const char *how)

Draws a water surface, with ripples and its reflection mixed in. Call it after you've drawn the level, with the same camera.

waterbake

int waterbake(int level, int i, levelviewfn draw, void *user)

Bakes a water's reflection once, so it doesn't have to be drawn every frame. Each call draws one of six views from just above the middle of the water; keep calling it until it returns 1. Things that move won't show in a baked reflection.

Returns 1 once it's done (or once it's clear it can't be baked), or 0 while there are views left.

waterbaked

int waterbaked(int level, int i)

Tells you whether a water's reflection has been baked.

Returns 1 if it's baked, 0 if not yet, or -1 if it can't be (the graphics card has no cube maps).

waterreflections

int waterreflections(int level, int i, int on)

Turns a water's reflection on or off, or asks whether it's on. With it off, the water is a plain sheet, which is quicker on a slow computer.

Returns 1 if reflections are on now, or 0 if they're off.

watercamera

void watercamera(const levelcam *cam)

Points the graphics chip's camera the same way as camera3d, so gpu_ drawing in a reflection lines up with the level. It also makes clockwise triangles the front ones.

Reflection probes

A shiny surface reflects the nearest reflection probe: a point in the level whose view all round is baked into a cube map (six square pictures, one each way). Probes need a graphics card with cube maps; without one, shiny surfaces are drawn as they are. A probe is a number of its own, not a level.

bakeprobes

int bakeprobes(int level)

Bakes any of a level's own probes that aren't baked yet, a little at a time. Call it once a frame until it returns 1; each call draws one of a probe's six views, so it never holds things up. If nothing in the level is shiny, there's nothing to do.

Returns 1 once all are done (or there's nothing to bake), or 0 while there's more to do.

levelprobelimit

int levelprobelimit(int level, int n)

Sets how many probes may be on the graphics card at once, the ones nearest the camera, or asks. Each takes about 400 KB of the card's memory.

Returns the limit now.

probecount

int probecount(int level)

Tells you how many probes a built level has.

Returns how many probes there are (0 before it's built).

probeat

int probeat(int level, int i)

Gives one of a level's probes as a probe number, for the calls below.

Returns the probe's number (the same one each time you ask for that probe).

newprobe

int newprobe(double x, double y, double z, int size)

Makes a probe of your own at a place. Bake it with probebake, then give its cube map to a level with levelsetprobes.

Returns the probe's number, or 0 if a place wasn't a proper number.

probepos

void probepos(int probe, double *x, double *y, double *z)

Gives where a probe is.

probebake

int probebake(int probe, levelviewfn draw, void *user, int level)

Bakes a probe, drawing one of its six views each call. Keep calling it until it returns 1.

Returns 1 once it's done (or it can't be baked), or 0 while there are views left.

probebaked

int probebaked(int probe)

Tells you whether a probe has been baked.

Returns 1 if it's baked, 0 if not yet, or -1 if it can't be (the graphics card has no cube maps).

probeenv

int probeenv(int probe)

Gives a baked probe's cube map. It's the same kind of number <macawgpu.h>'s gpu_envmap gives, so gpu_envpixels and gpu_envmapbytes can keep it as bytes and make it again.

Returns the cube map's number, or 0 if the probe isn't baked yet.

levelsetprobes

void levelsetprobes(int level, const int *envs, int n)

Tells a built level which cube maps its shiny surfaces reflect. Each shiny face uses the nearest one in front of it, and prefers one in the same room.

levelprobedata

int levelprobedata(int level, void *buf, int size)

Gives a level's baked probes as the bytes of a .prb file, for you to save. Call it with NULL first to find out how big a buffer you need.

Returns how many bytes there are in all, or 0 if none of its probes is baked.

applylevelprobes

int applylevelprobes(int level, const void *data, int count)

Gives a built level's probes their cube maps from a .prb file's bytes, so they don't need baking again.

Returns 1 if every probe got its cube map, or 0 if any didn't.

What's in a level

These read the level's table, so they work before it's built too.

levelstart

void levelstart(int level, double *x, double *y, double *z, double *angle)

Gives where the player starts: the player start's feet, and which way it faces. A level with no start gives 0, 2, 0 and an angle of 0.

levellightcount

int levellightcount(int level)

Tells you how many lamps and area lights a level has.

Returns how many lights there are.

levellight

int levellight(int level, int i, levellightinfo *light)

Gives one of a level's lamps or area lights, with the defaults filled in for what the level doesn't say.

Returns 1 if there's an i'th light, or 0 if not.

levelwatercount

int levelwatercount(int level)

Tells you how many water brushes a level's table has.

Returns how many water surfaces the table has.

levelwater

int levelwater(int level, int i, double *x0, double *z0, double *x1, double *z1, double *y)

Gives where one of the table's water surfaces is: the top of a water brush (a shaped one gives the box round it).

Returns 1 if there's an i'th water, or 0 if not.

levelprobecount

int levelprobecount(int level)

Tells you how many probe entities a level's table has.

Returns how many probes the table has.

levelprobe

int levelprobe(int level, int i, double *x, double *y, double *z)

Gives where one of the table's probes is.

Returns 1 if there's an i'th probe, or 0 if not.

Entities

Each entity has a class, its kind, such as "pickup" or "start", and fields with values. These calls read a level's entities of one class, with the class's defaults filled in for fields the level doesn't give. Give the class as NULL for every entity, in the level's order. A field reads as a number or as text, whichever it is.

levelentitycount

int levelentitycount(int level, const char *class_)

Tells you how many entities of a class a level has.

Returns how many there are.

levelentity

int levelentity(int level, const char *class_, int i, double *x, double *y, double *z)

Gives where an entity is.

Returns 1 if there's an i'th one, or 0 if not.

levelentityhas

int levelentityhas(int level, const char *class_, int i, const char *field)

Tells you whether an entity has a field, and what kind of value it holds.

Returns 0 if there's no such field, 1 for a number, 2 for text, or 3 for true or false.

levelentitynum

double levelentitynum(int level, const char *class_, int i, const char *field)

Gives an entity's field as a number. Text that's a number reads as that number, true as 1 and false as 0.

Returns the field's value, or 0 if there's no such field (or it isn't a number).

levelentitytext

int levelentitytext(int level, const char *class_, int i, const char *field, char *buf, int size)

Gives an entity's field as text. Numbers come out as a level file writes them, and true or false as "true" or "false".

Returns the length of the text, or 0 if there's no such field.

levelentityfield

int levelentityfield(int level, const char *class_, int i, int k, char *buf, int size)

Gives the name of one of an entity's fields, so you can go through them all. They come in alphabetical order, and "class" is one of them.

Returns the length of the name, or 0 once there are no more fields.

Kinds of entity

These tell you about the classes a level can have: Macaw OS's own (light, arealight, start, probe), the generic ones (marker, pickup, enemy, trigger, sound, sign) and a game's own, from an entities.lua in the level's folder or a folder above it. Where a call takes classes, you can give it a level's number for the level's own classes, or a number from entityclasses.

entityclasses

int entityclasses(const char *path)

Finds the classes a level at a place can have, reading the game's entities.lua if there is one. The level doesn't need to exist yet.

Returns a number for that set of classes, or 0 if they couldn't be found.

entityclasscount

int entityclasscount(int classes)

Tells you how many classes there are in a set.

Returns how many classes there are.

entityclassname

int entityclassname(int classes, int i, char *buf, int size)

Gives the name of one class in a set: Macaw OS's own first, then the game's, then the generic ones.

Returns the length of the name, or 0 once there are no more.

entityclassinfo

int entityclassinfo(int classes, const char *name, const char *what, char *buf, int size)

Gives one thing about a class, as text. A class nobody describes comes back as a plain grey box marked "unknown".

Returns the length of the text, or 0 if there's nothing (so 0 for a kind it isn't).

entityclassfieldcount

int entityclassfieldcount(int classes, const char *name)

Tells you how many fields a class describes.

Returns how many fields it has (0 for a class nobody describes).

entityclassfield

int entityclassfield(int classes, const char *name, int k, char *fname, int fsize, char *type, int tsize, char *def, int dsize)

Gives one of a class's fields: its name, its type and its default, each as text.

Returns 1 if there's a k'th field, or 0 if not.

Building from code: brushes

These change a level's table. Call buildlevel afterwards to see the changes. A box's six faces, in order, are -x, +x, -y, +y, -z and +z (west, east, bottom, top, south, north), and a texture is a built-in name like "brick" or a .pxl picture's path.

levelbrushcount

int levelbrushcount(int level)

Tells you how many brushes a level has.

Returns how many brushes there are.

leveladdbox

int leveladdbox(int level, double x0, double y0, double z0, double x1, double y1, double z1, const char **tex)

Adds a box-shaped brush, from one corner to the opposite one. The corners can be given in either order.

Returns the new brush's number, or -1 if a corner wasn't a proper number.

leveladdshape

int leveladdshape(int level, const float *planes, int n, const char **tex)

Adds a convex brush of any shape, such as a wedge or a ramp, made of flat sides (planes). The brush is the space inside all of them.

Returns the new brush's number, or -1 if it couldn't be added (too few planes, or a bad one).

levelremovebrush

void levelremovebrush(int level, int b)

Takes a brush out of the level. The brushes after it move down one number.

levelbrushbox

int levelbrushbox(int level, int b, double *x0, double *y0, double *z0, double *x1, double *y1, double *z1)

Gives a brush's corners: a box's own, or the box round a shaped brush.

Returns 1 for a box, 2 for a shaped brush, or 0 if there's no such brush.

levelbrushtexture

void levelbrushtexture(int level, int b, int face, const char *name)

Changes the texture on one face of a brush.

levelbrushwater

void levelbrushwater(int level, int b, int on)

Makes a brush water or solid again. Water isn't solid, and its top is a reflecting surface.

levelbrushglow

void levelbrushglow(int level, int b, int face, double strength, const char *colour)

Makes a face of a brush glow. A glowing face lights the level when its light is baked.

levelbrushshine

void levelbrushshine(int level, int b, int face, double shine)

Makes a brush, or one face of it, shiny, so it reflects the nearest probe.

Building from code: entities and settings

leveladdentity

int leveladdentity(int level, const char *class_, double x, double y, double z)

Adds an entity of a class at a place. Give it fields with the calls below.

Returns the entity's number, in the level's order (as levelentity counts with a NULL class), or -1 if the class was empty or a place wasn't a proper number.

levelremoveentity

void levelremoveentity(int level, int e)

Takes an entity out of the level. The entities after it move down one number.

levelmoveentity

void levelmoveentity(int level, int e, double x, double y, double z)

Moves an entity to a new place.

levelentitysetnum

void levelentitysetnum(int level, int e, const char *field, double v)

Sets one of an entity's fields to a number.

levelentitysettext

void levelentitysettext(int level, int e, const char *field, const char *text)

Sets one of an entity's fields to text, or takes the field away.

levelentitysetbool

void levelentitysetbool(int level, int e, const char *field, int v)

Sets one of an entity's fields to true or false.

levelsetting

double levelsetting(int level, const char *name)

Gives one of a level's number settings: "ambient" (the light everywhere), "bounce" (how much light bounces), "luxel" (the size of a patch of baked light) or "skylight" (how strong the sky's light is).

Returns the setting's value, or 0 if the level doesn't have it.

levelsettingtext

int levelsettingtext(int level, const char *name, char *buf, int size)

Gives one of a level's settings as text, such as "sky", the name of the sky round it.

Returns the length of the text, or 0 if the level doesn't have that setting.

levelsetsetting

void levelsetsetting(int level, const char *name, double v)

Sets one of a level's number settings. savelevel keeps "ambient", "bounce", "luxel" and "skylight".

levelsetsettingtext

void levelsetsettingtext(int level, const char *name, const char *text)

Sets one of a level's settings to text, such as "sky".

Skies and textures

levelskies

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

Gives the name of one of the skies there are, in alphabetical order, so you can list them.

Returns the length of the name, or 0 once there are no more.

levelsky

int levelsky(const char *name, int *pictures, double *r, double *g, double *b)

Gives a sky's six pictures, for levelsetsky, and its average colour.

Returns 1 if there's a sky of that name, or 0 if not.

leveltexture

int leveltexture(const char *name)

Gives the picture for a texture name, to draw with yourself.

Returns the picture's number. A name it doesn't know gives the stone picture.

leveltexturename

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

Gives the name of one of the built-in textures, so you can list them.

Returns the length of the name, or 0 once there are no more.

Baking light and the built level

Baking light works out how the level's lamps, glowing faces and sky light every surface, bounces and shadows included, and keeps the result. It takes a while, so it's done a step at a time. These calls need a built level.

levelbake

int levelbake(int level, const levellightinfo *lights, int n, double ambient, double bounce, double softness)

Starts baking a level's light. Then call levelbakestep until it's done.

Returns 1 if baking has started, or 0 if it couldn't (levelerror says why, such as a level too big to light).

levelbakestep

int levelbakestep(int level, double ms, double *progress, char *what, int size)

Bakes for a little while. Call it once a frame until it returns 1; then the level is lit.

Returns 1 once the baking is done, or 0 while there's more to do.

levellightdata

int levellightdata(int level, void *buf, int size)

Gives a level's baked light as bytes, to save in its .lit file. Call it with NULL first to find out how big a buffer you need.

Returns how many bytes there are in all, or 0 if the level isn't baked.

levelsetlight

int levelsetlight(int level, const void *data, int n)

Puts baked light back on a level, from bytes levellightdata gave or a .lit file holds.

Returns 1 if the light went on, or 0 if it was baked for a different level.

levelunbake

void levelunbake(int level)

Takes a level's baked light off, back to plain shading.

levelsetsky

void levelsetsky(int level, const int *pictures)

Puts a sky round a level, from six square pictures, or takes it away. Faces with the "sky" texture show it. buildlevel already puts on the sky the level's "sky" setting names.

levelfacecount

int levelfacecount(int level)

Tells you how many faces the built level has. Building cuts brushes into faces, so this isn't the same as six times the brushes.

Returns how many faces there are.

levelface

int levelface(int level, int i, float *xyz, int max, int *tex, double *nx, double *ny, double *nz)

Gives one face of the built level: its corners, its texture and which way it faces. The corners go anticlockwise, seen from the front.

Returns how many corners the face has (even if more than max), or 0 if there's no face i.

levelstat

double levelstat(int level, const char *name)

Gives a number about the built level, such as "faces", "leaves", "portals", "luxels" (patches of baked light), "baked" (1 if it's lit), "drawn" (faces drawn last time), "shiny" (shiny faces) or "probes" (cube maps it's been given).

Returns that number, or 0 for a name there isn't.