diff --git a/doc/files_format.md b/doc/files_format.md index 7430c1d..55cdb20 100644 --- a/doc/files_format.md +++ b/doc/files_format.md @@ -37,6 +37,8 @@ _Repeat for every avaible actions._ The settings file has the following format: +- The versions of the file format, stored with 1 byte +- The previous maximum pieces size selected, stored with 1 byte - The number of the chosen keybinds (from 0 to 4), stored with 1 byte - The DAS of the player, stored with 1 byte - The ARR of the player, stored with 1 byte @@ -47,8 +49,11 @@ The settings file has the following format: - The last selected width of the board, stored with 1 byte - The last selected height of the board, stored with 1 byte - The uniformity mode (0 for default distribution, 1 for uniformous distribution, 2 for custom distribution), stored with 1 byte - - For each size, store the custom proportion (from 0 to 10) (15x1 byte total) + - For each size, store the custom proportion (from 0 to 20) (15x1 byte total) - Every selected pieces - For every groupe of piece (ALL, CONVEX, HOLELESS, OTHER), use 1 byte for the type (once again converted from an Enum) and 1 byte for the size - For every single piece, use 1 byte for (the size + encoding that it is a single piece), and 3 bytes to store the number of the piece (allows number up to 16M, size 15 has 6M pieces). + +The current file format version is 10. +If the file starts with a number lower than that, it will be regenerated upon launching the game. diff --git a/src/GraphicalUI/AppMenus/StartUpAppMenu.cpp b/src/GraphicalUI/AppMenus/StartUpAppMenu.cpp index efdb735..d33965f 100644 --- a/src/GraphicalUI/AppMenus/StartUpAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/StartUpAppMenu.cpp @@ -53,7 +53,7 @@ void StartUpAppMenu::drawFrame() const { text.setFillColor(sf::Color(0, 0, 0)); text.setOutlineColor(sf::Color(255, 255, 255)); - this->placeTitle(text, {}, "SELECT MAXIMUM PIECES SIZE", 10.f, {}); + this->placeTitle(text, {}, "SELECT THE LOADED PIECES MAXIMUM SIZE", 10.f, {}); this->placeTitle(text, this->playerCursor, "< " + std::to_string(this->playerCursor.getPosition().x) + " >", 25.f, this->playerCursor.getPosition()); text.setOutlineColor({0, 0, 0}); diff --git a/src/GraphicalUI/Settings.cpp b/src/GraphicalUI/Settings.cpp index 9c5ee00..ba546bc 100644 --- a/src/GraphicalUI/Settings.cpp +++ b/src/GraphicalUI/Settings.cpp @@ -13,44 +13,54 @@ static const sf::Vector2u BASE_WINDOW_SIZE = {80, 50}; static const int WINDOW_SIZE_MULTIPLIERS[] = {4, 6, 10, 14, 20, 30, 40}; static const int WINDOW_SIZE_LAST_MODE = (sizeof(WINDOW_SIZE_MULTIPLIERS) / sizeof(int)) - 1; static const int START_TIMER_MAX = 4; -static const int DISTRIBUTION_MAX = 10; +static const int DISTRIBUTION_MAX = 20; -Settings::Settings(int maximumPiecesSize) { - if (maximumPiecesSize < MINIMUM_PIECES_SIZE || maximumPiecesSize > LOADED_PIECES_SIZE) { - this->maximumPiecesSize = 0; - this->loadedPieces = false; - } - else { - bool loaded = true; - int i = 1; - while (loaded && (i <= maximumPiecesSize)) { - loaded = this->menu.getPiecesList().loadPieces(i); - i++; - } - - if (loaded) { - this->maximumPiecesSize = maximumPiecesSize; - this->loadedPieces = true; - } - else { - this->maximumPiecesSize = 0; - this->loadedPieces = false; - } - } - +Settings::Settings(bool loadPieces) { this->keybinds.reserve(NUMBER_OF_KEYBINDS); for (int i = 0; i < NUMBER_OF_KEYBINDS; i++) { this->keybinds.emplace_back(i); } - this->loadSettingsFromFile(); + this->loadSettingsFromFile(loadPieces); } -void Settings::loadSettingsFromFile() { +bool Settings::loadPieces(int maximumPiecesSizeRequest) { + if (maximumPiecesSizeRequest < MINIMUM_PIECES_SIZE) { + maximumPiecesSizeRequest = MINIMUM_PIECES_SIZE; + } + else if (maximumPiecesSizeRequest > LOADED_PIECES_SIZE || maximumPiecesSizeRequest > MAXIMUM_PIECES_SIZE) { + maximumPiecesSizeRequest = LOADED_PIECES_SIZE; + } + + bool succeeded = true; + int i = 1; + while (succeeded && (i <= maximumPiecesSizeRequest)) { + succeeded = this->menu.getPiecesList().loadPieces(i); + i++; + } + + this->maximumPiecesSize = (succeeded) ? maximumPiecesSizeRequest : 0; + return succeeded; + +} + +void Settings::loadSettingsFromFile(bool loadPieces) { std::ifstream settingsFile("data/config/settings.bin", std::ios::binary); char byte; + // file format version + settingsFile.get(byte); + + // maximum pieces size + settingsFile.get(byte); + if (loadPieces) { + this->loadedPieces = this->loadPieces(byte); + } + else { + this->loadedPieces = false; + } + // keybind layout settingsFile.get(byte); this->chosenKeybinds = byte; @@ -144,6 +154,14 @@ void Settings::saveSettingsToFile() const { std::ofstream settingsFile("data/config/settings.bin", std::ios::trunc | std::ios::binary); char byte; + // file format version + byte = CURRENT_FILE_FORMAT_VERSION; + settingsFile.write(&byte, 1); + + // maximum pieces size + byte = this->maximumPiecesSize; + settingsFile.write(&byte, 1); + // keybind layout byte = this->chosenKeybinds; settingsFile.write(&byte, 1); diff --git a/src/GraphicalUI/Settings.h b/src/GraphicalUI/Settings.h index e1c6897..acbea99 100644 --- a/src/GraphicalUI/Settings.h +++ b/src/GraphicalUI/Settings.h @@ -7,6 +7,8 @@ #include #include +static const int CURRENT_FILE_FORMAT_VERSION = 10; + static const int MAXIMUM_BOARD_WIDTH = 40; static const int MAXIMUM_BOARD_HEIGHT = 40; @@ -37,9 +39,13 @@ class Settings { std::vector distributions; public: - Settings(int maximumPiecesSize); + Settings(bool loadPieces); - void loadSettingsFromFile(); + private: + bool loadPieces(int maximumPiecesSizeRequest); + + public: + void loadSettingsFromFile(bool loadPieces = true); void saveSettingsToFile() const; diff --git a/src/GraphicalUI/main.cpp b/src/GraphicalUI/main.cpp index 16b7922..59c4487 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -19,10 +19,21 @@ int main() { pf.savePieces(i); } } + if (!std::filesystem::exists("data/config/settings.bin")) { std::cout << "settings file not found, generating..." << std::endl; resetSettingsFile(); } + else { + std::ifstream settingsFile("data/config/settings.bin", std::ios::binary); + char byte; + + settingsFile.get(byte); + if ((unsigned char) byte < CURRENT_FILE_FORMAT_VERSION) { + resetSettingsFile(); + } + } + for (int i = 0; i < NUMBER_OF_KEYBINDS; i++) { if (!std::filesystem::exists("data/config/keybinds/layout" + std::to_string(i) + ".bin")) { std::cout << "keybind file n°" << (i + 1) << "/" << NUMBER_OF_KEYBINDS << " not found, generating..." << std::endl; @@ -43,6 +54,14 @@ void resetSettingsFile() { Menu menu; + // file format version + byte = CURRENT_FILE_FORMAT_VERSION; + settingsFile.write(&byte, 1); + + // maximum pieces size + byte = MINIMUM_PIECES_SIZE; + settingsFile.write(&byte, 1); + // keybind layout byte = 0; settingsFile.write(&byte, 1);