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