]> git.saurik.com Git - wxWidgets.git/blame - demos/life/game.h
Use /bin/echo for creation of Mac OS X PkgInfo files.
[wxWidgets.git] / demos / life / game.h
CommitLineData
2480be69
GRG
1/////////////////////////////////////////////////////////////////////////////
2// Name: game.h
29b07a38 3// Purpose: Life! game logic
2480be69
GRG
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
2480be69 15// --------------------------------------------------------------------------
f6bcfd97 16// LifePattern
2480be69
GRG
17// --------------------------------------------------------------------------
18
e0a40292 19// A class which holds a pattern
f6bcfd97 20class LifePattern
2480be69
GRG
21{
22public:
f6bcfd97
BP
23 // This ctor is used by the LifeReader class
24 LifePattern(wxString name,
25 wxString description,
26 wxString rules,
27 wxArrayString shape)
28 {
29 m_name = name;
30 m_description = description;
31 m_rules = rules;
32 m_shape = shape;
33 };
5d2ac6b8
WS
34
35 // A more convenient ctor for the built-in samples
f6bcfd97
BP
36 LifePattern(wxString name,
37 wxString description,
38 int width,
39 int height,
40 const char *shape)
2480be69 41 {
f6bcfd97
BP
42 m_name = name;
43 m_description = description;
5d2ac6b8 44 m_rules = wxEmptyString;
5cf83b1a
JS
45 // TODO: add the positions later, since the formatting command
46 // causes a crash due to conversion objects not being available
47 // during initialisation.
48#ifndef __WXMAC__
9a83f860 49 m_shape.Add( wxString::Format(wxT("%i %i"), -width/2, -height/2) );
5cf83b1a 50#endif
f6bcfd97 51 for(int j = 0; j < height; j++)
f37c24e0
MB
52 {
53 wxString tmp;
54
55 for(int i = 0; i < width; i++)
56 {
57 tmp += wxChar(shape[j * width + i]);
58 }
59
60 m_shape.Add( tmp );
61 }
f6bcfd97
BP
62 };
63
64 wxString m_name;
65 wxString m_description;
66 wxString m_rules;
67 wxArrayString m_shape;
2480be69
GRG
68};
69
e0a40292 70
2480be69
GRG
71// --------------------------------------------------------------------------
72// Life
73// --------------------------------------------------------------------------
74
f6bcfd97 75// A struct used to pass cell coordinates around
764835a5 76struct LifeCell
f6bcfd97
BP
77{
78 wxInt32 i;
79 wxInt32 j;
5d2ac6b8 80};
f6bcfd97
BP
81
82// A private class that contains data about a block of cells
764835a5 83class LifeCellBox;
e0a40292 84
f6bcfd97 85// A class that models a Life game instance
2480be69
GRG
86class Life
87{
88public:
89 // ctor and dtor
e0a40292 90 Life();
2480be69 91 ~Life();
e0a40292
GRG
92
93 // accessors
f6bcfd97
BP
94 inline wxUint32 GetNumCells() const { return m_numcells; };
95 inline wxString GetRules() const { return m_rules; };
96 inline wxString GetDescription() const { return m_description; };
97 bool IsAlive(wxInt32 x, wxInt32 y);
3ac8bd7b 98 void SetCell(wxInt32 x, wxInt32 y, bool alive = true);
f6bcfd97 99 void SetPattern(const LifePattern &pattern);
e0a40292
GRG
100
101 // game control
2480be69 102 void Clear();
2480be69
GRG
103 bool NextTic();
104
29b07a38 105 // navigation
764835a5
GD
106 LifeCell FindNorth();
107 LifeCell FindSouth();
108 LifeCell FindWest();
109 LifeCell FindEast();
110 LifeCell FindCenter();
29b07a38 111
e0a40292
GRG
112 // The following functions find cells within a given viewport; either
113 // all alive cells, or only those cells which have changed since last
114 // generation. You first call BeginFind() to specify the viewport,
3ac8bd7b 115 // then keep calling FindMore() until it returns true.
e0a40292
GRG
116 //
117 // BeginFind:
118 // Specify the viewport and whether to look for alive cells or for
119 // cells which have changed since the last generation and thus need
120 // to be repainted. In this latter case, there is no distinction
121 // between newborn or just-dead cells.
122 //
123 // FindMore:
124 // Fills an array with cells that match the specification given with
125 // BeginFind(). The array itself belongs to the Life object and must
126 // not be modified or freed by the caller. If this function returns
3ac8bd7b 127 // false, then the operation is not complete: just process all cells
e0a40292
GRG
128 // and call FillMore() again.
129 //
f6bcfd97
BP
130 void BeginFind(wxInt32 x0, wxInt32 y0,
131 wxInt32 x1, wxInt32 y1,
e0a40292 132 bool changed);
764835a5 133 bool FindMore(LifeCell *cells[], size_t *ncells);
2480be69 134
e0a40292
GRG
135private:
136 // cellbox-related
764835a5 137 LifeCellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
3ac8bd7b 138 LifeCellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = true);
764835a5 139 void KillBox(LifeCellBox *c);
e0a40292 140
29b07a38 141 // helper for BeginFind & FindMore
f6bcfd97
BP
142 void DoLine(wxInt32 x, wxInt32 y, wxUint32 alive, wxUint32 old = 0);
143
144
145 // pattern description
146 wxString m_name; // name (currently unused)
147 wxString m_rules; // rules (currently unused)
148 wxString m_description; // description
149
150 // pattern data
764835a5
GD
151 LifeCellBox *m_head; // list of alive boxes
152 LifeCellBox *m_available; // list of reusable dead boxes
153 LifeCellBox **m_boxes; // hash table of alive boxes
f6bcfd97
BP
154 wxUint32 m_numcells; // population (number of alive cells)
155
156 // state vars for BeginFind & FindMore
764835a5 157 LifeCell *m_cells; // array of cells
f6bcfd97
BP
158 size_t m_ncells; // number of valid entries in m_cells
159 wxInt32 m_x, m_y, // counters and search mode
160 m_x0, m_y0,
161 m_x1, m_y1;
162 bool m_changed;
163 bool m_findmore;
2480be69
GRG
164};
165
166#endif // _LIFE_GAME_H_