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