aboutsummaryrefslogtreecommitdiff
path: root/src/cart.h
blob: 7478472f6840af9598f8ab71ad3a404d4b8a329e (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
110
111
112
113
114
115
116
117
118
119
120
#ifndef PW_CART_H
#define PW_CART_H

#include <SDL3/SDL_render.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 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.
 *
 * Carts are identified by name for for `LOAD()`-ing within PICO-8, this
 * enables multi-cart support.
 */
struct pw_cart_file {
  char name[PW_CART_NAME_MAX];
  char path[PW_CART_PATH_MAX];
};

/**
 * A top-level loadable cart that can be selected from the cart menu.
 *
 * Each cart has a separate multi-cart sub-directory for adding additional
 * carts that can be `LOAD()`-ed at runtime.
 */
struct pw_cart_entry {
  struct pw_cart_file file;
  struct pw_cart_file *multi;
  size_t nmulti;
};

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

/**
 * 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];
};

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

/**
 * Embedded cart data for running in PICO-8.
 */
struct pw_cart_data {
  size_t count;
  struct pw_cart carts[];
};

/**
 * 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.
 */
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 *cart);

/**
 * Loads the main cart from an entry.
 *
 * This will also decode and load the PNG to the cart `png` texture for use in
 * rendering.
 */
bool pw_cart_png_load(struct pw_cart_entry *entry, struct pw_cart_png *cart);

/**
 * Loads cart data from a cart entry. It always has at least one cart (the main
 * cart PNG from the entry), with an additional entry for each sub-cart in the
 * case of a multi-cart.
 *
 * Optionally specify a loaded cart PNG to copy the initial ROM from.
 */
struct pw_cart_data *pw_cart_data_load(struct pw_cart_entry *entry,
                                       struct pw_cart_png *cart);

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

#endif