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 // ==========================================================================
13 // headers, declarations, constants
14 // ==========================================================================
17 #pragma implementation "life.h"
20 #include "wx/statline.h"
26 // --------------------------------------------------------------------------
28 // --------------------------------------------------------------------------
30 #if defined(__WXGTK__) || defined(__WXMOTIF__)
31 // the application icon
32 #include "mondrian.xpm"
34 // bitmap buttons for the toolbar
35 #include "bitmaps/reset.xpm"
36 #include "bitmaps/play.xpm"
37 #include "bitmaps/stop.xpm"
38 #include "bitmaps/zoomin.xpm"
39 #include "bitmaps/zoomout.xpm"
42 // --------------------------------------------------------------------------
44 // --------------------------------------------------------------------------
46 // IDs for the controls and the menu commands
49 // menu items and toolbar buttons
62 // speed selection slider
66 // --------------------------------------------------------------------------
67 // event tables and other macros for wxWindows
68 // --------------------------------------------------------------------------
71 BEGIN_EVENT_TABLE(LifeFrame
, wxFrame
)
72 EVT_MENU (ID_SAMPLES
, LifeFrame::OnSamples
)
73 EVT_MENU (ID_RESET
, LifeFrame::OnMenu
)
74 EVT_MENU (ID_ABOUT
, LifeFrame::OnMenu
)
75 EVT_MENU (ID_EXIT
, LifeFrame::OnMenu
)
76 EVT_MENU (ID_CENTER
, LifeFrame::OnMenu
)
77 EVT_MENU (ID_START
, LifeFrame::OnMenu
)
78 EVT_MENU (ID_STEP
, LifeFrame::OnMenu
)
79 EVT_MENU (ID_STOP
, LifeFrame::OnMenu
)
80 EVT_MENU (ID_ZOOMIN
, LifeFrame::OnMenu
)
81 EVT_MENU (ID_ZOOMOUT
, LifeFrame::OnMenu
)
82 EVT_MENU (ID_TOPSPEED
, LifeFrame::OnMenu
)
83 EVT_COMMAND_SCROLL (ID_SLIDER
, LifeFrame::OnSlider
)
84 EVT_CLOSE ( LifeFrame::OnClose
)
87 BEGIN_EVENT_TABLE(LifeCanvas
, wxWindow
)
88 EVT_PAINT ( LifeCanvas::OnPaint
)
89 EVT_SCROLLWIN ( LifeCanvas::OnScroll
)
90 EVT_SIZE ( LifeCanvas::OnSize
)
91 EVT_MOUSE_EVENTS ( LifeCanvas::OnMouse
)
92 EVT_ERASE_BACKGROUND( LifeCanvas::OnEraseBackground
)
96 // Create a new application object
97 IMPLEMENT_APP(LifeApp
)
100 // ==========================================================================
102 // ==========================================================================
105 #define ADD_TOOL(id, bmp, tooltip, help) \
106 toolBar->AddTool(id, bmp, wxNullBitmap, FALSE, -1, -1, (wxObject *)0, tooltip, help)
108 #define GET_FRAME() ((LifeFrame *) wxGetApp().GetTopWindow())
110 // --------------------------------------------------------------------------
112 // --------------------------------------------------------------------------
114 // 'Main program' equivalent: the program execution "starts" here
115 bool LifeApp::OnInit()
117 // create the main application window
118 LifeFrame
*frame
= new LifeFrame();
120 // show it and tell the application that it's our main window
124 // enter the main message loop and run the app
128 // --------------------------------------------------------------------------
130 // --------------------------------------------------------------------------
133 LifeFrame::LifeFrame() : wxFrame((wxFrame
*)0, -1, _("Life!"), wxPoint(50, 50))
136 SetIcon(wxICON(mondrian
));
139 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
140 wxMenu
*menuGame
= new wxMenu("", wxMENU_TEAROFF
);
142 menuFile
->Append(ID_RESET
, _("Reset"), _("Start a new game"));
143 menuFile
->Append(ID_SAMPLES
, _("Sample game..."), _("Select a sample configuration"));
144 menuFile
->AppendSeparator();
145 menuFile
->Append(ID_ABOUT
, _("&About...\tCtrl-A"), _("Show about dialog"));
146 menuFile
->AppendSeparator();
147 menuFile
->Append(ID_EXIT
, _("E&xit\tAlt-X"), _("Quit this program"));
149 menuGame
->Append(ID_CENTER
, _("Re¢er\tCtrl-C"), _("Go to (0, 0)"));
150 menuGame
->Append(ID_START
, _("&Start\tCtrl-S"), _("Start"));
151 menuGame
->Append(ID_STEP
, _("&Next\tCtrl-N"), _("Single step"));
152 menuGame
->Append(ID_STOP
, _("S&top\tCtrl-T"), _("Stop"));
153 menuGame
->Enable(ID_STOP
, FALSE
);
154 menuGame
->AppendSeparator();
155 menuGame
->Append(ID_TOPSPEED
, _("Top speed!"), _("Go as fast as possible"));
156 menuGame
->AppendSeparator();
157 menuGame
->Append(ID_ZOOMIN
, _("Zoom &in\tCtrl-I"));
158 menuGame
->Append(ID_ZOOMOUT
, _("Zoom &out\tCtrl-O"));
160 wxMenuBar
*menuBar
= new wxMenuBar();
161 menuBar
->Append(menuFile
, _("&File"));
162 menuBar
->Append(menuGame
, _("&Game"));
166 wxBitmap tbBitmaps
[5];
168 tbBitmaps
[0] = wxBITMAP(reset
);
169 tbBitmaps
[1] = wxBITMAP(play
);
170 tbBitmaps
[2] = wxBITMAP(stop
);
171 tbBitmaps
[3] = wxBITMAP(zoomin
);
172 tbBitmaps
[4] = wxBITMAP(zoomout
);
174 wxToolBar
*toolBar
= CreateToolBar();
175 toolBar
->SetMargins(5, 5);
176 toolBar
->SetToolBitmapSize(wxSize(16, 16));
177 ADD_TOOL(ID_RESET
, tbBitmaps
[0], _("Reset"), _("Start a new game"));
178 ADD_TOOL(ID_START
, tbBitmaps
[1], _("Start"), _("Start"));
179 ADD_TOOL(ID_STOP
, tbBitmaps
[2], _("Stop"), _("Stop"));
180 toolBar
->AddSeparator();
181 ADD_TOOL(ID_ZOOMIN
, tbBitmaps
[3], _("Zoom in"), _("Zoom in"));
182 ADD_TOOL(ID_ZOOMOUT
, tbBitmaps
[4], _("Zoom out"), _("Zoom out"));
184 toolBar
->EnableTool(ID_STOP
, FALSE
); // must be after Realize() !
188 SetStatusText(_("Welcome to Life!"));
191 wxPanel
*panel
= new wxPanel(this, -1);
193 m_canvas
= new LifeCanvas(panel
, m_life
);
194 m_timer
= new LifeTimer();
199 m_text
= new wxStaticText(panel
, -1, "");
202 // speed selection slider
203 wxSlider
*slider
= new wxSlider(panel
, ID_SLIDER
,
207 wxSL_HORIZONTAL
| wxSL_AUTOTICKS
);
210 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
211 sizer
->Add(new wxStaticLine(panel
, -1), 0, wxGROW
| wxCENTRE
);
212 sizer
->Add(m_canvas
, 1, wxGROW
| wxCENTRE
| wxALL
, 2);
213 sizer
->Add(new wxStaticLine(panel
, -1), 0, wxGROW
| wxCENTRE
);
214 sizer
->Add(m_text
, 0, wxCENTRE
| wxTOP
, 4);
215 sizer
->Add(slider
, 0, wxCENTRE
| wxALL
, 4);
216 panel
->SetSizer(sizer
);
217 panel
->SetAutoLayout(TRUE
);
219 sizer
->SetSizeHints(this);
222 LifeFrame::~LifeFrame()
227 void LifeFrame::UpdateInfoText()
231 msg
.Printf(_(" Generation: %u (T: %u ms), Population: %u "),
233 m_topspeed
? 0 : m_interval
,
234 m_life
->GetNumCells());
235 m_text
->SetLabel(msg
);
238 // Enable or disable tools and menu entries according to the current
239 // state. See also wxEVT_UPDATE_UI events for a slightly different
241 void LifeFrame::UpdateUI()
244 GetToolBar()->EnableTool(ID_START
, !m_running
);
245 GetToolBar()->EnableTool(ID_STOP
, m_running
);
246 GetMenuBar()->GetMenu(1)->Enable(ID_START
, !m_running
);
247 GetMenuBar()->GetMenu(1)->Enable(ID_STEP
, !m_running
);
248 GetMenuBar()->GetMenu(1)->Enable(ID_STOP
, m_running
);
251 int cellsize
= m_canvas
->GetCellSize();
252 GetToolBar()->EnableTool(ID_ZOOMIN
, cellsize
< 32);
253 GetToolBar()->EnableTool(ID_ZOOMOUT
, cellsize
> 1);
254 GetMenuBar()->GetMenu(1)->Enable(ID_ZOOMIN
, cellsize
< 32);
255 GetMenuBar()->GetMenu(1)->Enable(ID_ZOOMOUT
, cellsize
> 1);
259 void LifeFrame::OnMenu(wxCommandEvent
& event
)
261 switch (event
.GetId())
263 case ID_CENTER
: m_canvas
->Recenter(0, 0); break;
264 case ID_START
: OnStart(); break;
265 case ID_STEP
: OnTimer(); break;
266 case ID_STOP
: OnStop(); break;
269 int cellsize
= m_canvas
->GetCellSize();
272 m_canvas
->SetCellSize(cellsize
* 2);
279 int cellsize
= m_canvas
->GetCellSize();
282 m_canvas
->SetCellSize(cellsize
/ 2);
292 while (m_running
&& m_topspeed
)
301 // stop if it was running
304 m_canvas
->Recenter(0, 0);
311 LifeAboutDialog
dialog(this);
317 // TRUE is to force the frame to close
324 void LifeFrame::OnClose(wxCloseEvent
& WXUNUSED(event
))
326 // Stop if it was running; this is absolutely needed because
327 // the frame won't be actually destroyed until there are no
328 // more pending events, and this in turn won't ever happen
329 // if the timer is running faster than the window can redraw.
334 void LifeFrame::OnSamples(wxCommandEvent
& WXUNUSED(event
))
336 // stop if it was running
340 LifeSamplesDialog
dialog(this);
343 if (dialog
.ShowModal() == wxID_OK
)
345 const LifeShape shape
= dialog
.GetShape();
349 m_life
->SetShape(shape
);
352 m_canvas
->Recenter(0, 0);
358 void LifeFrame::OnStart()
362 m_timer
->Start(m_interval
);
368 void LifeFrame::OnStop()
379 void LifeFrame::OnTimer()
381 if (m_life
->NextTic())
386 m_canvas
->DrawChanged();
390 void LifeFrame::OnSlider(wxScrollEvent
& event
)
392 m_interval
= event
.GetPosition() * 100;
403 // --------------------------------------------------------------------------
405 // --------------------------------------------------------------------------
407 void LifeTimer::Notify()
409 GET_FRAME()->OnTimer();
412 // --------------------------------------------------------------------------
414 // --------------------------------------------------------------------------
416 // canvas constructor
417 LifeCanvas::LifeCanvas(wxWindow
*parent
, Life
*life
, bool interactive
)
418 : wxWindow(parent
, -1, wxPoint(0, 0), wxSize(100, 100),
422 m_interactive
= interactive
;
424 m_status
= MOUSE_NOACTION
;
431 SetCursor(*wxCROSS_CURSOR
);
433 // reduce flicker if wxEVT_ERASE_BACKGROUND is not available
434 SetBackgroundColour(*wxWHITE
);
437 LifeCanvas::~LifeCanvas()
442 // recenter at the given position
443 void LifeCanvas::Recenter(wxInt32 i
, wxInt32 j
)
445 m_viewportX
= i
- m_viewportW
/ 2;
446 m_viewportY
= j
- m_viewportH
/ 2;
452 // set the cell size and refresh display
453 void LifeCanvas::SetCellSize(int cellsize
)
455 m_cellsize
= cellsize
;
457 // find current center
458 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
459 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
461 // get current canvas size and adjust viewport accordingly
463 GetClientSize(&w
, &h
);
464 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
465 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
468 m_viewportX
= cx
- m_viewportW
/ 2;
469 m_viewportY
= cy
- m_viewportH
/ 2;
474 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
475 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
476 m_thumbX
= m_viewportW
;
477 m_thumbY
= m_viewportH
;
484 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, bool alive
)
488 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
489 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
496 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, wxDC
&dc
)
498 wxCoord x
= CellToX(i
);
499 wxCoord y
= CellToY(j
);
501 // if cellsize is 1 or 2, there will be no grid
508 dc
.DrawRectangle(x
, y
, 2, 2);
511 dc
.DrawRectangle(x
+ 1, y
+ 1, m_cellsize
- 1, m_cellsize
- 1);
515 // draw all changed cells
516 void LifeCanvas::DrawChanged()
524 m_life
->BeginFind(m_viewportX
,
526 m_viewportX
+ m_viewportW
,
527 m_viewportY
+ m_viewportH
,
531 dc
.SetLogicalFunction(wxINVERT
);
535 // drawn using DrawPoint
536 dc
.SetPen(*wxBLACK_PEN
);
540 // drawn using DrawRectangle
541 dc
.SetPen(*wxTRANSPARENT_PEN
);
542 dc
.SetBrush(*wxBLACK_BRUSH
);
547 done
= m_life
->FindMore(&cells
, &ncells
);
549 for (size_t m
= 0; m
< ncells
; m
++)
550 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
556 void LifeCanvas::OnPaint(wxPaintEvent
& event
)
559 wxRect rect
= GetUpdateRegion().GetBox();
561 wxInt32 i0
, j0
, i1
, j1
;
567 h
= rect
.GetHeight();
571 i1
= XToCell(x
+ w
- 1);
572 j1
= YToCell(y
+ h
- 1);
578 m_life
->BeginFind(i0
, j0
, i1
, j1
, FALSE
);
579 done
= m_life
->FindMore(&cells
, &ncells
);
581 // erase all damaged cells and draw the grid
583 dc
.SetBrush(*wxWHITE_BRUSH
);
588 dc
.SetPen(*wxWHITE_PEN
);
589 dc
.DrawRectangle(x
, y
, w
, h
);
595 w
= CellToX(i1
+ 1) - x
+ 1;
596 h
= CellToY(j1
+ 1) - y
+ 1;
598 dc
.SetPen(*wxLIGHT_GREY_PEN
);
599 for (wxInt32 yy
= y
; yy
<= (y
+ h
- m_cellsize
); yy
+= m_cellsize
)
600 dc
.DrawRectangle(x
, yy
, w
, m_cellsize
+ 1);
601 for (wxInt32 xx
= x
; xx
<= (x
+ w
- m_cellsize
); xx
+= m_cellsize
)
602 dc
.DrawLine(xx
, y
, xx
, y
+ h
);
605 // draw all alive cells
606 dc
.SetPen(*wxBLACK_PEN
);
607 dc
.SetBrush(*wxBLACK_BRUSH
);
611 for (size_t m
= 0; m
< ncells
; m
++)
612 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
614 done
= m_life
->FindMore(&cells
, &ncells
);
618 for (size_t m
= 0; m
< ncells
; m
++)
619 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
624 void LifeCanvas::OnMouse(wxMouseEvent
& event
)
629 // which cell are we pointing at?
630 wxInt32 i
= XToCell( event
.GetX() );
631 wxInt32 j
= YToCell( event
.GetY() );
633 // set statusbar text
635 msg
.Printf(_("Cell: (%d, %d)"), i
, j
);
636 GET_FRAME()->SetStatusText(msg
, 1);
639 if (!event
.LeftIsDown())
641 m_status
= MOUSE_NOACTION
;
645 // button just pressed?
646 if (m_status
== MOUSE_NOACTION
)
648 // yes, update status and toggle this cell
649 m_status
= (m_life
->IsAlive(i
, j
)? MOUSE_ERASING
: MOUSE_DRAWING
);
653 m_life
->SetCell(i
, j
, m_status
== MOUSE_DRAWING
);
654 DrawCell(i
, j
, m_status
== MOUSE_DRAWING
);
656 else if ((m_mi
!= i
) || (m_mj
!= j
))
658 // draw a line of cells using Bresenham's algorithm
659 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
662 si
= (di
< 0)? -1 : 1;
665 sj
= (dj
< 0)? -1 : 1;
677 m_life
->SetCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
678 DrawCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
695 m_life
->SetCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
696 DrawCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
708 m_life
->SetCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
709 DrawCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
714 GET_FRAME()->UpdateInfoText();
717 void LifeCanvas::OnSize(wxSizeEvent
& event
)
720 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
721 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
724 wxCoord w
= event
.GetSize().GetX();
725 wxCoord h
= event
.GetSize().GetY();
726 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
727 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
730 m_viewportX
= cx
- m_viewportW
/ 2;
731 m_viewportY
= cy
- m_viewportH
/ 2;
736 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
737 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
738 m_thumbX
= m_viewportW
;
739 m_thumbY
= m_viewportH
;
742 // allow default processing
746 void LifeCanvas::OnScroll(wxScrollWinEvent
& event
)
748 WXTYPE type
= event
.GetEventType();
749 int pos
= event
.GetPosition();
750 int orient
= event
.GetOrientation();
752 // calculate scroll increment
756 case wxEVT_SCROLLWIN_TOP
:
758 if (orient
== wxHORIZONTAL
)
759 scrollinc
= -m_viewportW
;
761 scrollinc
= -m_viewportH
;
764 case wxEVT_SCROLLWIN_BOTTOM
:
766 if (orient
== wxHORIZONTAL
)
767 scrollinc
= m_viewportW
;
769 scrollinc
= m_viewportH
;
772 case wxEVT_SCROLLWIN_LINEUP
: scrollinc
= -1; break;
773 case wxEVT_SCROLLWIN_LINEDOWN
: scrollinc
= +1; break;
774 case wxEVT_SCROLLWIN_PAGEUP
: scrollinc
= -10; break;
775 case wxEVT_SCROLLWIN_PAGEDOWN
: scrollinc
= +10; break;
776 case wxEVT_SCROLLWIN_THUMBTRACK
:
778 if (orient
== wxHORIZONTAL
)
780 scrollinc
= pos
- m_thumbX
;
785 scrollinc
= pos
- m_thumbY
;
790 case wxEVT_SCROLLWIN_THUMBRELEASE
:
792 m_thumbX
= m_viewportW
;
793 m_thumbY
= m_viewportH
;
797 #ifdef __WXGTK__ // what about Motif?
798 // wxGTK updates the thumb automatically (wxMSW doesn't); reset it back
799 if (type
!= wxEVT_SCROLLWIN_THUMBTRACK
)
801 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
802 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
806 if (scrollinc
== 0) return;
808 // scroll the window and adjust the viewport
809 if (orient
== wxHORIZONTAL
)
811 m_viewportX
+= scrollinc
;
812 ScrollWindow( -m_cellsize
* scrollinc
, 0, (const wxRect
*) NULL
);
816 m_viewportY
+= scrollinc
;
817 ScrollWindow( 0, -m_cellsize
* scrollinc
, (const wxRect
*) NULL
);
821 void LifeCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
823 // do nothing. I just don't want the background to be erased, you know.