git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5221
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
toolBar->AddTool(a, b, wxNullBitmap, FALSE, -1, -1, (wxObject *)0, c, d)
#define GET_FRAME() \
toolBar->AddTool(a, b, wxNullBitmap, FALSE, -1, -1, (wxObject *)0, c, d)
#define GET_FRAME() \
- ((wxFrame *) wxGetApp().GetTopWindow())
+ ((LifeFrame *) wxGetApp().GetTopWindow())
// --------------------------------------------------------------------------
// headers
// --------------------------------------------------------------------------
// headers
inline bool HasChanged(int x, int y) const;
inline void SetCell(int x, int y, bool alive = TRUE);
inline bool HasChanged(int x, int y) const;
inline void SetCell(int x, int y, bool alive = TRUE);
private:
enum CellFlags {
private:
enum CellFlags {
wxBitmap *m_bmp;
int m_height;
int m_width;
wxBitmap *m_bmp;
int m_height;
int m_width;
class LifeTimer : public wxTimer
{
public:
class LifeTimer : public wxTimer
{
public:
- LifeTimer(LifeFrame *parent);
-
-private:
- LifeFrame *m_parent;
// event handlers
void OnMenu(wxCommandEvent& event);
// event handlers
void OnMenu(wxCommandEvent& event);
- void OnSlider(wxScrollEvent& event);
- void OnNewGame();
+ void OnNewGame(wxCommandEvent& event);
void OnStart();
void OnStop();
void OnTimer();
void OnStart();
void OnStop();
void OnTimer();
+ void OnSlider(wxScrollEvent& event);
private:
// any class wishing to process wxWindows events must use this macro
private:
// any class wishing to process wxWindows events must use this macro
// event handlers
void OnOK(wxCommandEvent& event);
// event handlers
void OnOK(wxCommandEvent& event);
- void OnCancel(wxCommandEvent& event);
private:
// any class wishing to process wxWindows events must use this macro
private:
// any class wishing to process wxWindows events must use this macro
enum
{
// menu items and toolbar buttons
enum
{
// menu items and toolbar buttons
ID_CLEAR,
ID_START,
ID_STOP,
ID_CLEAR,
ID_START,
ID_STOP,
};
// --------------------------------------------------------------------------
};
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
// Event tables
// --------------------------------------------------------------------------
// Event tables
BEGIN_EVENT_TABLE(LifeFrame, wxFrame)
BEGIN_EVENT_TABLE(LifeFrame, wxFrame)
- EVT_MENU_RANGE (ID_NEWGAME, ID_ABOUT, LifeFrame::OnMenu)
- EVT_COMMAND_SCROLL (ID_SLIDER, LifeFrame::OnSlider)
-END_EVENT_TABLE()
+ EVT_MENU (ID_NEWGAME, LifeFrame::OnNewGame)
+ EVT_MENU (ID_CLEAR, LifeFrame::OnMenu)
+ EVT_MENU (ID_START, LifeFrame::OnMenu)
+ EVT_MENU (ID_STOP, LifeFrame::OnMenu)
+ EVT_MENU (ID_ABOUT, LifeFrame::OnMenu)
+ EVT_MENU (ID_EXIT, LifeFrame::OnMenu)
+ EVT_COMMAND_SCROLL (ID_SLIDER, LifeFrame::OnSlider)
+END_EVENT_TABLE()
BEGIN_EVENT_TABLE(LifeCanvas, wxScrolledWindow)
BEGIN_EVENT_TABLE(LifeCanvas, wxScrolledWindow)
- EVT_PAINT ( LifeCanvas::OnPaint)
- EVT_SIZE ( LifeCanvas::OnSize)
- EVT_MOUSE_EVENTS ( LifeCanvas::OnMouse)
+ EVT_PAINT ( LifeCanvas::OnPaint)
+ EVT_SIZE ( LifeCanvas::OnSize)
+ EVT_MOUSE_EVENTS ( LifeCanvas::OnMouse)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(LifeNewGameDialog, wxDialog)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(LifeNewGameDialog, wxDialog)
- EVT_BUTTON (wxID_OK, LifeNewGameDialog::OnOK)
- EVT_BUTTON (wxID_CANCEL, LifeNewGameDialog::OnCancel)
+ EVT_BUTTON (wxID_OK, LifeNewGameDialog::OnOK)
// game
m_life = new Life(20, 20);
m_canvas = new LifeCanvas(panel, m_life);
// game
m_life = new Life(20, 20);
m_canvas = new LifeCanvas(panel, m_life);
- m_timer = new LifeTimer(this);
+ m_timer = new LifeTimer();
m_interval = 500;
m_tics = 0;
m_text = new wxStaticText(panel, -1, "");
m_interval = 500;
m_tics = 0;
m_text = new wxStaticText(panel, -1, "");
{
case ID_START : OnStart(); break;
case ID_STOP : OnStop(); break;
{
case ID_START : OnStart(); break;
case ID_STOP : OnStop(); break;
- case ID_NEWGAME : OnNewGame(); break;
case ID_CLEAR :
{
OnStop();
case ID_CLEAR :
{
OnStop();
-void LifeFrame::OnSlider(wxScrollEvent& event)
-{
- m_interval = event.GetPosition() * 100;
-
- // restart timer if running, to set the new interval
- if (m_running)
- {
- m_timer->Stop();
- m_timer->Start(m_interval);
- }
-
- UpdateInfoText();
-}
-
-void LifeFrame::OnNewGame()
+void LifeFrame::OnNewGame(wxCommandEvent& WXUNUSED(event))
{
int w = m_life->GetWidth();
int h = m_life->GetHeight();
{
int w = m_life->GetWidth();
int h = m_life->GetHeight();
void LifeFrame::OnTimer()
{
void LifeFrame::OnTimer()
{
- m_tics++;
- UpdateInfoText();
+ if (m_life->NextTic())
+ m_tics++;
+ else
+ OnStop();
m_canvas->DrawEverything();
m_canvas->Refresh(FALSE);
}
m_canvas->DrawEverything();
m_canvas->Refresh(FALSE);
}
+void LifeFrame::OnSlider(wxScrollEvent& event)
+{
+ m_interval = event.GetPosition() * 100;
+
+ // restart timer if running, to set the new interval
+ if (m_running)
+ {
+ m_timer->Stop();
+ m_timer->Start(m_interval);
+ }
+
+ UpdateInfoText();
+}
+
// --------------------------------------------------------------------------
// LifeTimer
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
// LifeTimer
// --------------------------------------------------------------------------
-LifeTimer::LifeTimer(LifeFrame *parent) : wxTimer()
+/*
+LifeTimer::LifeTimer() : wxTimer()
void LifeTimer::Notify()
{
void LifeTimer::Notify()
{
+ GET_FRAME()->OnTimer();
}
// --------------------------------------------------------------------------
}
// --------------------------------------------------------------------------
-void LifeNewGameDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
-{
- *m_w = -1;
- *m_h = -1;
-
- EndModal(wxID_CANCEL);
-}
-
// --------------------------------------------------------------------------
// Life
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
// Life
// --------------------------------------------------------------------------
{
wxASSERT(x < m_width || y < m_height);
{
wxASSERT(x < m_width || y < m_height);
- // set the CELL_MARK flag to notify that this cell has changed
m_cells[y * m_width + x] = (alive? CELL_ALIVE : CELL_DEAD);
}
m_cells[y * m_width + x] = (alive? CELL_ALIVE : CELL_DEAD);
}
/* 1st pass. Find and mark deaths and births for this generation.
*
* Rules:
/* 1st pass. Find and mark deaths and births for this generation.
*
* Rules:
* thus must be updated in the screen.
*/
if (m_cells[j * m_width + i] & CELL_MARK)
* thus must be updated in the screen.
*/
if (m_cells[j * m_width + i] & CELL_MARK)
m_cells[j * m_width + i] ^= CELL_ALIVE;
m_cells[j * m_width + i] ^= CELL_ALIVE;
+
+ return (changed != 0);
}
int Life::GetNeighbors(int x, int y) const
}
int Life::GetNeighbors(int x, int y) const