]> git.saurik.com Git - wxWidgets.git/blob - demos/bombs/bombs.cpp
Spelling fixes [#1017001], source cleaning.
[wxWidgets.git] / demos / bombs / bombs.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: bombs.cpp
3 // Purpose: Bombs game
4 // Author: P. Foggia 1996
5 // Modified by: Wlodzimierz Skiba (ABX) 2003
6 // Created: 1996
7 // RCS-ID: $Id$
8 // Copyright: (c) 1996 P. Foggia
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 # pragma implementation
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 # pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 # include "wx/wx.h"
24 #endif //precompiled headers
25
26 #include "bombs.h"
27
28 #include <stdlib.h>
29
30 #ifndef __WXWINCE__
31 # include <time.h>
32 #endif
33
34 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) \
35 || defined(__WXMAC__) || defined(__WXMGL__)
36 # include "bombs.xpm"
37 #endif
38
39 IMPLEMENT_APP(BombsApp)
40
41 #ifdef __WXWINCE__
42 STDAPI_(__int64) CeGetRandomSeed();
43 #endif
44
45 // Called to initialize the program
46 bool BombsApp::OnInit()
47 {
48 #ifdef __WXWINCE__
49 srand((unsigned) CeGetRandomSeed());
50 #else
51 srand((unsigned) time(NULL));
52 #endif
53
54 m_frame = new BombsFrame(&m_game);
55
56 m_frame->NewGame(bombsID_EASY, false);
57
58 return true;
59 }
60
61 BEGIN_EVENT_TABLE(BombsFrame, wxFrame)
62 EVT_MENU(bombsID_EASY, BombsFrame::OnNewEasyGame)
63 EVT_MENU(bombsID_MEDIUM, BombsFrame::OnNewMediumGame)
64 EVT_MENU(bombsID_HARD, BombsFrame::OnNewHardGame)
65 EVT_MENU(wxID_EXIT, BombsFrame::OnExit)
66 EVT_MENU(wxID_ABOUT, BombsFrame::OnAbout)
67 END_EVENT_TABLE()
68
69 BombsFrame::BombsFrame(BombsGame *game)
70 : wxFrame(NULL, wxID_ANY, wxT("wxBombs"), wxDefaultPosition,
71 wxSize(300, 300), wxDEFAULT_DIALOG_STYLE|wxMINIMIZE_BOX)
72 {
73 m_game = game;
74
75 SetIcon(wxICON(bombs));
76
77 #if wxUSE_STATUSBAR
78 CreateStatusBar();
79 #endif
80
81 // Create a menu bar for the frame
82 wxMenuBar *menuBar = new wxMenuBar;
83 wxMenu *menuFile = new wxMenu;
84 wxMenu *menuLevel = new wxMenu;
85 menuLevel->AppendRadioItem(bombsID_EASY, wxT("&Easy (10x10)\tCtrl-1"));
86 menuLevel->AppendRadioItem(bombsID_MEDIUM, wxT("&Medium (15x15)\tCtrl-2"));
87 menuLevel->AppendRadioItem(bombsID_HARD, wxT("&Hard (25x20)\tCtrl-3"));
88
89 menuFile->Append(bombsID_NEWGAME, wxT("&New Game"),
90 menuLevel, wxT("Starts a new game"));
91
92 menuFile->AppendSeparator();
93 menuFile->Append(wxID_EXIT, wxT("E&xit"), wxT("Quits the application"));
94
95 menuBar->Append(menuFile, wxT("&File"));
96
97
98 wxMenu *menuHelp = new wxMenu;
99 menuHelp->Append(wxID_ABOUT, wxT("&About"),
100 wxT("Displays the program information") );
101
102 menuBar->Append(menuHelp, wxT("&Help"));
103
104 SetMenuBar(menuBar);
105
106 // Create child subwindows.
107 m_canvas = new BombsCanvas(this, m_game);
108
109 // Ensure the subwindows get resized o.k.
110 // OnSize(width, height);
111
112 // Centre frame on the screen.
113 Centre(wxBOTH);
114
115 // Show the frame.
116 Show();
117 }
118
119 void BombsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
120 {
121 Close();
122 }
123
124 void BombsFrame::NewGame(int level, bool query)
125 {
126 if(query)
127 {
128 int ok = wxMessageBox(
129 wxT("Start new game regardless previous board?"),
130 wxT("Confirm"),
131 wxYES_NO | wxICON_QUESTION,
132 this
133 );
134 if(ok!=wxYES)return;
135 }
136
137 int numHorzCells = 20, numVertCells = 20;
138
139 switch(level)
140 {
141 case bombsID_EASY:
142 numHorzCells = numVertCells = 10;
143 break;
144
145 case bombsID_MEDIUM:
146 numHorzCells = numVertCells = 15;
147 break;
148
149 case bombsID_HARD:
150 numHorzCells = 25; numVertCells = 20;
151 break;
152
153 default :
154 wxFAIL_MSG(wxT("Invalid level"));
155 break;
156 }
157
158 m_game->Init(numHorzCells, numVertCells);
159
160 GetMenuBar()->Check(level, true);
161
162 m_canvas->UpdateGridSize();
163 SetClientSize(m_canvas->GetGridSizeInPixels());
164 }
165
166 void BombsFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
167 {
168 wxMessageBox(
169 wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"),
170 wxT("About wxBombs") );
171 }
172
173 void BombsFrame::OnNewEasyGame(wxCommandEvent& WXUNUSED(event))
174 {
175 NewGame(bombsID_EASY, true);
176 }
177
178 void BombsFrame::OnNewMediumGame(wxCommandEvent& WXUNUSED(event))
179 {
180 NewGame(bombsID_MEDIUM, true);
181 }
182
183 void BombsFrame::OnNewHardGame(wxCommandEvent& WXUNUSED(event))
184 {
185 NewGame(bombsID_HARD, true);
186 }
187
188 BEGIN_EVENT_TABLE(BombsCanvas, wxPanel)
189 EVT_PAINT(BombsCanvas::OnPaint)
190 EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent)
191 EVT_CHAR(BombsCanvas::OnChar)
192 END_EVENT_TABLE()
193
194 BombsCanvas::BombsCanvas(wxFrame *parent, BombsGame *game)
195 : wxPanel(parent, wxID_ANY)
196 {
197 m_game = game;
198 int sx, sy;
199 wxClientDC dc(this);
200 wxFont font= BOMBS_FONT;
201 dc.SetFont(font);
202
203 long chw, chh;
204 wxString buf = wxT("M");
205
206 dc.GetTextExtent(buf, &chw, &chh);
207 dc.SetFont(wxNullFont);
208
209 dc.SetMapMode(wxMM_METRIC);
210
211 int xcm = dc.LogicalToDeviceX(10);
212 int ycm = dc.LogicalToDeviceY(10);
213 // To have a square cell, there must be :
214 // sx*ycm == sy*xcm
215 if (chw*ycm < chh*xcm)
216 {
217 sy = chh;
218 sx = chh*xcm/ycm;
219 }
220 else
221 {
222 sx = chw;
223 sy = chw*ycm/xcm;
224 }
225
226 m_cellWidth = (sx+3+X_UNIT)/X_UNIT;
227 m_cellHeight = (sy+3+Y_UNIT)/Y_UNIT;
228 dc.SetMapMode(wxMM_TEXT);
229 m_bmp = NULL;
230 }
231
232 BombsCanvas::~BombsCanvas()
233 {
234 if (m_bmp)
235 {
236 delete m_bmp;
237 m_bmp = NULL;
238 }
239 }
240
241 // Called when canvas needs to be repainted.
242 void BombsCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
243 {
244 wxPaintDC dc(this);
245
246 const int numHorzCells = m_game->GetWidth();
247 const int numVertCells = m_game->GetHeight();
248 // Insert your drawing code here.
249 if (!m_bmp)
250 {
251 wxSize size = dc.GetSize();
252 m_bmp = new wxBitmap(size.GetWidth(), size.GetHeight());
253 if (m_bmp)
254 {
255 wxMemoryDC memDC;
256 memDC.SelectObject(*m_bmp);
257 DrawField(&memDC, 0, 0, numHorzCells-1, numVertCells-1);
258 memDC.SelectObject(wxNullBitmap);
259 }
260 }
261
262 if (m_bmp)
263 {
264 wxMemoryDC memDC;
265 memDC.SelectObject(*m_bmp);
266 wxSize size = dc.GetSize();
267 dc.Blit(0, 0, size.GetWidth(), size.GetHeight(),
268 &memDC, 0, 0, wxCOPY);
269 memDC.SelectObject(wxNullBitmap);
270 }
271 else
272 {
273 DrawField(&dc, 0, 0, numHorzCells-1, numVertCells-1);
274 }
275 }
276
277 void BombsCanvas::UpdateGridSize()
278 {
279
280 if (m_bmp)
281 {
282 delete m_bmp;
283 m_bmp = NULL;
284 }
285
286 Refresh();
287 }
288
289 wxSize BombsCanvas::GetGridSizeInPixels() const
290 {
291 return wxSize(m_cellWidth*X_UNIT*m_game->GetWidth(),
292 m_cellHeight*Y_UNIT*m_game->GetHeight());
293 }
294