Add multifolder support

This commit is contained in:
NaiJi ✨ 2021-04-24 23:46:31 +03:00
parent 93449bd39a
commit 9c9fd95ea9
6 changed files with 68 additions and 39 deletions

View File

@ -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)

View File

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

View File

@ -24,6 +24,7 @@ private:
int image_splitting;
sf::Vector2i game_resolution;
std::string image_path;
std::vector<std::string> loaded_image_pathes;
};
#endif // ARGSPROCESSOR_H

View File

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

View File

@ -25,7 +25,56 @@ namespace filepath
return std::equal(ending.rbegin(), ending.rend(), string.rbegin());
}
std::tuple<int, std::string> parsePath(const std::string &argv)
std::string randomChoice(const std::vector<std::string>& poll)
{
std::cout << "Loading random image!\n";
const auto range = poll.size() - 1;
return poll.at(rand() & range);
}
std::vector<std::tuple<std::filesystem::path, std::filesystem::file_status>> extractFilesFrom(std::filesystem::path&& path)
{
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);
return dir_items;
}
bool isImage(const std::filesystem::path& filepath)
{
static std::set<std::string> 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::tuple<int, std::vector<std::string>> parseFolder(std::filesystem::path&& path)
{
auto directory_items = extractFilesFrom(std::move(path));
std::vector<std::string> image_pathes;
for (const auto &[local_path, status] : directory_items)
{
if (isImage(local_path))
image_pathes.emplace_back(local_path.string());
}
if (image_pathes.empty())
return {EXIT_FAILURE, {}};
const std::tuple<int, std::vector<std::string>> empty_folder = {EXIT_FAILURE, {}};
const std::tuple<int, std::vector<std::string>> found_images = {EXIT_SUCCESS, image_pathes};
return image_pathes.empty()
? empty_folder
: found_images;
}
std::tuple<int, std::vector<std::string>> parsePath(const std::string &argv)
{
std::filesystem::path path(argv);
if (!std::filesystem::exists(path))
@ -34,38 +83,12 @@ namespace filepath
return {EXIT_FAILURE, {}};
}
// 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<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);
std::set<std::string> allowed_ext = {".bmp", ".dds", ".jpg", ".png", ".tga", ".psd"};
// Now getting images
std::vector<std::string> dir_image_items;
for (const auto &[local_path, status] : dir_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);
}
return {EXIT_SUCCESS, {path.string()}};
}
if (dir_image_items.empty())
{
std::cout << "No images found at " << path << "\n--help for more information.\n";
return {EXIT_FAILURE, {}};
}
std::cout << "Loading random image file from " << path << "\n";
srand(static_cast<unsigned int>(time(nullptr)));
return {EXIT_SUCCESS, dir_image_items[rand() & (dir_image_items.size() - 1)]};
return parseFolder(std::move(path));
}
std::vector<std::string> split(const std::string &s, char delim)

View File

@ -3,6 +3,8 @@
int main(int argc, char **argv)
{
srand(static_cast<unsigned int>(time(nullptr)));
ArgsProcessor args(argc, argv);
if (args.broken())