Various cleanups and clarifications to devel.but; some from Richard
[sgt/puzzles] / puzzles.h
1 /*
2 * puzzles.h: header file for my puzzle collection
3 */
4
5 #ifndef PUZZLES_PUZZLES_H
6 #define PUZZLES_PUZZLES_H
7
8 #include <stdlib.h> /* for size_t */
9
10 #ifndef TRUE
11 #define TRUE 1
12 #endif
13 #ifndef FALSE
14 #define FALSE 0
15 #endif
16
17 #define PI 3.141592653589793238462643383279502884197169399
18
19 #define lenof(array) ( sizeof(array) / sizeof(*(array)) )
20
21 #define STR_INT(x) #x
22 #define STR(x) STR_INT(x)
23
24 /* NB not perfect because they evaluate arguments multiple times. */
25 #ifndef max
26 #define max(x,y) ( (x)>(y) ? (x) : (y) )
27 #endif /* max */
28 #ifndef min
29 #define min(x,y) ( (x)<(y) ? (x) : (y) )
30 #endif /* min */
31
32 enum {
33 LEFT_BUTTON = 0x0200,
34 MIDDLE_BUTTON,
35 RIGHT_BUTTON,
36 LEFT_DRAG,
37 MIDDLE_DRAG,
38 RIGHT_DRAG,
39 LEFT_RELEASE,
40 MIDDLE_RELEASE,
41 RIGHT_RELEASE,
42 CURSOR_UP,
43 CURSOR_DOWN,
44 CURSOR_LEFT,
45 CURSOR_RIGHT,
46 CURSOR_SELECT,
47
48 /* made smaller because of 'limited range of datatype' errors. */
49 MOD_CTRL = 0x1000,
50 MOD_SHFT = 0x2000,
51 MOD_NUM_KEYPAD = 0x4000,
52 MOD_MASK = 0x7000 /* mask for all modifiers */
53 };
54
55 #define IS_MOUSE_DOWN(m) ( (unsigned)((m) - LEFT_BUTTON) <= \
56 (unsigned)(RIGHT_BUTTON - LEFT_BUTTON))
57 #define IS_MOUSE_DRAG(m) ( (unsigned)((m) - LEFT_DRAG) <= \
58 (unsigned)(RIGHT_DRAG - LEFT_DRAG))
59 #define IS_MOUSE_RELEASE(m) ( (unsigned)((m) - LEFT_RELEASE) <= \
60 (unsigned)(RIGHT_RELEASE - LEFT_RELEASE))
61
62 /* Bit flags indicating mouse button priorities */
63 #define BUTTON_BEATS(x,y) ( 1 << (((x)-LEFT_BUTTON)*3+(y)-LEFT_BUTTON) )
64
65 /* Another random flag that goes in the mouse priorities section for want
66 * of a better place to put it */
67 #define SOLVE_ANIMATES ( 1 << 9 )
68
69 #define IGNOREARG(x) ( (x) = (x) )
70
71 typedef struct frontend frontend;
72 typedef struct config_item config_item;
73 typedef struct midend_data midend_data;
74 typedef struct random_state random_state;
75 typedef struct game_params game_params;
76 typedef struct game_state game_state;
77 typedef struct game_ui game_ui;
78 typedef struct game_drawstate game_drawstate;
79 typedef struct game game;
80 typedef struct blitter blitter;
81
82 #define ALIGN_VNORMAL 0x000
83 #define ALIGN_VCENTRE 0x100
84
85 #define ALIGN_HLEFT 0x000
86 #define ALIGN_HCENTRE 0x001
87 #define ALIGN_HRIGHT 0x002
88
89 #define FONT_FIXED 0
90 #define FONT_VARIABLE 1
91
92 /*
93 * Structure used to pass configuration data between frontend and
94 * game
95 */
96 enum { C_STRING, C_CHOICES, C_BOOLEAN, C_END };
97 struct config_item {
98 /*
99 * `name' is never dynamically allocated.
100 */
101 char *name;
102 /*
103 * `type' contains one of the above values.
104 */
105 int type;
106 /*
107 * For C_STRING, `sval' is always dynamically allocated and
108 * non-NULL. For C_BOOLEAN and C_END, `sval' is always NULL.
109 * For C_CHOICES, `sval' is non-NULL, _not_ dynamically
110 * allocated, and contains a set of option strings separated by
111 * a delimiter. The delimeter is also the first character in
112 * the string, so for example ":Foo:Bar:Baz" gives three
113 * options `Foo', `Bar' and `Baz'.
114 */
115 char *sval;
116 /*
117 * For C_BOOLEAN, this is TRUE or FALSE. For C_CHOICES, it
118 * indicates the chosen index from the `sval' list. In the
119 * above example, 0==Foo, 1==Bar and 2==Baz.
120 */
121 int ival;
122 };
123
124 /*
125 * Platform routines
126 */
127
128 /* We can't use #ifdef DEBUG, because Cygwin defines it by default. */
129 #ifdef DEBUGGING
130 #define debug(x) (debug_printf x)
131 void debug_printf(char *fmt, ...);
132 #else
133 #define debug(x)
134 #endif
135
136 void fatal(char *fmt, ...);
137 void frontend_default_colour(frontend *fe, float *output);
138 void draw_text(frontend *fe, int x, int y, int fonttype, int fontsize,
139 int align, int colour, char *text);
140 void draw_rect(frontend *fe, int x, int y, int w, int h, int colour);
141 void draw_line(frontend *fe, int x1, int y1, int x2, int y2, int colour);
142 void draw_polygon(frontend *fe, int *coords, int npoints,
143 int fillcolour, int outlinecolour);
144 void draw_circle(frontend *fe, int cx, int cy, int radius,
145 int fillcolour, int outlinecolour);
146 void clip(frontend *fe, int x, int y, int w, int h);
147 void unclip(frontend *fe);
148 void start_draw(frontend *fe);
149 void draw_update(frontend *fe, int x, int y, int w, int h);
150 void end_draw(frontend *fe);
151 void deactivate_timer(frontend *fe);
152 void activate_timer(frontend *fe);
153 void status_bar(frontend *fe, char *text);
154 void get_random_seed(void **randseed, int *randseedsize);
155
156 blitter *blitter_new(int w, int h);
157 void blitter_free(blitter *bl);
158 /* save puts the portion of the current display with top-left corner
159 * (x,y) to the blitter. load puts it back again to the specified
160 * coords, or else wherever it was saved from
161 * (if x = y = BLITTER_FROMSAVED). */
162 void blitter_save(frontend *fe, blitter *bl, int x, int y);
163 #define BLITTER_FROMSAVED (-1)
164 void blitter_load(frontend *fe, blitter *bl, int x, int y);
165
166 /*
167 * midend.c
168 */
169 midend_data *midend_new(frontend *fe, const game *ourgame);
170 void midend_free(midend_data *me);
171 void midend_set_params(midend_data *me, game_params *params);
172 void midend_size(midend_data *me, int *x, int *y, int expand);
173 void midend_new_game(midend_data *me);
174 void midend_restart_game(midend_data *me);
175 void midend_stop_anim(midend_data *me);
176 int midend_process_key(midend_data *me, int x, int y, int button);
177 void midend_force_redraw(midend_data *me);
178 void midend_redraw(midend_data *me);
179 float *midend_colours(midend_data *me, int *ncolours);
180 void midend_timer(midend_data *me, float tplus);
181 int midend_num_presets(midend_data *me);
182 void midend_fetch_preset(midend_data *me, int n,
183 char **name, game_params **params);
184 int midend_wants_statusbar(midend_data *me);
185 enum { CFG_SETTINGS, CFG_SEED, CFG_DESC };
186 config_item *midend_get_config(midend_data *me, int which, char **wintitle);
187 char *midend_set_config(midend_data *me, int which, config_item *cfg);
188 char *midend_game_id(midend_data *me, char *id);
189 char *midend_text_format(midend_data *me);
190 char *midend_solve(midend_data *me);
191 void midend_supersede_game_desc(midend_data *me, char *desc, char *privdesc);
192 char *midend_rewrite_statusbar(midend_data *me, char *text);
193 void midend_serialise(midend_data *me,
194 void (*write)(void *ctx, void *buf, int len),
195 void *wctx);
196 char *midend_deserialise(midend_data *me,
197 int (*read)(void *ctx, void *buf, int len),
198 void *rctx);
199
200 /*
201 * malloc.c
202 */
203 void *smalloc(size_t size);
204 void *srealloc(void *p, size_t size);
205 void sfree(void *p);
206 char *dupstr(const char *s);
207 #define snew(type) \
208 ( (type *) smalloc (sizeof (type)) )
209 #define snewn(number, type) \
210 ( (type *) smalloc ((number) * sizeof (type)) )
211 #define sresize(array, number, type) \
212 ( (type *) srealloc ((array), (number) * sizeof (type)) )
213
214 /*
215 * misc.c
216 */
217 void free_cfg(config_item *cfg);
218 void obfuscate_bitmap(unsigned char *bmp, int bits, int decode);
219
220 /* allocates output each time. len is always in bytes of binary data.
221 * May assert (or just go wrong) if lengths are unchecked. */
222 char *bin2hex(const unsigned char *in, int inlen);
223 unsigned char *hex2bin(const char *in, int outlen);
224
225 /* Sets (and possibly dims) background from frontend default colour,
226 * and auto-generates highlight and lowlight colours too. */
227 void game_mkhighlight(frontend *fe, float *ret,
228 int background, int highlight, int lowlight);
229
230 /* Randomly shuffles an array of items. */
231 void shuffle(void *array, int nelts, int eltsize, random_state *rs);
232
233 /* Draw a rectangle outline, using the frontend's draw_line. */
234 void draw_rect_outline(frontend *fe, int x, int y, int w, int h,
235 int colour);
236
237 /*
238 * version.c
239 */
240 extern char ver[];
241
242 /*
243 * random.c
244 */
245 random_state *random_init(char *seed, int len);
246 random_state *random_copy(random_state *tocopy);
247 unsigned long random_bits(random_state *state, int bits);
248 unsigned long random_upto(random_state *state, unsigned long limit);
249 void random_free(random_state *state);
250 char *random_state_encode(random_state *state);
251 random_state *random_state_decode(char *input);
252 /* random.c also exports SHA, which occasionally comes in useful. */
253 typedef unsigned long uint32;
254 typedef struct {
255 uint32 h[5];
256 unsigned char block[64];
257 int blkused;
258 uint32 lenhi, lenlo;
259 } SHA_State;
260 void SHA_Init(SHA_State *s);
261 void SHA_Bytes(SHA_State *s, void *p, int len);
262 void SHA_Final(SHA_State *s, unsigned char *output);
263 void SHA_Simple(void *p, int len, unsigned char *output);
264
265 /*
266 * Data structure containing the function calls and data specific
267 * to a particular game. This is enclosed in a data structure so
268 * that a particular platform can choose, if it wishes, to compile
269 * all the games into a single combined executable rather than
270 * having lots of little ones.
271 */
272 struct game {
273 const char *name;
274 const char *winhelp_topic;
275 game_params *(*default_params)(void);
276 int (*fetch_preset)(int i, char **name, game_params **params);
277 void (*decode_params)(game_params *, char const *string);
278 char *(*encode_params)(game_params *, int full);
279 void (*free_params)(game_params *params);
280 game_params *(*dup_params)(game_params *params);
281 int can_configure;
282 config_item *(*configure)(game_params *params);
283 game_params *(*custom_params)(config_item *cfg);
284 char *(*validate_params)(game_params *params, int full);
285 char *(*new_desc)(game_params *params, random_state *rs,
286 char **aux, int interactive);
287 char *(*validate_desc)(game_params *params, char *desc);
288 game_state *(*new_game)(midend_data *me, game_params *params, char *desc);
289 game_state *(*dup_game)(game_state *state);
290 void (*free_game)(game_state *state);
291 int can_solve;
292 char *(*solve)(game_state *orig, game_state *curr,
293 char *aux, char **error);
294 int can_format_as_text;
295 char *(*text_format)(game_state *state);
296 game_ui *(*new_ui)(game_state *state);
297 void (*free_ui)(game_ui *ui);
298 char *(*encode_ui)(game_ui *ui);
299 void (*decode_ui)(game_ui *ui, char *encoding);
300 void (*changed_state)(game_ui *ui, game_state *oldstate,
301 game_state *newstate);
302 char *(*interpret_move)(game_state *state, game_ui *ui, game_drawstate *ds,
303 int x, int y, int button);
304 game_state *(*execute_move)(game_state *state, char *move);
305 int preferred_tilesize;
306 void (*compute_size)(game_params *params, int tilesize, int *x, int *y);
307 void (*set_size)(game_drawstate *ds, game_params *params, int tilesize);
308 float *(*colours)(frontend *fe, game_state *state, int *ncolours);
309 game_drawstate *(*new_drawstate)(game_state *state);
310 void (*free_drawstate)(game_drawstate *ds);
311 void (*redraw)(frontend *fe, game_drawstate *ds, game_state *oldstate,
312 game_state *newstate, int dir, game_ui *ui, float anim_time,
313 float flash_time);
314 float (*anim_length)(game_state *oldstate, game_state *newstate, int dir,
315 game_ui *ui);
316 float (*flash_length)(game_state *oldstate, game_state *newstate, int dir,
317 game_ui *ui);
318 int (*wants_statusbar)(void);
319 int is_timed;
320 int (*timing_state)(game_state *state, game_ui *ui);
321 int mouse_priorities;
322 };
323
324 /*
325 * For one-game-at-a-time platforms, there's a single structure
326 * like the above, under a fixed name. For all-at-once platforms,
327 * there's a list of all available puzzles in array form.
328 */
329 #ifdef COMBINED
330 extern const game *gamelist[];
331 extern const int gamecount;
332 #else
333 extern const game thegame;
334 #endif
335
336 #endif /* PUZZLES_PUZZLES_H */