2020-08-22 23:15:46 +00:00
|
|
|
#include "sdlpp.hpp"
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
#include <mutex>
|
|
|
|
#include <SDL2/SDL2_framerate.h>
|
|
|
|
|
|
|
|
#define COLIDER_ID 0x00000001
|
|
|
|
#define BRICK_ID 0x00000002
|
|
|
|
#define GAME_OVER 0x00000003
|
|
|
|
|
2020-08-23 09:37:07 +00:00
|
|
|
#define LEFT_BORDER 0.3
|
|
|
|
#define RIGHT_BORDER 0.7
|
|
|
|
#define BOTTOM_BORDER 1
|
|
|
|
#define TOP_BORDER 0.16
|
|
|
|
#define BLOCK_SIZE 0.04
|
|
|
|
|
2020-08-23 13:30:45 +00:00
|
|
|
#define TICKS_TILL_FALL 500
|
|
|
|
#define TICKS_TILL_DESCEND 50
|
|
|
|
|
2020-08-23 13:49:38 +00:00
|
|
|
#define TETRIS_BRICK 0
|
|
|
|
#define TETRIS_T 1
|
|
|
|
#define TETRIS_L_RIGHT 2
|
|
|
|
#define TETRIS_Z_RIGHT 3
|
|
|
|
#define TETRIS_LINE 4
|
|
|
|
#define TETRIS_L_LEFT 5
|
|
|
|
#define TETRIS_Z_LEFT 6
|
|
|
|
|
2020-08-24 16:44:42 +00:00
|
|
|
#define PAUSE_PAUSE 1
|
|
|
|
#define PAUSE_GAME_OVER 2
|
|
|
|
|
|
|
|
int pause = 0;
|
2020-08-22 23:15:46 +00:00
|
|
|
int pause_select = 0;
|
2020-08-24 16:21:35 +00:00
|
|
|
int pause_max = 2;
|
2020-08-24 16:44:42 +00:00
|
|
|
int game_over_select = 0;
|
|
|
|
int game_over_max = 1;
|
2020-08-23 13:30:45 +00:00
|
|
|
int ticks_till_fall = TICKS_TILL_FALL;
|
|
|
|
int ticks_till_descend = TICKS_TILL_DESCEND;
|
2020-08-23 07:50:05 +00:00
|
|
|
std::vector< std::shared_ptr< SDLPP::RectangleRender > > pause_options;
|
2020-08-24 16:44:42 +00:00
|
|
|
std::vector< std::shared_ptr< SDLPP::RectangleRender > > game_over_options;
|
2020-08-23 07:50:05 +00:00
|
|
|
std::shared_ptr< SDLPP::TextRenderer > score_texture;
|
|
|
|
std::shared_ptr< SDLPP::Renderer > active_renderer;
|
2020-08-22 23:15:46 +00:00
|
|
|
int score = 0;
|
|
|
|
bool update_score = false;
|
2020-08-23 13:30:45 +00:00
|
|
|
bool checked_line = false;
|
|
|
|
bool wait_for_anim = false;
|
2020-08-22 23:15:46 +00:00
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
std::vector< int > bag = { 28, 28, 28, 28, 28, 28, 28 };
|
2020-08-23 13:49:38 +00:00
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
std::shared_ptr< SDLPP::Font > font;
|
|
|
|
std::shared_ptr< SDLPP::Scene > active_scene;
|
|
|
|
std::shared_ptr< SDLPP::Scene > pause_scene;
|
2020-08-24 16:44:42 +00:00
|
|
|
std::shared_ptr< SDLPP::Scene > game_over_scene;
|
2020-08-22 23:15:46 +00:00
|
|
|
|
2020-08-23 13:49:38 +00:00
|
|
|
class TetrisBlock : public SDLPP::RectangleRender {
|
|
|
|
public:
|
|
|
|
TetrisBlock() = delete;
|
|
|
|
TetrisBlock( double x, double y, double w, double h,
|
2020-08-24 06:48:25 +00:00
|
|
|
std::shared_ptr< SDLPP::Renderer > &r,
|
|
|
|
const std::string &img_or_color, bool is_polygon = false,
|
|
|
|
int index = 0 )
|
2020-08-23 13:49:38 +00:00
|
|
|
: RectangleRender( x, y, w, h, r, img_or_color, is_polygon ) {
|
2020-08-24 06:48:25 +00:00
|
|
|
_index = index;
|
|
|
|
bag[_index]--;
|
2020-08-23 13:49:38 +00:00
|
|
|
}
|
|
|
|
~TetrisBlock() {
|
|
|
|
bag[_index]++;
|
|
|
|
}
|
2020-08-24 14:36:24 +00:00
|
|
|
bool isSamePos(const SDLPP::RenderObject &other) const {
|
|
|
|
auto mypos = getPos();
|
|
|
|
auto otherpos = other.getPos();
|
|
|
|
auto diff1 = mypos.first - otherpos.first;
|
|
|
|
diff1 = (diff1 < 0) * (-1) * diff1 + (diff1 > 0) * diff1;
|
|
|
|
auto diff2 = mypos.second - otherpos.second;
|
|
|
|
diff2 = (diff2 < 0) * (-1) * diff2 + (diff2 > 0) * diff2;
|
|
|
|
return diff1 < 0.0001 && diff2 < 0.0001;
|
|
|
|
}
|
2020-08-24 06:48:25 +00:00
|
|
|
|
2020-08-23 13:49:38 +00:00
|
|
|
private:
|
|
|
|
int _index = 0;
|
|
|
|
};
|
|
|
|
|
2020-08-22 23:15:46 +00:00
|
|
|
class TetrisPiece {
|
|
|
|
public:
|
2020-08-23 09:37:07 +00:00
|
|
|
TetrisPiece() {
|
|
|
|
original_pos.reserve( 4 );
|
|
|
|
}
|
2020-08-24 14:36:24 +00:00
|
|
|
void addPiece( std::shared_ptr< TetrisBlock > piece, int x,
|
2020-08-23 07:50:05 +00:00
|
|
|
int y ) {
|
|
|
|
pieces.push_back( piece );
|
|
|
|
pieces_rel_position.push_back( { 0, 0, 0, 0 } );
|
2020-08-22 23:15:46 +00:00
|
|
|
// done this way for SPEEEEEEED
|
2020-08-23 07:50:05 +00:00
|
|
|
// left
|
|
|
|
pieces_rel_position.back()[0] = ( x < 0 ) * ( -1 ) * x;
|
|
|
|
// right
|
|
|
|
pieces_rel_position.back()[1] = ( x > 0 ) * x;
|
|
|
|
// top
|
|
|
|
pieces_rel_position.back()[2] = ( y < 0 ) * ( -1 ) * y;
|
|
|
|
// bottom
|
|
|
|
pieces_rel_position.back()[3] = ( y > 0 ) * y;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
void rotate() {
|
2020-08-23 07:50:05 +00:00
|
|
|
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
auto &piece = pieces[i];
|
|
|
|
auto &positions = pieces_rel_position[i];
|
|
|
|
auto position = piece->getPos();
|
2020-08-23 09:37:07 +00:00
|
|
|
original_pos[i] = position;
|
|
|
|
position.first += positions[0] * BLOCK_SIZE;
|
|
|
|
position.first -= positions[1] * BLOCK_SIZE;
|
|
|
|
position.second += positions[2] * BLOCK_SIZE;
|
|
|
|
position.second -= positions[3] * BLOCK_SIZE;
|
2020-08-22 23:15:46 +00:00
|
|
|
auto bottom = positions[3];
|
|
|
|
auto top = positions[2];
|
|
|
|
positions[3] = positions[1];
|
|
|
|
positions[2] = positions[0];
|
|
|
|
positions[1] = top;
|
|
|
|
positions[0] = bottom;
|
2020-08-23 09:37:07 +00:00
|
|
|
position.first -= positions[0] * BLOCK_SIZE;
|
|
|
|
position.first += positions[1] * BLOCK_SIZE;
|
|
|
|
position.second -= positions[2] * BLOCK_SIZE;
|
|
|
|
position.second += positions[3] * BLOCK_SIZE;
|
2020-08-23 07:50:05 +00:00
|
|
|
piece->setPos( position.first, position.second );
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-23 09:37:07 +00:00
|
|
|
void revert() {
|
|
|
|
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
|
2020-08-24 15:44:34 +00:00
|
|
|
auto &piece = pieces[i];
|
|
|
|
auto &positions = pieces_rel_position[i];
|
|
|
|
piece->setPos( original_pos[i].first, original_pos[i].second );
|
|
|
|
auto top = positions[1];
|
|
|
|
auto bottom = positions[0];
|
|
|
|
positions[1] = positions[3];
|
|
|
|
positions[0] = positions[2];
|
|
|
|
positions[2] = top;
|
|
|
|
positions[3] = bottom;
|
2020-08-23 09:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-24 14:36:24 +00:00
|
|
|
std::vector< std::shared_ptr< TetrisBlock > > &getObjects() {
|
2020-08-22 23:15:46 +00:00
|
|
|
return pieces;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
void setPos( double x, double y ) {
|
|
|
|
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
auto &piece = pieces[i];
|
|
|
|
auto pos = piece->getPos();
|
2020-08-23 07:50:05 +00:00
|
|
|
piece->setPos( x + pos.first - default_x,
|
|
|
|
y + pos.second - default_y );
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
setDefPos( x, y );
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
void setDefPos( double x, double y ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
default_x = x;
|
|
|
|
default_y = y;
|
|
|
|
}
|
|
|
|
void clear() {
|
|
|
|
pieces.clear();
|
|
|
|
pieces_rel_position.clear();
|
|
|
|
}
|
|
|
|
void startDescend() {
|
|
|
|
descend = true;
|
|
|
|
}
|
|
|
|
void stopDescend() {
|
|
|
|
descend = false;
|
|
|
|
}
|
|
|
|
bool isDescending() {
|
|
|
|
return descend;
|
|
|
|
}
|
2020-08-24 14:36:24 +00:00
|
|
|
bool isLeft(const SDLPP::RenderObject &block) const {
|
|
|
|
return isPosition(block, 0);
|
|
|
|
}
|
|
|
|
bool isRight(const SDLPP::RenderObject &block) const {
|
|
|
|
return isPosition(block, 1);
|
|
|
|
}
|
|
|
|
void movePiece(double x, double y) {
|
|
|
|
for ( auto &block : getObjects() ) {
|
|
|
|
auto pos = block->getPos();
|
|
|
|
block->setPos( pos.first + x, pos.second + y );
|
|
|
|
}
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
|
2020-08-22 23:15:46 +00:00
|
|
|
private:
|
2020-08-24 14:36:24 +00:00
|
|
|
bool isPosition(const SDLPP::RenderObject &block, int pos) const {
|
|
|
|
for(int i = 0; i < 4; i++) {
|
|
|
|
if(pieces[i]->isSamePos(block)) {
|
|
|
|
return pieces_rel_position[i][pos] != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
std::vector< std::vector< int > > pieces_rel_position;
|
2020-08-24 14:36:24 +00:00
|
|
|
std::vector< std::shared_ptr< TetrisBlock > > pieces;
|
2020-08-23 09:37:07 +00:00
|
|
|
std::vector< std::pair< double, double > > original_pos;
|
2020-08-22 23:15:46 +00:00
|
|
|
double default_x;
|
|
|
|
double default_y;
|
|
|
|
bool descend = false;
|
|
|
|
};
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
std::vector< std::shared_ptr< SDLPP::RectangleRender > > line_coliders;
|
2020-08-24 06:48:25 +00:00
|
|
|
std::shared_ptr< TetrisPiece > cur_object;
|
|
|
|
std::shared_ptr< TetrisPiece > next_object;
|
2020-08-22 23:15:46 +00:00
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
void doInput( std::shared_ptr< SDLPP::Scene > scene );
|
2020-08-22 23:15:46 +00:00
|
|
|
void doInputPause();
|
2020-08-24 16:44:42 +00:00
|
|
|
void doInputGameOver();
|
2020-08-22 23:15:46 +00:00
|
|
|
bool quit = false;
|
|
|
|
|
|
|
|
std::mutex movement_mutex;
|
|
|
|
|
2020-08-24 14:36:24 +00:00
|
|
|
std::shared_ptr< TetrisBlock >
|
2020-08-23 07:50:05 +00:00
|
|
|
createTetrisBlock( double x, double y, const std::string &color,
|
2020-08-23 13:49:38 +00:00
|
|
|
const std::string &outline, int index,
|
2020-08-23 07:50:05 +00:00
|
|
|
std::shared_ptr< SDLPP::Renderer > renderer,
|
|
|
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
2020-08-24 06:48:25 +00:00
|
|
|
auto ret = std::make_shared< TetrisBlock >( x, y, BLOCK_SIZE, BLOCK_SIZE,
|
|
|
|
renderer, color, true, index );
|
2020-08-23 07:50:05 +00:00
|
|
|
ret->setOutlineColor( outline );
|
|
|
|
ret->addCollision( SDLPP::Rect( 0.1, 0.1, 0.8, 0.8 ) );
|
|
|
|
ret->setId( BRICK_ID );
|
2020-08-22 23:15:46 +00:00
|
|
|
ret->centerX();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene->addObject( ret );
|
2020-08-22 23:15:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
std::shared_ptr< TetrisPiece >
|
|
|
|
tetrisBrick( std::shared_ptr< SDLPP::Renderer > renderer,
|
|
|
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
|
|
|
auto retPiece = std::make_shared< TetrisPiece >();
|
2020-08-22 23:15:46 +00:00
|
|
|
auto color = "#FF0000";
|
|
|
|
auto outline = "#AA0000";
|
2020-08-24 06:48:25 +00:00
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
|
|
|
outline, TETRIS_BRICK, renderer,
|
|
|
|
scene ),
|
|
|
|
0, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
|
|
|
TETRIS_BRICK, renderer, scene ),
|
|
|
|
0, 0 );
|
|
|
|
retPiece->addPiece(
|
|
|
|
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_BRICK, renderer, scene ),
|
2020-08-23 09:37:07 +00:00
|
|
|
0, 0 );
|
2020-08-24 06:48:25 +00:00
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_BRICK, renderer,
|
|
|
|
scene ),
|
|
|
|
0, 0 );
|
|
|
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
2020-08-22 23:15:46 +00:00
|
|
|
return retPiece;
|
|
|
|
}
|
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
std::shared_ptr< TetrisPiece >
|
|
|
|
tetrisT( std::shared_ptr< SDLPP::Renderer > renderer,
|
|
|
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
|
|
|
auto retPiece = std::make_shared< TetrisPiece >();
|
2020-08-22 23:15:46 +00:00
|
|
|
auto color = "#00FF00";
|
|
|
|
auto outline = "#00AA00";
|
2020-08-24 06:48:25 +00:00
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
|
|
|
TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_T, renderer, scene ),
|
|
|
|
-1, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_T, renderer, scene ),
|
|
|
|
0, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
|
|
|
TETRIS_T, renderer, scene ),
|
|
|
|
0, -1 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
|
|
|
|
TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_T, renderer, scene ),
|
|
|
|
1, 0 );
|
|
|
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
2020-08-22 23:15:46 +00:00
|
|
|
return retPiece;
|
|
|
|
}
|
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
std::shared_ptr< TetrisPiece >
|
|
|
|
tetrisLRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
|
|
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
|
|
|
auto retPiece = std::make_shared< TetrisPiece >();
|
2020-08-22 23:15:46 +00:00
|
|
|
auto color = "#0000FF";
|
|
|
|
auto outline = "#0000AA";
|
2020-08-24 06:48:25 +00:00
|
|
|
retPiece->addPiece(
|
|
|
|
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_L_RIGHT, renderer, scene ),
|
|
|
|
-2, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_L_RIGHT, renderer,
|
|
|
|
scene ),
|
|
|
|
-1, 0 );
|
|
|
|
retPiece->addPiece(
|
|
|
|
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_L_RIGHT, renderer, scene ),
|
|
|
|
0, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
|
|
|
outline, TETRIS_L_RIGHT, renderer,
|
|
|
|
scene ),
|
|
|
|
0, -1 );
|
|
|
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
2020-08-22 23:15:46 +00:00
|
|
|
return retPiece;
|
|
|
|
}
|
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
std::shared_ptr< TetrisPiece >
|
|
|
|
tetrisZRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
|
|
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
|
|
|
auto retPiece = std::make_shared< TetrisPiece >();
|
2020-08-22 23:15:46 +00:00
|
|
|
auto color = "#FF00FF";
|
|
|
|
auto outline = "#AA00AA";
|
2020-08-24 06:48:25 +00:00
|
|
|
retPiece->addPiece(
|
|
|
|
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_Z_RIGHT, renderer, scene ),
|
|
|
|
-1, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_Z_RIGHT, renderer,
|
|
|
|
scene ),
|
|
|
|
0, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
|
|
|
TETRIS_Z_RIGHT, renderer, scene ),
|
|
|
|
0, -1 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
|
|
|
outline, TETRIS_Z_RIGHT, renderer,
|
|
|
|
scene ),
|
|
|
|
1, -1 );
|
|
|
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
2020-08-22 23:15:46 +00:00
|
|
|
return retPiece;
|
|
|
|
}
|
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
std::shared_ptr< TetrisPiece >
|
|
|
|
tetrisLine( std::shared_ptr< SDLPP::Renderer > renderer,
|
|
|
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
|
|
|
auto retPiece = std::make_shared< TetrisPiece >();
|
2020-08-22 23:15:46 +00:00
|
|
|
auto color = "#FFFF00";
|
|
|
|
auto outline = "#AAAA00";
|
2020-08-24 06:48:25 +00:00
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5 - 2 * BLOCK_SIZE, TOP_BORDER,
|
|
|
|
color, outline, TETRIS_LINE,
|
|
|
|
renderer, scene ),
|
|
|
|
-1, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
|
|
|
outline, TETRIS_LINE, renderer,
|
|
|
|
scene ),
|
|
|
|
0, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
|
|
|
TETRIS_LINE, renderer, scene ),
|
|
|
|
1, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
|
|
|
outline, TETRIS_LINE, renderer,
|
|
|
|
scene ),
|
|
|
|
2, 0 );
|
|
|
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
2020-08-22 23:15:46 +00:00
|
|
|
return retPiece;
|
|
|
|
}
|
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
std::shared_ptr< TetrisPiece >
|
|
|
|
tetrisLLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
|
|
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
|
|
|
auto retPiece = std::make_shared< TetrisPiece >();
|
2020-08-22 23:15:46 +00:00
|
|
|
auto color = "#00FFFF";
|
|
|
|
auto outline = "#00AAAA";
|
2020-08-24 06:48:25 +00:00
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
|
|
|
outline, TETRIS_L_LEFT, renderer,
|
|
|
|
scene ),
|
|
|
|
0, -1 );
|
|
|
|
retPiece->addPiece(
|
|
|
|
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_L_LEFT, renderer, scene ),
|
|
|
|
0, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_L_LEFT, renderer,
|
|
|
|
scene ),
|
|
|
|
1, 0 );
|
|
|
|
retPiece->addPiece(
|
|
|
|
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_L_LEFT, renderer, scene ),
|
|
|
|
2, 0 );
|
|
|
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
2020-08-22 23:15:46 +00:00
|
|
|
return retPiece;
|
|
|
|
}
|
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
std::shared_ptr< TetrisPiece >
|
|
|
|
tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
|
|
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
|
|
|
auto retPiece = std::make_shared< TetrisPiece >();
|
2020-08-22 23:15:46 +00:00
|
|
|
auto color = "#FFFFFF";
|
|
|
|
auto outline = "#AAAAAA";
|
2020-08-24 06:48:25 +00:00
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
|
|
|
outline, TETRIS_Z_LEFT, renderer,
|
|
|
|
scene ),
|
|
|
|
-1, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
|
|
|
TETRIS_Z_LEFT, renderer, scene ),
|
|
|
|
0, 0 );
|
|
|
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_Z_LEFT, renderer,
|
|
|
|
scene ),
|
|
|
|
0, 1 );
|
|
|
|
retPiece->addPiece(
|
|
|
|
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
|
|
|
outline, TETRIS_Z_LEFT, renderer, scene ),
|
|
|
|
1, 1 );
|
|
|
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
2020-08-22 23:15:46 +00:00
|
|
|
return retPiece;
|
|
|
|
}
|
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
std::vector< std::shared_ptr< TetrisPiece > ( * )(
|
|
|
|
std::shared_ptr< SDLPP::Renderer >, std::shared_ptr< SDLPP::Scene > ) >
|
2020-08-23 07:50:05 +00:00
|
|
|
tetrisFunctions = {
|
|
|
|
tetrisBrick, tetrisT, tetrisLRight, tetrisZRight,
|
|
|
|
tetrisLine, tetrisLLeft, tetrisZLeft,
|
|
|
|
};
|
|
|
|
|
|
|
|
void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
|
|
|
|
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
|
|
|
|
"#101090FF", true );
|
2020-08-24 16:21:35 +00:00
|
|
|
bg->setPermanent();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( bg );
|
|
|
|
auto left_barrier = std::make_shared< SDLPP::RectangleRender >(
|
2020-08-23 09:37:07 +00:00
|
|
|
LEFT_BORDER - 0.02, 0, 0.02, BOTTOM_BORDER, r, "#FF000080", true );
|
2020-08-22 23:15:46 +00:00
|
|
|
left_barrier->centerX();
|
2020-08-24 16:21:35 +00:00
|
|
|
left_barrier->setStatic();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( left_barrier );
|
|
|
|
auto right_barrier = std::make_shared< SDLPP::RectangleRender >(
|
2020-08-23 09:37:07 +00:00
|
|
|
RIGHT_BORDER, 0, 0.02, BOTTOM_BORDER, r, "#FF000080", true );
|
2020-08-22 23:15:46 +00:00
|
|
|
right_barrier->centerX();
|
2020-08-24 16:21:35 +00:00
|
|
|
right_barrier->setStatic();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( right_barrier );
|
|
|
|
auto bottom_barrier = std::make_shared< SDLPP::RectangleRender >(
|
2020-08-23 09:37:07 +00:00
|
|
|
LEFT_BORDER - 0.02, BOTTOM_BORDER, RIGHT_BORDER - LEFT_BORDER + 0.04,
|
|
|
|
0.02, r, "#FF000080", true );
|
2020-08-22 23:15:46 +00:00
|
|
|
bottom_barrier->centerX();
|
2020-08-24 16:21:35 +00:00
|
|
|
bottom_barrier->setStatic();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( bottom_barrier );
|
|
|
|
auto tetris = std::make_shared< SDLPP::TextRenderer >(
|
|
|
|
0.4, 0, 0.2, 0.1, r, *font, "TETRIS", "FFFFFF", "000000", 5 );
|
2020-08-22 23:15:46 +00:00
|
|
|
tetris->centerX();
|
2020-08-24 16:21:35 +00:00
|
|
|
tetris->setStatic();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( tetris );
|
|
|
|
auto next = std::make_shared< SDLPP::TextRenderer >(
|
2020-08-23 09:37:07 +00:00
|
|
|
RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, *font, "NEXT", "FFFFFF",
|
|
|
|
"000000", 5, SDLPP_TEXT_CENTER );
|
2020-08-22 23:15:46 +00:00
|
|
|
next->centerX();
|
2020-08-24 16:21:35 +00:00
|
|
|
next->setStatic();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( next );
|
2020-08-22 23:15:46 +00:00
|
|
|
double posy = 1;
|
2020-08-23 09:37:07 +00:00
|
|
|
auto gameover = std::make_shared< SDLPP::RectangleRender >(
|
|
|
|
0.5, 0, 0, TOP_BORDER + BLOCK_SIZE, r );
|
|
|
|
auto gameover_collision = SDLPP::Rect( -1, 0, -1, 0.9 );
|
2020-08-22 23:15:46 +00:00
|
|
|
gameover_collision.setInfinite();
|
2020-08-23 07:50:05 +00:00
|
|
|
gameover->addCollision( gameover_collision );
|
|
|
|
gameover->setId( GAME_OVER );
|
|
|
|
gameover->setColiderColor( "FF0000" );
|
2020-08-24 16:21:35 +00:00
|
|
|
gameover->setStatic();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( gameover );
|
|
|
|
auto score_text = std::make_shared< SDLPP::TextRenderer >(
|
2020-08-23 09:37:07 +00:00
|
|
|
RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, *font, "SCORE", "#FFFFFF",
|
|
|
|
"#000000", 5, SDLPP_TEXT_CENTER );
|
2020-08-22 23:15:46 +00:00
|
|
|
score_text->centerX();
|
2020-08-24 16:21:35 +00:00
|
|
|
score_text->setStatic();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( score_text );
|
2020-08-22 23:15:46 +00:00
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
score_texture = std::make_shared< SDLPP::TextRenderer >(
|
2020-08-23 09:37:07 +00:00
|
|
|
RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, *font, std::to_string( score ),
|
|
|
|
"FFFFFF", "000000", 5, SDLPP_TEXT_TOP );
|
2020-08-22 23:15:46 +00:00
|
|
|
score_texture->centerX();
|
2020-08-24 16:21:35 +00:00
|
|
|
score_texture->setStatic();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( score_texture );
|
|
|
|
for ( int i = 0; i < 20; i++ ) {
|
2020-08-23 09:37:07 +00:00
|
|
|
posy -= BLOCK_SIZE;
|
2020-08-23 07:50:05 +00:00
|
|
|
auto colider = std::make_shared< SDLPP::RectangleRender >(
|
2020-08-23 09:37:07 +00:00
|
|
|
LEFT_BORDER, posy, BLOCK_SIZE, BLOCK_SIZE, r );
|
2020-08-23 07:50:05 +00:00
|
|
|
auto colider_colider = SDLPP::Rect( -1, 0.1, -1, 0.8 );
|
2020-08-22 23:15:46 +00:00
|
|
|
colider_colider.setInfinite();
|
2020-08-23 07:50:05 +00:00
|
|
|
colider->addCollision( colider_colider );
|
|
|
|
colider->setId( COLIDER_ID );
|
2020-08-24 16:21:35 +00:00
|
|
|
colider->setStatic();
|
2020-08-23 07:50:05 +00:00
|
|
|
line_coliders.push_back( colider );
|
|
|
|
scene.addObject( colider );
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateScore() {
|
2020-08-23 07:50:05 +00:00
|
|
|
score_texture->setText( *font, std::to_string( score ), "#FFFFFF",
|
|
|
|
"#000000", 5 );
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
void addPause( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
|
|
|
|
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
|
|
|
|
"#00000080", true );
|
|
|
|
bg->setId( 123 );
|
|
|
|
bg->setPermanent( true );
|
|
|
|
scene.addObject( bg );
|
|
|
|
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
|
|
|
|
y->setText( *font, "PAUSED", "#FFFFFF", "#000000", 5 );
|
|
|
|
y->setId( 0 );
|
2020-08-22 23:15:46 +00:00
|
|
|
y->centerX();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( y );
|
|
|
|
auto resume =
|
2020-08-24 16:21:35 +00:00
|
|
|
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.51, 0.2, 0.08, r );
|
2020-08-23 07:50:05 +00:00
|
|
|
resume->setText( *font, "Resume", "#FFFFFF", "#000000", 5 );
|
|
|
|
resume->setColor( "#FFFFFF40" );
|
2020-08-22 23:15:46 +00:00
|
|
|
resume->centerX();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( resume );
|
|
|
|
pause_options.push_back( resume );
|
2020-08-24 16:21:35 +00:00
|
|
|
auto restart =
|
|
|
|
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.61, 0.2, 0.08, r );
|
|
|
|
restart->setText( *font, "Restart", "#FFFFFF", "#000000", 5 );
|
|
|
|
restart->centerX();
|
|
|
|
scene.addObject( restart );
|
|
|
|
pause_options.push_back( restart );
|
2020-08-23 07:50:05 +00:00
|
|
|
auto quit =
|
2020-08-24 16:21:35 +00:00
|
|
|
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.71, 0.2, 0.08, r );
|
2020-08-23 07:50:05 +00:00
|
|
|
quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 );
|
2020-08-22 23:15:46 +00:00
|
|
|
quit->centerX();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( quit );
|
|
|
|
pause_options.push_back( quit );
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 16:44:42 +00:00
|
|
|
void addGameOver( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
|
|
|
|
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
|
|
|
|
"#00000080", true );
|
|
|
|
bg->setId( 123 );
|
|
|
|
bg->setPermanent( true );
|
|
|
|
scene.addObject( bg );
|
|
|
|
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
|
|
|
|
y->setText( *font, "GAME OVER", "#FFFFFF", "#000000", 5 );
|
|
|
|
y->setId( 0 );
|
|
|
|
y->centerX();
|
|
|
|
scene.addObject( y );
|
|
|
|
auto restart =
|
|
|
|
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 1, r );
|
|
|
|
restart->setText( *font, "Restart", "#FFFFFF", "#000000", 5 );
|
|
|
|
restart->centerX();
|
|
|
|
restart->setColor( "#FFFFFF40" );
|
|
|
|
scene.addObject( restart );
|
|
|
|
game_over_options.push_back( restart );
|
|
|
|
auto quit =
|
|
|
|
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 1, r );
|
|
|
|
quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 );
|
|
|
|
quit->centerX();
|
|
|
|
scene.addObject( quit );
|
|
|
|
game_over_options.push_back( quit );
|
|
|
|
}
|
|
|
|
|
2020-08-22 23:15:46 +00:00
|
|
|
void quitGame() {
|
|
|
|
std::cout << "Quitting!" << std::endl;
|
|
|
|
quit = true;
|
|
|
|
}
|
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
void checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene ) {
|
2020-08-23 09:37:07 +00:00
|
|
|
bool crash = true;
|
|
|
|
int left = 0x01;
|
|
|
|
int right = 0x02;
|
|
|
|
int bottom = 0x04;
|
|
|
|
int flags = 0;
|
2020-08-24 14:36:24 +00:00
|
|
|
// game board limits
|
2020-08-23 09:37:07 +00:00
|
|
|
while ( crash ) {
|
|
|
|
crash = false;
|
|
|
|
flags = 0;
|
2020-08-24 06:48:25 +00:00
|
|
|
for ( auto &block : piece->getObjects() ) {
|
2020-08-23 09:37:07 +00:00
|
|
|
auto pos = block->getPos();
|
|
|
|
if ( pos.first < LEFT_BORDER - 0.01 ) {
|
2020-08-24 15:44:34 +00:00
|
|
|
flags |= left;
|
2020-08-23 09:37:07 +00:00
|
|
|
crash = true;
|
|
|
|
break;
|
|
|
|
} else if ( pos.first > RIGHT_BORDER - BLOCK_SIZE + 0.01 ) {
|
|
|
|
crash = true;
|
2020-08-24 15:44:34 +00:00
|
|
|
flags |= right;
|
2020-08-23 09:37:07 +00:00
|
|
|
break;
|
|
|
|
} else if ( pos.second >= BOTTOM_BORDER ) {
|
|
|
|
crash = true;
|
2020-08-24 15:44:34 +00:00
|
|
|
flags |= bottom;
|
2020-08-23 09:37:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( crash ) {
|
2020-08-24 15:44:34 +00:00
|
|
|
if ( flags & bottom || (flags & left && flags & right) ) {
|
2020-08-24 06:48:25 +00:00
|
|
|
piece->revert();
|
2020-08-23 09:37:07 +00:00
|
|
|
} else {
|
2020-08-24 15:44:34 +00:00
|
|
|
if( flags & left )
|
2020-08-24 14:36:24 +00:00
|
|
|
piece->movePiece(BLOCK_SIZE, 0);
|
2020-08-24 15:44:34 +00:00
|
|
|
else if( flags & right )
|
2020-08-24 14:36:24 +00:00
|
|
|
piece->movePiece(-BLOCK_SIZE, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// blocks
|
|
|
|
crash = true;
|
|
|
|
bool was_left = false;
|
|
|
|
bool was_right = false;
|
|
|
|
bool is_left = false;
|
|
|
|
bool is_right = false;
|
|
|
|
while ( crash ) {
|
|
|
|
is_left = false;
|
|
|
|
is_right = false;
|
|
|
|
crash = false;
|
|
|
|
for(auto &block : piece->getObjects()) {
|
|
|
|
auto collisions = scene.getCollisions(*block, {BRICK_ID});
|
|
|
|
if(collisions.size() == 1)
|
|
|
|
continue;
|
|
|
|
for(auto &col : collisions) {
|
|
|
|
crash = true;
|
|
|
|
is_left |= piece->isLeft(*col);
|
|
|
|
is_right |= piece->isRight(*col);
|
2020-08-23 09:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-24 14:36:24 +00:00
|
|
|
if(!crash)
|
|
|
|
break;
|
|
|
|
if((is_left && is_right) || (is_left && was_right) || (is_right && was_left)) {
|
|
|
|
piece->revert();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
was_left = is_left;
|
|
|
|
was_right = is_right;
|
|
|
|
if(is_left)
|
|
|
|
piece->movePiece(BLOCK_SIZE, 0);
|
|
|
|
if(is_right)
|
|
|
|
piece->movePiece(-BLOCK_SIZE, 0);
|
|
|
|
// either bottom or up
|
|
|
|
if(!is_left && !is_right) {
|
|
|
|
piece->revert();
|
|
|
|
break;
|
|
|
|
}
|
2020-08-23 09:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
bool crash = false;
|
2020-08-23 07:50:05 +00:00
|
|
|
switch ( key ) {
|
2020-08-24 15:44:34 +00:00
|
|
|
case SDLK_ESCAPE:
|
|
|
|
{
|
2020-08-24 16:44:42 +00:00
|
|
|
pause = PAUSE_PAUSE;
|
2020-08-23 07:50:05 +00:00
|
|
|
pause_scene->updateSizeAndPosition();
|
|
|
|
std::thread pauseThread( doInputPause );
|
|
|
|
pauseThread.detach();
|
|
|
|
} break;
|
|
|
|
case SDLK_LEFT:
|
|
|
|
case SDLK_a:
|
2020-08-24 06:48:25 +00:00
|
|
|
if(!cur_object)
|
|
|
|
break;
|
2020-08-24 14:36:24 +00:00
|
|
|
cur_object->movePiece(-BLOCK_SIZE, 0);
|
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
for ( auto &x : cur_object->getObjects() ) {
|
2020-08-23 07:50:05 +00:00
|
|
|
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
|
2020-08-24 14:36:24 +00:00
|
|
|
auto pos = x->getPos();
|
|
|
|
if ( collisions.size() > 1 || pos.first < ( LEFT_BORDER - 0.01 ) )
|
2020-08-23 07:50:05 +00:00
|
|
|
crash = true;
|
|
|
|
}
|
2020-08-24 14:36:24 +00:00
|
|
|
if ( crash )
|
|
|
|
cur_object->movePiece(BLOCK_SIZE, 0);
|
2020-08-23 07:50:05 +00:00
|
|
|
break;
|
|
|
|
case SDLK_RIGHT:
|
|
|
|
case SDLK_d:
|
2020-08-24 06:48:25 +00:00
|
|
|
if(!cur_object)
|
|
|
|
break;
|
2020-08-24 14:36:24 +00:00
|
|
|
cur_object->movePiece(BLOCK_SIZE, 0);
|
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
for ( auto &x : cur_object->getObjects() ) {
|
2020-08-23 07:50:05 +00:00
|
|
|
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
|
2020-08-24 14:36:24 +00:00
|
|
|
auto pos = x->getPos();
|
|
|
|
if ( collisions.size() > 1 || pos.first > RIGHT_BORDER - BLOCK_SIZE + 0.01 ) {
|
2020-08-23 07:50:05 +00:00
|
|
|
crash = true;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
}
|
2020-08-24 14:36:24 +00:00
|
|
|
if ( crash )
|
|
|
|
cur_object->movePiece(-BLOCK_SIZE, 0);
|
2020-08-23 07:50:05 +00:00
|
|
|
break;
|
|
|
|
case SDLK_DOWN:
|
|
|
|
case SDLK_s:
|
2020-08-24 06:48:25 +00:00
|
|
|
if(!cur_object)
|
|
|
|
break;
|
|
|
|
cur_object->startDescend();
|
2020-08-23 07:50:05 +00:00
|
|
|
break;
|
|
|
|
case SDLK_UP:
|
|
|
|
case SDLK_w:
|
2020-08-24 06:48:25 +00:00
|
|
|
if(!cur_object)
|
|
|
|
break;
|
|
|
|
cur_object->rotate();
|
2020-08-23 09:37:07 +00:00
|
|
|
checkRotation( cur_object, scene );
|
2020-08-23 07:50:05 +00:00
|
|
|
break;
|
|
|
|
case SDLK_r:
|
|
|
|
scene.getRenderer().setRenderColiders(
|
|
|
|
!scene.getRenderer().getRenderColiders() );
|
|
|
|
default:
|
|
|
|
break;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
void handleKeyUp( SDL_Keycode key ) {
|
|
|
|
if ( key == SDLK_DOWN || key == SDLK_s ) {
|
2020-08-24 15:44:34 +00:00
|
|
|
if(cur_object)
|
|
|
|
cur_object->stopDescend();
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
void handleKeyDownPause( SDL_Keycode key ) {
|
|
|
|
switch ( key ) {
|
|
|
|
case SDLK_ESCAPE: {
|
2020-08-24 16:44:42 +00:00
|
|
|
pause = 0;
|
2020-08-23 07:50:05 +00:00
|
|
|
active_scene->setPrevTicks( SDL_GetTicks() );
|
|
|
|
std::thread inputThread( doInput, active_scene );
|
|
|
|
inputThread.detach();
|
|
|
|
} break;
|
|
|
|
case SDLK_r:
|
|
|
|
active_scene->getRenderer().setRenderColiders(
|
|
|
|
!active_scene->getRenderer().getRenderColiders() );
|
|
|
|
break;
|
|
|
|
case SDLK_s:
|
|
|
|
case SDLK_DOWN:
|
|
|
|
pause_options[pause_select]->unsetColor();
|
|
|
|
pause_select++;
|
|
|
|
if ( pause_select > pause_max )
|
|
|
|
pause_select = 0;
|
|
|
|
pause_options[pause_select]->setColor( "FFFFFF40" );
|
|
|
|
break;
|
|
|
|
case SDLK_w:
|
|
|
|
case SDLK_UP:
|
|
|
|
pause_options[pause_select]->unsetColor();
|
|
|
|
pause_select--;
|
|
|
|
if ( pause_select < 0 )
|
|
|
|
pause_select = pause_max;
|
|
|
|
pause_options[pause_select]->setColor( "FFFFFF40" );
|
|
|
|
break;
|
|
|
|
case SDLK_RETURN:
|
|
|
|
switch ( pause_select ) {
|
|
|
|
case 0: {
|
2020-08-24 16:44:42 +00:00
|
|
|
pause = 0;
|
2020-08-23 07:50:05 +00:00
|
|
|
active_scene->setPrevTicks( SDL_GetTicks() );
|
|
|
|
std::thread inputThread( doInput, active_scene );
|
|
|
|
inputThread.detach();
|
|
|
|
} break;
|
|
|
|
case 1:
|
2020-08-24 16:21:35 +00:00
|
|
|
{
|
2020-08-24 16:44:42 +00:00
|
|
|
pause = 0;
|
2020-08-24 16:21:35 +00:00
|
|
|
cur_object.reset();
|
|
|
|
checked_line = true;
|
|
|
|
next_object.reset();
|
2020-08-24 16:44:42 +00:00
|
|
|
score = 0;
|
|
|
|
update_score = true;
|
|
|
|
active_scene->resetScene();
|
|
|
|
active_scene->setPrevTicks( SDL_GetTicks() );
|
|
|
|
|
|
|
|
next_object = tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
|
|
|
active_scene->getRendererShared(), active_scene );
|
|
|
|
next_object->setPos( 0.9, 0.5 );
|
|
|
|
std::thread inputThread( doInput, active_scene );
|
|
|
|
inputThread.detach();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
quitGame();
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleKeyDownGameOver( SDL_Keycode key ) {
|
|
|
|
switch ( key ) {
|
|
|
|
case SDLK_r:
|
|
|
|
active_scene->getRenderer().setRenderColiders(
|
|
|
|
!active_scene->getRenderer().getRenderColiders() );
|
|
|
|
break;
|
|
|
|
case SDLK_s:
|
|
|
|
case SDLK_DOWN:
|
|
|
|
game_over_options[game_over_select]->unsetColor();
|
|
|
|
game_over_select++;
|
|
|
|
if ( game_over_select > game_over_max )
|
|
|
|
game_over_select = 0;
|
|
|
|
game_over_options[game_over_select]->setColor( "FFFFFF40" );
|
|
|
|
break;
|
|
|
|
case SDLK_w:
|
|
|
|
case SDLK_UP:
|
|
|
|
game_over_options[game_over_select]->unsetColor();
|
|
|
|
game_over_select--;
|
|
|
|
if ( game_over_select < 0 )
|
|
|
|
game_over_select = game_over_max;
|
|
|
|
game_over_options[game_over_select]->setColor( "FFFFFF40" );
|
|
|
|
break;
|
|
|
|
case SDLK_RETURN:
|
|
|
|
switch ( game_over_select ) {
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
// TODO reset function
|
|
|
|
pause = 0;
|
|
|
|
cur_object.reset();
|
|
|
|
checked_line = true;
|
|
|
|
next_object.reset();
|
|
|
|
score = 0;
|
|
|
|
update_score = true;
|
2020-08-24 16:21:35 +00:00
|
|
|
active_scene->resetScene();
|
|
|
|
active_scene->setPrevTicks( SDL_GetTicks() );
|
|
|
|
|
|
|
|
next_object = tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
|
|
|
active_scene->getRendererShared(), active_scene );
|
|
|
|
next_object->setPos( 0.9, 0.5 );
|
|
|
|
std::thread inputThread( doInput, active_scene );
|
|
|
|
inputThread.detach();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
2020-08-23 07:50:05 +00:00
|
|
|
quitGame();
|
2020-08-22 23:15:46 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-08-23 07:50:05 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
void pollEvents( SDLPP::Scene &scene ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
SDL_Event event;
|
2020-08-23 07:50:05 +00:00
|
|
|
while ( SDL_PollEvent( &event ) != 0 ) {
|
|
|
|
switch ( event.type ) {
|
|
|
|
case SDL_QUIT:
|
|
|
|
quitGame();
|
|
|
|
break;
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
if ( !event.key.repeat )
|
|
|
|
handleKeyDown( event.key.keysym.sym, scene );
|
|
|
|
break;
|
|
|
|
case SDL_KEYUP:
|
|
|
|
handleKeyUp( event.key.keysym.sym );
|
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
if ( event.window.event == SDL_WINDOWEVENT_RESIZED )
|
|
|
|
scene.updateSizeAndPosition();
|
|
|
|
default:
|
|
|
|
break;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pollEventsPause() {
|
|
|
|
SDL_Event event;
|
2020-08-23 07:50:05 +00:00
|
|
|
while ( SDL_PollEvent( &event ) != 0 ) {
|
|
|
|
switch ( event.type ) {
|
|
|
|
case SDL_QUIT:
|
|
|
|
quitGame();
|
|
|
|
break;
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
if ( !event.key.repeat )
|
|
|
|
handleKeyDownPause( event.key.keysym.sym );
|
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
|
|
|
active_scene->updateSizeAndPosition();
|
|
|
|
pause_scene->updateSizeAndPosition();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 16:44:42 +00:00
|
|
|
void pollEventsGameOver() {
|
|
|
|
SDL_Event event;
|
|
|
|
while ( SDL_PollEvent( &event ) != 0 ) {
|
|
|
|
switch ( event.type ) {
|
|
|
|
case SDL_QUIT:
|
|
|
|
quitGame();
|
|
|
|
break;
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
if ( !event.key.repeat )
|
|
|
|
handleKeyDownGameOver( event.key.keysym.sym );
|
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
|
|
|
active_scene->updateSizeAndPosition();
|
|
|
|
game_over_scene->updateSizeAndPosition();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
ticks_till_fall -= ticks;
|
2020-08-24 06:48:25 +00:00
|
|
|
if ( cur_object->isDescending() )
|
2020-08-22 23:15:46 +00:00
|
|
|
ticks_till_descend -= ticks;
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( ticks_till_fall > 0 ) {
|
2020-08-24 06:48:25 +00:00
|
|
|
if ( cur_object->isDescending() && ticks_till_descend <= 0 ) {
|
2020-08-23 13:30:45 +00:00
|
|
|
ticks_till_descend = TICKS_TILL_DESCEND;
|
2020-08-22 23:15:46 +00:00
|
|
|
goto fall;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-08-23 13:30:45 +00:00
|
|
|
ticks_till_fall = TICKS_TILL_FALL;
|
2020-08-22 23:15:46 +00:00
|
|
|
fall:
|
2020-08-24 14:36:24 +00:00
|
|
|
cur_object->movePiece(0, BLOCK_SIZE);
|
2020-08-22 23:15:46 +00:00
|
|
|
bool fell = false;
|
2020-08-24 06:48:25 +00:00
|
|
|
for ( auto &x : cur_object->getObjects() ) {
|
2020-08-23 07:50:05 +00:00
|
|
|
auto collisions = scene->getCollisions( *x, { BRICK_ID } );
|
|
|
|
if ( collisions.size() > 1 ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
fell = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( x->getPos().second >= 1 ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
fell = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( fell ) {
|
2020-08-24 14:36:24 +00:00
|
|
|
cur_object->movePiece(0, -BLOCK_SIZE);
|
2020-08-24 06:48:25 +00:00
|
|
|
for ( auto &block : cur_object->getObjects() ) {
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
|
2020-08-24 16:44:42 +00:00
|
|
|
pause = PAUSE_GAME_OVER;
|
|
|
|
game_over_scene->updateSizeAndPosition();
|
|
|
|
std::thread pauseThread( doInputGameOver );
|
|
|
|
pauseThread.detach();
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-24 15:44:34 +00:00
|
|
|
cur_object.reset();
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
FPSmanager gFPS;
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_initFramerate( &gFPS );
|
|
|
|
SDL_setFramerate( &gFPS, 200 );
|
2020-08-22 23:15:46 +00:00
|
|
|
auto base = SDL_GetTicks();
|
2020-08-23 07:50:05 +00:00
|
|
|
while ( !quit && !pause ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
base = SDL_GetTicks();
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_framerateDelay( &gFPS );
|
2020-08-24 15:44:34 +00:00
|
|
|
std::lock_guard< std::mutex > guard( movement_mutex );
|
2020-08-23 07:50:05 +00:00
|
|
|
pollEvents( *scene );
|
2020-08-24 15:44:34 +00:00
|
|
|
if ( cur_object ) {
|
2020-08-23 07:50:05 +00:00
|
|
|
moveThem( scene, SDL_GetTicks() - base );
|
2020-08-22 23:15:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
for ( auto &colider : line_coliders ) {
|
|
|
|
auto collisions = scene->getCollisions( *colider, { BRICK_ID } );
|
|
|
|
while ( collisions.size() == 10 ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
score += 10;
|
|
|
|
update_score = true;
|
2020-08-23 07:50:05 +00:00
|
|
|
for ( auto &col : collisions ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
col->destroy();
|
|
|
|
}
|
|
|
|
auto colider_y = colider->getPos().second;
|
2020-08-23 07:50:05 +00:00
|
|
|
for ( auto &elem : scene->getObjects() ) {
|
|
|
|
if ( elem->getId() != BRICK_ID )
|
2020-08-22 23:15:46 +00:00
|
|
|
continue;
|
|
|
|
auto pos = elem->getPos();
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( pos.second < colider_y ) {
|
2020-08-23 09:37:07 +00:00
|
|
|
elem->setPos( pos.first, pos.second + BLOCK_SIZE );
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
using namespace std::chrono_literals;
|
2020-08-23 13:30:45 +00:00
|
|
|
wait_for_anim = true;
|
2020-08-24 06:48:25 +00:00
|
|
|
while ( wait_for_anim ) {
|
|
|
|
std::this_thread::sleep_for( 0.1s );
|
2020-08-23 13:30:45 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
collisions = scene->getCollisions( *colider, { BRICK_ID } );
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-23 13:30:45 +00:00
|
|
|
checked_line = true;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void doInputPause() {
|
|
|
|
FPSmanager gFPS;
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_initFramerate( &gFPS );
|
|
|
|
SDL_setFramerate( &gFPS, 200 );
|
|
|
|
while ( pause ) {
|
|
|
|
SDL_framerateDelay( &gFPS );
|
2020-08-22 23:15:46 +00:00
|
|
|
pollEventsPause();
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( !pause )
|
2020-08-22 23:15:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 16:44:42 +00:00
|
|
|
void doInputGameOver() {
|
|
|
|
FPSmanager gFPS;
|
|
|
|
SDL_initFramerate( &gFPS );
|
|
|
|
SDL_setFramerate( &gFPS, 200 );
|
|
|
|
while ( pause ) {
|
|
|
|
SDL_framerateDelay( &gFPS );
|
|
|
|
pollEventsGameOver();
|
|
|
|
if ( !pause )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 23:15:46 +00:00
|
|
|
int main() {
|
|
|
|
SDLPP::init();
|
2020-08-23 07:50:05 +00:00
|
|
|
SDLPP::Window w( "Tetris clone!" );
|
|
|
|
auto renderer = std::make_shared< SDLPP::Renderer >( w );
|
2020-08-22 23:15:46 +00:00
|
|
|
active_renderer = renderer;
|
2020-08-23 07:50:05 +00:00
|
|
|
renderer->setBlendMode( SDL_BLENDMODE_BLEND );
|
|
|
|
auto main_scene = std::make_shared< SDLPP::Scene >( renderer );
|
2020-08-22 23:15:46 +00:00
|
|
|
active_scene = main_scene;
|
2020-08-23 07:50:05 +00:00
|
|
|
font = std::make_shared< SDLPP::Font >( "testfont.ttf", 96 );
|
|
|
|
addStuff( *main_scene, renderer );
|
2020-08-24 16:21:35 +00:00
|
|
|
main_scene->saveScene();
|
2020-08-22 23:15:46 +00:00
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
pause_scene = std::make_shared< SDLPP::Scene >( renderer );
|
|
|
|
addPause( *pause_scene, renderer );
|
2020-08-24 16:44:42 +00:00
|
|
|
game_over_scene = std::make_shared<SDLPP::Scene>(renderer);
|
|
|
|
addGameOver(*game_over_scene, renderer);
|
2020-08-22 23:15:46 +00:00
|
|
|
|
2020-08-23 13:30:45 +00:00
|
|
|
auto base = SDL_GetTicks();
|
2020-08-22 23:15:46 +00:00
|
|
|
int frames = 0;
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
std::srand( std::time( nullptr ) );
|
2020-08-22 23:15:46 +00:00
|
|
|
FPSmanager gFPS;
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_initFramerate( &gFPS );
|
|
|
|
SDL_setFramerate( &gFPS, 60 );
|
|
|
|
std::thread inputThread( doInput, main_scene );
|
2020-08-22 23:15:46 +00:00
|
|
|
inputThread.detach();
|
2020-08-23 07:50:05 +00:00
|
|
|
next_object = tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
|
|
|
renderer, main_scene );
|
2020-08-24 06:48:25 +00:00
|
|
|
next_object->setPos( 0.9, 0.5 );
|
2020-08-23 07:50:05 +00:00
|
|
|
while ( !quit ) {
|
2020-08-24 06:48:25 +00:00
|
|
|
SDL_framerateDelay( &gFPS );
|
2020-08-24 15:44:34 +00:00
|
|
|
if ( !cur_object && checked_line ) {
|
2020-08-23 13:30:45 +00:00
|
|
|
std::lock_guard< std::mutex > guard( movement_mutex );
|
|
|
|
cur_object = next_object;
|
2020-08-24 06:48:25 +00:00
|
|
|
cur_object->setPos( 0.5, TOP_BORDER );
|
2020-08-23 13:49:38 +00:00
|
|
|
auto rand_index = std::rand() / ( ( RAND_MAX + 1u ) / 7 );
|
|
|
|
int retries = 0;
|
2020-08-24 06:48:25 +00:00
|
|
|
while ( bag[rand_index] < 4 ) {
|
|
|
|
rand_index = ( rand_index + 1 ) % 7;
|
2020-08-23 13:49:38 +00:00
|
|
|
retries++;
|
2020-08-24 06:48:25 +00:00
|
|
|
if ( retries == 7 )
|
2020-08-23 13:49:38 +00:00
|
|
|
quitGame();
|
|
|
|
}
|
2020-08-24 06:48:25 +00:00
|
|
|
next_object.reset();
|
|
|
|
next_object = tetrisFunctions[rand_index]( renderer, main_scene );
|
|
|
|
next_object->setPos( 0.9, 0.5 );
|
2020-08-23 13:30:45 +00:00
|
|
|
checked_line = false;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( update_score ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
updateScore();
|
|
|
|
update_score = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
main_scene->renderScene();
|
2020-08-24 16:44:42 +00:00
|
|
|
if ( pause == PAUSE_PAUSE ) {
|
2020-08-23 07:50:05 +00:00
|
|
|
pause_scene->renderScene( false );
|
2020-08-24 16:44:42 +00:00
|
|
|
} else if ( pause == PAUSE_GAME_OVER ) {
|
|
|
|
game_over_scene->renderScene( false );
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
main_scene->presentScene();
|
2020-08-23 13:30:45 +00:00
|
|
|
wait_for_anim = false;
|
2020-08-22 23:15:46 +00:00
|
|
|
frames++;
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( SDL_GetTicks() - base >= 1000 ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
base = SDL_GetTicks();
|
2020-08-23 07:49:04 +00:00
|
|
|
std::cout << "FPS: " << frames << std::endl;
|
2020-08-22 23:15:46 +00:00
|
|
|
frames = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-08-24 06:48:25 +00:00
|
|
|
return 0;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|