]>
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 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #endif //precompiled headers
22 #include "wx/stockitem.h"
32 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) \
37 IMPLEMENT_APP(BombsApp
)
40 STDAPI_(__int64
) CeGetRandomSeed();
43 // Called to initialize the program
44 bool BombsApp::OnInit()
47 srand((unsigned) CeGetRandomSeed());
49 srand((unsigned) time(NULL
));
52 m_frame
= new BombsFrame(&m_game
);
54 m_frame
->NewGame(bombsID_EASY
, false);
59 BEGIN_EVENT_TABLE(BombsFrame
, wxFrame
)
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
)
64 EVT_MENU(bombsID_EASYCORNER
, BombsFrame::OnEasyCorner
)
65 EVT_MENU(wxID_EXIT
, BombsFrame::OnExit
)
66 EVT_MENU(wxID_ABOUT
, BombsFrame::OnAbout
)
69 BombsFrame::BombsFrame(BombsGame
*game
)
70 : wxFrame(NULL
, wxID_ANY
, wxT("wxBombs"), wxDefaultPosition
,
71 wxSize(300, 300), wxDEFAULT_DIALOG_STYLE
|wxMINIMIZE_BOX
)
75 m_lastLevel
= bombsID_EASY
;
77 SetIcon(wxICON(bombs
));
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"));
91 menuFile
->Append(wxID_NEW
, wxT("&New game\tCtrl-N"));
92 menuFile
->Append(bombsID_LEVEL
, wxT("&Level"),menuLevel
, wxT("Starts a new game"));
93 menuFile
->AppendCheckItem(bombsID_EASYCORNER
, wxT("&Easy corner"));
95 menuFile
->AppendSeparator();
96 menuFile
->Append(wxID_EXIT
, wxGetStockLabel(wxID_EXIT
), wxT("Quits the application"));
98 menuBar
->Append(menuFile
, wxT("&File"));
101 wxMenu
*menuHelp
= new wxMenu
;
102 menuHelp
->Append(wxID_ABOUT
, wxT("&About"),
103 wxT("Displays the program information") );
105 menuBar
->Append(menuHelp
, wxT("&Help"));
109 // Create child subwindows.
110 m_canvas
= new BombsCanvas(this, m_game
);
112 // Ensure the subwindows get resized o.k.
113 // OnSize(width, height);
115 // Centre frame on the screen.
122 void BombsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
127 void BombsFrame::NewGame(int level
, bool query
)
131 int ok
= wxMessageBox(
132 wxT("Start new game regardless previous board?"),
134 wxYES_NO
| wxICON_QUESTION
,
140 int numHorzCells
= 20, numVertCells
= 20;
146 numHorzCells
= numVertCells
= 10;
150 numHorzCells
= numVertCells
= 15;
154 numHorzCells
= 25; numVertCells
= 20;
158 wxFAIL_MSG(wxT("Invalid level"));
162 m_game
->Init(numHorzCells
, numVertCells
, m_easyCorner
);
164 GetMenuBar()->Check(level
, true);
166 m_canvas
->UpdateGridSize();
167 SetClientSize(m_canvas
->GetGridSizeInPixels());
170 void BombsFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
173 wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"),
174 wxT("About wxBombs") );
177 void BombsFrame::OnNewGame(wxCommandEvent
& WXUNUSED(event
))
179 NewGame(m_lastLevel
, true);
182 void BombsFrame::OnEasyGame(wxCommandEvent
& WXUNUSED(event
))
184 NewGame(bombsID_EASY
, true);
187 void BombsFrame::OnMediumGame(wxCommandEvent
& WXUNUSED(event
))
189 NewGame(bombsID_MEDIUM
, true);
192 void BombsFrame::OnHardGame(wxCommandEvent
& WXUNUSED(event
))
194 NewGame(bombsID_HARD
, true);
197 void BombsFrame::OnEasyCorner(wxCommandEvent
& WXUNUSED(event
))
203 msg
= wxT("disable");
205 msg
= wxT("Do you really want to ") + msg
+ wxT(" having\ntop left corner always empty for easier start?");
207 int ok
= wxMessageBox(
210 wxYES_NO
| wxICON_QUESTION
,
216 m_easyCorner
= !m_easyCorner
;
218 NewGame(m_lastLevel
, true);
221 BEGIN_EVENT_TABLE(BombsCanvas
, wxPanel
)
222 EVT_PAINT(BombsCanvas::OnPaint
)
223 EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent
)
224 EVT_CHAR(BombsCanvas::OnChar
)
227 BombsCanvas::BombsCanvas(wxFrame
*parent
, BombsGame
*game
)
228 : wxPanel(parent
, wxID_ANY
)
233 wxFont font
= BOMBS_FONT
;
237 wxString buf
= wxT("M");
239 dc
.GetTextExtent(buf
, &chw
, &chh
);
240 dc
.SetFont(wxNullFont
);
242 dc
.SetMapMode(wxMM_METRIC
);
244 int xcm
= dc
.LogicalToDeviceX(10);
245 int ycm
= dc
.LogicalToDeviceY(10);
246 // To have a square cell, there must be :
248 if (chw
*ycm
< chh
*xcm
)
259 m_cellWidth
= (sx
+3+X_UNIT
)/X_UNIT
;
260 m_cellHeight
= (sy
+3+Y_UNIT
)/Y_UNIT
;
261 dc
.SetMapMode(wxMM_TEXT
);
265 BombsCanvas::~BombsCanvas()
274 // Called when canvas needs to be repainted.
275 void BombsCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
279 const int numHorzCells
= m_game
->GetWidth();
280 const int numVertCells
= m_game
->GetHeight();
281 // Insert your drawing code here.
284 wxSize size
= dc
.GetSize();
285 m_bmp
= new wxBitmap(size
.GetWidth(), size
.GetHeight());
289 memDC
.SelectObject(*m_bmp
);
290 DrawField(&memDC
, 0, 0, numHorzCells
-1, numVertCells
-1);
291 memDC
.SelectObject(wxNullBitmap
);
298 memDC
.SelectObject(*m_bmp
);
299 wxSize size
= dc
.GetSize();
300 dc
.Blit(0, 0, size
.GetWidth(), size
.GetHeight(),
301 &memDC
, 0, 0, wxCOPY
);
302 memDC
.SelectObject(wxNullBitmap
);
306 DrawField(&dc
, 0, 0, numHorzCells
-1, numVertCells
-1);
310 void BombsCanvas::UpdateGridSize()
318 SetSize(GetGridSizeInPixels());
322 wxSize
BombsCanvas::GetGridSizeInPixels() const
324 return wxSize(m_cellWidth
*X_UNIT
*m_game
->GetWidth(),
325 m_cellHeight
*Y_UNIT
*m_game
->GetHeight());