44109b7871052839bdfbcd20bb3e77315154a866
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

1) /* Blinker
2)    Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
3)    Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4)    a blinkenarea.org project */
5) 
6) #ifndef BLINKER_TETRIS_H
7) #define BLINKER_TETRIS_H
8) 
9) #include <string>
10) #include <vector>
11) 
12) #include <BlinkenLib/BlinkenFrame.h>
13) 
14) #include "Color.h"
15) #include "ColorFile.h"
16) #include "File.h"
17) #include "Format.h"
18) #include "FormatFile.h"
19) #include "Game.h"
20) #include "Mgrs.h"
21) #include "Module.h"
22) #include "NameFile.h"
23) #include "OpConn.h"
24) #include "OpConnIf.h"
25) #include "OpReqIf.h"
26) #include "OutStreamFile.h"
27) #include "Time.h"
28) #include "TimeCallee.h"
29) #include "UIntFile.h"
30) 
31) namespace Blinker {
32) 
33) /// tetris game
34) class Tetris: public Game, public OpReqIf
35) {
36) protected:
Stefan Schuermans different colors for tetris...

Stefan Schuermans authored 5 years ago

37)   /// indices of the stones
38)   enum StoneIndex {
39)     StoneI,
40)     StoneL,
41)     StoneJ,
42)     StoneT,
43)     StoneO,
44)     StoneZ,
45)     StoneS,
Stefan Schuermans make tetris stones change c...

Stefan Schuermans authored 5 years ago

46)     StoneCnt ///< not an index, but number of stones
47)   };
48) 
49)   /// stone state
50)   enum State {
51)     Active, ///< stone is falling
52)     Fixed, ///< stone is fixed part of field
53)     StateCnt ///< not an index, but number of stone states
Stefan Schuermans different colors for tetris...

Stefan Schuermans authored 5 years ago

54)   };
55) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

56)   /// coordinates of a pixel part of a stone
57)   struct Coord {
58)     int x;
59)     int y;
60)   };
61) 
62)   /// descriptor of a certain rotation of a stone
63)   struct RotStone {
64)     Coord pixels[4]; ///< coordinates of the 4 pixels
65)   };
66) 
67)   /// descriptor of a stone
68)   struct Stone {
69)     RotStone rot[4]; ///< rotations of the stone
70)   };
71) 
72) public:
73)   /**
74)    * @brief constructor
75)    * @param[in] name module name
76)    * @param[in] mgrs managers
77)    * @param[in] dirBase base directory
78)    */
79)   Tetris(const std::string &name, Mgrs &mgrs, const Directory &dirBase);
80) 
81)   /// virtual destructor
82)   virtual ~Tetris();
83) 
84) private:
85)   /// copy constructor disabled
86)   Tetris(const Tetris &that);
87) 
88)   /// assignment operator disabled
89)   const Tetris & operator=(const Tetris &that);
90) 
91) public:
Stefan Schuermans change of some game params...

Stefan Schuermans authored 5 years ago

92)   /**
93)    * @brief check for update of configuration (derived game)
94)    * @param[in,out] doReinit set to true to ask for reinitialization
95)    * @param[in,out] doRedraw set to true to ask for redrawing
96)    */
97)   virtual void updateConfigGame(bool &doReinit, bool &doRedraw);
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

98) 
99)   /**
100)    * @brief check if accepting new operator connection is possible
101)    * @param[in] name operator interface name
102)    * @return if accepting new connection is possible
103)    */
104)   virtual bool acceptNewOpConn(const std::string &name);
105) 
106)   /**
107)    * @brief new operator connection
108)    * @param[in] name operator interface name
109)    * @param[in] pConn operator connection object
110)    *
111)    * The new connection may not yet be used for sending inside this callback.
112)    */
113)   virtual void newOpConn(const std::string &name, OpConn *pConn);
114) 
115)   /**
116)    * @brief key command received on operator connection
117)    * @param[in] pConn operator connection object
118)    * @param[in] key key that was pressed
119)    */
120)   virtual void opConnRecvKey(OpConn *pConn, char key);
121) 
122)   /**
123)    * @brief play command received on operator connection
124)    * @param[in] pConn operator connection object
125)    * @param[in] sound name of sound to play
126)    */
127)   virtual void opConnRecvPlay(OpConn *pConn, const std::string &sound);
128) 
129)   /**
130)    * @brief operator connection is closed
131)    * @param[in] pConn operator connection object
132)    *
133)    * The connection may not be used for sending any more in this callback.
134)    */
135)   virtual void opConnClose(OpConn *pConn);
136) 
137) protected:
138)   /// re-initialize game (e.g. due to config change)
139)   virtual void reinitialize();
140) 
141)   /// redraw current game image, expected to call sendFrame() at end
142)   virtual void redraw();
143) 
144)   /// process next time step of game
145)   virtual void timeStep();
146) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

147)   /// count time at end of game
148)   void timeGameOver();
149) 
150)   /// blink completed rows
151)   void timeBlinkRows();
152) 
153)   /// falling stone
154)   void timeStone();
155) 
Stefan Schuermans tetris: remove completed rows

Stefan Schuermans authored 5 years ago

156)   /// check for completed rows to disappear (new stone afterwards)
157)   void checkComplete();
158) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

159)   /// set up a new stone
160)   void newStone();
161) 
162)   /// set time for next time step of game - or unset if not needed
163)   void planTimeStep();
164) 
Stefan Schuermans tetris WIP: move/freeze stones

Stefan Schuermans authored 5 years ago

165)   /// get rotatation of stone from stone/rotation index (or NULL in invalid)
166)   static RotStone const * getRotStone(int stone, int rot);
167) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

168)   /// check if stone fits at position
169)   bool checkStoneFit(int stone, int rot, int y, int x) const;
170) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

171)   /// check if stone overflow game field
172)   bool checkStoneOverflow(int stone, int rot, int y, int x) const;
173) 
Stefan Schuermans tetris WIP: move/freeze stones

Stefan Schuermans authored 5 years ago

174)   /// freeze stone to field at position
175)   void freezeStone(int stone, int rot, int y, int x);
176) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

177)   /// draw a stone to image buffer
Stefan Schuermans make tetris stones change c...

Stefan Schuermans authored 5 years ago

178)   void drawStone(int stone, int rot, int y, int x, State state);
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

179) 
180)   /// wipe a stone from image buffer (i.e. replace it with background color)
181)   void wipeStone(int stone, int rot, int y, int x);
182) 
183)   /// set shape of stone to color in image buffer
184)   void colorStone(int stone, int rot, int y, int x, ColorData const &color);
185) 
186) protected:
Stefan Schuermans different colors for tetris...

Stefan Schuermans authored 5 years ago

187)   /// stone data (make sure it matches the StoneIndex enum)
188)   static Stone const c_stones[StoneCnt];
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

189) 
190)   static int const c_stoneCnt; ///< number of stones
191)   static int const c_rotCnt;   ///< number of rotations per stone
192)   static int const c_pixelCnt; ///< number of pixels per stone
193) 
194)   /// descriptor for delay value
195)   static ValueDescr const c_delayDescr;
196) 
Stefan Schuermans tetris: dropping stones

Stefan Schuermans authored 5 years ago

197)   /// descriptor for delay value during dropping a stone
198)   static ValueDescr const c_dropDelayDescr;
199) 
Stefan Schuermans tetris: add blink delay

Stefan Schuermans authored 5 years ago

200)   /// descriptor for delay value during blinking of disappearing rows
201)   static ValueDescr const c_blinkDelayDescr;
202) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

203)   /// descriptor for delay value at end of game
204)   static ValueDescr const c_gameOverDelayDescr;
205) 
Stefan Schuermans make tetris stones change c...

Stefan Schuermans authored 5 years ago

206)   ColorFile m_fileStoneIColor;   ///< color file for active I stone color
207)   ColorFile m_fileStoneLColor;   ///< color file for active L stone color
208)   ColorFile m_fileStoneJColor;   ///< color file for active J stone color
209)   ColorFile m_fileStoneTColor;   ///< color file for active T stone color
210)   ColorFile m_fileStoneOColor;   ///< color file for active O stone color
211)   ColorFile m_fileStoneZColor;   ///< color file for active Z stone color
212)   ColorFile m_fileStoneSColor;   ///< color file for active S stone color
213)   ColorFile m_fileFixedIColor;   ///< color file for fixed I stone color
214)   ColorFile m_fileFixedLColor;   ///< color file for fixed L stone color
215)   ColorFile m_fileFixedJColor;   ///< color file for fixed J stone color
216)   ColorFile m_fileFixedTColor;   ///< color file for fixed T stone color
217)   ColorFile m_fileFixedOColor;   ///< color file for fixed O stone color
218)   ColorFile m_fileFixedZColor;   ///< color file for fixed Z stone color
219)   ColorFile m_fileFixedSColor;   ///< color file for fixed S stone color
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

220)   UIntFile  m_fileDelay;         ///< file for initial delay in ms per frame
221)   UIntFile  m_fileDropDelay;     ///< file for delay while dropping (ms/frame)
222)   UIntFile  m_fileBlinkDelay;    ///< file for delay while blinking (ms/frame)
223)   UIntFile  m_fileGameOverDelay; ///< file for delay at end (ms/frame)
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

224) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

225)   NameFile m_fileStartSound;       ///< "start game" sound name file
226)   NameFile m_fileRowCompleteSound; ///< "row complete" sound name file
227)   NameFile m_fileGameOverSound;    ///< "game over" sound name file
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

228) 
Stefan Schuermans make tetris stones change c...

Stefan Schuermans authored 5 years ago

229)   ColorData m_stoneColors[StateCnt][StoneCnt]; ///< stone colors
Stefan Schuermans different colors for tetris...

Stefan Schuermans authored 5 years ago

230) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

231)   unsigned int m_delay;         ///< initial delay in ms per frame
232)   unsigned int m_dropDelay;     ///< delay while dropping in ms per frame
233)   unsigned int m_blinkDelay;    ///< delay while rows blinking in ms per frame
234)   unsigned int m_gameOverDelay; ///< delay at end of game in ms per frame
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

235) 
236)   OpConn *m_pConn; ///< operator connection of player (or NULL)
237) 
Stefan Schuermans tetris: one time step at ga...

Stefan Schuermans authored 5 years ago

238)   int  m_stone;     ///< index of current stone
239)   int  m_rot;       ///< rotation of current stone
240)   int  m_posX;      ///< x position of current stone
241)   int  m_posY;      ///< y position of current stone
242)   bool m_dropping;  ///< whether currently dropping a stone
243)   int  m_blinking;  ///< step of blinking: 0 not blinking, lsb == 0 -> visible
244)   int  m_completed; ///< number of overall completed rows
245)   bool m_gameOver;  ///< counter at end of game: 0 means game is running
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

246) 
247)   /// tetris field (y * m_width + x), -1 for free, >= 0 for pixel from stone
248)   std::vector<int> m_field;
Stefan Schuermans tetris: remove completed rows

Stefan Schuermans authored 5 years ago

249)   /// rows that are currently blinking (bool for each row, true when blinking)
250)   std::vector<bool> m_rowsBlink;