BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
bd73fc2
Branches
Tags
master
Blinker
src
common
Tetris.h
tetris: remove completed rows
Stefan Schuermans
commited
bd73fc2
at 2019-07-14 18:59:44
Tetris.h
Blame
History
Raw
/* Blinker Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #ifndef BLINKER_TETRIS_H #define BLINKER_TETRIS_H #include <string> #include <vector> #include <BlinkenLib/BlinkenFrame.h> #include "Color.h" #include "ColorFile.h" #include "File.h" #include "Format.h" #include "FormatFile.h" #include "Game.h" #include "Mgrs.h" #include "Module.h" #include "NameFile.h" #include "OpConn.h" #include "OpConnIf.h" #include "OpReqIf.h" #include "OutStreamFile.h" #include "Time.h" #include "TimeCallee.h" #include "UIntFile.h" namespace Blinker { /// tetris game class Tetris: public Game, public OpReqIf { protected: /// coordinates of a pixel part of a stone struct Coord { int x; int y; }; /// descriptor of a certain rotation of a stone struct RotStone { Coord pixels[4]; ///< coordinates of the 4 pixels }; /// descriptor of a stone struct Stone { RotStone rot[4]; ///< rotations of the stone }; public: /** * @brief constructor * @param[in] name module name * @param[in] mgrs managers * @param[in] dirBase base directory */ Tetris(const std::string &name, Mgrs &mgrs, const Directory &dirBase); /// virtual destructor virtual ~Tetris(); private: /// copy constructor disabled Tetris(const Tetris &that); /// assignment operator disabled const Tetris & operator=(const Tetris &that); public: /// check for update of configuration (derived game), return true on update virtual bool updateConfigGame(); /** * @brief check if accepting new operator connection is possible * @param[in] name operator interface name * @return if accepting new connection is possible */ virtual bool acceptNewOpConn(const std::string &name); /** * @brief new operator connection * @param[in] name operator interface name * @param[in] pConn operator connection object * * The new connection may not yet be used for sending inside this callback. */ virtual void newOpConn(const std::string &name, OpConn *pConn); /** * @brief key command received on operator connection * @param[in] pConn operator connection object * @param[in] key key that was pressed */ virtual void opConnRecvKey(OpConn *pConn, char key); /** * @brief play command received on operator connection * @param[in] pConn operator connection object * @param[in] sound name of sound to play */ virtual void opConnRecvPlay(OpConn *pConn, const std::string &sound); /** * @brief operator connection is closed * @param[in] pConn operator connection object * * The connection may not be used for sending any more in this callback. */ virtual void opConnClose(OpConn *pConn); protected: /// re-initialize game (e.g. due to config change) virtual void reinitialize(); /// redraw current game image, expected to call sendFrame() at end virtual void redraw(); /// process next time step of game virtual void timeStep(); /// check for completed rows to disappear (new stone afterwards) void checkComplete(); /// set up a new stone void newStone(); /// set time for next time step of game - or unset if not needed void planTimeStep(); /// get rotatation of stone from stone/rotation index (or NULL in invalid) static RotStone const * getRotStone(int stone, int rot); /// check if stone fits at position bool checkStoneFit(int stone, int rot, int y, int x) const; /// freeze stone to field at position void freezeStone(int stone, int rot, int y, int x); /// draw a stone to image buffer void drawStone(int stone, int rot, int y, int x); /// wipe a stone from image buffer (i.e. replace it with background color) void wipeStone(int stone, int rot, int y, int x); /// set shape of stone to color in image buffer void colorStone(int stone, int rot, int y, int x, ColorData const &color); protected: /// stone data static Stone const c_stones[7]; static int const c_stoneCnt; ///< number of stones static int const c_rotCnt; ///< number of rotations per stone static int const c_pixelCnt; ///< number of pixels per stone /// descriptor for delay value static ValueDescr const c_delayDescr; /// descriptor for delay value during dropping a stone static ValueDescr const c_dropDelayDescr; /// descriptor for delay value during blinking of disappearing rows static ValueDescr const c_blinkDelayDescr; ColorFile m_fileStoneColor; ///< color file for stone color UIntFile m_fileDelay; ///< file for initial delay in ms per frame UIntFile m_fileDropDelay; ///< file for delay while dropping (ms / frame) UIntFile m_fileBlinkDelay; ///< file for delay while blinking (ms / frame) NameFile m_fileStartSound; ///< "start game" sound name file ColorData m_stoneColor; ///< stone color unsigned int m_delay; ///< initial delay in ms per frame unsigned int m_dropDelay; ///< delay while dropping in ms per frame unsigned int m_blinkDelay; ///< delay while rwos blinking in ms per frame OpConn *m_pConn; ///< operator connection of player (or NULL) int m_stone; ///< index of current stone int m_rot; ///< rotation of current stone int m_posX; ///< x position of current stone int m_posY; ///< y position of current stone bool m_dropping; ///< whether currently dropping a stone int m_blinking; ///< step of blinking: 0 not blinking, lsb == 0 -> visible /// tetris field (y * m_width + x), -1 for free, >= 0 for pixel from stone std::vector<int> m_field; /// rows that are currently blinking (bool for each row, true when blinking) std::vector<bool> m_rowsBlink; }; // class Tetris } // namespace Blinker #endif // #ifndef BLINKER_TETRIS_H