]>
git.saurik.com Git - wxWidgets.git/blob - samples/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("BLACK");
40 wxColour
*wxWhite
= wxTheColourDatabase
->FindColour("WHITE");
41 wxColour
*wxRed
= wxTheColourDatabase
->FindColour("RED");
42 wxColour
*wxBlue
= wxTheColourDatabase
->FindColour("BLUE");
43 wxColour
*wxGrey
= wxTheColourDatabase
->FindColour("LIGHT GREY");
44 wxColour
*wxGreen
= wxTheColourDatabase
->FindColour("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
) + '0';
127 dc
->GetTextExtent(buf
, &chw
, &chh
);
129 { case '0': dc
->SetTextForeground(* wxGreen
); break;
130 case '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 sprintf(buf
, "%d bombs %d remaining cells",
145 wxGetApp().Game
.GetBombs(), wxGetApp().Game
.GetRemainingCells());
146 wxGetApp().BombsFrame
->SetStatusText(buf
, 0);
150 /*-------- BombCanvasClass::Refresh(xc1, yc1, xc2, yc2) -------------*/
151 /* Refreshes the field image */
152 /* xc1,yc1 etc. are the (inclusive) limits of the area to be drawn, */
153 /* expressed in cells. */
154 /*---------------------------------------------------------------------*/
155 void BombsCanvasClass::Refresh(int xc1
, int yc1
, int xc2
, int yc2
)
158 DrawField(& dc
, xc1
, yc1
, xc2
, yc2
);
161 memDC
.SelectObject(* bmp
);
162 DrawField(&memDC
, xc1
, yc1
, xc2
, yc2
);
163 memDC
.SelectObject(wxNullBitmap
);
167 // Called when the canvas receives a mouse event.
168 void BombsCanvasClass::OnEvent(wxMouseEvent
& event
)
171 event
.GetPosition(&fx
, &fy
);
172 int x
= fx
/(x_cell
*X_UNIT
);
173 int y
= fy
/(y_cell
*Y_UNIT
);
174 if (x
<field_width
&& y
<field_height
)
175 { if ( (event
.RightDown() || (event
.LeftDown() && event
.ShiftDown()))
176 && (wxGetApp().Game
.IsHidden(x
,y
)
177 || wxGetApp().Game
.GetRemainingCells()==0))
178 { wxGetApp().Game
.Mark(x
,y
);
182 else if (event
.LeftDown() && wxGetApp().Game
.IsHidden(x
,y
)
183 && !wxGetApp().Game
.IsMarked(x
,y
))
184 { wxGetApp().Game
.Unhide(x
,y
);
186 if (wxGetApp().Game
.IsBomb(x
,y
) || wxGetApp().Game
.GetRemainingCells()==0)
188 if (!wxGetApp().Game
.IsBomb(x
,y
))
189 { wxMessageBox("Nice! You found all the bombs!", "wxWin Bombs",
190 wxOK
|wxCENTRE
, wxGetApp().BombsFrame
);
192 else // x,y is a bomb
193 { wxGetApp().Game
.Explode(x
, y
);
195 for(x
=0; x
<field_width
; x
++)
196 for(y
=0; y
<field_height
; y
++)
197 wxGetApp().Game
.Unhide(x
,y
);
198 Refresh(0, 0, field_width
-1, field_height
-1);