]> git.saurik.com Git - wxWidgets.git/blob - demos/life/game.h
Added a Motif-only extra call to UpdateInfoText() just after frame creation
[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 #ifdef __GNUG__
16 #pragma interface "game.h"
17 #endif
18
19 // for compilers that support precompilation, includes "wx/wx.h"
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 // for all others, include the necessary headers
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 // --------------------------------------------------------------------------
32 // Cell
33 // --------------------------------------------------------------------------
34
35 // A Cell is just a struct which contains a pair of (i, j) coords.
36 // These structs are not used internally anywhere; they are just
37 // used to pass cell coordinates around.
38
39 struct Cell
40 {
41 wxInt32 i;
42 wxInt32 j;
43 };
44
45 // --------------------------------------------------------------------------
46 // LifeShape
47 // --------------------------------------------------------------------------
48
49 // A class which holds a pattern
50 class LifeShape
51 {
52 public:
53 LifeShape(wxString name,
54 wxString desc,
55 int width,
56 int height,
57 char *data)
58 {
59 m_name = name;
60 m_desc = desc;
61 m_width = width;
62 m_height = height;
63 m_data = data;
64 }
65
66 wxString m_name;
67 wxString m_desc;
68 int m_width;
69 int m_height;
70 char *m_data;
71 };
72
73
74 // --------------------------------------------------------------------------
75 // Life
76 // --------------------------------------------------------------------------
77
78 class CellBox;
79
80 class Life
81 {
82 public:
83 // ctor and dtor
84 Life();
85 ~Life();
86
87 // accessors
88 inline wxUint32 GetNumCells() const { return m_numcells; };
89 bool IsAlive (wxInt32 x, wxInt32 y);
90 void SetCell (wxInt32 x, wxInt32 y, bool alive = TRUE);
91 void SetShape (const LifeShape &shape);
92
93 // game control
94 void Clear();
95 bool NextTic();
96
97 // navigation
98 Cell FindNorth();
99 Cell FindSouth();
100 Cell FindWest();
101 Cell FindEast();
102 Cell FindCenter();
103
104 // The following functions find cells within a given viewport; either
105 // all alive cells, or only those cells which have changed since last
106 // generation. You first call BeginFind() to specify the viewport,
107 // then keep calling FindMore() until it returns TRUE.
108 //
109 // BeginFind:
110 // Specify the viewport and whether to look for alive cells or for
111 // cells which have changed since the last generation and thus need
112 // to be repainted. In this latter case, there is no distinction
113 // between newborn or just-dead cells.
114 //
115 // FindMore:
116 // Fills an array with cells that match the specification given with
117 // BeginFind(). The array itself belongs to the Life object and must
118 // not be modified or freed by the caller. If this function returns
119 // FALSE, then the operation is not complete: just process all cells
120 // and call FillMore() again.
121 //
122 void BeginFind(wxInt32 i0, wxInt32 j0,
123 wxInt32 i1, wxInt32 j1,
124 bool changed);
125 bool FindMore(Cell *cells[], size_t *ncells);
126
127 private:
128 // cellbox-related
129 CellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
130 CellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = TRUE);
131 void KillBox(CellBox *c);
132
133 // helper for BeginFind & FindMore
134 void DoLine(wxInt32 i, wxInt32 j, wxUint32 alive, wxUint32 old = 0);
135
136
137 CellBox *m_head; // list of alive boxes
138 CellBox *m_available; // list of reusable dead boxes
139 CellBox **m_boxes; // hash table of alive boxes
140 wxUint32 m_numcells; // population (number of alive cells)
141 Cell *m_cells; // cell array for FindMore()
142 size_t m_ncells; // number of valid cells in cell array
143 wxInt32 m_i, m_j, // state vars for FindMore()
144 m_i0, m_j0,
145 m_i1, m_j1;
146 bool m_changed;
147 bool m_findmore;
148 };
149
150 #endif // _LIFE_GAME_H_