fichier settings
This commit is contained in:
@@ -38,7 +38,7 @@ _Repeat for every avaible actions._
|
|||||||
The settings file has the following format:
|
The settings file has the following format:
|
||||||
|
|
||||||
- The number of the chosen keybinds (from 0 to 4), stored with 1 byte
|
- The number of the chosen keybinds (from 0 to 4), stored with 1 byte
|
||||||
- The size multiplier of the window, stored with 1 byte
|
- The window size mode, stored with 1 byte
|
||||||
- The number of the last selected gamemode (converted from an Enum), stored with 1 byte
|
- The number of the last selected gamemode (converted from an Enum), stored with 1 byte
|
||||||
- The last selected width of the board, stored with 1 byte
|
- The last selected width of the board, stored with 1 byte
|
||||||
- The last selected height of the board, stored with 1 byte
|
- The last selected height of the board, stored with 1 byte
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "MainAppMenu.h"
|
#include "MainAppMenu.h"
|
||||||
|
|
||||||
#include "AppMenu.h"
|
#include "AppMenu.h"
|
||||||
|
#include "InGameAppMenu.h"
|
||||||
|
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -14,7 +15,7 @@ MainAppMenu::MainAppMenu(std::shared_ptr<std::stack<AppMenu>> menuStack, std::sh
|
|||||||
|
|
||||||
void MainAppMenu::computeFrame() {
|
void MainAppMenu::computeFrame() {
|
||||||
if (sf::Keyboard::isKeyPressed(sfKey::Enter)) {
|
if (sf::Keyboard::isKeyPressed(sfKey::Enter)) {
|
||||||
|
this->menuStack->push(InGameAppMenu(this->menuStack, this->settings, this->renderWindow));
|
||||||
}
|
}
|
||||||
else if (sf::Keyboard::isKeyPressed(sfKey::Escape)) {
|
else if (sf::Keyboard::isKeyPressed(sfKey::Escape)) {
|
||||||
this->menuStack->pop();
|
this->menuStack->pop();
|
||||||
|
|||||||
@@ -8,13 +8,17 @@
|
|||||||
|
|
||||||
using sfKey = sf::Keyboard::Key;
|
using sfKey = sf::Keyboard::Key;
|
||||||
|
|
||||||
|
static const int NUMBER_OF_KEYBINDS = 5;
|
||||||
|
static const int CUSTOMIZABLE_KEYBINDS = NUMBER_OF_KEYBINDS - 1;
|
||||||
|
|
||||||
|
|
||||||
class Keybinds {
|
class Keybinds {
|
||||||
private:
|
private:
|
||||||
std::map<Action, std::vector<sfKey>> keybinds;
|
std::map<Action, std::vector<sfKey>> keybinds;
|
||||||
|
int layoutNumber;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Keybinds();
|
Keybinds(int layoutNumber);
|
||||||
|
|
||||||
void loadKeybindsFromFile();
|
void loadKeybindsFromFile();
|
||||||
|
|
||||||
|
|||||||
@@ -2,20 +2,20 @@
|
|||||||
|
|
||||||
|
|
||||||
enum PiecesType {
|
enum PiecesType {
|
||||||
CONVEX,
|
CONVEX_PIECES,
|
||||||
HOLELESS,
|
HOLELESS_PIECES,
|
||||||
OTHERS,
|
OTHER_PIECES,
|
||||||
ALL,
|
ALL_PIECES,
|
||||||
SINGLE
|
SINGLE_PIECE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
inline int getSizeOfPieces(PiecesType type) {
|
inline int getSizeOfPieces(PiecesType type) {
|
||||||
if (type < SINGLE) return 0;
|
if (type < SINGLE_PIECE) return 0;
|
||||||
|
|
||||||
else return (type - SINGLE + 1);
|
else return (type - SINGLE_PIECE + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline PiecesType createSinglePieceType(int size) {
|
inline PiecesType createSinglePieceType(int size) {
|
||||||
return PiecesType(SINGLE + size - 1);
|
return PiecesType(SINGLE_PIECE + size - 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,31 +4,99 @@
|
|||||||
#include "Keybinds.h"
|
#include "Keybinds.h"
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
static const int NUMBER_OF_KEYBINDS = 5;
|
|
||||||
static const int CUSTOMIZABLE_KEYBINDS = NUMBER_OF_KEYBINDS - 1;
|
|
||||||
static const sf::Vector2u BASE_WINDOW_SIZE = {80, 50};
|
static const sf::Vector2u BASE_WINDOW_SIZE = {80, 50};
|
||||||
static const int WINDOW_SIZE_MULTIPLIERS[] = {4, 6, 9, 14, 20};
|
static const int WINDOW_SIZE_MULTIPLIERS[] = {4, 6, 9, 14, 20};
|
||||||
static const int WINDOW_SIZE_LAST_MODE = (sizeof(WINDOW_SIZE_MULTIPLIERS) / sizeof(int)) - 1;
|
static const int WINDOW_SIZE_LAST_MODE = (sizeof(WINDOW_SIZE_MULTIPLIERS) / sizeof(int)) - 1;
|
||||||
|
|
||||||
|
|
||||||
Settings::Settings() {
|
Settings::Settings() {
|
||||||
for (int i = 1; i <= 15; i++) {
|
for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) {
|
||||||
this->menu.getPiecesList().loadPieces(i);
|
this->menu.getPiecesList().loadPieces(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this->keybinds.reserve(NUMBER_OF_KEYBINDS);
|
||||||
|
for (int i = 0; i < NUMBER_OF_KEYBINDS; i++) {
|
||||||
|
this->keybinds.emplace_back(i);
|
||||||
|
}
|
||||||
|
|
||||||
this->loadSettingsFromFile();
|
this->loadSettingsFromFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::loadSettingsFromFile() {
|
void Settings::loadSettingsFromFile() {
|
||||||
|
std::ifstream settingsFile("data/config/settings.bin", std::ios::binary);
|
||||||
|
char byte;
|
||||||
|
|
||||||
|
// keybind layout
|
||||||
|
settingsFile.get(&byte, 1);
|
||||||
|
this->chosenKeybinds = byte;
|
||||||
|
|
||||||
|
// window size mode
|
||||||
|
settingsFile.get(&byte, 1);
|
||||||
|
this->windowSizeMode = byte;
|
||||||
|
|
||||||
|
// gamemode
|
||||||
|
settingsFile.get(&byte, 1);
|
||||||
|
this->gamemode = Gamemode(byte);
|
||||||
|
|
||||||
|
// board width
|
||||||
|
settingsFile.get(&byte, 1);
|
||||||
|
this->menu.setBoardWidth(byte);
|
||||||
|
|
||||||
|
// board height
|
||||||
|
settingsFile.get(&byte, 1);
|
||||||
|
this->menu.setBoardHeight(byte);
|
||||||
|
|
||||||
|
// piece distribution
|
||||||
|
settingsFile.get(&byte, 1);
|
||||||
//TODO
|
//TODO
|
||||||
this->menu.getPiecesList().unselectAll();
|
|
||||||
this->menu.getPiecesList().selectAllPieces(4);
|
// selected pieces
|
||||||
this->windowSizeMode = 2;
|
char pieceType;
|
||||||
|
char pieceValue;
|
||||||
|
this->selectedPieces.clear();
|
||||||
|
while (settingsFile.get(&pieceType, 1)) {
|
||||||
|
settingsFile.get(&pieceValue, 1);
|
||||||
|
this->selectedPieces.push_back({PiecesType(pieceType), pieceValue});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::saveSettingsToFile() const {
|
void Settings::saveSettingsToFile() const {
|
||||||
|
std::ofstream settingsFile("data/config/settings.bin", std::ios::trunc | std::ios::binary);
|
||||||
|
char byte;
|
||||||
|
|
||||||
|
// keybind layout
|
||||||
|
byte = this->chosenKeybinds;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// window size mode
|
||||||
|
byte = this->windowSizeMode;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// gamemode
|
||||||
|
byte = this->gamemode;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// board width
|
||||||
|
byte = this->menu.getBoardWidth();
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// board height
|
||||||
|
byte = this->menu.getBoardHeight();
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// piece distribution
|
||||||
//TODO
|
//TODO
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// selected pieces
|
||||||
|
for (const auto& [type, value] : this->selectedPieces) {
|
||||||
|
byte = type;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
byte = value;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Settings::selectNextKeybinds() {
|
bool Settings::selectNextKeybinds() {
|
||||||
@@ -89,14 +157,14 @@ void Settings::confirmSelectedPieces() {
|
|||||||
|
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case CONVEX : {this->menu.getPiecesList().selectConvexPieces(value); break;}
|
case CONVEX_PIECES : {this->menu.getPiecesList().selectConvexPieces(value); break;}
|
||||||
case HOLELESS : {this->menu.getPiecesList().selectHolelessPieces(value); break;}
|
case HOLELESS_PIECES : {this->menu.getPiecesList().selectHolelessPieces(value); break;}
|
||||||
case OTHERS : {this->menu.getPiecesList().selectOtherPieces(value); break;}
|
case OTHER_PIECES : {this->menu.getPiecesList().selectOtherPieces(value); break;}
|
||||||
case ALL : {this->menu.getPiecesList().selectAllPieces(value); break;}
|
case ALL_PIECES : {this->menu.getPiecesList().selectAllPieces(value); break;}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (size > 15) return;
|
if (size > MAXIMUM_PIECES_SIZE) return;
|
||||||
|
|
||||||
this->menu.getPiecesList().selectPiece(size, value);
|
this->menu.getPiecesList().selectPiece(size, value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,9 @@
|
|||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
static const int MAXIMUM_BOARD_WIDTH = 40;
|
||||||
|
static const int MAXIMUM_BOARD_HEIGHT = 40;
|
||||||
|
|
||||||
|
|
||||||
class Settings {
|
class Settings {
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -1,12 +1,18 @@
|
|||||||
#include "GraphApp.h"
|
#include "GraphApp.h"
|
||||||
#include "../Pieces/PiecesFiles.h"
|
#include "../Pieces/PiecesFiles.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
|
||||||
|
void resetConfigFiles();
|
||||||
|
void resetSettingsFile();
|
||||||
|
void resetKeybindFile(int layout);
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::srand(std::time(NULL));
|
std::srand(std::time(NULL));
|
||||||
|
|
||||||
// dev version
|
// keep only for dev
|
||||||
PiecesFiles pf;
|
PiecesFiles pf;
|
||||||
for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) {
|
for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) {
|
||||||
if (!std::filesystem::exists("data/pieces/" + std::to_string(i) + "minos.bin")) {
|
if (!std::filesystem::exists("data/pieces/" + std::to_string(i) + "minos.bin")) {
|
||||||
@@ -23,7 +29,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// release version
|
// before compiling release version
|
||||||
//resetConfigFiles();
|
//resetConfigFiles();
|
||||||
|
|
||||||
GraphApp UI;
|
GraphApp UI;
|
||||||
@@ -41,7 +47,38 @@ void resetConfigFiles() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void resetSettingsFile() {
|
void resetSettingsFile() {
|
||||||
|
std::ofstream settingsFile("data/config/settings.bin", std::ios::trunc | std::ios::binary);
|
||||||
|
char byte;
|
||||||
|
|
||||||
|
// keybind layout
|
||||||
|
byte = 0;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// window size mode
|
||||||
|
byte = 2;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// gamemode
|
||||||
|
byte = SPRINT;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// board width
|
||||||
|
byte = 10;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// board height
|
||||||
|
byte = 20;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// piece distribution
|
||||||
|
byte = 0;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
|
||||||
|
// selected pieces
|
||||||
|
byte = ALL_PIECES;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
|
byte = 4;
|
||||||
|
settingsFile.write(&byte, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void resetKeybindFile(int layout) {
|
void resetKeybindFile(int layout) {
|
||||||
|
|||||||
Reference in New Issue
Block a user