Life! version 2
[wxWidgets.git] / demos / life / life.h
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
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
41 {
42 public:
43 // ctor and dtor
44 LifeCanvas(wxWindow* parent, Life* life, bool interactive = TRUE);
45 ~LifeCanvas();
46
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);
62
63 // event handlers
64 void OnPaint(wxPaintEvent& event);
65 void OnMouse(wxMouseEvent& event);
66 void OnSize(wxSizeEvent& event);
67 void OnScroll(wxScrollWinEvent& event);
68 void OnEraseBackground(wxEraseEvent& event);
69
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; };
75
76 // what is the user doing?
77 enum MouseStatus
78 {
79 MOUSE_NOACTION,
80 MOUSE_DRAWING,
81 MOUSE_ERASING
82 };
83
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
94 };
95
96 // --------------------------------------------------------------------------
97 // LifeTimer
98 // --------------------------------------------------------------------------
99
100 // Life timer
101 class LifeTimer : public wxTimer
102 {
103 public:
104 void Notify();
105 };
106
107 // --------------------------------------------------------------------------
108 // LifeFrame
109 // --------------------------------------------------------------------------
110
111 class LifeFrame : public wxFrame
112 {
113 public:
114 // ctor and dtor
115 LifeFrame();
116 ~LifeFrame();
117
118 // member functions
119 void UpdateInfoText();
120 void UpdateUI();
121 void OnTimer();
122
123 private:
124 // any class wishing to process wxWindows events must use this macro
125 DECLARE_EVENT_TABLE()
126
127 // event handlers
128 void OnMenu(wxCommandEvent& event);
129 void OnSamples(wxCommandEvent& event);
130 void OnSlider(wxScrollEvent& event);
131 void OnClose(wxCloseEvent& event);
132 void OnStart();
133 void OnStop();
134
135 Life *m_life;
136 LifeTimer *m_timer;
137 LifeCanvas *m_canvas;
138 wxStaticText *m_text;
139 bool m_running;
140 bool m_topspeed;
141 long m_interval;
142 long m_tics;
143 };
144
145 // --------------------------------------------------------------------------
146 // LifeApp
147 // --------------------------------------------------------------------------
148
149 class LifeApp : public wxApp
150 {
151 public:
152 virtual bool OnInit();
153 };
154
155 #endif // _LIFE_APP_H_