From 9c9fd95ea9771493d5fcf2de3162c3bdf614f2d0 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Sat, 24 Apr 2021 23:46:31 +0300 Subject: [PATCH] Add multifolder support --- CMakeLists.txt | 12 +++---- argsprocessor.cpp | 10 +++--- argsprocessor.h | 1 + board.cpp | 1 + filepath_util.h | 79 ++++++++++++++++++++++++++++++----------------- main.cpp | 2 ++ 6 files changed, 67 insertions(+), 38 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 103f5e1..131d60e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,12 +13,12 @@ set(SFML_LIB_DIR ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-graphics.so.2.5 ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-system.so.2.5 ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-window.so.2.5) -set(SFML_INCL_DIR ${CMAKE_SOURCE_DIR}/SFML-2.5.1/include) -include_directories(${SFML_INCL_DIR}) -add_executable(sliding-puzzle ${SOURCES} ${HEADER_FILES} ) -target_link_libraries(sliding-puzzle ${SFML_LIB_DIR}) +#set(SFML_INCL_DIR ${CMAKE_SOURCE_DIR}/SFML-2.5.1/include) +#include_directories(${SFML_INCL_DIR}) +#target_link_libraries(sliding-puzzle ${SFML_LIB_DIR}) # DYNAMIC # # You only need to install SFML from your package manager -#find_package(SFML REQUIRED graphics window system) -#target_link_libraries(sliding-puzzle sfml-system sfml-graphics sfml-network) +find_package(SFML REQUIRED graphics window system) +add_executable(sliding-puzzle ${SOURCES} ${HEADER_FILES} ) +target_link_libraries(sliding-puzzle sfml-system sfml-graphics sfml-network) diff --git a/argsprocessor.cpp b/argsprocessor.cpp index e716f03..e30f6fa 100644 --- a/argsprocessor.cpp +++ b/argsprocessor.cpp @@ -38,16 +38,18 @@ int ArgsProcessor::tryConvertInput(int argc, char **argv) if (error) return error; - if (image_path == DEFAULT_PATH) + if (image_path == DEFAULT_PATH && loaded_image_pathes.empty()) { // no path was given, loading random image from '.' const auto &[error, ret_path] = filepath::parsePath(image_path); if (error) return makeError(output::IMG_FAIL_MSG); - image_path = ret_path; + loaded_image_pathes.insert(loaded_image_pathes.end(), ret_path.begin(), ret_path.end()); } + image_path = filepath::randomChoice(loaded_image_pathes); + return EXIT_SUCCESS; } @@ -86,9 +88,9 @@ int ArgsProcessor::iterateArgc(int argc, char **argv) const auto &[error, ret_path] = filepath::parsePath(argv[current_arg]); if (error) - return makeError(output::IMG_FAIL_MSG); + continue; - image_path = ret_path; + loaded_image_pathes.insert(loaded_image_pathes.end(), ret_path.begin(), ret_path.end()); } return EXIT_SUCCESS; diff --git a/argsprocessor.h b/argsprocessor.h index 6d16e53..9e2d1a8 100644 --- a/argsprocessor.h +++ b/argsprocessor.h @@ -24,6 +24,7 @@ private: int image_splitting; sf::Vector2i game_resolution; std::string image_path; + std::vector loaded_image_pathes; }; #endif // ARGSPROCESSOR_H diff --git a/board.cpp b/board.cpp index 9671c1d..19f6624 100644 --- a/board.cpp +++ b/board.cpp @@ -28,6 +28,7 @@ Board::~Board() bool Board::init(const std::string& path, int splitting, const sf::RenderWindow &window) { + std::cout << path << '\n'; if (!global_texture.loadFromFile(path) ) return false; diff --git a/filepath_util.h b/filepath_util.h index d4fc857..044e5a2 100644 --- a/filepath_util.h +++ b/filepath_util.h @@ -25,47 +25,70 @@ namespace filepath return std::equal(ending.rbegin(), ending.rend(), string.rbegin()); } - std::tuple parsePath(const std::string &argv) + std::string randomChoice(const std::vector& poll) { - std::filesystem::path path(argv); - if (!std::filesystem::exists(path)) - { - std::cout << "Path " << path << " does not exist.\n"; - return {EXIT_FAILURE, {}}; - } + std::cout << "Loading random image!\n"; + const auto range = poll.size() - 1; + return poll.at(rand() & range); + } - // Maybe user chose a specific image, not a folder - if (std::filesystem::is_regular_file(path)) - return {EXIT_SUCCESS, path.string()}; - // 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> extractFilesFrom(std::filesystem::path&& path) + { std::vector> dir_items; std::transform(std::filesystem::directory_iterator(path), {}, std::back_inserter(dir_items), filepath::getFileInfo); + return dir_items; + } + + bool isImage(const std::filesystem::path& filepath) + { + static std::set allowed_ext = {".bmp", ".jpg", "*.jpeg", ".png"}; + const std::string path_string = filepath.string(); + + return std::filesystem::is_regular_file(filepath) && + std::any_of(allowed_ext.begin(), allowed_ext.end(), + [&](const std::string& e) + { + return filepath::endsWith(path_string, e); + }); + } - std::set allowed_ext = {".bmp", ".dds", ".jpg", ".png", ".tga", ".psd"}; + std::tuple> parseFolder(std::filesystem::path&& path) + { + auto directory_items = extractFilesFrom(std::move(path)); - // Now getting images - std::vector dir_image_items; - for (const auto &[local_path, status] : dir_items) + std::vector image_pathes; + for (const auto &[local_path, status] : directory_items) { - const std::string str_path = local_path.string(); - if (std::filesystem::is_regular_file(local_path) && - std::any_of(allowed_ext.begin(), allowed_ext.end(), [&](const std::string& e) { return filepath::endsWith(str_path, e); })) - { - dir_image_items.emplace_back(str_path); - } + if (isImage(local_path)) + image_pathes.emplace_back(local_path.string()); } - if (dir_image_items.empty()) + if (image_pathes.empty()) + return {EXIT_FAILURE, {}}; + + const std::tuple> empty_folder = {EXIT_FAILURE, {}}; + const std::tuple> found_images = {EXIT_SUCCESS, image_pathes}; + + return image_pathes.empty() + ? empty_folder + : found_images; + } + + std::tuple> parsePath(const std::string &argv) + { + std::filesystem::path path(argv); + if (!std::filesystem::exists(path)) { - std::cout << "No images found at " << path << "\n--help for more information.\n"; + std::cout << "Path " << path << " does not exist.\n"; return {EXIT_FAILURE, {}}; } - std::cout << "Loading random image file from " << path << "\n"; - srand(static_cast(time(nullptr))); - return {EXIT_SUCCESS, dir_image_items[rand() & (dir_image_items.size() - 1)]}; + if (std::filesystem::is_regular_file(path)) + { + return {EXIT_SUCCESS, {path.string()}}; + } + + return parseFolder(std::move(path)); } std::vector split(const std::string &s, char delim) diff --git a/main.cpp b/main.cpp index f8d5c0e..c3b04ef 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,8 @@ int main(int argc, char **argv) { + srand(static_cast(time(nullptr))); + ArgsProcessor args(argc, argv); if (args.broken())