diff options
author | scratko <m@scratko.xyz> | 2024-11-21 23:01:56 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2024-11-21 23:01:56 +0300 |
commit | e9078b6980eaf22c1f6352930bd36d4a9e85a4b0 (patch) | |
tree | dea9e3659bd18dbd52dc3eedb55ac6acfcc14370 /puzzle.cpp | |
parent | ca449b813cef6c8f7348ff51302f125587a90a53 (diff) | |
download | picture-puzzle-master.tar.gz picture-puzzle-master.tar.bz2 picture-puzzle-master.zip |
Convert pictures to array data (image_converter/converter.c)
Resources are located in image_converter/resources.o
Diffstat (limited to 'puzzle.cpp')
-rw-r--r-- | puzzle.cpp | 58 |
1 files changed, 45 insertions, 13 deletions
@@ -1,5 +1,6 @@ #include "puzzle.hpp" #include "gameplay.hpp" +#include "image_converter/resources.hpp" #include <algorithm> #include <stdlib.h> @@ -42,6 +43,30 @@ static void find_path_to_picture(std::string& path, std::to_string(number % puzzles_per_side) + ".png"; } +Fl_PNG_Image *GameParams::LoadPictureParts(std::unique_ptr<Puzzle>& tmp_puzzle) +{ + Fl_PNG_Image *img = nullptr; + /* + * if loading from Resources dir + */ + if(cur_directory.size()) { + find_path_to_picture(tmp_puzzle->path, cur_directory, + tmp_puzzle->sequence_number); + img = new Fl_PNG_Image(tmp_puzzle->path.c_str()); + /* + * loading a standard image from RAM (image_converter/resources.cpp) + */ + } else { + int offset = 0; + for(int i = 0; i < tmp_puzzle->sequence_number; ++i) + offset += image_sizes[i]; + unsigned char *image_buffer = image_data + offset; + img = new Fl_PNG_Image(nullptr, image_buffer, + image_sizes[tmp_puzzle->sequence_number]); + } + return img; +} + void GameParams::NextUntestedPuzzles() { int idx_random_puzzle; @@ -75,9 +100,7 @@ void GameParams::NextUntestedPuzzles() std::unique_ptr<Puzzle>(new Puzzle(standard_puzzle_coordinates[i].x, standard_puzzle_coordinates[i].y)); tmp_puzzle->sequence_number = idx_random_puzzle; - find_path_to_picture(tmp_puzzle->path, cur_directory, - tmp_puzzle->sequence_number); - Fl_PNG_Image *img = new Fl_PNG_Image(tmp_puzzle->path.c_str()); + Fl_PNG_Image *img = LoadPictureParts(tmp_puzzle); switch (img->fail()) { case Fl_Image::ERR_NO_IMAGE: case Fl_Image::ERR_FILE_ACCESS: @@ -126,17 +149,26 @@ void GameParams::CreateNewPuzzles() void GameParams::SelectRandomPicture() { - std::vector<std::string> choices; - for (const auto& entry : std::filesystem::directory_iterator("resources")) { - choices.emplace_back(entry.path().string()); - } - std::random_device rd; - std::mt19937 g(rd()); - std::shuffle(choices.begin(), choices.end(), g); - cur_directory = choices[0]; + std::filesystem::path res_path = std::string("resources"); + bool res_path_exists = std::filesystem::exists(res_path); + if(res_path_exists) { + std::vector<std::string> choices; + for (const auto& entry : std::filesystem::directory_iterator("resources")) { + choices.emplace_back(entry.path().string()); + } + choices.emplace_back("standard"); + std::random_device rd; + std::mt19937 g(rd()); + std::shuffle(choices.begin(), choices.end(), g); + if(choices[0] == "standard") + cur_directory.clear(); + else { + cur_directory = choices[0]; #if defined(_WIN32) - cur_directory.append("\\"); + cur_directory.append("\\"); #else - cur_directory.append("/"); + cur_directory.append("/"); + } + } #endif } |