~ubuntu-branches/ubuntu/intrepid/enigma/intrepid

« back to all changes in this revision

Viewing changes to src/enigma.hh

  • Committer: Bazaar Package Importer
  • Author(s): Erich Schubert
  • Date: 2005-08-28 15:30:09 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050828153009-sky64kb6tcq37xt5
Tags: 0.92.1-1
* New upstream subversion checkout
* Remove menu.s3m, which we are allowed to distributed but not to modify
  also copyright notice is confusing... (Closes: #321669)
* Rebuild with new libzipios (Closes: #325405)
  I hope this works without a versioned build-dependency
* Added "enigma replaces enigma-data" for upgrades (Closes: #308558)
* Added notes about the fonts copyright.
* updated to policy 3.6.2.1 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2002,2004 Daniel Heck
3
 
 *
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.
8
 
 *
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.
13
 
 *
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.
17
 
 *
18
 
 * $Id: enigma.hh,v 1.30 2004/05/27 20:38:15 dheck Exp $
19
 
 */
20
 
#ifndef ENIGMA_HH
21
 
#define ENIGMA_HH
22
 
 
23
 
/*
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.
27
 
 */
28
 
 
29
 
#include "fwd.hh"
30
 
#include "file.hh"
31
 
#include "px/pxfwd.hh"
32
 
#include "px/math.hh"
33
 
#include "px/tools.hh"
34
 
#include "px/cache.hh"
35
 
 
36
 
#define NUMENTRIES(array) (sizeof(array)/sizeof(*array))
37
 
 
38
 
namespace enigma
39
 
{
40
 
    using namespace file;
41
 
 
42
 
/* -------------------- Various types -------------------- */
43
 
 
44
 
    enum Difficulty {
45
 
        DIFFICULTY_EASY = 1,
46
 
        DIFFICULTY_HARD = 2
47
 
    };
48
 
 
49
 
 
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,
53
 
      etc.  */
54
 
    enum GameType {
55
 
        GAMET_FIRST,
56
 
 
57
 
        GAMET_ENIGMA = GAMET_FIRST,
58
 
        GAMET_OXYD1,
59
 
        GAMET_PEROXYD,
60
 
        GAMET_OXYDEXTRA,
61
 
        GAMET_OXYDMAGNUM,
62
 
 
63
 
        GAMET_LAST = GAMET_OXYDMAGNUM,
64
 
        GAMET_COUNT,
65
 
        GAMET_UNKNOWN
66
 
    };
67
 
 
68
 
/* -------------------- Resource Management -------------------- */
69
 
 
70
 
    class ImageCache : public px::PtrCache<px::Surface> {
71
 
    public:
72
 
        using px::PtrCache<px::Surface>::store;
73
 
 
74
 
        px::Surface *acquire(const std::string &name);
75
 
    };
76
 
 
77
 
 
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.  */
81
 
   
82
 
 
83
 
    void DefineFont (const char *name, 
84
 
                     const char *ttf_name, 
85
 
                     int ttf_size,
86
 
                     const char *bmf_name,
87
 
                     int r, int g, int b);
88
 
    px::Font *GetFont (const char *name);
89
 
 
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();
94
 
 
95
 
/* -------------------- Direction, DirectionBits -------------------- */
96
 
 
97
 
    enum Direction {
98
 
        NODIR = -1,
99
 
        WEST  = 0,
100
 
        SOUTH = 1,
101
 
        EAST  = 2,
102
 
        NORTH = 3,
103
 
    };
104
 
 
105
 
    Direction reverse (Direction d);
106
 
    Direction rotate_cw (Direction d);
107
 
    Direction rotate_ccw (Direction d);
108
 
 
109
 
    std::string to_suffix(Direction d);
110
 
 
111
 
    enum DirectionBits {
112
 
        NODIRBIT       = 0,
113
 
        WESTBIT        = 1 << WEST,
114
 
        SOUTHBIT       = 1 << SOUTH,
115
 
        EASTBIT        = 1 << EAST,
116
 
        NORTHBIT       = 1 << NORTH,
117
 
        ALL_DIRECTIONS = 15
118
 
    };
119
 
 
120
 
    DirectionBits rotate (DirectionBits d, bool clockwise);
121
 
    DirectionBits to_bits (Direction d);
122
 
    bool has_dir (DirectionBits db, Direction dir);
123
 
 
124
 
 
125
 
/* -------------------- Value -------------------- */
126
 
 
127
 
    class Value {
128
 
    public:
129
 
        enum Type { NIL, DOUBLE, STRING };
130
 
 
131
 
        Value() : type(NIL) {}
132
 
        Value(double d) : type(DOUBLE) { val.dval = d; }
133
 
        Value(const char* str);
134
 
        Value(const std::string& str);
135
 
        ~Value();
136
 
 
137
 
        Value(const Value& v);
138
 
        Value& operator=(const Value& v);
139
 
 
140
 
        void assign(double d);
141
 
        void assign(const char* s);
142
 
 
143
 
        Type    get_type() const { return type; }
144
 
        double  get_double() const throw();
145
 
        const char* get_string() const throw();
146
 
    private:
147
 
        void clear();
148
 
 
149
 
        // Variables
150
 
        Type type;
151
 
        union {
152
 
            double dval;
153
 
            char* str;
154
 
        } val;
155
 
    };
156
 
 
157
 
    px::Buffer& operator<<(px::Buffer& buf, const Value& val);
158
 
    px::Buffer& operator>>(px::Buffer& buf, Value& val);
159
 
 
160
 
    std::ostream& operator<<(std::ostream& os, const Value& val);
161
 
 
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);
167
 
 
168
 
/* -------------------- Timers -------------------- */
169
 
 
170
 
    /* Interface for time event handlers. */
171
 
    class TimeHandler {
172
 
    public:
173
 
        virtual ~TimeHandler() {}
174
 
        virtual void tick (double /*dtime*/) {}
175
 
        virtual void alarm() {}
176
 
    };
177
 
 
178
 
/* -------------------- GridPos -------------------- */
179
 
 
180
 
    struct GridPos {
181
 
        // Variables
182
 
        int x, y;
183
 
 
184
 
        // Methods
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;
189
 
    };
190
 
 
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);
196
 
 
197
 
    /* 516
198
 
       203
199
 
       748 */
200
 
    GridPos get_neighbour (GridPos p, int i);
201
 
 
202
 
    // source and target have to be adjacent
203
 
    Direction direction_fromto(GridPos source, GridPos target); 
204
 
 
205
 
    std::ostream& operator<<(std::ostream& os, const GridPos& val);
206
 
 
207
 
 
208
 
    // ---------- GridLayer ----------
209
 
 
210
 
    enum GridLayer {
211
 
        GRID_FLOOR, 
212
 
        GRID_ITEMS, 
213
 
        GRID_STONES, 
214
 
        GRID_COUNT
215
 
    };
216
 
 
217
 
    enum GridLayerBits {
218
 
        GRID_FLOOR_BIT  = 1,
219
 
        GRID_ITEMS_BIT  = 2,
220
 
        GRID_STONES_BIT = 4
221
 
    };
222
 
 
223
 
    // ---------- GridLoc ----------
224
 
 
225
 
    struct GridLoc {
226
 
        // Variables
227
 
        GridPos pos;
228
 
        GridLayer layer;
229
 
 
230
 
        // Constructor
231
 
        GridLoc(GridLayer l = GRID_FLOOR, GridPos p = GridPos());
232
 
    };
233
 
 
234
 
    /*! Converts strings like "it(10 10)", "st(5 2)" to GridLoc
235
 
      structures. */
236
 
    bool to_gridloc (const char *str, GridLoc &loc);
237
 
 
238
 
 
239
 
/* -------------------- Random Numbers -------------------- */
240
 
 
241
 
    void   Randomize ();
242
 
    void   Randomize (unsigned seed);
243
 
    int    IntegerRand (int min, int max);
244
 
    double DoubleRand (double min, double max);
245
 
 
246
 
/* -------------------- Time & Date -------------------- */
247
 
 
248
 
    const char *date(const char *format); // format see 'man strftime'
249
 
 
250
 
 
251
 
    /* ==================== Inline definitions ==================== */
252
 
 
253
 
    inline DirectionBits to_bits(Direction d) {
254
 
        if (d==NODIR)
255
 
            return NODIRBIT;
256
 
        return DirectionBits(1 << d);
257
 
    }
258
 
 
259
 
    inline bool has_dir(DirectionBits db, Direction dir) {
260
 
        return db & to_bits(dir);
261
 
    }
262
 
 
263
 
 
264
 
    // ---------- GridPos ----------
265
 
 
266
 
    inline GridPos::GridPos(int xx, int yy) 
267
 
    : x(xx), y(yy) 
268
 
    {}
269
 
 
270
 
 
271
 
    inline void GridPos::move(Direction dir) {
272
 
        switch(dir) {
273
 
        case NORTH: y--; break; 
274
 
        case SOUTH: y++; break;
275
 
        case EAST: x++; break; 
276
 
        case WEST: x--; break;
277
 
        case NODIR: break;
278
 
        }
279
 
    }
280
 
 
281
 
    inline px::V2 GridPos::center() const { 
282
 
        return px::V2(x+.5, y+.5); 
283
 
    }
284
 
 
285
 
 
286
 
    inline GridPos move(GridPos p, Direction dir) {
287
 
        GridPos tmp = p;
288
 
        tmp.move(dir);
289
 
        return tmp;
290
 
    }
291
 
 
292
 
    inline GridPos move(GridPos p, Direction dir, Direction dir2) {
293
 
        GridPos tmp = p;
294
 
        tmp.move(dir);
295
 
        tmp.move(dir2);
296
 
        return tmp;
297
 
    }
298
 
 
299
 
    inline bool operator == (GridPos a, GridPos b) { 
300
 
        return (a.x==b.x && a.y==b.y); 
301
 
    }
302
 
  
303
 
    inline bool operator != (GridPos a, GridPos b) { 
304
 
        return (a.x!=b.x || a.y!=b.y); 
305
 
    }
306
 
 
307
 
    inline bool operator< (GridPos a, GridPos b) {
308
 
        return ((a.y<<16) + a.x) < ((b.y<<16) + b.x);
309
 
    }
310
 
 
311
 
 
312
 
    // ---------- GridLoc ---------- 
313
 
 
314
 
    inline GridLoc::GridLoc(GridLayer l, GridPos p) 
315
 
    : pos(p), layer(l) 
316
 
    {}
317
 
 
318
 
}
319
 
 
320
 
#endif