bd73fc2378f29446e724ee7cc98bf5644cfc300b
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: remove completed rows

Stefan Schuermans authored 5 years ago

124)   /// check for completed rows to disappear (new stone afterwards)
125)   void checkComplete();
126) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

127)   /// set up a new stone
128)   void newStone();
129) 
130)   /// set time for next time step of game - or unset if not needed
131)   void planTimeStep();
132) 
Stefan Schuermans tetris WIP: move/freeze stones

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

136)   /// check if stone fits at position
137)   bool checkStoneFit(int stone, int rot, int y, int x) const;
138) 
Stefan Schuermans tetris WIP: move/freeze stones

Stefan Schuermans authored 5 years ago

139)   /// freeze stone to field at position
140)   void freezeStone(int stone, int rot, int y, int x);
141) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

142)   /// draw a stone to image buffer
143)   void drawStone(int stone, int rot, int y, int x);
144) 
145)   /// wipe a stone from image buffer (i.e. replace it with background color)
146)   void wipeStone(int stone, int rot, int y, int x);
147) 
148)   /// set shape of stone to color in image buffer
149)   void colorStone(int stone, int rot, int y, int x, ColorData const &color);
150) 
151) protected:
152)   /// stone data
153)   static Stone const c_stones[7];
154) 
155)   static int const c_stoneCnt; ///< number of stones
156)   static int const c_rotCnt;   ///< number of rotations per stone
157)   static int const c_pixelCnt; ///< number of pixels per stone
158) 
159)   /// descriptor for delay value
160)   static ValueDescr const c_delayDescr;
161) 
Stefan Schuermans tetris: dropping stones

Stefan Schuermans authored 5 years ago

162)   /// descriptor for delay value during dropping a stone
163)   static ValueDescr const c_dropDelayDescr;
164) 
Stefan Schuermans tetris: add blink delay

Stefan Schuermans authored 5 years ago

165)   /// descriptor for delay value during blinking of disappearing rows
166)   static ValueDescr const c_blinkDelayDescr;
167) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

168)   ColorFile m_fileStoneColor; ///< color file for stone color
169)   UIntFile  m_fileDelay;      ///< file for initial delay in ms per frame
Stefan Schuermans tetris: dropping stones

Stefan Schuermans authored 5 years ago

170)   UIntFile  m_fileDropDelay;  ///< file for delay while dropping (ms / frame)
Stefan Schuermans tetris: add blink delay

Stefan Schuermans authored 5 years ago

171)   UIntFile  m_fileBlinkDelay; ///< file for delay while blinking (ms / frame)
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

172) 
173)   NameFile m_fileStartSound; ///< "start game" sound name file
174) 
175)   ColorData    m_stoneColor; ///< stone color
Stefan Schuermans tetris: dropping stones

Stefan Schuermans authored 5 years ago

176)   unsigned int m_delay;      ///< initial delay in ms per frame
177)   unsigned int m_dropDelay;  ///< delay while dropping in ms per frame
Stefan Schuermans tetris: add blink delay

Stefan Schuermans authored 5 years ago

178)   unsigned int m_blinkDelay; ///< delay while rwos blinking in ms per frame
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

179) 
180)   OpConn *m_pConn; ///< operator connection of player (or NULL)
181) 
Stefan Schuermans tetris: dropping stones

Stefan Schuermans authored 5 years ago

182)   int   m_stone;    ///< index of current stone
183)   int   m_rot;      ///< rotation of current stone
184)   int   m_posX;     ///< x position of current stone
185)   int   m_posY;     ///< y position of current stone
186)   bool  m_dropping; ///< whether currently dropping a stone
Stefan Schuermans tetris: remove completed rows

Stefan Schuermans authored 5 years ago

187)   int   m_blinking; ///< step of blinking: 0 not blinking, lsb == 0 -> visible
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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