Some little changes
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5221 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
toolBar->AddTool(a, b, wxNullBitmap, FALSE, -1, -1, (wxObject *)0, c, d)
|
toolBar->AddTool(a, b, wxNullBitmap, FALSE, -1, -1, (wxObject *)0, c, d)
|
||||||
|
|
||||||
#define GET_FRAME() \
|
#define GET_FRAME() \
|
||||||
((wxFrame *) wxGetApp().GetTopWindow())
|
((LifeFrame *) wxGetApp().GetTopWindow())
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
// headers
|
// headers
|
||||||
@@ -90,9 +90,9 @@ public:
|
|||||||
inline bool HasChanged(int x, int y) const;
|
inline bool HasChanged(int x, int y) const;
|
||||||
inline void SetCell(int x, int y, bool alive = TRUE);
|
inline void SetCell(int x, int y, bool alive = TRUE);
|
||||||
|
|
||||||
// game operations
|
// member funcions
|
||||||
void Clear();
|
void Clear();
|
||||||
void NextTic();
|
bool NextTic();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum CellFlags {
|
enum CellFlags {
|
||||||
@@ -142,7 +142,6 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
Life *m_life;
|
Life *m_life;
|
||||||
wxFrame *m_frame;
|
|
||||||
wxBitmap *m_bmp;
|
wxBitmap *m_bmp;
|
||||||
int m_height;
|
int m_height;
|
||||||
int m_width;
|
int m_width;
|
||||||
@@ -156,11 +155,7 @@ private:
|
|||||||
class LifeTimer : public wxTimer
|
class LifeTimer : public wxTimer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LifeTimer(LifeFrame *parent);
|
|
||||||
void Notify();
|
void Notify();
|
||||||
|
|
||||||
private:
|
|
||||||
LifeFrame *m_parent;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Life main frame
|
// Life main frame
|
||||||
@@ -176,11 +171,11 @@ public:
|
|||||||
|
|
||||||
// event handlers
|
// event handlers
|
||||||
void OnMenu(wxCommandEvent& event);
|
void OnMenu(wxCommandEvent& event);
|
||||||
void OnSlider(wxScrollEvent& event);
|
void OnNewGame(wxCommandEvent& event);
|
||||||
void OnNewGame();
|
|
||||||
void OnStart();
|
void OnStart();
|
||||||
void OnStop();
|
void OnStop();
|
||||||
void OnTimer();
|
void OnTimer();
|
||||||
|
void OnSlider(wxScrollEvent& event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// any class wishing to process wxWindows events must use this macro
|
// any class wishing to process wxWindows events must use this macro
|
||||||
@@ -204,7 +199,6 @@ public:
|
|||||||
|
|
||||||
// event handlers
|
// event handlers
|
||||||
void OnOK(wxCommandEvent& event);
|
void OnOK(wxCommandEvent& event);
|
||||||
void OnCancel(wxCommandEvent& event);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// any class wishing to process wxWindows events must use this macro
|
// any class wishing to process wxWindows events must use this macro
|
||||||
@@ -232,7 +226,7 @@ public:
|
|||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
// menu items and toolbar buttons
|
// menu items and toolbar buttons
|
||||||
ID_NEWGAME = 101,
|
ID_NEWGAME = 1001,
|
||||||
ID_CLEAR,
|
ID_CLEAR,
|
||||||
ID_START,
|
ID_START,
|
||||||
ID_STOP,
|
ID_STOP,
|
||||||
@@ -240,7 +234,7 @@ enum
|
|||||||
ID_ABOUT,
|
ID_ABOUT,
|
||||||
|
|
||||||
// slider
|
// slider
|
||||||
ID_SLIDER
|
ID_SLIDER,
|
||||||
};
|
};
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
@@ -248,21 +242,24 @@ enum
|
|||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
// Event tables
|
// Event tables
|
||||||
|
|
||||||
BEGIN_EVENT_TABLE(LifeFrame, wxFrame)
|
BEGIN_EVENT_TABLE(LifeFrame, wxFrame)
|
||||||
EVT_MENU_RANGE (ID_NEWGAME, ID_ABOUT, LifeFrame::OnMenu)
|
EVT_MENU (ID_NEWGAME, LifeFrame::OnNewGame)
|
||||||
EVT_COMMAND_SCROLL (ID_SLIDER, LifeFrame::OnSlider)
|
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()
|
END_EVENT_TABLE()
|
||||||
|
|
||||||
BEGIN_EVENT_TABLE(LifeCanvas, wxScrolledWindow)
|
BEGIN_EVENT_TABLE(LifeCanvas, wxScrolledWindow)
|
||||||
EVT_PAINT ( LifeCanvas::OnPaint)
|
EVT_PAINT ( LifeCanvas::OnPaint)
|
||||||
EVT_SIZE ( LifeCanvas::OnSize)
|
EVT_SIZE ( LifeCanvas::OnSize)
|
||||||
EVT_MOUSE_EVENTS ( LifeCanvas::OnMouse)
|
EVT_MOUSE_EVENTS ( LifeCanvas::OnMouse)
|
||||||
END_EVENT_TABLE()
|
END_EVENT_TABLE()
|
||||||
|
|
||||||
BEGIN_EVENT_TABLE(LifeNewGameDialog, wxDialog)
|
BEGIN_EVENT_TABLE(LifeNewGameDialog, wxDialog)
|
||||||
EVT_BUTTON (wxID_OK, LifeNewGameDialog::OnOK)
|
EVT_BUTTON (wxID_OK, LifeNewGameDialog::OnOK)
|
||||||
EVT_BUTTON (wxID_CANCEL, LifeNewGameDialog::OnCancel)
|
|
||||||
END_EVENT_TABLE()
|
END_EVENT_TABLE()
|
||||||
|
|
||||||
|
|
||||||
@@ -343,7 +340,7 @@ LifeFrame::LifeFrame() : wxFrame((wxFrame *)0, -1, _("Life!"), wxPoint(50, 50))
|
|||||||
// game
|
// game
|
||||||
m_life = new Life(20, 20);
|
m_life = new Life(20, 20);
|
||||||
m_canvas = new LifeCanvas(panel, m_life);
|
m_canvas = new LifeCanvas(panel, m_life);
|
||||||
m_timer = new LifeTimer(this);
|
m_timer = new LifeTimer();
|
||||||
m_interval = 500;
|
m_interval = 500;
|
||||||
m_tics = 0;
|
m_tics = 0;
|
||||||
m_text = new wxStaticText(panel, -1, "");
|
m_text = new wxStaticText(panel, -1, "");
|
||||||
@@ -386,7 +383,6 @@ void LifeFrame::OnMenu(wxCommandEvent& event)
|
|||||||
{
|
{
|
||||||
case ID_START : OnStart(); break;
|
case ID_START : OnStart(); break;
|
||||||
case ID_STOP : OnStop(); break;
|
case ID_STOP : OnStop(); break;
|
||||||
case ID_NEWGAME : OnNewGame(); break;
|
|
||||||
case ID_CLEAR :
|
case ID_CLEAR :
|
||||||
{
|
{
|
||||||
OnStop();
|
OnStop();
|
||||||
@@ -416,21 +412,7 @@ void LifeFrame::OnMenu(wxCommandEvent& event)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LifeFrame::OnSlider(wxScrollEvent& event)
|
void LifeFrame::OnNewGame(wxCommandEvent& WXUNUSED(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()
|
|
||||||
{
|
{
|
||||||
int w = m_life->GetWidth();
|
int w = m_life->GetWidth();
|
||||||
int h = m_life->GetHeight();
|
int h = m_life->GetHeight();
|
||||||
@@ -491,26 +473,43 @@ void LifeFrame::OnStop()
|
|||||||
|
|
||||||
void LifeFrame::OnTimer()
|
void LifeFrame::OnTimer()
|
||||||
{
|
{
|
||||||
m_tics++;
|
if (m_life->NextTic())
|
||||||
UpdateInfoText();
|
m_tics++;
|
||||||
|
else
|
||||||
|
OnStop();
|
||||||
|
|
||||||
m_life->NextTic();
|
UpdateInfoText();
|
||||||
m_canvas->DrawEverything();
|
m_canvas->DrawEverything();
|
||||||
m_canvas->Refresh(FALSE);
|
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()
|
||||||
{
|
{
|
||||||
m_parent = parent;
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
void LifeTimer::Notify()
|
void LifeTimer::Notify()
|
||||||
{
|
{
|
||||||
m_parent->OnTimer();
|
GET_FRAME()->OnTimer();
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
@@ -766,14 +765,6 @@ void LifeNewGameDialog::OnOK(wxCommandEvent& WXUNUSED(event))
|
|||||||
EndModal(wxID_OK);
|
EndModal(wxID_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LifeNewGameDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
|
|
||||||
{
|
|
||||||
*m_w = -1;
|
|
||||||
*m_h = -1;
|
|
||||||
|
|
||||||
EndModal(wxID_CANCEL);
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
// Life
|
// Life
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
@@ -827,12 +818,13 @@ void Life::SetCell(int x, int y, bool alive)
|
|||||||
{
|
{
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Life::NextTic()
|
bool Life::NextTic()
|
||||||
{
|
{
|
||||||
|
long changed = 0;
|
||||||
|
|
||||||
/* 1st pass. Find and mark deaths and births for this generation.
|
/* 1st pass. Find and mark deaths and births for this generation.
|
||||||
*
|
*
|
||||||
* Rules:
|
* Rules:
|
||||||
@@ -869,8 +861,13 @@ void Life::NextTic()
|
|||||||
* thus must be updated in the screen.
|
* thus must be updated in the screen.
|
||||||
*/
|
*/
|
||||||
if (m_cells[j * m_width + i] & CELL_MARK)
|
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;
|
||||||
|
changed++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (changed != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Life::GetNeighbors(int x, int y) const
|
int Life::GetNeighbors(int x, int y) const
|
||||||
|
Reference in New Issue
Block a user