aboutsummaryrefslogtreecommitdiff
path: root/src/cart.h
blob: b43c6a0cdc1b4122f96a5de4e935fa6e29762482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef PW_CART_H
#define PW_CART_H

#include <SDL3/SDL_render.h>

#include "fs.h"

/** The maximum supported PICO-8 cart path size. */
#define PW_CART_PATH_MAX 960

/** The maximum supported PICO-8 cart name size. */
#define PW_CART_NAME_MAX 64

/** The maximum number of embedded PICO-8 carts. */
#define PW_CART_EMBEDDED_MAX 32

/** The size of a PICO-8 ROM. */
#define PW_CART_ROM_SIZE 32768

/** The width in pixels of a PICO-8 PNG. */
#define PW_CART_PNG_WIDTH 160

/** The height in pixels of a PICO-8 PNG. */
#define PW_CART_PNG_HEIGHT 205

/**
 * A PICO-8 cart file that can be loaded.
 */
struct pw_cart_file {
  char name[PW_CART_NAME_MAX];
  char path[PW_CART_PATH_MAX];
};

/**
 * The list of carts read from the carts directory.
 */
struct pw_carts {
  size_t count;
  struct pw_cart_file files[];
};

/**
 * A loaded PICO-8 PNG cart, with both a texture for rendring the cart PNG
 * as well as its steganographically encoded ROM.
 */
struct pw_cart_png {
  SDL_Texture *texture;
  uint8_t rom[PW_CART_ROM_SIZE];
};

/**
 * PICO-8 cart data.
 */
struct pw_cart_data {
  char name[PW_CART_NAME_MAX];
  uint8_t rom[PW_CART_ROM_SIZE];
};

/**
 * A PICO-8 cart, its loaded embedded ROMs and mounted storage.
 */
struct pw_cart {
  size_t count;
  struct pw_fs *storage;
  struct pw_cart_data embedded[];
};

/**
 * List all carts in the `dir`. Returns `NULL` on failure.
 */
struct pw_carts *pw_carts_list(char const *dir);

/**
 * Free all memory associated with a carts list.
 */
void pw_carts_free(struct pw_carts *carts);

/**
 * Initializes an empty cart PNG.
 */
struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer);

/**
 * Frees a cart and all of its resources.
 */
void pw_cart_png_free(struct pw_cart_png *png);

/**
 * Loads the main cart PNG from a file.
 *
 * This will also decode and load the steganographically encoded ROM.
 */
bool pw_cart_png_load(struct pw_cart_file *file, struct pw_cart_png *png);

/**
 * Loads a cart from a file, including mounting its user data file-system.
 *
 * Optionally specify a loaded cart PNG to copy the ROM from instead of
 * re-reading from disk.
 */
struct pw_cart *pw_cart_load(struct pw_cart_file *file,
                             struct pw_cart_png *png);

/**
 * Frees a multi-cart and all of its resources.
 */
void pw_cart_free(struct pw_cart *cart);

#endif