]>
Commit | Line | Data |
---|---|---|
025e88c5 JS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: bombs1.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 | ||
12 | /* | |
13 | * implementation of the methods DrawField and OnEvent of the | |
14 | * class BombsCanvas | |
15 | */ | |
16 | ||
17 | #ifdef __GNUG__ | |
0c65afdb | 18 | # pragma implementation |
025e88c5 JS |
19 | #endif |
20 | ||
21 | #include "wx/wxprec.h" | |
22 | ||
0c65afdb DS |
23 | #ifdef __BORLANDC__ |
24 | # pragma hdrstop | |
25 | #endif | |
26 | ||
025e88c5 | 27 | #ifndef WX_PRECOMP |
0c65afdb | 28 | # include "wx/wx.h" |
025e88c5 JS |
29 | #endif //precompiled headers |
30 | ||
31 | #include "bombs.h" | |
32 | ||
0c65afdb DS |
33 | // Draws the field on the device context dc |
34 | // xc1,yc1 etc. are the (inclusive) limits of the area to be drawn, | |
35 | // expressed in cells. | |
36 | void BombsCanvas::DrawField(wxDC *dc, int xc1, int yc1, int xc2, int yc2) | |
37 | { | |
38 | wxString buf; | |
39 | long chw, chh; | |
40 | ||
709d0081 | 41 | wxColour wxYellow = wxTheColourDatabase->Find(wxT("YELLOW")); |
0c65afdb | 42 | wxColour wxFocused = wxTheColourDatabase->Find(wxT("GREY")); |
0c65afdb | 43 | |
709d0081 WS |
44 | wxPen *bluePen = wxThePenList->FindOrCreatePen(*wxBLUE, 1, wxSOLID); |
45 | ||
0c65afdb | 46 | wxBrush *focusedBrush = wxTheBrushList->FindOrCreateBrush(wxFocused, wxSOLID); |
709d0081 | 47 | wxBrush *yellowBrush = wxTheBrushList->FindOrCreateBrush(wxYellow, wxSOLID); |
0c65afdb | 48 | |
709d0081 | 49 | dc->SetPen(*wxBLACK_PEN); |
0c65afdb DS |
50 | |
51 | int x, y; | |
52 | int xMax = this->GetGridSizeInPixels().GetWidth(); | |
53 | int yMax = this->GetGridSizeInPixels().GetHeight(); | |
54 | for(x=xc1; x<=xc2; x++) | |
55 | dc->DrawLine(x*m_cellWidth*X_UNIT, 0, x*m_cellWidth*X_UNIT, yMax); | |
56 | for(y=xc1; y<=yc2; y++) | |
57 | dc->DrawLine(0, y*m_cellHeight*Y_UNIT, xMax, y*m_cellHeight*Y_UNIT); | |
58 | ||
59 | ||
60 | wxFont font= BOMBS_FONT; | |
6759ff7d | 61 | dc->SetFont(font); |
0c65afdb DS |
62 | |
63 | for(x=xc1; x<=xc2; x++) | |
64 | for(y=yc1; y<=yc2; y++) | |
65 | { | |
66 | if (m_game->IsMarked(x,y)) | |
67 | { | |
709d0081 | 68 | dc->SetPen(*wxBLACK_PEN); |
0c65afdb DS |
69 | |
70 | if (m_game->IsFocussed(x, y)) | |
709d0081 | 71 | dc->SetBrush(*focusedBrush); |
0c65afdb | 72 | else |
709d0081 | 73 | dc->SetBrush(*wxLIGHT_GREY_BRUSH); |
0c65afdb DS |
74 | |
75 | dc->DrawRectangle( x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT, | |
76 | m_cellWidth*X_UNIT+1, m_cellHeight*Y_UNIT+1); | |
77 | buf = wxT("M"); | |
78 | if (!m_game->IsHidden(x,y) && m_game->IsBomb(x,y)) | |
709d0081 | 79 | dc->SetTextForeground(*wxBLUE); |
0c65afdb | 80 | else |
709d0081 | 81 | dc->SetTextForeground(*wxRED); |
0c65afdb | 82 | |
709d0081 | 83 | dc->SetTextBackground(*wxLIGHT_GREY); |
0c65afdb DS |
84 | dc->GetTextExtent(buf, &chw, &chh); |
85 | dc->DrawText( buf, | |
86 | x*m_cellWidth*X_UNIT + (m_cellWidth*X_UNIT-chw)/2, | |
87 | y*m_cellHeight*Y_UNIT + (m_cellHeight*Y_UNIT-chh)/2 ); | |
88 | ||
89 | if (!m_game->IsHidden(x,y) && m_game->IsBomb(x,y)) | |
90 | { | |
709d0081 | 91 | dc->SetPen(*wxRED_PEN); |
0c65afdb DS |
92 | dc->DrawLine(x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT, |
93 | (x+1)*m_cellWidth*X_UNIT, (y+1)*m_cellHeight*Y_UNIT); | |
94 | dc->DrawLine(x*m_cellWidth*X_UNIT, (y+1)*m_cellHeight*Y_UNIT, | |
95 | (x+1)*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT); | |
96 | } | |
97 | } | |
98 | else if (m_game->IsHidden(x,y)) | |
99 | { | |
709d0081 | 100 | dc->SetPen(*wxBLACK_PEN); |
0c65afdb | 101 | if (m_game->IsFocussed(x, y)) |
709d0081 | 102 | dc->SetBrush(*focusedBrush); |
0c65afdb | 103 | else |
709d0081 | 104 | dc->SetBrush(*wxLIGHT_GREY_BRUSH); |
0c65afdb DS |
105 | |
106 | dc->DrawRectangle( x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT, | |
107 | m_cellWidth*X_UNIT+1, m_cellHeight*Y_UNIT+1); | |
108 | } | |
109 | else if (m_game->IsBomb(x,y)) | |
110 | { | |
709d0081 WS |
111 | dc->SetPen(*wxBLACK_PEN); |
112 | dc->SetBrush(*wxRED_BRUSH); | |
0c65afdb DS |
113 | dc->DrawRectangle( x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT, |
114 | m_cellWidth*X_UNIT+1, m_cellHeight*Y_UNIT+1); | |
115 | buf = wxT("B"); | |
709d0081 WS |
116 | dc->SetTextForeground(*wxBLACK); |
117 | dc->SetTextBackground(*wxRED); | |
0c65afdb DS |
118 | dc->GetTextExtent(buf, &chw, &chh); |
119 | dc->DrawText( buf, | |
120 | x*m_cellWidth*X_UNIT + (m_cellWidth*X_UNIT-chw)/2, | |
121 | y*m_cellHeight*Y_UNIT + (m_cellHeight*Y_UNIT-chh)/2); | |
122 | if (m_game->IsExploded(x,y)) | |
123 | { | |
709d0081 | 124 | dc->SetPen(*bluePen); |
0c65afdb DS |
125 | dc->DrawLine(x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT, |
126 | (x+1)*m_cellWidth*X_UNIT, (y+1)*m_cellHeight*Y_UNIT); | |
127 | dc->DrawLine(x*m_cellWidth*X_UNIT, (y+1)*m_cellHeight*Y_UNIT, | |
128 | (x+1)*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT); | |
129 | } | |
130 | } | |
131 | else // Display a digit | |
132 | { | |
709d0081 | 133 | dc->SetPen(*wxBLACK_PEN); |
0c65afdb | 134 | if (m_game->IsFocussed(x, y)) |
709d0081 WS |
135 | dc->SetBrush(*focusedBrush); |
136 | else if (m_game->IsSelected(x,y)) | |
137 | dc->SetBrush(*wxWHITE_BRUSH); | |
138 | else | |
139 | dc->SetBrush(*yellowBrush); | |
0c65afdb DS |
140 | dc->DrawRectangle( x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT, |
141 | m_cellWidth*X_UNIT+1, m_cellHeight*Y_UNIT+1); | |
142 | ||
143 | int digit_value = m_game->Get(x,y) & BG_MASK; | |
144 | switch(digit_value) | |
145 | { | |
146 | case 0: | |
147 | buf = wxT("0"); | |
709d0081 | 148 | dc->SetTextForeground(*wxGREEN); |
0c65afdb | 149 | break; |
6759ff7d | 150 | case 1: |
0c65afdb | 151 | buf = wxT("1"); |
709d0081 | 152 | dc->SetTextForeground(*wxBLUE); |
0c65afdb DS |
153 | break; |
154 | default: | |
155 | buf.Printf(wxT("%d"),digit_value); | |
709d0081 | 156 | dc->SetTextForeground(*wxBLACK); |
0c65afdb DS |
157 | break; |
158 | } | |
025e88c5 | 159 | dc->GetTextExtent(buf, &chw, &chh); |
709d0081 | 160 | dc->SetTextBackground(*wxWHITE); |
025e88c5 | 161 | dc->DrawText( buf, |
0c65afdb DS |
162 | x*m_cellWidth*X_UNIT + (m_cellWidth*X_UNIT-chw)/2, |
163 | y*m_cellHeight*Y_UNIT + (m_cellHeight*Y_UNIT-chh)/2); | |
164 | } | |
025e88c5 | 165 | } |
0c65afdb DS |
166 | dc->SetFont(wxNullFont); |
167 | ||
924fe7da | 168 | wxString msg; |
709d0081 WS |
169 | msg.Printf(wxT("%d bombs, %u marked, %d remaining cells"), |
170 | m_game->GetNumBombs(), m_game->GetNumMarkedCells(), | |
171 | m_game->GetNumRemainingCells() ); | |
924fe7da WS |
172 | |
173 | #if wxUSE_LOG && wxUSE_STATUSBAR | |
174 | wxLogStatus(msg); | |
175 | #else | |
176 | this->GetParent()->SetTitle(msg); | |
0c65afdb | 177 | #endif |
025e88c5 JS |
178 | } |
179 | ||
0c65afdb DS |
180 | // Refreshes the field image |
181 | // xc1,yc1 etc. are the (inclusive) limits of the area to be drawn, | |
182 | // expressed in cells. | |
183 | void BombsCanvas::RefreshField(int xc1, int yc1, int xc2, int yc2) | |
184 | { | |
025e88c5 JS |
185 | wxClientDC dc(this); |
186 | DrawField(& dc, xc1, yc1, xc2, yc2); | |
0c65afdb DS |
187 | if (m_bmp) |
188 | { | |
189 | wxMemoryDC memDC; | |
190 | memDC.SelectObject(*m_bmp); | |
025e88c5 JS |
191 | DrawField(&memDC, xc1, yc1, xc2, yc2); |
192 | memDC.SelectObject(wxNullBitmap); | |
0c65afdb DS |
193 | } |
194 | } | |
025e88c5 | 195 | |
7874bf54 | 196 | // Called when uncovering a cell. |
0c65afdb | 197 | void BombsCanvas::Uncover(int x, int y) |
7874bf54 | 198 | { |
709d0081 | 199 | m_game->Unhide(x,y,true); |
0c65afdb DS |
200 | RefreshField(x, y, x, y); |
201 | ||
202 | const int gridWidth = m_game->GetWidth(); | |
203 | const int gridHeight = m_game->GetHeight(); | |
204 | ||
205 | const bool hasWon = m_game->GetNumRemainingCells() == 0; | |
206 | if (m_game->IsBomb(x,y) || hasWon) | |
207 | { | |
208 | wxBell(); | |
209 | if (hasWon) | |
210 | { | |
211 | wxMessageBox(wxT("Nice! You found all the bombs!"), | |
212 | wxT("wxWin Bombs"), wxOK|wxCENTRE); | |
213 | } | |
214 | else // x,y is a bomb | |
215 | { | |
216 | m_game->Explode(x, y); | |
217 | } | |
218 | ||
219 | for(x=0; x<gridWidth; x++) | |
220 | for(y=0; y<gridHeight; y++) | |
709d0081 WS |
221 | m_game->Unhide(x,y,false); |
222 | ||
223 | RefreshField(0, 0, gridWidth-1, gridHeight-1); | |
7874bf54 | 224 | } |
709d0081 | 225 | else if (0 == (m_game->Get(x, y) & BG_MASK)) |
0c65afdb DS |
226 | { |
227 | int left = ( x > 0 ) ? x-1 : 0; | |
228 | int right = ( x < gridWidth - 1 ) | |
229 | ? x+1 | |
230 | : gridWidth - 1; | |
231 | int top = ( y > 0 ) ? y-1 : 0; | |
232 | int bottom = ( y < gridHeight - 1 ) | |
233 | ? y+1 | |
234 | : gridHeight - 1; | |
235 | ||
236 | int i, j; | |
237 | for (j=top; j<=bottom; j++) | |
238 | for (i=left; i<=right; i++) | |
239 | if ( (i != x || j != y) && m_game->IsHidden(i, j) | |
240 | && !m_game->IsMarked(i, j) ) | |
241 | { | |
242 | Uncover(i, j); | |
243 | } | |
7874bf54 VZ |
244 | } |
245 | } | |
246 | ||
025e88c5 | 247 | // Called when the canvas receives a mouse event. |
0c65afdb | 248 | void BombsCanvas::OnMouseEvent(wxMouseEvent& event) |
f5d01a1c | 249 | { |
0c65afdb DS |
250 | const int gridWidth = m_game->GetWidth(); |
251 | const int gridHeight = m_game->GetHeight(); | |
252 | ||
253 | wxCoord fx, fy; | |
254 | event.GetPosition(&fx, &fy); | |
255 | int x = fx/(m_cellWidth*X_UNIT); | |
256 | int y = fy/(m_cellHeight*Y_UNIT); | |
257 | if (x<gridWidth && y<gridHeight) | |
258 | { | |
259 | if ( (event.RightDown() || (event.LeftDown() && event.ShiftDown())) | |
260 | && (m_game->IsHidden(x,y) | |
261 | || !m_game->GetNumRemainingCells() ) ) | |
6759ff7d | 262 | { |
0c65afdb DS |
263 | // store previous and current field |
264 | int prevFocusX = m_game->m_gridFocusX; | |
265 | int prevFocusY = m_game->m_gridFocusY; | |
266 | m_game->m_gridFocusX = x; | |
267 | m_game->m_gridFocusY = y; | |
268 | RefreshField(prevFocusX, prevFocusY, prevFocusX, prevFocusY); | |
269 | m_game->Mark(x, y); | |
270 | RefreshField(x, y, x, y); | |
271 | return; | |
025e88c5 | 272 | } |
0c65afdb DS |
273 | else if (event.LeftDown() && m_game->IsHidden(x,y) |
274 | && !m_game->IsMarked(x,y)) | |
275 | { | |
276 | // store previous and current field | |
277 | int prevGridFocusX = m_game->m_gridFocusX; | |
278 | int prevGridFocusY = m_game->m_gridFocusY; | |
279 | m_game->m_gridFocusX = x; | |
280 | m_game->m_gridFocusY = y; | |
281 | RefreshField(prevGridFocusX, prevGridFocusY, | |
282 | prevGridFocusX, prevGridFocusY); | |
283 | Uncover(x, y); | |
284 | return; | |
025e88c5 JS |
285 | } |
286 | } | |
287 | } | |
288 | ||
0c65afdb DS |
289 | void BombsCanvas::OnChar(wxKeyEvent& event) |
290 | { | |
291 | int keyCode = event.GetKeyCode(); | |
292 | int prevGridFocusX = m_game->m_gridFocusX; | |
293 | int prevGridFocusY = m_game->m_gridFocusY; | |
294 | ||
295 | const int gridWidth = m_game->GetWidth(); | |
296 | const int gridHeight = m_game->GetHeight(); | |
297 | ||
298 | switch(keyCode) | |
299 | { | |
300 | ||
301 | case WXK_RIGHT: | |
302 | m_game->m_gridFocusX++; | |
303 | if (m_game->m_gridFocusX >= gridWidth) m_game->m_gridFocusX = 0; | |
304 | break; | |
305 | ||
306 | case WXK_LEFT: | |
307 | m_game->m_gridFocusX--; | |
308 | if (m_game->m_gridFocusX<0) m_game->m_gridFocusX = gridWidth-1; | |
309 | break; | |
310 | ||
311 | case WXK_DOWN: | |
312 | m_game->m_gridFocusY++; | |
313 | if (m_game->m_gridFocusY >= gridHeight) m_game->m_gridFocusY = 0; | |
314 | break; | |
315 | ||
316 | case WXK_UP: | |
317 | m_game->m_gridFocusY--; | |
318 | if (m_game->m_gridFocusY<0) m_game->m_gridFocusY = gridHeight-1; | |
319 | break; | |
320 | ||
321 | case WXK_RETURN: | |
322 | if ( (prevGridFocusX == m_game->m_gridFocusX) | |
323 | && (prevGridFocusY == m_game->m_gridFocusY) | |
324 | && (m_game->IsHidden(m_game->m_gridFocusX, m_game->m_gridFocusY)) ) | |
325 | { | |
326 | m_game->Mark(m_game->m_gridFocusX, m_game->m_gridFocusY); | |
327 | if (!m_game->IsMarked(m_game->m_gridFocusX, m_game->m_gridFocusY)) | |
328 | { | |
329 | Uncover(m_game->m_gridFocusX, m_game->m_gridFocusY); | |
330 | } | |
331 | RefreshField(m_game->m_gridFocusX, m_game->m_gridFocusY, | |
332 | m_game->m_gridFocusX, m_game->m_gridFocusY); | |
333 | } | |
334 | break; | |
335 | ||
336 | default: | |
337 | event.Skip(); | |
338 | ||
339 | } | |
340 | ||
341 | if ((prevGridFocusX != m_game->m_gridFocusX) | |
342 | || (prevGridFocusY != m_game->m_gridFocusY)) | |
343 | { | |
23290a8c JS |
344 | // cause focused field to be visible after first key hit after launching new game |
345 | if( m_game->m_gridFocusX < 0 ) m_game->m_gridFocusX = 0; | |
346 | if( m_game->m_gridFocusY < 0 ) m_game->m_gridFocusY = 0; | |
347 | ||
0c65afdb DS |
348 | // refresh previous field and focused field |
349 | RefreshField(prevGridFocusX, prevGridFocusY, | |
350 | prevGridFocusX, prevGridFocusY); | |
351 | RefreshField(m_game->m_gridFocusX, m_game->m_gridFocusY, | |
352 | m_game->m_gridFocusX, m_game->m_gridFocusY); | |
353 | } | |
354 | } |