diff options
author | scratko <m@scratko.xyz> | 2024-11-21 00:42:10 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2024-11-21 00:50:56 +0300 |
commit | 0b72fabadec642dcecfe861c56dfeddb0c82a898 (patch) | |
tree | 76d8a003db2c45bd0a94753ab921fc07d080f970 /puzzle.cpp | |
parent | 503e7350fa3d8065e2f4814181664382154dc702 (diff) | |
download | picture-puzzle-0b72fabadec642dcecfe861c56dfeddb0c82a898.tar.gz picture-puzzle-0b72fabadec642dcecfe861c56dfeddb0c82a898.tar.bz2 picture-puzzle-0b72fabadec642dcecfe861c56dfeddb0c82a898.zip |
Fixed memory leak
The image widget is responsible for deleting the loaded part of the image
Check for correct type of selected file
Error checking after image upload
Diffstat (limited to 'puzzle.cpp')
-rw-r--r-- | puzzle.cpp | 32 |
1 files changed, 28 insertions, 4 deletions
@@ -4,13 +4,15 @@ #include <algorithm> #include <stdlib.h> #include <string> +#include <string.h> +#include <FL/Fl.H> #include <FL/Fl_PNG_Image.H> +#include <FL/fl_ask.H> #include <utility> #include <memory> #include <filesystem> #include <random> - GameParams* GameParams::instance = nullptr; void GameParams::CalculateStandardPuzzlePos() @@ -47,8 +49,10 @@ void GameParams::NextUntestedPuzzles() /* * check if puzzles already were created */ - if(puzzles.size() != 0) + if(puzzles.size() != 0) { puzzles.clear(); + Fl::do_widget_deletion(); + } /* * ======== creating puzzles =========== */ @@ -74,13 +78,33 @@ void GameParams::NextUntestedPuzzles() 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(); + switch (img->fail()) { + case Fl_Image::ERR_NO_IMAGE: + case Fl_Image::ERR_FILE_ACCESS: + fl_alert("%s: %s", tmp_puzzle->path.c_str(), strerror(errno)); + exit(1); + case Fl_Image::ERR_FORMAT: + fl_alert("couldn't decode image"); + exit(1); + } tmp_puzzle->image(img); + tmp_puzzle->stored_img_pointer = img; tmp_puzzle->callback(press_button_callback, this); puzzles.push_back(std::move(tmp_puzzle)); } - win->end(); ResetFreePuzzles(); + /* + * Checking image shuffling + */ + bool shuffled_img = false; + for(int i = 0; i < puzzle_pieces-1; ++i) + if(puzzles[i]->sequence_number != i) { + shuffled_img = true; + break; + } + if(!shuffled_img) + NextUntestedPuzzles(); + win->end(); } bool GameParams::IsSolvability() |