]> git.saurik.com Git - wxWidgets.git/blob - demos/life/game.h
Tweaks for demos on MacOSX
[wxWidgets.git] / demos / life / game.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: game.h
3 // Purpose: Life! game logic
4 // Author: Guillermo Rodriguez Garcia, <guille@iies.es>
5 // Modified by:
6 // Created: Jan/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000, Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _LIFE_GAME_H_
13 #define _LIFE_GAME_H_
14
15 // for compilers that support precompilation, includes "wx/wx.h"
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 // for all others, include the necessary headers
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 // --------------------------------------------------------------------------
28 // LifePattern
29 // --------------------------------------------------------------------------
30
31 // A class which holds a pattern
32 class LifePattern
33 {
34 public:
35 // This ctor is used by the LifeReader class
36 LifePattern(wxString name,
37 wxString description,
38 wxString rules,
39 wxArrayString shape)
40 {
41 m_name = name;
42 m_description = description;
43 m_rules = rules;
44 m_shape = shape;
45 };
46
47 // A more convenient ctor for the built-in samples
48 LifePattern(wxString name,
49 wxString description,
50 int width,
51 int height,
52 const char *shape)
53 {
54 m_name = name;
55 m_description = description;
56 m_rules = wxEmptyString;
57 m_shape.Add( wxString::Format(_T("%i %i"), -width/2, -height/2) );
58 for(int j = 0; j < height; j++)
59 {
60 wxString tmp;
61
62 for(int i = 0; i < width; i++)
63 {
64 tmp += wxChar(shape[j * width + i]);
65 }
66
67 m_shape.Add( tmp );
68 }
69 };
70
71 wxString m_name;
72 wxString m_description;
73 wxString m_rules;
74 wxArrayString m_shape;
75 };
76
77
78 // --------------------------------------------------------------------------
79 // Life
80 // --------------------------------------------------------------------------
81
82 // A struct used to pass cell coordinates around
83 struct LifeCell
84 {
85 wxInt32 i;
86 wxInt32 j;
87 };
88
89 // A private class that contains data about a block of cells
90 class LifeCellBox;
91
92 // A class that models a Life game instance
93 class Life
94 {
95 public:
96 // ctor and dtor
97 Life();
98 ~Life();
99
100 // accessors
101 inline wxUint32 GetNumCells() const { return m_numcells; };
102 inline wxString GetRules() const { return m_rules; };
103 inline wxString GetDescription() const { return m_description; };
104 bool IsAlive(wxInt32 x, wxInt32 y);
105 void SetCell(wxInt32 x, wxInt32 y, bool alive = true);
106 void SetPattern(const LifePattern &pattern);
107
108 // game control
109 void Clear();
110 bool NextTic();
111
112 // navigation
113 LifeCell FindNorth();
114 LifeCell FindSouth();
115 LifeCell FindWest();
116 LifeCell FindEast();
117 LifeCell FindCenter();
118
119 // The following functions find cells within a given viewport; either
120 // all alive cells, or only those cells which have changed since last
121 // generation. You first call BeginFind() to specify the viewport,
122 // then keep calling FindMore() until it returns true.
123 //
124 // BeginFind:
125 // Specify the viewport and whether to look for alive cells or for
126 // cells which have changed since the last generation and thus need
127 // to be repainted. In this latter case, there is no distinction
128 // between newborn or just-dead cells.
129 //
130 // FindMore:
131 // Fills an array with cells that match the specification given with
132 // BeginFind(). The array itself belongs to the Life object and must
133 // not be modified or freed by the caller. If this function returns
134 // false, then the operation is not complete: just process all cells
135 // and call FillMore() again.
136 //
137 void BeginFind(wxInt32 x0, wxInt32 y0,
138 wxInt32 x1, wxInt32 y1,
139 bool changed);
140 bool FindMore(LifeCell *cells[], size_t *ncells);
141
142 private:
143 // cellbox-related
144 LifeCellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
145 LifeCellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = true);
146 void KillBox(LifeCellBox *c);
147
148 // helper for BeginFind & FindMore
149 void DoLine(wxInt32 x, wxInt32 y, wxUint32 alive, wxUint32 old = 0);
150
151
152 // pattern description
153 wxString m_name; // name (currently unused)
154 wxString m_rules; // rules (currently unused)
155 wxString m_description; // description
156
157 // pattern data
158 LifeCellBox *m_head; // list of alive boxes
159 LifeCellBox *m_available; // list of reusable dead boxes
160 LifeCellBox **m_boxes; // hash table of alive boxes
161 wxUint32 m_numcells; // population (number of alive cells)
162
163 // state vars for BeginFind & FindMore
164 LifeCell *m_cells; // array of cells
165 size_t m_ncells; // number of valid entries in m_cells
166 wxInt32 m_x, m_y, // counters and search mode
167 m_x0, m_y0,
168 m_x1, m_y1;
169 bool m_changed;
170 bool m_findmore;
171 };
172
173 #endif // _LIFE_GAME_H_