Life! version 2

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5912 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Guillermo Rodriguez Garcia
2000-02-08 20:04:48 +00:00
parent bf4d9b2b9b
commit e0a4029251
14 changed files with 2086 additions and 645 deletions

BIN
demos/life/bitmaps/life.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

View File

@@ -0,0 +1,81 @@
/* XPM */
static char * life_xpm[] = {
"151 73 5 1",
" c None",
". c #808080",
"X c Black",
"o c Gray100",
"O c #c0c0c0",
" .X ",
" .XXo.. ",
" .XXOooooX ",
" .XXooOoooooX ",
" XXXoooooOoooooX ",
" .XXXoooOoooooOooooX. ",
" .XXoOoooooOoooooOoooOOX ",
" .XXXooooOoooooOoooooOOOoooX ",
" .XXooOoooooOoooooOoooOOOoooooX ",
" .XXXooooooooooooooooo.OOoooOoooooX ",
" .XXoooOooooooOoooooOo.XXXOoooooOoooo.. ",
" ..XXooooooOooooooOoooOXXXXXXX.oooooOoooooX ",
" .XXooooooooooOooooooO.XXXXXXXXXXXoooooOoooOOX ",
" .XXooOooooooooooOooo.XXXXXXXXXXXXXXXoooooOOOOooX ",
" .XXOoooooOoooooOoooo.XXXXXXXXXXXXXXXXXXXooOOOOoooooX. ",
" .XXoooOoooooOoooooOo.XXXXXXXXXXXXXXXXXXXXXXOOoooOOoooooX ",
" .XXoOoooooOoooooOooooOXXXXXXXXXXXXXXXXXXXXXX.oOooooooOoooooX ",
" .XXOooooOoooooOoooooOOOOooXXXXXXXXXXXXXXXXXX.OoooooOoooooOoooooX ",
" .XXOooooooooOoooooOoo.XX.oooooXXXXXXXXXXXXXXX.oooOoooooOoooooOoooOOX. ",
" .XX.ooooooooooOoooooOXXXXXXOoooooXXXXXXXXXXX.OOoooooOoooooOoooooOOOooo.X ",
" .XXOooOoooooOoooooOoO.XXXXXXXXXOoooooXXXXXXXXoooooOoooooOoooooOooOOoOOoooooX ",
" XXXOoooooOoooooOoooo.XXXXXXXXXXXXXOoooooXXXXXXXooooooOoooooOooooOOOoooooOoooooX ",
" .XXXooooOoooooOooooo..XXXXXXXXXXXXXXXXXOoooOOXXXXXXXooooooOoooooOOOOooOooooooOoooooX ",
" ..XXooOoooooOoooooOooOXXXXXXXXXXXXXXXXXXXXX.OOoooXXXXXXXooooooOoo.XXXooooooOoooooOoooooX. ",
" ..XXOoooooOoooooOoooooXXXXXXXXXXXXXXXXXXXXXXXOoOoooooXXXXXXXoooooOXXXXXXXooooooOoooooOoooOOOX ",
" ..XXoooOooooooooooooooOOooXXXXXXXXXXXXXXXXXXX.oooooOoooooXXXXXXXo.XXXXXXXXXXXOoooooOoooooOOOooooX ",
" ..XXoOoooooOoooooOoooO.XOoooooXXXXXXXXXXXXXXX.OoOooooooOoooooXXXXXXXXXXXXXXXXXXXXXoooooOoOOOOoOoooooX ",
" ..XX.ooooOoooooOooooo..XXXXXoooooOXXXXXXXXXXXXOoooooOooooooOoooooXXXXXXXXXXXXXXXXXXXXXooooOOooooooOoooooX. ",
" ..XXoooOoooooOoooooOooOOoXXXXXXXooooo.XXXXXXX.OoooOoooooOooooooOooOOOXXXXXXXXXXXXXXXXXXXXXOOOooOooooooOooooo.X ",
" .XXOOoooooOoooooOoooooOOOooooXXXXXXXooooo.XXXXXXooooooOoooooOoooooOOoooooXXXXXXXXXXXXXXXXXXXOOooooooOoooooOooooooX ",
" .XXOoooOOoooooOoooooOoOOooOooooooXXXXXX.ooooOXXXXXXXooooooOoooooOoOOOooOoooooXXXXXXXXXXXXXXX.ooooOooooooOoooooOoooOOoX ",
" ..XX.oOoooooOooooooOoooOOOoooooOooooo.XXXXXXOOOOooXXXXXXXooooooOooo.XXooooooOoooooXXXXXXXXXX.OooOoooooOooooooOoooooOOoooooX. ",
" ..XXooooooOoooooOoooooOOOooooooooooOoooooXXXXXXXOoooooXXXXXXXoooooOXXXXXXXooooooOoooooXXXXXXX.ooooooOoooooOooooooOoOOOooOooooo.X ",
" .XXXoOoooooOoooooOoooooOOoooooOooooooOoooooXXXXXXXooooooXXXXXXXoooO.XXXXXXXXooooooOoooooXXXXXXX.ooooooOooooooOoooo.XOoooooOooooo.X ",
" XoooooOoooooOoooooXOOooooOoooooOoooooOoooOOOXXXXXXXooooooXXXXXXX.XXXXXXXXXXXXooooooOoooOOXXXXXXXXooooooOooooooO.XXXXXooooooOooooooX ",
" XoooooOooooooOo.XXX.ooooooOoooooOoooooOOOooooXXXXXXXoooooOXXXXXXXXXXXXXXXXXXXXoooooOOOOoooXXXXXXXXooooooOooo.XXXXXXXXXOoooooOooooooX ",
" XoooooOooooO.XXXXXXooooooOooooooOoOOooOooooo.XXXXXXXooooo.XXXXXXXXXXXXXXXXXXXXooOOOoOooooo.XXXXXXXooooooXXXXXXXXXXXXXX.oooooOOoooOOX. ",
" ..oooooOOOoooXXXXXXXooooooOooooOOOoooooOoooooXXXXXXX.OOOoo.XXXXXXXXXXXXXXXXXXXOOoooooOooooo.XXXXXXXoO.XXXXXXXXXXXXXXXXXXooooooOOOooo.X ",
" XooOOOOoooooOXXXXXX.ooooooOOOoooOoooooOooooooXXXXXXXOoooooXXXXXXXXXXXXXXXXXoooOooooooOooooo.XXXXXXXXXXXXXXXXXXXXXXXXXXXXooOOOoOooooooX ",
" .XoooooOoooooXXXXXXXooOOOOOooooooOoooooOooooooXXXXXXXooooooXXXXXXXXXXXX.OooooooOooooooOooooo.XXXXXXXXXXXXXXXXXXXXXXXXXXXXOoooooOooooooX ",
" XoooooOooooooXXXXXXXOoooooOoooooOooooooOoOOOooXXXXXXXooooooXXXXXXXX.ooooOooooooOooooooOooOOO.XXXXXXXXXXXXXXXXXXXXXXXX.ooOooooooOooooooX ",
" .XoooooOooooo.XXXXXX.ooooooOoooooOooooOOOooooo.XXXXXXXooooooXXXXXXXooooooOooooooOoooooOOOoooo.XXXXXXXXXXXXXXXXXXX.OOoooooOOoooooOOoooooX.",
" XoooooOoooOOOXXXXXXXooooooOooooooOOOoooOOoooooXXXXXXXXoooOOOXXXXXXXooooooOooooooOoOOoooOooooo.XXXXXXXXXXXXXXXXoooooOooooooOooooooOoo.XXXX",
" XooooOOOoooooXXXXXXXooooooOooOOooOoooooOooooooXXXXXXX.OOooo.XXXXXXXooooooOoooOOOOooooooOooooooXXXXXXXXXXX.ooOooooooOooooooOooooO.XXXX ",
" XOOOoooOoooooXXXXXXX.ooooOOOoooooOooooooOooooooXXXXXXXOooooo.XXXXXXXooooooOOoooooOooooooOooooooXXXXXX.OooooooOooooooOooooooXO.XXX. ",
" XoooooOooooooXXXXXXXOOOoooOOoooooOooooooOooooOOXXXXXXXooooooXXXXXXXXoOOOooOooooooOooooooOoooooOXXOoooOOooooooOooooooOooo.XXXX. ",
" XooooooOooooo.XXXXXXXooooooOooooooOoooooOOOOooo.XXXXXXXooooooXXXXXXX.ooooooOooooooOooooooOoOOOooOooooooOooooooOoooooOXXXX. ",
" XoooooOoooooOXXXXXXX.ooooooOooooooOooOOOOooooooXXXXXXXXooooooXXXXXXX.ooooooOooooooOooooOOOooooooOooooooOooooooOO.XXXX. ",
" ..oooooOOOOoooXXXXXXXoooooooOooooOOOoooooOooooooXXXXXXXXooOOooXXXXXXXOoooooOOoooooOOOOOoooOooooooOooooooOoooO.XXX.. ",
" XoooOOOOooooo.XXXXXXXooooooOOOOoooOooooooOooooooXXXXXXX.oooooOXXXXXXXOoooooOOooOOOoOooooooOooooooOoooooOXXXXX. ",
" .XoooooOooooooXXXXXXX.ooOOOOOooooooOooooooOoooooOXXXXXXXooooooOXXXXXXXoooooOOOooooooOooooooOooooooOo.XXXX. ",
" XooooooOoooooOXXXXXXXOoooooOOoooooOooooooOoooOOo.XXXXXXXoooooo.XXXXXXXOOOOooOoooooooOooooooOoooO.XXXX. ",
" .XoooooOooooooXXXXXXXXooooooOooooooOooooO.XooooooXXXXXXXXoooooo.XXXXXOOooooooOooooooooooooo.XXXXX. ",
" XooooooOoooOOOXXXXXXX.ooooooOoooooo..XXXXXXooooooXXXXXXXXoooooOXOoooooOooooooOoooooooooO.XXX. ",
" XoooooOOOoooo.XXXXXXXooooooOooo.XXXXXXXXXX.ooooooXXXXXXXXOOOoooOooooooOooooooOooooO.XXX. ",
" XOOOOooOooooooXXXXXXXXooooO.XXXXXXXXXXXXXXXooooooOXXXXXXOOooooooOooooooOooooooX.XXXX. ",
" XooooooOooooooXXXXXXXX.XXXXXXXXXXXXXXXXXXXXooooooOXOooooOoooooooOooooooOoo.XXXX. ",
" X.oooooOoooooo.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXoOOOooOooooooOooooooOOoooo.XXXX. ",
" XooooooOoooooOXXXXXXXXXXXXXXXXXXXXXXXXXXX.OOooooooOooooooOoooooo...XXX. ",
" X.oooooOOOOoooOXXXXXXXXXXXXXXXXXXXXXX.OooooOOooooooOooooooOoo..XXX. ",
" XoooOOoOooooooXXXXXXXXXXXXXXXXXX.OooOooooooOooooooOOoooo.XXXX. ",
" .XoooooOOooooooXXXXXXXXXXXXXOOoooooooOooooooOooooooX.XXXX. ",
" XooooooOoooooo.XXXXXXXXOoooooOooooooOoooooooOoo.XXXX. ",
" .XooooooOooooooXXXXoooOooooooOOooooooOooooO.XXXX. ",
" XooooooOoooOOOoOooooooOooooooOoooooooX.XXXX. ",
" XoooooOOOoooooOooooooOOooooooOoO.XXXX.. ",
" XXOOoooOOooooooOooooooOooooO.XXXX. ",
" XooooooOooooooOooooooOX.XXX. ",
" X.ooooooOooooooOoO.XXXX. ",
" XooooooOooooO.XXX.. ",
" X.oooooOXXXXX. ",
" XOO.XXX.. ",
".XX. "};

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 B

View File

@@ -0,0 +1,23 @@
/* XPM */
static char * zoomin_xpm[] = {
"16 16 3 1",
" c None",
". c Black",
"X c Gray100",
" .... ",
" ..XXXX.. ",
" .XXXXXXXX. ",
" .XXX..XXX. ",
".XXXX..XXXX. ",
".XX......XX. ",
".XX......XX. ",
".XXXX..XXXX. ",
" .XXX..XXX. ",
" .XXXXXXXX. ",
" ..XXXX... ",
" .... ... ",
" ... ",
" ... ",
" ... ",
" .. "
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 B

View File

@@ -0,0 +1,23 @@
/* XPM */
static char * zoomout_xpm[] = {
"16 16 3 1",
" c None",
". c Black",
"X c Gray100",
" .... ",
" ..XXXX.. ",
" .XXXXXXXX. ",
" .XXXXXXXX. ",
".XXXXXXXXXX. ",
".XX......XX. ",
".XX......XX. ",
".XXXXXXXXXX. ",
" .XXXXXXXX. ",
" .XXXXXXXX. ",
" ..XXXX... ",
" .... ... ",
" ... ",
" ... ",
" ... ",
" .. "
};

View File

@@ -10,29 +10,13 @@
/////////////////////////////////////////////////////////////////////////////
// ==========================================================================
// declarations
// headers, declarations, constants
// ==========================================================================
// --------------------------------------------------------------------------
// headers
// --------------------------------------------------------------------------
#ifdef __GNUG__
#pragma implementation "dialogs.h"
#endif
// for compilers that support precompilation, includes "wx/wx.h"
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "wx/statline.h"
#include "wx/spinctrl.h"
@@ -40,6 +24,18 @@
#include "life.h"
#include "game.h"
// --------------------------------------------------------------------------
// resources
// --------------------------------------------------------------------------
#if defined(__WXGTK__) || defined(__WXMOTIF__)
// logo for the about dialog
#include "bitmaps/life.xpm"
#endif
// sample configurations
#include "samples.inc"
// --------------------------------------------------------------------------
// constants
// --------------------------------------------------------------------------
@@ -47,24 +43,21 @@
// IDs for the controls and the menu commands
enum
{
// listbox in samples dialog
ID_LISTBOX = 2001
};
// bmp window in about dialog
ID_BMPWIN = 2001,
// sample configurations
#include "samples.inc"
// listbox in samples dialog
ID_LISTBOX
};
// --------------------------------------------------------------------------
// event tables and other macros for wxWindows
// --------------------------------------------------------------------------
// Event tables
BEGIN_EVENT_TABLE(LifeNewGameDialog, wxDialog)
EVT_BUTTON (wxID_OK, LifeNewGameDialog::OnOK)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(LifeSamplesDialog, wxDialog)
EVT_LISTBOX (ID_LISTBOX, LifeSamplesDialog::OnListBox)
END_EVENT_TABLE()
@@ -72,62 +65,6 @@ END_EVENT_TABLE()
// implementation
// ==========================================================================
// --------------------------------------------------------------------------
// LifeNewGameDialog
// --------------------------------------------------------------------------
LifeNewGameDialog::LifeNewGameDialog(wxWindow *parent, int *w, int *h)
: wxDialog(parent, -1,
_("New game"),
wxDefaultPosition,
wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL)
{
m_w = w;
m_h = h;
// spin ctrls
m_spinctrlw = new wxSpinCtrl( this, -1 );
m_spinctrlw->SetValue(*m_w);
m_spinctrlw->SetRange(LIFE_MIN, LIFE_MAX);
m_spinctrlh = new wxSpinCtrl( this, -1 );
m_spinctrlh->SetValue(*m_h);
m_spinctrlh->SetRange(LIFE_MIN, LIFE_MAX);
// component layout
wxBoxSizer *inputsizer1 = new wxBoxSizer( wxHORIZONTAL );
inputsizer1->Add( new wxStaticText(this, -1, _("Width")), 1, wxCENTRE | wxLEFT, 20);
inputsizer1->Add( m_spinctrlw, 2, wxCENTRE | wxLEFT | wxRIGHT, 20 );
wxBoxSizer *inputsizer2 = new wxBoxSizer( wxHORIZONTAL );
inputsizer2->Add( new wxStaticText(this, -1, _("Height")), 1, wxCENTRE | wxLEFT, 20);
inputsizer2->Add( m_spinctrlh, 2, wxCENTRE | wxLEFT | wxRIGHT, 20 );
wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
topsizer->Add( CreateTextSizer(_("Enter board dimensions")), 0, wxALL, 10 );
topsizer->Add( new wxStaticLine(this, -1), 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 10);
topsizer->Add( inputsizer1, 1, wxGROW | wxLEFT | wxRIGHT, 5 );
topsizer->Add( inputsizer2, 1, wxGROW | wxLEFT | wxRIGHT, 5 );
topsizer->Add( new wxStaticLine(this, -1), 0, wxGROW | wxLEFT | wxRIGHT | wxTOP, 10);
topsizer->Add( CreateButtonSizer(wxOK | wxCANCEL), 0, wxCENTRE | wxALL, 10);
// activate
SetSizer(topsizer);
SetAutoLayout(TRUE);
topsizer->SetSizeHints(this);
topsizer->Fit(this);
Centre(wxBOTH);
}
void LifeNewGameDialog::OnOK(wxCommandEvent& WXUNUSED(event))
{
*m_w = m_spinctrlw->GetValue();
*m_h = m_spinctrlh->GetValue();
EndModal(wxID_OK);
}
// --------------------------------------------------------------------------
// LifeSamplesDialog
// --------------------------------------------------------------------------
@@ -153,7 +90,7 @@ LifeSamplesDialog::LifeSamplesDialog(wxWindow *parent)
// descriptions
wxStaticBox *statbox = new wxStaticBox( this, -1, _("Description"));
m_life = new Life( 16, 16 );
m_life = new Life();
m_life->SetShape(g_shapes[0]);
m_canvas = new LifeCanvas( this, m_life, FALSE );
m_text = new wxTextCtrl( this, -1,
@@ -183,30 +120,73 @@ LifeSamplesDialog::LifeSamplesDialog(wxWindow *parent)
SetAutoLayout(TRUE);
sizer3->SetSizeHints(this);
sizer3->Fit(this);
Centre(wxBOTH);
Centre(wxBOTH | wxCENTRE_ON_SCREEN);
}
LifeSamplesDialog::~LifeSamplesDialog()
{
m_canvas->Destroy();
delete m_life;
}
int LifeSamplesDialog::GetValue()
const LifeShape& LifeSamplesDialog::GetShape()
{
return m_value;
return g_shapes[m_value];
}
void LifeSamplesDialog::OnListBox(wxCommandEvent& event)
{
if (event.GetSelection() != -1)
int sel = event.GetSelection();
if (sel != -1)
{
m_value = m_list->GetSelection();
m_text->SetValue(g_shapes[ event.GetSelection() ].m_desc);
m_life->SetShape(g_shapes[ event.GetSelection() ]);
m_text->SetValue(g_shapes[ sel ].m_desc);
m_life->SetShape(g_shapes[ sel ]);
m_canvas->DrawEverything(TRUE); // force redraw everything
m_canvas->Refresh(FALSE); // do not erase background
// quick and dirty :-)
if ((g_shapes[ sel ].m_width > 36) ||
(g_shapes[ sel ].m_height > 22))
m_canvas->SetCellSize(2);
else
m_canvas->SetCellSize(8);
}
}
// --------------------------------------------------------------------------
// LifeAboutDialog
// --------------------------------------------------------------------------
LifeAboutDialog::LifeAboutDialog(wxWindow *parent)
: wxDialog(parent, -1,
_("About Life!"),
wxDefaultPosition,
wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL)
{
// logo
wxBitmap bmp = wxBITMAP(life);
#if !defined(__WXGTK__) && !defined(__WXMOTIF__)
bmp.SetMask(new wxMask(bmp, *wxBLUE));
#endif
wxStaticBitmap *sbmp = new wxStaticBitmap(this, -1, bmp);
// layout components
wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
sizer->Add( sbmp, 0, wxCENTRE | wxALL, 10 );
sizer->Add( new wxStaticLine(this, -1), 0, wxGROW | wxLEFT | wxRIGHT, 5 );
sizer->Add( CreateTextSizer(_("Life! for wxWindows, version 2.0\n\n"
"(c) 2000 Guillermo Rodriguez Garcia\n\n"
"<guille@iies.es>\n\n"
"Portions of the code are based in XLife\n"
"XLife is (c) 1989 by Jon Bennett et al.")),
0, wxCENTRE | wxALL, 20 );
sizer->Add( new wxStaticLine(this, -1), 0, wxGROW | wxLEFT | wxRIGHT, 5 );
sizer->Add( CreateButtonSizer(wxOK), 0, wxCENTRE | wxALL, 10 );
// activate
SetSizer(sizer);
SetAutoLayout(TRUE);
sizer->SetSizeHints(this);
sizer->Fit(this);
Centre(wxBOTH | wxCENTRE_ON_SCREEN);
}

View File

@@ -34,33 +34,6 @@
#include "game.h"
// sample configurations
extern LifeShape g_shapes[];
// --------------------------------------------------------------------------
// LifeNewGameDialog
// --------------------------------------------------------------------------
class LifeNewGameDialog : public wxDialog
{
public:
// ctor
LifeNewGameDialog(wxWindow *parent, int *w, int *h);
// event handlers
void OnOK(wxCommandEvent& event);
private:
// any class wishing to process wxWindows events must use this macro
DECLARE_EVENT_TABLE();
int *m_w;
int *m_h;
wxSpinCtrl *m_spinctrlw;
wxSpinCtrl *m_spinctrlh;
};
// --------------------------------------------------------------------------
// LifeSamplesDialog
// --------------------------------------------------------------------------
@@ -73,14 +46,14 @@ public:
~LifeSamplesDialog();
// members
int GetValue();
const LifeShape& GetShape();
// event handlers
void OnListBox(wxCommandEvent &event);
private:
// any class wishing to process wxWindows events must use this macro
DECLARE_EVENT_TABLE();
DECLARE_EVENT_TABLE()
int m_value;
wxListBox *m_list;
@@ -89,4 +62,15 @@ private:
Life *m_life;
};
// --------------------------------------------------------------------------
// LifeAboutDialog
// --------------------------------------------------------------------------
class LifeAboutDialog : public wxDialog
{
public:
// ctor
LifeAboutDialog(wxWindow *parent);
};
#endif // _LIFE_DIALOGS_H_

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
/////////////////////////////////////////////////////////////////////////////
// Name: game.h
// Purpose: Life! game logic
// Purpose: Life! game logic, version 2
// Author: Guillermo Rodriguez Garcia, <guille@iies.es>
// Modified by:
// Created: Jan/2000
@@ -29,41 +29,37 @@
#endif
// --------------------------------------------------------------------------
// constants
// Cell
// --------------------------------------------------------------------------
// minimum and maximum table size, in each dimension
#define LIFE_MIN 20
#define LIFE_MAX 200
// --------------------------------------------------------------------------
// Cell and CellArray;
// --------------------------------------------------------------------------
typedef long Cell;
typedef wxArrayLong CellArray;
// A Cell is just a struct which contains a pair of (i, j) coords.
// These structs are not used internally anywhere; they are just
// used to pass cell coordinates around.
struct Cell
{
wxInt32 i;
wxInt32 j;
};
// --------------------------------------------------------------------------
// LifeShape
// --------------------------------------------------------------------------
// A class which holds a pattern
class LifeShape
{
public:
LifeShape::LifeShape(wxString name,
wxString desc,
int width, int height, char *data,
int fieldWidth = 20, int fieldHeight = 20,
bool wrap = TRUE)
int width,
int height,
char *data)
{
m_name = name;
m_desc = desc;
m_width = width;
m_height = height;
m_data = data;
m_fieldWidth = fieldWidth;
m_fieldHeight = fieldHeight;
m_wrap = wrap;
m_name = name;
m_desc = desc;
m_width = width;
m_height = height;
m_data = data;
}
wxString m_name;
@@ -71,59 +67,77 @@ public:
int m_width;
int m_height;
char *m_data;
int m_fieldWidth;
int m_fieldHeight;
bool m_wrap;
};
// --------------------------------------------------------------------------
// Life
// --------------------------------------------------------------------------
class CellBox;
class Life
{
public:
// ctor and dtor
Life(int width, int height);
Life();
~Life();
void Create(int width, int height);
void Destroy();
// game field
inline int GetWidth() const { return m_width; };
inline int GetHeight() const { return m_height; };
inline void SetBorderWrap(bool on) { m_wrap = on; };
// accessors
inline wxUint32 GetNumCells() const { return m_numcells; };
bool IsAlive (wxInt32 x, wxInt32 y);
void SetCell (wxInt32 x, wxInt32 y, bool alive = TRUE);
void SetShape(const LifeShape &shape);
// cells
bool IsAlive(int i, int j) const;
bool IsAlive(Cell c) const;
int GetX(Cell c) const;
int GetY(Cell c) const;
const CellArray* GetCells() const { return &m_cells; };
const CellArray* GetChangedCells() const { return &m_changed; };
// game logic
// game control
void Clear();
Cell SetCell(int i, int j, bool alive = TRUE);
void SetShape(LifeShape &shape);
bool NextTic();
// The following functions find cells within a given viewport; either
// all alive cells, or only those cells which have changed since last
// generation. You first call BeginFind() to specify the viewport,
// then keep calling FindMore() until it returns TRUE.
//
// BeginFind:
// Specify the viewport and whether to look for alive cells or for
// cells which have changed since the last generation and thus need
// to be repainted. In this latter case, there is no distinction
// between newborn or just-dead cells.
//
// FindMore:
// Fills an array with cells that match the specification given with
// BeginFind(). The array itself belongs to the Life object and must
// not be modified or freed by the caller. If this function returns
// FALSE, then the operation is not complete: just process all cells
// and call FillMore() again.
//
void BeginFind(wxInt32 i0, wxInt32 j0,
wxInt32 i1, wxInt32 j1,
bool changed);
bool FindMore(Cell *cells[], size_t *ncells);
private:
int GetNeighbors(int i, int j) const;
inline Cell MakeCell(int i, int j, bool alive) const;
// cellbox-related
CellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
CellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = TRUE);
void KillBox(CellBox *c);
enum CellFlags
{
CELL_DEAD = 0x0000, // is dead
CELL_ALIVE = 0x0001, // is alive
CELL_MARK = 0x0002, // will change / has changed
};
// helpers for FindMore & co.
void DoLine(wxInt32 i, wxInt32 j, wxUint32 alive, wxUint32 old);
void DoLine(wxInt32 i, wxInt32 j, wxUint32 alive);
int m_width;
int m_height;
CellArray m_cells;
CellArray m_changed;
bool m_wrap;
CellBox *m_head; // list of alive boxes
CellBox *m_available; // list of reusable dead boxes
CellBox **m_boxes; // hash table of alive boxes
wxUint32 m_numcells; // population (number of alive cells)
Cell *m_cells; // cell array for FindMore()
size_t m_ncells; // number of valid cells in cell array
wxInt32 m_i, m_j, // state vars for FindMore()
m_i0, m_j0,
m_i1, m_j1;
bool m_changed;
bool m_findmore;
};
#endif // _LIFE_GAME_H_

View File

@@ -10,29 +10,13 @@
/////////////////////////////////////////////////////////////////////////////
// ==========================================================================
// declarations
// headers, declarations, constants
// ==========================================================================
// --------------------------------------------------------------------------
// headers
// --------------------------------------------------------------------------
#ifdef __GNUG__
#pragma implementation "life.h"
#endif
// for compilers that support precompilation, includes "wx/wx.h"
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "wx/statline.h"
#include "life.h"
@@ -51,6 +35,8 @@
#include "bitmaps/reset.xpm"
#include "bitmaps/play.xpm"
#include "bitmaps/stop.xpm"
#include "bitmaps/zoomin.xpm"
#include "bitmaps/zoomout.xpm"
#endif
// --------------------------------------------------------------------------
@@ -61,15 +47,17 @@
enum
{
// menu items and toolbar buttons
ID_NEWGAME = 1001,
ID_RESET = 1001,
ID_SAMPLES,
ID_ABOUT,
ID_EXIT,
ID_CLEAR,
ID_CENTER,
ID_START,
ID_STEP,
ID_STOP,
ID_WRAP,
ID_ZOOMIN,
ID_ZOOMOUT,
ID_TOPSPEED,
// speed selection slider
ID_SLIDER
@@ -81,44 +69,49 @@ enum
// Event tables
BEGIN_EVENT_TABLE(LifeFrame, wxFrame)
EVT_MENU (ID_NEWGAME, LifeFrame::OnNewGame)
EVT_MENU (ID_SAMPLES, LifeFrame::OnSamples)
EVT_MENU (ID_ABOUT, LifeFrame::OnMenu)
EVT_MENU (ID_EXIT, LifeFrame::OnMenu)
EVT_MENU (ID_CLEAR, LifeFrame::OnMenu)
EVT_MENU (ID_START, LifeFrame::OnMenu)
EVT_MENU (ID_STEP, LifeFrame::OnMenu)
EVT_MENU (ID_STOP, LifeFrame::OnMenu)
EVT_MENU (ID_WRAP, LifeFrame::OnMenu)
EVT_COMMAND_SCROLL (ID_SLIDER, LifeFrame::OnSlider)
EVT_MENU (ID_SAMPLES, LifeFrame::OnSamples)
EVT_MENU (ID_RESET, LifeFrame::OnMenu)
EVT_MENU (ID_ABOUT, LifeFrame::OnMenu)
EVT_MENU (ID_EXIT, LifeFrame::OnMenu)
EVT_MENU (ID_CENTER, LifeFrame::OnMenu)
EVT_MENU (ID_START, LifeFrame::OnMenu)
EVT_MENU (ID_STEP, LifeFrame::OnMenu)
EVT_MENU (ID_STOP, LifeFrame::OnMenu)
EVT_MENU (ID_ZOOMIN, LifeFrame::OnMenu)
EVT_MENU (ID_ZOOMOUT, LifeFrame::OnMenu)
EVT_MENU (ID_TOPSPEED, LifeFrame::OnMenu)
EVT_COMMAND_SCROLL (ID_SLIDER, LifeFrame::OnSlider)
EVT_CLOSE ( LifeFrame::OnClose)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(LifeCanvas, wxScrolledWindow)
EVT_PAINT ( LifeCanvas::OnPaint)
EVT_SIZE ( LifeCanvas::OnSize)
EVT_MOUSE_EVENTS ( LifeCanvas::OnMouse)
BEGIN_EVENT_TABLE(LifeCanvas, wxWindow)
EVT_PAINT ( LifeCanvas::OnPaint)
EVT_SCROLLWIN ( LifeCanvas::OnScroll)
EVT_SIZE ( LifeCanvas::OnSize)
EVT_MOUSE_EVENTS ( LifeCanvas::OnMouse)
EVT_ERASE_BACKGROUND( LifeCanvas::OnEraseBackground)
END_EVENT_TABLE()
// Create a new application object
IMPLEMENT_APP(LifeApp)
// ==========================================================================
// implementation
// ==========================================================================
// some shortcuts
#define ADD_TOOL(a, b, c, d) \
toolBar->AddTool(a, b, wxNullBitmap, FALSE, -1, -1, (wxObject *)0, c, d)
#define ADD_TOOL(id, bmp, tooltip, help) \
toolBar->AddTool(id, bmp, wxNullBitmap, FALSE, -1, -1, (wxObject *)0, tooltip, help)
#define GET_FRAME() \
((LifeFrame *) wxGetApp().GetTopWindow())
#define GET_FRAME() ((LifeFrame *) wxGetApp().GetTopWindow())
// --------------------------------------------------------------------------
// LifeApp
// --------------------------------------------------------------------------
// `Main program' equivalent: the program execution "starts" here
// 'Main program' equivalent: the program execution "starts" here
bool LifeApp::OnInit()
{
// create the main application window
@@ -146,21 +139,23 @@ LifeFrame::LifeFrame() : wxFrame((wxFrame *)0, -1, _("Life!"), wxPoint(50, 50))
wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
wxMenu *menuGame = new wxMenu("", wxMENU_TEAROFF);
menuFile->Append(ID_NEWGAME, _("New game..."), _("Start a new game"));
menuFile->Append(ID_RESET, _("Reset"), _("Start a new game"));
menuFile->Append(ID_SAMPLES, _("Sample game..."), _("Select a sample configuration"));
menuFile->AppendSeparator();
menuFile->Append(ID_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog"));
menuFile->AppendSeparator();
menuFile->Append(ID_EXIT, _("E&xit\tAlt-X"), _("Quit this program"));
menuGame->Append(ID_CLEAR, _("&Clear\tCtrl-C"), _("Clear game field"));
menuGame->Append(ID_CENTER, _("Re&center\tCtrl-C"), _("Go to (0, 0)"));
menuGame->Append(ID_START, _("&Start\tCtrl-S"), _("Start"));
menuGame->Append(ID_STEP, _("&Next\tCtrl-N"), _("Single step"));
menuGame->Append(ID_STOP, _("S&top\tCtrl-T"), _("Stop"));
menuGame->Enable(ID_STOP, FALSE);
menuGame->AppendSeparator();
menuGame->Append(ID_WRAP, _("&Wraparound\tCtrl-W"), _("Wrap around borders"), TRUE);
menuGame->Check (ID_WRAP, TRUE);
menuGame->Append(ID_TOPSPEED, _("Top speed!"), _("Go as fast as possible"));
menuGame->AppendSeparator();
menuGame->Append(ID_ZOOMIN, _("Zoom &in\tCtrl-I"));
menuGame->Append(ID_ZOOMOUT, _("Zoom &out\tCtrl-O"));
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(menuFile, _("&File"));
@@ -168,36 +163,43 @@ LifeFrame::LifeFrame() : wxFrame((wxFrame *)0, -1, _("Life!"), wxPoint(50, 50))
SetMenuBar(menuBar);
// tool bar
wxBitmap tbBitmaps[3];
wxBitmap tbBitmaps[5];
tbBitmaps[0] = wxBITMAP(reset);
tbBitmaps[1] = wxBITMAP(play);
tbBitmaps[2] = wxBITMAP(stop);
tbBitmaps[3] = wxBITMAP(zoomin);
tbBitmaps[4] = wxBITMAP(zoomout);
wxToolBar *toolBar = CreateToolBar();
toolBar->SetMargins(5, 5);
toolBar->SetToolBitmapSize(wxSize(16, 16));
ADD_TOOL(ID_CLEAR, tbBitmaps[0], _("Clear"), _("Clear game board"));
ADD_TOOL(ID_START, tbBitmaps[1], _("Start"), _("Start"));
ADD_TOOL(ID_STOP , tbBitmaps[2], _("Stop"), _("Stop"));
toolBar->EnableTool(ID_STOP, FALSE);
ADD_TOOL(ID_RESET, tbBitmaps[0], _("Reset"), _("Start a new game"));
ADD_TOOL(ID_START, tbBitmaps[1], _("Start"), _("Start"));
ADD_TOOL(ID_STOP, tbBitmaps[2], _("Stop"), _("Stop"));
toolBar->AddSeparator();
ADD_TOOL(ID_ZOOMIN, tbBitmaps[3], _("Zoom in"), _("Zoom in"));
ADD_TOOL(ID_ZOOMOUT, tbBitmaps[4], _("Zoom out"), _("Zoom out"));
toolBar->Realize();
toolBar->EnableTool(ID_STOP, FALSE); // must be after Realize() !
// status bar
CreateStatusBar(2);
SetStatusText(_("Welcome to Life!"));
// game
// game and canvas
wxPanel *panel = new wxPanel(this, -1);
m_life = new Life(20, 20);
m_life = new Life();
m_canvas = new LifeCanvas(panel, m_life);
m_timer = new LifeTimer();
m_running = FALSE;
m_topspeed = FALSE;
m_interval = 500;
m_tics = 0;
m_text = new wxStaticText(panel, -1, "");
UpdateInfoText();
// slider
// speed selection slider
wxSlider *slider = new wxSlider(panel, ID_SLIDER,
5, 1, 10,
wxDefaultPosition,
@@ -220,49 +222,80 @@ LifeFrame::LifeFrame() : wxFrame((wxFrame *)0, -1, _("Life!"), wxPoint(50, 50))
LifeFrame::~LifeFrame()
{
delete m_timer;
delete m_life;
}
void LifeFrame::UpdateInfoText()
{
wxString msg;
msg.Printf(_("Generation: %u, Interval: %u ms"), m_tics, m_interval);
msg.Printf(_(" Generation: %u (T: %u ms), Population: %u "),
m_tics,
m_topspeed? 0 : m_interval,
m_life->GetNumCells());
m_text->SetLabel(msg);
}
// Enable or disable tools and menu entries according to the current
// state. See also wxEVT_UPDATE_UI events for a slightly different
// way to do this.
void LifeFrame::UpdateUI()
{
GetToolBar()->EnableTool(ID_START, !m_running);
GetToolBar()->EnableTool(ID_STOP, m_running);
GetMenuBar()->GetMenu(1)->Enable(ID_START, !m_running);
GetMenuBar()->GetMenu(1)->Enable(ID_STEP, !m_running);
GetMenuBar()->GetMenu(1)->Enable(ID_STOP, m_running);
}
// event handlers
void LifeFrame::OnMenu(wxCommandEvent& event)
{
switch (event.GetId())
{
case ID_CENTER : m_canvas->Recenter(0, 0); break;
case ID_START : OnStart(); break;
case ID_STEP : OnTimer(); break;
case ID_STOP : OnStop(); break;
case ID_WRAP :
case ID_ZOOMIN :
{
bool checked = GetMenuBar()->GetMenu(1)->IsChecked(ID_WRAP);
m_life->SetBorderWrap(checked);
int cellsize = m_canvas->GetCellSize();
if (cellsize < 32)
m_canvas->SetCellSize(cellsize * 2);
break;
}
case ID_CLEAR :
case ID_ZOOMOUT :
{
int cellsize = m_canvas->GetCellSize();
if (cellsize > 1)
m_canvas->SetCellSize(cellsize / 2);
break;
}
case ID_TOPSPEED:
{
m_running = TRUE;
m_topspeed = TRUE;
UpdateUI();
while (m_running && m_topspeed)
{
OnTimer();
wxYield();
}
break;
}
case ID_RESET:
{
// stop if it was running
OnStop();
m_life->Clear();
m_canvas->DrawEverything(TRUE);
m_canvas->Refresh(FALSE);
m_canvas->Recenter(0, 0);
m_tics = 0;
UpdateInfoText();
break;
}
case ID_ABOUT :
case ID_ABOUT:
{
wxMessageBox(
_("This is the about dialog of the Life! sample.\n"
"(c) 2000 Guillermo Rodriguez Garcia"),
_("About Life!"),
wxOK | wxICON_INFORMATION,
this);
LifeAboutDialog dialog(this);
dialog.ShowModal();
break;
}
case ID_EXIT :
@@ -274,42 +307,14 @@ void LifeFrame::OnMenu(wxCommandEvent& event)
}
}
void LifeFrame::OnNewGame(wxCommandEvent& WXUNUSED(event))
void LifeFrame::OnClose(wxCloseEvent& WXUNUSED(event))
{
int w = m_life->GetWidth();
int h = m_life->GetHeight();
// stop if it was running
// Stop if it was running; this is absolutely needed because
// the frame won't be actually destroyed until there are no
// more pending events, and this in turn won't ever happen
// if the timer is running faster than the window can redraw.
OnStop();
// dialog box
LifeNewGameDialog dialog(this, &w, &h);
// new game?
if (dialog.ShowModal() == wxID_OK)
{
// check dimensions
if (w >= LIFE_MIN && w <= LIFE_MAX &&
h >= LIFE_MIN && h <= LIFE_MAX)
{
// resize game field
m_life->Destroy();
m_life->Create(w, h);
// tell the canvas
m_canvas->Reset();
m_canvas->Refresh();
m_tics = 0;
UpdateInfoText();
}
else
{
wxString msg;
msg.Printf(_("Both dimensions must be within %u and %u.\n"),
LIFE_MIN, LIFE_MAX);
wxMessageBox(msg, _("Error!"), wxOK | wxICON_EXCLAMATION, this);
}
}
Destroy();
}
void LifeFrame::OnSamples(wxCommandEvent& WXUNUSED(event))
@@ -323,40 +328,14 @@ void LifeFrame::OnSamples(wxCommandEvent& WXUNUSED(event))
// new game?
if (dialog.ShowModal() == wxID_OK)
{
int result = dialog.GetValue();
if (result == -1)
return;
int gw = g_shapes[result].m_fieldWidth;
int gh = g_shapes[result].m_fieldHeight;
int wrap = g_shapes[result].m_wrap;
// set wraparound (don't ask the user)
m_life->SetBorderWrap(wrap);
GetMenuBar()->GetMenu(1)->Check(ID_WRAP, wrap);
// need to resize the game field?
if (gw > m_life->GetWidth() || gh > m_life->GetHeight())
{
wxString s;
s.Printf(_("Your game field is too small for this configuration.\n"
"It is recommended to resize it to %u x %u. Proceed?\n"),
gw, gh);
if (wxMessageBox(s, _("Question"), wxYES_NO | wxICON_QUESTION, this) == wxYES)
{
m_life->Destroy();
m_life->Create(gw, gh);
}
}
const LifeShape shape = dialog.GetShape();
// put the shape
m_life->SetShape(g_shapes[result]);
m_life->Clear();
m_life->SetShape(shape);
// tell the canvas about the change
m_canvas->Reset();
m_canvas->Refresh();
// recenter canvas
m_canvas->Recenter(0, 0);
m_tics = 0;
UpdateInfoText();
}
@@ -366,14 +345,9 @@ void LifeFrame::OnStart()
{
if (!m_running)
{
GetToolBar()->EnableTool(ID_START, FALSE);
GetToolBar()->EnableTool(ID_STOP, TRUE);
GetMenuBar()->GetMenu(1)->Enable(ID_START, FALSE);
GetMenuBar()->GetMenu(1)->Enable(ID_STEP, FALSE);
GetMenuBar()->GetMenu(1)->Enable(ID_STOP, TRUE);
m_timer->Start(m_interval);
m_running = TRUE;
UpdateUI();
}
}
@@ -381,14 +355,10 @@ void LifeFrame::OnStop()
{
if (m_running)
{
GetToolBar()->EnableTool(ID_START, TRUE);
GetToolBar()->EnableTool(ID_STOP, FALSE);
GetMenuBar()->GetMenu(1)->Enable(ID_START, TRUE);
GetMenuBar()->GetMenu(1)->Enable(ID_STEP, TRUE);
GetMenuBar()->GetMenu(1)->Enable(ID_STOP, FALSE);
m_timer->Stop();
m_running = FALSE;
m_topspeed = FALSE;
UpdateUI();
}
}
@@ -399,22 +369,20 @@ void LifeFrame::OnTimer()
else
OnStop();
m_canvas->DrawChanged();
UpdateInfoText();
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);
OnStop();
OnStart();
}
UpdateInfoText();
}
@@ -433,129 +401,209 @@ void LifeTimer::Notify()
// canvas constructor
LifeCanvas::LifeCanvas(wxWindow *parent, Life *life, bool interactive)
: wxScrolledWindow(parent, -1, wxPoint(0, 0), wxSize(100, 100))
: wxWindow(parent, -1, wxPoint(0, 0), wxSize(100, 100),
wxSUNKEN_BORDER)
{
m_life = life;
m_interactive = interactive;
m_cellsize = 8;
m_bmp = NULL;
Reset();
m_status = MOUSE_NOACTION;
m_viewportX = 0;
m_viewportY = 0;
m_viewportH = 0;
m_viewportW = 0;
if (m_interactive)
SetCursor(*wxCROSS_CURSOR);
// reduce flicker if wxEVT_ERASE_BACKGROUND is not available
SetBackgroundColour(*wxWHITE);
}
LifeCanvas::~LifeCanvas()
{
delete m_bmp;
delete m_life;
}
void LifeCanvas::Reset()
// recenter at the given position
void LifeCanvas::Recenter(wxInt32 i, wxInt32 j)
{
if (m_bmp)
delete m_bmp;
m_status = MOUSE_NOACTION;
m_width = CellToCoord(m_life->GetWidth()) + 1;
m_height = CellToCoord(m_life->GetHeight()) + 1;
m_bmp = new wxBitmap(m_width, m_height);
wxCoord w = GetClientSize().GetX();
wxCoord h = GetClientSize().GetY();
m_xoffset = (w > m_width)? ((w - m_width) / 2) : 0;
m_yoffset = (h > m_height)? ((h - m_height) / 2) : 0;
m_viewportX = i - m_viewportW / 2;
m_viewportY = j - m_viewportH / 2;
// redraw everything
DrawEverything(TRUE);
SetScrollbars(10, 10, (m_width + 9) / 10, (m_height + 9) / 10);
Refresh(FALSE);
}
void LifeCanvas::DrawEverything(bool force)
// set the cell size and refresh display
void LifeCanvas::SetCellSize(int cellsize)
{
wxMemoryDC dc;
m_cellsize = cellsize;
dc.SelectObject(*m_bmp);
dc.BeginDrawing();
// find current center
wxInt32 cx = m_viewportX + m_viewportW / 2;
wxInt32 cy = m_viewportY + m_viewportH / 2;
// draw cells
const CellArray *cells =
force? m_life->GetCells() : m_life->GetChangedCells();
// get current canvas size and adjust viewport accordingly
wxCoord w, h;
GetClientSize(&w, &h);
m_viewportW = (w + m_cellsize - 1) / m_cellsize;
m_viewportH = (h + m_cellsize - 1) / m_cellsize;
// recenter
m_viewportX = cx - m_viewportW / 2;
m_viewportY = cy - m_viewportH / 2;
for (unsigned i = 0; i < cells->GetCount(); i++)
DrawCell(cells->Item(i), dc);
// bounding rectangle (always drawn - better than clipping region)
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxTRANSPARENT_BRUSH);
dc.DrawRectangle(0, 0, m_width, m_height);
dc.EndDrawing();
dc.SelectObject(wxNullBitmap);
}
void LifeCanvas::DrawCell(Cell c)
{
wxMemoryDC dc;
dc.SelectObject(*m_bmp);
dc.BeginDrawing();
dc.SetClippingRegion(1, 1, m_width - 2, m_height - 2);
DrawCell(c, dc);
dc.EndDrawing();
dc.SelectObject(wxNullBitmap);
}
void LifeCanvas::DrawCell(Cell c, wxDC &dc)
{
if (m_life->IsAlive(c))
// adjust scrollbars
if (m_interactive)
{
SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW);
SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH);
m_thumbX = m_viewportW;
m_thumbY = m_viewportH;
}
Refresh(FALSE);
}
// draw a cell
void LifeCanvas::DrawCell(wxInt32 i, wxInt32 j, bool alive)
{
wxClientDC dc(this);
dc.SetPen(alive? *wxBLACK_PEN : *wxWHITE_PEN);
dc.SetBrush(alive? *wxBLACK_BRUSH : *wxWHITE_BRUSH);
dc.BeginDrawing();
DrawCell(i, j, dc);
dc.EndDrawing();
}
void LifeCanvas::DrawCell(wxInt32 i, wxInt32 j, wxDC &dc)
{
wxCoord x = CellToX(i);
wxCoord y = CellToY(j);
// if cellsize is 1 or 2, there will be no grid
switch (m_cellsize)
{
case 1:
dc.DrawPoint(x, y);
break;
case 2:
dc.DrawRectangle(x, y, 2, 2);
break;
default:
dc.DrawRectangle(x + 1, y + 1, m_cellsize - 1, m_cellsize - 1);
}
}
// draw all changed cells
void LifeCanvas::DrawChanged()
{
wxClientDC dc(this);
size_t ncells;
Cell *cells;
bool done = FALSE;
m_life->BeginFind(m_viewportX,
m_viewportY,
m_viewportX + m_viewportW,
m_viewportY + m_viewportH,
TRUE);
dc.BeginDrawing();
dc.SetLogicalFunction(wxINVERT);
if (m_cellsize == 1)
{
// drawn using DrawPoint
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxBLACK_BRUSH);
dc.DrawRectangle(CellToCoord( m_life->GetX(c) ),
CellToCoord( m_life->GetY(c) ),
m_cellsize,
m_cellsize);
}
else
{
dc.SetPen(*wxLIGHT_GREY_PEN);
dc.SetBrush(*wxTRANSPARENT_BRUSH);
dc.DrawRectangle(CellToCoord( m_life->GetX(c) ),
CellToCoord( m_life->GetY(c) ),
m_cellsize,
m_cellsize);
dc.SetPen(*wxWHITE_PEN);
dc.SetBrush(*wxWHITE_BRUSH);
dc.DrawRectangle(CellToCoord( m_life->GetX(c) ) + 1,
CellToCoord( m_life->GetY(c) ) + 1,
m_cellsize - 1,
m_cellsize - 1);
// drawn using DrawRectangle
dc.SetPen(*wxTRANSPARENT_PEN);
dc.SetBrush(*wxBLACK_BRUSH);
}
while (!done)
{
done = m_life->FindMore(&cells, &ncells);
for (size_t m = 0; m < ncells; m++)
DrawCell(cells[m].i, cells[m].j, dc);
}
dc.EndDrawing();
}
// event handlers
void LifeCanvas::OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(this);
wxMemoryDC memdc;
wxRect rect = GetUpdateRegion().GetBox();
wxCoord x, y, w, h;
wxInt32 i0, j0, i1, j1;
wxRegionIterator upd(GetUpdateRegion());
wxCoord x, y, w, h, xx, yy;
// find damaged area
x = rect.GetX();
y = rect.GetY();
w = rect.GetWidth();
h = rect.GetHeight();
i0 = XToCell(x);
j0 = YToCell(y);
i1 = XToCell(x + w - 1);
j1 = YToCell(y + h - 1);
size_t ncells;
Cell *cells;
bool done = FALSE;
m_life->BeginFind(i0, j0, i1, j1, FALSE);
done = m_life->FindMore(&cells, &ncells);
// erase all damaged cells and draw the grid
dc.BeginDrawing();
memdc.SelectObject(*m_bmp);
dc.SetBrush(*wxWHITE_BRUSH);
while(upd)
if (m_cellsize <= 2)
{
x = upd.GetX();
y = upd.GetY();
w = upd.GetW();
h = upd.GetH();
CalcUnscrolledPosition(x, y, &xx, &yy);
// no grid
dc.SetPen(*wxWHITE_PEN);
dc.DrawRectangle(x, y, w, h);
}
else
{
x = CellToX(i0);
y = CellToY(j0);
w = CellToX(i1 + 1) - x + 1;
h = CellToY(j1 + 1) - y + 1;
dc.Blit(x, y, w, h, &memdc, xx - m_xoffset, yy - m_yoffset);
upd++;
dc.SetPen(*wxLIGHT_GREY_PEN);
for (wxInt32 yy = y; yy <= (y + h - m_cellsize); yy += m_cellsize)
dc.DrawRectangle(x, yy, w, m_cellsize + 1);
for (wxInt32 xx = x; xx <= (x + w - m_cellsize); xx += m_cellsize)
dc.DrawLine(xx, y, xx, y + h);
}
memdc.SelectObject(wxNullBitmap);
// draw all alive cells
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxBLACK_BRUSH);
while (!done)
{
for (size_t m = 0; m < ncells; m++)
DrawCell(cells[m].i, cells[m].j, dc);
done = m_life->FindMore(&cells, &ncells);
}
// last set
for (size_t m = 0; m < ncells; m++)
DrawCell(cells[m].i, cells[m].j, dc);
dc.EndDrawing();
}
@@ -564,42 +612,21 @@ void LifeCanvas::OnMouse(wxMouseEvent& event)
if (!m_interactive)
return;
int x, y, xx, yy, i, j;
// which cell are we pointing at?
x = event.GetX();
y = event.GetY();
CalcUnscrolledPosition(x, y, &xx, &yy);
i = CoordToCell( xx - m_xoffset );
j = CoordToCell( yy - m_yoffset );
wxInt32 i = XToCell( event.GetX() );
wxInt32 j = YToCell( event.GetY() );
// adjust x, y to point to the upper left corner of the cell
CalcScrolledPosition( CellToCoord(i) + m_xoffset,
CellToCoord(j) + m_yoffset,
&x, &y );
// set cursor shape and statusbar text
if (i < 0 || i >= m_life->GetWidth() ||
j < 0 || j >= m_life->GetHeight())
{
GET_FRAME()->SetStatusText(wxEmptyString, 1);
SetCursor(*wxSTANDARD_CURSOR);
}
else
{
wxString msg;
msg.Printf(_("Cell: (%u, %u)"), i, j);
GET_FRAME()->SetStatusText(msg, 1);
SetCursor(*wxCROSS_CURSOR);
}
// set statusbar text
wxString msg;
msg.Printf(_("Cell: (%d, %d)"), i, j);
GET_FRAME()->SetStatusText(msg, 1);
// button pressed?
if (!event.LeftIsDown())
{
m_status = MOUSE_NOACTION;
}
else if (i >= 0 && i < m_life->GetWidth() &&
j >= 0 && j < m_life->GetHeight())
else
{
bool alive = m_life->IsAlive(i, j);
@@ -611,20 +638,124 @@ void LifeCanvas::OnMouse(wxMouseEvent& event)
if (((m_status == MOUSE_ERASING) && alive) ||
((m_status == MOUSE_DRAWING) && !alive))
{
wxRect rect(x, y, m_cellsize + 1, m_cellsize + 1);
DrawCell( m_life->SetCell(i, j, !alive) );
Refresh(FALSE, &rect);
m_life->SetCell(i, j, !alive);
DrawCell(i, j, !alive);
GET_FRAME()->UpdateInfoText();
}
}
}
void LifeCanvas::OnSize(wxSizeEvent& event)
{
// find center
wxInt32 cx = m_viewportX + m_viewportW / 2;
wxInt32 cy = m_viewportY + m_viewportH / 2;
// get new size
wxCoord w = event.GetSize().GetX();
wxCoord h = event.GetSize().GetY();
m_xoffset = (w > m_width)? ((w - m_width) / 2) : 0;
m_yoffset = (h > m_height)? ((h - m_height) / 2) : 0;
m_viewportW = (w + m_cellsize - 1) / m_cellsize;
m_viewportH = (h + m_cellsize - 1) / m_cellsize;
// recenter
m_viewportX = cx - m_viewportW / 2;
m_viewportY = cy - m_viewportH / 2;
// scrollbars
if (m_interactive)
{
SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW);
SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH);
m_thumbX = m_viewportW;
m_thumbY = m_viewportH;
}
// allow default processing
event.Skip();
}
void LifeCanvas::OnScroll(wxScrollWinEvent& event)
{
WXTYPE type = event.GetEventType();
int pos = event.GetPosition();
int orient = event.GetOrientation();
bool scrolling = event.IsScrolling();
int scrollinc = 0;
// calculate scroll increment
switch (type)
{
case wxEVT_SCROLLWIN_TOP:
{
if (orient == wxHORIZONTAL)
scrollinc = -m_viewportW;
else
scrollinc = -m_viewportH;
break;
}
case wxEVT_SCROLLWIN_BOTTOM:
{
if (orient == wxHORIZONTAL)
scrollinc = m_viewportW;
else
scrollinc = m_viewportH;
break;
}
case wxEVT_SCROLLWIN_LINEUP: scrollinc = -1; break;
case wxEVT_SCROLLWIN_LINEDOWN: scrollinc = +1; break;
case wxEVT_SCROLLWIN_PAGEUP: scrollinc = -10; break;
case wxEVT_SCROLLWIN_PAGEDOWN: scrollinc = +10; break;
case wxEVT_SCROLLWIN_THUMBTRACK:
{
if (scrolling)
{
// user is dragging the thumb in the scrollbar
if (orient == wxHORIZONTAL)
{
scrollinc = pos - m_thumbX;
m_thumbX = pos;
}
else
{
scrollinc = pos - m_thumbY;
m_thumbY = pos;
}
}
else
{
// user released the thumb after dragging
m_thumbX = m_viewportW;
m_thumbY = m_viewportH;
}
break;
}
}
#ifdef __WXGTK__ // what about Motif?
// wxGTK updates the thumb automatically (wxMSW doesn't); reset it back
if ((type != wxEVT_SCROLLWIN_THUMBTRACK) || !scrolling)
{
SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW);
SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH);
}
#endif
if (scrollinc == 0) return;
// scroll the window and adjust the viewport
if (orient == wxHORIZONTAL)
{
m_viewportX += scrollinc;
ScrollWindow( -m_cellsize * scrollinc, 0, (const wxRect *) NULL);
}
else
{
m_viewportY += scrollinc;
ScrollWindow( 0, -m_cellsize * scrollinc, (const wxRect *) NULL);
}
}
void LifeCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
{
// do nothing. I just don't want the background to be erased, you know.
}

View File

@@ -34,30 +34,46 @@
// LifeCanvas
// --------------------------------------------------------------------------
class LifeCanvas : public wxScrolledWindow
/* Note that in LifeCanvas, all cell coordinates are
* named i, j, while screen coordinates are named x, y.
*/
class LifeCanvas : public wxWindow
{
public:
// ctor and dtor
LifeCanvas(wxWindow* parent, Life* life, bool interactive = TRUE);
~LifeCanvas();
// member functions
void Reset();
void DrawEverything(bool force = FALSE);
void DrawCell(Cell c);
void DrawCell(Cell c, wxDC &dc);
inline int CellToCoord(int i) const { return (i * m_cellsize); };
inline int CoordToCell(int x) const { return ((x >= 0)? (x / m_cellsize) : -1); };
// view management
int GetCellSize() const { return m_cellsize; };
void SetCellSize(int cellsize);
void Recenter(wxInt32 i, wxInt32 j);
// event handlers
void OnPaint(wxPaintEvent& event);
void OnMouse(wxMouseEvent& event);
void OnSize(wxSizeEvent& event);
// drawing
void DrawChanged();
void DrawCell(wxInt32 i, wxInt32 j, bool alive);
private:
// any class wishing to process wxWindows events must use this macro
DECLARE_EVENT_TABLE()
// draw a cell (parametrized by DC)
void DrawCell(wxInt32 i, wxInt32 j, wxDC &dc);
// event handlers
void OnPaint(wxPaintEvent& event);
void OnMouse(wxMouseEvent& event);
void OnSize(wxSizeEvent& event);
void OnScroll(wxScrollWinEvent& event);
void OnEraseBackground(wxEraseEvent& event);
// conversion between cell and screen coordinates
inline wxInt32 XToCell(wxCoord x) const { return (x / m_cellsize) + m_viewportX; };
inline wxInt32 YToCell(wxCoord y) const { return (y / m_cellsize) + m_viewportY; };
inline wxCoord CellToX(wxInt32 i) const { return (i - m_viewportX) * m_cellsize; };
inline wxCoord CellToY(wxInt32 j) const { return (j - m_viewportY) * m_cellsize; };
// what is the user doing?
enum MouseStatus
{
MOUSE_NOACTION,
@@ -65,15 +81,16 @@ private:
MOUSE_ERASING
};
Life *m_life;
wxBitmap *m_bmp;
int m_height;
int m_width;
int m_cellsize;
wxCoord m_xoffset;
wxCoord m_yoffset;
MouseStatus m_status;
bool m_interactive;
Life *m_life; // Life object
int m_cellsize; // current cell size, in pixels
bool m_interactive; // is this canvas interactive?
MouseStatus m_status; // what is the user doing?
wxInt32 m_viewportX; // first visible cell (x coord)
wxInt32 m_viewportY; // first visible cell (y coord)
wxInt32 m_viewportW; // number of visible cells (w)
wxInt32 m_viewportH; // number of visible cells (h)
int m_thumbX; // horiz. scrollbar thumb position
int m_thumbY; // vert. scrollbar thumb position
};
// --------------------------------------------------------------------------
@@ -100,25 +117,27 @@ public:
// member functions
void UpdateInfoText();
// event handlers
void OnMenu(wxCommandEvent& event);
void OnNewGame(wxCommandEvent& event);
void OnSamples(wxCommandEvent& event);
void OnStart();
void OnStop();
void UpdateUI();
void OnTimer();
void OnSlider(wxScrollEvent& event);
private:
// any class wishing to process wxWindows events must use this macro
DECLARE_EVENT_TABLE()
Life *m_life;
LifeTimer *m_timer;
// event handlers
void OnMenu(wxCommandEvent& event);
void OnSamples(wxCommandEvent& event);
void OnSlider(wxScrollEvent& event);
void OnClose(wxCloseEvent& event);
void OnStart();
void OnStop();
Life *m_life;
LifeTimer *m_timer;
LifeCanvas *m_canvas;
wxStaticText *m_text;
bool m_running;
bool m_topspeed;
long m_interval;
long m_tics;
};

View File

@@ -1,6 +1,9 @@
mondrian ICON "mondrian.ico"
#include "wx/msw/wx.rc"
reset BITMAP "bitmaps/reset.bmp"
play BITMAP "bitmaps/play.bmp"
stop BITMAP "bitmaps/stop.bmp"
reset BITMAP "bitmaps/reset.bmp"
play BITMAP "bitmaps/play.bmp"
stop BITMAP "bitmaps/stop.bmp"
zoomin BITMAP "bitmaps/zoomin.bmp"
zoomout BITMAP "bitmaps/zoomout.bmp"
life BITMAP "bitmaps/life.bmp"

View File

@@ -17,14 +17,13 @@
*
* Name,
* Description,
* Width, Height,
* Data, ('*' = alive, '.' = dead)
* Field width, Field height, (optional, defaults to 20 x 20)
* Wraparound (optional, defaults to TRUE)
* Width,
* Height,
* Data ('*' = alive, '.' = dead)
*
*/
LifeShape g_shapes[] =
const LifeShape g_shapes[] =
{
LifeShape( _("Glider"),
_("The glider is the first of a series of life forms, known "
@@ -111,8 +110,7 @@ LifeShape g_shapes[] =
3, 3,
".**"
"**."
".*.",
80, 80, FALSE ),
".*." ),
LifeShape( _("Thunderbird"),
_("The thunderbird is another popular methuselah, which "
"doesn't stabilize until the 243th generation. Note that "
@@ -124,17 +122,14 @@ LifeShape g_shapes[] =
"..."
".*."
".*."
".*.",
60, 60, FALSE ),
".*." ),
LifeShape( _("Accorn"),
_("Probably the most popular methuselah, the accorn lives "
"for 5206 (!) generations. To see it in action, a very "
"large game field is needed."),
"for 5206 (!) generations."),
7, 3,
".*....."
"...*..."
"**..***",
150, 150, FALSE ),
"**..***" ),
LifeShape( _("Galaxy"),
_("One from my personal collection. It is really beautiful "
"to see this configuration expand and shrink periodically "
@@ -152,6 +147,110 @@ LifeShape g_shapes[] =
"*.....*......"
"......*......"
"......*......"
".......***...",
80, 80, FALSE )
".......***..." ),
LifeShape( _("Glider gun"),
_("A gun is a stationary pattern that emits spaceships "
"forever. The glider gun shown here was the first known "
"gun, and indeed the first known finite pattern with "
"unbounded growth. It was found by Bill Gosper in "
"November 1970. Many new guns have since been found."),
36, 9,
".........................*.........."
"......................****....*....."
".............*.......****.....*....."
"............*.*......*..*.........**"
"...........*...**....****.........**"
"**.........*...**.....****.........."
"**.........*...**........*.........."
"............*.*....................."
".............*......................" ),
LifeShape( _("Puffer train"),
_("A puffer is an object that moves like a spaceship, except "
"that it leaves a trail of debris behind. The puffer train "
"is one of the best-known puffers. Originally found by "
"Bill Gosper, this is a very dirty puffer; the tail does "
"not stabilize until generation 5533. It consists of a "
"B-heptomino (the middle pattern) escorted by two light "
"weight space ships."),
5, 18,
"...*."
"....*"
"*...*"
".****"
"....."
"....."
"....."
"*...."
".**.."
"..*.."
"..*.."
".*..."
"....."
"....."
"...*."
"....*"
"*...*"
".****" ),
LifeShape( _("Max"),
_("Max is the fastest-growing known pattern in Conway's Game "
"of Life (possibly the fastest possible). It fills space to "
"a density of 1/2, conjectured to be the maximum density, "
"and does it at a speed of c/2 in each of the 4 directions, "
"which has been proven to be the maximum possible speed.\n"
"\n"
"Population growth is:\n"
"[(t+19)^2+463]/4 for t divisible by 4;\n"
"[(t+19)^2+487]/4 for t even, not div. by 4;\n"
"[(t+18)^2+639]/4 for t odd.\n"
"\n"
"Original construction, top/bottom stretchers by Hartmut "
"Holzwart; Size optimization, left/right stretchers by David "
"Bell; Original idea, middle part, left/right stretcher "
"connection by Al Hensel.\n"
"\n"
"This spacefiller by David Bell, September 1993."),
29, 43,
".....*.*....................."
"....*..*....................."
"...**........................"
"..*.........................."
".****........................"
"*....*......................."
"*..*........................."
"*..*........................."
".*.........***...***........."
"..****.*..*..*...*..*........"
"...*...*.....*...*..........."
"....*........*...*..........."
"....*.*......*...*..........."
"............................."
"...***.....***...***........."
"...**.......*.....*.........."
"...***......*******.........."
"...........*.......*........."
"....*.*...***********........"
"...*..*..*............**....."
"...*.....************...*...."
"...*...*.............*...*..."
"....*...************.....*..."
".....**............*..*..*..."
"........***********...*.*...."
".........*.......*..........."
"..........*******......***..."
"..........*.....*.......**..."
".........***...***.....***..."
"............................."
"...........*...*......*.*...."
"...........*...*........*...."
"...........*...*.....*...*..."
"........*..*...*..*..*.****.."
".........***...***.........*."
".........................*..*"
".........................*..*"
".......................*....*"
"........................****."
"..........................*.."
"........................**..."
".....................*..*...."
".....................*.*....." )
};