]> git.saurik.com Git - wxWidgets.git/blame - demos/life/game.h
Applied patch [ 1331340 ] faster Drawing of Polygons and Polylines
[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
GRG
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// --------------------------------------------------------------------------
f6bcfd97 28// LifePattern
2480be69
GRG
29// --------------------------------------------------------------------------
30
e0a40292 31// A class which holds a pattern
f6bcfd97 32class LifePattern
2480be69
GRG
33{
34public:
f6bcfd97
BP
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 };
5d2ac6b8
WS
46
47 // A more convenient ctor for the built-in samples
f6bcfd97
BP
48 LifePattern(wxString name,
49 wxString description,
50 int width,
51 int height,
52 const char *shape)
2480be69 53 {
f6bcfd97
BP
54 m_name = name;
55 m_description = description;
5d2ac6b8 56 m_rules = wxEmptyString;
f37c24e0 57 m_shape.Add( wxString::Format(_T("%i %i"), -width/2, -height/2) );
f6bcfd97 58 for(int j = 0; j < height; j++)
f37c24e0
MB
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 }
f6bcfd97
BP
69 };
70
71 wxString m_name;
72 wxString m_description;
73 wxString m_rules;
74 wxArrayString m_shape;
2480be69
GRG
75};
76
e0a40292 77
2480be69
GRG
78// --------------------------------------------------------------------------
79// Life
80// --------------------------------------------------------------------------
81
f6bcfd97 82// A struct used to pass cell coordinates around
764835a5 83struct LifeCell
f6bcfd97
BP
84{
85 wxInt32 i;
86 wxInt32 j;
5d2ac6b8 87};
f6bcfd97
BP
88
89// A private class that contains data about a block of cells
764835a5 90class LifeCellBox;
e0a40292 91
f6bcfd97 92// A class that models a Life game instance
2480be69
GRG
93class Life
94{
95public:
96 // ctor and dtor
e0a40292 97 Life();
2480be69 98 ~Life();
e0a40292
GRG
99
100 // accessors
f6bcfd97
BP
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);
3ac8bd7b 105 void SetCell(wxInt32 x, wxInt32 y, bool alive = true);
f6bcfd97 106 void SetPattern(const LifePattern &pattern);
e0a40292
GRG
107
108 // game control
2480be69 109 void Clear();
2480be69
GRG
110 bool NextTic();
111
29b07a38 112 // navigation
764835a5
GD
113 LifeCell FindNorth();
114 LifeCell FindSouth();
115 LifeCell FindWest();
116 LifeCell FindEast();
117 LifeCell FindCenter();
29b07a38 118
e0a40292
GRG
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,
3ac8bd7b 122 // then keep calling FindMore() until it returns true.
e0a40292
GRG
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
3ac8bd7b 134 // false, then the operation is not complete: just process all cells
e0a40292
GRG
135 // and call FillMore() again.
136 //
f6bcfd97
BP
137 void BeginFind(wxInt32 x0, wxInt32 y0,
138 wxInt32 x1, wxInt32 y1,
e0a40292 139 bool changed);
764835a5 140 bool FindMore(LifeCell *cells[], size_t *ncells);
2480be69 141
e0a40292
GRG
142private:
143 // cellbox-related
764835a5 144 LifeCellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
3ac8bd7b 145 LifeCellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = true);
764835a5 146 void KillBox(LifeCellBox *c);
e0a40292 147
29b07a38 148 // helper for BeginFind & FindMore
f6bcfd97
BP
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
764835a5
GD
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
f6bcfd97
BP
161 wxUint32 m_numcells; // population (number of alive cells)
162
163 // state vars for BeginFind & FindMore
764835a5 164 LifeCell *m_cells; // array of cells
f6bcfd97
BP
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;
2480be69
GRG
171};
172
173#endif // _LIFE_GAME_H_