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