Life! version 2
[wxWidgets.git] / demos / life / game.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: game.h
3 // Purpose: Life! game logic, version 2
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 struct Cell
39 {
40 wxInt32 i;
41 wxInt32 j;
42 };
43
44 // --------------------------------------------------------------------------
45 // LifeShape
46 // --------------------------------------------------------------------------
47
48 // A class which holds a pattern
49 class LifeShape
50 {
51 public:
52 LifeShape::LifeShape(wxString name,
53 wxString desc,
54 int width,
55 int height,
56 char *data)
57 {
58 m_name = name;
59 m_desc = desc;
60 m_width = width;
61 m_height = height;
62 m_data = data;
63 }
64
65 wxString m_name;
66 wxString m_desc;
67 int m_width;
68 int m_height;
69 char *m_data;
70 };
71
72
73 // --------------------------------------------------------------------------
74 // Life
75 // --------------------------------------------------------------------------
76
77 class CellBox;
78
79 class Life
80 {
81 public:
82 // ctor and dtor
83 Life();
84 ~Life();
85
86 // accessors
87 inline wxUint32 GetNumCells() const { return m_numcells; };
88 bool IsAlive (wxInt32 x, wxInt32 y);
89 void SetCell (wxInt32 x, wxInt32 y, bool alive = TRUE);
90 void SetShape(const LifeShape &shape);
91
92 // game control
93 void Clear();
94 bool NextTic();
95
96 // The following functions find cells within a given viewport; either
97 // all alive cells, or only those cells which have changed since last
98 // generation. You first call BeginFind() to specify the viewport,
99 // then keep calling FindMore() until it returns TRUE.
100 //
101 // BeginFind:
102 // Specify the viewport and whether to look for alive cells or for
103 // cells which have changed since the last generation and thus need
104 // to be repainted. In this latter case, there is no distinction
105 // between newborn or just-dead cells.
106 //
107 // FindMore:
108 // Fills an array with cells that match the specification given with
109 // BeginFind(). The array itself belongs to the Life object and must
110 // not be modified or freed by the caller. If this function returns
111 // FALSE, then the operation is not complete: just process all cells
112 // and call FillMore() again.
113 //
114 void BeginFind(wxInt32 i0, wxInt32 j0,
115 wxInt32 i1, wxInt32 j1,
116 bool changed);
117 bool FindMore(Cell *cells[], size_t *ncells);
118
119 private:
120 // cellbox-related
121 CellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
122 CellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = TRUE);
123 void KillBox(CellBox *c);
124
125 // helpers for FindMore & co.
126 void DoLine(wxInt32 i, wxInt32 j, wxUint32 alive, wxUint32 old);
127 void DoLine(wxInt32 i, wxInt32 j, wxUint32 alive);
128
129
130 CellBox *m_head; // list of alive boxes
131 CellBox *m_available; // list of reusable dead boxes
132 CellBox **m_boxes; // hash table of alive boxes
133 wxUint32 m_numcells; // population (number of alive cells)
134 Cell *m_cells; // cell array for FindMore()
135 size_t m_ncells; // number of valid cells in cell array
136 wxInt32 m_i, m_j, // state vars for FindMore()
137 m_i0, m_j0,
138 m_i1, m_j1;
139 bool m_changed;
140 bool m_findmore;
141 };
142
143 #endif // _LIFE_GAME_H_