Firefly Zero
The official C SDK for making Firefly Zero apps.
Loading...
Searching...
No Matches
firefly.h
Go to the documentation of this file.
1
3
4#pragma once
5
6#include "firefly_bindings.h"
7#include <stdbool.h>
8#include <stdint.h>
9
11#define BOOT __attribute__((export_name("boot")))
12
14#define UPDATE __attribute__((export_name("update")))
15
17#define RENDER __attribute__((export_name("render")))
18
20#define CHEAT __attribute__((export_name("cheat")))
21
23#define BEFORE_EXIT __attribute__((export_name("BEFORE_EXIT")))
24
26#define WIDTH 240
28#define HEIGHT 160
29
30// -- GRAPHICS -- //
31
35struct Point
36{
38 int32_t x;
40 int32_t y;
41};
42typedef struct Point Point;
43
47struct Size
48{
50 int32_t width;
52 int32_t height;
53};
54typedef struct Size Size;
55
60struct Angle
61{
63 float a;
64};
65typedef struct Angle Angle;
66
69{
71 NONE = 0,
73 BLACK = 1,
75 PURPLE = 2,
77 RED = 3,
79 ORANGE = 4,
81 YELLOW = 5,
85 GREEN = 7,
91 BLUE = 10,
95 CYAN = 12,
97 WHITE = 13,
101 GRAY = 15,
104};
105typedef enum Color Color;
106
118typedef struct Style Style;
119
122{
126 int32_t width;
127};
128typedef struct LineStyle LineStyle;
129
131struct RGB
132{
134 int8_t r;
136 int8_t g;
138 int8_t b;
139};
140typedef struct RGB RGB;
141
142// -- FS -- //
143
147struct Buffer
148{
150 size_t size;
152 char *head;
153};
154typedef struct Buffer Buffer;
155typedef struct Buffer File;
156typedef struct Buffer Image;
157typedef struct Buffer Canvas;
158typedef struct Buffer Stash;
159typedef struct Buffer Font;
160
163{
165 Image image;
170};
171typedef struct SubImage SubImage;
172
173// -- INPUT -- //
174
176struct Pad
177{
178 int16_t x;
179 int16_t y;
182};
183typedef struct Pad Pad;
184
207struct DPad8
208{
210 bool left;
212 bool right;
214 bool up;
216 bool down;
217};
218typedef struct DPad8 DPad8;
219
220enum DPad4
221{
222 DPAD4_NONE = 0,
223 DPAD4_LEFT = 1,
224 DPAD4_RIGHT = 2,
225 DPAD4_UP = 3,
226 DPAD4_DOWN = 4,
227};
228typedef enum DPad4 DPad4;
229
232{
235 bool s;
238 bool e;
241 bool w;
244 bool n;
246 bool menu;
247};
248typedef struct Buttons Buttons;
249
250// -- NET -- //
251
253struct Peers
254{
256 uint32_t online;
257};
258typedef struct Peers Peers;
259
261typedef int32_t Peer;
262
264const Peer COMBINED = 0xFF;
265
266// -- STATS -- //
267
268typedef uint32_t Badge;
269typedef uint32_t Board;
270typedef int32_t Score;
271
273{
274 uint16_t done;
275 uint16_t goal;
276};
277typedef struct Progress Progress;
278
279// -- MISC -- //
280
311typedef enum Language Language;
312
314struct Theme
315{
316 uint8_t id;
325};
326typedef struct Theme Theme;
327
330{
333
336
342
345
353
356};
357typedef struct Settings Settings;
358
359// -- AUDIO -- //
360
362#define SAMPLE_RATE 44100
363
366{
368 uint32_t id;
369};
370typedef struct AudioNode AudioNode;
371
373const AudioNode OUT = {.id = 0};
374
378{
379 uint32_t samples;
380};
381typedef struct AudioTime AudioTime;
382
390{
391 AudioTime start_at;
392 AudioTime end_at;
393};
394typedef struct LinearModulator LinearModulator;
395
403{
404 AudioTime time;
405};
406typedef struct HoldModulator HoldModulator;
407
420{
421 AudioTime attack;
422 AudioTime decay;
423 AudioTime sustain;
424 float sustain_level;
425 AudioTime release;
426};
427typedef struct AdsrModulator AdsrModulator;
428
433{
434 float freq;
435};
436typedef struct SineModulator SineModulator;
437
442{
443 AudioTime period;
444};
445typedef struct SquareModulator SquareModulator;
446
451{
452 AudioTime period;
453};
455
456// -- FUNCTIONS -- //
457
458Angle radians(float a);
459Angle degrees(float a);
460AudioTime samples(int32_t s);
461AudioTime seconds(int32_t s);
462AudioTime miliseconds(int32_t s);
464DPad4 pad_to_dpad4(Pad pad);
465
466void clear_screen(Color c);
467void set_color(Color c, RGB v);
468void draw_point(Point p, Color c);
469void draw_line(Point a, Point b, LineStyle s);
470void draw_rect(Point p, Size b, Style s);
471void draw_rounded_rect(Point p, Size b, Size c, Style s);
472void draw_circle(Point p, int32_t d, Style s);
473void draw_ellipse(Point p, Size b, Style s);
474void draw_triangle(Point a, Point b, Point c, Style s);
475void draw_text(char *t, Font f, Point p, Color c);
476void draw_qr(char *t, Point p, Color black, Color white);
477void draw_arc(Point p, int32_t d, Angle start, Angle sweep, Style s);
478void draw_sector(Point p, int32_t d, Angle start, Angle sweep, Style s);
479void draw_image(Image img, Point p);
480void draw_sub_image(SubImage s, Point p);
481void draw_sub_tile(SubImage i, Point p, Size s);
482void draw_nine_slice(SubImage i, Point p, Size s);
483void set_canvas(Canvas c);
484void unset_canvas();
485
486Pad read_pad(Peer peer);
488
489size_t get_file_size(char *path);
490File load_file(char *path, Buffer buf);
491void dump_file(char *path, File f);
492void remove_file(char *path);
493
494Peer get_me();
496bool is_online(Peers peers, Peer peer);
497void save_stash(Peer p, Stash s);
498Stash load_stash(Peer p, Buffer s);
499
500void log_debug(char *msg);
501void log_error(char *msg);
502void set_seed(uintptr_t seed);
503uintptr_t get_random();
504Buffer get_name(Peer p, Buffer buf);
506void restart();
507void quit();
508
509Progress add_progress(Peer p, Badge b, int16_t v);
510Progress get_progress(Peer p, Badge b);
511Score add_score(Peer p, Board b, Score v);
512Score get_score(Peer p, Board b);
513
514AudioNode add_sine(AudioNode parent, float freq, float phase);
515AudioNode add_square(AudioNode parent, float freq, float phase);
516AudioNode add_sawtooth(AudioNode parent, float freq, float phase);
517AudioNode add_triangle(AudioNode parent, float freq, float phase);
518AudioNode add_noise(AudioNode parent, int32_t seed);
521AudioNode add_file(AudioNode parent, char *path);
524AudioNode add_gain(AudioNode parent, float lvl);
527AudioNode add_pan(AudioNode parent, float lvl);
531AudioNode add_low_pass(AudioNode parent, float freq, float q);
532AudioNode add_high_pass(AudioNode parent, float freq, float q);
536AudioNode add_clip(AudioNode parent, float low, float high);
537
538void mod_linear(AudioNode node, float low, float high, LinearModulator mod);
539void mod_hold(AudioNode node, float low, float high, HoldModulator mod);
540void mod_adsr(AudioNode node, float low, float high, AdsrModulator mod);
541void mod_sine(AudioNode node, float low, float high, SineModulator mod);
542void mod_square(AudioNode node, float low, float high, SquareModulator mod);
543void mod_sawtooth(AudioNode node, float low, float high, SawtoothModulator mod);
544
545void audio_reset(AudioNode node);
546void audio_reset_all(AudioNode node);
547void audio_clear(AudioNode node);
AudioNode add_pan(AudioNode parent, float lvl)
Add pan AudioNode as a child node for the given node.
Definition firefly.c:596
AudioTime seconds(int32_t s)
Time in seconds.
Definition firefly.c:32
void set_color(Color c, RGB v)
Set a color value in the palette.
Definition firefly.c:99
DPad4 pad_to_dpad4(Pad pad)
Convert Pad to DPad4.
Definition firefly.c:57
AudioTime miliseconds(int32_t s)
Time in miliseconds.
Definition firefly.c:39
Settings get_settings(Peer p)
Get system settings.
Definition firefly.c:410
void mod_hold(AudioNode node, float low, float high, HoldModulator mod)
Modulate an audio node's main parameter using HoldModulator.
Definition firefly.c:706
AudioNode add_pause(AudioNode parent)
Add pause AudioNode as a child node for the given node.
Definition firefly.c:612
void mod_adsr(AudioNode node, float low, float high, AdsrModulator mod)
Modulate an audio node's main parameter using AdsrModulator.
Definition firefly.c:712
void remove_file(char *path)
Delete a file created using dump_file().
Definition firefly.c:307
void unset_canvas()
Make all subsequent drawing operations target the screen instead of a canvas.
Definition firefly.c:228
void draw_circle(Point p, int32_t d, Style s)
Draw a circle with the given diameter.
Definition firefly.c:129
void audio_reset_all(AudioNode node)
Reset the state of the given AudioNode and all its child nodes.
Definition firefly.c:682
AudioNode add_all_for_one(AudioNode parent)
Add allforone AudioNode as a child node for the given node.
Definition firefly.c:564
void restart()
Ask the runtime to restart the app after the current update iteration.
Definition firefly.c:435
void set_canvas(Canvas c)
Set the target image for all subsequent drawing operations.
Definition firefly.c:221
void quit()
Ask the runtime to exit the app after the current update iteration.
Definition firefly.c:441
void draw_sub_tile(SubImage i, Point p, Size s)
Tile the given screen area with the provided sub-image.
Definition firefly.c:189
void mod_square(AudioNode node, float low, float high, SquareModulator mod)
Modulate an audio node's main parameter using SquareModulator.
Definition firefly.c:728
Peers get_peers()
Get a mapping of peers currently online.
Definition firefly.c:322
void log_error(char *msg)
Write an error message.
Definition firefly.c:375
Language
User interface language.
Definition firefly.h:283
@ Turkish
tr 🇹🇷 🕌
Definition firefly.h:305
@ Swedish
sv 🇸🇪 ❄️
Definition firefly.h:303
@ Romanian
ro 🇷🇴 🧛
Definition firefly.h:297
@ Russian
ru 🇷🇺 🪆
Definition firefly.h:299
@ German
de 🇩🇪 🥨
Definition firefly.h:291
@ Italian
it 🇮🇹 🍕
Definition firefly.h:293
@ Spanish
es 🇪🇸 🐂
Definition firefly.h:301
@ English
en 🇬🇧 💂
Definition firefly.h:285
@ Polish
pl 🇵🇱 🥟
Definition firefly.h:295
@ Ukrainian
uk 🇺🇦 ✊
Definition firefly.h:307
@ TokiPona
tp 🇨🇦 🙂
Definition firefly.h:309
@ Dutch
nl 🇳🇱 🧀
Definition firefly.h:287
@ French
fr 🇫🇷 🥐
Definition firefly.h:289
AudioNode add_track_position(AudioNode parent)
Add trackposition AudioNode as a child node for the given node.
Definition firefly.c:620
void draw_point(Point p, Color c)
Set a single point (1 pixel is scaling is 1) on the frame.
Definition firefly.c:105
size_t get_file_size(char *path)
Get size (in bytes) of the given file.
Definition firefly.c:273
AudioNode add_mute(AudioNode parent)
Add mute AudioNode as a child node for the given node.
Definition firefly.c:604
void mod_sine(AudioNode node, float low, float high, SineModulator mod)
Modulate an audio node's main parameter using SineModulator.
Definition firefly.c:722
AudioNode add_sine(AudioNode parent, float freq, float phase)
Add sine AudioNode as a child node for the given node.
Definition firefly.c:491
Progress add_progress(Peer p, Badge b, int16_t v)
Add the given value to the progress for the badge.
Definition firefly.c:455
void log_debug(char *msg)
Write a debug message.
Definition firefly.c:368
AudioNode add_triangle(AudioNode parent, float freq, float phase)
Add triangle AudioNode as a child node for the given node.
Definition firefly.c:515
AudioNode add_zero(AudioNode parent)
Add zero AudioNode as a child node for the given node.
Definition firefly.c:539
void draw_sector(Point p, int32_t d, Angle start, Angle sweep, Style s)
Draw a sector.
Definition firefly.c:153
bool is_online(Peers peers, Peer peer)
Check if the given Peer is online.
Definition firefly.c:332
AudioNode add_empty(AudioNode parent)
Add empty AudioNode as a child node for the given node.
Definition firefly.c:531
AudioNode add_take_left(AudioNode parent)
Add takeleft AudioNode as a child node for the given node.
Definition firefly.c:644
void set_seed(uintptr_t seed)
Set the random seed. Useful for testing.
Definition firefly.c:382
void draw_arc(Point p, int32_t d, Angle start, Angle sweep, Style s)
Draw an arc.
Definition firefly.c:147
AudioNode add_noise(AudioNode parent, int32_t seed)
Add noise AudioNode as a child node for the given node.
Definition firefly.c:523
void draw_line(Point a, Point b, LineStyle s)
Draw a straight line from point a to point b.
Definition firefly.c:111
Buffer get_name(Peer p, Buffer buf)
Write device name into the given Buffer.
Definition firefly.c:395
AudioNode add_gain(AudioNode parent, float lvl)
Add gain AudioNode as a child node for the given node.
Definition firefly.c:572
const Peer COMBINED
A peer ID representing all peers at once.
Definition firefly.h:264
Pad read_pad(Peer peer)
Read touchpad state: if it's pressed and where.
Definition firefly.c:236
AudioNode add_clip(AudioNode parent, float low, float high)
Add clip AudioNode as a child node for the given node.
Definition firefly.c:668
void audio_clear(AudioNode node)
Remove all child nodes from the given AudioNode.
Definition firefly.c:688
void draw_triangle(Point a, Point b, Point c, Style s)
Draw a triangle.
Definition firefly.c:141
Buttons read_buttons(Peer peer)
Get pressed buttons.
Definition firefly.c:256
void audio_reset(AudioNode node)
Reset the state of the given AudioNode.
Definition firefly.c:676
int32_t Peer
An ID of a connected device in the multiplayer.
Definition firefly.h:261
void dump_file(char *path, File f)
Write the given content into the given path.
Definition firefly.c:299
void draw_ellipse(Point p, Size b, Style s)
Draw an ellipse (oval).
Definition firefly.c:135
AudioNode add_high_pass(AudioNode parent, float freq, float q)
Add highpass AudioNode as a child node for the given node.
Definition firefly.c:636
AudioNode add_swap(AudioNode parent)
Add swap AudioNode as a child node for the given node.
Definition firefly.c:660
AudioNode add_file(AudioNode parent, char *path)
Add file AudioNode as a child node for the given node.
Definition firefly.c:547
void draw_text(char *t, Font f, Point p, Color c)
Render a text message using the given font.
Definition firefly.c:159
void draw_image(Image img, Point p)
Draw an image.
Definition firefly.c:173
Color
A pointer to a color in the color palette.
Definition firefly.h:69
@ PURPLE
Purple color: #5D275D.
Definition firefly.h:75
@ WHITE
White color: F4F4F4.
Definition firefly.h:97
@ BLUE
Blue color: #3B5DC9.
Definition firefly.h:91
@ GRAY
Gray color: #566C86.
Definition firefly.h:101
@ DARK_GRAY
Dark gray color: #333C57.
Definition firefly.h:103
@ LIGHT_BLUE
Light blue color: #41A6F6.
Definition firefly.h:93
@ LIGHT_GRAY
Light gray color: #94B0C2.
Definition firefly.h:99
@ DARK_GREEN
Dark green color: #257179.
Definition firefly.h:87
@ DARK_BLUE
Dark blue color: #29366F.
Definition firefly.h:89
@ GREEN
Green color: #38B764.
Definition firefly.h:85
@ CYAN
Cyan color: #73EFF7.
Definition firefly.h:95
@ NONE
No color (100% transparency).
Definition firefly.h:71
@ ORANGE
Orange color: EF7D57.
Definition firefly.h:79
@ YELLOW
Yellow color: FFCD75.
Definition firefly.h:81
@ BLACK
Black color: #1A1C2C.
Definition firefly.h:73
@ RED
Red color: B13E53.
Definition firefly.h:77
@ LIGHT_GREEN
Light green color: A7F070.
Definition firefly.h:83
AudioNode add_loop(AudioNode parent)
Add loop AudioNode as a child node for the given node.
Definition firefly.c:580
Angle degrees(float a)
An angle in degrees where 360.0 is the full circle.
Definition firefly.c:18
uintptr_t get_random()
Get a random integer.
Definition firefly.c:388
Progress get_progress(Peer p, Badge b)
Get the progress of earning the badge.
Definition firefly.c:465
AudioNode add_mix(AudioNode parent)
Add mix AudioNode as a child node for the given node.
Definition firefly.c:556
Stash load_stash(Peer p, Buffer s)
Load Stash saved earlier (in this or previous run) by save_stash.
Definition firefly.c:357
void mod_sawtooth(AudioNode node, float low, float high, SawtoothModulator mod)
Modulate an audio node's main parameter using SawtoothModulator.
Definition firefly.c:734
void draw_rect(Point p, Size b, Style s)
Draw a rectangle filling the given bounding box.
Definition firefly.c:117
AudioNode add_low_pass(AudioNode parent, float freq, float q)
Add lowpass AudioNode as a child node for the given node.
Definition firefly.c:628
void draw_rounded_rect(Point p, Size b, Size c, Style s)
Draw a rectangle with rounded corners.
Definition firefly.c:123
File load_file(char *path, Buffer buf)
Read file from the given path into the given buffer.
Definition firefly.c:282
Peer get_me()
Get the Peer corresponding to the current device.
Definition firefly.c:316
void draw_nine_slice(SubImage i, Point p, Size s)
Fill the given area with the given 9-slice.
Definition firefly.c:210
void mod_linear(AudioNode node, float low, float high, LinearModulator mod)
Modulate an audio node's main parameter using LinearModulator.
Definition firefly.c:700
Angle radians(float a)
An angle in radians where τ(2π) is the full circle.
Definition firefly.c:11
DPad8 pad_to_dpad8(Pad pad)
Convert Pad to DPad8.
Definition firefly.c:46
const AudioNode OUT
The root audio node. Its child nodes are mixed and played on the device output.
Definition firefly.h:373
void draw_sub_image(SubImage s, Point p)
Draw an image subregion.
Definition firefly.c:179
AudioNode add_take_right(AudioNode parent)
Add takeright AudioNode as a child node for the given node.
Definition firefly.c:652
Score get_score(Peer p, Board b)
Get the personal best of the player.
Definition firefly.c:483
void save_stash(Peer p, Stash s)
Save the given Stash.
Definition firefly.c:344
Score add_score(Peer p, Board b, Score v)
Add the given score to the board.
Definition firefly.c:477
AudioNode add_concat(AudioNode parent)
Add concat AudioNode as a child node for the given node.
Definition firefly.c:588
void clear_screen(Color c)
Fill the whole frame with the given color.
Definition firefly.c:93
AudioNode add_sawtooth(AudioNode parent, float freq, float phase)
Add sawtooth AudioNode as a child node for the given node.
Definition firefly.c:507
AudioTime samples(int32_t s)
Time in the number of samples.
Definition firefly.c:25
void draw_qr(char *t, Point p, Color black, Color white)
Render a QR code for the given text.
Definition firefly.c:166
AudioNode add_square(AudioNode parent, float freq, float phase)
Add square AudioNode as a child node for the given node.
Definition firefly.c:499
ADSR envelope.
Definition firefly.h:420
An angle between two vectors.
Definition firefly.h:61
A node in the audio tree.
Definition firefly.h:366
A time or duration used by audio modulators.
Definition firefly.h:378
A dynamically sized array.
Definition firefly.h:148
size_t size
The slice size, in bytes.
Definition firefly.h:150
char * head
The pointer to the first element of the underlying array.
Definition firefly.h:152
State of the buttons. True is pressed, false is released.
Definition firefly.h:232
bool s
South. The bottom button, like A on the X-Box controller.
Definition firefly.h:235
bool n
North. The top button, like Y on the X-Box controller.
Definition firefly.h:244
bool menu
The menu button, almost always handled by the runtime.
Definition firefly.h:246
bool w
West. The left button, like X on the X-Box controller.
Definition firefly.h:241
bool e
East. The right button, like B on the X-Box controller.
Definition firefly.h:238
8-directional DPad-like representation of the Pad.
Definition firefly.h:208
bool left
If the left segment of the Pad is pressed.
Definition firefly.h:210
bool up
If the upper segment of the Pad is pressed.
Definition firefly.h:214
bool right
If the right segment of the Pad is pressed.
Definition firefly.h:212
bool down
If the lower segment of the Pad is pressed.
Definition firefly.h:216
Hold envelope.
Definition firefly.h:403
The same as Style but without a fill color (only stroke color and width).
Definition firefly.h:122
Color color
The line color.
Definition firefly.h:124
int32_t width
The line width, in pixels.
Definition firefly.h:126
Linear (ramp up or down) envelope.
Definition firefly.h:390
Get if the touchpad is pressed, and if so, where.
Definition firefly.h:177
bool touched
If the touchpad is touched.
Definition firefly.h:181
The bitmap of peers currently online.
Definition firefly.h:254
A point on the screen.
Definition firefly.h:36
int32_t y
The Y coordinate, the number of pixels from the screen top.
Definition firefly.h:40
int32_t x
The X coordinate, the number of pixels from the screen left.
Definition firefly.h:38
Definition firefly.h:273
The RGB value of a color in the palette.
Definition firefly.h:132
int8_t g
The green component.
Definition firefly.h:136
int8_t r
The red component.
Definition firefly.h:134
int8_t b
The blue component.
Definition firefly.h:138
Sawtooth wave low-frequency oscillator.
Definition firefly.h:451
System settings.
Definition firefly.h:330
Theme theme
The preferred color scheme of the player.
Definition firefly.h:332
bool reduce_flashing
The player has photosensitivity. The app should avoid any rapid flashes.
Definition firefly.h:344
bool easter_eggs
If true, the player wants to see easter eggs, holiday effects, and weird jokes.
Definition firefly.h:355
bool contrast
The player wants increased contrast for colors.
Definition firefly.h:352
bool rotate_screen
If true, the screen is rotated 180 degrees.
Definition firefly.h:341
Language language
The configured interface language.
Definition firefly.h:335
Sine wave low-frequency oscillator.
Definition firefly.h:433
Size of a bounding box for a shape.
Definition firefly.h:48
int32_t width
The width of the bounding box, in pixels.
Definition firefly.h:50
int32_t height
The height of the bounding box, in pixels.
Definition firefly.h:52
Square wave low-frequency oscillator.
Definition firefly.h:442
Style of a shape.
Definition firefly.h:109
Color stroke_color
The color to use for the shape stroke.
Definition firefly.h:113
int32_t stroke_width
The width of the shape stroke.
Definition firefly.h:116
Color fill_color
The color to use to fill the shape.
Definition firefly.h:111
A subregion of an Image.
Definition firefly.h:163
Point point
The coordinates of the upper-left corner of the subregion.
Definition firefly.h:167
Size size
The width and height of the subregion.
Definition firefly.h:169
Image image
The original image.
Definition firefly.h:165
User preferred color scheme.
Definition firefly.h:315
Color secondary
The color of disable options, muted text, etc.
Definition firefly.h:320
Color bg
The background color, the most contrast color to primary.
Definition firefly.h:324
Color accent
The color of important elements, active options, etc.
Definition firefly.h:322
Color primary
The main color of text and boxes.
Definition firefly.h:318