]>
Commit | Line | Data |
---|---|---|
2480be69 GRG |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: life.h | |
3 | // Purpose: The game of life, created by J. H. Conway | |
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_APP_H_ | |
13 | #define _LIFE_APP_H_ | |
14 | ||
15 | #ifdef __GNUG__ | |
16 | #pragma interface "life.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 | #include "game.h" | |
32 | ||
33 | // -------------------------------------------------------------------------- | |
34 | // LifeCanvas | |
35 | // -------------------------------------------------------------------------- | |
36 | ||
e0a40292 GRG |
37 | /* Note that in LifeCanvas, all cell coordinates are |
38 | * named i, j, while screen coordinates are named x, y. | |
39 | */ | |
40 | class LifeCanvas : public wxWindow | |
2480be69 GRG |
41 | { |
42 | public: | |
43 | // ctor and dtor | |
44 | LifeCanvas(wxWindow* parent, Life* life, bool interactive = TRUE); | |
45 | ~LifeCanvas(); | |
46 | ||
e0a40292 GRG |
47 | // view management |
48 | int GetCellSize() const { return m_cellsize; }; | |
49 | void SetCellSize(int cellsize); | |
50 | void Recenter(wxInt32 i, wxInt32 j); | |
51 | ||
52 | // drawing | |
53 | void DrawChanged(); | |
54 | void DrawCell(wxInt32 i, wxInt32 j, bool alive); | |
55 | ||
56 | private: | |
57 | // any class wishing to process wxWindows events must use this macro | |
58 | DECLARE_EVENT_TABLE() | |
59 | ||
60 | // draw a cell (parametrized by DC) | |
61 | void DrawCell(wxInt32 i, wxInt32 j, wxDC &dc); | |
2480be69 GRG |
62 | |
63 | // event handlers | |
64 | void OnPaint(wxPaintEvent& event); | |
65 | void OnMouse(wxMouseEvent& event); | |
66 | void OnSize(wxSizeEvent& event); | |
e0a40292 GRG |
67 | void OnScroll(wxScrollWinEvent& event); |
68 | void OnEraseBackground(wxEraseEvent& event); | |
2480be69 | 69 | |
e0a40292 GRG |
70 | // conversion between cell and screen coordinates |
71 | inline wxInt32 XToCell(wxCoord x) const { return (x / m_cellsize) + m_viewportX; }; | |
72 | inline wxInt32 YToCell(wxCoord y) const { return (y / m_cellsize) + m_viewportY; }; | |
73 | inline wxCoord CellToX(wxInt32 i) const { return (i - m_viewportX) * m_cellsize; }; | |
74 | inline wxCoord CellToY(wxInt32 j) const { return (j - m_viewportY) * m_cellsize; }; | |
2480be69 | 75 | |
e0a40292 | 76 | // what is the user doing? |
2480be69 GRG |
77 | enum MouseStatus |
78 | { | |
79 | MOUSE_NOACTION, | |
80 | MOUSE_DRAWING, | |
81 | MOUSE_ERASING | |
82 | }; | |
83 | ||
e0a40292 GRG |
84 | Life *m_life; // Life object |
85 | int m_cellsize; // current cell size, in pixels | |
86 | bool m_interactive; // is this canvas interactive? | |
87 | MouseStatus m_status; // what is the user doing? | |
88 | wxInt32 m_viewportX; // first visible cell (x coord) | |
89 | wxInt32 m_viewportY; // first visible cell (y coord) | |
90 | wxInt32 m_viewportW; // number of visible cells (w) | |
91 | wxInt32 m_viewportH; // number of visible cells (h) | |
92 | int m_thumbX; // horiz. scrollbar thumb position | |
93 | int m_thumbY; // vert. scrollbar thumb position | |
7989fb37 | 94 | wxInt32 m_mi, m_mj; // last mouse position |
2480be69 GRG |
95 | }; |
96 | ||
97 | // -------------------------------------------------------------------------- | |
98 | // LifeTimer | |
99 | // -------------------------------------------------------------------------- | |
100 | ||
101 | // Life timer | |
102 | class LifeTimer : public wxTimer | |
103 | { | |
104 | public: | |
105 | void Notify(); | |
106 | }; | |
107 | ||
108 | // -------------------------------------------------------------------------- | |
109 | // LifeFrame | |
110 | // -------------------------------------------------------------------------- | |
111 | ||
112 | class LifeFrame : public wxFrame | |
113 | { | |
114 | public: | |
115 | // ctor and dtor | |
116 | LifeFrame(); | |
117 | ~LifeFrame(); | |
118 | ||
119 | // member functions | |
120 | void UpdateInfoText(); | |
e0a40292 GRG |
121 | void UpdateUI(); |
122 | void OnTimer(); | |
123 | ||
124 | private: | |
125 | // any class wishing to process wxWindows events must use this macro | |
126 | DECLARE_EVENT_TABLE() | |
2480be69 GRG |
127 | |
128 | // event handlers | |
129 | void OnMenu(wxCommandEvent& event); | |
2480be69 | 130 | void OnSamples(wxCommandEvent& event); |
e0a40292 GRG |
131 | void OnSlider(wxScrollEvent& event); |
132 | void OnClose(wxCloseEvent& event); | |
2480be69 GRG |
133 | void OnStart(); |
134 | void OnStop(); | |
2480be69 | 135 | |
e0a40292 GRG |
136 | Life *m_life; |
137 | LifeTimer *m_timer; | |
2480be69 GRG |
138 | LifeCanvas *m_canvas; |
139 | wxStaticText *m_text; | |
140 | bool m_running; | |
e0a40292 | 141 | bool m_topspeed; |
2480be69 GRG |
142 | long m_interval; |
143 | long m_tics; | |
144 | }; | |
145 | ||
146 | // -------------------------------------------------------------------------- | |
147 | // LifeApp | |
148 | // -------------------------------------------------------------------------- | |
149 | ||
150 | class LifeApp : public wxApp | |
151 | { | |
152 | public: | |
153 | virtual bool OnInit(); | |
154 | }; | |
155 | ||
156 | #endif // _LIFE_APP_H_ |