1 /*
2  *      tetristabitem.h
3  *
4  *      Copyright 2009 David Vachulka <arch_dvx@users.sourceforge.net>
5  *      Copyright (C) 2007-2008 Graeme Gott <graeme@gottcode.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  *
12  *      This program is distributed in the hope that it will be useful,
13  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *      GNU General Public License for more details.
16  *
17  *      You should have received a copy of the GNU General Public License
18  *      along with this program; if not, write to the Free Software
19  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  *      MA 02110-1301, USA.
21  */
22 
23 #ifndef TETRISTABITEM_H
24 #define	TETRISTABITEM_H
25 
26 #include "defs.h"
27 #include "dxtabbook.h"
28 #include "fxext.h"
29 
30 static const FXint columns = 12;
31 static const FXint rows = 20;
32 
33 struct Cell
34 {
35     Cell(FXint x1 = -1, FXint y1 = -1)
xCell36     :   x(x1),
37         y(y1)
38     {
39     }
40 
41     bool operator==(const Cell& cell) const
42     {
43         return x == cell.x && y == cell.y;
44     }
45 
46     FXint x;
47     FXint y;
48 };
49 
50 class TetrisTabItem;
51 
52 class Piece
53 {
54 public:
55     Piece(FXint type, TetrisTabItem *parent);
56 
isValid()57     FXbool isValid() const { return m_valid; }
moveLeft()58     FXbool moveLeft() { return move(-1, 0); }
moveRight()59     FXbool moveRight() { return move(1, 0); }
moveDown()60     FXbool moveDown() { return move(0, 1); }
61     FXbool rotate();
62     void drop();
63     static void cells(Cell* cells, FXint type);
64 
65 private:
66     FXint m_type;
67     TetrisTabItem *m_parent;
68     Cell m_cells[4];
69     Cell m_pivot;
70     FXbool m_valid;
71 
72     FXbool move(FXint x, FXint y);
73     FXbool updatePosition(const Cell* ucells);
74 };
75 
76 class TetrisTabItem : public dxEXTabItem
77 {
78     FXDECLARE(TetrisTabItem)
79 public:
80     TetrisTabItem(dxTabBook*, const FXString&, FXIcon*, FXuint, FXint);
81     virtual ~TetrisTabItem();
82 
83     FXbool cell(FXint x, FXint y) const;
84     void addCell(FXint x, FXint y, FXint type);
85     void removeCell(FXint x, FXint y, FXbool update=TRUE);
86     void findFullLines();
87     void createGeom();
88     void setColor(IrcColor);
89     void setGameFocus();
90     void newGame();
91     void stopGame();
92     void pauseResumeGame();
93     void reparentTab();
94     void redraw();
95     void moveLeft();
96     void moveRight();
97     void rotate();
98     void drop();
isPauseEnable()99     FXbool isPauseEnable() { return m_pauseEnable; }
isPaused()100     FXbool isPaused() { return m_paused; }
getID()101     FXint getID() { return m_id; }
102 
103     long onPaint(FXObject*, FXSelector, void*);
104     long onTimeout(FXObject*, FXSelector, void*);
105     long onNewGame(FXObject*, FXSelector, void*);
106     long onPauseGame(FXObject*, FXSelector, void*);
107 
108 private:
TetrisTabItem()109     TetrisTabItem():m_parent(NULL),m_mainframe(NULL),m_gameframe(NULL),m_otherframe(NULL),m_splitter(NULL)
110     ,m_gamecanvas(NULL),m_nextcanvas(NULL),m_levelLabel(NULL),m_scoreLabel(NULL),m_linesLabel(NULL)
111     ,m_newButton(NULL),m_pauseButton(NULL),m_messageFont(NULL),m_apiece(0),m_anext(0),m_removedLines(0)
112     ,m_level(0),m_score(0),m_nextPiece(0),m_id(-1),m_paused(FALSE),m_done(FALSE),m_pauseEnable(FALSE)
113     ,m_piece(NULL),m_penColor(0)
114     {}
115 
116     dxTabBook *m_parent;
117     FXVerticalFrame *m_mainframe, *m_gameframe, *m_otherframe;
118     FXSplitter *m_splitter;
119     FXCanvas *m_gamecanvas, *m_nextcanvas;
120     FXLabel *m_levelLabel, *m_scoreLabel, *m_linesLabel;
121     dxEXButton *m_newButton, *m_pauseButton;
122     FXFont *m_messageFont;
123     FXint m_cells[columns][rows];
124     FXint m_fullLines[4];
125     FXint m_apiece, m_anext;
126     FXint m_removedLines, m_level, m_score, m_nextPiece, m_id;
127     FXbool m_paused, m_done, m_pauseEnable;
128     Piece *m_piece;
129     FXColor m_penColor;
130 
131     void createPiece();
132     void drawLines();
133     void drawNextPiece(FXint type);
134     void removeLines();
135     void landPiece();
136     void gameOver();
137     void updateLabels();
138     void updateCell(FXint x, FXint y);
139 };
140 
141 #endif	/* TETRISTABITEM_H */
142 
143