TETRIS: use new sdlpp library

This commit is contained in:
zvon 2020-11-21 20:58:52 +01:00
parent a67e9e5b1a
commit 90879a3f21
11 changed files with 635 additions and 507 deletions

View File

@ -6,14 +6,12 @@ LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread
.PHONY: default
default: tetris
tetris: tetris.o sdlpp.o scenes.o config.o functions.o global_vars.o
$(CXX) $(CFLAGS) -o $@ $^ ${LDFLAGS}
tetris: tetris.o scenes.o config.o functions.o global_vars.o libsdlpp.so
$(CXX) $(CFLAGS) -o $@ $^ ${LDFLAGS} -L -llibsdlpp
sdlpp.o: ../sdlpp.cpp ../sdlpp.hpp
tetris.o: tetris.cpp ../sdlpp/sdlpp.hpp config.hpp custom_classes.hpp scenes.hpp global_vars.hpp functions.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
tetris.o: tetris.cpp ../sdlpp.hpp config.hpp custom_classes.hpp scenes.hpp global_vars.hpp functions.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
scenes.o: scenes.cpp ../sdlpp.hpp config.hpp scenes.hpp functions.hpp global_vars.hpp
scenes.o: scenes.cpp ../sdlpp/sdlpp.hpp config.hpp scenes.hpp functions.hpp global_vars.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
config.o: config.cpp config.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
@ -21,6 +19,13 @@ functions.o: functions.cpp config.hpp functions.hpp global_vars.hpp scenes.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
global_vars.o: global_vars.cpp config.hpp global_vars.hpp functions.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
libsdlpp.so: ../sdlpp
$(MAKE) -C ../sdlpp
cp ../sdlpp/libsdlpp.so .
ln -sf libsdlpp.so libsdlpp.so.1
start:
LD_LIBRARY_PATH=$$(pwd) ./tetris
clean:
rm -Rf *.o tetris

View File

@ -1,108 +1,114 @@
#include "config.hpp"
std::map<std::string, std::map<std::string, std::string>> color_schemes = {
{ "default", {
{"piece_brick", "#FF0000"},
{"piece_brick_out", "#AA0000"},
{"piece_T", "#00FF00"},
{"piece_T_out", "#00AA00"},
{"piece_L_right", "#0000FF"},
{"piece_L_right_out", "#0000AA"},
{"piece_Z_right", "#FF00FF"},
{"piece_Z_right_out", "#AA00AA"},
{"piece_line", "#FFFF00"},
{"piece_line_out", "#AAAA00"},
{"piece_L_left", "#00FFFF"},
{"piece_L_left_out", "#00AAAA"},
{"piece_Z_left", "#FFFFFF"},
{"piece_Z_left_out", "#AAAAAA"},
{"shadow", "#AAAAAAAA"},
{"shadow_out", "#888888"},
{"background", "#222222"},
{"line", "#888888"},
{"barrier", "#AA0000"},
{"text", "#FFFFFF"},
{"text_out", "#000000"},
{"menu_background", "#000000BB"},
{"menu_item_background", "#FFFFFF40"},
}},
{ "gruvbox_dark", {
{"piece_brick", "#cc241d"},
{"piece_brick_out", "#fb4934"},
{"piece_T", "#98971a"},
{"piece_T_out", "#b8bb26"},
{"piece_L_right", "#458588"},
{"piece_L_right_out", "#83a598"},
{"piece_Z_right", "#b16286"},
{"piece_Z_right_out", "#d3869b"},
{"piece_line", "#d79921"},
{"piece_line_out", "#fabd2f"},
{"piece_L_left", "#689d6a"},
{"piece_L_left_out", "#8ec07c"},
{"piece_Z_left", "#a89984"},
{"piece_Z_left_out", "#ebdbb2"},
{"shadow", "#bdae9380"},
{"shadow_out", "#fbf1c7"},
{"background", "#282828"},
{"line", "#fbf1c7"},
{"barrier", "#d65d0e"},
{"text", "#ebdbb2"},
{"text_out", "#1d2021"},
{"menu_background", "#282828BB"},
{"menu_item_background", "#d5c4a180"},
}},
{ "gruvbox_light", {
{"piece_brick", "#cc241d"},
{"piece_brick_out", "#9d0006"},
{"piece_T", "#98971a"},
{"piece_T_out", "#79740e"},
{"piece_L_right", "#458588"},
{"piece_L_right_out", "#076678"},
{"piece_Z_right", "#b16286"},
{"piece_Z_right_out", "#8f3f71"},
{"piece_line", "#d79921"},
{"piece_line_out", "#b57614"},
{"piece_L_left", "#689d6a"},
{"piece_L_left_out", "#427b58"},
{"piece_Z_left", "#7c6f64"},
{"piece_Z_left_out", "#3c3836"},
{"shadow", "#665c5480"},
{"shadow_out", "#282828"},
{"background", "#d5c4a1"},
{"line", "#282828"},
{"barrier", "#d65d0e"},
{"text", "#3c3836"},
{"text_out", "#f9f5d7"},
{"menu_background", "#d5c4a1BB"},
{"menu_item_background", "#50494580"},
}},
{ "blackandwhite", {
{"piece_brick", "#FFFFFF"},
{"piece_brick_out", "#000000"},
{"piece_T", "#FFFFFF"},
{"piece_T_out", "#000000"},
{"piece_L_right", "#FFFFFF"},
{"piece_L_right_out", "#000000"},
{"piece_Z_right", "#FFFFFF"},
{"piece_Z_right_out", "#000000"},
{"piece_line", "#FFFFFF"},
{"piece_line_out", "#000000"},
{"piece_L_left", "#FFFFFF"},
{"piece_L_left_out", "#000000"},
{"piece_Z_left", "#FFFFFF"},
{"piece_Z_left_out", "#000000"},
{"shadow", "#FFFFFF80"},
{"shadow_out", "#FFFFFF"},
{"background", "#000000"},
{"line", "#FFFFFF"},
{"barrier", "#FFFFFF"},
{"text", "#FFFFFF"},
{"text_out", "#000000"},
{"menu_background", "#000000BB"},
{"menu_item_background", "#FFFFFF40"},
}},
std::map< std::string, std::map< std::string, std::string > > color_schemes = {
{ "default",
{
{ "piece_brick", "#FF0000" },
{ "piece_brick_out", "#AA0000" },
{ "piece_T", "#00FF00" },
{ "piece_T_out", "#00AA00" },
{ "piece_L_right", "#0000FF" },
{ "piece_L_right_out", "#0000AA" },
{ "piece_Z_right", "#FF00FF" },
{ "piece_Z_right_out", "#AA00AA" },
{ "piece_line", "#FFFF00" },
{ "piece_line_out", "#AAAA00" },
{ "piece_L_left", "#00FFFF" },
{ "piece_L_left_out", "#00AAAA" },
{ "piece_Z_left", "#FFFFFF" },
{ "piece_Z_left_out", "#AAAAAA" },
{ "shadow", "#AAAAAAAA" },
{ "shadow_out", "#888888" },
{ "background", "#222222" },
{ "line", "#888888" },
{ "barrier", "#AA0000" },
{ "text", "#FFFFFF" },
{ "text_out", "#000000" },
{ "menu_background", "#000000BB" },
{ "menu_item_background", "#FFFFFF40" },
} },
{ "gruvbox_dark",
{
{ "piece_brick", "#cc241d" },
{ "piece_brick_out", "#fb4934" },
{ "piece_T", "#98971a" },
{ "piece_T_out", "#b8bb26" },
{ "piece_L_right", "#458588" },
{ "piece_L_right_out", "#83a598" },
{ "piece_Z_right", "#b16286" },
{ "piece_Z_right_out", "#d3869b" },
{ "piece_line", "#d79921" },
{ "piece_line_out", "#fabd2f" },
{ "piece_L_left", "#689d6a" },
{ "piece_L_left_out", "#8ec07c" },
{ "piece_Z_left", "#a89984" },
{ "piece_Z_left_out", "#ebdbb2" },
{ "shadow", "#bdae9380" },
{ "shadow_out", "#fbf1c7" },
{ "background", "#282828" },
{ "line", "#fbf1c7" },
{ "barrier", "#d65d0e" },
{ "text", "#ebdbb2" },
{ "text_out", "#1d2021" },
{ "menu_background", "#282828BB" },
{ "menu_item_background", "#d5c4a180" },
} },
{ "gruvbox_light",
{
{ "piece_brick", "#cc241d" },
{ "piece_brick_out", "#9d0006" },
{ "piece_T", "#98971a" },
{ "piece_T_out", "#79740e" },
{ "piece_L_right", "#458588" },
{ "piece_L_right_out", "#076678" },
{ "piece_Z_right", "#b16286" },
{ "piece_Z_right_out", "#8f3f71" },
{ "piece_line", "#d79921" },
{ "piece_line_out", "#b57614" },
{ "piece_L_left", "#689d6a" },
{ "piece_L_left_out", "#427b58" },
{ "piece_Z_left", "#7c6f64" },
{ "piece_Z_left_out", "#3c3836" },
{ "shadow", "#665c5480" },
{ "shadow_out", "#282828" },
{ "background", "#d5c4a1" },
{ "line", "#282828" },
{ "barrier", "#d65d0e" },
{ "text", "#3c3836" },
{ "text_out", "#f9f5d7" },
{ "menu_background", "#d5c4a1BB" },
{ "menu_item_background", "#50494580" },
} },
{ "blackandwhite",
{
{ "piece_brick", "#FFFFFF" },
{ "piece_brick_out", "#000000" },
{ "piece_T", "#FFFFFF" },
{ "piece_T_out", "#000000" },
{ "piece_L_right", "#FFFFFF" },
{ "piece_L_right_out", "#000000" },
{ "piece_Z_right", "#FFFFFF" },
{ "piece_Z_right_out", "#000000" },
{ "piece_line", "#FFFFFF" },
{ "piece_line_out", "#000000" },
{ "piece_L_left", "#FFFFFF" },
{ "piece_L_left_out", "#000000" },
{ "piece_Z_left", "#FFFFFF" },
{ "piece_Z_left_out", "#000000" },
{ "shadow", "#FFFFFF80" },
{ "shadow_out", "#FFFFFF" },
{ "background", "#000000" },
{ "line", "#FFFFFF" },
{ "barrier", "#FFFFFF" },
{ "text", "#FFFFFF" },
{ "text_out", "#000000" },
{ "menu_background", "#000000BB" },
{ "menu_item_background", "#FFFFFF40" },
} },
};
std::vector<std::string> color_schemes_names = { "default", "gruvbox_dark", "gruvbox_light", "blackandwhite" };
std::vector< std::string > color_schemes_names = { "default", "gruvbox_dark",
"gruvbox_light",
"blackandwhite" };
long unsigned int selected_color_scheme = 0;
bool g_show_shadow = true;
bool g_show_3d = false;

View File

@ -55,8 +55,9 @@
#define colors color_schemes[color_schemes_names[selected_color_scheme]]
extern std::map<std::string, std::map<std::string, std::string>> color_schemes;
extern std::vector<std::string> color_schemes_names;
extern std::map< std::string, std::map< std::string, std::string > >
color_schemes;
extern std::vector< std::string > color_schemes_names;
extern long unsigned int selected_color_scheme;
extern bool g_show_shadow;
extern bool g_show_3d;

View File

@ -1,7 +1,7 @@
#ifndef TETRIS_CUSTOM_CLASSES_H
#define TETRIS_CUSTOM_CLASSES_H
#include "../sdlpp.hpp"
#include "../sdlpp/sdlpp.hpp"
#include "config.hpp"
class TetrisBlock : public SDLPP::RectangleRender {
@ -9,53 +9,61 @@ public:
TetrisBlock() = delete;
TetrisBlock( double x, double y, double w, double h,
const std::shared_ptr< SDLPP::Renderer > &r,
const std::string &img_or_color, bool is_polygon,
int index, std::shared_ptr<SDLPP::Scene> scene, std::vector<int> &bag )
: RectangleRender( x, y, w, h, r, img_or_color, is_polygon ), pieces_bag(bag) {
const std::string &img_or_color, bool is_polygon, int index,
std::shared_ptr< SDLPP::Scene > scene,
std::vector< int > &bag )
: RectangleRender( x, y, w, h, r, img_or_color, is_polygon ),
pieces_bag( bag ) {
_index = index;
pieces_bag[_index]--;
_scene = scene;
setColors();
if(g_show_3d)
setTexture("block.png");
if ( g_show_3d )
setTexture( "block.png" );
}
TetrisBlock( const TetrisBlock &other ) : TetrisBlock(other.getDoubleRect().first.first,other.getDoubleRect().first.second,other.getDoubleRect().second.first,other.getDoubleRect().second.second,other.getRenderer(), other.getColor(), true, other._index, other._scene, other.pieces_bag) {}
TetrisBlock( const TetrisBlock &other )
: TetrisBlock( other.getDoubleRect().first.first,
other.getDoubleRect().first.second,
other.getDoubleRect().second.first,
other.getDoubleRect().second.second,
other.getRenderer(), other.getColor(), true,
other._index, other._scene, other.pieces_bag ) {}
~TetrisBlock() {
if(_index != PIECE_SHADOW)
if ( _index != PIECE_SHADOW )
pieces_bag[_index]++;
}
virtual std::shared_ptr<RenderObject> copySelf() override {
return std::make_shared<TetrisBlock>(*this);
virtual std::shared_ptr< RenderObject > copySelf() override {
return std::make_shared< TetrisBlock >( *this );
}
std::shared_ptr<TetrisBlock> copyInScene() {
auto ret = std::shared_ptr<TetrisBlock>(new TetrisBlock(*this));
_scene->addObject(ret);
std::shared_ptr< TetrisBlock > copyInScene() {
auto ret = std::shared_ptr< TetrisBlock >( new TetrisBlock( *this ) );
_scene->addObject( ret );
return ret;
}
bool isSamePos(const SDLPP::RenderObject &other) const {
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;
diff1 = ( diff1 < 0 ) * ( -1 ) * diff1 + ( diff1 > 0 ) * diff1;
auto diff2 = mypos.second - otherpos.second;
diff2 = (diff2 < 0) * (-1) * diff2 + (diff2 > 0) * diff2;
diff2 = ( diff2 < 0 ) * ( -1 ) * diff2 + ( diff2 > 0 ) * diff2;
return diff1 < 0.0001 && diff2 < 0.0001;
}
virtual void specialAction( int code ) override {
switch(code) {
case PIECE_ACTION_UPDATE_COLOR: {
setColors();
if(g_show_3d)
setTexture("block.png");
else
unsetTexture();
}
default:
break;
switch ( code ) {
case PIECE_ACTION_UPDATE_COLOR: {
setColors();
if ( g_show_3d )
setTexture( "block.png" );
else
unsetTexture();
}
default:
break;
}
}
void turnIntoShadow() {
setId(SHADOW_ID);
setId( SHADOW_ID );
// shadows don't consume pieces from bag
pieces_bag[_index]++;
_index = PIECE_SHADOW;
@ -64,36 +72,36 @@ public:
private:
std::string getPieceName() {
switch(_index) {
case PIECE_BRICK:
return "piece_brick";
case PIECE_T:
return "piece_T";
case PIECE_L_RIGHT:
return "piece_L_right";
case PIECE_Z_RIGHT:
return "piece_Z_right";
case PIECE_LINE:
return "piece_line";
case PIECE_L_LEFT:
return "piece_L_left";
case PIECE_Z_LEFT:
return "piece_Z_left";
case PIECE_SHADOW:
return "shadow";
default:
break;
switch ( _index ) {
case PIECE_BRICK:
return "piece_brick";
case PIECE_T:
return "piece_T";
case PIECE_L_RIGHT:
return "piece_L_right";
case PIECE_Z_RIGHT:
return "piece_Z_right";
case PIECE_LINE:
return "piece_line";
case PIECE_L_LEFT:
return "piece_L_left";
case PIECE_Z_LEFT:
return "piece_Z_left";
case PIECE_SHADOW:
return "shadow";
default:
break;
}
return "";
}
void setColors() {
auto piece_name = getPieceName();
setColor(colors[piece_name]);
setOutlineColor(colors[piece_name + "_out"]);
setColor( colors[piece_name] );
setOutlineColor( colors[piece_name + "_out"] );
}
int _index = 0;
std::shared_ptr<SDLPP::Scene> _scene;
std::vector<int> &pieces_bag;
std::shared_ptr< SDLPP::Scene > _scene;
std::vector< int > &pieces_bag;
};
class TetrisPiece {
@ -101,8 +109,7 @@ public:
TetrisPiece() {
original_pos.reserve( 4 );
}
void addPiece( std::shared_ptr< TetrisBlock > piece, int x,
int y ) {
void addPiece( std::shared_ptr< TetrisBlock > piece, int x, int y ) {
pieces.push_back( piece );
pieces_rel_position.push_back( { 0, 0, 0, 0 } );
// done this way for SPEEEEEEED
@ -116,7 +123,7 @@ public:
pieces_rel_position.back()[3] = ( y > 0 ) * y;
}
void rotate() {
if(!rotate_allowed)
if ( !rotate_allowed )
return;
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
auto &piece = pieces[i];
@ -160,7 +167,7 @@ public:
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
auto &piece = pieces[i];
auto &positions = pieces_rel_position[i];
std::pair<double, double> pos = {x, y};
std::pair< double, double > pos = { x, y };
pos.first -= positions[0] * BLOCK_SIZE;
pos.first += positions[1] * BLOCK_SIZE;
pos.second -= positions[2] * BLOCK_SIZE;
@ -168,10 +175,10 @@ public:
piece->setPos( pos.first, pos.second );
}
}
void setPos(const std::pair<double,double> &pos) {
setPos(pos.first, pos.second);
void setPos( const std::pair< double, double > &pos ) {
setPos( pos.first, pos.second );
}
std::pair<double, double> getPos() {
std::pair< double, double > getPos() {
auto &piece = pieces[0];
auto &relpositions = pieces_rel_position[0];
auto pos = piece->getPos();
@ -203,13 +210,13 @@ public:
bool isMoving() {
return userMovement > 0;
}
bool isLeft(const SDLPP::RenderObject &block) const {
return isPosition(block, 0);
bool isLeft( const SDLPP::RenderObject &block ) const {
return isPosition( block, 0 );
}
bool isRight(const SDLPP::RenderObject &block) const {
return isPosition(block, 1);
bool isRight( const SDLPP::RenderObject &block ) const {
return isPosition( block, 1 );
}
void movePiece(double x, double y) {
void movePiece( double x, double y ) {
for ( auto &block : getObjects() ) {
auto pos = block->getPos();
block->setPos( pos.first + x, pos.second + y );
@ -220,38 +227,38 @@ public:
}
void turnIntoShadow() {
for(auto &block : getObjects() )
for ( auto &block : getObjects() )
block->turnIntoShadow();
setHidden(!g_show_shadow);
setHidden( !g_show_shadow );
}
std::shared_ptr<TetrisPiece> copySelf() {
auto ret = std::make_shared<TetrisPiece>();
for(int i = 0; i < 4; i++) {
std::shared_ptr< TetrisPiece > copySelf() {
auto ret = std::make_shared< TetrisPiece >();
for ( int i = 0; i < 4; i++ ) {
auto block = pieces[i]->copyInScene();
block->centerX();
ret->addBlockInPos(block, pieces_rel_position[i]);
ret->addBlockInPos( block, pieces_rel_position[i] );
}
if(!rotate_allowed)
if ( !rotate_allowed )
ret->disableRotation();
ret->setHidden(_hidden);
ret->setHidden( _hidden );
return ret;
}
void destroy() {
for(auto &x : getObjects()) {
for ( auto &x : getObjects() ) {
x->destroy();
}
}
void addMovement(int x, int y) {
void addMovement( int x, int y ) {
movement.first += x;
movement.second += y;
}
std::pair<int,int> getMovement() const {
std::pair< int, int > getMovement() const {
return movement;
}
void setHidden(bool hidden) {
void setHidden( bool hidden ) {
_hidden = hidden;
for(auto &x : getObjects()) {
x->setHidden(hidden);
for ( auto &x : getObjects() ) {
x->setHidden( hidden );
}
}
bool getHidden() {
@ -259,21 +266,22 @@ public:
}
private:
bool isPosition(const SDLPP::RenderObject &block, int pos) const {
for(int i = 0; i < 4; i++) {
if(pieces[i]->isSamePos(block)) {
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;
}
void resetBlock(int index, std::shared_ptr< TetrisBlock > piece) {
piece->setPos(pieces[index]->getPos());
void resetBlock( int index, std::shared_ptr< TetrisBlock > piece ) {
piece->setPos( pieces[index]->getPos() );
pieces[index] = piece;
}
void addBlockInPos(std::shared_ptr<TetrisBlock> piece, const std::vector<int> &relpos) {
void addBlockInPos( std::shared_ptr< TetrisBlock > piece,
const std::vector< int > &relpos ) {
pieces.push_back( piece );
pieces_rel_position.push_back(relpos);
pieces_rel_position.push_back( relpos );
}
std::vector< std::vector< int > > pieces_rel_position;
std::vector< std::shared_ptr< TetrisBlock > > pieces;
@ -281,7 +289,7 @@ private:
bool descend = false;
int userMovement = 0;
bool rotate_allowed = true;
std::pair<int,int> movement = {0,0};
std::pair< int, int > movement = { 0, 0 };
bool _hidden = false;
};

View File

@ -4,47 +4,50 @@
#include "scenes.hpp"
#include <thread>
bool validPos(SDLPP::Scene &scene, std::shared_ptr<TetrisPiece> piece) {
bool validPos( SDLPP::Scene &scene, std::shared_ptr< TetrisPiece > piece ) {
for ( auto &x : piece->getObjects() ) {
auto collisions = scene.getCollisions( *x, { BRICK_ID, FLOOR_ID, BORDER_LEFT_ID, BORDER_RIGHT_ID } );
auto collisions = scene.getCollisions(
*x, { BRICK_ID, FLOOR_ID, BORDER_LEFT_ID, BORDER_RIGHT_ID } );
if ( collisions.size() > 1 )
return false;
}
return true;
}
void updateShadow(SDLPP::Scene &scene) {
if(!g_cur_object) {
void updateShadow( SDLPP::Scene &scene ) {
if ( !g_cur_object ) {
g_cur_shadow->destroy();
g_cur_shadow.reset();
return;
}
if(!g_cur_shadow)
if ( !g_cur_shadow )
return;
g_cur_shadow->setPos(g_cur_object->getPos());
g_cur_shadow->setPos( g_cur_object->getPos() );
double shadow_drop = BOTTOM_BORDER;
auto &invalid_objects = g_cur_object->getObjects();
for( auto &x : g_cur_shadow->getObjects() ) {
for ( auto &x : g_cur_shadow->getObjects() ) {
auto block_pos = x->getPos();
if(BOTTOM_BORDER - block_pos.second < shadow_drop)
if ( BOTTOM_BORDER - block_pos.second < shadow_drop )
shadow_drop = BOTTOM_BORDER - block_pos.second;
// set colider column's position to current block's X position
g_shadow_colider->setPos(block_pos.first, TOP_BORDER);
auto collisions = scene.getCollisions( *g_shadow_colider, { BRICK_ID } );
g_shadow_colider->setPos( block_pos.first, TOP_BORDER );
auto collisions =
scene.getCollisions( *g_shadow_colider, { BRICK_ID } );
auto curY = block_pos.second;
for(auto &col : collisions) {
for ( auto &col : collisions ) {
// if collision with g_cur_object, ignore
if(std::find(invalid_objects.begin(), invalid_objects.end(), col) != invalid_objects.end())
if ( std::find( invalid_objects.begin(), invalid_objects.end(),
col ) != invalid_objects.end() )
continue;
auto possible_drop = col->getPos().second - curY;
if(possible_drop < shadow_drop && possible_drop >= 0)
if ( possible_drop < shadow_drop && possible_drop >= 0 )
shadow_drop = possible_drop;
}
}
// we want the shadow to rest on top of the nearest floor
shadow_drop -= BLOCK_SIZE;
auto shadow_pos = g_cur_shadow->getPos();
g_cur_shadow->setPos(shadow_pos.first, shadow_pos.second + shadow_drop);
g_cur_shadow->setPos( shadow_pos.first, shadow_pos.second + shadow_drop );
}
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
@ -61,18 +64,18 @@ void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
if ( g_ticks_till_fall > 0 ) {
if ( g_cur_object->isMoving() && g_ticks_till_movement <= 0 ) {
g_ticks_till_movement = TICKS_TILL_MOVE;
g_cur_object->movePiece(movement.first * BLOCK_SIZE, 0);
if(!validPos(*scene, g_cur_object)) {
g_cur_object->movePiece(movement.first * -BLOCK_SIZE, 0);
g_cur_object->movePiece( movement.first * BLOCK_SIZE, 0 );
if ( !validPos( *scene, g_cur_object ) ) {
g_cur_object->movePiece( movement.first * -BLOCK_SIZE, 0 );
return;
} else
goto check_floor;
}
if ( g_cur_object->isDescending() && g_ticks_till_descend <= 0 ) {
g_ticks_till_descend = TICKS_TILL_DESCEND;
g_cur_object->movePiece(0, movement.second * BLOCK_SIZE);
if(!validPos(*scene, g_cur_object)) {
g_cur_object->movePiece(0, movement.second * -BLOCK_SIZE);
g_cur_object->movePiece( 0, movement.second * BLOCK_SIZE );
if ( !validPos( *scene, g_cur_object ) ) {
g_cur_object->movePiece( 0, movement.second * -BLOCK_SIZE );
return;
} else
goto check_floor;
@ -80,7 +83,7 @@ void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
return;
}
g_ticks_till_fall = TICKS_TILL_FALL;
g_cur_object->movePiece(0, BLOCK_SIZE);
g_cur_object->movePiece( 0, BLOCK_SIZE );
check_floor:
bool fell = false;
for ( auto &x : g_cur_object->getObjects() ) {
@ -92,18 +95,18 @@ check_floor:
}
}
if ( fell ) {
g_cur_object->movePiece(0, -BLOCK_SIZE);
g_cur_object->movePiece( 0, -BLOCK_SIZE );
for ( auto &block : g_cur_object->getObjects() ) {
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
g_game_over_scene->updateSizeAndPosition();
g_active_scenes.push_back( g_game_over_scene );
g_input_functions.push_back(gameOverSceneInput);
g_input_functions.push_back( gameOverSceneInput );
break;
}
}
g_cur_object.reset();
}
updateShadow(*scene);
updateShadow( *scene );
}
void quitGame() {
@ -120,35 +123,38 @@ void resetGame() {
g_main_scene->resetScene();
g_main_scene->setPrevTicks( SDL_GetTicks() );
g_active_scenes = {g_main_scene};
g_input_functions = {mainSceneInput};
for(int i = 0; i < 7; i++)
g_active_scenes = { g_main_scene };
g_input_functions = { mainSceneInput };
for ( int i = 0; i < 7; i++ )
g_bag[i] = 28;
}
int crashFlags( std::shared_ptr<TetrisPiece> piece, std::shared_ptr<TetrisBlock> block, SDLPP::Scene &scene, int left, int right, int bottom) {
int crashFlags( std::shared_ptr< TetrisPiece > piece,
std::shared_ptr< TetrisBlock > block, SDLPP::Scene &scene,
int left, int right, int bottom ) {
int retFlags = 0;
auto collisions = scene.getCollisions(*block, {BORDER_LEFT_ID, BORDER_RIGHT_ID, FLOOR_ID});
for(auto &col : collisions) {
switch(col->getId()) {
case BORDER_LEFT_ID:
retFlags |= left;
break;
case BORDER_RIGHT_ID:
retFlags |= right;
break;
case FLOOR_ID:
retFlags |= bottom;
default:
break;
auto collisions = scene.getCollisions(
*block, { BORDER_LEFT_ID, BORDER_RIGHT_ID, FLOOR_ID } );
for ( auto &col : collisions ) {
switch ( col->getId() ) {
case BORDER_LEFT_ID:
retFlags |= left;
break;
case BORDER_RIGHT_ID:
retFlags |= right;
break;
case FLOOR_ID:
retFlags |= bottom;
default:
break;
}
}
collisions = scene.getCollisions(*block, {BRICK_ID});
if(collisions.size() > 1) {
for(auto &col : collisions) {
if(piece->isLeft(*col))
collisions = scene.getCollisions( *block, { BRICK_ID } );
if ( collisions.size() > 1 ) {
for ( auto &col : collisions ) {
if ( piece->isLeft( *col ) )
retFlags |= left;
else if(piece->isRight(*col))
else if ( piece->isRight( *col ) )
retFlags |= right;
else
retFlags |= bottom;
@ -157,7 +163,8 @@ int crashFlags( std::shared_ptr<TetrisPiece> piece, std::shared_ptr<TetrisBlock>
return retFlags;
}
bool checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene ) {
bool checkRotation( std::shared_ptr< TetrisPiece > piece,
SDLPP::Scene &scene ) {
int crash = 0;
int cur_left = 0x01;
int cur_right = 0x02;
@ -167,31 +174,33 @@ bool checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene ) {
int counter = 0;
do {
counter++;
if(counter > 5) {
if ( counter > 5 ) {
piece->revert();
return false;
}
crash = 0;
for(auto &block : piece->getObjects()) {
crash |= crashFlags(piece, block, scene, cur_left, cur_right, bottom);
for ( auto &block : piece->getObjects() ) {
crash |=
crashFlags( piece, block, scene, cur_left, cur_right, bottom );
}
if(crash & bottom || (crash & cur_left && crash & cur_right) ||
(crash & cur_left && crash & was_right) || (crash & cur_right && crash & was_left)) {
if ( crash & bottom || ( crash & cur_left && crash & cur_right ) ||
( crash & cur_left && crash & was_right ) ||
( crash & cur_right && crash & was_left ) ) {
piece->revert();
return false;
}
crash &= ~(was_left | was_right);
if(crash & cur_left) {
piece->movePiece(BLOCK_SIZE, 0);
crash &= ~( was_left | was_right );
if ( crash & cur_left ) {
piece->movePiece( BLOCK_SIZE, 0 );
crash &= ~cur_left;
crash |= was_left;
}
if(crash & cur_right) {
piece->movePiece(-BLOCK_SIZE, 0);
if ( crash & cur_right ) {
piece->movePiece( -BLOCK_SIZE, 0 );
crash &= ~cur_right;
crash |= was_right;
}
} while(crash);
} while ( crash );
return true;
}
@ -200,10 +209,11 @@ createTetrisBlock( double x, double y, const std::string &color,
const std::string &outline, int index,
std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto ret = std::make_shared< TetrisBlock >( x, y, BLOCK_SIZE, BLOCK_SIZE,
renderer, color, true, index, scene, g_bag );
auto ret =
std::make_shared< TetrisBlock >( x, y, BLOCK_SIZE, BLOCK_SIZE, renderer,
color, true, index, scene, g_bag );
ret->setOutlineColor( outline );
ret->addCollision( SDLPP::Rect( 0.1, 0.1, 0.8, 0.8 ) );
ret->addCollision( SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) );
ret->setId( BRICK_ID );
ret->centerX();
scene->addObject( ret );
@ -381,38 +391,41 @@ tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
}
void updateColors() {
for(auto &x : g_main_scene->getObjects({BRICK_ID, SHADOW_ID})) {
x->specialAction(PIECE_ACTION_UPDATE_COLOR);
for ( auto &x : g_main_scene->getObjects( { BRICK_ID, SHADOW_ID } ) ) {
x->specialAction( PIECE_ACTION_UPDATE_COLOR );
}
for(auto &x : g_main_scene->getObjects({BARRIER_ID})) {
x->setColor(colors["barrier"]);
for ( auto &x : g_main_scene->getObjects( { BARRIER_ID } ) ) {
x->setColor( colors["barrier"] );
}
for(auto &x : g_main_scene->getObjects({BACKGROUND_ID})) {
x->setColor(colors["background"]);
for ( auto &x : g_main_scene->getObjects( { BACKGROUND_ID } ) ) {
x->setColor( colors["background"] );
}
for(auto &x : g_main_scene->getObjects({SHADOW_ID})) {
x->setColor(colors["shadow"]);
for ( auto &x : g_main_scene->getObjects( { SHADOW_ID } ) ) {
x->setColor( colors["shadow"] );
}
for(auto &x : g_main_scene->getObjects({LINE_ID})) {
x->setColor(colors["line"]);
for ( auto &x : g_main_scene->getObjects( { LINE_ID } ) ) {
x->setColor( colors["line"] );
}
for(auto &x : g_main_scene->getObjects({TEXT_ID})) {
std::dynamic_pointer_cast<SDLPP::TextRenderer>(x)->setTextColor(*g_font, colors["text"], colors["text_out"], 5);
for ( auto &x : g_main_scene->getObjects( { TEXT_ID } ) ) {
std::dynamic_pointer_cast< SDLPP::TextRenderer >( x )->setTextColor(
*g_font, colors["text"], colors["text_out"], 5 );
}
g_menu_options[g_menu_select]->setColor(colors["menu_item_background"]);
g_game_over_options[g_game_over_select]->setColor(colors["menu_item_background"]);
g_options_options[g_options_select]->setColor(colors["menu_item_background"]);
for(auto &x : g_menu_scene->getObjects({MENU_BACKGROUND_ID})) {
x->setColor(colors["menu_background"]);
g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
g_game_over_options[g_game_over_select]->setColor(
colors["menu_item_background"] );
g_options_options[g_options_select]->setColor(
colors["menu_item_background"] );
for ( auto &x : g_menu_scene->getObjects( { MENU_BACKGROUND_ID } ) ) {
x->setColor( colors["menu_background"] );
}
for(auto &x : g_game_over_scene->getObjects({MENU_BACKGROUND_ID})) {
x->setColor(colors["menu_background"]);
for ( auto &x : g_game_over_scene->getObjects( { MENU_BACKGROUND_ID } ) ) {
x->setColor( colors["menu_background"] );
}
for(auto &x : g_options_scene->getObjects({MENU_BACKGROUND_ID})) {
x->setColor(colors["menu_background"]);
for ( auto &x : g_options_scene->getObjects( { MENU_BACKGROUND_ID } ) ) {
x->setColor( colors["menu_background"] );
}
for(auto &x : g_options_scene->getObjects({MENU_ITEM_ID})) {
std::dynamic_pointer_cast<SDLPP::TextRenderer>(x)->setTextColor(*g_font, colors["text"], colors["text_out"]);
for ( auto &x : g_options_scene->getObjects( { MENU_ITEM_ID } ) ) {
std::dynamic_pointer_cast< SDLPP::TextRenderer >( x )->setTextColor(
*g_font, colors["text"], colors["text_out"] );
}
}

View File

@ -1,15 +1,15 @@
#ifndef TETRIS_FUNCTIONS_H
#define TETRIS_FUNCTIONS_H
#include "../sdlpp.hpp"
#include "../sdlpp/sdlpp.hpp"
#include "custom_classes.hpp"
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks );
bool validPos(SDLPP::Scene &scene, std::shared_ptr<TetrisPiece> piece);
void updateShadow(SDLPP::Scene &scene);
bool validPos( SDLPP::Scene &scene, std::shared_ptr< TetrisPiece > piece );
void updateShadow( SDLPP::Scene &scene );
void quitGame();
void resetGame();
bool checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene );
bool checkRotation( std::shared_ptr< TetrisPiece > piece, SDLPP::Scene &scene );
std::shared_ptr< TetrisPiece >
tetrisBrick( std::shared_ptr< SDLPP::Renderer > renderer,

View File

@ -36,8 +36,11 @@ std::shared_ptr< SDLPP::RectangleRender > g_shadow_colider{};
std::mutex g_movement_mutex{};
bool g_quit = false;
std::vector<std::function<void(std::shared_ptr<SDLPP::Scene>, int, std::vector<std::shared_ptr<SDLPP::RenderObject>>&)>>g_input_functions{};
std::vector<std::shared_ptr< SDLPP::Scene >> g_active_scenes{};
std::vector< std::function< void(
std::shared_ptr< SDLPP::Scene >, int,
std::vector< std::shared_ptr< SDLPP::RenderObject > > & ) > >
g_input_functions{};
std::vector< std::shared_ptr< SDLPP::Scene > > g_active_scenes{};
std::vector< std::shared_ptr< TetrisPiece > ( * )(
std::shared_ptr< SDLPP::Renderer >, std::shared_ptr< SDLPP::Scene > ) >
@ -46,4 +49,4 @@ std::vector< std::shared_ptr< TetrisPiece > ( * )(
tetrisLine, tetrisLLeft, tetrisZLeft,
};
std::shared_ptr<SDLPP::Font> g_font{};
std::shared_ptr< SDLPP::Font > g_font{};

View File

@ -1,7 +1,7 @@
#ifndef TETRIS_GLOBAL_VARS_H
#define TETRIS_GLOBAL_VARS_H
#include "../sdlpp.hpp"
#include "../sdlpp/sdlpp.hpp"
#include "custom_classes.hpp"
#include <functional>
@ -14,8 +14,10 @@ extern int g_menu_select;
extern int g_game_over_select;
extern int g_options_select;
extern std::vector< std::shared_ptr< SDLPP::RectangleRender > > g_menu_options;
extern std::vector< std::shared_ptr< SDLPP::RectangleRender > > g_game_over_options;
extern std::vector< std::shared_ptr< SDLPP::RectangleRender > > g_options_options;
extern std::vector< std::shared_ptr< SDLPP::RectangleRender > >
g_game_over_options;
extern std::vector< std::shared_ptr< SDLPP::RectangleRender > >
g_options_options;
extern std::shared_ptr< SDLPP::TextRenderer > g_score_texture;
extern std::shared_ptr< SDLPP::Renderer > g_active_renderer;
extern int g_score;
@ -40,13 +42,16 @@ extern std::shared_ptr< SDLPP::RectangleRender > g_shadow_colider;
extern std::mutex g_movement_mutex;
extern bool g_quit;
extern std::vector<std::function<void(std::shared_ptr<SDLPP::Scene>, int, std::vector<std::shared_ptr<SDLPP::RenderObject>>&)>>g_input_functions;
extern std::vector<std::shared_ptr< SDLPP::Scene >> g_active_scenes;
extern std::vector< std::function< void(
std::shared_ptr< SDLPP::Scene >, int,
std::vector< std::shared_ptr< SDLPP::RenderObject > > & ) > >
g_input_functions;
extern std::vector< std::shared_ptr< SDLPP::Scene > > g_active_scenes;
extern std::vector< std::shared_ptr< TetrisPiece > ( * )(
std::shared_ptr< SDLPP::Renderer >, std::shared_ptr< SDLPP::Scene > ) >
g_tetrisFunctions;
extern std::shared_ptr<SDLPP::Font> g_font;
extern std::shared_ptr< SDLPP::Font > g_font;
#endif

View File

@ -20,11 +20,13 @@ constexpr uint64_t OPTIONS_MENU_SAVE = 3;
// Scene preparation
void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r, std::shared_ptr<SDLPP::Font> font ) {
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
colors["background"], true );
void addMainSceneItems( SDLPP::Scene &scene,
std::shared_ptr< SDLPP::Renderer > &r,
std::shared_ptr< SDLPP::Font > font ) {
auto bg = std::make_shared< SDLPP::RectangleRender >(
0, 0, 10, 10, r, colors["background"], true );
bg->setPermanent();
bg->setId(BACKGROUND_ID);
bg->setId( BACKGROUND_ID );
scene.addObject( bg );
// create coliders for counting blocks in line
@ -33,7 +35,7 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
posy -= BLOCK_SIZE;
auto colider = std::make_shared< SDLPP::RectangleRender >(
LEFT_BORDER, posy, RIGHT_BORDER - LEFT_BORDER, BLOCK_SIZE, r );
colider->addCollision(SDLPP::Rect( 0.01, 0.1, 0.98, 0.8 ));
colider->addCollision( SDLPP::RectColider( 0.01, 0.1, 0.98, 0.8 ) );
colider->setId( COLIDER_ID );
colider->setStatic();
colider->centerX();
@ -44,35 +46,39 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
posy = 1;
for ( int i = 0; i < 20; i++ ) {
posy -= BLOCK_SIZE;
auto line = std::make_shared< SDLPP::LineRenderer >( LEFT_BORDER, posy, RIGHT_BORDER, posy, r, colors["line"] );
auto line = std::make_shared< SDLPP::LineRenderer >(
LEFT_BORDER, posy, RIGHT_BORDER, posy, r, colors["line"] );
line->setStatic();
line->centerX();
line->setId(LINE_ID);
line->setId( LINE_ID );
scene.addObject( line );
}
auto posx = RIGHT_BORDER;
for ( int i = 0; i < 9; i++ ) {
posx -= BLOCK_SIZE;
auto line = std::make_shared< SDLPP::LineRenderer >( posx, TOP_BORDER + BLOCK_SIZE, posx, BOTTOM_BORDER, r, colors["line"] );
auto line = std::make_shared< SDLPP::LineRenderer >(
posx, TOP_BORDER + BLOCK_SIZE, posx, BOTTOM_BORDER, r,
colors["line"] );
line->setStatic();
line->centerX();
line->setId(LINE_ID);
line->setId( LINE_ID );
scene.addObject( line );
}
auto left_barrier = std::make_shared< SDLPP::RectangleRender >(
LEFT_BORDER - 0.02, 0, 0.02, BOTTOM_BORDER, r, colors["barrier"], true );
LEFT_BORDER - 0.02, 0, 0.02, BOTTOM_BORDER, r, colors["barrier"],
true );
left_barrier->centerX();
left_barrier->setStatic();
left_barrier->setId(BARRIER_ID);
left_barrier->setId( BARRIER_ID );
scene.addObject( left_barrier );
auto right_barrier = std::make_shared< SDLPP::RectangleRender >(
RIGHT_BORDER, 0, 0.02, BOTTOM_BORDER, r, colors["barrier"], true );
right_barrier->centerX();
right_barrier->setStatic();
right_barrier->setId(BARRIER_ID);
right_barrier->setId( BARRIER_ID );
scene.addObject( right_barrier );
auto bottom_barrier = std::make_shared< SDLPP::RectangleRender >(
@ -80,27 +86,29 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
0.02, r, colors["barrier"], true );
bottom_barrier->centerX();
bottom_barrier->setStatic();
bottom_barrier->setId(BARRIER_ID);
bottom_barrier->setId( BARRIER_ID );
scene.addObject( bottom_barrier );
auto tetris = std::make_shared< SDLPP::TextRenderer >(
0.4, 0, 0.2, 0.1, r, *font, "TETRIS", colors["text"], colors["text_out"], 5 );
0.4, 0, 0.2, 0.1, r, *font, "TETRIS", colors["text"],
colors["text_out"], 5 );
tetris->centerX();
tetris->setStatic();
tetris->setId(TEXT_ID);
tetris->setId( TEXT_ID );
scene.addObject( tetris );
auto next = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, *font, "NEXT", colors["text"], colors["text_out"], 5, SDLPP_TEXT_CENTER );
RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, *font, "NEXT", colors["text"],
colors["text_out"], 5, SDLPP_TEXT_CENTER );
next->centerX();
next->setStatic();
next->setId(TEXT_ID);
next->setId( TEXT_ID );
scene.addObject( next );
// gameover colider
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 );
auto gameover_collision = SDLPP::RectColider( -1, 0, -1, 0.9 );
gameover_collision.setInfinite();
gameover->addCollision( gameover_collision );
gameover->setId( GAME_OVER );
@ -109,50 +117,55 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
scene.addObject( gameover );
auto score_text = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, *font, "SCORE", colors["text"], colors["text_out"], 5, SDLPP_TEXT_CENTER );
RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, *font, "SCORE", colors["text"],
colors["text_out"], 5, SDLPP_TEXT_CENTER );
score_text->centerX();
score_text->setStatic();
score_text->setId(TEXT_ID);
score_text->setId( TEXT_ID );
scene.addObject( score_text );
auto score_texture = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, *font, "0",
colors["text"], colors["text_out"], 5, SDLPP_TEXT_TOP );
RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, *font, "0", colors["text"],
colors["text_out"], 5, SDLPP_TEXT_TOP );
score_texture->centerX();
score_texture->setStatic();
score_texture->setId(SCORE_TEXTURE_ID);
score_texture->setId( SCORE_TEXTURE_ID );
scene.addObject( score_texture );
auto border = std::make_shared< SDLPP::RectangleRender >( LEFT_BORDER - 1, 0, 1, BOTTOM_BORDER, r);
auto border = std::make_shared< SDLPP::RectangleRender >(
LEFT_BORDER - 1, 0, 1, BOTTOM_BORDER, r );
border->setId( BORDER_LEFT_ID );
border->setStatic();
border->centerX();
border->addCollision(SDLPP::Rect( 0, 0, 0.99, 1));
border->setColiderColor("#FF00FF");
scene.addObject(border);
border->addCollision( SDLPP::RectColider( 0, 0, 0.99, 1 ) );
border->setColiderColor( "#FF00FF" );
scene.addObject( border );
border = std::make_shared< SDLPP::RectangleRender >( RIGHT_BORDER, 0, 1, BOTTOM_BORDER, r);
border = std::make_shared< SDLPP::RectangleRender >( RIGHT_BORDER, 0, 1,
BOTTOM_BORDER, r );
border->setId( BORDER_RIGHT_ID );
border->setStatic();
border->centerX();
border->addCollision(SDLPP::Rect( 0.01, 0, 1, 1));
border->setColiderColor("#FF00FF");
scene.addObject(border);
border->addCollision( SDLPP::RectColider( 0.01, 0, 1, 1 ) );
border->setColiderColor( "#FF00FF" );
scene.addObject( border );
auto floor = std::make_shared< SDLPP::RectangleRender >( LEFT_BORDER, BOTTOM_BORDER, RIGHT_BORDER - LEFT_BORDER, 1, r);
auto floor = std::make_shared< SDLPP::RectangleRender >(
LEFT_BORDER, BOTTOM_BORDER, RIGHT_BORDER - LEFT_BORDER, 1, r );
floor->setId( FLOOR_ID );
floor->setStatic();
floor->centerX();
floor->addCollision(SDLPP::Rect(0, 0.01, 1, 1));
floor->setColiderColor("#00FF00");
scene.addObject(floor);
floor->addCollision( SDLPP::RectColider( 0, 0.01, 1, 1 ) );
floor->setColiderColor( "#00FF00" );
scene.addObject( floor );
}
void addMenuSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r, std::shared_ptr<SDLPP::Font> font ) {
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
colors["menu_background"], true );
bg->setId(MENU_BACKGROUND_ID);
void addMenuSceneItems( SDLPP::Scene &scene,
std::shared_ptr< SDLPP::Renderer > &r,
std::shared_ptr< SDLPP::Font > font ) {
auto bg = std::make_shared< SDLPP::RectangleRender >(
0, 0, 10, 10, r, colors["menu_background"], true );
bg->setId( MENU_BACKGROUND_ID );
bg->setPermanent( true );
scene.addObject( bg );
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
@ -165,34 +178,37 @@ void addMenuSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
resume->setText( *font, "Resume", colors["text"], colors["text_out"], 5 );
resume->setColor( colors["menu_item_background"] );
resume->centerX();
resume->setId(MENU_ITEM_ID);
g_menu_options.push_back(resume);
resume->setId( MENU_ITEM_ID );
g_menu_options.push_back( resume );
scene.addObject( resume );
auto options = std::make_shared< SDLPP::TextRenderer >(0.4, 0.56, 0.2, 0.08, r);
options->setText(*font, "Options", colors["text"], colors["text_out"], 5);
auto options =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.56, 0.2, 0.08, r );
options->setText( *font, "Options", colors["text"], colors["text_out"], 5 );
options->centerX();
options->setId(MENU_ITEM_ID);
g_menu_options.push_back(options);
scene.addObject(options);
options->setId( MENU_ITEM_ID );
g_menu_options.push_back( options );
scene.addObject( options );
auto restart =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.66, 0.2, 0.08, r );
restart->setText( *font, "Restart", colors["text"], colors["text_out"], 5 );
restart->centerX();
restart->setId(MENU_ITEM_ID);
g_menu_options.push_back(restart);
restart->setId( MENU_ITEM_ID );
g_menu_options.push_back( restart );
scene.addObject( restart );
auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.76, 0.2, 0.08, r );
quit->setText( *font, "Quit Game", colors["text"], colors["text_out"], 5 );
quit->centerX();
quit->setId(MENU_ITEM_ID);
g_menu_options.push_back(quit);
quit->setId( MENU_ITEM_ID );
g_menu_options.push_back( quit );
scene.addObject( quit );
}
void addGameOverSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r, std::shared_ptr<SDLPP::Font> font ) {
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
colors["menu_background"], true );
void addGameOverSceneItems( SDLPP::Scene &scene,
std::shared_ptr< SDLPP::Renderer > &r,
std::shared_ptr< SDLPP::Font > font ) {
auto bg = std::make_shared< SDLPP::RectangleRender >(
0, 0, 10, 10, r, colors["menu_background"], true );
bg->setId( MENU_BACKGROUND_ID );
bg->setPermanent( true );
scene.addObject( bg );
@ -206,21 +222,23 @@ void addGameOverSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Rendere
restart->setText( *font, "Restart", colors["text"], colors["text_out"], 5 );
restart->centerX();
restart->setColor( colors["menu_item_background"] );
restart->setId(MENU_ITEM_ID);
g_game_over_options.push_back(restart);
restart->setId( MENU_ITEM_ID );
g_game_over_options.push_back( restart );
scene.addObject( restart );
auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r );
quit->setText( *font, "Quit Game", colors["text"], colors["text_out"], 5 );
quit->centerX();
quit->setId(MENU_ITEM_ID);
g_game_over_options.push_back(quit);
quit->setId( MENU_ITEM_ID );
g_game_over_options.push_back( quit );
scene.addObject( quit );
}
void addOptionsSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r, std::shared_ptr<SDLPP::Font> font ) {
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
colors["menu_background"], true );
void addOptionsSceneItems( SDLPP::Scene &scene,
std::shared_ptr< SDLPP::Renderer > &r,
std::shared_ptr< SDLPP::Font > font ) {
auto bg = std::make_shared< SDLPP::RectangleRender >(
0, 0, 10, 10, r, colors["menu_background"], true );
bg->setId( MENU_BACKGROUND_ID );
bg->setPermanent( true );
scene.addObject( bg );
@ -231,56 +249,68 @@ void addOptionsSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer
scene.addObject( y );
auto color_scheme =
std::make_shared< SDLPP::TextRenderer >( 0.35, 0.3, 0.3, 0.09, r );
color_scheme->setText( *font, "Color scheme: " + color_schemes_names[selected_color_scheme], colors["text"], colors["text_out"], 5 );
color_scheme->setText(
*font, "Color scheme: " + color_schemes_names[selected_color_scheme],
colors["text"], colors["text_out"], 5 );
color_scheme->centerX();
color_scheme->setColor( colors["menu_item_background"] );
color_scheme->setId(MENU_ITEM_ID);
g_options_options.push_back(color_scheme);
color_scheme->setId( MENU_ITEM_ID );
g_options_options.push_back( color_scheme );
scene.addObject( color_scheme );
auto shadow =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.4, 0.2, 0.09, r );
shadow->setText( *font, "Show shadow: YES", colors["text"], colors["text_out"], 5 );
shadow->setText( *font, "Show shadow: YES", colors["text"],
colors["text_out"], 5 );
shadow->centerX();
shadow->setId(MENU_ITEM_ID);
g_options_options.push_back(shadow);
scene.addObject(shadow);
shadow->setId( MENU_ITEM_ID );
g_options_options.push_back( shadow );
scene.addObject( shadow );
auto show3d =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.09, r );
show3d->setText( *font, "Show block texture: NO", colors["text"], colors["text_out"], 5 );
show3d->setText( *font, "Show block texture: NO", colors["text"],
colors["text_out"], 5 );
show3d->centerX();
show3d->setId(MENU_ITEM_ID);
g_options_options.push_back(show3d);
scene.addObject(show3d);
show3d->setId( MENU_ITEM_ID );
g_options_options.push_back( show3d );
scene.addObject( show3d );
auto save =
std::make_shared< SDLPP::TextRenderer >( 0.45, 0.6, 0.1, 0.09, r );
save->setText( *font, "SAVE", colors["text"], colors["text_out"], 5 );
save->centerX();
save->setId(MENU_ITEM_ID);
g_options_options.push_back(save);
save->setId( MENU_ITEM_ID );
g_options_options.push_back( save );
scene.addObject( save );
}
std::shared_ptr<SDLPP::Scene> prepareMainScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font) {
std::shared_ptr< SDLPP::Scene >
prepareMainScene( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Font > font ) {
auto scene = std::make_shared< SDLPP::Scene >( renderer );
addMainSceneItems( *scene, renderer, font );
return scene;
}
std::shared_ptr<SDLPP::Scene> prepareMenuScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font) {
std::shared_ptr< SDLPP::Scene >
prepareMenuScene( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Font > font ) {
auto scene = std::make_shared< SDLPP::Scene >( renderer );
addMenuSceneItems(*scene, renderer, font);
addMenuSceneItems( *scene, renderer, font );
return scene;
}
std::shared_ptr<SDLPP::Scene> prepareGameOverScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font) {
std::shared_ptr< SDLPP::Scene >
prepareGameOverScene( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Font > font ) {
auto scene = std::make_shared< SDLPP::Scene >( renderer );
addGameOverSceneItems(*scene, renderer, font);
addGameOverSceneItems( *scene, renderer, font );
return scene;
}
std::shared_ptr<SDLPP::Scene> prepareOptionsScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font) {
std::shared_ptr< SDLPP::Scene >
prepareOptionsScene( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Font > font ) {
auto scene = std::make_shared< SDLPP::Scene >( renderer );
addOptionsSceneItems(*scene, renderer, font);
addOptionsSceneItems( *scene, renderer, font );
return scene;
}
@ -291,57 +321,57 @@ void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
case SDLK_ESCAPE:
g_menu_scene->updateSizeAndPosition();
g_active_scenes.push_back( g_menu_scene );
g_input_functions.push_back(menuSceneInput);
g_input_functions.push_back( menuSceneInput );
break;
case SDLK_LEFT:
case SDLK_a:
if(!g_cur_object)
if ( !g_cur_object )
break;
g_cur_object->movePiece(-BLOCK_SIZE, 0);
if(!validPos(scene, g_cur_object))
g_cur_object->movePiece(BLOCK_SIZE, 0);
g_cur_object->movePiece( -BLOCK_SIZE, 0 );
if ( !validPos( scene, g_cur_object ) )
g_cur_object->movePiece( BLOCK_SIZE, 0 );
else
updateShadow(scene);
updateShadow( scene );
g_ticks_till_movement = 2*TICKS_TILL_MOVE;
g_ticks_till_movement = 2 * TICKS_TILL_MOVE;
g_cur_object->startMovement();
g_cur_object->addMovement(-1,0);
g_cur_object->addMovement( -1, 0 );
break;
case SDLK_RIGHT:
case SDLK_d:
if(!g_cur_object)
if ( !g_cur_object )
break;
g_cur_object->movePiece(BLOCK_SIZE, 0);
if(!validPos(scene, g_cur_object))
g_cur_object->movePiece(-BLOCK_SIZE, 0);
g_cur_object->movePiece( BLOCK_SIZE, 0 );
if ( !validPos( scene, g_cur_object ) )
g_cur_object->movePiece( -BLOCK_SIZE, 0 );
else
updateShadow(scene);
updateShadow( scene );
g_ticks_till_movement = 2*TICKS_TILL_MOVE;
g_ticks_till_movement = 2 * TICKS_TILL_MOVE;
g_cur_object->startMovement();
g_cur_object->addMovement(1,0);
g_cur_object->addMovement( 1, 0 );
break;
case SDLK_DOWN:
case SDLK_s:
if(!g_cur_object)
if ( !g_cur_object )
break;
g_ticks_till_descend = 0;
g_cur_object->startDescend();
g_cur_object->addMovement(0,1);
g_cur_object->addMovement( 0, 1 );
break;
case SDLK_UP:
case SDLK_w:
if(!g_cur_object)
if ( !g_cur_object )
break;
g_cur_object->rotate();
if( checkRotation( g_cur_object, scene ) )
if ( checkRotation( g_cur_object, scene ) )
g_cur_shadow->rotate();
updateShadow(scene);
updateShadow( scene );
break;
case SDLK_SPACE:
if(!g_cur_object)
if ( !g_cur_object )
break;
g_cur_object->setPos(g_cur_shadow->getPos());
g_cur_object->setPos( g_cur_shadow->getPos() );
break;
#ifdef DEBUG
case SDLK_r:
@ -354,39 +384,38 @@ void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
}
void handleKeyUpMain( SDL_Keycode key ) {
switch(key) {
case SDLK_DOWN:
case SDLK_s:
if(!g_cur_object)
break;
if(g_cur_object->isDescending()) {
g_cur_object->stopDescend();
g_cur_object->addMovement(0,-1);
}
switch ( key ) {
case SDLK_DOWN:
case SDLK_s:
if ( !g_cur_object )
break;
case SDLK_LEFT:
case SDLK_a:
if(!g_cur_object)
break;
if(g_cur_object->isMoving()) {
g_cur_object->stopMovement();
g_cur_object->addMovement(1,0);
}
if ( g_cur_object->isDescending() ) {
g_cur_object->stopDescend();
g_cur_object->addMovement( 0, -1 );
}
break;
case SDLK_LEFT:
case SDLK_a:
if ( !g_cur_object )
break;
case SDLK_RIGHT:
case SDLK_d:
if(!g_cur_object)
break;
if(g_cur_object->isMoving()) {
g_cur_object->stopDescend();
g_cur_object->addMovement(-1,0);
}
default:
if ( g_cur_object->isMoving() ) {
g_cur_object->stopMovement();
g_cur_object->addMovement( 1, 0 );
}
break;
case SDLK_RIGHT:
case SDLK_d:
if ( !g_cur_object )
break;
if ( g_cur_object->isMoving() ) {
g_cur_object->stopDescend();
g_cur_object->addMovement( -1, 0 );
}
default:
break;
}
}
void pollEventsMain( SDLPP::Scene &scene ) {
SDL_Event event;
while ( SDLPP::getSDLEvent( event ) ) {
@ -403,7 +432,7 @@ void pollEventsMain( SDLPP::Scene &scene ) {
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
for(auto &x : g_active_scenes)
for ( auto &x : g_active_scenes )
x->updateSizeAndPosition();
}
default:
@ -412,7 +441,9 @@ void pollEventsMain( SDLPP::Scene &scene ) {
}
}
void mainSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vector<std::shared_ptr<SDLPP::RenderObject>> &line_coliders ) {
void mainSceneInput(
std::shared_ptr< SDLPP::Scene > scene, int base,
std::vector< std::shared_ptr< SDLPP::RenderObject > > &line_coliders ) {
std::lock_guard< std::mutex > guard( g_movement_mutex );
pollEventsMain( *scene );
if ( g_cur_object ) {
@ -428,7 +459,7 @@ void mainSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vecto
col->destroy();
}
auto colider_y = colider->getPos().second;
for ( auto &elem : scene->getObjects({BRICK_ID}) ) {
for ( auto &elem : scene->getObjects( { BRICK_ID } ) ) {
auto pos = elem->getPos();
if ( pos.second < colider_y && pos.first <= RIGHT_BORDER ) {
elem->setPos( pos.first, pos.second + BLOCK_SIZE );
@ -442,22 +473,22 @@ void mainSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vecto
collisions = scene->getCollisions( *colider, { BRICK_ID } );
}
}
if(lines > 0) {
if ( lines > 0 ) {
g_update_score = true;
switch(lines) {
case 1:
g_score += 40;
break;
case 2:
g_score += 100;
break;
case 3:
g_score += 300;
break;
case 4:
g_score += 1200;
default:
break;
switch ( lines ) {
case 1:
g_score += 40;
break;
case 2:
g_score += 100;
break;
case 3:
g_score += 300;
break;
case 4:
g_score += 1200;
default:
break;
}
}
g_checked_line = true;
@ -480,9 +511,10 @@ void handleKeyDownMenu( SDL_Keycode key ) {
case SDLK_DOWN:
g_menu_options[g_menu_select]->unsetColor();
g_menu_select++;
if ( static_cast<size_t>(g_menu_select) >= g_menu_options.size() )
if ( static_cast< size_t >( g_menu_select ) >= g_menu_options.size() )
g_menu_select = 0;
g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
g_menu_options[g_menu_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_w:
case SDLK_UP:
@ -490,7 +522,8 @@ void handleKeyDownMenu( SDL_Keycode key ) {
g_menu_select--;
if ( g_menu_select < 0 )
g_menu_select = g_menu_options.size() - 1;
g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
g_menu_options[g_menu_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_RETURN:
switch ( g_menu_select ) {
@ -501,12 +534,12 @@ void handleKeyDownMenu( SDL_Keycode key ) {
} break;
case MAIN_MENU_OPTIONS:
g_options_scene->updateSizeAndPosition();
g_active_scenes.push_back(g_options_scene);
g_input_functions.push_back(optionsSceneInput);
g_active_scenes.push_back( g_options_scene );
g_input_functions.push_back( optionsSceneInput );
break;
case MAIN_MENU_RESTART:
resetGame();
break;
break;
case MAIN_MENU_QUIT:
quitGame();
default:
@ -530,7 +563,7 @@ void pollEventsMenu() {
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
for(auto &x : g_active_scenes)
for ( auto &x : g_active_scenes )
x->updateSizeAndPosition();
}
default:
@ -539,7 +572,9 @@ void pollEventsMenu() {
}
}
void menuSceneInput( std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/, std::vector<std::shared_ptr<SDLPP::RenderObject>> &/*UNUSED*/ ) {
void menuSceneInput(
std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
std::vector< std::shared_ptr< SDLPP::RenderObject > > & /*UNUSED*/ ) {
pollEventsMenu();
}
@ -555,9 +590,11 @@ void handleKeyDownGameOver( SDL_Keycode key ) {
case SDLK_DOWN:
g_game_over_options[g_game_over_select]->unsetColor();
g_game_over_select++;
if ( static_cast<size_t>(g_game_over_select) >= g_game_over_options.size() )
if ( static_cast< size_t >( g_game_over_select ) >=
g_game_over_options.size() )
g_game_over_select = 0;
g_game_over_options[g_game_over_select]->setColor( colors["menu_item_background"] );
g_game_over_options[g_game_over_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_w:
case SDLK_UP:
@ -565,13 +602,14 @@ void handleKeyDownGameOver( SDL_Keycode key ) {
g_game_over_select--;
if ( g_game_over_select < 0 )
g_game_over_select = g_game_over_options.size() - 1;
g_game_over_options[g_game_over_select]->setColor( colors["menu_item_background"] );
g_game_over_options[g_game_over_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_RETURN:
switch ( g_game_over_select ) {
case GAME_OVER_RESTART:
resetGame();
break;
break;
case GAME_OVER_QUIT:
quitGame();
default:
@ -595,7 +633,7 @@ void pollEventsGameOver() {
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
for(auto &x : g_active_scenes)
for ( auto &x : g_active_scenes )
x->updateSizeAndPosition();
}
default:
@ -604,14 +642,16 @@ void pollEventsGameOver() {
}
}
void gameOverSceneInput( std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/, std::vector<std::shared_ptr<SDLPP::RenderObject>> &/*UNUSED*/ ) {
void gameOverSceneInput(
std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
std::vector< std::shared_ptr< SDLPP::RenderObject > > & /*UNUSED*/ ) {
pollEventsGameOver();
}
void saveOptions() {
g_update_colors = true;
if(g_cur_shadow)
g_cur_shadow->setHidden(!g_show_shadow);
if ( g_cur_shadow )
g_cur_shadow->setHidden( !g_show_shadow );
g_main_scene->setPrevTicks( SDL_GetTicks() );
}
@ -627,9 +667,11 @@ void handleKeyDownOptions( SDL_Keycode key ) {
case SDLK_DOWN:
g_options_options[g_options_select]->unsetColor();
g_options_select++;
if ( static_cast<size_t>(g_options_select) >= g_options_options.size() )
if ( static_cast< size_t >( g_options_select ) >=
g_options_options.size() )
g_options_select = 0;
g_options_options[g_options_select]->setColor( colors["menu_item_background"] );
g_options_options[g_options_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_w:
case SDLK_UP:
@ -637,26 +679,37 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_select--;
if ( g_options_select < 0 )
g_options_select = g_options_options.size() - 1;
g_options_options[g_options_select]->setColor( colors["menu_item_background"] );
g_options_options[g_options_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_RIGHT:
case SDLK_d:
switch( g_options_select ) {
switch ( g_options_select ) {
case OPTIONS_MENU_COLOR_SCHEME:
selected_color_scheme++;
if(static_cast<size_t>(selected_color_scheme) >= color_schemes_names.size())
if ( static_cast< size_t >( selected_color_scheme ) >=
color_schemes_names.size() )
selected_color_scheme = 0;
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_COLOR_SCHEME])->changeText("Color scheme: " + color_schemes_names[selected_color_scheme]);
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
g_options_options[OPTIONS_MENU_COLOR_SCHEME] )
->changeText( "Color scheme: " +
color_schemes_names[selected_color_scheme] );
g_update_colors = true;
break;
case OPTIONS_MENU_SHADOW:
g_show_shadow = !g_show_shadow;
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_SHADOW])->changeText(std::string("Show shadow: ") + (g_show_shadow ? "YES" : "NO"));
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
g_options_options[OPTIONS_MENU_SHADOW] )
->changeText( std::string( "Show shadow: " ) +
( g_show_shadow ? "YES" : "NO" ) );
g_update_colors = true;
break;
case OPTIONS_MENU_3D:
g_show_3d = !g_show_3d;
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_3D])->changeText(std::string("Show block texture: ") + (g_show_3d ? "YES" : "NO"));
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
g_options_options[OPTIONS_MENU_3D] )
->changeText( std::string( "Show block texture: " ) +
( g_show_3d ? "YES" : "NO" ) );
g_update_colors = true;
default:
break;
@ -664,22 +717,31 @@ void handleKeyDownOptions( SDL_Keycode key ) {
break;
case SDLK_LEFT:
case SDLK_a:
switch( g_options_select ) {
switch ( g_options_select ) {
case OPTIONS_MENU_COLOR_SCHEME:
if(selected_color_scheme == 0)
if ( selected_color_scheme == 0 )
selected_color_scheme = color_schemes_names.size();
selected_color_scheme--;
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_COLOR_SCHEME])->changeText("Color scheme: " + color_schemes_names[selected_color_scheme]);
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
g_options_options[OPTIONS_MENU_COLOR_SCHEME] )
->changeText( "Color scheme: " +
color_schemes_names[selected_color_scheme] );
g_update_colors = true;
break;
case OPTIONS_MENU_SHADOW:
g_show_shadow = !g_show_shadow;
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_SHADOW])->changeText(std::string("Show shadow: ") + (g_show_shadow ? "YES" : "NO"));
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
g_options_options[OPTIONS_MENU_SHADOW] )
->changeText( std::string( "Show shadow: " ) +
( g_show_shadow ? "YES" : "NO" ) );
g_update_colors = true;
break;
case OPTIONS_MENU_3D:
g_show_3d = !g_show_3d;
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_3D])->changeText(std::string("Show block texture: ") + (g_show_3d ? "YES" : "NO"));
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
g_options_options[OPTIONS_MENU_3D] )
->changeText( std::string( "Show block texture: " ) +
( g_show_3d ? "YES" : "NO" ) );
g_update_colors = true;
default:
break;
@ -718,7 +780,7 @@ void pollEventsOptions() {
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
for(auto &x : g_active_scenes)
for ( auto &x : g_active_scenes )
x->updateSizeAndPosition();
}
default:
@ -727,6 +789,8 @@ void pollEventsOptions() {
}
}
void optionsSceneInput( std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/, std::vector<std::shared_ptr<SDLPP::RenderObject>> &/*UNUSED*/ ) {
void optionsSceneInput(
std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
std::vector< std::shared_ptr< SDLPP::RenderObject > > & /*UNUSED*/ ) {
pollEventsOptions();
}

View File

@ -1,16 +1,32 @@
#ifndef TETRIS_MAIN_SCENE
#define TETRIS_MAIN_SCENE
#include "../sdlpp.hpp"
#include "../sdlpp/sdlpp.hpp"
std::shared_ptr<SDLPP::Scene> prepareMainScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font);
std::shared_ptr<SDLPP::Scene> prepareMenuScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font);
std::shared_ptr<SDLPP::Scene> prepareGameOverScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font);
std::shared_ptr<SDLPP::Scene> prepareOptionsScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font);
std::shared_ptr< SDLPP::Scene >
prepareMainScene( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Font > font );
std::shared_ptr< SDLPP::Scene >
prepareMenuScene( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Font > font );
std::shared_ptr< SDLPP::Scene >
prepareGameOverScene( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Font > font );
std::shared_ptr< SDLPP::Scene >
prepareOptionsScene( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Font > font );
void mainSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vector<std::shared_ptr<SDLPP::RenderObject>> &line_coliders );
void menuSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vector<std::shared_ptr<SDLPP::RenderObject>> &line_coliders );
void gameOverSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vector<std::shared_ptr<SDLPP::RenderObject>> &line_coliders );
void optionsSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vector<std::shared_ptr<SDLPP::RenderObject>> &line_coliders );
void mainSceneInput(
std::shared_ptr< SDLPP::Scene > scene, int base,
std::vector< std::shared_ptr< SDLPP::RenderObject > > &line_coliders );
void menuSceneInput(
std::shared_ptr< SDLPP::Scene > scene, int base,
std::vector< std::shared_ptr< SDLPP::RenderObject > > &line_coliders );
void gameOverSceneInput(
std::shared_ptr< SDLPP::Scene > scene, int base,
std::vector< std::shared_ptr< SDLPP::RenderObject > > &line_coliders );
void optionsSceneInput(
std::shared_ptr< SDLPP::Scene > scene, int base,
std::vector< std::shared_ptr< SDLPP::RenderObject > > &line_coliders );
#endif

View File

@ -1,4 +1,4 @@
#include "../sdlpp.hpp"
#include "../sdlpp/sdlpp.hpp"
#include "config.hpp"
#include "custom_classes.hpp"
#include "scenes.hpp"
@ -18,8 +18,9 @@
std::vector< std::shared_ptr< SDLPP::RenderObject > > line_coliders{};
void updateScore(std::shared_ptr<SDLPP::Font> font) {
g_score_texture->setText( *font, std::to_string( g_score ), colors["text"], colors["text_out"], 5 );
void updateScore( std::shared_ptr< SDLPP::Font > font ) {
g_score_texture->setText( *font, std::to_string( g_score ), colors["text"],
colors["text_out"], 5 );
}
void doInput() {
@ -30,43 +31,47 @@ void doInput() {
while ( !g_quit ) {
base = SDL_GetTicks();
SDL_framerateDelay( &gFPS );
g_input_functions.back()(g_main_scene, base, line_coliders);
g_input_functions.back()( g_main_scene, base, line_coliders );
}
}
void prepareShadowColider(std::shared_ptr<SDLPP::Renderer> r) {
g_shadow_colider = std::make_shared< SDLPP::RectangleRender >( 0, TOP_BORDER, BLOCK_SIZE, BOTTOM_BORDER - TOP_BORDER, r );
g_shadow_colider->addCollision(SDLPP::Rect( 0.1, 0.01, 0.8, 0.98 ));
void prepareShadowColider( std::shared_ptr< SDLPP::Renderer > r ) {
g_shadow_colider = std::make_shared< SDLPP::RectangleRender >(
0, TOP_BORDER, BLOCK_SIZE, BOTTOM_BORDER - TOP_BORDER, r );
g_shadow_colider->addCollision(
SDLPP::RectColider( 0.1, 0.01, 0.8, 0.98 ) );
g_shadow_colider->setId( COLIDER_ID );
g_shadow_colider->setStatic();
g_shadow_colider->centerX();
}
#ifdef _WIN32
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR szCmdLine, int nCmdShow) {
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR szCmdLine, int nCmdShow ) {
#else
int main() {
#endif
SDLPP::init();
SDLPP::Window w( "Tetris clone!" );
w.setResizable(true);
w.setResizable( true );
auto renderer = std::make_shared< SDLPP::Renderer >( w );
g_active_renderer = renderer;
renderer->setBlendMode( SDL_BLENDMODE_BLEND );
prepareShadowColider(renderer);
prepareShadowColider( renderer );
g_font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 );
g_main_scene = prepareMainScene(renderer, g_font);
line_coliders = g_main_scene->getObjects( {COLIDER_ID} );
g_score_texture = std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_main_scene->getObjects( {SCORE_TEXTURE_ID} )[0]);
g_main_scene = prepareMainScene( renderer, g_font );
line_coliders = g_main_scene->getObjects( { COLIDER_ID } );
g_score_texture = std::dynamic_pointer_cast< SDLPP::TextRenderer >(
g_main_scene->getObjects( { SCORE_TEXTURE_ID } )[0] );
g_active_scenes.push_back( g_main_scene );
g_main_scene->saveScene();
g_menu_scene = prepareMenuScene(renderer, g_font);
g_game_over_scene = prepareGameOverScene(renderer, g_font);
g_options_scene = prepareOptionsScene(renderer, g_font);
g_menu_scene = prepareMenuScene( renderer, g_font );
g_game_over_scene = prepareGameOverScene( renderer, g_font );
g_options_scene = prepareOptionsScene( renderer, g_font );
auto base = SDL_GetTicks();
int frames = 0;
@ -82,7 +87,7 @@ int main() {
g_next_object = g_tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
renderer, g_main_scene );
g_next_object->setPos( 0.9, 0.5 );
g_input_functions.push_back(mainSceneInput);
g_input_functions.push_back( mainSceneInput );
while ( !g_quit ) {
SDL_PumpEvents();
@ -90,8 +95,9 @@ int main() {
if ( !g_cur_object && g_checked_line ) {
std::lock_guard< std::mutex > guard( g_movement_mutex );
if ( !g_next_object ) {
g_next_object = g_tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
g_main_scene->getRendererShared(), g_main_scene );
g_next_object =
g_tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
g_main_scene->getRendererShared(), g_main_scene );
g_next_object->setPos( 0.9, 0.5 );
}
@ -99,11 +105,11 @@ int main() {
g_cur_object->setPos( 0.5, TOP_BORDER - BLOCK_SIZE );
g_cur_shadow = g_cur_object->copySelf();
g_cur_shadow->turnIntoShadow();
for(auto &piece : g_cur_shadow->getObjects()) {
for ( auto &piece : g_cur_shadow->getObjects() ) {
// make it so shadow gets covered by g_cur_object
g_main_scene->moveZ(piece, -4);
g_main_scene->moveZ( piece, -4 );
}
updateShadow(*g_main_scene);
updateShadow( *g_main_scene );
auto rand_index = std::rand() / ( ( RAND_MAX + 1u ) / 7 );
int retries = 0;
@ -114,17 +120,18 @@ int main() {
if ( retries == 7 ) {
g_game_over_scene->updateSizeAndPosition();
g_active_scenes.push_back( g_game_over_scene );
g_input_functions.push_back(gameOverSceneInput);
g_input_functions.push_back( gameOverSceneInput );
break;
}
}
g_next_object.reset();
g_next_object = g_tetrisFunctions[rand_index]( renderer, g_main_scene );
g_next_object =
g_tetrisFunctions[rand_index]( renderer, g_main_scene );
g_next_object->setPos( 0.9, 0.5 );
g_checked_line = false;
}
if ( g_update_score ) {
updateScore(g_font);
updateScore( g_font );
g_update_score = false;
}
if ( g_update_colors ) {
@ -133,8 +140,8 @@ int main() {
}
renderer->clearRenderer();
for(auto &x : g_active_scenes) {
x->renderScene(false);
for ( auto &x : g_active_scenes ) {
x->renderScene( false );
}
renderer->presentRenderer();
g_wait_for_anim = false;