Add auto scaling

This commit is contained in:
NaiJi ✨ 2020-12-19 03:03:48 +03:00
parent 6accec7c57
commit 5a8fbddd93
6 changed files with 38 additions and 14 deletions

View File

@ -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();
}

View File

@ -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<float>(SCREEN_WIDTH) / static_cast<float>(width);
if (height > width && height > SCREEN_HEIGHT)
scaling = static_cast<float>(SCREEN_HEIGHT) / static_cast<float>(height);
if (scaling != 0.)
{
int old_side_length = Cell::side_length;
Cell::side_length = static_cast<int>(static_cast<float>(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<float>(shift) * static_cast<float>((i < cells_on_width) ? i : i % cells_on_width);
if (i >= cells_on_width)
move_y = static_cast<float>(shift) * static_cast<float>(i / cells_on_width);
vec_field[i]->sprite->scale(scaling, scaling);
vec_field[i]->sprite->move(move_x, move_y);
}
}
// SHUFFLING //
srand(static_cast<unsigned int>(time(nullptr)));

View File

@ -8,6 +8,9 @@
using pos = std::pair<int, int>;
constexpr int SCREEN_WIDTH = 1600;
constexpr int SCREEN_HEIGHT = 900;
enum class DIRECTION
{
UP,

View File

@ -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<std::tuple<std::filesystem::path, std::filesystem::file_status>> dir_items;
std::transform(std::filesystem::directory_iterator(path), {}, std::back_inserter(dir_items), filepath::getFileInfo);

View File

@ -69,7 +69,7 @@ static std::tuple<int, int, std::string> 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);

View File

@ -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";
}