]>
git.saurik.com Git - wxWidgets.git/blob - demos/bombs/bombs.cpp
1 ///////////////////////////////////////////////////////////////////////////////
4 // Author: P. Foggia 1996
8 // Copyright: (c) 1996 P. Foggia
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 #include "wx/wxprec.h"
20 #endif //precompiled headers
27 #if defined(__WXGTK__) || defined(__WXMOTIF__)
31 IMPLEMENT_APP(AppClass
)
33 // Called to initialize the program
34 bool AppClass::OnInit()
36 srand((unsigned)time(NULL
));
38 // Initialize all the top-level window members to NULL.
43 new BombsFrameClass(NULL
, "wxBombs", wxPoint(155, 165), wxSize(300, 300), wxMINIMIZE_BOX
| wxSYSTEM_MENU
| wxCAPTION
);
45 int xmax
=BombsFrame
->BombsCanvas
->field_width
*BombsFrame
->BombsCanvas
->x_cell
*X_UNIT
;
46 int ymax
=BombsFrame
->BombsCanvas
->field_height
*BombsFrame
->BombsCanvas
->y_cell
*Y_UNIT
;
47 BombsFrame
->SetClientSize(xmax
, ymax
);
52 BEGIN_EVENT_TABLE(BombsFrameClass
, wxFrame
)
53 EVT_MENU(IDM_EASY
, BombsFrameClass::OnEasy
)
54 EVT_MENU(IDM_MEDIUM
, BombsFrameClass::OnMedium
)
55 EVT_MENU(IDM_DIFFICULT
, BombsFrameClass::OnDifficult
)
56 EVT_MENU(IDM_EXIT
, BombsFrameClass::OnExit
)
57 EVT_MENU(IDM_ABOUT
, BombsFrameClass::OnAbout
)
58 EVT_MENU(IDM_RESTART
, BombsFrameClass::OnRestart
)
59 EVT_CLOSE(BombsFrameClass::OnCloseWindow
)
62 BombsFrameClass::BombsFrameClass(wxFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
):
63 wxFrame(parent
, -1, title
, pos
, size
, style
)
65 // Initialize child subwindow members.
68 SetIcon(wxICON(bombs
));
72 // Create a menu bar for the frame
73 wxMenuBar
*menuBar1
= new wxMenuBar
;
74 wxMenu
*menu1
= new wxMenu
;
75 menu1
->Append(IDM_EXIT
, "E&xit"); // , "Quit the program");
76 menu1
->AppendSeparator();
77 menu1
->Append(IDM_ABOUT
, "&About..."); // , "Infos on wxBombs");
78 menuBar1
->Append(menu1
, "&File");
79 wxMenu
*menu2
= new wxMenu
;
80 menu2
->Append(IDM_RESTART
, "&Restart"); // , "Clear the play field");
81 menu2
->AppendSeparator();
82 menu2
->Append(IDM_EASY
, "&Easy", wxEmptyString
, TRUE
); // "10x10 play field", TRUE);
83 menu2
->Append(IDM_MEDIUM
, "&Medium", wxEmptyString
, TRUE
); // "15x15 play field", TRUE);
84 menu2
->Append(IDM_DIFFICULT
, "&Difficult", wxEmptyString
, TRUE
); // "25x20 play field", TRUE);
85 menuBar1
->Append(menu2
, "&Game");
88 menuBar
->Check(wxGetApp().level
, TRUE
);
90 // Create child subwindows.
91 BombsCanvas
= new BombsCanvasClass(this);
93 // Ensure the subwindows get resized o.k.
94 // OnSize(width, height);
96 // Centre frame on the screen.
103 BombsFrameClass::~BombsFrameClass(void)
107 void BombsFrameClass::OnCloseWindow(wxCloseEvent
& event
)
112 void BombsFrameClass::OnExit(wxCommandEvent
& event
)
117 void BombsFrameClass::OnRestart(wxCommandEvent
& event
)
119 BombsCanvas
->UpdateFieldSize();
120 int xmax
=BombsCanvas
->field_width
*BombsCanvas
->x_cell
*X_UNIT
;
121 int ymax
=BombsCanvas
->field_height
*BombsCanvas
->y_cell
*Y_UNIT
;
122 wxGetApp().BombsFrame
->SetClientSize(xmax
, ymax
);
125 void BombsFrameClass::OnAbout(wxCommandEvent
& event
)
127 wxMessageBox("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>", "About wxBombs");
130 void BombsFrameClass::OnEasy(wxCommandEvent
& event
)
132 menuBar
->Check(wxGetApp().level
, FALSE
);
133 wxGetApp().level
=IDM_EASY
;
134 menuBar
->Check(wxGetApp().level
, TRUE
);
137 void BombsFrameClass::OnMedium(wxCommandEvent
& event
)
139 menuBar
->Check(wxGetApp().level
, FALSE
);
140 wxGetApp().level
=IDM_MEDIUM
;
141 menuBar
->Check(wxGetApp().level
, TRUE
);
144 void BombsFrameClass::OnDifficult(wxCommandEvent
& event
)
146 menuBar
->Check(wxGetApp().level
, FALSE
);
147 wxGetApp().level
=IDM_DIFFICULT
;
148 menuBar
->Check(wxGetApp().level
, TRUE
);
151 BEGIN_EVENT_TABLE(BombsCanvasClass
, wxWindow
)
152 EVT_PAINT(BombsCanvasClass::OnPaint
)
153 EVT_MOUSE_EVENTS(BombsCanvasClass::OnEvent
)
156 BombsCanvasClass::BombsCanvasClass(wxFrame
*parent
, const wxPoint
& pos
, const wxSize
& size
, long style
):
157 wxWindow(parent
, -1, pos
, size
, style
)
161 wxFont font
= BOMBS_FONT
;
167 dc
.GetTextExtent(buf
, &chw
, &chh
);
168 dc
.SetFont(wxNullFont
);
170 dc
.SetMapMode(wxMM_METRIC
);
172 int xcm
= dc
.LogicalToDeviceX(10.0);
173 int ycm
= dc
.LogicalToDeviceY(10.0);
174 // To have a square cell, there must be :
176 if (chw
*ycm
< chh
*xcm
)
184 x_cell
= (sx
+3+X_UNIT
)/X_UNIT
;
185 y_cell
= (sy
+3+Y_UNIT
)/Y_UNIT
;
186 dc
.SetMapMode(wxMM_TEXT
);
191 BombsCanvasClass::~BombsCanvasClass(void)
197 // Called when canvas needs to be repainted.
198 void BombsCanvasClass::OnPaint(wxPaintEvent
& event
)
202 // Insert your drawing code here.
204 { bmp
=new wxBitmap(field_width
*x_cell
*X_UNIT
+1,
205 field_height
*y_cell
*Y_UNIT
+1);
208 memDC
.SelectObject(* bmp
);
209 DrawField(&memDC
, 0, 0, field_width
-1, field_height
-1);
210 memDC
.SelectObject(wxNullBitmap
);
215 memDC
.SelectObject(* bmp
);
216 dc
.Blit(0, 0, field_width
*x_cell
*X_UNIT
+1,
217 field_height
*y_cell
*Y_UNIT
+1,
218 &memDC
, 0, 0, wxCOPY
);
219 memDC
.SelectObject(wxNullBitmap
);
222 DrawField(& dc
, 0, 0, field_width
-1, field_height
-1);
225 // Updates the field size depending on wxGetApp().level and
226 // redraws the canvas
227 void BombsCanvasClass::UpdateFieldSize()
231 switch(wxGetApp().level
)
245 wxGetApp().Game
.Init(field_width
, field_height
);