]> git.saurik.com Git - wxWidgets.git/blob - demos/bombs/bombs.cpp
fix almost all deprecation warnings
[wxWidgets.git] / demos / bombs / bombs.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: bombs.cpp
3 // Purpose: Bombs game
4 // Author: P. Foggia 1996
5 // Modified by: Wlodzimierz Skiba (ABX) since 2003
6 // Created: 1996
7 // RCS-ID: $Id$
8 // Copyright: (c) 1996 P. Foggia
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 # pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 # include "wx/wx.h"
20 #endif //precompiled headers
21
22 #include "wx/stockitem.h"
23
24 #include "bombs.h"
25
26 #include <stdlib.h>
27
28 #ifndef __WXWINCE__
29 # include <time.h>
30 #endif
31
32 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) \
33 || defined(__WXMAC__) || defined(__WXMGL__)
34 # include "bombs.xpm"
35 #endif
36
37 IMPLEMENT_APP(BombsApp)
38
39 #ifdef __WXWINCE__
40 STDAPI_(__int64) CeGetRandomSeed();
41 #endif
42
43 // Called to initialize the program
44 bool BombsApp::OnInit()
45 {
46 #ifdef __WXWINCE__
47 srand((unsigned) CeGetRandomSeed());
48 #else
49 srand((unsigned) time(NULL));
50 #endif
51
52 m_frame = new BombsFrame(&m_game);
53
54 m_frame->NewGame(bombsID_EASY, false);
55
56 return true;
57 }
58
59 BEGIN_EVENT_TABLE(BombsFrame, wxFrame)
60 EVT_MENU(wxID_NEW, BombsFrame::OnNewGame)
61 EVT_MENU(bombsID_EASY, BombsFrame::OnEasyGame)
62 EVT_MENU(bombsID_MEDIUM, BombsFrame::OnMediumGame)
63 EVT_MENU(bombsID_HARD, BombsFrame::OnHardGame)
64 EVT_MENU(bombsID_EASYCORNER, BombsFrame::OnEasyCorner)
65 EVT_MENU(wxID_EXIT, BombsFrame::OnExit)
66 EVT_MENU(wxID_ABOUT, BombsFrame::OnAbout)
67 END_EVENT_TABLE()
68
69 BombsFrame::BombsFrame(BombsGame *game)
70 : wxFrame(NULL, wxID_ANY, wxT("wxBombs"), wxDefaultPosition,
71 wxSize(300, 300), wxDEFAULT_DIALOG_STYLE|wxMINIMIZE_BOX)
72 {
73 m_game = game;
74 m_easyCorner = false;
75 m_lastLevel = bombsID_EASY;
76
77 SetIcon(wxICON(bombs));
78
79 #if wxUSE_STATUSBAR
80 CreateStatusBar();
81 #endif
82
83 // Create a menu bar for the frame
84 wxMenuBar *menuBar = new wxMenuBar;
85 wxMenu *menuFile = new wxMenu;
86 wxMenu *menuLevel = new wxMenu;
87 menuLevel->AppendRadioItem(bombsID_EASY, wxT("&Easy (10x10)\tCtrl-1"));
88 menuLevel->AppendRadioItem(bombsID_MEDIUM, wxT("&Medium (15x15)\tCtrl-2"));
89 menuLevel->AppendRadioItem(bombsID_HARD, wxT("&Hard (25x20)\tCtrl-3"));
90
91 menuFile->Append(wxID_NEW, wxT("&New game\tCtrl-N"));
92 menuFile->Append(bombsID_LEVEL, wxT("&Level"),menuLevel, wxT("Starts a new game"));
93 menuFile->AppendCheckItem(bombsID_EASYCORNER, wxT("&Easy corner"));
94
95 menuFile->AppendSeparator();
96 menuFile->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT), wxT("Quits the application"));
97
98 menuBar->Append(menuFile, wxT("&File"));
99
100
101 wxMenu *menuHelp = new wxMenu;
102 menuHelp->Append(wxID_ABOUT, wxT("&About"),
103 wxT("Displays the program information") );
104
105 menuBar->Append(menuHelp, wxT("&Help"));
106
107 SetMenuBar(menuBar);
108
109 // Create child subwindows.
110 m_canvas = new BombsCanvas(this, m_game);
111
112 // Ensure the subwindows get resized o.k.
113 // OnSize(width, height);
114
115 // Centre frame on the screen.
116 Centre(wxBOTH);
117
118 // Show the frame.
119 Show();
120 }
121
122 void BombsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
123 {
124 Close();
125 }
126
127 void BombsFrame::NewGame(int level, bool query)
128 {
129 if(query)
130 {
131 int ok = wxMessageBox(
132 wxT("Start new game regardless previous board?"),
133 wxT("Confirm"),
134 wxYES_NO | wxICON_QUESTION,
135 this
136 );
137 if(ok!=wxYES)return;
138 }
139
140 int numHorzCells = 20, numVertCells = 20;
141 m_lastLevel = level;
142
143 switch(level)
144 {
145 case bombsID_EASY:
146 numHorzCells = numVertCells = 10;
147 break;
148
149 case bombsID_MEDIUM:
150 numHorzCells = numVertCells = 15;
151 break;
152
153 case bombsID_HARD:
154 numHorzCells = 25; numVertCells = 20;
155 break;
156
157 default :
158 wxFAIL_MSG(wxT("Invalid level"));
159 break;
160 }
161
162 m_game->Init(numHorzCells, numVertCells, m_easyCorner);
163
164 GetMenuBar()->Check(level, true);
165
166 m_canvas->UpdateGridSize();
167 SetClientSize(m_canvas->GetGridSizeInPixels());
168 }
169
170 void BombsFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
171 {
172 wxMessageBox(
173 wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"),
174 wxT("About wxBombs") );
175 }
176
177 void BombsFrame::OnNewGame(wxCommandEvent& WXUNUSED(event))
178 {
179 NewGame(m_lastLevel, true);
180 }
181
182 void BombsFrame::OnEasyGame(wxCommandEvent& WXUNUSED(event))
183 {
184 NewGame(bombsID_EASY, true);
185 }
186
187 void BombsFrame::OnMediumGame(wxCommandEvent& WXUNUSED(event))
188 {
189 NewGame(bombsID_MEDIUM, true);
190 }
191
192 void BombsFrame::OnHardGame(wxCommandEvent& WXUNUSED(event))
193 {
194 NewGame(bombsID_HARD, true);
195 }
196
197 void BombsFrame::OnEasyCorner(wxCommandEvent& WXUNUSED(event))
198 {
199 wxString msg;
200 if(m_easyCorner)
201 msg = wxT("enable");
202 else
203 msg = wxT("disable");
204
205 msg = wxT("Do you really want to ") + msg + wxT(" having\ntop left corner always empty for easier start?");
206
207 int ok = wxMessageBox(
208 msg,
209 wxT("Confirm"),
210 wxYES_NO | wxICON_QUESTION,
211 this
212 );
213
214 if(ok!=wxYES)return;
215
216 m_easyCorner = !m_easyCorner;
217
218 NewGame(m_lastLevel, true);
219 }
220
221 BEGIN_EVENT_TABLE(BombsCanvas, wxPanel)
222 EVT_PAINT(BombsCanvas::OnPaint)
223 EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent)
224 EVT_CHAR(BombsCanvas::OnChar)
225 END_EVENT_TABLE()
226
227 BombsCanvas::BombsCanvas(wxFrame *parent, BombsGame *game)
228 : wxPanel(parent, wxID_ANY)
229 {
230 m_game = game;
231 int sx, sy;
232 wxClientDC dc(this);
233 wxFont font= BOMBS_FONT;
234 dc.SetFont(font);
235
236 wxCoord chw, chh;
237 wxString buf = wxT("M");
238
239 dc.GetTextExtent(buf, &chw, &chh);
240 dc.SetFont(wxNullFont);
241
242 dc.SetMapMode(wxMM_METRIC);
243
244 int xcm = dc.LogicalToDeviceX(10);
245 int ycm = dc.LogicalToDeviceY(10);
246 // To have a square cell, there must be :
247 // sx*ycm == sy*xcm
248 if (chw*ycm < chh*xcm)
249 {
250 sy = chh;
251 sx = chh*xcm/ycm;
252 }
253 else
254 {
255 sx = chw;
256 sy = chw*ycm/xcm;
257 }
258
259 m_cellWidth = (sx+3+X_UNIT)/X_UNIT;
260 m_cellHeight = (sy+3+Y_UNIT)/Y_UNIT;
261 dc.SetMapMode(wxMM_TEXT);
262 m_bmp = NULL;
263 }
264
265 BombsCanvas::~BombsCanvas()
266 {
267 if (m_bmp)
268 {
269 delete m_bmp;
270 m_bmp = NULL;
271 }
272 }
273
274 // Called when canvas needs to be repainted.
275 void BombsCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
276 {
277 wxPaintDC dc(this);
278
279 const int numHorzCells = m_game->GetWidth();
280 const int numVertCells = m_game->GetHeight();
281 // Insert your drawing code here.
282 if (!m_bmp)
283 {
284 wxSize size = dc.GetSize();
285 m_bmp = new wxBitmap(size.GetWidth(), size.GetHeight());
286 if (m_bmp)
287 {
288 wxMemoryDC memDC;
289 memDC.SelectObject(*m_bmp);
290 DrawField(&memDC, 0, 0, numHorzCells-1, numVertCells-1);
291 memDC.SelectObject(wxNullBitmap);
292 }
293 }
294
295 if (m_bmp)
296 {
297 wxMemoryDC memDC;
298 memDC.SelectObject(*m_bmp);
299 wxSize size = dc.GetSize();
300 dc.Blit(0, 0, size.GetWidth(), size.GetHeight(),
301 &memDC, 0, 0, wxCOPY);
302 memDC.SelectObject(wxNullBitmap);
303 }
304 else
305 {
306 DrawField(&dc, 0, 0, numHorzCells-1, numVertCells-1);
307 }
308 }
309
310 void BombsCanvas::UpdateGridSize()
311 {
312
313 if (m_bmp)
314 {
315 delete m_bmp;
316 m_bmp = NULL;
317 }
318 SetSize(GetGridSizeInPixels());
319 Refresh();
320 }
321
322 wxSize BombsCanvas::GetGridSizeInPixels() const
323 {
324 return wxSize(m_cellWidth*X_UNIT*m_game->GetWidth(),
325 m_cellHeight*Y_UNIT*m_game->GetHeight());
326 }
327