Skip to content

Commit

Permalink
Merge pull request #3 from GlockPL/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
GlockPL authored Jul 15, 2022
2 parents e9e2a1b + 59e768a commit 53718a7
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 96 deletions.
11 changes: 2 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)
68 changes: 63 additions & 5 deletions src/Deposits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Deposits
std::vector<Block> deposits;
int playfield_width;
int playfield_height;
int a = 255;
int fade_speed = 15;

public:
Deposits(int playfield_width, int playfield_height);
Expand All @@ -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<int> 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<Block> newDeposits);
std::vector<int> findAllFullLines();
Block at(int i, int j);
~Deposits();
};
Expand Down Expand Up @@ -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<int> 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;
Expand All @@ -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;
Expand All @@ -91,6 +129,23 @@ void Deposits::moveLine(int from, int to) {
}
}

std::vector<int> Deposits::findAllFullLines()
{
std::vector<int> 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())
Expand All @@ -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)
Expand Down
21 changes: 14 additions & 7 deletions src/Grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <string>
#include <stdexcept>
#include <SDL2/SDL.h>

#include <Score.hpp>
#include "Structures.h"
#include "Deposits.hpp"
#include "Text.hpp"
Expand All @@ -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> 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;
Expand All @@ -33,11 +35,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<Block>(total_grid_height * total_grid_width, initialBlock);


// nextBlockText = Text(Fonts::BulkyPixel, 25, path_to_exec);// + "/"
Expand Down Expand Up @@ -101,9 +103,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;
}

Expand Down Expand Up @@ -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:", {.r = 255, .g = 255, .b = 255, .a = 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;
Expand Down Expand Up @@ -529,6 +535,7 @@ class Grid
std::shared_ptr<Tetromino> ghostTetr;
std::shared_ptr<Tetromino> nextTetr;
std::shared_ptr<Deposits> deposits;
std::shared_ptr<Score> score;
Text nextBlockText;
};

Expand Down
75 changes: 75 additions & 0 deletions src/Score.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#pragma once

#include <fstream>
#include <string>
#include <filesystem>


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;
}
6 changes: 3 additions & 3 deletions src/Structures.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
18 changes: 9 additions & 9 deletions src/Tetrominos/ITetromino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("0->1", {{{0,0}, {-2,0},{-1,0},{+2,+1},{-1,-2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("1->0", {{{0,0}, {-1,0},{+2,0},{-1,2},{2,-1}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("1->2", {{{0,0}, {-1,0},{2,0},{-1,2},{2,-1}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("2->1", {{{0,0}, {1,0},{-2,0},{1,-2},{-2,1}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("2->3", {{{0,0}, {2,0},{-1,0},{2,1},{-1,-2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("3->2", {{{0,0}, {-2,0},{1,0},{-2,-1},{1,2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("3->0", {{{0,0}, {1,0},{-2,0},{1,-2},{-2,1}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("0->3", {{{0,0}, {-1,0},{2,0},{-1,2},{2,-1}}}));
}

ITetromino::~ITetromino()
Expand Down
18 changes: 9 additions & 9 deletions src/Tetrominos/JTetromino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("0->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("1->0", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("1->2", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("2->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("2->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("3->2", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("3->0", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("0->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}}));

}

Expand Down
18 changes: 9 additions & 9 deletions src/Tetrominos/LTetromino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("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<std::string, std::array<KickPair, Tests>>("0->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("1->0", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("1->2", {{{0,0}, {1,0},{1,-1},{0,2},{1,2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("2->1", {{{0,0}, {-1,0},{-1,1},{0,-2},{-1,-2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("2->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("3->2", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("3->0", {{{0,0}, {-1,0},{-1,-1},{0,2},{-1,2}}}));
orderedKicks.insert(std::pair<std::string, std::array<KickPair, Tests>>("0->3", {{{0,0}, {1,0},{1,1},{0,-2},{1,-2}}}));
}

LTetromino::~LTetromino()
Expand Down
2 changes: 1 addition & 1 deletion src/Tetrominos/OTetromino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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})
{
}

Expand Down
Loading

0 comments on commit 53718a7

Please sign in to comment.