bombs.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: bombs.cpp
  3. // Purpose: Bombs game
  4. // Author: P. Foggia 1996
  5. // Modified by: Wlodzimierz Skiba (ABX) since 2003
  6. // Created: 1996
  7. // Copyright: (c) 1996 P. Foggia
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "wx/wxprec.h"
  11. #ifdef __BORLANDC__
  12. # pragma hdrstop
  13. #endif
  14. #ifndef WX_PRECOMP
  15. # include "wx/wx.h"
  16. #endif //precompiled headers
  17. #include "wx/stockitem.h"
  18. #include "bombs.h"
  19. #include <stdlib.h>
  20. #ifndef __WXWINCE__
  21. # include <time.h>
  22. #endif
  23. #ifndef wxHAS_IMAGES_IN_RESOURCES
  24. # include "bombs.xpm"
  25. #endif
  26. IMPLEMENT_APP(BombsApp)
  27. #ifdef __WXWINCE__
  28. STDAPI_(__int64) CeGetRandomSeed();
  29. #endif
  30. // Called to initialize the program
  31. bool BombsApp::OnInit()
  32. {
  33. #ifdef __WXWINCE__
  34. srand((unsigned) CeGetRandomSeed());
  35. #else
  36. srand((unsigned) time(NULL));
  37. #endif
  38. m_frame = new BombsFrame(&m_game);
  39. m_frame->NewGame(bombsID_EASY, false);
  40. return true;
  41. }
  42. BEGIN_EVENT_TABLE(BombsFrame, wxFrame)
  43. EVT_MENU(wxID_NEW, BombsFrame::OnNewGame)
  44. EVT_MENU(bombsID_EASY, BombsFrame::OnEasyGame)
  45. EVT_MENU(bombsID_MEDIUM, BombsFrame::OnMediumGame)
  46. EVT_MENU(bombsID_HARD, BombsFrame::OnHardGame)
  47. EVT_MENU(bombsID_EASYCORNER, BombsFrame::OnEasyCorner)
  48. EVT_MENU(wxID_EXIT, BombsFrame::OnExit)
  49. EVT_MENU(wxID_ABOUT, BombsFrame::OnAbout)
  50. END_EVENT_TABLE()
  51. BombsFrame::BombsFrame(BombsGame *game)
  52. : wxFrame(NULL, wxID_ANY, wxT("wxBombs"), wxDefaultPosition,
  53. wxSize(300, 300), wxDEFAULT_DIALOG_STYLE|wxMINIMIZE_BOX)
  54. {
  55. m_game = game;
  56. m_easyCorner = false;
  57. m_lastLevel = bombsID_EASY;
  58. SetIcon(wxICON(bombs));
  59. #if wxUSE_STATUSBAR
  60. CreateStatusBar();
  61. #endif
  62. // Create a menu bar for the frame
  63. wxMenuBar *menuBar = new wxMenuBar;
  64. wxMenu *menuFile = new wxMenu;
  65. wxMenu *menuLevel = new wxMenu;
  66. menuLevel->AppendRadioItem(bombsID_EASY, wxT("&Easy (10x10)\tCtrl-1"));
  67. menuLevel->AppendRadioItem(bombsID_MEDIUM, wxT("&Medium (15x15)\tCtrl-2"));
  68. menuLevel->AppendRadioItem(bombsID_HARD, wxT("&Hard (25x20)\tCtrl-3"));
  69. menuFile->Append(wxID_NEW, wxT("&New game\tCtrl-N"));
  70. menuFile->Append(bombsID_LEVEL, wxT("&Level"),menuLevel, wxT("Starts a new game"));
  71. menuFile->AppendCheckItem(bombsID_EASYCORNER, wxT("&Easy corner"));
  72. menuFile->AppendSeparator();
  73. menuFile->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT), wxT("Quits the application"));
  74. menuBar->Append(menuFile, wxT("&File"));
  75. wxMenu *menuHelp = new wxMenu;
  76. menuHelp->Append(wxID_ABOUT, wxT("&About"),
  77. wxT("Displays the program information") );
  78. menuBar->Append(menuHelp, wxT("&Help"));
  79. SetMenuBar(menuBar);
  80. // Create child subwindows.
  81. m_canvas = new BombsCanvas(this, m_game);
  82. // Ensure the subwindows get resized o.k.
  83. // OnSize(width, height);
  84. // Centre frame on the screen.
  85. Centre(wxBOTH);
  86. // Show the frame.
  87. Show();
  88. }
  89. void BombsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
  90. {
  91. Close();
  92. }
  93. void BombsFrame::NewGame(int level, bool query)
  94. {
  95. if(query)
  96. {
  97. int ok = wxMessageBox(
  98. wxT("Start new game regardless previous board?"),
  99. wxT("Confirm"),
  100. wxYES_NO | wxICON_QUESTION,
  101. this
  102. );
  103. if(ok!=wxYES)return;
  104. }
  105. int numHorzCells = 20, numVertCells = 20;
  106. m_lastLevel = level;
  107. switch(level)
  108. {
  109. case bombsID_EASY:
  110. numHorzCells = numVertCells = 10;
  111. break;
  112. case bombsID_MEDIUM:
  113. numHorzCells = numVertCells = 15;
  114. break;
  115. case bombsID_HARD:
  116. numHorzCells = 25; numVertCells = 20;
  117. break;
  118. default :
  119. wxFAIL_MSG(wxT("Invalid level"));
  120. break;
  121. }
  122. m_game->Init(numHorzCells, numVertCells, m_easyCorner);
  123. GetMenuBar()->Check(level, true);
  124. m_canvas->UpdateGridSize();
  125. SetClientSize(m_canvas->GetGridSizeInPixels());
  126. }
  127. void BombsFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  128. {
  129. wxMessageBox(
  130. wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"),
  131. wxT("About wxBombs") );
  132. }
  133. void BombsFrame::OnNewGame(wxCommandEvent& WXUNUSED(event))
  134. {
  135. NewGame(m_lastLevel, true);
  136. }
  137. void BombsFrame::OnEasyGame(wxCommandEvent& WXUNUSED(event))
  138. {
  139. NewGame(bombsID_EASY, true);
  140. }
  141. void BombsFrame::OnMediumGame(wxCommandEvent& WXUNUSED(event))
  142. {
  143. NewGame(bombsID_MEDIUM, true);
  144. }
  145. void BombsFrame::OnHardGame(wxCommandEvent& WXUNUSED(event))
  146. {
  147. NewGame(bombsID_HARD, true);
  148. }
  149. void BombsFrame::OnEasyCorner(wxCommandEvent& WXUNUSED(event))
  150. {
  151. wxString msg;
  152. if(m_easyCorner)
  153. msg = wxT("enable");
  154. else
  155. msg = wxT("disable");
  156. msg = wxT("Do you really want to ") + msg + wxT(" having\ntop left corner always empty for easier start?");
  157. int ok = wxMessageBox(
  158. msg,
  159. wxT("Confirm"),
  160. wxYES_NO | wxICON_QUESTION,
  161. this
  162. );
  163. if(ok!=wxYES)return;
  164. m_easyCorner = !m_easyCorner;
  165. NewGame(m_lastLevel, true);
  166. }
  167. BEGIN_EVENT_TABLE(BombsCanvas, wxPanel)
  168. EVT_PAINT(BombsCanvas::OnPaint)
  169. EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent)
  170. EVT_CHAR(BombsCanvas::OnChar)
  171. END_EVENT_TABLE()
  172. BombsCanvas::BombsCanvas(wxFrame *parent, BombsGame *game)
  173. : wxPanel(parent, wxID_ANY)
  174. {
  175. m_game = game;
  176. int sx, sy;
  177. wxClientDC dc(this);
  178. wxFont font= BOMBS_FONT;
  179. dc.SetFont(font);
  180. wxCoord chw, chh;
  181. wxString buf = wxT("M");
  182. dc.GetTextExtent(buf, &chw, &chh);
  183. dc.SetFont(wxNullFont);
  184. dc.SetMapMode(wxMM_METRIC);
  185. int xcm = dc.LogicalToDeviceX(10);
  186. int ycm = dc.LogicalToDeviceY(10);
  187. // To have a square cell, there must be :
  188. // sx*ycm == sy*xcm
  189. if (chw*ycm < chh*xcm)
  190. {
  191. sy = chh;
  192. sx = chh*xcm/ycm;
  193. }
  194. else
  195. {
  196. sx = chw;
  197. sy = chw*ycm/xcm;
  198. }
  199. m_cellWidth = (sx+3+X_UNIT)/X_UNIT;
  200. m_cellHeight = (sy+3+Y_UNIT)/Y_UNIT;
  201. dc.SetMapMode(wxMM_TEXT);
  202. m_bmp = NULL;
  203. }
  204. BombsCanvas::~BombsCanvas()
  205. {
  206. if (m_bmp)
  207. {
  208. delete m_bmp;
  209. m_bmp = NULL;
  210. }
  211. }
  212. // Called when canvas needs to be repainted.
  213. void BombsCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
  214. {
  215. wxPaintDC dc(this);
  216. const int numHorzCells = m_game->GetWidth();
  217. const int numVertCells = m_game->GetHeight();
  218. // Insert your drawing code here.
  219. if (!m_bmp)
  220. {
  221. wxSize size = dc.GetSize();
  222. m_bmp = new wxBitmap(size.GetWidth(), size.GetHeight());
  223. if (m_bmp)
  224. {
  225. wxMemoryDC memDC;
  226. memDC.SelectObject(*m_bmp);
  227. DrawField(&memDC, 0, 0, numHorzCells-1, numVertCells-1);
  228. memDC.SelectObject(wxNullBitmap);
  229. }
  230. }
  231. if (m_bmp)
  232. {
  233. wxMemoryDC memDC;
  234. memDC.SelectObject(*m_bmp);
  235. wxSize size = dc.GetSize();
  236. dc.Blit(0, 0, size.GetWidth(), size.GetHeight(),
  237. &memDC, 0, 0, wxCOPY);
  238. memDC.SelectObject(wxNullBitmap);
  239. }
  240. else
  241. {
  242. DrawField(&dc, 0, 0, numHorzCells-1, numVertCells-1);
  243. }
  244. }
  245. void BombsCanvas::UpdateGridSize()
  246. {
  247. if (m_bmp)
  248. {
  249. delete m_bmp;
  250. m_bmp = NULL;
  251. }
  252. SetSize(GetGridSizeInPixels());
  253. Refresh();
  254. }
  255. wxSize BombsCanvas::GetGridSizeInPixels() const
  256. {
  257. return wxSize(m_cellWidth*X_UNIT*m_game->GetWidth(),
  258. m_cellHeight*Y_UNIT*m_game->GetHeight());
  259. }