Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

5.4 KB
Raw Blame
/* NumPlay launcher: shared definitions. */
#ifndef NP_H
#define NP_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

typedef uint16_t color_t;
#define RGB(r, g, b) ((color_t)((((r) & 0xF8) << 8) | (((g) & 0xFC) << 3) | ((b) >> 3)))
#define HEX(v) RGB(((v) >> 16) & 0xFF, ((v) >> 8) & 0xFF, (v) & 0xFF)
#define SCREEN_W 320
#define SCREEN_H 240
#define NP_MIN(a, b) ((a) < (b) ? (a) : (b))
#define NP_MAX(a, b) ((a) > (b) ? (a) : (b))
#define NP_CLAMP(v, lo, hi) ((v) < (lo) ? (lo) : (v) > (hi) ? (hi) : (v))

/* Keys, independent of the calculator's numbering. The number pad doubles
 * as arrows (8 4 6 2) and 5 as OK. */
enum {
  K_LEFT = 1, K_RIGHT = 2, K_UP = 4, K_DOWN = 8, K_OK = 16, K_BACK = 32, K_HOME = 64, K_BACKSPACE = 128
};

/* ---- platform (platform.c) */
uint32_t np_keys(void);           /* keys held right now */
uint32_t np_millis(void);
void np_sleep(unsigned ms);
void np_vblank(void);
void np_push(int x, int y, int w, int h, const color_t *px);
void np_fill_screen(int x, int y, int w, int h, color_t c);
/* Waits until every key is released (after a game, or before quitting). */
void np_wait_release(void);

/* ---- one arena for everything big: the running game's RAM, or the
 * launcher's buffers while no game runs */
extern uint8_t np_arena[];
extern const uint32_t np_arena_size;
uint32_t np_arena_room(void);     /* the arena and the RAM after it, up to the end of the app's */
void *np_alloc(uint32_t size);    /* bump allocator in the arena */
void np_alloc_reset(void);

/* ---- games (games.c, table generated by tools/gen_games.py) */
typedef struct {
  uint16_t w, h, ncolors;
  uint32_t zsize;
  const color_t *palette;
  const uint8_t *z;             /* raw DEFLATE of w*h palette indices */
} np_shot_t;

typedef struct {
  const char *id, *title, *tagline, *credit;
  uint32_t top, bottom, accent;          /* 0xRRGGBB theme colours */
  const uint32_t *begin, *end;           /* the game's block of flash */
  int (*main)(void);
  uint8_t *data;                         /* the game's RAM inside the arena */
  const uint8_t *data_init;
  uint32_t data_size;
  uint8_t *bss;
  uint32_t bss_size;
  const np_shot_t *shots;
  uint8_t nshots;
  const char *const *records;            /* save files, deleted on uninstall (name*: all starting so) */
  const char *const *progress;           /* the ones Reset deletes (not levels made in an editor) */
#if NP_SIMULATOR
  uint32_t est_size;                     /* code and data, for the simulator */
#else
  uint32_t heap;                         /* the least RAM the game takes after its own (its heap) */
#endif
} np_game_t;

/* every game of games.json, the kit's too, can be in one NumPlay (tools/gen_games.py checks it) */
#define NP_MAX_GAMES 96
#if NP_SIMULATOR
extern const np_game_t np_games[];
extern const int np_game_count;
#else
/* Each game's entry is a section of its own; numplay.ld puts those of the games it links in a row. */
extern const np_game_t np_games[], np_games_end[];
#define np_game_count ((int)(np_games_end - np_games))
#endif

bool np_game_installed(int i);
uint32_t np_game_size(int i);            /* bytes of flash */
uint32_t np_game_ram_missing(int i);  /* the bytes of RAM a game needs more than there is, or 0 */
void np_game_run(int i);
/* The C library's malloc of a big game takes its memory here (the build renames the game's _sbrk):
 * the RAM after the running game's own, as _heap_start is for it (np_<game>_heap) */
void *np_sbrk(ptrdiff_t incr);

/* ---- storage (storage.c): Epsilon's record file system */
bool np_storage_delete(const char *name);   /* true if gone (or never there) */
uint32_t np_storage_record_size(const char *name);
/* a record's content (NULL if none), and writing one: for the games too */
const uint8_t *np_file_read(const char *name, uint32_t *len);
bool np_file_write(const char *name, const void *data, uint32_t len);
/* deletes a game's progress (np_game_t.progress); true if all of it is gone */
bool np_reset_game(int game);

/* NumPlay's own settings, in the file numplay.set */
enum { NP_SECRET_XNT, NP_SECRET_VAR, NP_SECRET_TOOLBOX, NP_SECRET_PI, NP_SECRET_SQRT, NP_SECRET_MENU, NP_SECRET_KONAMI, NP_SECRET_COUNT };
typedef struct {
  bool disguise;  /* start as Matrices, a calculator app; NumPlay opens through the secret */
  uint8_t secret; /* NP_SECRET_* */
  bool hint;      /* Matrices names the secret in light gray, bottom left */
} np_config_t;
void np_config_load(np_config_t *c);
bool np_config_save(const np_config_t *c);
bool np_bg_load(int *mode);   /* the home screen's live background chosen last; false when none was saved */
bool np_bg_save(int mode);
/* A copy of every save in numplay_saves.py, a Python script: the only kind of
   file the NumWorks installer keeps when NumPlay is updated. */
void np_progress_backup(void);
void np_progress_restore(void); /* only when none of the saves are left */
/* Matrices, the disguise: true when the secret was used (open NumPlay), false to quit */
bool np_matrices(const np_config_t *c);

/* ---- uninstalling (flash.c) */
enum { NP_UNINSTALL_OK, NP_UNINSTALL_UNSUPPORTED, NP_UNINSTALL_BATTERY, NP_UNINSTALL_FAILED };
bool np_uninstall_supported(void);
bool np_battery_ok(void);
int np_uninstall(int game, void (*progress)(int done, int total));
void np_finish_pending_uninstalls(void);

/* ---- the screens (home.c, settings.c) */
int np_home(int *selected, bool returning); /* a game index, -1 for settings, -2 to quit */
void np_settings(void);

#endif