]>
git.saurik.com Git - wxWidgets.git/blob - samples/life/life.cpp
9bcd0bbf58b781ceb3cbb1954ce920b78e9ddf7d
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: The game of life, created by J. H. Conway
4 // Author: Guillermo Rodriguez Garcia, <guille@iies.es>
8 // Copyright: (c) 2000, Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ==========================================================================
14 // ==========================================================================
16 // minimum and maximum table size, in each dimension
21 #define ADD_TOOL(a, b, c, d) \
22 toolBar->AddTool(a, b, wxNullBitmap, FALSE, -1, -1, (wxObject *)0, c, d)
25 ((LifeFrame *) wxGetApp().GetTopWindow())
27 // --------------------------------------------------------------------------
29 // --------------------------------------------------------------------------
32 #pragma implementation "life.cpp"
33 #pragma interface "life.cpp"
36 // for compilers that support precompilation, includes "wx/wx.h".
37 #include "wx/wxprec.h"
43 // for all others, include the necessary headers
48 #include "wx/statline.h"
49 #include "wx/spinctrl.h"
51 // --------------------------------------------------------------------------
53 // --------------------------------------------------------------------------
55 #if defined(__WXGTK__) || defined(__WXMOTIF__)
56 // the application icon
57 #include "mondrian.xpm"
59 // bitmap buttons for the toolbar
60 #include "bitmaps/reset.xpm"
61 #include "bitmaps/play.xpm"
62 #include "bitmaps/stop.xpm"
65 // --------------------------------------------------------------------------
67 // --------------------------------------------------------------------------
75 class LifeNewGameDialog
;
76 class LifeSamplesDialog
;
78 // --------------------------------------------------------------------------
80 // --------------------------------------------------------------------------
87 Life(int width
, int height
);
89 void Create(int width
, int height
);
93 inline int GetWidth() const { return m_width
; };
94 inline int GetHeight() const { return m_height
; };
95 inline bool IsAlive(int x
, int y
) const;
96 inline bool HasChanged(int x
, int y
) const;
99 void SetBorderWrap(bool on
);
103 void SetCell(int x
, int y
, bool alive
= TRUE
);
104 void SetShape(LifeShape
&shape
);
109 CELL_DEAD
= 0x0000, // is dead
110 CELL_ALIVE
= 0x0001, // is alive
111 CELL_MARK
= 0x0002, // will change / has changed
115 int GetNeighbors(int x
, int y
) const;
116 inline void SetCell(int x
, int y
, Cell status
);
128 LifeShape::LifeShape(wxString name
,
130 int width
, int height
, char *data
,
131 int fieldWidth
= 20, int fieldHeight
= 20,
139 m_fieldWidth
= fieldWidth
;
140 m_fieldHeight
= fieldHeight
;
154 // --------------------------------------------------------------------------
156 // --------------------------------------------------------------------------
159 class LifeCanvas
: public wxScrolledWindow
163 LifeCanvas(wxWindow
* parent
, Life
* life
, bool interactive
= TRUE
);
168 void DrawEverything(bool force
= FALSE
);
169 void DrawCell(int i
, int j
);
170 void DrawCell(int i
, int j
, wxDC
&dc
);
171 inline int CellToCoord(int i
) const { return (i
* m_cellsize
); };
172 inline int CoordToCell(int x
) const { return ((x
>= 0)? (x
/ m_cellsize
) : -1); };
175 void OnPaint(wxPaintEvent
& event
);
176 void OnMouse(wxMouseEvent
& event
);
177 void OnSize(wxSizeEvent
& event
);
180 // any class wishing to process wxWindows events must use this macro
181 DECLARE_EVENT_TABLE()
196 MouseStatus m_status
;
201 class LifeTimer
: public wxTimer
208 class LifeFrame
: public wxFrame
216 void UpdateInfoText();
219 void OnMenu(wxCommandEvent
& event
);
220 void OnNewGame(wxCommandEvent
& event
);
221 void OnSamples(wxCommandEvent
& event
);
225 void OnSlider(wxScrollEvent
& event
);
228 // any class wishing to process wxWindows events must use this macro
229 DECLARE_EVENT_TABLE()
233 LifeCanvas
*m_canvas
;
234 wxStaticText
*m_text
;
240 // Life new game dialog
241 class LifeNewGameDialog
: public wxDialog
245 LifeNewGameDialog(wxWindow
*parent
, int *w
, int *h
);
248 void OnOK(wxCommandEvent
& event
);
251 // any class wishing to process wxWindows events must use this macro
252 DECLARE_EVENT_TABLE();
256 wxSpinCtrl
*m_spinctrlw
;
257 wxSpinCtrl
*m_spinctrlh
;
260 // Life sample configurations dialog
261 class LifeSamplesDialog
: public wxDialog
265 LifeSamplesDialog(wxWindow
*parent
);
266 ~LifeSamplesDialog();
272 void OnListBox(wxCommandEvent
&event
);
275 // any class wishing to process wxWindows events must use this macro
276 DECLARE_EVENT_TABLE();
281 LifeCanvas
*m_canvas
;
286 class LifeApp
: public wxApp
289 virtual bool OnInit();
292 // --------------------------------------------------------------------------
294 // --------------------------------------------------------------------------
296 // IDs for the controls and the menu commands
299 // menu items and toolbar buttons
310 // speed selection slider
313 // listbox in samples dialog
318 // built-in sample games
319 #include "samples.inc"
321 // --------------------------------------------------------------------------
322 // event tables and other macros for wxWindows
323 // --------------------------------------------------------------------------
326 BEGIN_EVENT_TABLE(LifeFrame
, wxFrame
)
327 EVT_MENU (ID_NEWGAME
, LifeFrame::OnNewGame
)
328 EVT_MENU (ID_SAMPLES
, LifeFrame::OnSamples
)
329 EVT_MENU (ID_ABOUT
, LifeFrame::OnMenu
)
330 EVT_MENU (ID_EXIT
, LifeFrame::OnMenu
)
331 EVT_MENU (ID_CLEAR
, LifeFrame::OnMenu
)
332 EVT_MENU (ID_START
, LifeFrame::OnMenu
)
333 EVT_MENU (ID_STEP
, LifeFrame::OnMenu
)
334 EVT_MENU (ID_STOP
, LifeFrame::OnMenu
)
335 EVT_MENU (ID_WRAP
, LifeFrame::OnMenu
)
336 EVT_COMMAND_SCROLL (ID_SLIDER
, LifeFrame::OnSlider
)
339 BEGIN_EVENT_TABLE(LifeCanvas
, wxScrolledWindow
)
340 EVT_PAINT ( LifeCanvas::OnPaint
)
341 EVT_SIZE ( LifeCanvas::OnSize
)
342 EVT_MOUSE_EVENTS ( LifeCanvas::OnMouse
)
345 BEGIN_EVENT_TABLE(LifeNewGameDialog
, wxDialog
)
346 EVT_BUTTON (wxID_OK
, LifeNewGameDialog::OnOK
)
349 BEGIN_EVENT_TABLE(LifeSamplesDialog
, wxDialog
)
350 EVT_LISTBOX (ID_LISTBOX
, LifeSamplesDialog::OnListBox
)
354 // Create a new application object
355 IMPLEMENT_APP(LifeApp
)
357 // ==========================================================================
359 // ==========================================================================
361 // --------------------------------------------------------------------------
363 // --------------------------------------------------------------------------
365 // `Main program' equivalent: the program execution "starts" here
366 bool LifeApp::OnInit()
368 // create the main application window
369 LifeFrame
*frame
= new LifeFrame();
371 // show it and tell the application that it's our main window
375 // enter the main message loop and run the app
379 // --------------------------------------------------------------------------
381 // --------------------------------------------------------------------------
384 LifeFrame::LifeFrame() : wxFrame((wxFrame
*)0, -1, _("Life!"), wxPoint(50, 50))
387 SetIcon(wxICON(mondrian
));
390 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
391 wxMenu
*menuGame
= new wxMenu("", wxMENU_TEAROFF
);
393 menuFile
->Append(ID_NEWGAME
, _("New game..."), _("Start a new game"));
394 menuFile
->Append(ID_SAMPLES
, _("Sample game..."), _("Select a sample configuration"));
395 menuFile
->AppendSeparator();
396 menuFile
->Append(ID_ABOUT
, _("&About...\tCtrl-A"), _("Show about dialog"));
397 menuFile
->AppendSeparator();
398 menuFile
->Append(ID_EXIT
, _("E&xit\tAlt-X"), _("Quit this program"));
400 menuGame
->Append(ID_CLEAR
, _("&Clear\tCtrl-C"), _("Clear game field"));
401 menuGame
->Append(ID_START
, _("&Start\tCtrl-S"), _("Start"));
402 menuGame
->Append(ID_STEP
, _("&Next\tCtrl-N"), _("Single step"));
403 menuGame
->Append(ID_STOP
, _("S&top\tCtrl-T"), _("Stop"));
404 menuGame
->Enable(ID_STOP
, FALSE
);
405 menuGame
->AppendSeparator();
406 menuGame
->Append(ID_WRAP
, _("&Wraparound\tCtrl-W"), _("Wrap around borders"), TRUE
);
407 menuGame
->Check (ID_WRAP
, TRUE
);
410 wxMenuBar
*menuBar
= new wxMenuBar();
411 menuBar
->Append(menuFile
, _("&File"));
412 menuBar
->Append(menuGame
, _("&Game"));
416 wxBitmap tbBitmaps
[3];
417 tbBitmaps
[0] = wxBITMAP(reset
);
418 tbBitmaps
[1] = wxBITMAP(play
);
419 tbBitmaps
[2] = wxBITMAP(stop
);
421 wxToolBar
*toolBar
= CreateToolBar();
422 toolBar
->SetMargins(5, 5);
423 toolBar
->SetToolBitmapSize(wxSize(16, 16));
424 ADD_TOOL(ID_CLEAR
, tbBitmaps
[0], _("Clear"), _("Clear game board"));
425 ADD_TOOL(ID_START
, tbBitmaps
[1], _("Start"), _("Start"));
426 ADD_TOOL(ID_STOP
, tbBitmaps
[2], _("Stop"), _("Stop"));
427 toolBar
->EnableTool(ID_STOP
, FALSE
);
432 SetStatusText(_("Welcome to Life!"));
435 wxPanel
*panel
= new wxPanel(this, -1);
438 m_life
= new Life(20, 20);
439 m_canvas
= new LifeCanvas(panel
, m_life
);
440 m_timer
= new LifeTimer();
443 m_text
= new wxStaticText(panel
, -1, "");
447 wxSlider
*slider
= new wxSlider(panel
, ID_SLIDER
, 5, 1, 10,
448 wxDefaultPosition
, wxSize(200, -1), wxSL_HORIZONTAL
| wxSL_AUTOTICKS
);
451 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
452 sizer
->Add(new wxStaticLine(panel
, -1), 0, wxGROW
| wxCENTRE
);
453 sizer
->Add(m_canvas
, 1, wxGROW
| wxCENTRE
| wxALL
, 5);
454 sizer
->Add(new wxStaticLine(panel
, -1), 0, wxGROW
| wxCENTRE
);
455 sizer
->Add(m_text
, 0, wxCENTRE
| wxTOP
, 5);
456 sizer
->Add(slider
, 0, wxCENTRE
| wxALL
, 5);
457 panel
->SetSizer(sizer
);
458 panel
->SetAutoLayout(TRUE
);
460 sizer
->SetSizeHints(this);
463 LifeFrame::~LifeFrame()
469 void LifeFrame::UpdateInfoText()
473 msg
.Printf(_("Generation: %u, Interval: %u ms"), m_tics
, m_interval
);
474 m_text
->SetLabel(msg
);
478 void LifeFrame::OnMenu(wxCommandEvent
& event
)
480 switch (event
.GetId())
482 case ID_START
: OnStart(); break;
483 case ID_STEP
: OnTimer(); break;
484 case ID_STOP
: OnStop(); break;
487 bool checked
= GetMenuBar()->GetMenu(1)->IsChecked(ID_WRAP
);
488 m_life
->SetBorderWrap(checked
);
495 m_canvas
->DrawEverything(TRUE
);
496 m_canvas
->Refresh(FALSE
);
504 _("This is the about dialog of the Life! sample.\n"
505 "(c) 2000 Guillermo Rodriguez Garcia"),
507 wxOK
| wxICON_INFORMATION
,
513 // TRUE is to force the frame to close
520 void LifeFrame::OnNewGame(wxCommandEvent
& WXUNUSED(event
))
522 int w
= m_life
->GetWidth();
523 int h
= m_life
->GetHeight();
526 // stop if it was running
530 LifeNewGameDialog
dialog(this, &w
, &h
);
531 result
= dialog
.ShowModal();
534 if (result
== wxID_OK
)
537 if (w
>= LIFE_MIN
&& w
<= LIFE_MAX
&&
538 h
>= LIFE_MIN
&& h
<= LIFE_MAX
)
542 m_life
->Create(w
, h
);
553 msg
.Printf(_("Both dimensions must be within %u and %u.\n"),
555 wxMessageBox(msg
, _("Error!"), wxOK
| wxICON_EXCLAMATION
, this);
560 void LifeFrame::OnSamples(wxCommandEvent
& WXUNUSED(event
))
562 // stop if it was running
566 LifeSamplesDialog
dialog(this);
569 if (dialog
.ShowModal() == wxID_OK
)
571 int result
= dialog
.GetValue();
576 int gw
= g_shapes
[result
].m_fieldWidth
;
577 int gh
= g_shapes
[result
].m_fieldHeight
;
578 int wrap
= g_shapes
[result
].m_wrap
;
580 // set wraparound (don't ask the user)
581 m_life
->SetBorderWrap(wrap
);
582 GetMenuBar()->GetMenu(1)->Check(ID_WRAP
, wrap
);
584 // need to resize the game field?
585 if (gw
> m_life
->GetWidth() || gh
> m_life
->GetHeight())
588 s
.Printf(_("Your game field is too small for this configuration.\n"
589 "It is recommended to resize it to %u x %u. Proceed?\n"),
592 if (wxMessageBox(s
, _("Question"), wxYES_NO
| wxICON_QUESTION
, this) == wxYES
)
595 m_life
->Create(gw
, gh
);
600 m_life
->SetShape(g_shapes
[result
]);
602 // tell the canvas about the change
610 void LifeFrame::OnStart()
614 GetToolBar()->EnableTool(ID_START
, FALSE
);
615 GetToolBar()->EnableTool(ID_STOP
, TRUE
);
616 GetMenuBar()->GetMenu(1)->Enable(ID_START
, FALSE
);
617 GetMenuBar()->GetMenu(1)->Enable(ID_STEP
, FALSE
);
618 GetMenuBar()->GetMenu(1)->Enable(ID_STOP
, TRUE
);
620 m_timer
->Start(m_interval
);
625 void LifeFrame::OnStop()
629 GetToolBar()->EnableTool(ID_START
, TRUE
);
630 GetToolBar()->EnableTool(ID_STOP
, FALSE
);
631 GetMenuBar()->GetMenu(1)->Enable(ID_START
, TRUE
);
632 GetMenuBar()->GetMenu(1)->Enable(ID_STEP
, TRUE
);
633 GetMenuBar()->GetMenu(1)->Enable(ID_STOP
, FALSE
);
640 void LifeFrame::OnTimer()
642 if (m_life
->NextTic())
648 m_canvas
->DrawEverything();
649 m_canvas
->Refresh(FALSE
);
652 void LifeFrame::OnSlider(wxScrollEvent
& event
)
654 m_interval
= event
.GetPosition() * 100;
656 // restart timer if running, to set the new interval
660 m_timer
->Start(m_interval
);
666 // --------------------------------------------------------------------------
668 // --------------------------------------------------------------------------
670 void LifeTimer::Notify()
672 GET_FRAME()->OnTimer();
675 // --------------------------------------------------------------------------
677 // --------------------------------------------------------------------------
679 // canvas constructor
680 LifeCanvas::LifeCanvas(wxWindow
*parent
, Life
*life
, bool interactive
)
681 : wxScrolledWindow(parent
, -1, wxPoint(0, 0), wxSize(100, 100))
684 m_interactive
= interactive
;
690 LifeCanvas::~LifeCanvas()
695 void LifeCanvas::Reset()
700 m_status
= MOUSE_NOACTION
;
701 m_width
= CellToCoord(m_life
->GetWidth()) + 1;
702 m_height
= CellToCoord(m_life
->GetHeight()) + 1;
703 m_bmp
= new wxBitmap(m_width
, m_height
);
704 wxCoord w
= GetClientSize().GetX();
705 wxCoord h
= GetClientSize().GetY();
706 m_xoffset
= (w
> m_width
)? ((w
- m_width
) / 2) : 0;
707 m_yoffset
= (h
> m_height
)? ((h
- m_height
) / 2) : 0;
710 DrawEverything(TRUE
);
711 SetScrollbars(10, 10, (m_width
+ 9) / 10, (m_height
+ 9) / 10);
714 void LifeCanvas::DrawEverything(bool force
)
718 dc
.SelectObject(*m_bmp
);
722 for (int j
= 0; j
< m_life
->GetWidth(); j
++)
723 for (int i
= 0; i
< m_life
->GetHeight(); i
++)
724 if (force
|| m_life
->HasChanged(i
, j
))
727 // bounding rectangle (always drawn - better than clipping region)
728 dc
.SetPen(*wxBLACK_PEN
);
729 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
730 dc
.DrawRectangle(0, 0, m_width
, m_height
);
733 dc
.SelectObject(wxNullBitmap
);
736 void LifeCanvas::DrawCell(int i
, int j
)
740 dc
.SelectObject(*m_bmp
);
743 dc
.SetClippingRegion(1, 1, m_width
- 2, m_height
- 2);
747 dc
.SelectObject(wxNullBitmap
);
750 void LifeCanvas::DrawCell(int i
, int j
, wxDC
&dc
)
752 if (m_life
->IsAlive(i
, j
))
754 dc
.SetPen(*wxBLACK_PEN
);
755 dc
.SetBrush(*wxBLACK_BRUSH
);
756 dc
.DrawRectangle(CellToCoord(i
),
763 dc
.SetPen(*wxLIGHT_GREY_PEN
);
764 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
765 dc
.DrawRectangle(CellToCoord(i
),
769 dc
.SetPen(*wxWHITE_PEN
);
770 dc
.SetBrush(*wxWHITE_BRUSH
);
771 dc
.DrawRectangle(CellToCoord(i
) + 1,
779 void LifeCanvas::OnPaint(wxPaintEvent
& event
)
784 wxRegionIterator
upd(GetUpdateRegion());
785 wxCoord x
, y
, w
, h
, xx
, yy
;
788 memdc
.SelectObject(*m_bmp
);
796 CalcUnscrolledPosition(x
, y
, &xx
, &yy
);
798 dc
.Blit(x
, y
, w
, h
, &memdc
, xx
- m_xoffset
, yy
- m_yoffset
);
802 memdc
.SelectObject(wxNullBitmap
);
806 void LifeCanvas::OnMouse(wxMouseEvent
& event
)
811 int x
, y
, xx
, yy
, i
, j
;
813 // which cell are we pointing at?
816 CalcUnscrolledPosition(x
, y
, &xx
, &yy
);
817 i
= CoordToCell( xx
- m_xoffset
);
818 j
= CoordToCell( yy
- m_yoffset
);
820 // adjust x, y to point to the upper left corner of the cell
821 CalcScrolledPosition( CellToCoord(i
) + m_xoffset
,
822 CellToCoord(j
) + m_yoffset
,
825 // set cursor shape and statusbar text
826 if (i
< 0 || i
>= m_life
->GetWidth() ||
827 j
< 0 || j
>= m_life
->GetHeight())
829 GET_FRAME()->SetStatusText(wxEmptyString
, 1);
830 SetCursor(*wxSTANDARD_CURSOR
);
835 msg
.Printf(_("Cell: (%u, %u)"), i
, j
);
836 GET_FRAME()->SetStatusText(msg
, 1);
837 SetCursor(*wxCROSS_CURSOR
);
841 if (!event
.LeftIsDown())
843 m_status
= MOUSE_NOACTION
;
845 else if (i
>= 0 && i
< m_life
->GetWidth() &&
846 j
>= 0 && j
< m_life
->GetHeight())
848 bool alive
= m_life
->IsAlive(i
, j
);
850 // if just pressed, update status
851 if (m_status
== MOUSE_NOACTION
)
852 m_status
= (alive
? MOUSE_ERASING
: MOUSE_DRAWING
);
854 // toggle cell and refresh if needed
855 if (((m_status
== MOUSE_ERASING
) && alive
) ||
856 ((m_status
== MOUSE_DRAWING
) && !alive
))
858 wxRect
rect(x
, y
, m_cellsize
+ 1, m_cellsize
+ 1);
859 m_life
->SetCell(i
, j
, !alive
);
861 Refresh(FALSE
, &rect
);
866 void LifeCanvas::OnSize(wxSizeEvent
& event
)
868 wxCoord w
= event
.GetSize().GetX();
869 wxCoord h
= event
.GetSize().GetY();
870 m_xoffset
= (w
> m_width
)? ((w
- m_width
) / 2) : 0;
871 m_yoffset
= (h
> m_height
)? ((h
- m_height
) / 2) : 0;
873 // allow default processing
877 // --------------------------------------------------------------------------
879 // --------------------------------------------------------------------------
881 LifeNewGameDialog::LifeNewGameDialog(wxWindow
*parent
, int *w
, int *h
)
882 : wxDialog(parent
, -1,
886 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
)
891 // prompts and text controls
893 strw
.Printf(_("%u"), *m_w
);
894 strh
.Printf(_("%u"), *m_h
);
895 m_spinctrlw
= new wxSpinCtrl( this, -1, strw
);
896 m_spinctrlh
= new wxSpinCtrl( this, -1, strh
);
899 wxBoxSizer
*inputsizer1
= new wxBoxSizer( wxHORIZONTAL
);
900 inputsizer1
->Add( new wxStaticText(this, -1, _("Width")), 1, wxCENTRE
| wxLEFT
, 20);
901 inputsizer1
->Add( m_spinctrlw
, 2, wxCENTRE
| wxLEFT
| wxRIGHT
, 20 );
903 wxBoxSizer
*inputsizer2
= new wxBoxSizer( wxHORIZONTAL
);
904 inputsizer2
->Add( new wxStaticText(this, -1, _("Height")), 1, wxCENTRE
| wxLEFT
, 20);
905 inputsizer2
->Add( m_spinctrlh
, 2, wxCENTRE
| wxLEFT
| wxRIGHT
, 20 );
907 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
908 topsizer
->Add( CreateTextSizer(_("Enter board dimensions")), 0, wxALL
, 10 );
909 topsizer
->Add( new wxStaticLine(this, -1), 0, wxGROW
| wxLEFT
| wxRIGHT
| wxBOTTOM
, 10);
910 topsizer
->Add( inputsizer1
, 1, wxGROW
| wxLEFT
| wxRIGHT
, 5 );
911 topsizer
->Add( inputsizer2
, 1, wxGROW
| wxLEFT
| wxRIGHT
, 5 );
912 topsizer
->Add( new wxStaticLine(this, -1), 0, wxGROW
| wxLEFT
| wxRIGHT
| wxTOP
, 10);
913 topsizer
->Add( CreateButtonSizer(wxOK
| wxCANCEL
), 0, wxCENTRE
| wxALL
, 10);
918 topsizer
->SetSizeHints(this);
923 void LifeNewGameDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
925 *m_w
= m_spinctrlw
->GetValue();
926 *m_h
= m_spinctrlh
->GetValue();
931 // --------------------------------------------------------------------------
933 // --------------------------------------------------------------------------
935 LifeSamplesDialog::LifeSamplesDialog(wxWindow
*parent
)
936 : wxDialog(parent
, -1,
940 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
)
944 // create and populate the list of available samples
945 m_list
= new wxListBox( this, ID_LISTBOX
,
949 wxLB_SINGLE
| wxLB_NEEDED_SB
| wxLB_HSCROLL
);
951 for (unsigned i
= 0; i
< (sizeof(g_shapes
) / sizeof(LifeShape
)); i
++)
952 m_list
->Append(g_shapes
[i
].m_name
);
955 wxStaticBox
*statbox
= new wxStaticBox( this, -1, _("Description"));
956 m_life
= new Life( 16, 16 );
957 m_life
->SetShape(g_shapes
[0]);
958 m_canvas
= new LifeCanvas( this, m_life
, FALSE
);
959 m_text
= new wxTextCtrl( this, -1,
963 wxTE_MULTILINE
| wxTE_READONLY
);
966 wxStaticBoxSizer
*sizer1
= new wxStaticBoxSizer( statbox
, wxVERTICAL
);
967 sizer1
->Add( m_canvas
, 2, wxGROW
| wxCENTRE
| wxALL
, 5);
968 sizer1
->Add( m_text
, 1, wxGROW
| wxCENTRE
| wxALL
, 5 );
970 wxBoxSizer
*sizer2
= new wxBoxSizer( wxHORIZONTAL
);
971 sizer2
->Add( m_list
, 0, wxGROW
| wxCENTRE
| wxALL
, 5 );
972 sizer2
->Add( sizer1
, 1, wxGROW
| wxCENTRE
| wxALL
, 5 );
974 wxBoxSizer
*sizer3
= new wxBoxSizer( wxVERTICAL
);
975 sizer3
->Add( CreateTextSizer(_("Select one configuration")), 0, wxALL
, 10 );
976 sizer3
->Add( new wxStaticLine(this, -1), 0, wxGROW
| wxLEFT
| wxRIGHT
, 10 );
977 sizer3
->Add( sizer2
, 1, wxGROW
| wxCENTRE
| wxALL
, 5 );
978 sizer3
->Add( new wxStaticLine(this, -1), 0, wxGROW
| wxLEFT
| wxRIGHT
, 10 );
979 sizer3
->Add( CreateButtonSizer(wxOK
| wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
984 sizer3
->SetSizeHints(this);
989 LifeSamplesDialog::~LifeSamplesDialog()
995 int LifeSamplesDialog::GetValue()
1000 void LifeSamplesDialog::OnListBox(wxCommandEvent
& event
)
1002 if (event
.GetSelection() != -1)
1004 m_value
= m_list
->GetSelection();
1005 m_text
->SetValue(g_shapes
[ event
.GetSelection() ].m_desc
);
1006 m_life
->SetShape(g_shapes
[ event
.GetSelection() ]);
1008 m_canvas
->DrawEverything(TRUE
); // force redraw everything
1009 m_canvas
->Refresh(FALSE
); // do not erase background
1013 // --------------------------------------------------------------------------
1015 // --------------------------------------------------------------------------
1017 Life::Life(int width
, int height
)
1021 Create(width
, height
);
1029 void Life::Create(int width
, int height
)
1031 wxASSERT(width
> 0 && height
> 0);
1035 m_cells
= new Cell
[m_width
* m_height
];
1039 void Life::Destroy()
1046 for (int i
= 0; i
< m_width
* m_height
; i
++)
1047 m_cells
[i
] = CELL_DEAD
;
1050 bool Life::IsAlive(int x
, int y
) const
1052 wxASSERT(x
>= 0 && y
>= 0 && x
< m_width
&& y
< m_height
);
1054 return (m_cells
[y
* m_width
+ x
] & CELL_ALIVE
);
1057 bool Life::HasChanged(int x
, int y
) const
1059 wxASSERT(x
>= 0 && y
>= 0 && x
< m_width
&& y
< m_height
);
1061 return (m_cells
[y
* m_width
+ x
] & CELL_MARK
) != 0;
1064 void Life::SetBorderWrap(bool on
)
1069 void Life::SetCell(int x
, int y
, bool alive
)
1071 wxASSERT(x
>= 0 && y
>= 0 && x
< m_width
&& y
< m_height
);
1073 m_cells
[y
* m_width
+ x
] = (alive
? CELL_ALIVE
: CELL_DEAD
);
1076 void Life::SetShape(LifeShape
& shape
)
1078 wxASSERT((m_width
>= shape
.m_width
) && (m_height
>= shape
.m_height
));
1080 int x0
= (m_width
- shape
.m_width
) / 2;
1081 int y0
= (m_height
- shape
.m_height
) / 2;
1082 char *p
= shape
.m_data
;
1085 for (int j
= y0
; j
< y0
+ shape
.m_height
; j
++)
1086 for (int i
= x0
; i
< x0
+ shape
.m_width
; i
++)
1087 SetCell(i
, j
, *(p
++) == '*');
1090 bool Life::NextTic()
1095 /* 1st pass. Find and mark deaths and births for this generation.
1098 * An organism with <= 1 neighbors will die due to isolation.
1099 * An organism with >= 4 neighbors will die due to starvation.
1100 * New organisms are born in cells with exactly 3 neighbors.
1102 for (j
= 0; j
< m_height
; j
++)
1103 for (i
= 0; i
< m_width
; i
++)
1105 int neighbors
= GetNeighbors(i
, j
);
1106 bool alive
= IsAlive(i
, j
);
1108 /* Set CELL_MARK if this cell must change, clear it
1109 * otherwise. We cannot toggle the CELL_ALIVE bit yet
1110 * because all deaths and births are simultaneous (it
1111 * would affect neighbouring cells).
1113 if ((!alive
&& neighbors
== 3) ||
1114 (alive
&& (neighbors
<= 1 || neighbors
>= 4)))
1115 m_cells
[j
* m_width
+ i
] |= CELL_MARK
;
1117 m_cells
[j
* m_width
+ i
] &= ~CELL_MARK
;
1120 /* 2nd pass. Stabilize.
1122 for (j
= 0; j
< m_height
; j
++)
1123 for (i
= 0; i
< m_width
; i
++)
1125 /* Toggle CELL_ALIVE for those cells marked in the
1126 * previous pass. Do not clear the CELL_MARK bit yet;
1127 * it is useful to know which cells have changed and
1128 * thus must be updated in the screen.
1130 if (m_cells
[j
* m_width
+ i
] & CELL_MARK
)
1132 m_cells
[j
* m_width
+ i
] ^= CELL_ALIVE
;
1137 return (changed
!= 0);
1140 int Life::GetNeighbors(int x
, int y
) const
1142 wxASSERT(x
>= 0 && y
>= 0 && x
< m_width
&& y
< m_height
);
1146 int i0
= (x
)? (x
- 1) : 0;
1147 int j0
= (y
)? (y
- 1) : 0;
1148 int i1
= (x
< (m_width
- 1))? (x
+ 1) : (m_width
- 1);
1149 int j1
= (y
< (m_height
- 1))? (y
+ 1) : (m_height
- 1);
1151 if (m_wrap
&& ( !x
|| !y
|| x
== (m_width
- 1) || y
== (m_height
- 1)))
1153 // this is an outer cell and wraparound is on
1154 for (int j
= y
- 1; j
<= y
+ 1; j
++)
1155 for (int i
= x
- 1; i
<= x
+ 1; i
++)
1156 if (IsAlive( ((i
< 0)? (i
+ m_width
) : (i
% m_width
)),
1157 ((j
< 0)? (j
+ m_height
) : (j
% m_height
)) ))
1162 // this is an inner cell, or wraparound is off
1163 for (int j
= j0
; j
<= j1
; j
++)
1164 for (int i
= i0
; i
<= i1
; i
++)
1169 // do not count ourselves
1170 if (IsAlive(x
, y
)) neighbors
--;
1175 void Life::SetCell(int x
, int y
, Cell status
)
1177 wxASSERT(x
>= 0 && y
>= 0 && x
< m_width
&& y
< m_height
);
1179 m_cells
[y
* m_width
+ x
] = status
;