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