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