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 .PHONY: default
default: tetris default: tetris
tetris: tetris.o sdlpp.o scenes.o config.o functions.o global_vars.o tetris: tetris.o scenes.o config.o functions.o global_vars.o libsdlpp.so
$(CXX) $(CFLAGS) -o $@ $^ ${LDFLAGS} $(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 $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
tetris.o: tetris.cpp ../sdlpp.hpp config.hpp custom_classes.hpp scenes.hpp global_vars.hpp functions.hpp scenes.o: scenes.cpp ../sdlpp/sdlpp.hpp config.hpp scenes.hpp functions.hpp global_vars.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
scenes.o: scenes.cpp ../sdlpp.hpp config.hpp scenes.hpp functions.hpp global_vars.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
config.o: config.cpp config.hpp config.o: config.cpp config.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(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 $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
global_vars.o: global_vars.cpp config.hpp global_vars.hpp functions.hpp global_vars.o: global_vars.cpp config.hpp global_vars.hpp functions.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(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: clean:
rm -Rf *.o tetris rm -Rf *.o tetris

View File

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

View File

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

View File

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

View File

@ -4,47 +4,50 @@
#include "scenes.hpp" #include "scenes.hpp"
#include <thread> #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() ) { 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 ) if ( collisions.size() > 1 )
return false; return false;
} }
return true; return true;
} }
void updateShadow(SDLPP::Scene &scene) { void updateShadow( SDLPP::Scene &scene ) {
if(!g_cur_object) { if ( !g_cur_object ) {
g_cur_shadow->destroy(); g_cur_shadow->destroy();
g_cur_shadow.reset(); g_cur_shadow.reset();
return; return;
} }
if(!g_cur_shadow) if ( !g_cur_shadow )
return; return;
g_cur_shadow->setPos(g_cur_object->getPos()); g_cur_shadow->setPos( g_cur_object->getPos() );
double shadow_drop = BOTTOM_BORDER; double shadow_drop = BOTTOM_BORDER;
auto &invalid_objects = g_cur_object->getObjects(); 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(); 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; shadow_drop = BOTTOM_BORDER - block_pos.second;
// set colider column's position to current block's X position // set colider column's position to current block's X position
g_shadow_colider->setPos(block_pos.first, TOP_BORDER); g_shadow_colider->setPos( block_pos.first, TOP_BORDER );
auto collisions = scene.getCollisions( *g_shadow_colider, { BRICK_ID } ); auto collisions =
scene.getCollisions( *g_shadow_colider, { BRICK_ID } );
auto curY = block_pos.second; auto curY = block_pos.second;
for(auto &col : collisions) { for ( auto &col : collisions ) {
// if collision with g_cur_object, ignore // 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; continue;
auto possible_drop = col->getPos().second - curY; 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; shadow_drop = possible_drop;
} }
} }
// we want the shadow to rest on top of the nearest floor // we want the shadow to rest on top of the nearest floor
shadow_drop -= BLOCK_SIZE; shadow_drop -= BLOCK_SIZE;
auto shadow_pos = g_cur_shadow->getPos(); 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 ) { 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_ticks_till_fall > 0 ) {
if ( g_cur_object->isMoving() && g_ticks_till_movement <= 0 ) { if ( g_cur_object->isMoving() && g_ticks_till_movement <= 0 ) {
g_ticks_till_movement = TICKS_TILL_MOVE; g_ticks_till_movement = TICKS_TILL_MOVE;
g_cur_object->movePiece(movement.first * BLOCK_SIZE, 0); g_cur_object->movePiece( movement.first * BLOCK_SIZE, 0 );
if(!validPos(*scene, g_cur_object)) { if ( !validPos( *scene, g_cur_object ) ) {
g_cur_object->movePiece(movement.first * -BLOCK_SIZE, 0); g_cur_object->movePiece( movement.first * -BLOCK_SIZE, 0 );
return; return;
} else } else
goto check_floor; goto check_floor;
} }
if ( g_cur_object->isDescending() && g_ticks_till_descend <= 0 ) { if ( g_cur_object->isDescending() && g_ticks_till_descend <= 0 ) {
g_ticks_till_descend = TICKS_TILL_DESCEND; g_ticks_till_descend = TICKS_TILL_DESCEND;
g_cur_object->movePiece(0, movement.second * BLOCK_SIZE); g_cur_object->movePiece( 0, movement.second * BLOCK_SIZE );
if(!validPos(*scene, g_cur_object)) { if ( !validPos( *scene, g_cur_object ) ) {
g_cur_object->movePiece(0, movement.second * -BLOCK_SIZE); g_cur_object->movePiece( 0, movement.second * -BLOCK_SIZE );
return; return;
} else } else
goto check_floor; goto check_floor;
@ -80,7 +83,7 @@ void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
return; return;
} }
g_ticks_till_fall = TICKS_TILL_FALL; g_ticks_till_fall = TICKS_TILL_FALL;
g_cur_object->movePiece(0, BLOCK_SIZE); g_cur_object->movePiece( 0, BLOCK_SIZE );
check_floor: check_floor:
bool fell = false; bool fell = false;
for ( auto &x : g_cur_object->getObjects() ) { for ( auto &x : g_cur_object->getObjects() ) {
@ -92,18 +95,18 @@ check_floor:
} }
} }
if ( fell ) { if ( fell ) {
g_cur_object->movePiece(0, -BLOCK_SIZE); g_cur_object->movePiece( 0, -BLOCK_SIZE );
for ( auto &block : g_cur_object->getObjects() ) { for ( auto &block : g_cur_object->getObjects() ) {
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) { if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
g_game_over_scene->updateSizeAndPosition(); g_game_over_scene->updateSizeAndPosition();
g_active_scenes.push_back( g_game_over_scene ); g_active_scenes.push_back( g_game_over_scene );
g_input_functions.push_back(gameOverSceneInput); g_input_functions.push_back( gameOverSceneInput );
break; break;
} }
} }
g_cur_object.reset(); g_cur_object.reset();
} }
updateShadow(*scene); updateShadow( *scene );
} }
void quitGame() { void quitGame() {
@ -120,17 +123,20 @@ void resetGame() {
g_main_scene->resetScene(); g_main_scene->resetScene();
g_main_scene->setPrevTicks( SDL_GetTicks() ); g_main_scene->setPrevTicks( SDL_GetTicks() );
g_active_scenes = {g_main_scene}; g_active_scenes = { g_main_scene };
g_input_functions = {mainSceneInput}; g_input_functions = { mainSceneInput };
for(int i = 0; i < 7; i++) for ( int i = 0; i < 7; i++ )
g_bag[i] = 28; 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; int retFlags = 0;
auto collisions = scene.getCollisions(*block, {BORDER_LEFT_ID, BORDER_RIGHT_ID, FLOOR_ID}); auto collisions = scene.getCollisions(
for(auto &col : collisions) { *block, { BORDER_LEFT_ID, BORDER_RIGHT_ID, FLOOR_ID } );
switch(col->getId()) { for ( auto &col : collisions ) {
switch ( col->getId() ) {
case BORDER_LEFT_ID: case BORDER_LEFT_ID:
retFlags |= left; retFlags |= left;
break; break;
@ -143,12 +149,12 @@ int crashFlags( std::shared_ptr<TetrisPiece> piece, std::shared_ptr<TetrisBlock>
break; break;
} }
} }
collisions = scene.getCollisions(*block, {BRICK_ID}); collisions = scene.getCollisions( *block, { BRICK_ID } );
if(collisions.size() > 1) { if ( collisions.size() > 1 ) {
for(auto &col : collisions) { for ( auto &col : collisions ) {
if(piece->isLeft(*col)) if ( piece->isLeft( *col ) )
retFlags |= left; retFlags |= left;
else if(piece->isRight(*col)) else if ( piece->isRight( *col ) )
retFlags |= right; retFlags |= right;
else else
retFlags |= bottom; retFlags |= bottom;
@ -157,7 +163,8 @@ int crashFlags( std::shared_ptr<TetrisPiece> piece, std::shared_ptr<TetrisBlock>
return retFlags; 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 crash = 0;
int cur_left = 0x01; int cur_left = 0x01;
int cur_right = 0x02; int cur_right = 0x02;
@ -167,31 +174,33 @@ bool checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene ) {
int counter = 0; int counter = 0;
do { do {
counter++; counter++;
if(counter > 5) { if ( counter > 5 ) {
piece->revert(); piece->revert();
return false; return false;
} }
crash = 0; crash = 0;
for(auto &block : piece->getObjects()) { for ( auto &block : piece->getObjects() ) {
crash |= crashFlags(piece, block, scene, cur_left, cur_right, bottom); crash |=
crashFlags( piece, block, scene, cur_left, cur_right, bottom );
} }
if(crash & bottom || (crash & cur_left && crash & cur_right) || if ( crash & bottom || ( crash & cur_left && crash & cur_right ) ||
(crash & cur_left && crash & was_right) || (crash & cur_right && crash & was_left)) { ( crash & cur_left && crash & was_right ) ||
( crash & cur_right && crash & was_left ) ) {
piece->revert(); piece->revert();
return false; return false;
} }
crash &= ~(was_left | was_right); crash &= ~( was_left | was_right );
if(crash & cur_left) { if ( crash & cur_left ) {
piece->movePiece(BLOCK_SIZE, 0); piece->movePiece( BLOCK_SIZE, 0 );
crash &= ~cur_left; crash &= ~cur_left;
crash |= was_left; crash |= was_left;
} }
if(crash & cur_right) { if ( crash & cur_right ) {
piece->movePiece(-BLOCK_SIZE, 0); piece->movePiece( -BLOCK_SIZE, 0 );
crash &= ~cur_right; crash &= ~cur_right;
crash |= was_right; crash |= was_right;
} }
} while(crash); } while ( crash );
return true; return true;
} }
@ -200,10 +209,11 @@ createTetrisBlock( double x, double y, const std::string &color,
const std::string &outline, int index, const std::string &outline, int index,
std::shared_ptr< SDLPP::Renderer > renderer, std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) { std::shared_ptr< SDLPP::Scene > scene ) {
auto ret = std::make_shared< TetrisBlock >( x, y, BLOCK_SIZE, BLOCK_SIZE, auto ret =
renderer, color, true, index, scene, g_bag ); std::make_shared< TetrisBlock >( x, y, BLOCK_SIZE, BLOCK_SIZE, renderer,
color, true, index, scene, g_bag );
ret->setOutlineColor( outline ); 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->setId( BRICK_ID );
ret->centerX(); ret->centerX();
scene->addObject( ret ); scene->addObject( ret );
@ -381,38 +391,41 @@ tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
} }
void updateColors() { void updateColors() {
for(auto &x : g_main_scene->getObjects({BRICK_ID, SHADOW_ID})) { for ( auto &x : g_main_scene->getObjects( { BRICK_ID, SHADOW_ID } ) ) {
x->specialAction(PIECE_ACTION_UPDATE_COLOR); x->specialAction( PIECE_ACTION_UPDATE_COLOR );
} }
for(auto &x : g_main_scene->getObjects({BARRIER_ID})) { for ( auto &x : g_main_scene->getObjects( { BARRIER_ID } ) ) {
x->setColor(colors["barrier"]); x->setColor( colors["barrier"] );
} }
for(auto &x : g_main_scene->getObjects({BACKGROUND_ID})) { for ( auto &x : g_main_scene->getObjects( { BACKGROUND_ID } ) ) {
x->setColor(colors["background"]); x->setColor( colors["background"] );
} }
for(auto &x : g_main_scene->getObjects({SHADOW_ID})) { for ( auto &x : g_main_scene->getObjects( { SHADOW_ID } ) ) {
x->setColor(colors["shadow"]); x->setColor( colors["shadow"] );
} }
for(auto &x : g_main_scene->getObjects({LINE_ID})) { for ( auto &x : g_main_scene->getObjects( { LINE_ID } ) ) {
x->setColor(colors["line"]); x->setColor( colors["line"] );
} }
for(auto &x : g_main_scene->getObjects({TEXT_ID})) { 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); 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_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
g_game_over_options[g_game_over_select]->setColor(colors["menu_item_background"]); g_game_over_options[g_game_over_select]->setColor(
g_options_options[g_options_select]->setColor(colors["menu_item_background"]); colors["menu_item_background"] );
for(auto &x : g_menu_scene->getObjects({MENU_BACKGROUND_ID})) { g_options_options[g_options_select]->setColor(
x->setColor(colors["menu_background"]); 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})) { for ( auto &x : g_game_over_scene->getObjects( { MENU_BACKGROUND_ID } ) ) {
x->setColor(colors["menu_background"]); x->setColor( colors["menu_background"] );
} }
for(auto &x : g_options_scene->getObjects({MENU_BACKGROUND_ID})) { for ( auto &x : g_options_scene->getObjects( { MENU_BACKGROUND_ID } ) ) {
x->setColor(colors["menu_background"]); x->setColor( colors["menu_background"] );
} }
for(auto &x : g_options_scene->getObjects({MENU_ITEM_ID})) { 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"]); 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 #ifndef TETRIS_FUNCTIONS_H
#define TETRIS_FUNCTIONS_H #define TETRIS_FUNCTIONS_H
#include "../sdlpp.hpp" #include "../sdlpp/sdlpp.hpp"
#include "custom_classes.hpp" #include "custom_classes.hpp"
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ); void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks );
bool validPos(SDLPP::Scene &scene, std::shared_ptr<TetrisPiece> piece); bool validPos( SDLPP::Scene &scene, std::shared_ptr< TetrisPiece > piece );
void updateShadow(SDLPP::Scene &scene); void updateShadow( SDLPP::Scene &scene );
void quitGame(); void quitGame();
void resetGame(); 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 > std::shared_ptr< TetrisPiece >
tetrisBrick( std::shared_ptr< SDLPP::Renderer > renderer, 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{}; std::mutex g_movement_mutex{};
bool g_quit = false; 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::function< void(
std::vector<std::shared_ptr< SDLPP::Scene >> g_active_scenes{}; 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::vector< std::shared_ptr< TetrisPiece > ( * )(
std::shared_ptr< SDLPP::Renderer >, std::shared_ptr< SDLPP::Scene > ) > std::shared_ptr< SDLPP::Renderer >, std::shared_ptr< SDLPP::Scene > ) >
@ -46,4 +49,4 @@ std::vector< std::shared_ptr< TetrisPiece > ( * )(
tetrisLine, tetrisLLeft, tetrisZLeft, 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 #ifndef TETRIS_GLOBAL_VARS_H
#define TETRIS_GLOBAL_VARS_H #define TETRIS_GLOBAL_VARS_H
#include "../sdlpp.hpp" #include "../sdlpp/sdlpp.hpp"
#include "custom_classes.hpp" #include "custom_classes.hpp"
#include <functional> #include <functional>
@ -14,8 +14,10 @@ extern int g_menu_select;
extern int g_game_over_select; extern int g_game_over_select;
extern int g_options_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_menu_options;
extern std::vector< std::shared_ptr< SDLPP::RectangleRender > > g_game_over_options; extern std::vector< std::shared_ptr< SDLPP::RectangleRender > >
extern std::vector< std::shared_ptr< SDLPP::RectangleRender > > g_options_options; 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::TextRenderer > g_score_texture;
extern std::shared_ptr< SDLPP::Renderer > g_active_renderer; extern std::shared_ptr< SDLPP::Renderer > g_active_renderer;
extern int g_score; extern int g_score;
@ -40,13 +42,16 @@ extern std::shared_ptr< SDLPP::RectangleRender > g_shadow_colider;
extern std::mutex g_movement_mutex; extern std::mutex g_movement_mutex;
extern bool g_quit; 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::function< void(
extern std::vector<std::shared_ptr< SDLPP::Scene >> g_active_scenes; 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 > ( * )( extern std::vector< std::shared_ptr< TetrisPiece > ( * )(
std::shared_ptr< SDLPP::Renderer >, std::shared_ptr< SDLPP::Scene > ) > std::shared_ptr< SDLPP::Renderer >, std::shared_ptr< SDLPP::Scene > ) >
g_tetrisFunctions; g_tetrisFunctions;
extern std::shared_ptr<SDLPP::Font> g_font; extern std::shared_ptr< SDLPP::Font > g_font;
#endif #endif

View File

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

View File

@ -1,16 +1,32 @@
#ifndef TETRIS_MAIN_SCENE #ifndef TETRIS_MAIN_SCENE
#define 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 >
std::shared_ptr<SDLPP::Scene> prepareMenuScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font); prepareMainScene( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr<SDLPP::Scene> prepareGameOverScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font); 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 >
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 mainSceneInput(
void menuSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vector<std::shared_ptr<SDLPP::RenderObject>> &line_coliders ); std::shared_ptr< SDLPP::Scene > scene, int base,
void gameOverSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vector<std::shared_ptr<SDLPP::RenderObject>> &line_coliders ); 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 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 #endif

View File

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