]>
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 wxPen
*whitePen
= wxThePenList
->FindOrCreatePen(*wxWhite
, 1, wxSOLID
);
50 wxPen
*greyPen
= wxThePenList
->FindOrCreatePen(*wxGrey
, 1, wxSOLID
);
51 wxBrush
*whiteBrush
= wxTheBrushList
->FindOrCreateBrush(*wxWhite
, wxSOLID
);
52 wxBrush
*greyBrush
= wxTheBrushList
->FindOrCreateBrush(*wxGrey
, wxSOLID
);
53 wxBrush
*redBrush
= wxTheBrushList
->FindOrCreateBrush(*wxRed
, wxSOLID
);
55 xmax
=field_width
*x_cell
*X_UNIT
;
56 ymax
=field_height
*y_cell
*Y_UNIT
;
59 dc
->SetPen(* blackPen
);
60 for(x
=xc1
; x
<=xc2
; x
++)
61 dc
->DrawLine(x
*x_cell
*X_UNIT
, 0, x
*x_cell
*X_UNIT
, ymax
);
62 for(y
=xc1
; y
<=yc2
; y
++)
63 dc
->DrawLine(0, y
*y_cell
*Y_UNIT
, xmax
, y
*y_cell
*Y_UNIT
);
66 wxFont font
= BOMBS_FONT
;
70 for(x
=xc1
; x
<=xc2
; x
++)
71 for(y
=yc1
; y
<=yc2
; y
++)
72 { if (wxGetApp().Game
.IsMarked(x
,y
))
73 { dc
->SetPen(* blackPen
);
74 dc
->SetBrush(* greyBrush
);
75 dc
->DrawRectangle( x
*x_cell
*X_UNIT
, y
*y_cell
*Y_UNIT
,
76 x_cell
*X_UNIT
+1, y_cell
*Y_UNIT
+1);
78 if (!wxGetApp().Game
.IsHidden(x
,y
) && wxGetApp().Game
.IsBomb(x
,y
))
79 dc
->SetTextForeground(*wxBlue
);
81 dc
->SetTextForeground(*wxRed
);
82 dc
->SetTextBackground(*wxGrey
);
83 dc
->GetTextExtent(buf
, &chw
, &chh
);
85 x
*x_cell
*X_UNIT
+ (x_cell
*X_UNIT
-chw
)/2,
86 y
*y_cell
*Y_UNIT
+ (y_cell
*Y_UNIT
-chh
)/2
88 if (!wxGetApp().Game
.IsHidden(x
,y
) && wxGetApp().Game
.IsBomb(x
,y
))
89 { dc
->SetPen(*redPen
);
90 dc
->DrawLine(x
*x_cell
*X_UNIT
, y
*y_cell
*Y_UNIT
,
91 (x
+1)*x_cell
*X_UNIT
, (y
+1)*y_cell
*Y_UNIT
);
92 dc
->DrawLine(x
*x_cell
*X_UNIT
, (y
+1)*y_cell
*Y_UNIT
,
93 (x
+1)*x_cell
*X_UNIT
, y
*y_cell
*Y_UNIT
);
96 else if (wxGetApp().Game
.IsHidden(x
,y
))
97 { dc
->SetPen(*blackPen
);
98 dc
->SetBrush(*greyBrush
);
99 dc
->DrawRectangle( x
*x_cell
*X_UNIT
, y
*y_cell
*Y_UNIT
,
100 x_cell
*X_UNIT
+1, y_cell
*Y_UNIT
+1);
102 else if (wxGetApp().Game
.IsBomb(x
,y
))
103 { dc
->SetPen(* blackPen
);
104 dc
->SetBrush(* redBrush
);
105 dc
->DrawRectangle( x
*x_cell
*X_UNIT
, y
*y_cell
*Y_UNIT
,
106 x_cell
*X_UNIT
+1, y_cell
*Y_UNIT
+1);
108 dc
->SetTextForeground(* wxBlack
);
109 dc
->SetTextBackground(* wxRed
);
110 dc
->GetTextExtent(buf
, &chw
, &chh
);
112 x
*x_cell
*X_UNIT
+ (x_cell
*X_UNIT
-chw
)/2,
113 y
*y_cell
*Y_UNIT
+ (y_cell
*Y_UNIT
-chh
)/2
115 if (wxGetApp().Game
.IsExploded(x
,y
))
116 { dc
->SetPen(* bluePen
);
117 dc
->DrawLine(x
*x_cell
*X_UNIT
, y
*y_cell
*Y_UNIT
,
118 (x
+1)*x_cell
*X_UNIT
, (y
+1)*y_cell
*Y_UNIT
);
119 dc
->DrawLine(x
*x_cell
*X_UNIT
, (y
+1)*y_cell
*Y_UNIT
,
120 (x
+1)*x_cell
*X_UNIT
, y
*y_cell
*Y_UNIT
);
123 else // Display a digit
124 { dc
->SetPen(* blackPen
);
125 dc
->SetBrush(* whiteBrush
);
126 dc
->DrawRectangle( x
*x_cell
*X_UNIT
, y
*y_cell
*Y_UNIT
,
127 x_cell
*X_UNIT
+1, y_cell
*Y_UNIT
+1);
128 *buf
= (wxGetApp().Game
.Get(x
,y
) & BG_MASK
) + '0';
129 dc
->GetTextExtent(buf
, &chw
, &chh
);
131 { case '0': dc
->SetTextForeground(* wxGreen
); break;
132 case '1': dc
->SetTextForeground(* wxBlue
); break;
133 default: dc
->SetTextForeground(* wxBlack
); break;
135 dc
->SetTextBackground(* wxWhite
);
137 x
*x_cell
*X_UNIT
+ (x_cell
*X_UNIT
-chw
)/2,
138 y
*y_cell
*Y_UNIT
+ (y_cell
*Y_UNIT
-chh
)/2
142 dc
->SetFont(wxNullFont
);
144 if (wxGetApp().BombsFrame
)
146 sprintf(buf
, "%d bombs %d remaining cells",
147 wxGetApp().Game
.GetBombs(), wxGetApp().Game
.GetRemainingCells());
148 wxGetApp().BombsFrame
->SetStatusText(buf
, 0);
152 /*-------- BombCanvasClass::Refresh(xc1, yc1, xc2, yc2) -------------*/
153 /* Refreshes the field image */
154 /* xc1,yc1 etc. are the (inclusive) limits of the area to be drawn, */
155 /* expressed in cells. */
156 /*---------------------------------------------------------------------*/
157 void BombsCanvasClass::Refresh(int xc1
, int yc1
, int xc2
, int yc2
)
160 DrawField(& dc
, xc1
, yc1
, xc2
, yc2
);
163 memDC
.SelectObject(* bmp
);
164 DrawField(&memDC
, xc1
, yc1
, xc2
, yc2
);
165 memDC
.SelectObject(wxNullBitmap
);
169 // Called when the canvas receives a mouse event.
170 void BombsCanvasClass::OnEvent(wxMouseEvent
& event
)
172 event
.Position(&fx
, &fy
);
173 int x
= fx
/(x_cell
*X_UNIT
);
174 int y
= fy
/(y_cell
*Y_UNIT
);
175 if (x
<field_width
&& y
<field_height
)
176 { if ( (event
.RightDown() || (event
.LeftDown() && event
.ShiftDown()))
177 && (wxGetApp().Game
.IsHidden(x
,y
)
178 || wxGetApp().Game
.GetRemainingCells()==0))
179 { wxGetApp().Game
.Mark(x
,y
);
183 else if (event
.LeftDown() && wxGetApp().Game
.IsHidden(x
,y
)
184 && !wxGetApp().Game
.IsMarked(x
,y
))
185 { wxGetApp().Game
.Unhide(x
,y
);
187 if (wxGetApp().Game
.IsBomb(x
,y
) || wxGetApp().Game
.GetRemainingCells()==0)
189 if (!wxGetApp().Game
.IsBomb(x
,y
))
190 { wxMessageBox("Nice! You found all the bombs!", "wxWin Bombs",
191 wxOK
|wxCENTRE
, wxGetApp().BombsFrame
);
193 else // x,y is a bomb
194 { wxGetApp().Game
.Explode(x
, y
);
196 for(x
=0; x
<field_width
; x
++)
197 for(y
=0; y
<field_height
; y
++)
198 wxGetApp().Game
.Unhide(x
,y
);
199 Refresh(0, 0, field_width
-1, field_height
-1);