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