]> git.saurik.com Git - wxWidgets.git/blob - samples/bombs/bombs1.cpp
Rmoved more wxprop files
[wxWidgets.git] / samples / 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 char buf[2];
37 long chw, chh;
38
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");
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 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);
54
55 xmax=field_width*x_cell*X_UNIT;
56 ymax=field_height*y_cell*Y_UNIT;
57
58
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);
64
65
66 wxFont font= BOMBS_FONT;
67 dc->SetFont(font);
68
69 buf[1]='\0';
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);
77 *buf='M';
78 if (!wxGetApp().Game.IsHidden(x,y) && wxGetApp().Game.IsBomb(x,y))
79 dc->SetTextForeground(*wxBlue);
80 else
81 dc->SetTextForeground(*wxRed);
82 dc->SetTextBackground(*wxGrey);
83 dc->GetTextExtent(buf, &chw, &chh);
84 dc->DrawText( buf,
85 x*x_cell*X_UNIT + (x_cell*X_UNIT-chw)/2,
86 y*y_cell*Y_UNIT + (y_cell*Y_UNIT-chh)/2
87 );
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);
94 }
95 }
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);
101 }
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);
107 *buf='B';
108 dc->SetTextForeground(* wxBlack);
109 dc->SetTextBackground(* wxRed);
110 dc->GetTextExtent(buf, &chw, &chh);
111 dc->DrawText( buf,
112 x*x_cell*X_UNIT + (x_cell*X_UNIT-chw)/2,
113 y*y_cell*Y_UNIT + (y_cell*Y_UNIT-chh)/2
114 );
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);
121 }
122 }
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);
130 switch(*buf)
131 { case '0': dc->SetTextForeground(* wxGreen); break;
132 case '1': dc->SetTextForeground(* wxBlue); break;
133 default: dc->SetTextForeground(* wxBlack); break;
134 }
135 dc->SetTextBackground(* wxWhite);
136 dc->DrawText( buf,
137 x*x_cell*X_UNIT + (x_cell*X_UNIT-chw)/2,
138 y*y_cell*Y_UNIT + (y_cell*Y_UNIT-chh)/2
139 );
140 }
141 }
142 dc->SetFont(wxNullFont);
143
144 if (wxGetApp().BombsFrame)
145 { char buf[80];
146 sprintf(buf, "%d bombs %d remaining cells",
147 wxGetApp().Game.GetBombs(), wxGetApp().Game.GetRemainingCells());
148 wxGetApp().BombsFrame->SetStatusText(buf, 0);
149 }
150 }
151
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)
158 {
159 wxClientDC dc(this);
160 DrawField(& dc, xc1, yc1, xc2, yc2);
161 if (bmp)
162 { wxMemoryDC memDC;
163 memDC.SelectObject(* bmp);
164 DrawField(&memDC, xc1, yc1, xc2, yc2);
165 memDC.SelectObject(wxNullBitmap);
166 }
167 }
168
169 // Called when the canvas receives a mouse event.
170 void BombsCanvasClass::OnEvent(wxMouseEvent& event)
171 { float fx, fy;
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);
180 Refresh(x, y, x, y);
181 return;
182 }
183 else if (event.LeftDown() && wxGetApp().Game.IsHidden(x,y)
184 && !wxGetApp().Game.IsMarked(x,y))
185 { wxGetApp().Game.Unhide(x,y);
186 Refresh(x, y, x, y);
187 if (wxGetApp().Game.IsBomb(x,y) || wxGetApp().Game.GetRemainingCells()==0)
188 { wxBell();
189 if (!wxGetApp().Game.IsBomb(x,y))
190 { wxMessageBox("Nice! You found all the bombs!", "wxWin Bombs",
191 wxOK|wxCENTRE, wxGetApp().BombsFrame);
192 }
193 else // x,y is a bomb
194 { wxGetApp().Game.Explode(x, y);
195 }
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);
200 }
201 return;
202 }
203 }
204 }
205