]>
git.saurik.com Git - wxWidgets.git/blob - demos/bombs/bombs.cpp
1 ///////////////////////////////////////////////////////////////////////////////
4 // Author: P. Foggia 1996
5 // Modified by: Wlodzimierz Skiba (ABX) since 2003
8 // Copyright: (c) 1996 P. Foggia
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 # pragma implementation
16 #include "wx/wxprec.h"
24 #endif //precompiled headers
26 #include "wx/stockitem.h"
36 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) \
37 || defined(__WXMAC__) || defined(__WXMGL__)
41 IMPLEMENT_APP(BombsApp
)
44 STDAPI_(__int64
) CeGetRandomSeed();
47 // Called to initialize the program
48 bool BombsApp::OnInit()
51 srand((unsigned) CeGetRandomSeed());
53 srand((unsigned) time(NULL
));
56 m_frame
= new BombsFrame(&m_game
);
58 m_frame
->NewGame(bombsID_EASY
, false);
63 BEGIN_EVENT_TABLE(BombsFrame
, wxFrame
)
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
)
68 EVT_MENU(bombsID_EASYCORNER
, BombsFrame::OnEasyCorner
)
69 EVT_MENU(wxID_EXIT
, BombsFrame::OnExit
)
70 EVT_MENU(wxID_ABOUT
, BombsFrame::OnAbout
)
73 BombsFrame::BombsFrame(BombsGame
*game
)
74 : wxFrame(NULL
, wxID_ANY
, wxT("wxBombs"), wxDefaultPosition
,
75 wxSize(300, 300), wxDEFAULT_DIALOG_STYLE
|wxMINIMIZE_BOX
)
79 m_lastLevel
= bombsID_EASY
;
81 SetIcon(wxICON(bombs
));
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"));
95 menuFile
->Append(wxID_NEW
, wxT("&New game\tCtrl-N"));
96 menuFile
->Append(bombsID_LEVEL
, wxT("&Level"),menuLevel
, wxT("Starts a new game"));
97 menuFile
->AppendCheckItem(bombsID_EASYCORNER
, wxT("&Easy corner"));
99 menuFile
->AppendSeparator();
100 menuFile
->Append(wxID_EXIT
, wxGetStockLabel(wxID_EXIT
), wxT("Quits the application"));
102 menuBar
->Append(menuFile
, wxT("&File"));
105 wxMenu
*menuHelp
= new wxMenu
;
106 menuHelp
->Append(wxID_ABOUT
, wxT("&About"),
107 wxT("Displays the program information") );
109 menuBar
->Append(menuHelp
, wxT("&Help"));
113 // Create child subwindows.
114 m_canvas
= new BombsCanvas(this, m_game
);
116 // Ensure the subwindows get resized o.k.
117 // OnSize(width, height);
119 // Centre frame on the screen.
126 void BombsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
131 void BombsFrame::NewGame(int level
, bool query
)
135 int ok
= wxMessageBox(
136 wxT("Start new game regardless previous board?"),
138 wxYES_NO
| wxICON_QUESTION
,
144 int numHorzCells
= 20, numVertCells
= 20;
150 numHorzCells
= numVertCells
= 10;
154 numHorzCells
= numVertCells
= 15;
158 numHorzCells
= 25; numVertCells
= 20;
162 wxFAIL_MSG(wxT("Invalid level"));
166 m_game
->Init(numHorzCells
, numVertCells
, m_easyCorner
);
168 GetMenuBar()->Check(level
, true);
170 m_canvas
->UpdateGridSize();
171 SetClientSize(m_canvas
->GetGridSizeInPixels());
174 void BombsFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
177 wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"),
178 wxT("About wxBombs") );
181 void BombsFrame::OnNewGame(wxCommandEvent
& WXUNUSED(event
))
183 NewGame(m_lastLevel
, true);
186 void BombsFrame::OnEasyGame(wxCommandEvent
& WXUNUSED(event
))
188 NewGame(bombsID_EASY
, true);
191 void BombsFrame::OnMediumGame(wxCommandEvent
& WXUNUSED(event
))
193 NewGame(bombsID_MEDIUM
, true);
196 void BombsFrame::OnHardGame(wxCommandEvent
& WXUNUSED(event
))
198 NewGame(bombsID_HARD
, true);
201 void BombsFrame::OnEasyCorner(wxCommandEvent
& WXUNUSED(event
))
207 msg
= wxT("disable");
209 msg
= wxT("Do you really want to ") + msg
+ wxT(" having\ntop left corner always empty for easier start?");
211 int ok
= wxMessageBox(
214 wxYES_NO
| wxICON_QUESTION
,
220 m_easyCorner
= !m_easyCorner
;
222 NewGame(m_lastLevel
, true);
225 BEGIN_EVENT_TABLE(BombsCanvas
, wxPanel
)
226 EVT_PAINT(BombsCanvas::OnPaint
)
227 EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent
)
228 EVT_CHAR(BombsCanvas::OnChar
)
231 BombsCanvas::BombsCanvas(wxFrame
*parent
, BombsGame
*game
)
232 : wxPanel(parent
, wxID_ANY
)
237 wxFont font
= BOMBS_FONT
;
241 wxString buf
= wxT("M");
243 dc
.GetTextExtent(buf
, &chw
, &chh
);
244 dc
.SetFont(wxNullFont
);
246 dc
.SetMapMode(wxMM_METRIC
);
248 int xcm
= dc
.LogicalToDeviceX(10);
249 int ycm
= dc
.LogicalToDeviceY(10);
250 // To have a square cell, there must be :
252 if (chw
*ycm
< chh
*xcm
)
263 m_cellWidth
= (sx
+3+X_UNIT
)/X_UNIT
;
264 m_cellHeight
= (sy
+3+Y_UNIT
)/Y_UNIT
;
265 dc
.SetMapMode(wxMM_TEXT
);
269 BombsCanvas::~BombsCanvas()
278 // Called when canvas needs to be repainted.
279 void BombsCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
283 const int numHorzCells
= m_game
->GetWidth();
284 const int numVertCells
= m_game
->GetHeight();
285 // Insert your drawing code here.
288 wxSize size
= dc
.GetSize();
289 m_bmp
= new wxBitmap(size
.GetWidth(), size
.GetHeight());
293 memDC
.SelectObject(*m_bmp
);
294 DrawField(&memDC
, 0, 0, numHorzCells
-1, numVertCells
-1);
295 memDC
.SelectObject(wxNullBitmap
);
302 memDC
.SelectObject(*m_bmp
);
303 wxSize size
= dc
.GetSize();
304 dc
.Blit(0, 0, size
.GetWidth(), size
.GetHeight(),
305 &memDC
, 0, 0, wxCOPY
);
306 memDC
.SelectObject(wxNullBitmap
);
310 DrawField(&dc
, 0, 0, numHorzCells
-1, numVertCells
-1);
314 void BombsCanvas::UpdateGridSize()
322 SetSize(GetGridSizeInPixels());
326 wxSize
BombsCanvas::GetGridSizeInPixels() const
328 return wxSize(m_cellWidth
*X_UNIT
*m_game
->GetWidth(),
329 m_cellHeight
*Y_UNIT
*m_game
->GetHeight());