e897205a2f2e4ef710f8b48d0a3b8c7e93cd8632
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:
Stefan Schuermans change of some game params...

Stefan Schuermans authored 5 years ago

73)   /**
74)    * @brief check for update of configuration (derived game)
75)    * @param[in,out] doReinit set to true to ask for reinitialization
76)    * @param[in,out] doRedraw set to true to ask for redrawing
77)    */
78)   virtual void updateConfigGame(bool &doReinit, bool &doRedraw);
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

128)   /// count time at end of game
129)   void timeGameOver();
130) 
131)   /// blink completed rows
132)   void timeBlinkRows();
133) 
134)   /// falling stone
135)   void timeStone();
136) 
Stefan Schuermans tetris: remove completed rows

Stefan Schuermans authored 5 years ago

137)   /// check for completed rows to disappear (new stone afterwards)
138)   void checkComplete();
139) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

155)   /// freeze stone to field at position
156)   void freezeStone(int stone, int rot, int y, int x);
157) 
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

178)   /// descriptor for delay value during dropping a stone
179)   static ValueDescr const c_dropDelayDescr;
180) 
Stefan Schuermans tetris: add blink delay

Stefan Schuermans authored 5 years ago

181)   /// descriptor for delay value during blinking of disappearing rows
182)   static ValueDescr const c_blinkDelayDescr;
183) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

184)   /// descriptor for delay value at end of game
185)   static ValueDescr const c_gameOverDelayDescr;
186) 
187)   ColorFile m_fileStoneColor;    ///< color file for stone color
188)   UIntFile  m_fileDelay;         ///< file for initial delay in ms per frame
189)   UIntFile  m_fileDropDelay;     ///< file for delay while dropping (ms/frame)
190)   UIntFile  m_fileBlinkDelay;    ///< file for delay while blinking (ms/frame)
191)   UIntFile  m_fileGameOverDelay; ///< file for delay at end (ms/frame)
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)   NameFile m_fileStartSound;       ///< "start game" sound name file
194)   NameFile m_fileRowCompleteSound; ///< "row complete" sound name file
195)   NameFile m_fileGameOverSound;    ///< "game over" sound name file
Stefan Schuermans tetris game WIP

Stefan Schuermans authored 5 years ago

196) 
Stefan Schuermans tetris: complete game logic

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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

Stefan Schuermans authored 5 years ago

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