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,7 +1,8 @@
#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", "#FF0000" },
{ "piece_brick_out", "#AA0000" }, { "piece_brick_out", "#AA0000" },
{ "piece_T", "#00FF00" }, { "piece_T", "#00FF00" },
@ -26,7 +27,8 @@ std::map<std::string, std::map<std::string, std::string>> color_schemes = {
{ "menu_background", "#000000BB" }, { "menu_background", "#000000BB" },
{ "menu_item_background", "#FFFFFF40" }, { "menu_item_background", "#FFFFFF40" },
} }, } },
{ "gruvbox_dark", { { "gruvbox_dark",
{
{ "piece_brick", "#cc241d" }, { "piece_brick", "#cc241d" },
{ "piece_brick_out", "#fb4934" }, { "piece_brick_out", "#fb4934" },
{ "piece_T", "#98971a" }, { "piece_T", "#98971a" },
@ -51,7 +53,8 @@ std::map<std::string, std::map<std::string, std::string>> color_schemes = {
{ "menu_background", "#282828BB" }, { "menu_background", "#282828BB" },
{ "menu_item_background", "#d5c4a180" }, { "menu_item_background", "#d5c4a180" },
} }, } },
{ "gruvbox_light", { { "gruvbox_light",
{
{ "piece_brick", "#cc241d" }, { "piece_brick", "#cc241d" },
{ "piece_brick_out", "#9d0006" }, { "piece_brick_out", "#9d0006" },
{ "piece_T", "#98971a" }, { "piece_T", "#98971a" },
@ -76,7 +79,8 @@ std::map<std::string, std::map<std::string, std::string>> color_schemes = {
{ "menu_background", "#d5c4a1BB" }, { "menu_background", "#d5c4a1BB" },
{ "menu_item_background", "#50494580" }, { "menu_item_background", "#50494580" },
} }, } },
{ "blackandwhite", { { "blackandwhite",
{
{ "piece_brick", "#FFFFFF" }, { "piece_brick", "#FFFFFF" },
{ "piece_brick_out", "#000000" }, { "piece_brick_out", "#000000" },
{ "piece_T", "#FFFFFF" }, { "piece_T", "#FFFFFF" },
@ -102,7 +106,9 @@ std::map<std::string, std::map<std::string, std::string>> color_schemes = {
{ "menu_item_background", "#FFFFFF40" }, { "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,7 +55,8 @@
#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 > >
color_schemes;
extern std::vector< std::string > color_schemes_names; 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;

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,9 +9,11 @@ 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;
@ -19,7 +21,13 @@ public:
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]++;
@ -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
@ -271,7 +278,8 @@ private:
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 );
} }

View File

@ -6,7 +6,8 @@
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;
} }
@ -30,11 +31,13 @@ void updateShadow(SDLPP::Scene &scene) {
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 )
@ -126,9 +129,12 @@ void resetGame() {
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(
*block, { BORDER_LEFT_ID, BORDER_RIGHT_ID, FLOOR_ID } );
for ( auto &col : collisions ) { for ( auto &col : collisions ) {
switch ( col->getId() ) { switch ( col->getId() ) {
case BORDER_LEFT_ID: case BORDER_LEFT_ID:
@ -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;
@ -173,10 +180,12 @@ bool checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene ) {
} }
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;
} }
@ -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 );
@ -397,11 +407,14 @@ void updateColors() {
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"] );
g_options_options[g_options_select]->setColor(
colors["menu_item_background"] );
for ( auto &x : g_menu_scene->getObjects( { MENU_BACKGROUND_ID } ) ) { for ( auto &x : g_menu_scene->getObjects( { MENU_BACKGROUND_ID } ) ) {
x->setColor( colors["menu_background"] ); x->setColor( colors["menu_background"] );
} }
@ -412,7 +425,7 @@ void updateColors() {
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,7 +1,7 @@
#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 );

View File

@ -36,7 +36,10 @@ 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::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< SDLPP::Scene > > g_active_scenes{};
std::vector< std::shared_ptr< TetrisPiece > ( * )( std::vector< std::shared_ptr< TetrisPiece > ( * )(

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,7 +42,10 @@ 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(
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< SDLPP::Scene > > g_active_scenes;
extern std::vector< std::shared_ptr< TetrisPiece > ( * )( extern std::vector< std::shared_ptr< TetrisPiece > ( * )(

View File

@ -20,9 +20,11 @@ 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 );
@ -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,7 +46,8 @@ 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 );
@ -54,7 +57,9 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
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 );
@ -62,7 +67,8 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
} }
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 );
@ -84,14 +90,16 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
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 );
@ -100,7 +108,7 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
// 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,49 +117,54 @@ 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 ) {
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 );
@ -168,7 +181,8 @@ void addMenuSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
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 =
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->setText( *font, "Options", colors["text"], colors["text_out"], 5 );
options->centerX(); options->centerX();
options->setId( MENU_ITEM_ID ); options->setId( MENU_ITEM_ID );
@ -190,9 +204,11 @@ void addMenuSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
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 );
@ -218,9 +234,11 @@ void addGameOverSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Rendere
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,7 +249,9 @@ 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 );
@ -239,14 +259,16 @@ void addOptionsSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer
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 );
@ -260,25 +282,33 @@ void addOptionsSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer
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;
@ -386,7 +416,6 @@ void handleKeyUpMain( SDL_Keycode key ) {
} }
} }
void pollEventsMain( SDLPP::Scene &scene ) { void pollEventsMain( SDLPP::Scene &scene ) {
SDL_Event event; SDL_Event event;
while ( SDLPP::getSDLEvent( event ) ) { while ( SDLPP::getSDLEvent( event ) ) {
@ -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 ) {
@ -482,7 +513,8 @@ void handleKeyDownMenu( SDL_Keycode key ) {
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 ) {
@ -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 ) {
@ -604,7 +642,9 @@ 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();
} }
@ -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;
@ -669,17 +722,26 @@ void handleKeyDownOptions( SDL_Keycode key ) {
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;
@ -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"
@ -19,7 +19,8 @@
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() {
@ -35,15 +36,18 @@ void doInput() {
} }
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
@ -60,7 +64,8 @@ int main() {
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();
@ -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 );
} }
@ -119,7 +125,8 @@ int main() {
} }
} }
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;
} }