From 5a8fbddd93f3d3fb99ec3af5443963ff09977a48 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Sat, 19 Dec 2020 03:03:48 +0300 Subject: [PATCH] Add auto scaling --- application.cpp | 3 --- board.cpp | 38 +++++++++++++++++++++++++++++++------- board.h | 3 +++ filepath_util.h | 4 ++-- main.cpp | 2 +- output_util.h | 2 +- 6 files changed, 38 insertions(+), 14 deletions(-) diff --git a/application.cpp b/application.cpp index 33258d6..dc1a944 100644 --- a/application.cpp +++ b/application.cpp @@ -2,8 +2,6 @@ #include "application.h" // Hardcoded for now -constexpr int SCREEN_WIDTH = 1280; -constexpr int SCREEN_HEIGHT = 720; const sf::Time TIME_PER_SECOND = sf::seconds(1.f / 60.f); Application::Application() : @@ -31,7 +29,6 @@ void Application::draw() { render_window.clear(); board.draw(render_window); - render_window.setView(render_window.getDefaultView()); render_window.display(); } diff --git a/board.cpp b/board.cpp index 299090e..85c843f 100644 --- a/board.cpp +++ b/board.cpp @@ -164,13 +164,6 @@ bool Board::init(const std::string& path, int splitting) if (!global_texture.loadFromFile(path) ) return false; - if (splitting <= 1) // If it's 1, the game is already over - { - sf::Sprite* sp = new sf::Sprite(global_texture); - vec_field.push_back(new Cell{0, 0, sp}); - return true; // why not - } - const int width = global_texture.getSize().x; const int height = global_texture.getSize().y; @@ -201,6 +194,37 @@ bool Board::init(const std::string& path, int splitting) } } + // SCALING // + + float scaling = 0.; + if (width > height && width > SCREEN_WIDTH) + scaling = static_cast(SCREEN_WIDTH) / static_cast(width); + if (height > width && height > SCREEN_HEIGHT) + scaling = static_cast(SCREEN_HEIGHT) / static_cast(height); + + if (scaling != 0.) + { + int old_side_length = Cell::side_length; + Cell::side_length = static_cast(static_cast(Cell::side_length) * scaling); + int shift = Cell::side_length - old_side_length; + float move_x = 0.f; + float move_y = 0.f; + for (Cells::size_type i = 0; i < vec_field.size(); ++i) + { + move_x = 0.f; + move_y = 0.f; + if (!(((i % cells_on_width == 0) && (i >= cells_on_width)) || i == 0)) + move_x = static_cast(shift) * static_cast((i < cells_on_width) ? i : i % cells_on_width); + if (i >= cells_on_width) + move_y = static_cast(shift) * static_cast(i / cells_on_width); + + vec_field[i]->sprite->scale(scaling, scaling); + vec_field[i]->sprite->move(move_x, move_y); + } + + } + + // SHUFFLING // srand(static_cast(time(nullptr))); diff --git a/board.h b/board.h index ff4c4b7..cd22b84 100644 --- a/board.h +++ b/board.h @@ -8,6 +8,9 @@ using pos = std::pair; +constexpr int SCREEN_WIDTH = 1600; +constexpr int SCREEN_HEIGHT = 900; + enum class DIRECTION { UP, diff --git a/filepath_util.h b/filepath_util.h index 87f9ba6..f11f852 100644 --- a/filepath_util.h +++ b/filepath_util.h @@ -37,8 +37,8 @@ namespace filepath // Maybe user chose a specific image, not a folder if (std::filesystem::is_regular_file(path)) return {EXIT_SUCCESS, path.string()}; - - // So... it is a folder + // TO DO : I KNOW THIS PART IS BAD! I have never worked with ::filesystem before, + // So... it is a folder i will rewrite it when the prject works and is done // Creating a vector of everything in the given directory std::vector> dir_items; std::transform(std::filesystem::directory_iterator(path), {}, std::back_inserter(dir_items), filepath::getFileInfo); diff --git a/main.cpp b/main.cpp index e224e35..fea8335 100644 --- a/main.cpp +++ b/main.cpp @@ -69,7 +69,7 @@ static std::tuple parseInput(int argc, char **argv) if (std::isdigit(*argv[2])) splitting = std::stoi(argv[2]); - if (splitting < 1) + if (splitting < 2) return error(output::SPLITTING_MSG); const auto &[ret_code, ret_path] = filepath::parsePath(argc == MAX_ARGC ? argv[3] : DEFAULT_PATH); diff --git a/output_util.h b/output_util.h index 4577067..0088792 100644 --- a/output_util.h +++ b/output_util.h @@ -16,6 +16,6 @@ namespace output " by the smallest side of given source texture.\n" " Hence, if your image is square, the amount of tiles\n" " will be num * num.\n"; - const char* SPLITTING_MSG = "-s should be given with a positive num as in [-s num].\n"; + const char* SPLITTING_MSG = "-s should be given with a positive num >= 2 as in [-s num].\n"; const char* IMG_FAIL_MSG = "Couldn't load image from given path.\n"; }