]>
Commit | Line | Data |
---|---|---|
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 |
39 | IMPLEMENT_APP(BombsApp) |
40 | ||
41 | #ifdef __WXWINCE__ | |
42 | STDAPI_(__int64) CeGetRandomSeed(); | |
43 | #endif | |
025e88c5 JS |
44 | |
45 | // Called to initialize the program | |
0c65afdb | 46 | bool 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 | |
0c65afdb | 56 | m_frame->NewGame(bombsID_EASY); |
025e88c5 | 57 | |
0c65afdb | 58 | return true; |
025e88c5 JS |
59 | } |
60 | ||
0c65afdb DS |
61 | BEGIN_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 |
67 | END_EVENT_TABLE() |
68 | ||
0c65afdb DS |
69 | BombsFrame::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 | 119 | void BombsFrame::OnExit(wxCommandEvent& WXUNUSED(event)) |
025e88c5 | 120 | { |
0c65afdb | 121 | Close(); |
025e88c5 JS |
122 | } |
123 | ||
0c65afdb | 124 | void BombsFrame::NewGame(int level) |
025e88c5 | 125 | { |
0c65afdb DS |
126 | |
127 | int numHorzCells = 20, numVertCells = 20; | |
128 | ||
129 | switch(level) | |
130 | { | |
131 | case bombsID_EASY: | |
132 | numHorzCells = numVertCells = 10; | |
133 | break; | |
134 | ||
135 | case bombsID_MEDIUM: | |
136 | numHorzCells = numVertCells = 15; | |
137 | break; | |
138 | ||
139 | case bombsID_HARD: | |
140 | numHorzCells = 25; numVertCells = 20; | |
141 | break; | |
142 | ||
143 | default : | |
144 | wxFAIL_MSG(wxT("Invalid level")); | |
145 | break; | |
146 | } | |
147 | ||
148 | m_game->Init(numHorzCells, numVertCells); | |
149 | ||
150 | GetMenuBar()->Check(level, true); | |
151 | ||
152 | m_canvas->UpdateGridSize(); | |
153 | SetClientSize(m_canvas->GetGridSizeInPixels()); | |
025e88c5 JS |
154 | } |
155 | ||
0c65afdb | 156 | void BombsFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
025e88c5 | 157 | { |
0c65afdb DS |
158 | wxMessageBox( |
159 | wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"), | |
160 | wxT("About wxBombs") ); | |
025e88c5 JS |
161 | } |
162 | ||
0c65afdb | 163 | void BombsFrame::OnNewEasyGame(wxCommandEvent& WXUNUSED(event)) |
025e88c5 | 164 | { |
0c65afdb | 165 | NewGame(bombsID_EASY); |
025e88c5 JS |
166 | } |
167 | ||
0c65afdb | 168 | void BombsFrame::OnNewMediumGame(wxCommandEvent& WXUNUSED(event)) |
025e88c5 | 169 | { |
0c65afdb | 170 | NewGame(bombsID_MEDIUM); |
025e88c5 JS |
171 | } |
172 | ||
0c65afdb | 173 | void BombsFrame::OnNewHardGame(wxCommandEvent& WXUNUSED(event)) |
025e88c5 | 174 | { |
0c65afdb | 175 | NewGame(bombsID_HARD); |
025e88c5 JS |
176 | } |
177 | ||
0c65afdb DS |
178 | BEGIN_EVENT_TABLE(BombsCanvas, wxPanel) |
179 | EVT_PAINT(BombsCanvas::OnPaint) | |
180 | EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent) | |
181 | EVT_CHAR(BombsCanvas::OnChar) | |
025e88c5 JS |
182 | END_EVENT_TABLE() |
183 | ||
0c65afdb DS |
184 | BombsCanvas::BombsCanvas(wxFrame *parent, BombsGame *game) |
185 | : wxPanel(parent, wxID_ANY) | |
186 | { | |
187 | m_game = game; | |
188 | int sx, sy; | |
189 | wxClientDC dc(this); | |
190 | wxFont font= BOMBS_FONT; | |
191 | dc.SetFont(font); | |
025e88c5 | 192 | |
0c65afdb DS |
193 | long chw, chh; |
194 | wxString buf = wxT("M"); | |
025e88c5 | 195 | |
0c65afdb DS |
196 | dc.GetTextExtent(buf, &chw, &chh); |
197 | dc.SetFont(wxNullFont); | |
025e88c5 | 198 | |
0c65afdb | 199 | dc.SetMapMode(wxMM_METRIC); |
025e88c5 | 200 | |
0c65afdb DS |
201 | int xcm = dc.LogicalToDeviceX(10); |
202 | int ycm = dc.LogicalToDeviceY(10); | |
025e88c5 JS |
203 | // To have a square cell, there must be : |
204 | // sx*ycm == sy*xcm | |
0c65afdb DS |
205 | if (chw*ycm < chh*xcm) |
206 | { | |
207 | sy = chh; | |
208 | sx = chh*xcm/ycm; | |
025e88c5 | 209 | } |
0c65afdb DS |
210 | else |
211 | { | |
212 | sx = chw; | |
213 | sy = chw*ycm/xcm; | |
025e88c5 | 214 | } |
0c65afdb DS |
215 | |
216 | m_cellWidth = (sx+3+X_UNIT)/X_UNIT; | |
217 | m_cellHeight = (sy+3+Y_UNIT)/Y_UNIT; | |
218 | dc.SetMapMode(wxMM_TEXT); | |
219 | m_bmp = NULL; | |
025e88c5 JS |
220 | } |
221 | ||
0c65afdb | 222 | BombsCanvas::~BombsCanvas() |
025e88c5 | 223 | { |
0c65afdb DS |
224 | if (m_bmp) |
225 | { | |
226 | delete m_bmp; | |
227 | m_bmp = NULL; | |
228 | } | |
025e88c5 JS |
229 | } |
230 | ||
231 | // Called when canvas needs to be repainted. | |
0c65afdb | 232 | void BombsCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) |
025e88c5 | 233 | { |
0c65afdb DS |
234 | wxPaintDC dc(this); |
235 | ||
236 | const int numHorzCells = m_game->GetWidth(); | |
237 | const int numVertCells = m_game->GetHeight(); | |
238 | // Insert your drawing code here. | |
239 | if (!m_bmp) | |
240 | { | |
241 | wxSize size = dc.GetSize(); | |
242 | m_bmp = new wxBitmap(size.GetWidth(), size.GetHeight()); | |
243 | if (m_bmp) | |
244 | { | |
245 | wxMemoryDC memDC; | |
246 | memDC.SelectObject(*m_bmp); | |
247 | DrawField(&memDC, 0, 0, numHorzCells-1, numVertCells-1); | |
248 | memDC.SelectObject(wxNullBitmap); | |
025e88c5 JS |
249 | } |
250 | } | |
0c65afdb DS |
251 | |
252 | if (m_bmp) | |
253 | { | |
254 | wxMemoryDC memDC; | |
255 | memDC.SelectObject(*m_bmp); | |
256 | wxSize size = dc.GetSize(); | |
257 | dc.Blit(0, 0, size.GetWidth(), size.GetHeight(), | |
258 | &memDC, 0, 0, wxCOPY); | |
025e88c5 JS |
259 | memDC.SelectObject(wxNullBitmap); |
260 | } | |
0c65afdb DS |
261 | else |
262 | { | |
263 | DrawField(&dc, 0, 0, numHorzCells-1, numVertCells-1); | |
264 | } | |
265 | } | |
266 | ||
267 | void BombsCanvas::UpdateGridSize() | |
268 | { | |
269 | ||
270 | if (m_bmp) | |
271 | { | |
272 | delete m_bmp; | |
273 | m_bmp = NULL; | |
274 | } | |
275 | ||
276 | Refresh(); | |
277 | } | |
278 | ||
279 | wxSize BombsCanvas::GetGridSizeInPixels() const | |
280 | { | |
281 | return wxSize(m_cellWidth*X_UNIT*m_game->GetWidth(), | |
282 | m_cellHeight*Y_UNIT*m_game->GetHeight()); | |
025e88c5 JS |
283 | } |
284 |