back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/puzzle.cpp
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-11-09 20:00:19 +0300
committerscratko <m@scratko.xyz>2024-11-09 20:12:48 +0300
commite64c2a9d94bb6e21ac24d7e1ff5915a29bbd67bf (patch)
tree5d42ff77afaeb101ff260ddf06b733cf0ef6fa61 /puzzle.cpp
parent365c27f05d5739213d3a98aa41dcc7f1cb0a6c60 (diff)
downloadpicture-puzzle-e64c2a9d94bb6e21ac24d7e1ff5915a29bbd67bf.tar.gz
picture-puzzle-e64c2a9d94bb6e21ac24d7e1ff5915a29bbd67bf.tar.bz2
picture-puzzle-e64c2a9d94bb6e21ac24d7e1ff5915a29bbd67bf.zip
Moving slides
Changed to 8 puzzles Added reaction to clicking a puzzle Puzzles are moved to vector
Diffstat (limited to 'puzzle.cpp')
-rw-r--r--puzzle.cpp29
1 files changed, 18 insertions, 11 deletions
diff --git a/puzzle.cpp b/puzzle.cpp
index 6d7d635..0e3574b 100644
--- a/puzzle.cpp
+++ b/puzzle.cpp
@@ -1,10 +1,13 @@
#include "puzzle.hpp"
+#include "gameplay.hpp"
#include <stdlib.h>
#include <string>
#include <FL/Fl_JPEG_Image.H>
+#include <utility>
+#include <memory>
-void GameParams::CalculatePuzzlePos()
+void GameParams::CalculateStandardPuzzlePos()
{
coordinates tmp;
@@ -15,7 +18,7 @@ void GameParams::CalculatePuzzlePos()
break;
tmp.x = i * (puzzle_size + spacing) + spacing;
tmp.y = j * (puzzle_size + spacing) + spacing;
- puzzle_coordinates[k] = tmp;
+ standard_puzzle_coordinates[k] = tmp;
}
}
@@ -35,29 +38,32 @@ static void find_path_to_picture(std::string& path, int number)
void GameParams::CreateNewPuzzles()
{
int idx_random_puzzle;
+ std::unique_ptr<Puzzle> tmp_puzzle;
/*
* check if puzzles already were created
*/
- for(int i = 0; i < puzzle_pieces; ++i)
- if(puzzles[i])
- delete puzzles[i];
+ if(puzzles.size() != 0)
+ puzzles.clear();
/*
* ======== creating puzzles ===========
*/
for(int i = 0; i < puzzle_pieces; ++i) {
- puzzles[i] =
- new Puzzle(puzzle_coordinates[i].x, puzzle_coordinates[i].y);
+ tmp_puzzle =
+ std::unique_ptr<Puzzle>(new Puzzle(standard_puzzle_coordinates[i].x,
+ standard_puzzle_coordinates[i].y));
idx_random_puzzle =
0 + (int)((double)puzzle_pieces * rand()/(RAND_MAX + 1.0));
while(!free_puzzles[idx_random_puzzle])
idx_random_puzzle =
0 + (int)((double)puzzle_pieces * rand()/(RAND_MAX + 1.0));
free_puzzles[idx_random_puzzle] = 0;
- puzzles[i]->sequence_number = idx_random_puzzle;
- find_path_to_picture(puzzles[i]->path, puzzles[i]->sequence_number);
- Fl_JPEG_Image *img = new Fl_JPEG_Image(puzzles[i]->path.c_str());
+ tmp_puzzle->sequence_number = idx_random_puzzle;
+ find_path_to_picture(tmp_puzzle->path, tmp_puzzle->sequence_number);
+ Fl_JPEG_Image *img = new Fl_JPEG_Image(tmp_puzzle->path.c_str());
// TODO check fails img->fail();
- puzzles[i]->image(img);
+ tmp_puzzle->image(img);
+ tmp_puzzle->callback(press_button_callback, this);
+ puzzles.push_back(std::move(tmp_puzzle));
}
}
@@ -65,4 +71,5 @@ void GameParams::NewGame()
{
ResetFreePuzzles();
CreateNewPuzzles();
+ SetXYEmptyBox(215, 215);
}