You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.6 KiB
C++

#include "application.h"
#include "output_util.h"
#include "filepath_util.h"
#include <cstring>
#include <cctype>
/////////////////////////////////////////////////////////////////////////
static constexpr int DEFAULT_SCREEN_WIDTH = 1280;
static constexpr int DEFAULT_SCREEN_HEIGHT = 720;
static constexpr int DEFAULT_SPLITTING = 4;
static const std::string DEFAULT_PATH = "."; // current folder, I guess
/////////////////////////////////////////////////////////////////////////
std::tuple<int, int, sf::Vector2i, std::string> error(const char* msg)
{
std::cout << msg;
return {EXIT_FAILURE, -1, {}, {}};
}
std::tuple<int, int, sf::Vector2i, std::string> parseInput(int argc, char **argv)
{
int splitting = DEFAULT_SPLITTING;
sf::Vector2i resolution(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT);
std::string path = DEFAULT_PATH;
for (int current_arg = 1; current_arg < argc; ++current_arg) // current_arg = 0 is executable name
{
if (strcmp(argv[current_arg], output::HELP_FLAG) == 0) // --help
return error(output::HELP_MSG);
if (strcmp(argv[current_arg], output::SPLITTING_FLAG) == 0) // -s num
{
const int value_rg = current_arg + 1;
if (value_rg == argc) // is '-s' is the last argument
return error(output::SPLITTING_MSG);
splitting = -1; // here assuming user is providing it on their own
if (std::isdigit(*argv[value_rg]))
splitting = std::stoi(argv[value_rg]);
if (splitting < 2)
return error(output::SPLITTING_MSG);
++current_arg; // skipping value after flag to not check it once again
continue;
}
if (strcmp(argv[current_arg], output::RESOLUTION_FLAG) == 0) // -r numxnum
{
const int value_rg = current_arg + 1;
if (value_rg == argc) // is '-s' is the last argument
return error(output::RESOLUTION_MSG);
std::vector<std::string> res = filepath::split(argv[value_rg], 'x'); // splitting argument by 'x' as in 600x900
if (res.size() < 2)
return error(output::RESOLUTION_MSG);
resolution = {-1, -1};
if (std::isdigit(*res[0].c_str()) && std::isdigit(*res[1].c_str()))
resolution = {std::stoi(res[0].c_str()), std::stoi(res[1].c_str())};
if (resolution.x < 1 || resolution.y < 1)
return error(output::RESOLUTION_MSG);
++current_arg; // skipping value after flag to not check it once again
continue;
}
// nothing else, then assuming it's filepath or folderpath
const auto &[ret_code, ret_path] = filepath::parsePath(argv[current_arg]);
if (ret_code)
return error(output::IMG_FAIL_MSG);
path = ret_path;
}
if (path == DEFAULT_PATH)
{
// no path was give, loading random image from '.'
const auto &[ret_code, ret_path] = filepath::parsePath(path);
if (ret_code)
return error(output::IMG_FAIL_MSG);
path = ret_path;
}
return {EXIT_SUCCESS, splitting, resolution, path};
}
/////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
const auto&[ret_code, splitting, resolution, path] = parseInput(argc, argv);
if (ret_code) // Error code is EXIT_FAILURE
return ret_code;
Application app(resolution.x, resolution.y);
if (app.init(path, splitting))
{
app.run();
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}