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
, 5);
213 sizer
->Add(new wxStaticLine(panel
, -1), 0, wxGROW
| wxCENTRE
);
214 sizer
->Add(m_text
, 0, wxCENTRE
| wxTOP
, 5);
215 sizer
->Add(slider
, 0, wxCENTRE
| wxALL
, 5);
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()
243 GetToolBar()->EnableTool(ID_START
, !m_running
);
244 GetToolBar()->EnableTool(ID_STOP
, m_running
);
245 GetMenuBar()->GetMenu(1)->Enable(ID_START
, !m_running
);
246 GetMenuBar()->GetMenu(1)->Enable(ID_STEP
, !m_running
);
247 GetMenuBar()->GetMenu(1)->Enable(ID_STOP
, m_running
);
251 void LifeFrame::OnMenu(wxCommandEvent
& event
)
253 switch (event
.GetId())
255 case ID_CENTER
: m_canvas
->Recenter(0, 0); break;
256 case ID_START
: OnStart(); break;
257 case ID_STEP
: OnTimer(); break;
258 case ID_STOP
: OnStop(); break;
261 int cellsize
= m_canvas
->GetCellSize();
263 m_canvas
->SetCellSize(cellsize
* 2);
268 int cellsize
= m_canvas
->GetCellSize();
270 m_canvas
->SetCellSize(cellsize
/ 2);
278 while (m_running
&& m_topspeed
)
287 // stop if it was running
290 m_canvas
->Recenter(0, 0);
297 LifeAboutDialog
dialog(this);
303 // TRUE is to force the frame to close
310 void LifeFrame::OnClose(wxCloseEvent
& WXUNUSED(event
))
312 // Stop if it was running; this is absolutely needed because
313 // the frame won't be actually destroyed until there are no
314 // more pending events, and this in turn won't ever happen
315 // if the timer is running faster than the window can redraw.
320 void LifeFrame::OnSamples(wxCommandEvent
& WXUNUSED(event
))
322 // stop if it was running
326 LifeSamplesDialog
dialog(this);
329 if (dialog
.ShowModal() == wxID_OK
)
331 const LifeShape shape
= dialog
.GetShape();
335 m_life
->SetShape(shape
);
338 m_canvas
->Recenter(0, 0);
344 void LifeFrame::OnStart()
348 m_timer
->Start(m_interval
);
354 void LifeFrame::OnStop()
365 void LifeFrame::OnTimer()
367 if (m_life
->NextTic())
372 m_canvas
->DrawChanged();
376 void LifeFrame::OnSlider(wxScrollEvent
& event
)
378 m_interval
= event
.GetPosition() * 100;
389 // --------------------------------------------------------------------------
391 // --------------------------------------------------------------------------
393 void LifeTimer::Notify()
395 GET_FRAME()->OnTimer();
398 // --------------------------------------------------------------------------
400 // --------------------------------------------------------------------------
402 // canvas constructor
403 LifeCanvas::LifeCanvas(wxWindow
*parent
, Life
*life
, bool interactive
)
404 : wxWindow(parent
, -1, wxPoint(0, 0), wxSize(100, 100),
408 m_interactive
= interactive
;
410 m_status
= MOUSE_NOACTION
;
417 SetCursor(*wxCROSS_CURSOR
);
419 // reduce flicker if wxEVT_ERASE_BACKGROUND is not available
420 SetBackgroundColour(*wxWHITE
);
423 LifeCanvas::~LifeCanvas()
428 // recenter at the given position
429 void LifeCanvas::Recenter(wxInt32 i
, wxInt32 j
)
431 m_viewportX
= i
- m_viewportW
/ 2;
432 m_viewportY
= j
- m_viewportH
/ 2;
438 // set the cell size and refresh display
439 void LifeCanvas::SetCellSize(int cellsize
)
441 m_cellsize
= cellsize
;
443 // find current center
444 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
445 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
447 // get current canvas size and adjust viewport accordingly
449 GetClientSize(&w
, &h
);
450 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
451 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
454 m_viewportX
= cx
- m_viewportW
/ 2;
455 m_viewportY
= cy
- m_viewportH
/ 2;
460 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
461 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
462 m_thumbX
= m_viewportW
;
463 m_thumbY
= m_viewportH
;
470 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, bool alive
)
474 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
475 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
482 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, wxDC
&dc
)
484 wxCoord x
= CellToX(i
);
485 wxCoord y
= CellToY(j
);
487 // if cellsize is 1 or 2, there will be no grid
494 dc
.DrawRectangle(x
, y
, 2, 2);
497 dc
.DrawRectangle(x
+ 1, y
+ 1, m_cellsize
- 1, m_cellsize
- 1);
501 // draw all changed cells
502 void LifeCanvas::DrawChanged()
510 m_life
->BeginFind(m_viewportX
,
512 m_viewportX
+ m_viewportW
,
513 m_viewportY
+ m_viewportH
,
517 dc
.SetLogicalFunction(wxINVERT
);
521 // drawn using DrawPoint
522 dc
.SetPen(*wxBLACK_PEN
);
526 // drawn using DrawRectangle
527 dc
.SetPen(*wxTRANSPARENT_PEN
);
528 dc
.SetBrush(*wxBLACK_BRUSH
);
533 done
= m_life
->FindMore(&cells
, &ncells
);
535 for (size_t m
= 0; m
< ncells
; m
++)
536 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
542 void LifeCanvas::OnPaint(wxPaintEvent
& event
)
545 wxRect rect
= GetUpdateRegion().GetBox();
547 wxInt32 i0
, j0
, i1
, j1
;
553 h
= rect
.GetHeight();
557 i1
= XToCell(x
+ w
- 1);
558 j1
= YToCell(y
+ h
- 1);
564 m_life
->BeginFind(i0
, j0
, i1
, j1
, FALSE
);
565 done
= m_life
->FindMore(&cells
, &ncells
);
567 // erase all damaged cells and draw the grid
569 dc
.SetBrush(*wxWHITE_BRUSH
);
574 dc
.SetPen(*wxWHITE_PEN
);
575 dc
.DrawRectangle(x
, y
, w
, h
);
581 w
= CellToX(i1
+ 1) - x
+ 1;
582 h
= CellToY(j1
+ 1) - y
+ 1;
584 dc
.SetPen(*wxLIGHT_GREY_PEN
);
585 for (wxInt32 yy
= y
; yy
<= (y
+ h
- m_cellsize
); yy
+= m_cellsize
)
586 dc
.DrawRectangle(x
, yy
, w
, m_cellsize
+ 1);
587 for (wxInt32 xx
= x
; xx
<= (x
+ w
- m_cellsize
); xx
+= m_cellsize
)
588 dc
.DrawLine(xx
, y
, xx
, y
+ h
);
591 // draw all alive cells
592 dc
.SetPen(*wxBLACK_PEN
);
593 dc
.SetBrush(*wxBLACK_BRUSH
);
597 for (size_t m
= 0; m
< ncells
; m
++)
598 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
600 done
= m_life
->FindMore(&cells
, &ncells
);
604 for (size_t m
= 0; m
< ncells
; m
++)
605 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
610 void LifeCanvas::OnMouse(wxMouseEvent
& event
)
615 // which cell are we pointing at?
616 wxInt32 i
= XToCell( event
.GetX() );
617 wxInt32 j
= YToCell( event
.GetY() );
619 // set statusbar text
621 msg
.Printf(_("Cell: (%d, %d)"), i
, j
);
622 GET_FRAME()->SetStatusText(msg
, 1);
625 if (!event
.LeftIsDown())
627 m_status
= MOUSE_NOACTION
;
631 // button just pressed?
632 if (m_status
== MOUSE_NOACTION
)
634 // yes, update status and toggle this cell
635 m_status
= (m_life
->IsAlive(i
, j
)? MOUSE_ERASING
: MOUSE_DRAWING
);
639 m_life
->SetCell(i
, j
, m_status
== MOUSE_DRAWING
);
640 DrawCell(i
, j
, m_status
== MOUSE_DRAWING
);
642 else if ((m_mi
!= i
) || (m_mj
!= j
))
644 // draw a line of cells using Bresenham's algorithm
645 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
648 si
= (di
< 0)? -1 : 1;
651 sj
= (dj
< 0)? -1 : 1;
663 m_life
->SetCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
664 DrawCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
681 m_life
->SetCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
682 DrawCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
694 m_life
->SetCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
695 DrawCell(ii
, jj
, m_status
== MOUSE_DRAWING
);
700 GET_FRAME()->UpdateInfoText();
703 void LifeCanvas::OnSize(wxSizeEvent
& event
)
706 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
707 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
710 wxCoord w
= event
.GetSize().GetX();
711 wxCoord h
= event
.GetSize().GetY();
712 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
713 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
716 m_viewportX
= cx
- m_viewportW
/ 2;
717 m_viewportY
= cy
- m_viewportH
/ 2;
722 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
723 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
724 m_thumbX
= m_viewportW
;
725 m_thumbY
= m_viewportH
;
728 // allow default processing
732 void LifeCanvas::OnScroll(wxScrollWinEvent
& event
)
734 WXTYPE type
= event
.GetEventType();
735 int pos
= event
.GetPosition();
736 int orient
= event
.GetOrientation();
737 bool scrolling
= event
.IsScrolling();
740 // calculate scroll increment
743 case wxEVT_SCROLLWIN_TOP
:
745 if (orient
== wxHORIZONTAL
)
746 scrollinc
= -m_viewportW
;
748 scrollinc
= -m_viewportH
;
751 case wxEVT_SCROLLWIN_BOTTOM
:
753 if (orient
== wxHORIZONTAL
)
754 scrollinc
= m_viewportW
;
756 scrollinc
= m_viewportH
;
759 case wxEVT_SCROLLWIN_LINEUP
: scrollinc
= -1; break;
760 case wxEVT_SCROLLWIN_LINEDOWN
: scrollinc
= +1; break;
761 case wxEVT_SCROLLWIN_PAGEUP
: scrollinc
= -10; break;
762 case wxEVT_SCROLLWIN_PAGEDOWN
: scrollinc
= +10; break;
763 case wxEVT_SCROLLWIN_THUMBTRACK
:
767 // user is dragging the thumb in the scrollbar
768 if (orient
== wxHORIZONTAL
)
770 scrollinc
= pos
- m_thumbX
;
775 scrollinc
= pos
- m_thumbY
;
781 // user released the thumb after dragging
782 m_thumbX
= m_viewportW
;
783 m_thumbY
= m_viewportH
;
789 #ifdef __WXGTK__ // what about Motif?
790 // wxGTK updates the thumb automatically (wxMSW doesn't); reset it back
791 if ((type
!= wxEVT_SCROLLWIN_THUMBTRACK
) || !scrolling
)
793 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
794 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
798 if (scrollinc
== 0) return;
800 // scroll the window and adjust the viewport
801 if (orient
== wxHORIZONTAL
)
803 m_viewportX
+= scrollinc
;
804 ScrollWindow( -m_cellsize
* scrollinc
, 0, (const wxRect
*) NULL
);
808 m_viewportY
+= scrollinc
;
809 ScrollWindow( 0, -m_cellsize
* scrollinc
, (const wxRect
*) NULL
);
813 void LifeCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
815 // do nothing. I just don't want the background to be erased, you know.