2b5e4c3f05478f29f9877d5b3efbb704a299db6f
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:
37)   /// coordinates of a pixel part of a stone
38)   struct Coord {
39)     int x;
40)     int y;
41)   };
42) 
43)   /// descriptor of a certain rotation of a stone
44)   struct RotStone {
45)     Coord pixels[4]; ///< coordinates of the 4 pixels
46)   };
47) 
48)   /// descriptor of a stone
49)   struct Stone {
50)     RotStone rot[4]; ///< rotations of the stone
51)   };
52) 
53) public:
54)   /**
55)    * @brief constructor
56)    * @param[in] name module name
57)    * @param[in] mgrs managers
58)    * @param[in] dirBase base directory
59)    */
60)   Tetris(const std::string &name, Mgrs &mgrs, const Directory &dirBase);
61) 
62)   /// virtual destructor
63)   virtual ~Tetris();
64) 
65) private:
66)   /// copy constructor disabled
67)   Tetris(const Tetris &that);
68) 
69)   /// assignment operator disabled
70)   const Tetris & operator=(const Tetris &that);
71) 
72) public:
73)   /// check for update of configuration (derived game), return true on update
74)   virtual bool updateConfigGame();
75) 
76)   /**
77)    * @brief check if accepting new operator connection is possible
78)    * @param[in] name operator interface name
79)    * @return if accepting new connection is possible
80)    */
81)   virtual bool acceptNewOpConn(const std::string &name);
82) 
83)   /**
84)    * @brief new operator connection
85)    * @param[in] name operator interface name
86)    * @param[in] pConn operator connection object
87)    *
88)    * The new connection may not yet be used for sending inside this callback.
89)    */
90)   virtual void newOpConn(const std::string &name, OpConn *pConn);
91) 
92)   /**
93)    * @brief key command received on operator connection
94)    * @param[in] pConn operator connection object
95)    * @param[in] key key that was pressed
96)    */
97)   virtual void opConnRecvKey(OpConn *pConn, char key);
98) 
99)   /**
100)    * @brief play command received on operator connection
101)    * @param[in] pConn operator connection object
102)    * @param[in] sound name of sound to play
103)    */
104)   virtual void opConnRecvPlay(OpConn *pConn, const std::string &sound);
105) 
106)   /**
107)    * @brief operator connection is closed
108)    * @param[in] pConn operator connection object
109)    *
110)    * The connection may not be used for sending any more in this callback.
111)    */
112)   virtual void opConnClose(OpConn *pConn);
113) 
114) protected:
115)   /// re-initialize game (e.g. due to config change)
116)   virtual void reinitialize();
117) 
118)   /// redraw current game image, expected to call sendFrame() at end
119)   virtual void redraw();
120) 
121)   /// process next time step of game
122)   virtual void timeStep();
123) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

124)   /// count time at end of game
125)   void timeGameOver();
126) 
127)   /// blink completed rows
128)   void timeBlinkRows();
129) 
130)   /// falling stone
131)   void timeStone();
132) 
Stefan Schuermans tetris: remove completed rows

Stefan Schuermans authored 5 years ago

133)   /// check for completed rows to disappear (new stone afterwards)
134)   void checkComplete();
135) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

136)   /// set up a new stone
137)   void newStone();
138) 
139)   /// set time for next time step of game - or unset if not needed
140)   void planTimeStep();
141) 
Stefan Schuermans tetris WIP: move/freeze stones

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

151)   /// freeze stone to field at position
152)   void freezeStone(int stone, int rot, int y, int x);
153) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

154)   /// draw a stone to image buffer
155)   void drawStone(int stone, int rot, int y, int x);
156) 
157)   /// wipe a stone from image buffer (i.e. replace it with background color)
158)   void wipeStone(int stone, int rot, int y, int x);
159) 
160)   /// set shape of stone to color in image buffer
161)   void colorStone(int stone, int rot, int y, int x, ColorData const &color);
162) 
163) protected:
164)   /// stone data
165)   static Stone const c_stones[7];
166) 
167)   static int const c_stoneCnt; ///< number of stones
168)   static int const c_rotCnt;   ///< number of rotations per stone
169)   static int const c_pixelCnt; ///< number of pixels per stone
170) 
171)   /// descriptor for delay value
172)   static ValueDescr const c_delayDescr;
173) 
Stefan Schuermans tetris: dropping stones

Stefan Schuermans authored 5 years ago

174)   /// descriptor for delay value during dropping a stone
175)   static ValueDescr const c_dropDelayDescr;
176) 
Stefan Schuermans tetris: add blink delay

Stefan Schuermans authored 5 years ago

177)   /// descriptor for delay value during blinking of disappearing rows
178)   static ValueDescr const c_blinkDelayDescr;
179) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

180)   /// descriptor for delay value at end of game
181)   static ValueDescr const c_gameOverDelayDescr;
182) 
183)   ColorFile m_fileStoneColor;    ///< color file for stone color
184)   UIntFile  m_fileDelay;         ///< file for initial delay in ms per frame
185)   UIntFile  m_fileDropDelay;     ///< file for delay while dropping (ms/frame)
186)   UIntFile  m_fileBlinkDelay;    ///< file for delay while blinking (ms/frame)
187)   UIntFile  m_fileGameOverDelay; ///< file for delay at end (ms/frame)
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

188) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

189)   NameFile m_fileStartSound;       ///< "start game" sound name file
190)   NameFile m_fileRowCompleteSound; ///< "row complete" sound name file
191)   NameFile m_fileGameOverSound;    ///< "game over" sound name file
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

192) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

193)   ColorData    m_stoneColor;    ///< stone color
194)   unsigned int m_delay;         ///< initial delay in ms per frame
195)   unsigned int m_dropDelay;     ///< delay while dropping in ms per frame
196)   unsigned int m_blinkDelay;    ///< delay while rows blinking in ms per frame
197)   unsigned int m_gameOverDelay; ///< delay at end of game in ms per frame
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

198) 
199)   OpConn *m_pConn; ///< operator connection of player (or NULL)
200) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

201)   int   m_stone;     ///< index of current stone
202)   int   m_rot;       ///< rotation of current stone
203)   int   m_posX;      ///< x position of current stone
204)   int   m_posY;      ///< y position of current stone
205)   bool  m_dropping;  ///< whether currently dropping a stone
206)   int   m_blinking;  ///< step of blinking: 0 not blinking, lsb == 0 -> visible
207)   int   m_completed; ///< number of overall completed rows
208)   int   m_gameOver;  ///< counter at end of game: 0 means game is running
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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