63b405136463fe6ba6c403e11d9e82df8569ddc3
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,
46)     StoneCnt // not an index, but number of stones
47)   };
48) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

85)   /**
86)    * @brief check for update of configuration (derived game)
87)    * @param[in,out] doReinit set to true to ask for reinitialization
88)    * @param[in,out] doRedraw set to true to ask for redrawing
89)    */
90)   virtual void updateConfigGame(bool &doReinit, bool &doRedraw);
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

140)   /// count time at end of game
141)   void timeGameOver();
142) 
143)   /// blink completed rows
144)   void timeBlinkRows();
145) 
146)   /// falling stone
147)   void timeStone();
148) 
Stefan Schuermans tetris: remove completed rows

Stefan Schuermans authored 5 years ago

149)   /// check for completed rows to disappear (new stone afterwards)
150)   void checkComplete();
151) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

152)   /// set up a new stone
153)   void newStone();
154) 
155)   /// set time for next time step of game - or unset if not needed
156)   void planTimeStep();
157) 
Stefan Schuermans tetris WIP: move/freeze stones

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

167)   /// freeze stone to field at position
168)   void freezeStone(int stone, int rot, int y, int x);
169) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

170)   /// draw a stone to image buffer
171)   void drawStone(int stone, int rot, int y, int x);
172) 
173)   /// wipe a stone from image buffer (i.e. replace it with background color)
174)   void wipeStone(int stone, int rot, int y, int x);
175) 
176)   /// set shape of stone to color in image buffer
177)   void colorStone(int stone, int rot, int y, int x, ColorData const &color);
178) 
179) protected:
Stefan Schuermans different colors for tetris...

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

182) 
183)   static int const c_stoneCnt; ///< number of stones
184)   static int const c_rotCnt;   ///< number of rotations per stone
185)   static int const c_pixelCnt; ///< number of pixels per stone
186) 
187)   /// descriptor for delay value
188)   static ValueDescr const c_delayDescr;
189) 
Stefan Schuermans tetris: dropping stones

Stefan Schuermans authored 5 years ago

190)   /// descriptor for delay value during dropping a stone
191)   static ValueDescr const c_dropDelayDescr;
192) 
Stefan Schuermans tetris: add blink delay

Stefan Schuermans authored 5 years ago

193)   /// descriptor for delay value during blinking of disappearing rows
194)   static ValueDescr const c_blinkDelayDescr;
195) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

196)   /// descriptor for delay value at end of game
197)   static ValueDescr const c_gameOverDelayDescr;
198) 
Stefan Schuermans different colors for tetris...

Stefan Schuermans authored 5 years ago

199)   ColorFile m_fileStoneIColor;   ///< color file for I stone color
200)   ColorFile m_fileStoneLColor;   ///< color file for L stone color
201)   ColorFile m_fileStoneJColor;   ///< color file for J stone color
202)   ColorFile m_fileStoneTColor;   ///< color file for T stone color
203)   ColorFile m_fileStoneOColor;   ///< color file for O stone color
204)   ColorFile m_fileStoneZColor;   ///< color file for Z stone color
205)   ColorFile m_fileStoneSColor;   ///< color file for S stone color
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

206)   UIntFile  m_fileDelay;         ///< file for initial delay in ms per frame
207)   UIntFile  m_fileDropDelay;     ///< file for delay while dropping (ms/frame)
208)   UIntFile  m_fileBlinkDelay;    ///< file for delay while blinking (ms/frame)
209)   UIntFile  m_fileGameOverDelay; ///< file for delay at end (ms/frame)
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

210) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

211)   NameFile m_fileStartSound;       ///< "start game" sound name file
212)   NameFile m_fileRowCompleteSound; ///< "row complete" sound name file
213)   NameFile m_fileGameOverSound;    ///< "game over" sound name file
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

214) 
Stefan Schuermans different colors for tetris...

Stefan Schuermans authored 5 years ago

215)   ColorData m_stoneColors[StoneCnt]; ///< stone colors
216) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

217)   unsigned int m_delay;         ///< initial delay in ms per frame
218)   unsigned int m_dropDelay;     ///< delay while dropping in ms per frame
219)   unsigned int m_blinkDelay;    ///< delay while rows blinking in ms per frame
220)   unsigned int m_gameOverDelay; ///< delay at end of game in ms per frame
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

224)   int  m_stone;     ///< index of current stone
225)   int  m_rot;       ///< rotation of current stone
226)   int  m_posX;      ///< x position of current stone
227)   int  m_posY;      ///< y position of current stone
228)   bool m_dropping;  ///< whether currently dropping a stone
229)   int  m_blinking;  ///< step of blinking: 0 not blinking, lsb == 0 -> visible
230)   int  m_completed; ///< number of overall completed rows
231)   bool m_gameOver;  ///< counter at end of game: 0 means game is running
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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