diff --git a/include/Block.hpp b/include/Block.hpp new file mode 100644 index 0000000..1ba2a83 --- /dev/null +++ b/include/Block.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include + +class Block { + public: + BlockType* type; + sf::Vector2i position; + int rotation_state; + Block(); + std::vector get_tiles(); +}; \ No newline at end of file diff --git a/include/BlockType.hpp b/include/BlockType.hpp new file mode 100644 index 0000000..bb695d2 --- /dev/null +++ b/include/BlockType.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +class BlockType { + public: + static BlockType i, j, l, o, s, t, z; + static BlockType* list[]; + static BlockType* random() { + return list[rand() % 7]; + } + TileType* tile_type; + std::vector> grid; + uint width, height, starting_line; + bool rotate; + BlockType(TileType* _tile_type, const std::vector> _grid, bool _rotate = true); +}; \ No newline at end of file diff --git a/include/Config.hpp b/include/Config.hpp new file mode 100644 index 0000000..f8bbad4 --- /dev/null +++ b/include/Config.hpp @@ -0,0 +1,16 @@ +#define TILE_SIZE 20 + +#define GRID_WIDTH 14 +#define GRID_HEIGHT 20 + +#define WINDOW_WIDTH 500 +#define WINDOW_HEIGHT 440 + +#define PLAYFIELD_X 20 +#define PLAYFIELD_Y 20 + +#define LINES_PER_LEVEL 5 +#define POINTS_1_LINE 40 +#define POINTS_2_LINES 100 +#define POINTS_3_LINES 300 +#define POINTS_4_LINES 1200 \ No newline at end of file diff --git a/include/NumberRenderer.hpp b/include/NumberRenderer.hpp new file mode 100644 index 0000000..7ea4953 --- /dev/null +++ b/include/NumberRenderer.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +class NumberRenderer { + public: + sf::Texture texture; + sf::IntRect comma_rect; + sf::IntRect numeral_rects[10]; + NumberRenderer( + sf::Texture _texture, + sf::IntRect _comma_rect, + std::initializer_list _numeral_rects + ); + void render(sf::RenderWindow* window, uint number, int x, int y); + private: + sf::Sprite sprite; +}; \ No newline at end of file diff --git a/include/TileType.hpp b/include/TileType.hpp new file mode 100644 index 0000000..e339487 --- /dev/null +++ b/include/TileType.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include + +#include + +class TileType { + public: + sf::IntRect texture_rect; + sf::IntRect ghost_texture_rect; + TileType(sf::IntRect _texture_rect, sf::IntRect _ghost_texture_rect); +}; \ No newline at end of file diff --git a/src/Block.cpp b/src/Block.cpp new file mode 100644 index 0000000..83c9a67 --- /dev/null +++ b/src/Block.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +Block::Block() { + type = BlockType::random(); + position = sf::Vector2i(GRID_WIDTH / 2 - type->grid[0].size() / 2, 0); + rotation_state = 0; +} + +std::vector Block::get_tiles() { + std::vectortiles = {}; + for (int y = 0; y < type->grid.size(); y++) { + for (int x = 0; x < type->grid[y].size(); x++) { + if (!type->grid[y][x]) { + continue; + } + int rotated_x = x; + int rotated_y = y; + if (type->rotate) { + int center_x = type->grid[0].size() / 2; + int center_y = type->grid.size() / 2; + int offset_x = x - center_x; + int offset_y = y - center_y; + switch (rotation_state) { + case 0: + rotated_x = x; + rotated_y = y; + break; + case 1: + rotated_x = center_x + offset_y; + rotated_y = center_y - offset_x; + break; + case 2: + rotated_x = center_x - offset_x; + rotated_y = center_y - offset_y; + break; + case 3: + rotated_x = center_x - offset_y; + rotated_y = center_y + offset_x; + break; + default: + rotation_state %= 4; + } + } + int global_x = rotated_x + position.x; + int global_y = rotated_y + position.y; + tiles.push_back(sf::Vector2i(global_x, global_y)); + } + } + return tiles; +} \ No newline at end of file diff --git a/src/BlockType.cpp b/src/BlockType.cpp new file mode 100644 index 0000000..63e8682 --- /dev/null +++ b/src/BlockType.cpp @@ -0,0 +1,28 @@ +#include + +#include +#include + +BlockType::BlockType(TileType* _tile_type, const std::vector> _grid, bool _rotate) { + tile_type = _tile_type; + grid = _grid; + rotate = _rotate; + // Used for alignment in "next block" area + width = 0; + starting_line = 0; + for (uint y = 0; y < grid.size(); y++) { + bool has_content = false; + for (uint x = 0; x < grid[y].size(); x++) { + if (grid[y][x]) { + width = std::max({width, x + 1}); + has_content = true; + } + } + if (has_content) { + if (height == 0) { + starting_line = y; + } + height = y + 1 - starting_line; + } + } +} \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index 81dda38..981f93f 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -19,86 +19,11 @@ #include #include -#define TILE_SIZE 20 - -#define GRID_WIDTH 14 -#define GRID_HEIGHT 20 - -#define WINDOW_WIDTH 500 -#define WINDOW_HEIGHT 440 - -#define PLAYFIELD_X 20 -#define PLAYFIELD_Y 20 - -#define LINES_PER_LEVEL 5 -#define POINTS_1_LINE 40 -#define POINTS_2_LINES 100 -#define POINTS_3_LINES 300 -#define POINTS_4_LINES 1200 - -class NumberRenderer { - public: - sf::Texture texture; - sf::IntRect comma_rect; - sf::IntRect numeral_rects[10]; - NumberRenderer( - sf::Texture _texture, - sf::IntRect _comma_rect, - std::initializer_list _numeral_rects - ) { - texture = _texture; - comma_rect = _comma_rect; - sprite = sf::Sprite(texture); - int i = 0; - for (auto numeral_rect = _numeral_rects.begin(); numeral_rect != _numeral_rects.end(); ++numeral_rect) { - numeral_rects[i] = *numeral_rect; - i++; - } - } - void render(sf::RenderWindow* window, uint number, int x, int y) { - auto number_string = std::to_string(number); - std::string numeral_string; - numeral_string.push_back(number_string.back()); - auto numeral_rect = numeral_rects[std::stoi(numeral_string)]; - int x_offset = -numeral_rect.width; - uint digits = number_string.length(); - for (int i = digits - 1; i >= 0; i--) { - char numeral_string[] = {number_string[i]}; - auto numeral_rect = numeral_rects[std::stoi(numeral_string)]; - if ((digits - i) % 3 == 1 && i != digits - 1) { - sprite.setTextureRect(comma_rect); - sprite.setPosition(x + x_offset, y); - window->draw(sprite); - x_offset -= numeral_rect.width; - } - sprite.setTextureRect(numeral_rect); - sprite.setPosition(x + x_offset, y); - window->draw(sprite); - if (i == 0) { - break; - } - if ((digits - i) % 3 == 0) { - x_offset -= comma_rect.width; - continue; - } - numeral_string[0] = number_string[i - 1]; - numeral_rect = numeral_rects[std::stoi(numeral_string)]; - x_offset -= numeral_rect.width; - } - } - private: - sf::Sprite sprite; -}; - -class TileType { - public: - sf::IntRect texture_rect; - sf::IntRect ghost_texture_rect; - TileType(sf::IntRect _texture_rect, sf::IntRect _ghost_texture_rect) { - texture_rect = _texture_rect; - ghost_texture_rect = _ghost_texture_rect; - } -}; +#include +#include +#include +#include +#include TileType tile_type_0( sf::IntRect(0, 0, TILE_SIZE, TILE_SIZE), @@ -129,42 +54,6 @@ TileType tile_type_6( sf::IntRect(TILE_SIZE * 6, TILE_SIZE, TILE_SIZE, TILE_SIZE) ); -class BlockType { - public: - static BlockType i, j, l, o, s, t, z; - static BlockType* list[]; - static BlockType* random() { - return list[rand() % 7]; - } - TileType* tile_type; - std::vector> grid; - uint width, height, starting_line; - bool rotate; - BlockType(TileType* _tile_type, const std::vector> _grid, bool _rotate = true) { - tile_type = _tile_type; - grid = _grid; - rotate = _rotate; - // Used for alignment in "next block" area - width = 0; - starting_line = 0; - for (uint y = 0; y < grid.size(); y++) { - bool has_content = false; - for (uint x = 0; x < grid[y].size(); x++) { - if (grid[y][x]) { - width = std::max({width, x + 1}); - has_content = true; - } - } - if (has_content) { - if (height == 0) { - starting_line = y; - } - height = y + 1 - starting_line; - } - } - } -}; - // https://gamedev.stackexchange.com/a/17978 BlockType BlockType::i(&tile_type_0, { {0, 0, 0, 0}, @@ -203,60 +92,6 @@ BlockType BlockType::z(&tile_type_6, { }); BlockType* BlockType::list[] = {&i, &j, &l, &o, &s, &t, &z}; -class Block { - public: - BlockType* type; - sf::Vector2i position; - int rotation_state; - Block() { - type = BlockType::random(); - position = sf::Vector2i(GRID_WIDTH / 2 - type->grid[0].size() / 2, 0); - rotation_state = 0; - } - std::vector get_tiles() { - std::vectortiles = {}; - for (int y = 0; y < type->grid.size(); y++) { - for (int x = 0; x < type->grid[y].size(); x++) { - if (!type->grid[y][x]) { - continue; - } - int rotated_x = x; - int rotated_y = y; - if (type->rotate) { - int center_x = type->grid[0].size() / 2; - int center_y = type->grid.size() / 2; - int offset_x = x - center_x; - int offset_y = y - center_y; - switch (rotation_state) { - case 0: - rotated_x = x; - rotated_y = y; - break; - case 1: - rotated_x = center_x + offset_y; - rotated_y = center_y - offset_x; - break; - case 2: - rotated_x = center_x - offset_x; - rotated_y = center_y - offset_y; - break; - case 3: - rotated_x = center_x - offset_y; - rotated_y = center_y + offset_x; - break; - default: - rotation_state %= 4; - } - } - int global_x = rotated_x + position.x; - int global_y = rotated_y + position.y; - tiles.push_back(sf::Vector2i(global_x, global_y)); - } - } - return tiles; - } -}; - uint get_level(int lines) { return std::min(lines / LINES_PER_LEVEL, 15); } diff --git a/src/NumberRenderer.cpp b/src/NumberRenderer.cpp new file mode 100644 index 0000000..cdcc768 --- /dev/null +++ b/src/NumberRenderer.cpp @@ -0,0 +1,51 @@ +#include +#include + +#include + +NumberRenderer::NumberRenderer( + sf::Texture _texture, + sf::IntRect _comma_rect, + std::initializer_list _numeral_rects +) { + texture = _texture; + comma_rect = _comma_rect; + sprite = sf::Sprite(texture); + int i = 0; + for (auto numeral_rect = _numeral_rects.begin(); numeral_rect != _numeral_rects.end(); ++numeral_rect) { + numeral_rects[i] = *numeral_rect; + i++; + } +} + +void NumberRenderer::render(sf::RenderWindow* window, uint number, int x, int y) { + auto number_string = std::to_string(number); + std::string numeral_string; + numeral_string.push_back(number_string.back()); + auto numeral_rect = numeral_rects[std::stoi(numeral_string)]; + int x_offset = -numeral_rect.width; + uint digits = number_string.length(); + for (int i = digits - 1; i >= 0; i--) { + char numeral_string[] = {number_string[i]}; + auto numeral_rect = numeral_rects[std::stoi(numeral_string)]; + if ((digits - i) % 3 == 1 && i != digits - 1) { + sprite.setTextureRect(comma_rect); + sprite.setPosition(x + x_offset, y); + window->draw(sprite); + x_offset -= numeral_rect.width; + } + sprite.setTextureRect(numeral_rect); + sprite.setPosition(x + x_offset, y); + window->draw(sprite); + if (i == 0) { + break; + } + if ((digits - i) % 3 == 0) { + x_offset -= comma_rect.width; + continue; + } + numeral_string[0] = number_string[i - 1]; + numeral_rect = numeral_rects[std::stoi(numeral_string)]; + x_offset -= numeral_rect.width; + } +} \ No newline at end of file diff --git a/src/TileType.cpp b/src/TileType.cpp new file mode 100644 index 0000000..c3096f6 --- /dev/null +++ b/src/TileType.cpp @@ -0,0 +1,8 @@ +#include + +#include + +TileType::TileType(sf::IntRect _texture_rect, sf::IntRect _ghost_texture_rect) { + texture_rect = _texture_rect; + ghost_texture_rect = _ghost_texture_rect; +} \ No newline at end of file