]> git.saurik.com Git - wxWidgets.git/blob - demos/bombs/bombs1.cpp
/usr/sbin is a better default location for sendmail than /usr/lib nowadays
[wxWidgets.git] / demos / bombs / bombs1.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: bombs1.cpp
3 // Purpose: Bombs game
4 // Author: P. Foggia 1996
5 // Modified by: Wlodzimierz Skiba (ABX) since 2003
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__
18 # pragma implementation
19 #endif
20
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 # pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 # include "wx/wx.h"
29 #endif //precompiled headers
30
31 #include "bombs.h"
32
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
41 wxColour wxYellow = wxTheColourDatabase->Find(wxT("YELLOW"));
42 wxColour wxFocused = wxTheColourDatabase->Find(wxT("GREY"));
43
44 wxPen *bluePen = wxThePenList->FindOrCreatePen(*wxBLUE, 1, wxSOLID);
45
46 wxBrush *focusedBrush = wxTheBrushList->FindOrCreateBrush(wxFocused, wxSOLID);
47 wxBrush *yellowBrush = wxTheBrushList->FindOrCreateBrush(wxYellow, wxSOLID);
48
49 dc->SetPen(*wxBLACK_PEN);
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;
61 dc->SetFont(font);
62
63 for(x=xc1; x<=xc2; x++)
64 for(y=yc1; y<=yc2; y++)
65 {
66 if (m_game->IsMarked(x,y))
67 {
68 dc->SetPen(*wxBLACK_PEN);
69
70 if (m_game->IsFocussed(x, y))
71 dc->SetBrush(*focusedBrush);
72 else
73 dc->SetBrush(*wxLIGHT_GREY_BRUSH);
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))
79 dc->SetTextForeground(*wxBLUE);
80 else
81 dc->SetTextForeground(*wxRED);
82
83 dc->SetTextBackground(*wxLIGHT_GREY);
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 {
91 dc->SetPen(*wxRED_PEN);
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 {
100 dc->SetPen(*wxBLACK_PEN);
101 if (m_game->IsFocussed(x, y))
102 dc->SetBrush(*focusedBrush);
103 else
104 dc->SetBrush(*wxLIGHT_GREY_BRUSH);
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 {
111 dc->SetPen(*wxBLACK_PEN);
112 dc->SetBrush(*wxRED_BRUSH);
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");
116 dc->SetTextForeground(*wxBLACK);
117 dc->SetTextBackground(*wxRED);
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 {
124 dc->SetPen(*bluePen);
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 {
133 dc->SetPen(*wxBLACK_PEN);
134 if (m_game->IsFocussed(x, y))
135 dc->SetBrush(*focusedBrush);
136 else if (m_game->IsSelected(x,y))
137 dc->SetBrush(*wxWHITE_BRUSH);
138 else
139 dc->SetBrush(*yellowBrush);
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");
148 dc->SetTextForeground(*wxGREEN);
149 break;
150 case 1:
151 buf = wxT("1");
152 dc->SetTextForeground(*wxBLUE);
153 break;
154 default:
155 buf.Printf(wxT("%d"),digit_value);
156 dc->SetTextForeground(*wxBLACK);
157 break;
158 }
159 dc->GetTextExtent(buf, &chw, &chh);
160 dc->SetTextBackground(*wxWHITE);
161 dc->DrawText( buf,
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 }
165 }
166 dc->SetFont(wxNullFont);
167
168 wxString msg;
169 msg.Printf(wxT("%d bombs, %u marked, %d remaining cells"),
170 m_game->GetNumBombs(), m_game->GetNumMarkedCells(),
171 m_game->GetNumRemainingCells() );
172
173 #if wxUSE_LOG && wxUSE_STATUSBAR
174 wxLogStatus(msg);
175 #else
176 this->GetParent()->SetTitle(msg);
177 #endif
178 }
179
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 {
185 wxClientDC dc(this);
186 DrawField(& dc, xc1, yc1, xc2, yc2);
187 if (m_bmp)
188 {
189 wxMemoryDC memDC;
190 memDC.SelectObject(*m_bmp);
191 DrawField(&memDC, xc1, yc1, xc2, yc2);
192 memDC.SelectObject(wxNullBitmap);
193 }
194 }
195
196 // Called when uncovering a cell.
197 void BombsCanvas::Uncover(int x, int y)
198 {
199 m_game->Unhide(x,y,true);
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++)
221 m_game->Unhide(x,y,false);
222
223 RefreshField(0, 0, gridWidth-1, gridHeight-1);
224 }
225 else if (0 == (m_game->Get(x, y) & BG_MASK))
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 }
244 }
245 }
246
247 // Called when the canvas receives a mouse event.
248 void BombsCanvas::OnMouseEvent(wxMouseEvent& event)
249 {
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() ) )
262 {
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;
272 }
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;
285 }
286 }
287 }
288
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 {
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
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 }