]>
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
7 // Copyright: (c) 1996 P. Foggia
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
19 #endif //precompiled headers
21 #include "wx/stockitem.h"
31 #ifndef wxHAS_IMAGES_IN_RESOURCES
35 IMPLEMENT_APP(BombsApp
)
38 STDAPI_(__int64
) CeGetRandomSeed();
41 // Called to initialize the program
42 bool BombsApp::OnInit()
45 srand((unsigned) CeGetRandomSeed());
47 srand((unsigned) time(NULL
));
50 m_frame
= new BombsFrame(&m_game
);
52 m_frame
->NewGame(bombsID_EASY
, false);
57 BEGIN_EVENT_TABLE(BombsFrame
, wxFrame
)
58 EVT_MENU(wxID_NEW
, BombsFrame::OnNewGame
)
59 EVT_MENU(bombsID_EASY
, BombsFrame::OnEasyGame
)
60 EVT_MENU(bombsID_MEDIUM
, BombsFrame::OnMediumGame
)
61 EVT_MENU(bombsID_HARD
, BombsFrame::OnHardGame
)
62 EVT_MENU(bombsID_EASYCORNER
, BombsFrame::OnEasyCorner
)
63 EVT_MENU(wxID_EXIT
, BombsFrame::OnExit
)
64 EVT_MENU(wxID_ABOUT
, BombsFrame::OnAbout
)
67 BombsFrame::BombsFrame(BombsGame
*game
)
68 : wxFrame(NULL
, wxID_ANY
, wxT("wxBombs"), wxDefaultPosition
,
69 wxSize(300, 300), wxDEFAULT_DIALOG_STYLE
|wxMINIMIZE_BOX
)
73 m_lastLevel
= bombsID_EASY
;
75 SetIcon(wxICON(bombs
));
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"));
89 menuFile
->Append(wxID_NEW
, wxT("&New game\tCtrl-N"));
90 menuFile
->Append(bombsID_LEVEL
, wxT("&Level"),menuLevel
, wxT("Starts a new game"));
91 menuFile
->AppendCheckItem(bombsID_EASYCORNER
, wxT("&Easy corner"));
93 menuFile
->AppendSeparator();
94 menuFile
->Append(wxID_EXIT
, wxGetStockLabel(wxID_EXIT
), wxT("Quits the application"));
96 menuBar
->Append(menuFile
, wxT("&File"));
99 wxMenu
*menuHelp
= new wxMenu
;
100 menuHelp
->Append(wxID_ABOUT
, wxT("&About"),
101 wxT("Displays the program information") );
103 menuBar
->Append(menuHelp
, wxT("&Help"));
107 // Create child subwindows.
108 m_canvas
= new BombsCanvas(this, m_game
);
110 // Ensure the subwindows get resized o.k.
111 // OnSize(width, height);
113 // Centre frame on the screen.
120 void BombsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
125 void BombsFrame::NewGame(int level
, bool query
)
129 int ok
= wxMessageBox(
130 wxT("Start new game regardless previous board?"),
132 wxYES_NO
| wxICON_QUESTION
,
138 int numHorzCells
= 20, numVertCells
= 20;
144 numHorzCells
= numVertCells
= 10;
148 numHorzCells
= numVertCells
= 15;
152 numHorzCells
= 25; numVertCells
= 20;
156 wxFAIL_MSG(wxT("Invalid level"));
160 m_game
->Init(numHorzCells
, numVertCells
, m_easyCorner
);
162 GetMenuBar()->Check(level
, true);
164 m_canvas
->UpdateGridSize();
165 SetClientSize(m_canvas
->GetGridSizeInPixels());
168 void BombsFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
171 wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"),
172 wxT("About wxBombs") );
175 void BombsFrame::OnNewGame(wxCommandEvent
& WXUNUSED(event
))
177 NewGame(m_lastLevel
, true);
180 void BombsFrame::OnEasyGame(wxCommandEvent
& WXUNUSED(event
))
182 NewGame(bombsID_EASY
, true);
185 void BombsFrame::OnMediumGame(wxCommandEvent
& WXUNUSED(event
))
187 NewGame(bombsID_MEDIUM
, true);
190 void BombsFrame::OnHardGame(wxCommandEvent
& WXUNUSED(event
))
192 NewGame(bombsID_HARD
, true);
195 void BombsFrame::OnEasyCorner(wxCommandEvent
& WXUNUSED(event
))
201 msg
= wxT("disable");
203 msg
= wxT("Do you really want to ") + msg
+ wxT(" having\ntop left corner always empty for easier start?");
205 int ok
= wxMessageBox(
208 wxYES_NO
| wxICON_QUESTION
,
214 m_easyCorner
= !m_easyCorner
;
216 NewGame(m_lastLevel
, true);
219 BEGIN_EVENT_TABLE(BombsCanvas
, wxPanel
)
220 EVT_PAINT(BombsCanvas::OnPaint
)
221 EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent
)
222 EVT_CHAR(BombsCanvas::OnChar
)
225 BombsCanvas::BombsCanvas(wxFrame
*parent
, BombsGame
*game
)
226 : wxPanel(parent
, wxID_ANY
)
231 wxFont font
= BOMBS_FONT
;
235 wxString buf
= wxT("M");
237 dc
.GetTextExtent(buf
, &chw
, &chh
);
238 dc
.SetFont(wxNullFont
);
240 dc
.SetMapMode(wxMM_METRIC
);
242 int xcm
= dc
.LogicalToDeviceX(10);
243 int ycm
= dc
.LogicalToDeviceY(10);
244 // To have a square cell, there must be :
246 if (chw
*ycm
< chh
*xcm
)
257 m_cellWidth
= (sx
+3+X_UNIT
)/X_UNIT
;
258 m_cellHeight
= (sy
+3+Y_UNIT
)/Y_UNIT
;
259 dc
.SetMapMode(wxMM_TEXT
);
263 BombsCanvas::~BombsCanvas()
272 // Called when canvas needs to be repainted.
273 void BombsCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
277 const int numHorzCells
= m_game
->GetWidth();
278 const int numVertCells
= m_game
->GetHeight();
279 // Insert your drawing code here.
282 wxSize size
= dc
.GetSize();
283 m_bmp
= new wxBitmap(size
.GetWidth(), size
.GetHeight());
287 memDC
.SelectObject(*m_bmp
);
288 DrawField(&memDC
, 0, 0, numHorzCells
-1, numVertCells
-1);
289 memDC
.SelectObject(wxNullBitmap
);
296 memDC
.SelectObject(*m_bmp
);
297 wxSize size
= dc
.GetSize();
298 dc
.Blit(0, 0, size
.GetWidth(), size
.GetHeight(),
299 &memDC
, 0, 0, wxCOPY
);
300 memDC
.SelectObject(wxNullBitmap
);
304 DrawField(&dc
, 0, 0, numHorzCells
-1, numVertCells
-1);
308 void BombsCanvas::UpdateGridSize()
316 SetSize(GetGridSizeInPixels());
320 wxSize
BombsCanvas::GetGridSizeInPixels() const
322 return wxSize(m_cellWidth
*X_UNIT
*m_game
->GetWidth(),
323 m_cellHeight
*Y_UNIT
*m_game
->GetHeight());