From 503e7350fa3d8065e2f4814181664382154dc702 Mon Sep 17 00:00:00 2001 From: scratko Date: Mon, 18 Nov 2024 18:01:32 +0300 Subject: Random image selection Added a dialog box about. Checks if the selected file is correct. --- gameplay.cpp | 1 + main.cpp | 5 +++-- menu_callbacks.cpp | 35 +++++++++++++++++++++++++++++------ menu_callbacks.hpp | 1 + puzzle.cpp | 29 ++++++++++++++++++++++++++--- puzzle.hpp | 3 ++- 6 files changed, 62 insertions(+), 12 deletions(-) diff --git a/gameplay.cpp b/gameplay.cpp index 0c6a1ab..d6bf59f 100644 --- a/gameplay.cpp +++ b/gameplay.cpp @@ -62,6 +62,7 @@ void press_button_callback(Fl_Widget *w, void *params) void PuzzleGame::StartGame(GameParams *gp) { + gp->SelectRandomPicture(); gp->ResetFreePuzzles(); gp->CreateNewPuzzles(); gp->win->redraw(); diff --git a/main.cpp b/main.cpp index d5ce3a1..d1646e1 100644 --- a/main.cpp +++ b/main.cpp @@ -14,10 +14,11 @@ int main() srand(time(nullptr)); Fl_Window *win = new Fl_Window(320, 355, "Picture puzzle"); GameParams *params = GameParams::SetUpParams(win); - Fl_Sys_Menu_Bar *sys_bar = new Fl_Sys_Menu_Bar(0, 0, 150, 20, nullptr); + Fl_Sys_Menu_Bar *sys_bar = new Fl_Sys_Menu_Bar(0, 0, 355, 20, nullptr); + sys_bar->add("&File/&New game", nullptr, new_game_callback, params); sys_bar->add("&File/&Load file", nullptr, load_file_callback); sys_bar->add("&File/&Exit", nullptr, exit_callback); - sys_bar->add("&Help/&Show solution", nullptr, solve_problem_callback, + sys_bar->add("&Options/&Show solution", nullptr, solve_problem_callback, params); sys_bar->add("&About", nullptr, about_callback); PuzzleGame::StartGame(params); diff --git a/menu_callbacks.cpp b/menu_callbacks.cpp index a48b899..ffc1aae 100644 --- a/menu_callbacks.cpp +++ b/menu_callbacks.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -8,12 +9,26 @@ #include "gameplay.hpp" #include "img_handler.hpp" +void new_game_callback(Fl_Widget*, void *gp) +{ + PuzzleGame::StartGame(reinterpret_cast(gp)); +} + +static bool check_correct_path_to_img(const char *path) +{ + if(path == nullptr) + return false; + std::string p(path); + std::string ext = p.substr(p.find_last_of('.')); + return ext == ".png" || ext == ".jpg"; +} + void load_file_callback(Fl_Widget *sender, void*) { const char *path = nullptr; auto dialog = Fl_Native_File_Chooser{}; dialog.type(Fl_Native_File_Chooser::BROWSE_FILE); - dialog.filter("JPEG Files (*.jpg)\tPNG Files (*.png)"); + dialog.filter("JPEG Files\t*.jpg\nPNG Files\t*.png"); #if defined(_WIN32) dialog.directory((string {getenv("HOMEPATH")} + "\\Desktop").c_str()); #else @@ -23,10 +38,14 @@ void load_file_callback(Fl_Widget *sender, void*) Fl_Native_File_Chooser::NEW_FOLDER); if (dialog.show() == 0) path = dialog.filename(); - ImageHandler ih(path); - ih.load_img(); - ih.resize_img(); - ih.save_img(); + if(check_correct_path_to_img(path)) { + ImageHandler ih(path); + ih.load_img(); + ih.resize_img(); + ih.save_img(); + fl_message_title("Info"); + fl_message("Image successfully added"); + } } void exit_callback(Fl_Widget *w, void*) @@ -45,9 +64,13 @@ void solve_problem_callback(Fl_Widget *w, void *gp) PuzzleGame::StartGame(game); else game->win->hide(); - } void about_callback(Fl_Widget *w, void*) { + fl_message_title("About"); + fl_message("This app is devoted to my friends, to who I'm appreciative for" + " their support:\nIlya Korsak\nMaxim Fadeev\n\n" + "For all questions, please contact me:\n" + "m@@scratko.xyz"); } diff --git a/menu_callbacks.hpp b/menu_callbacks.hpp index 561263e..71dc72e 100644 --- a/menu_callbacks.hpp +++ b/menu_callbacks.hpp @@ -3,6 +3,7 @@ #include +void new_game_callback(Fl_Widget*, void *gp); void load_file_callback(Fl_Widget *sender, void*); void exit_callback(Fl_Widget *w, void*); void solve_problem_callback(Fl_Widget *w, void *gp); diff --git a/puzzle.cpp b/puzzle.cpp index 0d9dc11..3fbd026 100644 --- a/puzzle.cpp +++ b/puzzle.cpp @@ -1,11 +1,15 @@ #include "puzzle.hpp" #include "gameplay.hpp" +#include #include #include #include #include #include +#include +#include + GameParams* GameParams::instance = nullptr; @@ -28,10 +32,11 @@ void GameParams::ResetFreePuzzles() free_puzzles[i] = 1; } -static void find_path_to_picture(std::string& path, int number) +static void find_path_to_picture(std::string& path, + const std::string& cur_directory, int number) { path = - "resources/tucan/" + std::to_string(number / puzzles_per_side) + + cur_directory + std::to_string(number / puzzles_per_side) + std::to_string(number % puzzles_per_side) + ".png"; } @@ -66,7 +71,8 @@ void GameParams::NextUntestedPuzzles() std::unique_ptr(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, tmp_puzzle->sequence_number); + 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()); // TODO check fails img->fail(); tmp_puzzle->image(img); @@ -93,3 +99,20 @@ void GameParams::CreateNewPuzzles() NextUntestedPuzzles(); } while(!IsSolvability()); } + +void GameParams::SelectRandomPicture() +{ + std::vector 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]; +#if defined(_WIN32) + cur_directory.append("\\"); +#else + cur_directory.append("/"); +#endif +} diff --git a/puzzle.hpp b/puzzle.hpp index 9370763..e44efd4 100644 --- a/puzzle.hpp +++ b/puzzle.hpp @@ -38,14 +38,15 @@ private: coordinates empty_box; std::vector> puzzles; Fl_Window *win; + std::string cur_directory; GameParams(Fl_Window *a_win = nullptr) : win(a_win) {} - void CalculateStandardPuzzlePos(); void ResetFreePuzzles(); void NextUntestedPuzzles(); bool IsSolvability(); void CreateNewPuzzles(); + void SelectRandomPicture(); friend class PuzzleGame; friend class ASearch; -- cgit v1.2.3