Graphics

๐Ÿ”ฌ Pixels#

The screen size 240x160 pixels, just like GameBoy Advance. Pixels are quite big, so it’s safe to draw one pixel wide lines and letters, they will be visible.

Coordinates: x=0 is left, x=240 is right, y=0 is top, and y=160 is bottom.

๐Ÿ–ผ Frames#

All those moments will be lost in time.

Each update of the image on the screen is called “frame”. Before each frame is rendered, the render callback function is called. This is where you should call all drawing functions you need.

Don’t draw from the update callback: while update is called ~60 times per second, render might be called less often if there are not enough resources. So, if you draw from update, you’ll waste resources on updating a frame that will be never rendered on the screen.

The frame that you had at the previous update is not cleared. So, if only a small region on the screen needs to be changed, you can redraw only that region and leave the rest intact. If it’s not what you want and you’d prefer to redraw the whole frame each time, call clear_screen, and it will fill the whole frame with the color you specified.

clear_screen(Color::White);
firefly.ClearScreen(firefly.ColorWhite)
ff.clearScreen(.white);
ff.clearScreen(ff.Color.White);
clear_screen(WHITE)
firefly.clear_screen(firefly.Color.White)
@firefly.clear_screen(White)
firefly.clear_screen(firefly.WHITE)

๐ŸŽจ Colors#

The enemy of art is the absence of limitations.

Each frame is stored in memory as one of 16 shades of gray. When the time comes to draw the frame on the screen, each of these shades is mapped to one of 16 colors in the color palette.

The default color palette is Sweetie 16:

If you use Dark Reader extension in your browser, make sure to turn it off when viewing the palette below. Otherwise, you’ll see incorrect colors!
  1. #1A1C2C: Black
  2. #5D275D: Purple
  3. #B13E53: Red
  4. #EF7D57: Orange
  5. #FFCD75: Yellow
  6. #A7F070: LightGreen
  7. #38B764: Green
  8. #257179: DarkGreen
  9. #29366F: DarkBlue
  10. #3B5DC9: Blue
  11. #41A6F6: LightBlue
  12. #73EFF7: Cyan
  13. #F4F4F4: White
  14. #94B0C2: LightGray
  15. #566C86: Gray
  16. #333C57: DarkGray

You can modify colors in the palette by calling the set_color function:

use firefly_rust::*;
set_color(
    Color::Red,
    RGB{r: 0xff, g: 0x00, b: 0x00},
);
firefly.SetColor(
    firefly.ColorRed,
    firefly.RGB{R: 0xff, G: 0x00, B: 0x00},
);
const ff = @import("firefly");
ff.setColor(
    .red,
    .{ .r = 0xff, .g = 0x00, .b = 0x00 },
);
import * as ff from "firefly-as/assembly";
ff.setColor(ff.Color.Red, ff.RGB.new(255, 0, 0));
#include "./vendor/firefly/firefly.c"
RGB color = {.r = 0xff, .g = 0x00, .b = 0x00};
set_color(RED, color);
color := firefly.rgb(0xff, 0x00, 0x00)
firefly.set_color(firefly.Color.Red, color)
@firefly.set_color(Color::Red, RGB::new(0xff, 0x00, 0x00))
firefly.set_color(firefly.RED, {r=0xff, g=0x00, b=0x00})

See palettes.fireflyzero.com for the list of popular color palettes and code snippets for setting them up.

๐Ÿ”ณ Shapes#

It is the sculptor’s power, so often alluded to, of finding the perfect form and features of a goddess, in the shapeless block of marble.

The runtime provides a bunch of functions that you can call to draw primitive shapes and lines. All these functions follow the same rules:

  1. The first argument is usually the coordinate of the upper-left corner of the bounding box in which the shape should be drawn (Point). Coordinates might point outside of the screen (including being negative). In that case, only the shape parts that are within the screen boundaries will be drawn.
  2. The second argument is usually the size of the bounding box (Size). Both width and height must be positive.
  3. The last argument is typically the style of the shape (Style): fill color, stroke color, and stroke width. The exception are draw_point, which accepts only one color, and draw_line, which doesn’t need the fill color.

For a reference, here is the list of available functions for drawing shapes:

  1. draw_point(๐Ÿ‘‰ Point, ๐Ÿ“ฆ Size, ๐Ÿ’„ Color)
  2. draw_line(๐Ÿ‘‰ Point, ๐Ÿ“ฆ Size, ๐Ÿ’„ LineStyle)
  3. draw_rect(๐Ÿ‘‰ Point, ๐Ÿ“ฆ Size, ๐Ÿ’„ Style)
  4. draw_rounded_rect(๐Ÿ‘‰ Point, ๐Ÿ“ฆ Size, ๐Ÿ“ฆ Size, ๐Ÿ’„ Style)
  5. draw_triangle(๐Ÿ‘‰ Point, ๐Ÿ‘‰ Point, ๐Ÿ‘‰ Point, ๐Ÿ’„ Style)
  6. draw_circle(๐Ÿ‘‰ Point, โญ• Diameter, ๐Ÿ’„ Style)
  7. draw_ellipse(๐Ÿ‘‰ Point, ๐Ÿ“ฆ Size, ๐Ÿ’„ Style)
  8. draw_arc(๐Ÿ‘‰ Point, โญ• Diameter, ๐Ÿ“ Angle, ๐Ÿ“ Angle, ๐Ÿ’„ Style)
  9. draw_sector(๐Ÿ‘‰ Point, โญ• Diameter, ๐Ÿ“ Angle, ๐Ÿ“ Angle, ๐Ÿ’„ Style)
use firefly_rust::*;
draw_triangle(
    Point::new(60, 10),
    Point::new(40, 40),
    Point::new(80, 40),
    Style {
        fill_color: Color::LightGray,
        stroke_color: Color::DarkBlue,
        stroke_width: 1,
    },
);
firefly.DrawTriangle(
    firefly.P(60, 10),
    firefly.P(40, 40),
    firefly.P(80, 40),
    firefly.Style{
        FillColor:   firefly.ColorDarkBlue,
        StrokeColor: firefly.ColorBlue,
        StrokeWidth: 1,
    },
)
const ff = @import("firefly");
ff.drawTriangle(
    .{ .x = 60, .y = 10 },
    .{ .x = 40, .y = 40 },
    .{ .x = 80, .y = 40 },
    .{
        .fill_color = .light_gray,
        .stroke_color = .dark_blue,
        .stroke_width = 1,
    },
);
ff.drawTriangle(
    ff.Point.new(60, 10),
    ff.Point.new(40, 40),
    ff.Point.new(80, 40),
    ff.Style.new(ff.Color.LightGray, ff.Color.DarkBlue, 1)
);
Point p1 = {60, 10};
Point p2 = {40, 40};
Point p3 = {80, 40};
Style s = {
    .fill_color = LIGHT_GRAY,
    .stroke_color = DARK_BLUE,
    .stroke_width = 1};
draw_triangle(p1, p2, p3, s);
draw_triangle(
    {60, 10}, {40, 40}, {80, 40},
    {
        .fill_color = LIGHT_GRAY,
        .stroke_color = DARK_BLUE,
        .stroke_width = 1,
    });
firefly.draw_triangle(
    firefly.p(60, 10),
    firefly.p(40, 40),
    firefly.p(80, 40),
    firefly.Style {
        fill_color = firefly.Color.LightGray,
        stroke_color = firefly.Color.DarkBlue,
        stroke_width = 1,
    },
)
using @firefly {type Point, type Style};
@firefly.draw_triangle(
    Point::new(60, 10),
    Point::new(40, 40),
    Point::new(80, 40),
    Style::{
        fill_color: LightGray,
        stroke_color: DarkBlue,
        stroke_width: 1,
    },
);
firefly.draw_triangle(
    {x=60, y=10},
    {x=40, y=40},
    {x=80, y=40},
    {
        fill_color=firefly.LIGHT_GRAY,
        stroke_color=firefly.DARK_BLUE,
        stroke_width=1,
    },
)

๐Ÿ“ธ Images#

A picture is worth a thousand words.

Draw the image you want using the default (Sweetie 16) color palette. You can also use a transparent background but then the maximum number of colors is limited to 15. Save the image as a PNG file.

Looking for a pixel art editor? Try pixel.fireflyzero.com or check out Additional resources at the end of this page.

Link the saved file in firefly.toml:

[files]
img = { path = "image.png" }

Where img is the name of the image in the ROM.

Next, load the image from ROM into the memory:

let file = firefly_rust::load_file_buf("img");
let img = file.as_image();
file := firefly.LoadFile("img", nil);
img := file.Image()
const file = ff.loadFile("img");
const img = file.toImage();
const file = ff.loadFile("img");
const img = file.toImage();
// We know the image size in advance.
// You can see it when running `ff build`.
// You could also use `get_file_size` to detect the file size
// in runtime but that requires using an allocator.
const size_t fileSize = 97;
char head[fileSize];
Buffer buf = {.head = head, .size = fileSize};
image = load_file("img", buf);
file := firefly.load_file_buf("img");
img := firefly.image(file)
let file = @firefly.load_file("img");
file = firefly.load_file("img");

Lastly, call draw_image with the image you loaded earlier:

draw_image(&image, Point::new(60, 10));
firefly.DrawImage(image, firefly.P(60, 10))
ff.drawImage(image, .{ .x = 60, .y = 10 });
ff.drawImage(image, ff.Point.new(60, 10));
Point p = {60, 10};
draw_image(image, p);
draw_image(image, {60, 10});
firefly.draw_image(image, firefly.p(60, 10))
@firefly.draw_image(image, Point::new(60, 10));
firefly.draw_image(image, {x=60, y=10})

You can also select an image region using Image.sub and then draw it using draw_sub_image which is particularly useful for tiling:

let sub = image.sub(Point::new(10, 10), Size::new(10, 10));
draw_sub_image(sub, Point::new(120, 100));
sub := image.Sub(firefly.P(10, 10), firefly.S(10, 10))
firefly.DrawSubImage(sub, firefly.P(120, 100))
const sub = image.sub(.{10, 10}, .{10, 10});
ff.drawSubImage(sub, .{120, 100});
const sub = image.sub(Point.new(10, 10), Size.new(10, 10));
ff.drawSubImage(sub, Point.new(120, 100));
Point p1 = {10, 10};
Size s1 = {10, 10};
SubImage sub = image.sub(p1, s1);
Point p2 = {120, 100};
draw_sub_image(sub, p2);
sub := firefly.sub(image, firefly.p(10, 10), firefly.s(10, 10))
firefly.draw_sub_image(sub, firefly.p(120, 100))
let sub = image.sub(Point::new(10, 10), Size::new(10, 10));
@firefly.draw_sub_image(sub, Point::new(120, 100));
-- TODO

If the image uses a custom color palette, you can add the palette into the palettes table in firefly.toml and reference it in the file:

# https://lospec.com/palette-list/pico-8
[palettes.pico-8]
1 = 0x000000
2 = 0x1D2B53
3 = 0x7E2553
# ...

[files]
img = { path = "image.png", palette = "pico-8" }

Just like for the runtime palette definitions, firefly.toml snippets for some color palettes are available at palettes.fireflyzero.com.

๐Ÿ”ค Text#

First, go to fonts.fireflyzero.com and download the font that you’d like to use. Then add it into firefly.toml as you add images:

[files]
font = { path = "eg_6x9.fff" }

Now you can load the font from ROM as you load images and call draw_text with the text you want to render and the font.

draw_text("Welcome, adventurer!", &font, Point::new(60, 10));
firefly.DrawText("Welcome, adventurer!", font, firefly.P(60, 10))
ff.drawText("Welcome, adventurer!", font, .{ .x = 60, .y = 10 });
ff.drawText("Welcome, adventurer!", font, ff.Point.new(60, 10));
Point p = {60, 10};
draw_text("Welcome, adventurer!", font, p);
draw_text("Welcome, adventurer!", font, {60, 10});
firefly.draw_text("Welcome, adventurer!", font, firefly.p(60, 10))
@firefly.draw_text("Welcome, adventurer!", font, Point::new(60, 10));
firefly.draw_text("Welcome, adventurer!", font, {x=60, y=10})
The text does not automatically wrap. If you need word wrapping, you should add new lines into the text yourself.

If you want to include in your app a link, you can use draw_qr to render a QR code.

๐Ÿ–Œ Canvas#

To create a new image, you need to pass the image size and a color. The color will be used to represent transparency. For example, if the transparency color is Red, then the image cannot contain the red color. So when you draw a red circle on the image, it will be represented as a transparent “hole” in the image instead.

let image = ImageBuf::new(Size::new(120, 120), Color::Black);
image := firefly.NewImage(firefly.Size{W: 120, H: 120}, firefly.ColorBlack)
// TODO: Canvas not fully supported by Zig yet.
const image = ff.Canvas.new(ff.Size.new(120, 120), ff.Color.Black);
// TODO: Canvas not supported by C yet.
image := firefly.empty_image(firefly.s(120, 120), firefly.Color.Black)
let image = Image::new(Size::new(120, 120), Black)
-- TODO: Canvas not supported by Lua yet

Any image (both loaded from a file or created from scratch) can be set as a “canvas”. When a canvas is set (using set_canvas), almost all subsequent drawing operations will target that image instead of the screen. To make the functions target the screen again, call unset_canvas.

Canvas is useful for image manipulation and for drawing from scratch some complex images that should be persisted between multiple frames. For example, it can store dirt on a character, bullet marks on walls, debris on the ground, randomly generated terrain, etc.

set_canvas(&image);
// fill canvas with white color
clear_screen(Color::White);
unset_canvas();
firefly.SetCanvas(image)
// fill canvas with white color
firefly.ClearScreen(firefly.ColorWhite)
firefly.UnsetCanvas()
ff.setCanvas(image);
ff.clearScreen(.white);
ff.unsetCanvas();
ff.setCanvas(image);
// fill canvas with white color
ff.clearScreen(ff.Color.White);
ff.unsetCanvas();
// TODO: Canvas not supported by C yet.
firefly.set_canvas(image)
// fill canvas with white color
firefly.clear_screen(firefly.Color.White)
firefly.unset_canvas()
@firefly.set_canvas(image)
// fill canvas with white color
@firefly.clear_screen(White)
@firefly.unset_canvas()
-- TODO: Canvas not supported by Lua yet

Some of the functions most commonly used with canvas:

  • flip_x: flip the image horizontally.
  • flip_y: flip the image vertically.
  • flip_x + flip_y: rotate the image 180 degrees.
  • clear_screen: fill the image with a color.
  • draw_point (and other draw_ functions): draw on the image.
Currently, draw_text, draw_image, and draw_sub_image don’t support canvas but it might change in the future. Don’t call these functions when you have a canvas set!

๐ŸชŸ Tiling#

๐Ÿงฑ Creating levels#

Most of the retro games use “sprites”: A collection of small square images (usually 8x8 or 16x16 pixels) which are used to tile the screen as a square grid to form the game level.

Tiled level example

tiling example

Sprites are typically all packed on a single image called “spritesheet” (or “atlas”).

Spritesheet example

atlas example

To create such levels, you can download an existing spritesheet (for example, from itch.io), pick the sprites that you want to use in any image editor (I use Pinta), and arrange them using a level editor (like Tiled or LDtk). You can then load the exported level directly as JSON but I prefer to convert it first to bytes (where eah byte is the sprite ID) using a simple Python script which significantly reduces the game size.

Python script
import xml.etree.ElementTree as xml
from pathlib import Path

assets_dir = Path(__file__).parent / 'assets'
tree = xml.parse(assets_dir / 'level.tmx')
root = tree.getroot()
data = root.findall('layer')[0].findall('data')[0]
raw = [int(byte.strip()) for byte in data.text.split(',')]
(assets_dir / 'level.bin').write_bytes(bytes(raw))

In Firefly, you can load a spritesheet as an Image and then select and render individual sprites as SubImage as covered in the Images section.

Advanced: single-sprite tiling

When a screen area (or maybe the whole screen?) needs to be tiled with the same sprite, draw_sub_tile can be used. This is not a very common scenario (make sure to add little variations to your tiles to make it look less repetitive!) but in case you ever need it, it’s there for you.

9๏ธโƒฃ Nine-slice#

The 9-slice scaling in graphics is a cool technique for rendering pretty boxes. In it, you take a rectangle image and split it into 9 segments:

9-slice example input

Then to fill an area with this image you place the corners unchanged and repeat all segments in between:

9-slice example output

To draw a 9-slice in Firefly, you load the box as an Image, select the middle segment (the green 5 in the example above) as a SubImage, and pass it into draw_nine_slice:

let sub = image.sub(Point::new(10, 10), Size::new(10, 10));
draw_nine_slice(sub, Point::new(120, 100), Size::new(50, 40));
sub := image.Sub(firefly.P(10, 10), firefly.S(10, 10))
firefly.DrawNineSlice(sub, firefly.P(120, 100), firefly.S(50, 40))
const sub = image.sub(.{10, 10}, .{10, 10});
ff.drawNineSlice(sub, .{120, 100}, .{50, 40});
const sub = image.sub(Point.new(10, 10), Size.new(10, 10));
ff.drawNineSlice(sub, Point.new(120, 100), Size.new(50, 40));
Point p1 = {10, 10};
Size s1 = {10, 10};
SubImage sub = image.sub(p1, s1);
Point p2 = {120, 100};
Size s2 = {50, 40};
draw_nine_slice(sub, p2, s2);
sub := firefly.sub(image, firefly.p(10, 10), firefly.s(10, 10))
firefly.draw_nine_slice(sub, firefly.p(120, 100), firefly.s(50, 40))
let sub = image.sub(Point::new(10, 10), Size::new(10, 10));
@firefly.draw_nine_slice(sub, Point::new(120, 100), Size::new(50, 40));
-- TODO

The 9-slice scaling is useful for drawing dialog boxes and bubbles and boxes for other UI elements (such as inventory and other in-game menus). It can also be used to speed up rendering of a room: the middle tile of the 9-slice will be the floor tile and the rest will be wall tiles.

๐Ÿ”— Additional resources#

If you’re looking for 2D pixel sprites for your game, check out:

Some assets might have too many colors or a color palette that you don’t like. Use Palette Quantizer to map any assets to any color palette.

Some of our official resources designed specifically for Firefly Zero:

Our Pixel Editor is designed to be as simple as possible. If you’re looking for a more powerful editor, check out the following:

Lastly, a few more helpful tools: