From 2ccbb792ad06cdf3155c3e3fd28dde913cbd7e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kulczykowski?= Date: Thu, 7 Jul 2022 16:03:25 +0200 Subject: [PATCH 1/3] Changes to aling with MSVS --- CMakeLists.txt | 11 ++--------- src/Grid.hpp | 12 ++++++------ src/Structures.h | 6 +++--- src/Tetrominos/ITetromino.hpp | 18 +++++++++--------- src/Tetrominos/JTetromino.hpp | 18 +++++++++--------- src/Tetrominos/LTetromino.hpp | 18 +++++++++--------- src/Tetrominos/OTetromino.hpp | 2 +- src/Tetrominos/STetromino.hpp | 18 +++++++++--------- src/Tetrominos/TTetromino.hpp | 18 +++++++++--------- src/Tetrominos/ZTetromino.hpp | 18 +++++++++--------- src/main.cpp | 1 + 11 files changed, 67 insertions(+), 73 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 271bb58..4475453 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,16 +1,13 @@ cmake_minimum_required(VERSION 3.0.0) project(Tetris VERSION 0.1.0) -# include(CTest) -# enable_testing() - - find_package(SDL2 REQUIRED) # find_package(SDL2_TTF REQUIRED) include_directories(${SDL2_INCLUDE_DIRS}) include_directories(${SDL2_TTF_INCLUDE_DIRS}) -file(GLOB src/ CONFIGURE_DEPENDS "*.hpp") +include_directories(src/) +file(GLOB_RECURSE SOURCE_FILES CONFIGURE_DEPENDS src/*.hpp ) add_executable(Tetris src/main.cpp) @@ -19,7 +16,3 @@ target_link_libraries(Tetris SDL2 SDL2_ttf) set_target_properties(Tetris PROPERTIES OUTPUT_NAME "Tetris") target_compile_features(Tetris PRIVATE cxx_std_17) add_compile_options(-fsanitize=undefined) - -# set(CPACK_PROJECT_NAME ${PROJECT_NAME}) -# set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) -# include(CPack) diff --git a/src/Grid.hpp b/src/Grid.hpp index 81c9047..ee6faba 100644 --- a/src/Grid.hpp +++ b/src/Grid.hpp @@ -33,11 +33,11 @@ class Grid assert(total_grid_width > 1.5 * playfield_width); assert(total_grid_height > playfield_height + 2); - wallColors = {{{.r = 240, .g = 0, .b = 255, .a = 255}, {.r = 217, .g = 32, .b = 228, .a = 255}, {.r = 156, .g = 19, .b = 165, .a = 255}}}; + wallColors = {{{240, 0, 255, 255}, {217, 32, 228, 255}, {156, 19, 165, 255}}}; Block initialBlock; - blocks = std::vector(total_grid_height * total_grid_width, initialBlock); + blocks = std::vector(total_grid_height * total_grid_width, initialBlock); // nextBlockText = Text(Fonts::BulkyPixel, 25, path_to_exec);// + "/" @@ -101,9 +101,9 @@ class Grid Block block; block.blockType = BackgroundType; block.visible = false; - block.color1 = {.r = 255, .g = 255, .b = 255, .a = 255}; - block.color2 = {.r = 255, .g = 255, .b = 255, .a = 255}; - block.color3 = {.r = 238, .g = 238, .b = 238, .a = 255}; + block.color1 = {255, 255, 255, 255}; + block.color2 = {255, 255, 255, 255}; + block.color3 = {238, 238, 238, 255}; return block; } @@ -234,7 +234,7 @@ class Grid { // Add moved tetromino to grid // displayText(renderer); - nextBlockText.displayText((right_wall_pos + 2) * grid_size, grid_size, "Next Block:", {.r = 255, .g = 255, .b = 255, .a = 255}, renderer); + nextBlockText.displayText((right_wall_pos + 2) * grid_size, grid_size, "Next Block:", {255, 255, 255, 255}, renderer); int inner_size = 4; int border_size = 2; Uint8 val = 25; diff --git a/src/Structures.h b/src/Structures.h index 9865c1a..ba48fc9 100644 --- a/src/Structures.h +++ b/src/Structures.h @@ -26,9 +26,9 @@ struct Block int y = 0; BlockType blockType = BlockType::BackgroundType; bool visible = false; - SDL_Color color1 = {.r = 123, .g = 123, .b = 123}; - SDL_Color color2 = {.r = 123, .g = 123, .b = 123}; - SDL_Color color3 = {.r = 123, .g = 123, .b = 123}; + SDL_Color color1 = {123, 123, 123,255}; + SDL_Color color2 = {123, 123, 123,255 }; + SDL_Color color3 = {123, 123, 123,255 }; bool operator < (const Block& str) const { diff --git a/src/Tetrominos/ITetromino.hpp b/src/Tetrominos/ITetromino.hpp index 12c228b..61b75cc 100644 --- a/src/Tetrominos/ITetromino.hpp +++ b/src/Tetrominos/ITetromino.hpp @@ -10,16 +10,16 @@ class ITetromino : public Tetromino ~ITetromino(); }; -ITetromino::ITetromino() : Tetromino(4, 4, {{{.r = 72, .g = 224, .b = 255, .a = 255}, {.r = 49, .g = 199, .b = 239, .a = 255}, {.r = 32, .g = 176, .b = 224, .a = 255}}}, {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}) +ITetromino::ITetromino() : Tetromino(4, 4, {{{72, 224, 255, 255}, {49, 199, 239, 255}, {32, 176, 224, 255}}}, {0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}) { - orderedKicks.insert(std::pair>("0->1", {{{.x=0,.y=0}, {.x=-2,.y=0},{.x=-1,.y=0},{.x=+2,.y=+1},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("1->0", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=+2,.y=0},{.x=-1,.y=2},{.x=2,.y=-1}}})); - orderedKicks.insert(std::pair>("1->2", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=2,.y=0},{.x=-1,.y=2},{.x=2,.y=-1}}})); - orderedKicks.insert(std::pair>("2->1", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=-2,.y=0},{.x=1,.y=-2},{.x=-2,.y=1}}})); - orderedKicks.insert(std::pair>("2->3", {{{.x=0,.y=0}, {.x=2,.y=0},{.x=-1,.y=0},{.x=2,.y=1},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("3->2", {{{.x=0,.y=0}, {.x=-2,.y=0},{.x=1,.y=0},{.x=-2,.y=-1},{.x=1,.y=2}}})); - orderedKicks.insert(std::pair>("3->0", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=-2,.y=0},{.x=1,.y=-2},{.x=-2,.y=1}}})); - orderedKicks.insert(std::pair>("0->3", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=2,.y=0},{.x=-1,.y=2},{.x=2,.y=-1}}})); + orderedKicks.insert(std::pair>("0->1", {{{0,0}, {-2,0},{-1,0},{+2,+1},{-1,-2}}})); + orderedKicks.insert(std::pair>("1->0", {{{0,0}, {-1,0},{+2,0},{-1,2},{2,-1}}})); + orderedKicks.insert(std::pair>("1->2", {{{0,0}, {-1,0},{2,0},{-1,2},{2,-1}}})); + orderedKicks.insert(std::pair>("2->1", {{{0,0}, {1,0},{-2,0},{1,-2},{-2,1}}})); + orderedKicks.insert(std::pair>("2->3", {{{0,0}, {2,0},{-1,0},{2,1},{-1,-2}}})); + orderedKicks.insert(std::pair>("3->2", {{{0,0}, {-2,0},{1,0},{-2,-1},{1,2}}})); + orderedKicks.insert(std::pair>("3->0", {{{0,0}, {1,0},{-2,0},{1,-2},{-2,1}}})); + orderedKicks.insert(std::pair>("0->3", {{{0,0}, {-1,0},{2,0},{-1,2},{2,-1}}})); } ITetromino::~ITetromino() diff --git a/src/Tetrominos/JTetromino.hpp b/src/Tetrominos/JTetromino.hpp index 29d589e..92ac760 100644 --- a/src/Tetrominos/JTetromino.hpp +++ b/src/Tetrominos/JTetromino.hpp @@ -13,17 +13,17 @@ class JTetromino : public Tetromino ~JTetromino(); }; -JTetromino::JTetromino(/* args */) : Tetromino(3, 3, {{{.r = 102, .g = 128, .b = 104, .a = 255}, {.r = 90, .g = 101, .b = 173, .a = 255}, {.r = 72, .g = 87, .b = 160, .a = 255}}}, {0, 1, 0, 0, 1, 0, 1, 1, 0}) +JTetromino::JTetromino(/* args */) : Tetromino(3, 3, {{{102, 128, 104, 255}, {90,101,173, 255}, {72,87,160,255}}}, {0, 1, 0, 0, 1, 0, 1, 1, 0}) { - orderedKicks.insert(std::pair>("0->1", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=1},{.x=0,.y=-2},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("1->0", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=-1},{.x=0,.y=2},{.x=1,.y=2}}})); - orderedKicks.insert(std::pair>("1->2", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=-1},{.x=0,.y=2},{.x=1,.y=2}}})); - orderedKicks.insert(std::pair>("2->1", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=1},{.x=0,.y=-2},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("2->3", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=1},{.x=0,.y=-2},{.x=1,.y=-2}}})); - orderedKicks.insert(std::pair>("3->2", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=-1},{.x=0,.y=2},{.x=-1,.y=2}}})); - orderedKicks.insert(std::pair>("3->0", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=-1},{.x=0,.y=2},{.x=-1,.y=2}}})); - orderedKicks.insert(std::pair>("0->3", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=1},{.x=0,.y=-2},{.x=1,.y=-2}}})); + orderedKicks.insert(std::pair>("0->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}})); + orderedKicks.insert(std::pair>("1->0", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}})); + orderedKicks.insert(std::pair>("1->2", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}})); + orderedKicks.insert(std::pair>("2->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}})); + orderedKicks.insert(std::pair>("2->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}})); + orderedKicks.insert(std::pair>("3->2", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}})); + orderedKicks.insert(std::pair>("3->0", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}})); + orderedKicks.insert(std::pair>("0->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}})); } diff --git a/src/Tetrominos/LTetromino.hpp b/src/Tetrominos/LTetromino.hpp index 2829020..ab0940f 100644 --- a/src/Tetrominos/LTetromino.hpp +++ b/src/Tetrominos/LTetromino.hpp @@ -10,16 +10,16 @@ class LTetromino: public Tetromino ~LTetromino(); }; -LTetromino::LTetromino(/* args */): Tetromino(3, 3, {{{.r = 255, .g = 144, .b = 48, .a = 255}, {.r = 239, .g = 121, .b = 33, .a = 255}, {.r = 224, .g = 104, .b = 16, .a = 255}}}, {0, 1, 0, 0, 1, 0, 0, 1, 1}) +LTetromino::LTetromino(/* args */): Tetromino(3, 3, {{{255, 144, 48, 255}, {239, 121, 33, 255}, {224, 104, 16, 255}}}, {0, 1, 0, 0, 1, 0, 0, 1, 1}) { - orderedKicks.insert(std::pair>("0->1", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=1},{.x=0,.y=-2},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("1->0", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=-1},{.x=0,.y=2},{.x=1,.y=2}}})); - orderedKicks.insert(std::pair>("1->2", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=-1},{.x=0,.y=2},{.x=1,.y=2}}})); - orderedKicks.insert(std::pair>("2->1", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=1},{.x=0,.y=-2},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("2->3", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=1},{.x=0,.y=-2},{.x=1,.y=-2}}})); - orderedKicks.insert(std::pair>("3->2", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=-1},{.x=0,.y=2},{.x=-1,.y=2}}})); - orderedKicks.insert(std::pair>("3->0", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=-1},{.x=0,.y=2},{.x=-1,.y=2}}})); - orderedKicks.insert(std::pair>("0->3", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=1},{.x=0,.y=-2},{.x=1,.y=-2}}})); + orderedKicks.insert(std::pair>("0->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}})); + orderedKicks.insert(std::pair>("1->0", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}})); + orderedKicks.insert(std::pair>("1->2", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}})); + orderedKicks.insert(std::pair>("2->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}})); + orderedKicks.insert(std::pair>("2->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}})); + orderedKicks.insert(std::pair>("3->2", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}})); + orderedKicks.insert(std::pair>("3->0", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}})); + orderedKicks.insert(std::pair>("0->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}})); } LTetromino::~LTetromino() diff --git a/src/Tetrominos/OTetromino.hpp b/src/Tetrominos/OTetromino.hpp index fc1e35d..54df23e 100644 --- a/src/Tetrominos/OTetromino.hpp +++ b/src/Tetrominos/OTetromino.hpp @@ -9,7 +9,7 @@ class OTetromino : public Tetromino ~OTetromino(); }; -OTetromino::OTetromino() : Tetromino(2, 2, {{{.r = 255, .g = 236, .b = 28, .a = 255}, {.r = 247, .g = 211, .b = 8, .a = 255}, {.r = 232, .g = 192, .b = 0, .a = 255}}}, {1, 1, 1, 1}) +OTetromino::OTetromino() : Tetromino(2, 2, {{{255, 236, 28, 255}, {247, 211, 8, 255}, {232, 192, 0, 255}}}, {1, 1, 1, 1}) { } diff --git a/src/Tetrominos/STetromino.hpp b/src/Tetrominos/STetromino.hpp index 2867f98..12f7f96 100644 --- a/src/Tetrominos/STetromino.hpp +++ b/src/Tetrominos/STetromino.hpp @@ -9,16 +9,16 @@ class STetromino: public Tetromino ~STetromino(); }; -STetromino::STetromino(/* args */) : Tetromino(3, 3, {{{.r = 72, .g = 208, .b = 72, .a = 255}, {.r = 66, .g = 182, .b = 66, .a = 255}, {.r = 48, .g = 160, .b = 48, .a = 255}}}, {0, 1, 1, 1, 1, 0, 0, 0, 0}) +STetromino::STetromino(/* args */) : Tetromino(3, 3, {{{72, 208, 72, 255}, {66, 182, 66, 255}, {48, 160, 48, 255}}}, {0, 1, 1, 1, 1, 0, 0, 0, 0}) { - orderedKicks.insert(std::pair>("0->1", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=1},{.x=0,.y=-2},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("1->0", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=-1},{.x=0,.y=2},{.x=1,.y=2}}})); - orderedKicks.insert(std::pair>("1->2", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=-1},{.x=0,.y=2},{.x=1,.y=2}}})); - orderedKicks.insert(std::pair>("2->1", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=1},{.x=0,.y=-2},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("2->3", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=1},{.x=0,.y=-2},{.x=1,.y=-2}}})); - orderedKicks.insert(std::pair>("3->2", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=-1},{.x=0,.y=2},{.x=-1,.y=2}}})); - orderedKicks.insert(std::pair>("3->0", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=-1},{.x=0,.y=2},{.x=-1,.y=2}}})); - orderedKicks.insert(std::pair>("0->3", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=1},{.x=0,.y=-2},{.x=1,.y=-2}}})); + orderedKicks.insert(std::pair>("0->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}})); + orderedKicks.insert(std::pair>("1->0", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}})); + orderedKicks.insert(std::pair>("1->2", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}})); + orderedKicks.insert(std::pair>("2->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}})); + orderedKicks.insert(std::pair>("2->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}})); + orderedKicks.insert(std::pair>("3->2", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}})); + orderedKicks.insert(std::pair>("3->0", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}})); + orderedKicks.insert(std::pair>("0->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}})); } diff --git a/src/Tetrominos/TTetromino.hpp b/src/Tetrominos/TTetromino.hpp index e66dd62..ab5749f 100644 --- a/src/Tetrominos/TTetromino.hpp +++ b/src/Tetrominos/TTetromino.hpp @@ -9,16 +9,16 @@ class TTetromino: public Tetromino ~TTetromino(); }; -TTetromino::TTetromino(/* args */) : Tetromino(3, 3, {{{.r = 198, .g = 88, .b = 180, .a = 255}, {.r = 173, .g = 77, .b = 156, .a = 255}, {.r = 152, .g = 56, .b = 160, .a = 136}}}, {0, 1, 0, 1, 1, 1, 0, 0, 0}) +TTetromino::TTetromino(/* args */) : Tetromino(3, 3, {{{198, 88, 180, 255}, {173, 77, 156, 255}, {152, 56, 160, 136}}}, {0, 1, 0, 1, 1, 1, 0, 0, 0}) { - orderedKicks.insert(std::pair>("0->1", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=1},{.x=0,.y=-2},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("1->0", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=-1},{.x=0,.y=2},{.x=1,.y=2}}})); - orderedKicks.insert(std::pair>("1->2", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=-1},{.x=0,.y=2},{.x=1,.y=2}}})); - orderedKicks.insert(std::pair>("2->1", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=1},{.x=0,.y=-2},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("2->3", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=1},{.x=0,.y=-2},{.x=1,.y=-2}}})); - orderedKicks.insert(std::pair>("3->2", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=-1},{.x=0,.y=2},{.x=-1,.y=2}}})); - orderedKicks.insert(std::pair>("3->0", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=-1},{.x=0,.y=2},{.x=-1,.y=2}}})); - orderedKicks.insert(std::pair>("0->3", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=1},{.x=0,.y=-2},{.x=1,.y=-2}}})); + orderedKicks.insert(std::pair>("0->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}})); + orderedKicks.insert(std::pair>("1->0", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}})); + orderedKicks.insert(std::pair>("1->2", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}})); + orderedKicks.insert(std::pair>("2->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}})); + orderedKicks.insert(std::pair>("2->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}})); + orderedKicks.insert(std::pair>("3->2", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}})); + orderedKicks.insert(std::pair>("3->0", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}})); + orderedKicks.insert(std::pair>("0->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}})); } diff --git a/src/Tetrominos/ZTetromino.hpp b/src/Tetrominos/ZTetromino.hpp index 12e639f..8505c25 100644 --- a/src/Tetrominos/ZTetromino.hpp +++ b/src/Tetrominos/ZTetromino.hpp @@ -9,16 +9,16 @@ class ZTetromino: public Tetromino ~ZTetromino(); }; -ZTetromino::ZTetromino(/* args */) : Tetromino(3, 3, {{{.r = 255, .g = 72, .b = 72, .a = 255}, {.r = 239, .g = 32, .b = 41, .a = 255}, {.r = 208, .g = 24, .b = 24, .a = 255}}}, {1, 1, 0, 0, 1, 1, 0, 0, 0}) +ZTetromino::ZTetromino(/* args */) : Tetromino(3, 3, {{{255,72,72,255}, {239,32,41,255}, {208,24,24,255}}}, {1, 1, 0, 0, 1, 1, 0, 0, 0}) { - orderedKicks.insert(std::pair>("0->1", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=1},{.x=0,.y=-2},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("1->0", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=-1},{.x=0,.y=2},{.x=1,.y=2}}})); - orderedKicks.insert(std::pair>("1->2", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=-1},{.x=0,.y=2},{.x=1,.y=2}}})); - orderedKicks.insert(std::pair>("2->1", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=1},{.x=0,.y=-2},{.x=-1,.y=-2}}})); - orderedKicks.insert(std::pair>("2->3", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=1},{.x=0,.y=-2},{.x=1,.y=-2}}})); - orderedKicks.insert(std::pair>("3->2", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=-1},{.x=0,.y=2},{.x=-1,.y=2}}})); - orderedKicks.insert(std::pair>("3->0", {{{.x=0,.y=0}, {.x=-1,.y=0},{.x=-1,.y=-1},{.x=0,.y=2},{.x=-1,.y=2}}})); - orderedKicks.insert(std::pair>("0->3", {{{.x=0,.y=0}, {.x=1,.y=0},{.x=1,.y=1},{.x=0,.y=-2},{.x=1,.y=-2}}})); + orderedKicks.insert(std::pair>("0->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}})); + orderedKicks.insert(std::pair>("1->0", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}})); + orderedKicks.insert(std::pair>("1->2", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}})); + orderedKicks.insert(std::pair>("2->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}})); + orderedKicks.insert(std::pair>("2->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}})); + orderedKicks.insert(std::pair>("3->2", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}})); + orderedKicks.insert(std::pair>("3->0", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}})); + orderedKicks.insert(std::pair>("0->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}})); } ZTetromino::~ZTetromino() diff --git a/src/main.cpp b/src/main.cpp index 47677cc..33d224f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ + #include "Grid.hpp" #include "Deposits.hpp" From 8cded62e1a9bc595b62f61a52d0de41ac371c41f Mon Sep 17 00:00:00 2001 From: GlockPL Date: Fri, 8 Jul 2022 17:49:15 +0200 Subject: [PATCH 2/3] Line clear with fadeout --- src/Deposits.hpp | 68 ++++++++++++++++++++++++++++++++++++++++++++---- src/main.cpp | 35 ++++++++++++++----------- 2 files changed, 83 insertions(+), 20 deletions(-) diff --git a/src/Deposits.hpp b/src/Deposits.hpp index c91c9fe..7478f94 100644 --- a/src/Deposits.hpp +++ b/src/Deposits.hpp @@ -9,6 +9,8 @@ class Deposits std::vector deposits; int playfield_width; int playfield_height; + int a = 255; + int fade_speed = 10; public: Deposits(int playfield_width, int playfield_height); @@ -20,9 +22,13 @@ class Deposits bool lineFull(int i); void clearLine(int i); void clearLines(); + void changeAlpha(int i, int j, int alpha); + bool fadeLines(std::vector line); + void fadeLine(int i, int alpha); void moveLine(int from, int to); void addBlock(Block block, int i, int j); void swapDeposits(std::vector newDeposits); + std::vector findAllFullLines(); Block at(int i, int j); ~Deposits(); }; @@ -70,6 +76,37 @@ bool Deposits::lineFull(int i) return col == playfield_width; } +void Deposits::changeAlpha(int i, int j, int alpha) +{ + int offset = toOffset(i, j); + Block block = deposits[offset]; + block.color1.a = alpha; + block.color2.a = alpha; + block.color2.a = alpha; + deposits[offset] = block; +} + +void Deposits::fadeLine(int i, int alpha) +{ + for (int j = 0; j < getWidth(); j++) + { + changeAlpha(i, j, alpha); + } +} + +bool Deposits::fadeLines(std::vector line) +{ + + a -= fade_speed; + + for (int &line : line) // access by reference to avoid copying + { + fadeLine(line, a); + } + + return a <= 0; +} + void Deposits::clearLine(int i) { Block defaultBlock; @@ -79,9 +116,10 @@ void Deposits::clearLine(int i) deposits[toOffset(i, j)] = defaultBlock; } } -//This assumes that to is always bigger than from -void Deposits::moveLine(int from, int to) { - int diff=0; +// This assumes that to is always bigger than from +void Deposits::moveLine(int from, int to) +{ + int diff = 0; for (int j = 0; j < getWidth(); j++) { diff = to - from; @@ -91,6 +129,23 @@ void Deposits::moveLine(int from, int to) { } } +std::vector Deposits::findAllFullLines() +{ + std::vector fullLines; + if (!deposits.empty()) + { + for (int i = getHeight() - 1; i >= 0; i--) + { + if (lineFull(i)) + { + fullLines.push_back(i); + } + } + } + + return fullLines; +} + void Deposits::clearLines() { if (!deposits.empty()) @@ -106,11 +161,14 @@ void Deposits::clearLines() continue; } - if(lines > 0) { - moveLine(i, i+lines); + if (lines > 0) + { + moveLine(i, i + lines); } } } + + a = 255; } void Deposits::addBlock(Block block, int i, int j) diff --git a/src/main.cpp b/src/main.cpp index 33d224f..64f790a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,6 +62,7 @@ int main(int argc, char **argv) int tetrominoTypes = 7; int tetrominoTypeNum = rand() % tetrominoTypes; int x_push = 0; + int y_push = 0; int start_gravity = 10; int gravity = start_gravity; // 5 grid moves per second int count_frames = 0; @@ -69,6 +70,7 @@ int main(int argc, char **argv) bool newBlock = true; bool move = false; bool firstCheck = true; + std::vector linesToClear; const char *title = "Tetris::The Begining"; std::string path_to_exec = argv[0]; @@ -102,9 +104,12 @@ int main(int argc, char **argv) while (!close) { + y_push = 1; + if (newBlock) { - deposits->clearLines(); + + linesToClear = deposits->findAllFullLines(); tetrominoTypeNum = rand() % tetrominoTypes; // tetrominoTypeNum = 0; tetr = nextTetr; @@ -158,8 +163,10 @@ int main(int argc, char **argv) switch (event.key.keysym.sym) { case SDLK_d: + // x_push = 0; break; case SDLK_a: + // x_push = 0; break; case SDLK_e: break; @@ -176,6 +183,16 @@ int main(int argc, char **argv) } } + if (linesToClear.size() > 0) + { + y_push = 0; + if (deposits->fadeLines(linesToClear)) + { + deposits->clearLines(); + linesToClear.clear(); + } + } + if (grid.detectCollision(0, 0)) close = true; @@ -188,11 +205,11 @@ int main(int argc, char **argv) } } - if (!grid.detectCollision(0, 1)) + if (!grid.detectCollision(0, y_push)) { if (move) { - tetr->move(0, 1); + tetr->move(0, y_push); } } else @@ -213,16 +230,6 @@ int main(int argc, char **argv) grid.placeTetrominosOnGrid(); - if (renderer == NULL) - { - std::cout << "Renderer null" << std::endl; - } - - if (renderer == nullptr) - { - std::cout << "Renderer null" << std::endl; - } - grid.render(renderer); SDL_Delay(1000 / speed); @@ -237,8 +244,6 @@ int main(int argc, char **argv) count_frames++; move = false; } - - // std::cout << "Current gravity: " << gravity << std::endl; } SDL_DestroyWindow(window); From 59e768a4ab89672c3d47a06e03a4f505fc47e57b Mon Sep 17 00:00:00 2001 From: GlockPL Date: Fri, 15 Jul 2022 17:28:30 +0200 Subject: [PATCH 3/3] Added scoring System --- src/Deposits.hpp | 2 +- src/Grid.hpp | 11 +++++-- src/Score.hpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 7 +++-- 4 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 src/Score.hpp diff --git a/src/Deposits.hpp b/src/Deposits.hpp index 7478f94..81cb21e 100644 --- a/src/Deposits.hpp +++ b/src/Deposits.hpp @@ -10,7 +10,7 @@ class Deposits int playfield_width; int playfield_height; int a = 255; - int fade_speed = 10; + int fade_speed = 15; public: Deposits(int playfield_width, int playfield_height); diff --git a/src/Grid.hpp b/src/Grid.hpp index ee6faba..a48facc 100644 --- a/src/Grid.hpp +++ b/src/Grid.hpp @@ -10,6 +10,8 @@ #include #include #include + +#include #include "Structures.h" #include "Deposits.hpp" #include "Text.hpp" @@ -18,7 +20,7 @@ class Grid { public: - Grid(int screen_width, int screen_height, int grid_size, std::string path_to_exec) : window_width(screen_width), window_height(screen_height), grid_size(grid_size), path_to_exec(path_to_exec), nextBlockText(Text(Fonts::BulkyPixel, 25, path_to_exec)) + Grid(int screen_width, int screen_height, int grid_size, std::string path_to_exec, std::shared_ptr score) : window_width(screen_width), window_height(screen_height), grid_size(grid_size), path_to_exec(path_to_exec), nextBlockText(Text(Fonts::BulkyPixel, 25, path_to_exec)), score(score) { std::string path_to_font = path_to_exec + "/../assets/fonts/light_pixel-7.ttf"; playfield_height = 25; @@ -234,7 +236,11 @@ class Grid { // Add moved tetromino to grid // displayText(renderer); - nextBlockText.displayText((right_wall_pos + 2) * grid_size, grid_size, "Next Block:", {255, 255, 255, 255}, renderer); + nextBlockText.displayText((right_wall_pos + 2) * grid_size, grid_size, "Next Block:", {144, 238, 245, 255}, renderer); + nextBlockText.displayText(2 * grid_size, grid_size, "High Score:", {255, 15, 15, 255}, renderer); + nextBlockText.displayText(2 * grid_size, grid_size*2, std::to_string(score->getHighScore()) , {255, 255, 255, 255}, renderer); + nextBlockText.displayText(2 * grid_size, grid_size*3, "Score:", {255, 255, 15, 255}, renderer); + nextBlockText.displayText(2 * grid_size, grid_size*4, std::to_string(score->getCurrentScore()) , {255, 255, 255, 255}, renderer); int inner_size = 4; int border_size = 2; Uint8 val = 25; @@ -529,6 +535,7 @@ class Grid std::shared_ptr ghostTetr; std::shared_ptr nextTetr; std::shared_ptr deposits; + std::shared_ptr score; Text nextBlockText; }; diff --git a/src/Score.hpp b/src/Score.hpp new file mode 100644 index 0000000..f71043f --- /dev/null +++ b/src/Score.hpp @@ -0,0 +1,75 @@ +#pragma once + +#include +#include +#include + + +class Score +{ +private: + std::string save_file_path = "./score.dat"; + int highScore = 0; + int currentScore = 0; + int level = 1; +public: + Score(/* args */); + ~Score(); + int getHighScore(); + int getCurrentScore(); + void setHighScore(int score); + int calculateScore(int linesCleared); + +}; + +Score::Score(/* args */) +{ + if(std::filesystem::exists(save_file_path)) { + std::ifstream rf(save_file_path, std::ios::out | std::ios::binary); + rf.read((char *) &highScore, sizeof(int)); + } +} + + +Score::~Score() +{ + std::ofstream wf(save_file_path, std::ios::out | std::ios::binary); + wf.write((char *) &highScore, sizeof(int)); +} + +void Score::setHighScore(int score) { + highScore = score > highScore ? score : highScore; +} + +int Score::getHighScore() { + return highScore; +} + +int Score::getCurrentScore() { + return currentScore; +} + +int Score::calculateScore(int linesCleared) { + switch (linesCleared) + { + case 1: + currentScore += 100 * level; + break; + case 2: + currentScore += 300 * level; + break; + case 3: + currentScore += 500 * level; + break; + case 4: + currentScore += 800 * level; + break; + + default: + break; + } + + setHighScore(currentScore); + + return currentScore; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 64f790a..63ad95b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include "Grid.hpp" #include "Deposits.hpp" +#include "Score.hpp" #include "Tetrominos/ITetromino.hpp" #include "Tetrominos/JTetromino.hpp" @@ -63,7 +64,7 @@ int main(int argc, char **argv) int tetrominoTypeNum = rand() % tetrominoTypes; int x_push = 0; int y_push = 0; - int start_gravity = 10; + int start_gravity = 20; int gravity = start_gravity; // 5 grid moves per second int count_frames = 0; bool close = false; @@ -92,7 +93,8 @@ int main(int argc, char **argv) renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); - Grid grid = Grid(1000, 1000, 35, path_to_exec); + std::shared_ptr score = std::make_shared(); + Grid grid = Grid(1000, 1000, 35, path_to_exec, score); std::shared_ptr deposits = std::make_shared(grid.getPlayfieldWidth(), grid.getPlayfieldHeight()); grid.addDeposits(deposits); @@ -188,6 +190,7 @@ int main(int argc, char **argv) y_push = 0; if (deposits->fadeLines(linesToClear)) { + score->calculateScore(linesToClear.size()); deposits->clearLines(); linesToClear.clear(); }