]> git.saurik.com Git - wxWidgets.git/blame - demos/bombs/bombs.cpp
added newline at end of file
[wxWidgets.git] / demos / bombs / bombs.cpp
CommitLineData
025e88c5
JS
1///////////////////////////////////////////////////////////////////////////////
2// Name: bombs.cpp
3// Purpose: Bombs game
4// Author: P. Foggia 1996
c8059953 5// Modified by: Wlodzimierz Skiba (ABX) since 2003
025e88c5
JS
6// Created: 1996
7// RCS-ID: $Id$
8// Copyright: (c) 1996 P. Foggia
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
025e88c5
JS
12#include "wx/wxprec.h"
13
0c65afdb
DS
14#ifdef __BORLANDC__
15# pragma hdrstop
16#endif
17
025e88c5 18#ifndef WX_PRECOMP
0c65afdb 19# include "wx/wx.h"
025e88c5
JS
20#endif //precompiled headers
21
6759ff7d
WS
22#include "wx/stockitem.h"
23
025e88c5
JS
24#include "bombs.h"
25
025e88c5
JS
26#include <stdlib.h>
27
0c65afdb
DS
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"
025e88c5
JS
35#endif
36
0c65afdb
DS
37IMPLEMENT_APP(BombsApp)
38
39#ifdef __WXWINCE__
40 STDAPI_(__int64) CeGetRandomSeed();
41#endif
025e88c5
JS
42
43// Called to initialize the program
0c65afdb 44bool BombsApp::OnInit()
025e88c5 45{
0c65afdb
DS
46#ifdef __WXWINCE__
47 srand((unsigned) CeGetRandomSeed());
48#else
49 srand((unsigned) time(NULL));
50#endif
025e88c5 51
0c65afdb 52 m_frame = new BombsFrame(&m_game);
025e88c5 53
23290a8c 54 m_frame->NewGame(bombsID_EASY, false);
025e88c5 55
0c65afdb 56 return true;
025e88c5
JS
57}
58
0c65afdb 59BEGIN_EVENT_TABLE(BombsFrame, wxFrame)
c8059953
WS
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)
43c3922b
WS
64 EVT_MENU(bombsID_EASYCORNER, BombsFrame::OnEasyCorner)
65 EVT_MENU(wxID_EXIT, BombsFrame::OnExit)
66 EVT_MENU(wxID_ABOUT, BombsFrame::OnAbout)
025e88c5
JS
67END_EVENT_TABLE()
68
0c65afdb
DS
69BombsFrame::BombsFrame(BombsGame *game)
70 : wxFrame(NULL, wxID_ANY, wxT("wxBombs"), wxDefaultPosition,
71 wxSize(300, 300), wxDEFAULT_DIALOG_STYLE|wxMINIMIZE_BOX)
025e88c5 72{
0c65afdb 73 m_game = game;
43c3922b
WS
74 m_easyCorner = false;
75 m_lastLevel = bombsID_EASY;
025e88c5 76
0c65afdb 77 SetIcon(wxICON(bombs));
025e88c5 78
0c65afdb
DS
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
c8059953
WS
91 menuFile->Append(wxID_NEW, wxT("&New game\tCtrl-N"));
92 menuFile->Append(bombsID_LEVEL, wxT("&Level"),menuLevel, wxT("Starts a new game"));
43c3922b 93 menuFile->AppendCheckItem(bombsID_EASYCORNER, wxT("&Easy corner"));
0c65afdb
DS
94
95 menuFile->AppendSeparator();
6759ff7d 96 menuFile->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT), wxT("Quits the application"));
0c65afdb
DS
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();
025e88c5
JS
120}
121
0c65afdb 122void BombsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
025e88c5 123{
0c65afdb 124 Close();
025e88c5
JS
125}
126
23290a8c 127void BombsFrame::NewGame(int level, bool query)
025e88c5 128{
23290a8c
JS
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 }
0c65afdb
DS
139
140 int numHorzCells = 20, numVertCells = 20;
43c3922b 141 m_lastLevel = level;
0c65afdb
DS
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
43c3922b 162 m_game->Init(numHorzCells, numVertCells, m_easyCorner);
0c65afdb
DS
163
164 GetMenuBar()->Check(level, true);
165
166 m_canvas->UpdateGridSize();
167 SetClientSize(m_canvas->GetGridSizeInPixels());
025e88c5
JS
168}
169
0c65afdb 170void BombsFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
025e88c5 171{
0c65afdb
DS
172 wxMessageBox(
173 wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"),
174 wxT("About wxBombs") );
025e88c5
JS
175}
176
c8059953
WS
177void BombsFrame::OnNewGame(wxCommandEvent& WXUNUSED(event))
178{
179 NewGame(m_lastLevel, true);
180}
181
182void BombsFrame::OnEasyGame(wxCommandEvent& WXUNUSED(event))
025e88c5 183{
23290a8c 184 NewGame(bombsID_EASY, true);
025e88c5
JS
185}
186
c8059953 187void BombsFrame::OnMediumGame(wxCommandEvent& WXUNUSED(event))
025e88c5 188{
23290a8c 189 NewGame(bombsID_MEDIUM, true);
025e88c5
JS
190}
191
c8059953 192void BombsFrame::OnHardGame(wxCommandEvent& WXUNUSED(event))
025e88c5 193{
23290a8c 194 NewGame(bombsID_HARD, true);
025e88c5
JS
195}
196
43c3922b
WS
197void 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
0c65afdb
DS
221BEGIN_EVENT_TABLE(BombsCanvas, wxPanel)
222 EVT_PAINT(BombsCanvas::OnPaint)
223 EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent)
224 EVT_CHAR(BombsCanvas::OnChar)
025e88c5
JS
225END_EVENT_TABLE()
226
0c65afdb
DS
227BombsCanvas::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);
025e88c5 235
0c65afdb
DS
236 long chw, chh;
237 wxString buf = wxT("M");
025e88c5 238
0c65afdb
DS
239 dc.GetTextExtent(buf, &chw, &chh);
240 dc.SetFont(wxNullFont);
025e88c5 241
0c65afdb 242 dc.SetMapMode(wxMM_METRIC);
025e88c5 243
0c65afdb
DS
244 int xcm = dc.LogicalToDeviceX(10);
245 int ycm = dc.LogicalToDeviceY(10);
025e88c5
JS
246 // To have a square cell, there must be :
247 // sx*ycm == sy*xcm
0c65afdb
DS
248 if (chw*ycm < chh*xcm)
249 {
250 sy = chh;
251 sx = chh*xcm/ycm;
025e88c5 252 }
0c65afdb
DS
253 else
254 {
255 sx = chw;
256 sy = chw*ycm/xcm;
025e88c5 257 }
0c65afdb
DS
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;
025e88c5
JS
263}
264
0c65afdb 265BombsCanvas::~BombsCanvas()
025e88c5 266{
0c65afdb
DS
267 if (m_bmp)
268 {
269 delete m_bmp;
270 m_bmp = NULL;
271 }
025e88c5
JS
272}
273
274// Called when canvas needs to be repainted.
0c65afdb 275void BombsCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
025e88c5 276{
0c65afdb
DS
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);
025e88c5
JS
292 }
293 }
0c65afdb
DS
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);
025e88c5
JS
302 memDC.SelectObject(wxNullBitmap);
303 }
0c65afdb
DS
304 else
305 {
306 DrawField(&dc, 0, 0, numHorzCells-1, numVertCells-1);
307 }
308}
309
310void BombsCanvas::UpdateGridSize()
311{
312
313 if (m_bmp)
314 {
315 delete m_bmp;
316 m_bmp = NULL;
317 }
d2fc28b1 318 SetSize(GetGridSizeInPixels());
0c65afdb
DS
319 Refresh();
320}
321
322wxSize BombsCanvas::GetGridSizeInPixels() const
323{
324 return wxSize(m_cellWidth*X_UNIT*m_game->GetWidth(),
325 m_cellHeight*Y_UNIT*m_game->GetHeight());
025e88c5
JS
326}
327