git repos / sago_multi_scrambler_puzzle2

commit 6258c28b

Poul Sander · 2023-12-25 20:37
6258c28b777cc4c7008906ae77d7a520e723abb2 patch · browse files
parent ac5cac2ff5deebec42eaf68f68ca8667a4139306

Started to create an image selector

Changed files

M src/ImageSelectState.cpp before
M src/ImageSelectState.hpp before
M src/sago_multi_scrambler_puzzle2.cpp before
diff --git a/src/ImageSelectState.cpp b/src/ImageSelectState.cpp index c3870a6..9b1eac3 100644 --- a/src/ImageSelectState.cpp +++ b/src/ImageSelectState.cpp
@@ -22,8 +22,51 @@ https://github.com/sago007/saland
*/
#include "ImageSelectState.hpp"
+#include <filesystem>
+#include <iostream>
+#include "globals.hpp"
+
+static void DrawRect(SDL_Renderer* target, int topx, int topy, int height, int width, const std::string& name) {
+ const int size = 32;
+ SDL_Rect bounds_ns = {topx, topy+size, width, height-2*size}; //bounds for south
+ SDL_Rect bounds_e = {topx, topy, width-size, height};
+ const sago::SagoSprite& n = globalData.spriteHolder->GetSprite(name+"n");
+ const sago::SagoSprite& s = globalData.spriteHolder->GetSprite(name+"s");
+ const sago::SagoSprite& e = globalData.spriteHolder->GetSprite(name+"e");
+ const sago::SagoSprite& w = globalData.spriteHolder->GetSprite(name+"w");
+ const sago::SagoSprite& fill = globalData.spriteHolder->GetSprite(name+"fill");
+ for (int i = 1; i < width/size; ++i) {
+ n.DrawBounded(target, SDL_GetTicks(), topx+i*size, topy, bounds_e);
+ for (int j = 1; j < height/size; ++j) {
+ w.DrawBounded(target, SDL_GetTicks(), topx, topy+j*size, bounds_ns);
+ fill.Draw(target, SDL_GetTicks(),topx+i*size, topy+j*size);
+ e.DrawBounded(target, SDL_GetTicks(), topx+width-size, topy+j*size, bounds_ns);
+ }
+ s.DrawBounded(target, SDL_GetTicks(), topx+i*size, topy+height-size, bounds_e);
+ }
+ //Corners
+ const sago::SagoSprite& nw = globalData.spriteHolder->GetSprite(name+"nw");
+ const sago::SagoSprite& ne = globalData.spriteHolder->GetSprite(name+"ne");
+ const sago::SagoSprite& se = globalData.spriteHolder->GetSprite(name+"se");
+ const sago::SagoSprite& sw = globalData.spriteHolder->GetSprite(name+"sw");
+ nw.Draw(target, SDL_GetTicks(), topx, topy);
+ ne.Draw(target, SDL_GetTicks(), topx+width-size, topy);
+ se.Draw(target, SDL_GetTicks(), topx+width-size, topy+height-size);
+ sw.Draw(target, SDL_GetTicks(), topx, topy+height-size);
+}
+
+static void DrawRectWhite(SDL_Renderer* target, int topx, int topy, int height, int width) {
+ std::string name = "ui_rect_white_";
+ DrawRect(target, topx, topy, height, width, name);
+}
+
+static void DrawRectYellow(SDL_Renderer* target, int topx, int topy, int height, int width) {
+ std::string name = "ui_rect_yellow_";
+ DrawRect(target, topx, topy, height, width, name);
+}
ImageSelectState::ImageSelectState() {
+
}
@@ -39,10 +82,60 @@ void ImageSelectState::ProcessInput(const SDL_Event& event, bool &processed) {
}
void ImageSelectState::Draw(SDL_Renderer* target) {
-
+ const int number_of_images_per_page = 6;
+ const int frame_size = 300;
+ const int frame_spacing = 20;
+ const int number_of_columns = 3;
+ const int top_x = (globalData.xsize-(3*frame_size+2*frame_spacing))/2;
+ const int top_y = (globalData.ysize-(2*frame_size+frame_spacing))/2;
+ for (int i = 0; i < number_of_images_per_page; ++i) {
+ int x = i % number_of_columns;
+ int y = i / number_of_columns;
+ DrawRectYellow(target, top_x+x*(frame_size+frame_spacing), top_y+y*(frame_size+frame_spacing), frame_size, frame_size);
+ }
}
void ImageSelectState::Update() {
+}
+
+/**
+ * @brief Compares two characters ignoring case
+ *
+ * @param a
+ * @param b
+ * @return true if the characters are equal ignoring case
+ * @return false if the characters are not equal ignoring case
+ */
+static bool ichar_equals(char a, char b) {
+ return std::tolower(static_cast<unsigned char>(a)) ==
+ std::tolower(static_cast<unsigned char>(b));
+}
+
+/**
+ * @brief Checks if the filename has the given extension. The comparison is case insensitive.
+ *
+ * @param filename The filename to check
+ * @param extension The extension to check for (example ".jpg")
+ * @return true if the filename has the given extension
+ * @return false if the filename does not have the given extension
+ */
+static bool HasExtension(const std::string& filename, const std::string& extension) {
+ if (filename.size() < extension.size()) {
+ return false;
+ }
+ return std::equal(extension.rbegin(), extension.rend(), filename.rbegin(), ichar_equals);
+}
+
+
+void ImageSelectState::Init() {
+ if (!folder.empty()) {
+ for (const auto& entry : std::filesystem::directory_iterator(folder)) {
+ if (entry.is_regular_file() && (HasExtension(entry.path().string(), ".jpg") || HasExtension(entry.path().string(), ".jpeg") || HasExtension(entry.path().string(), ".png") ) ) {
+ std::cout << entry.path() << std::endl;
+ imageList.push_back(entry.path().string());
+ }
+ }
+ }
}
\ No newline at end of file
diff --git a/src/ImageSelectState.hpp b/src/ImageSelectState.hpp index 406288a..3acca77 100644 --- a/src/ImageSelectState.hpp +++ b/src/ImageSelectState.hpp
@@ -22,6 +22,8 @@ https://github.com/sago007/saland
*/
#include "sago/GameStateInterface.hpp"
+#include <string>
+#include <vector>
#pragma once
@@ -36,6 +38,12 @@ public:
void ProcessInput(const SDL_Event& event, bool &processed) override;
void Draw(SDL_Renderer* target) override;
void Update() override;
+ void Init();
+
+ //Set to a filesystem folder to use that folder. Set to empty string to use collections.
+ std::string folder = "";
+ std::string collection = "";
+ std::vector<std::string> imageList;
private:
bool isActive = true;
diff --git a/src/sago_multi_scrambler_puzzle2.cpp b/src/sago_multi_scrambler_puzzle2.cpp index dff52de..9453697 100644 --- a/src/sago_multi_scrambler_puzzle2.cpp +++ b/src/sago_multi_scrambler_puzzle2.cpp
@@ -58,6 +58,16 @@ void runGame() {
void runCollection(const std::string& collection_name) {
ImageSelectState iss;
+ iss.collection = collection_name;
+ InitGame();
+ RunGameState(iss);
+ UninitGame();
+}
+
+void runFolder(const std::string& folder_name) {
+ ImageSelectState iss;
+ iss.folder = folder_name;
+ iss.Init();
InitGame();
RunGameState(iss);
UninitGame();
@@ -73,6 +83,7 @@ int main(int argc, const char* argv[]) {
("help,h", "Print basic usage information to stdout and quit")
("input-file", boost::program_options::value< std::vector<std::string> >(), "Image to open directly")
("collection", boost::program_options::value< std::string >(), "Jump straigt to a named collection. Like \"fairy_tales\"")
+ ("folder", boost::program_options::value< std::string >(), "Open a specific folder.")
;
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
@@ -96,6 +107,11 @@ int main(int argc, const char* argv[]) {
runCollection(collection_name);
return 0;
}
+ if (vm.count("folder")) {
+ const std::string& folder = vm["folder"].as<std::string>();
+ runFolder(folder);
+ return 0;
+ }
runGame();
return 0;
}