]>
git.saurik.com Git - wxWidgets.git/blob - demos/bombs/bombs1.cpp
1 ///////////////////////////////////////////////////////////////////////////////
4 // Author: P. Foggia 1996
8 // Copyright: (c) 1996 P. Foggia
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 * implementation of the methods DrawField and OnEvent of the
18 #pragma implementation
21 #include "wx/wxprec.h"
25 #endif //precompiled headers
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
)
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"));
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
);
53 xmax
=field_width
*x_cell
*X_UNIT
;
54 ymax
=field_height
*y_cell
*Y_UNIT
;
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
);
64 wxFont font
= BOMBS_FONT
;
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);
76 if (!wxGetApp().Game
.IsHidden(x
,y
) && wxGetApp().Game
.IsBomb(x
,y
))
77 dc
->SetTextForeground(*wxBlue
);
79 dc
->SetTextForeground(*wxRed
);
80 dc
->SetTextBackground(*wxGrey
);
81 dc
->GetTextExtent(buf
, &chw
, &chh
);
83 x
*x_cell
*X_UNIT
+ (x_cell
*X_UNIT
-chw
)/2,
84 y
*y_cell
*Y_UNIT
+ (y_cell
*Y_UNIT
-chh
)/2
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
);
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);
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);
106 dc
->SetTextForeground(* wxBlack
);
107 dc
->SetTextBackground(* wxRed
);
108 dc
->GetTextExtent(buf
, &chw
, &chh
);
110 x
*x_cell
*X_UNIT
+ (x_cell
*X_UNIT
-chw
)/2,
111 y
*y_cell
*Y_UNIT
+ (y_cell
*Y_UNIT
-chh
)/2
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
);
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
);
129 { case _T('0'): dc
->SetTextForeground(* wxGreen
); break;
130 case _T('1'): dc
->SetTextForeground(* wxBlue
); break;
131 default: dc
->SetTextForeground(* wxBlack
); break;
133 dc
->SetTextBackground(* wxWhite
);
135 x
*x_cell
*X_UNIT
+ (x_cell
*X_UNIT
-chw
)/2,
136 y
*y_cell
*Y_UNIT
+ (y_cell
*Y_UNIT
-chh
)/2
140 dc
->SetFont(wxNullFont
);
142 if (wxGetApp().BombsFrame
)
144 buf
.Printf(_T("%d bombs %d remaining cells"),
145 wxGetApp().Game
.GetBombs(),
146 wxGetApp().Game
.GetRemainingCells());
147 wxGetApp().BombsFrame
->SetStatusText(buf
, 0);
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
)
159 DrawField(& dc
, xc1
, yc1
, xc2
, yc2
);
162 memDC
.SelectObject(* bmp
);
163 DrawField(&memDC
, xc1
, yc1
, xc2
, yc2
);
164 memDC
.SelectObject(wxNullBitmap
);
168 // Called when uncovering a cell.
169 void BombsCanvasClass::Uncover(int x
, int y
)
171 wxGetApp().Game
.Unhide(x
,y
);
173 if (wxGetApp().Game
.IsBomb(x
,y
) || wxGetApp().Game
.GetRemainingCells()==0)
175 if (!wxGetApp().Game
.IsBomb(x
,y
))
176 { wxMessageBox(_T("Nice! You found all the bombs!"), _T("wxWin Bombs"),
177 wxOK
|wxCENTRE
, wxGetApp().BombsFrame
);
179 else // x,y is a bomb
180 { wxGetApp().Game
.Explode(x
, y
);
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);
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;
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
))
203 // Called when the canvas receives a mouse event.
204 void BombsCanvasClass::OnEvent(wxMouseEvent
& event
)
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
);
218 else if (event
.LeftDown() && wxGetApp().Game
.IsHidden(x
,y
)
219 && !wxGetApp().Game
.IsMarked(x
,y
))