]> git.saurik.com Git - wxWidgets.git/blob - demos/bombs/bombs1.cpp
Typo correction
[wxWidgets.git] / demos / bombs / bombs1.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: bombs1.cpp
3 // Purpose: Bombs game
4 // Author: P. Foggia 1996
5 // Modified by:
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 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif //precompiled headers
26
27 #include "bombs.h"
28
29 /*-------- BombCanvasClass::DrawField(dc, xc1, yc1, xc2, yc2) -------*/
30 /* Draws the field on the device context dc */
31 /* xc1,yc1 etc. are the (inclusive) limits of the area to be drawn, */
32 /* expressed in cells. */
33 /*---------------------------------------------------------------------*/
34 void BombsCanvasClass::DrawField(wxDC *dc, int xc1, int yc1, int xc2, int yc2)
35 { int x,y,xmax,ymax;
36 wxChar buf[2];
37 long chw, chh;
38
39 wxColour *wxBlack = wxTheColourDatabase->FindColour(_T("BLACK"));
40 wxColour *wxWhite = wxTheColourDatabase->FindColour(_T("WHITE"));
41 wxColour *wxRed = wxTheColourDatabase->FindColour(_T("RED"));
42 wxColour *wxBlue = wxTheColourDatabase->FindColour(_T("BLUE"));
43 wxColour *wxGrey = wxTheColourDatabase->FindColour(_T("LIGHT GREY"));
44 wxColour *wxGreen = wxTheColourDatabase->FindColour(_T("GREEN"));
45
46 wxPen *blackPen = wxThePenList->FindOrCreatePen(*wxBlack, 1, wxSOLID);
47 wxPen *redPen = wxThePenList->FindOrCreatePen(*wxRed, 1, wxSOLID);
48 wxPen *bluePen = wxThePenList->FindOrCreatePen(*wxBlue, 1, wxSOLID);
49 wxBrush *whiteBrush = wxTheBrushList->FindOrCreateBrush(*wxWhite, wxSOLID);
50 wxBrush *greyBrush = wxTheBrushList->FindOrCreateBrush(*wxGrey, wxSOLID);
51 wxBrush *redBrush = wxTheBrushList->FindOrCreateBrush(*wxRed, wxSOLID);
52
53 xmax=field_width*x_cell*X_UNIT;
54 ymax=field_height*y_cell*Y_UNIT;
55
56
57 dc->SetPen(* blackPen);
58 for(x=xc1; x<=xc2; x++)
59 dc->DrawLine(x*x_cell*X_UNIT, 0, x*x_cell*X_UNIT, ymax);
60 for(y=xc1; y<=yc2; y++)
61 dc->DrawLine(0, y*y_cell*Y_UNIT, xmax, y*y_cell*Y_UNIT);
62
63
64 wxFont font= BOMBS_FONT;
65 dc->SetFont(font);
66
67 buf[1]=_T('\0');
68 for(x=xc1; x<=xc2; x++)
69 for(y=yc1; y<=yc2; y++)
70 { if (wxGetApp().Game.IsMarked(x,y))
71 { dc->SetPen(* blackPen);
72 dc->SetBrush(* greyBrush);
73 dc->DrawRectangle( x*x_cell*X_UNIT, y*y_cell*Y_UNIT,
74 x_cell*X_UNIT+1, y_cell*Y_UNIT+1);
75 *buf=_T('M');
76 if (!wxGetApp().Game.IsHidden(x,y) && wxGetApp().Game.IsBomb(x,y))
77 dc->SetTextForeground(*wxBlue);
78 else
79 dc->SetTextForeground(*wxRed);
80 dc->SetTextBackground(*wxGrey);
81 dc->GetTextExtent(buf, &chw, &chh);
82 dc->DrawText( buf,
83 x*x_cell*X_UNIT + (x_cell*X_UNIT-chw)/2,
84 y*y_cell*Y_UNIT + (y_cell*Y_UNIT-chh)/2
85 );
86 if (!wxGetApp().Game.IsHidden(x,y) && wxGetApp().Game.IsBomb(x,y))
87 { dc->SetPen(*redPen);
88 dc->DrawLine(x*x_cell*X_UNIT, y*y_cell*Y_UNIT,
89 (x+1)*x_cell*X_UNIT, (y+1)*y_cell*Y_UNIT);
90 dc->DrawLine(x*x_cell*X_UNIT, (y+1)*y_cell*Y_UNIT,
91 (x+1)*x_cell*X_UNIT, y*y_cell*Y_UNIT);
92 }
93 }
94 else if (wxGetApp().Game.IsHidden(x,y))
95 { dc->SetPen(*blackPen);
96 dc->SetBrush(*greyBrush);
97 dc->DrawRectangle( x*x_cell*X_UNIT, y*y_cell*Y_UNIT,
98 x_cell*X_UNIT+1, y_cell*Y_UNIT+1);
99 }
100 else if (wxGetApp().Game.IsBomb(x,y))
101 { dc->SetPen(* blackPen);
102 dc->SetBrush(* redBrush);
103 dc->DrawRectangle( x*x_cell*X_UNIT, y*y_cell*Y_UNIT,
104 x_cell*X_UNIT+1, y_cell*Y_UNIT+1);
105 *buf=_T('B');
106 dc->SetTextForeground(* wxBlack);
107 dc->SetTextBackground(* wxRed);
108 dc->GetTextExtent(buf, &chw, &chh);
109 dc->DrawText( buf,
110 x*x_cell*X_UNIT + (x_cell*X_UNIT-chw)/2,
111 y*y_cell*Y_UNIT + (y_cell*Y_UNIT-chh)/2
112 );
113 if (wxGetApp().Game.IsExploded(x,y))
114 { dc->SetPen(* bluePen);
115 dc->DrawLine(x*x_cell*X_UNIT, y*y_cell*Y_UNIT,
116 (x+1)*x_cell*X_UNIT, (y+1)*y_cell*Y_UNIT);
117 dc->DrawLine(x*x_cell*X_UNIT, (y+1)*y_cell*Y_UNIT,
118 (x+1)*x_cell*X_UNIT, y*y_cell*Y_UNIT);
119 }
120 }
121 else // Display a digit
122 { dc->SetPen(* blackPen);
123 dc->SetBrush(* whiteBrush);
124 dc->DrawRectangle( x*x_cell*X_UNIT, y*y_cell*Y_UNIT,
125 x_cell*X_UNIT+1, y_cell*Y_UNIT+1);
126 *buf = (wxGetApp().Game.Get(x,y) & BG_MASK) + _T('0');
127 dc->GetTextExtent(buf, &chw, &chh);
128 switch(*buf)
129 { case _T('0'): dc->SetTextForeground(* wxGreen); break;
130 case _T('1'): dc->SetTextForeground(* wxBlue); break;
131 default: dc->SetTextForeground(* wxBlack); break;
132 }
133 dc->SetTextBackground(* wxWhite);
134 dc->DrawText( buf,
135 x*x_cell*X_UNIT + (x_cell*X_UNIT-chw)/2,
136 y*y_cell*Y_UNIT + (y_cell*Y_UNIT-chh)/2
137 );
138 }
139 }
140 dc->SetFont(wxNullFont);
141
142 if (wxGetApp().BombsFrame)
143 { wxString buf;
144 buf.Printf(_T("%d bombs %d remaining cells"),
145 wxGetApp().Game.GetBombs(),
146 wxGetApp().Game.GetRemainingCells());
147 wxGetApp().BombsFrame->SetStatusText(buf, 0);
148 }
149 }
150
151 /*-------- BombCanvasClass::Refresh(xc1, yc1, xc2, yc2) -------------*/
152 /* Refreshes the field image */
153 /* xc1,yc1 etc. are the (inclusive) limits of the area to be drawn, */
154 /* expressed in cells. */
155 /*---------------------------------------------------------------------*/
156 void BombsCanvasClass::Refresh(int xc1, int yc1, int xc2, int yc2)
157 {
158 wxClientDC dc(this);
159 DrawField(& dc, xc1, yc1, xc2, yc2);
160 if (bmp)
161 { wxMemoryDC memDC;
162 memDC.SelectObject(* bmp);
163 DrawField(&memDC, xc1, yc1, xc2, yc2);
164 memDC.SelectObject(wxNullBitmap);
165 }
166 }
167
168 // Called when uncovering a cell.
169 void BombsCanvasClass::Uncover(int x, int y)
170 {
171 wxGetApp().Game.Unhide(x,y);
172 Refresh(x, y, x, y);
173 if (wxGetApp().Game.IsBomb(x,y) || wxGetApp().Game.GetRemainingCells()==0)
174 { wxBell();
175 if (!wxGetApp().Game.IsBomb(x,y))
176 { wxMessageBox(_T("Nice! You found all the bombs!"), _T("wxWin Bombs"),
177 wxOK|wxCENTRE, wxGetApp().BombsFrame);
178 }
179 else // x,y is a bomb
180 { wxGetApp().Game.Explode(x, y);
181 }
182 for(x=0; x<field_width; x++)
183 for(y=0; y<field_height; y++)
184 wxGetApp().Game.Unhide(x,y);
185 Refresh(0, 0, field_width-1, field_height-1);
186 }
187 else if (!wxGetApp().Game.Get(x, y))
188 { int left = ( x > 0 ) ? x-1 : 0;
189 int right = ( x < wxGetApp().Game.GetWidth() - 1 )?
190 x+1 : wxGetApp().Game.GetWidth() - 1;
191 int top = ( y > 0 ) ? y-1 : 0;
192 int bottom = ( y < wxGetApp().Game.GetHeight() - 1 )?
193 y+1 : wxGetApp().Game.GetHeight() - 1;
194 int i,j;
195 for (j = top; j <= bottom; j++)
196 for (i=left; i <= right; i++)
197 if ((i != x || j != y) && wxGetApp().Game.IsHidden(i,j)
198 && !wxGetApp().Game.IsMarked(i,j))
199 Uncover(i,j);
200 }
201 }
202
203 // Called when the canvas receives a mouse event.
204 void BombsCanvasClass::OnEvent(wxMouseEvent& event)
205 {
206 wxCoord fx, fy;
207 event.GetPosition(&fx, &fy);
208 int x = fx/(x_cell*X_UNIT);
209 int y = fy/(y_cell*Y_UNIT);
210 if (x<field_width && y<field_height)
211 { if ( (event.RightDown() || (event.LeftDown() && event.ShiftDown()))
212 && (wxGetApp().Game.IsHidden(x,y)
213 || wxGetApp().Game.GetRemainingCells()==0))
214 { wxGetApp().Game.Mark(x,y);
215 Refresh(x, y, x, y);
216 return;
217 }
218 else if (event.LeftDown() && wxGetApp().Game.IsHidden(x,y)
219 && !wxGetApp().Game.IsMarked(x,y))
220 { Uncover(x,y);
221 return;
222 }
223 }
224 }
225