summaryrefslogtreecommitdiff
path: root/src/eetg.h
blob: f691894dd1af1acb96c64f7b944226835b8416d7 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (c) 2024 Richard Braun.
 *
 * Permission to use, copy, modify, and/or distribute this software for
 * any purpose with or without fee is hereby granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *
 * Engine for embedded text-based games.
 */

#ifndef EETG_H
#define EETG_H

#include <stdbool.h>
#include <stdint.h>

#define EETG_COLUMNS 80
#define EETG_ROWS    24

#define EETG_RAND_MAX 32767

#define EETG_COLOR_BLACK    0
#define EETG_COLOR_RED      1
#define EETG_COLOR_GREEN    2
#define EETG_COLOR_YELLOW   3
#define EETG_COLOR_BLUE     4
#define EETG_COLOR_MAGENTA  5
#define EETG_COLOR_CYAN     6
#define EETG_COLOR_WHITE    7

#define EETG_NR_LAYERS 2

struct eetg_world;

struct eetg_object;

typedef void (*eetg_write_fn)(const void *buffer, size_t size, void *arg);

/*
 * Type for collision handling functions.
 *
 * Return true if the given objects collide.
 *
 * This function may not call any other function that could trigger
 * another collision.
 *
 * If an object is removed from its world while handling a collision,
 * it must persist in memory until the operation that triggered the
 * collision is complete.
 */
typedef bool (*eetg_handle_collision_fn)(struct eetg_object *object1,
                                         struct eetg_object *object2,
                                         int x, int y, void *arg);

struct eetg_object {
    struct eetg_world *world;
    struct eetg_object *next;
    const char *sprite;
    int8_t color;
    int8_t type;
    int8_t x;
    int8_t y;
    int8_t width;
    int8_t height;
    int8_t layer_id;
};

struct eetg_layer {
    struct eetg_object *objects;
};

struct eetg_view_cell {
    char c;
    int8_t color;
};

struct eetg_view_row {
    struct eetg_view_cell columns[EETG_COLUMNS];
};

struct eetg_view {
    struct eetg_view_row rows[EETG_ROWS];
};

/*
 * World.
 *
 * A world contains layers, to which objects are added. When rendering
 * a world, lower layers are processed before higher layers.
 */
struct eetg_world {
    eetg_write_fn write_fn;
    void *write_fn_arg;
    eetg_handle_collision_fn handle_collision_fn;
    void *handle_collision_fn_arg;
    struct eetg_layer layers[EETG_NR_LAYERS];
    struct eetg_view views[2];
    struct eetg_view *view;
    struct eetg_view *prev_view;
    int8_t cursor_row;
    int8_t cursor_column;
    int8_t current_color;
    bool handling_collision;
};

/*
 * Initialize a world.
 */
void eetg_world_init(struct eetg_world *world,
                     eetg_write_fn write_fn, void *arg);

/*
 * Clear a world of all its objects.
 *
 * On return, all objects added to the world on entry are removed from
 * the world.
 */
void eetg_world_clear(struct eetg_world *world);

/*
 * Set the collision handling function of a world.
 *
 * The passed function may be NULL.
 */
void eetg_world_set_collision_fn(struct eetg_world *world,
         eetg_handle_collision_fn handle_collision_fn, void *arg);

/*
 * Add an object to a world.
 *
 * The object must not already be part of another world.
 *
 * The object may be placed partially or completely outside the visible
 * part of the world.
 *
 * After the addition, the world is scanned for collisions. If the addition
 * triggered a collision, it is cancelled.
 *
 * This function returns true if a collision occurred, false otherwise.
 */
bool eetg_world_add(struct eetg_world *world, struct eetg_object *object,
                    int x, int y, int layer_id);

/*
 * Look up an object in a world at the given coordinates.
 *
 * If the layer ID is valid, look up only in layers greater than or equal to
 * the given ID. Otherwise, look up in the entire world.
 */
struct eetg_object *eetg_world_lookup(struct eetg_world *world,
                                      int x, int y, int layer_id);

/*
 * Remove an object from a world.
 */
void eetg_world_remove(struct eetg_world *world, struct eetg_object *object);

/*
 * Render a world.
 *
 * If sync is true, the entire screen is written out. Otherwise, only update
 * the screen.
 */
void eetg_world_render(struct eetg_world *world, bool sync);

/*
 * Initialize an object.
 *
 * The sprite must be a null-terminated string. Its content must form a
 * rectangle, i.e. all lines must have the exact same length.
 *
 * On return, the object is not part of a world, its coordinates are 0,0
 * and its layer ID is invalid.
 */
void eetg_object_init(struct eetg_object *object, int type, const char *sprite);

/*
 * Set the color of an object.
 */
void eetg_object_set_color(struct eetg_object *object, int color);

/*
 * Get the type of an object.
 */
int eetg_object_get_type(const struct eetg_object *object);

/*
 * Get the sprite of an object.
 */
const char *eetg_object_get_sprite(const struct eetg_object *object);

/*
 * Get the coordinates of an object.
 */
int eetg_object_get_x(const struct eetg_object *object);
int eetg_object_get_y(const struct eetg_object *object);

/*
 * Get the dimensions of an object.
 */
int eetg_object_get_width(const struct eetg_object *object);
int eetg_object_get_height(const struct eetg_object *object);

/*
 * Check if an object is empty.
 *
 * An object is empty when the only characters of its sprite, except for
 * new line characters, are space (' ') characters.
 */
bool eetg_object_is_empty(const struct eetg_object *object);

/*
 * Move an object.
 *
 * If the object is part of a world, after the move, that world is scanned
 * for collisions. If the move triggered a collision, it is cancelled.
 *
 * This function returns true if a collision occurred, false otherwise.
 */
bool eetg_object_move(struct eetg_object *object, int x, int y);

/*
 * Update an object.
 *
 * This function must be called after modifying the sprite of an object. The
 * content of the sprite when calling this function must form a rectangle,
 * i.e. all lines must have the exact same length.
 *
 * If the object is part of a world, after the update, that world is scanned
 * for collisions. If the update triggered a collision, it is cancelled.
 *
 * This function returns true if a collision occurred, false otherwise.
 */
bool eetg_object_update(struct eetg_object *object);

/*
 * Get the cell of an object at the given coordinates.
 *
 * A cell is an index in the object sprite referring the to the given
 * coordinates
 *
 * The given coordinates are world coordinates, i.e. the object coordinates
 * are subtracted from the given coordinates.
 */
int eetg_object_get_cell(const struct eetg_object *object, int x, int y);

/*
 * Get the world of an object.
 */
struct eetg_world *eetg_object_get_world(const struct eetg_object *object);

/*
 * Check if any of the given objects has the given type.
 */
bool eetg_object_has_type(const struct eetg_object *object1,
                          const struct eetg_object *object2,
                          int type);

/*
 * Initialize the pseudo-random number generator.
 */
void eetg_init_rand(unsigned int seed);

/*
 * Generate a random number.
 *
 * See EETG_RAND_MAX.
 */
int eetg_rand(void);

#endif /* EETG_H */