]> git.saurik.com Git - wxWidgets.git/blobdiff - demos/life/game.h
fixed bug with the caret positioning after SetValue() introduced by the last commit
[wxWidgets.git] / demos / life / game.h
index 12bb4ff3ddc0ed625c2d93b255854be9ef20ba51..1366c18fecaf83ba74c083a552977f7149ebc204 100644 (file)
 #endif
 
 // --------------------------------------------------------------------------
-// Cell
-// --------------------------------------------------------------------------
-
-// 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
+// LifePattern
 // --------------------------------------------------------------------------
 
 // A class which holds a pattern
-class LifeShape
+class LifePattern
 {
 public:
-    LifeShape::LifeShape(wxString name,
-                         wxString desc,
-                         int width,
-                         int height,
-                         char *data)
+    // This ctor is used by the LifeReader class
+    LifePattern(wxString      name,
+                wxString      description,
+                wxString      rules,
+                wxArrayString shape)
+    {
+        m_name        = name;
+        m_description = description;
+        m_rules       = rules;
+        m_shape       = shape;
+    };
+    
+    // A more convenient ctor for the built-in samples    
+    LifePattern(wxString      name,
+                wxString      description,
+                int           width,
+                int           height,
+                const char   *shape)
     {
-        m_name   = name;
-        m_desc   = desc;
-        m_width  = width;
-        m_height = height;
-        m_data   = data;
-    }
-
-    wxString  m_name;
-    wxString  m_desc;
-    int       m_width;
-    int       m_height;
-    char     *m_data;
+        m_name        = name;
+        m_description = description;
+        m_rules       = _("");
+        m_shape.Add( wxString::Format("%i %i", -width/2, -height/2) );
+        for(int j = 0; j < height; j++)
+            m_shape.Add( wxString(shape + (j * width), (size_t) width) );        
+    };
+
+    wxString      m_name;
+    wxString      m_description;
+    wxString      m_rules;
+    wxArrayString m_shape;
 };
 
 
@@ -75,8 +74,17 @@ public:
 // Life
 // --------------------------------------------------------------------------
 
-class CellBox;
+// A struct used to pass cell coordinates around
+struct LifeCell
+{
+    wxInt32 i;
+    wxInt32 j;
+};       
+
+// A private class that contains data about a block of cells
+class LifeCellBox;
 
+// A class that models a Life game instance
 class Life
 {
 public:
@@ -85,21 +93,23 @@ public:
     ~Life();
 
     // 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);
+    inline wxUint32 GetNumCells() const    { return m_numcells; };
+    inline wxString GetRules() const       { return m_rules; };
+    inline wxString GetDescription() const { return m_description; };
+    bool IsAlive(wxInt32 x, wxInt32 y);
+    void SetCell(wxInt32 x, wxInt32 y, bool alive = TRUE);
+    void SetPattern(const LifePattern &pattern);
 
     // game control
     void Clear();
     bool NextTic();
 
     // navigation
-    Cell FindNorth();
-    Cell FindSouth();
-    Cell FindWest();
-    Cell FindEast();
-    Cell FindCenter();
+    LifeCell FindNorth();
+    LifeCell FindSouth();
+    LifeCell FindWest();
+    LifeCell FindEast();
+    LifeCell FindCenter();
 
     // The following functions find cells within a given viewport; either
     // all alive cells, or only those cells which have changed since last
@@ -119,32 +129,40 @@ public:
     //  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,
+    void BeginFind(wxInt32 x0, wxInt32 y0,
+                   wxInt32 x1, wxInt32 y1,
                    bool changed);
-    bool FindMore(Cell *cells[], size_t *ncells);
+    bool FindMore(LifeCell *cells[], size_t *ncells);
 
 private:
     // cellbox-related
-    CellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
-    CellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = TRUE);
-    void KillBox(CellBox *c);
+    LifeCellBox *CreateBox(wxInt32 x, wxInt32 y, wxUint32 hv);
+    LifeCellBox *LinkBox(wxInt32 x, wxInt32 y, bool create = TRUE);
+    void KillBox(LifeCellBox *c);
 
     // helper for BeginFind & FindMore
-    void DoLine(wxInt32 i, wxInt32 j, wxUint32 alive, wxUint32 old = 0);
-
-
-    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;
+    void DoLine(wxInt32 x, wxInt32 y, wxUint32 alive, wxUint32 old = 0);
+
+
+    // pattern description
+    wxString   m_name;          // name (currently unused)
+    wxString   m_rules;         // rules (currently unused)
+    wxString   m_description;   // description
+
+    // pattern data
+    LifeCellBox   *m_head;          // list of alive boxes
+    LifeCellBox   *m_available;     // list of reusable dead boxes
+    LifeCellBox  **m_boxes;         // hash table of alive boxes
+    wxUint32   m_numcells;      // population (number of alive cells)
+
+    // state vars for BeginFind & FindMore
+    LifeCell  *m_cells;         // array of cells
+    size_t     m_ncells;        // number of valid entries in m_cells
+    wxInt32    m_x, m_y,        // counters and search mode
+               m_x0, m_y0,
+               m_x1, m_y1;
+    bool       m_changed;
+    bool       m_findmore;
 };
 
 #endif  // _LIFE_GAME_H_