]>
git.saurik.com Git - wxWidgets.git/blob - demos/bombs/bombs.cpp
2c756fcff3fb8847a1e4eaf66ada5350bab634d8
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 #ifndef wxHAS_IMAGES_IN_RESOURCES
36 IMPLEMENT_APP(BombsApp
)
39 STDAPI_(__int64
) CeGetRandomSeed();
42 // Called to initialize the program
43 bool BombsApp::OnInit()
46 srand((unsigned) CeGetRandomSeed());
48 srand((unsigned) time(NULL
));
51 m_frame
= new BombsFrame(&m_game
);
53 m_frame
->NewGame(bombsID_EASY
, false);
58 BEGIN_EVENT_TABLE(BombsFrame
, wxFrame
)
59 EVT_MENU(wxID_NEW
, BombsFrame::OnNewGame
)
60 EVT_MENU(bombsID_EASY
, BombsFrame::OnEasyGame
)
61 EVT_MENU(bombsID_MEDIUM
, BombsFrame::OnMediumGame
)
62 EVT_MENU(bombsID_HARD
, BombsFrame::OnHardGame
)
63 EVT_MENU(bombsID_EASYCORNER
, BombsFrame::OnEasyCorner
)
64 EVT_MENU(wxID_EXIT
, BombsFrame::OnExit
)
65 EVT_MENU(wxID_ABOUT
, BombsFrame::OnAbout
)
68 BombsFrame::BombsFrame(BombsGame
*game
)
69 : wxFrame(NULL
, wxID_ANY
, wxT("wxBombs"), wxDefaultPosition
,
70 wxSize(300, 300), wxDEFAULT_DIALOG_STYLE
|wxMINIMIZE_BOX
)
74 m_lastLevel
= bombsID_EASY
;
76 SetIcon(wxICON(bombs
));
82 // Create a menu bar for the frame
83 wxMenuBar
*menuBar
= new wxMenuBar
;
84 wxMenu
*menuFile
= new wxMenu
;
85 wxMenu
*menuLevel
= new wxMenu
;
86 menuLevel
->AppendRadioItem(bombsID_EASY
, wxT("&Easy (10x10)\tCtrl-1"));
87 menuLevel
->AppendRadioItem(bombsID_MEDIUM
, wxT("&Medium (15x15)\tCtrl-2"));
88 menuLevel
->AppendRadioItem(bombsID_HARD
, wxT("&Hard (25x20)\tCtrl-3"));
90 menuFile
->Append(wxID_NEW
, wxT("&New game\tCtrl-N"));
91 menuFile
->Append(bombsID_LEVEL
, wxT("&Level"),menuLevel
, wxT("Starts a new game"));
92 menuFile
->AppendCheckItem(bombsID_EASYCORNER
, wxT("&Easy corner"));
94 menuFile
->AppendSeparator();
95 menuFile
->Append(wxID_EXIT
, wxGetStockLabel(wxID_EXIT
), wxT("Quits the application"));
97 menuBar
->Append(menuFile
, wxT("&File"));
100 wxMenu
*menuHelp
= new wxMenu
;
101 menuHelp
->Append(wxID_ABOUT
, wxT("&About"),
102 wxT("Displays the program information") );
104 menuBar
->Append(menuHelp
, wxT("&Help"));
108 // Create child subwindows.
109 m_canvas
= new BombsCanvas(this, m_game
);
111 // Ensure the subwindows get resized o.k.
112 // OnSize(width, height);
114 // Centre frame on the screen.
121 void BombsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
126 void BombsFrame::NewGame(int level
, bool query
)
130 int ok
= wxMessageBox(
131 wxT("Start new game regardless previous board?"),
133 wxYES_NO
| wxICON_QUESTION
,
139 int numHorzCells
= 20, numVertCells
= 20;
145 numHorzCells
= numVertCells
= 10;
149 numHorzCells
= numVertCells
= 15;
153 numHorzCells
= 25; numVertCells
= 20;
157 wxFAIL_MSG(wxT("Invalid level"));
161 m_game
->Init(numHorzCells
, numVertCells
, m_easyCorner
);
163 GetMenuBar()->Check(level
, true);
165 m_canvas
->UpdateGridSize();
166 SetClientSize(m_canvas
->GetGridSizeInPixels());
169 void BombsFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
172 wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"),
173 wxT("About wxBombs") );
176 void BombsFrame::OnNewGame(wxCommandEvent
& WXUNUSED(event
))
178 NewGame(m_lastLevel
, true);
181 void BombsFrame::OnEasyGame(wxCommandEvent
& WXUNUSED(event
))
183 NewGame(bombsID_EASY
, true);
186 void BombsFrame::OnMediumGame(wxCommandEvent
& WXUNUSED(event
))
188 NewGame(bombsID_MEDIUM
, true);
191 void BombsFrame::OnHardGame(wxCommandEvent
& WXUNUSED(event
))
193 NewGame(bombsID_HARD
, true);
196 void BombsFrame::OnEasyCorner(wxCommandEvent
& WXUNUSED(event
))
202 msg
= wxT("disable");
204 msg
= wxT("Do you really want to ") + msg
+ wxT(" having\ntop left corner always empty for easier start?");
206 int ok
= wxMessageBox(
209 wxYES_NO
| wxICON_QUESTION
,
215 m_easyCorner
= !m_easyCorner
;
217 NewGame(m_lastLevel
, true);
220 BEGIN_EVENT_TABLE(BombsCanvas
, wxPanel
)
221 EVT_PAINT(BombsCanvas::OnPaint
)
222 EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent
)
223 EVT_CHAR(BombsCanvas::OnChar
)
226 BombsCanvas::BombsCanvas(wxFrame
*parent
, BombsGame
*game
)
227 : wxPanel(parent
, wxID_ANY
)
232 wxFont font
= BOMBS_FONT
;
236 wxString buf
= wxT("M");
238 dc
.GetTextExtent(buf
, &chw
, &chh
);
239 dc
.SetFont(wxNullFont
);
241 dc
.SetMapMode(wxMM_METRIC
);
243 int xcm
= dc
.LogicalToDeviceX(10);
244 int ycm
= dc
.LogicalToDeviceY(10);
245 // To have a square cell, there must be :
247 if (chw
*ycm
< chh
*xcm
)
258 m_cellWidth
= (sx
+3+X_UNIT
)/X_UNIT
;
259 m_cellHeight
= (sy
+3+Y_UNIT
)/Y_UNIT
;
260 dc
.SetMapMode(wxMM_TEXT
);
264 BombsCanvas::~BombsCanvas()
273 // Called when canvas needs to be repainted.
274 void BombsCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
278 const int numHorzCells
= m_game
->GetWidth();
279 const int numVertCells
= m_game
->GetHeight();
280 // Insert your drawing code here.
283 wxSize size
= dc
.GetSize();
284 m_bmp
= new wxBitmap(size
.GetWidth(), size
.GetHeight());
288 memDC
.SelectObject(*m_bmp
);
289 DrawField(&memDC
, 0, 0, numHorzCells
-1, numVertCells
-1);
290 memDC
.SelectObject(wxNullBitmap
);
297 memDC
.SelectObject(*m_bmp
);
298 wxSize size
= dc
.GetSize();
299 dc
.Blit(0, 0, size
.GetWidth(), size
.GetHeight(),
300 &memDC
, 0, 0, wxCOPY
);
301 memDC
.SelectObject(wxNullBitmap
);
305 DrawField(&dc
, 0, 0, numHorzCells
-1, numVertCells
-1);
309 void BombsCanvas::UpdateGridSize()
317 SetSize(GetGridSizeInPixels());
321 wxSize
BombsCanvas::GetGridSizeInPixels() const
323 return wxSize(m_cellWidth
*X_UNIT
*m_game
->GetWidth(),
324 m_cellHeight
*Y_UNIT
*m_game
->GetHeight());