]>
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
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(bombsID_EASY
, BombsFrame::OnNewEasyGame
)
65 EVT_MENU(bombsID_MEDIUM
, BombsFrame::OnNewMediumGame
)
66 EVT_MENU(bombsID_HARD
, BombsFrame::OnNewHardGame
)
67 EVT_MENU(wxID_EXIT
, BombsFrame::OnExit
)
68 EVT_MENU(wxID_ABOUT
, BombsFrame::OnAbout
)
71 BombsFrame::BombsFrame(BombsGame
*game
)
72 : wxFrame(NULL
, wxID_ANY
, wxT("wxBombs"), wxDefaultPosition
,
73 wxSize(300, 300), wxDEFAULT_DIALOG_STYLE
|wxMINIMIZE_BOX
)
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(bombsID_NEWGAME
, wxT("&New Game"),
92 menuLevel
, wxT("Starts a new game"));
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;
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
);
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::OnNewEasyGame(wxCommandEvent
& WXUNUSED(event
))
177 NewGame(bombsID_EASY
, true);
180 void BombsFrame::OnNewMediumGame(wxCommandEvent
& WXUNUSED(event
))
182 NewGame(bombsID_MEDIUM
, true);
185 void BombsFrame::OnNewHardGame(wxCommandEvent
& WXUNUSED(event
))
187 NewGame(bombsID_HARD
, true);
190 BEGIN_EVENT_TABLE(BombsCanvas
, wxPanel
)
191 EVT_PAINT(BombsCanvas::OnPaint
)
192 EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent
)
193 EVT_CHAR(BombsCanvas::OnChar
)
196 BombsCanvas::BombsCanvas(wxFrame
*parent
, BombsGame
*game
)
197 : wxPanel(parent
, wxID_ANY
)
202 wxFont font
= BOMBS_FONT
;
206 wxString buf
= wxT("M");
208 dc
.GetTextExtent(buf
, &chw
, &chh
);
209 dc
.SetFont(wxNullFont
);
211 dc
.SetMapMode(wxMM_METRIC
);
213 int xcm
= dc
.LogicalToDeviceX(10);
214 int ycm
= dc
.LogicalToDeviceY(10);
215 // To have a square cell, there must be :
217 if (chw
*ycm
< chh
*xcm
)
228 m_cellWidth
= (sx
+3+X_UNIT
)/X_UNIT
;
229 m_cellHeight
= (sy
+3+Y_UNIT
)/Y_UNIT
;
230 dc
.SetMapMode(wxMM_TEXT
);
234 BombsCanvas::~BombsCanvas()
243 // Called when canvas needs to be repainted.
244 void BombsCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
248 const int numHorzCells
= m_game
->GetWidth();
249 const int numVertCells
= m_game
->GetHeight();
250 // Insert your drawing code here.
253 wxSize size
= dc
.GetSize();
254 m_bmp
= new wxBitmap(size
.GetWidth(), size
.GetHeight());
258 memDC
.SelectObject(*m_bmp
);
259 DrawField(&memDC
, 0, 0, numHorzCells
-1, numVertCells
-1);
260 memDC
.SelectObject(wxNullBitmap
);
267 memDC
.SelectObject(*m_bmp
);
268 wxSize size
= dc
.GetSize();
269 dc
.Blit(0, 0, size
.GetWidth(), size
.GetHeight(),
270 &memDC
, 0, 0, wxCOPY
);
271 memDC
.SelectObject(wxNullBitmap
);
275 DrawField(&dc
, 0, 0, numHorzCells
-1, numVertCells
-1);
279 void BombsCanvas::UpdateGridSize()
291 wxSize
BombsCanvas::GetGridSizeInPixels() const
293 return wxSize(m_cellWidth
*X_UNIT
*m_game
->GetWidth(),
294 m_cellHeight
*Y_UNIT
*m_game
->GetHeight());