2
* Copyright (C) 2002,2004 Daniel Heck
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License along
15
* with this program; if not, write to the Free Software Foundation, Inc.,
16
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18
* $Id: enigma.hh,v 1.30 2004/05/27 20:38:15 dheck Exp $
24
* This file contains declarations for facilities used by several
25
* different parts of the program, like common types and constants,
26
* and routines for resource management.
31
#include "px/pxfwd.hh"
33
#include "px/tools.hh"
34
#include "px/cache.hh"
36
#define NUMENTRIES(array) (sizeof(array)/sizeof(*array))
42
/* -------------------- Various types -------------------- */
50
/*! Enigma can run its own levels but also emulate various
51
versions of Oxyd. All these games behave similarly, but there
52
are a few differences in object behaviour, visual appearance,
57
GAMET_ENIGMA = GAMET_FIRST,
63
GAMET_LAST = GAMET_OXYDMAGNUM,
68
/* -------------------- Resource Management -------------------- */
70
class ImageCache : public px::PtrCache<px::Surface> {
72
using px::PtrCache<px::Surface>::store;
74
px::Surface *acquire(const std::string &name);
78
/* The `Get...' functions return a pointer to a cached copy of the
79
font or image. The `Load...' functions load a new copy which
80
you must deallocate yourself. */
83
void DefineFont (const char *name,
88
px::Font *GetFont (const char *name);
90
px::Surface *LoadImage (const char *name);
91
px::Surface *GetImage (const char *name, const char *ext = ".png");
92
px::Surface *RegisterImage (const char *name, px::Surface *s);
93
void ClearImageCache();
95
/* -------------------- Direction, DirectionBits -------------------- */
105
Direction reverse (Direction d);
106
Direction rotate_cw (Direction d);
107
Direction rotate_ccw (Direction d);
109
std::string to_suffix(Direction d);
114
SOUTHBIT = 1 << SOUTH,
116
NORTHBIT = 1 << NORTH,
120
DirectionBits rotate (DirectionBits d, bool clockwise);
121
DirectionBits to_bits (Direction d);
122
bool has_dir (DirectionBits db, Direction dir);
125
/* -------------------- Value -------------------- */
129
enum Type { NIL, DOUBLE, STRING };
131
Value() : type(NIL) {}
132
Value(double d) : type(DOUBLE) { val.dval = d; }
133
Value(const char* str);
134
Value(const std::string& str);
137
Value(const Value& v);
138
Value& operator=(const Value& v);
140
void assign(double d);
141
void assign(const char* s);
143
Type get_type() const { return type; }
144
double get_double() const throw();
145
const char* get_string() const throw();
157
px::Buffer& operator<<(px::Buffer& buf, const Value& val);
158
px::Buffer& operator>>(px::Buffer& buf, Value& val);
160
std::ostream& operator<<(std::ostream& os, const Value& val);
162
bool to_bool(const Value &v);
163
int to_int(const Value &v);
164
double to_double(const Value &v);
165
const char *to_string(const Value &v);
166
Direction to_direction (const Value &v);
168
/* -------------------- Timers -------------------- */
170
/* Interface for time event handlers. */
173
virtual ~TimeHandler() {}
174
virtual void tick (double /*dtime*/) {}
175
virtual void alarm() {}
178
/* -------------------- GridPos -------------------- */
185
explicit GridPos(int xx=0, int yy=0);
186
explicit GridPos(const px::V2& p);
187
void move(Direction dir);
188
px::V2 center() const;
191
GridPos move(GridPos p, Direction dir);
192
GridPos move(GridPos p, Direction dir, Direction dir2);
193
bool operator== (GridPos a, GridPos b);
194
bool operator != (GridPos a, GridPos b);
195
bool operator< (GridPos a, GridPos b);
200
GridPos get_neighbour (GridPos p, int i);
202
// source and target have to be adjacent
203
Direction direction_fromto(GridPos source, GridPos target);
205
std::ostream& operator<<(std::ostream& os, const GridPos& val);
208
// ---------- GridLayer ----------
223
// ---------- GridLoc ----------
231
GridLoc(GridLayer l = GRID_FLOOR, GridPos p = GridPos());
234
/*! Converts strings like "it(10 10)", "st(5 2)" to GridLoc
236
bool to_gridloc (const char *str, GridLoc &loc);
239
/* -------------------- Random Numbers -------------------- */
242
void Randomize (unsigned seed);
243
int IntegerRand (int min, int max);
244
double DoubleRand (double min, double max);
246
/* -------------------- Time & Date -------------------- */
248
const char *date(const char *format); // format see 'man strftime'
251
/* ==================== Inline definitions ==================== */
253
inline DirectionBits to_bits(Direction d) {
256
return DirectionBits(1 << d);
259
inline bool has_dir(DirectionBits db, Direction dir) {
260
return db & to_bits(dir);
264
// ---------- GridPos ----------
266
inline GridPos::GridPos(int xx, int yy)
271
inline void GridPos::move(Direction dir) {
273
case NORTH: y--; break;
274
case SOUTH: y++; break;
275
case EAST: x++; break;
276
case WEST: x--; break;
281
inline px::V2 GridPos::center() const {
282
return px::V2(x+.5, y+.5);
286
inline GridPos move(GridPos p, Direction dir) {
292
inline GridPos move(GridPos p, Direction dir, Direction dir2) {
299
inline bool operator == (GridPos a, GridPos b) {
300
return (a.x==b.x && a.y==b.y);
303
inline bool operator != (GridPos a, GridPos b) {
304
return (a.x!=b.x || a.y!=b.y);
307
inline bool operator< (GridPos a, GridPos b) {
308
return ((a.y<<16) + a.x) < ((b.y<<16) + b.x);
312
// ---------- GridLoc ----------
314
inline GridLoc::GridLoc(GridLayer l, GridPos p)