]>
git.saurik.com Git - wxWidgets.git/blob - demos/bombs/bombs.cpp
1 ///////////////////////////////////////////////////////////////////////////////
4 // Author: P. Foggia 1996
5 // Modified by: Wlodzimierz Skiba (ABX) 2003
8 // Copyright: (c) 1996 P. Foggia
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 # pragma implementation
16 #include "wx/wxprec.h"
24 #endif //precompiled headers
34 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) \
35 || defined(__WXMAC__) || defined(__WXMGL__)
39 IMPLEMENT_APP(BombsApp
)
42 STDAPI_(__int64
) CeGetRandomSeed();
45 // Called to initialize the program
46 bool BombsApp::OnInit()
49 srand((unsigned) CeGetRandomSeed());
51 srand((unsigned) time(NULL
));
54 m_frame
= new BombsFrame(&m_game
);
56 m_frame
->NewGame(bombsID_EASY
, false);
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
)
69 BombsFrame::BombsFrame(BombsGame
*game
)
70 : wxFrame(NULL
, wxID_ANY
, wxT("wxBombs"), wxDefaultPosition
,
71 wxSize(300, 300), wxDEFAULT_DIALOG_STYLE
|wxMINIMIZE_BOX
)
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(bombsID_NEWGAME
, wxT("&New Game"),
90 menuLevel
, wxT("Starts a new game"));
92 menuFile
->AppendSeparator();
93 menuFile
->Append(wxID_EXIT
, wxT("E&xit"), wxT("Quits the application"));
95 menuBar
->Append(menuFile
, wxT("&File"));
98 wxMenu
*menuHelp
= new wxMenu
;
99 menuHelp
->Append(wxID_ABOUT
, wxT("&About"),
100 wxT("Displays the program information") );
102 menuBar
->Append(menuHelp
, wxT("&Help"));
106 // Create child subwindows.
107 m_canvas
= new BombsCanvas(this, m_game
);
109 // Ensure the subwindows get resized o.k.
110 // OnSize(width, height);
112 // Centre frame on the screen.
119 void BombsFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
124 void BombsFrame::NewGame(int level
, bool query
)
128 int ok
= wxMessageBox(
129 wxT("Start new game regardless previous board?"),
131 wxYES_NO
| wxICON_QUESTION
,
137 int numHorzCells
= 20, numVertCells
= 20;
142 numHorzCells
= numVertCells
= 10;
146 numHorzCells
= numVertCells
= 15;
150 numHorzCells
= 25; numVertCells
= 20;
154 wxFAIL_MSG(wxT("Invalid level"));
158 m_game
->Init(numHorzCells
, numVertCells
);
160 GetMenuBar()->Check(level
, true);
162 m_canvas
->UpdateGridSize();
163 SetClientSize(m_canvas
->GetGridSizeInPixels());
166 void BombsFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
169 wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"),
170 wxT("About wxBombs") );
173 void BombsFrame::OnNewEasyGame(wxCommandEvent
& WXUNUSED(event
))
175 NewGame(bombsID_EASY
, true);
178 void BombsFrame::OnNewMediumGame(wxCommandEvent
& WXUNUSED(event
))
180 NewGame(bombsID_MEDIUM
, true);
183 void BombsFrame::OnNewHardGame(wxCommandEvent
& WXUNUSED(event
))
185 NewGame(bombsID_HARD
, true);
188 BEGIN_EVENT_TABLE(BombsCanvas
, wxPanel
)
189 EVT_PAINT(BombsCanvas::OnPaint
)
190 EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent
)
191 EVT_CHAR(BombsCanvas::OnChar
)
194 BombsCanvas::BombsCanvas(wxFrame
*parent
, BombsGame
*game
)
195 : wxPanel(parent
, wxID_ANY
)
200 wxFont font
= BOMBS_FONT
;
204 wxString buf
= wxT("M");
206 dc
.GetTextExtent(buf
, &chw
, &chh
);
207 dc
.SetFont(wxNullFont
);
209 dc
.SetMapMode(wxMM_METRIC
);
211 int xcm
= dc
.LogicalToDeviceX(10);
212 int ycm
= dc
.LogicalToDeviceY(10);
213 // To have a square cell, there must be :
215 if (chw
*ycm
< chh
*xcm
)
226 m_cellWidth
= (sx
+3+X_UNIT
)/X_UNIT
;
227 m_cellHeight
= (sy
+3+Y_UNIT
)/Y_UNIT
;
228 dc
.SetMapMode(wxMM_TEXT
);
232 BombsCanvas::~BombsCanvas()
241 // Called when canvas needs to be repainted.
242 void BombsCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
246 const int numHorzCells
= m_game
->GetWidth();
247 const int numVertCells
= m_game
->GetHeight();
248 // Insert your drawing code here.
251 wxSize size
= dc
.GetSize();
252 m_bmp
= new wxBitmap(size
.GetWidth(), size
.GetHeight());
256 memDC
.SelectObject(*m_bmp
);
257 DrawField(&memDC
, 0, 0, numHorzCells
-1, numVertCells
-1);
258 memDC
.SelectObject(wxNullBitmap
);
265 memDC
.SelectObject(*m_bmp
);
266 wxSize size
= dc
.GetSize();
267 dc
.Blit(0, 0, size
.GetWidth(), size
.GetHeight(),
268 &memDC
, 0, 0, wxCOPY
);
269 memDC
.SelectObject(wxNullBitmap
);
273 DrawField(&dc
, 0, 0, numHorzCells
-1, numVertCells
-1);
277 void BombsCanvas::UpdateGridSize()
289 wxSize
BombsCanvas::GetGridSizeInPixels() const
291 return wxSize(m_cellWidth
*X_UNIT
*m_game
->GetWidth(),
292 m_cellHeight
*Y_UNIT
*m_game
->GetHeight());