]> git.saurik.com Git - wxWidgets.git/blame - demos/life/game.h
OS/2 patch
[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
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// --------------------------------------------------------------------------
e0a40292 32// Cell
2480be69
GRG
33// --------------------------------------------------------------------------
34
e0a40292
GRG
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.
29b07a38 38
e0a40292
GRG
39struct Cell
40{
41 wxInt32 i;
42 wxInt32 j;
43};
2480be69
GRG
44
45// --------------------------------------------------------------------------
46// LifeShape
47// --------------------------------------------------------------------------
48
e0a40292 49// A class which holds a pattern
2480be69
GRG
50class LifeShape
51{
52public:
6da3a3ef
GRG
53 LifeShape(wxString name,
54 wxString desc,
55 int width,
56 int height,
57 char *data)
2480be69 58 {
e0a40292
GRG
59 m_name = name;
60 m_desc = desc;
61 m_width = width;
62 m_height = height;
63 m_data = data;
2480be69
GRG
64 }
65
66 wxString m_name;
67 wxString m_desc;
68 int m_width;
69 int m_height;
70 char *m_data;
2480be69
GRG
71};
72
e0a40292 73
2480be69
GRG
74// --------------------------------------------------------------------------
75// Life
76// --------------------------------------------------------------------------
77
e0a40292
GRG
78class CellBox;
79
2480be69
GRG
80class Life
81{
82public:
83 // ctor and dtor
e0a40292 84 Life();
2480be69 85 ~Life();
e0a40292
GRG
86
87 // accessors
88 inline wxUint32 GetNumCells() const { return m_numcells; };
29b07a38
GRG
89 bool IsAlive (wxInt32 x, wxInt32 y);
90 void SetCell (wxInt32 x, wxInt32 y, bool alive = TRUE);
91 void SetShape (const LifeShape &shape);
e0a40292
GRG
92
93 // game control
2480be69 94 void Clear();
2480be69
GRG
95 bool NextTic();
96
29b07a38
GRG
97 // navigation
98 Cell FindNorth();
99 Cell FindSouth();
100 Cell FindWest();
101 Cell FindEast();
102 Cell FindCenter();
103
e0a40292
GRG
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);
2480be69 126
e0a40292
GRG
127private:
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
29b07a38
GRG
133 // helper for BeginFind & FindMore
134 void DoLine(wxInt32 i, wxInt32 j, wxUint32 alive, wxUint32 old = 0);
e0a40292
GRG
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;
2480be69
GRG
148};
149
150#endif // _LIFE_GAME_H_