2 Commits

Author SHA1 Message Date
d5ac79559e updated file format 2025-03-31 19:22:52 +02:00
5ea47ddd25 added startup menu 2025-03-31 18:59:42 +02:00
13 changed files with 253 additions and 104 deletions

View File

@@ -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.

View File

@@ -10,6 +10,7 @@
#include <memory>
#include <utility>
#include <cstdlib>
#include <iostream>
GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& piecesList, int nextQueueLength) :

View File

@@ -7,7 +7,7 @@
GameParameters::GameParameters(Gamemode gamemode, const Player& controls) :
gamemode(gamemode),
controls(controls) {
this->reset();
}

View File

@@ -120,8 +120,6 @@ void GamePlayingAppMenu::drawFrame() const {
}
}
// end coutdown
if (drawActivePiece) {
// ghost piece
sf::Color ghostColor = this->getColorOfBlock(this->game.getActivePiece()->getBlockType(), 100);

View File

@@ -0,0 +1,74 @@
#include "StartUpAppMenu.h"
#include "AppMenu.h"
#include "MainAppMenu.h"
#include "../PlayerCursor.h"
#include <stack>
#include <memory>
#include <SFML/Graphics.hpp>
StartUpAppMenu::StartUpAppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow) :
AppMenu(menuStack, settings, renderWindow),
playerCursor({LOADED_PIECES_SIZE + 1}) {
this->playerCursor.goToPosition({MINIMUM_PIECES_SIZE, 0});
}
void StartUpAppMenu::computeFrame() {
this->updateMetaBinds();
this->playerCursor.updatePosition();
if (this->playerCursor.getPosition().x < MINIMUM_PIECES_SIZE) {
if (this->playerCursor.movedLeft()) {
this->playerCursor.goToPosition({LOADED_PIECES_SIZE, 0});
}
else {
this->playerCursor.goToPosition({MINIMUM_PIECES_SIZE, 0});
}
}
if (this->enterReleased) {
*this->settings = Settings(this->playerCursor.getPosition().x);
this->menuStack->pop();
if (this->settings->hasLoadedPieces()) {
this->menuStack->push(std::make_shared<MainAppMenu>(this->menuStack, this->settings, this->renderWindow));
}
else {
std::cout << "ERROR: COULD NOT LOAD PIECES" << std::endl;
std::cout << "ARGUMENT WAS: " << this->playerCursor.getPosition().x << std::endl;
}
}
else if (this->escReleased) {
this->menuStack->pop();
}
}
void StartUpAppMenu::drawFrame() const {
this->renderWindow->clear(sf::Color(200, 200, 200));
sf::Text text(this->pressStartFont, "", this->settings->getWindowSizeMultiplier() * 2);
text.setFillColor(sf::Color(0, 0, 0));
text.setOutlineColor(sf::Color(255, 255, 255));
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});
if (this->playerCursor.getPosition().x <= 10) {
text.setFillColor({0, 255, 0});
this->placeTitle(text, {}, "LOW LOAD TIME", 40.f, {});
}
else if (this->playerCursor.getPosition().x <= 13) {
text.setFillColor({255, 255, 0});
this->placeTitle(text, {}, "MEDIUM LOAD TIME", 40.f, {});
}
else {
text.setFillColor({255, 0, 0});
this->placeTitle(text, {}, "LONG LOAD TIME", 40.f, {});
}
this->renderWindow->display();
}

View File

@@ -0,0 +1,21 @@
#pragma once
#include "AppMenu.h"
#include "../PlayerCursor.h"
#include <stack>
#include <memory>
#include <SFML/Graphics.hpp>
class StartUpAppMenu : public AppMenu {
private:
PlayerCursor playerCursor;
public:
StartUpAppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow);
void computeFrame() override;
void drawFrame() const override;
};

View File

@@ -1,7 +1,7 @@
#include "GraphApp.h"
#include "AppMenus/AppMenu.h"
#include "AppMenus/MainAppMenu.h"
#include "AppMenus/StartUpAppMenu.h"
#include "Settings.h"
#include <stack>
@@ -11,15 +11,15 @@
static const double TIME_BETWEEN_FRAMES = (1000.f / FRAMES_PER_SECOND);
GraphApp::GraphApp(int maximumPiecesSize) {
this->settings = std::make_shared<Settings>(maximumPiecesSize);
GraphApp::GraphApp() {
this->settings = std::make_shared<Settings>(0);
this->menuStack = std::make_shared<MenuStack>();
this->renderWindow = std::make_shared<sf::RenderWindow>();
}
void GraphApp::run() {
this->settings->changeVideoMode(*this->renderWindow);
this->menuStack->push(std::make_shared<MainAppMenu>(this->menuStack, this->settings, this->renderWindow));
this->menuStack->push(std::make_shared<StartUpAppMenu>(this->menuStack, this->settings, this->renderWindow));
bool quit = false;
double timeAtNextFrame = 0;

View File

@@ -15,7 +15,7 @@ class GraphApp {
std::shared_ptr<sf::RenderWindow> renderWindow;
public:
GraphApp(int maximumPiecesSize);
GraphApp();
void run();
};

View File

@@ -2,7 +2,9 @@
#include "../Core/Menu.h"
#include "Keybinds.h"
#include "PiecesType.h"
#include <vector>
#include <fstream>
#include <algorithm>
#include <SFML/Graphics.hpp>
@@ -11,27 +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) {
this->maximumPiecesSize = maximumPiecesSize;
for (int i = 1; i <= this->maximumPiecesSize; i++) {
this->menu.getPiecesList().loadPieces(i);
}
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;
@@ -68,55 +97,71 @@ void Settings::loadSettingsFromFile() {
settingsFile.get(byte);
this->menu.setBoardHeight(byte);
// piece distribution
settingsFile.get(byte);
this->menu.getPiecesList().setDistributionMode(DistributionMode(byte));
this->distributions.clear();
this->distributions.push_back(0);
for (int i = 1; i <= 15; i++) {
if (this->loadedPieces) {
// piece distribution
settingsFile.get(byte);
this->distributions.push_back(byte);
}
this->confirmDistribution();
this->menu.getPiecesList().setDistributionMode(DistributionMode(byte));
// selected pieces
char pieceType;
char pieceSize;
char lowByte;
char midByte;
char highByte;
this->distributions.clear();
this->distributions.push_back(0);
for (int i = 1; i <= 15; i++) {
settingsFile.get(byte);
this->distributions.push_back(byte);
}
this->confirmDistribution();
this->selectedPieces.clear();
while (settingsFile.get(pieceType)) {
if (settingsFile.eof()) break;
if (getSizeOfPieces(PiecesType(pieceType)) == 0) {
settingsFile.get(pieceSize);
// selected pieces
char pieceType;
char pieceSize;
char lowByte;
char midByte;
char highByte;
if (!(pieceSize > this->maximumPiecesSize)) {
this->selectedPieces.emplace_back(PiecesType(pieceType), pieceSize);
}
}
else {
if (!(getSizeOfPieces(PiecesType(pieceType)) > this->maximumPiecesSize)) {
settingsFile.get(lowByte);
settingsFile.get(midByte);
settingsFile.get(highByte);
int pieceNumber = ((unsigned char) lowByte) + ((unsigned char) midByte << 8) + ((unsigned char) highByte << 16);
this->selectedPieces.emplace_back(PiecesType(pieceType), pieceNumber);
this->selectedPieces.clear();
while (settingsFile.get(pieceType)) {
if (settingsFile.eof()) break;
if (getSizeOfPieces(PiecesType(pieceType)) == 0) {
settingsFile.get(pieceSize);
if (!(pieceSize > this->maximumPiecesSize)) {
this->selectedPieces.emplace_back(PiecesType(pieceType), pieceSize);
}
}
else {
if (!(getSizeOfPieces(PiecesType(pieceType)) > this->maximumPiecesSize)) {
settingsFile.get(lowByte);
settingsFile.get(midByte);
settingsFile.get(highByte);
int pieceNumber = ((unsigned char) lowByte) + ((unsigned char) midByte << 8) + ((unsigned char) highByte << 16);
this->selectedPieces.emplace_back(PiecesType(pieceType), pieceNumber);
}
}
}
this->confirmSelectedPieces();
}
else {
this->distributions.clear();
this->selectedPieces.clear();
}
this->confirmSelectedPieces();
}
void Settings::saveSettingsToFile() const {
if (!this->loadedPieces) return;
this->keybinds.at(CUSTOMIZABLE_KEYBINDS).saveKeybindsToFile();
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);
@@ -247,16 +292,21 @@ void Settings::setGamemode(Gamemode gamemode) {
}
void Settings::selectPieces(PiecesType type, int value) {
if (!this->loadedPieces) return;
this->selectedPieces.emplace_back(type, value);
}
void Settings::unselectPieces(int index) {
if (!this->loadedPieces) return;
if (index >= this->selectedPieces.size()) return;
this->selectedPieces.erase(this->selectedPieces.begin() + index);
}
void Settings::confirmSelectedPieces() {
if (!this->loadedPieces) return;
this->menu.getPiecesList().unselectAll();
if (this->getSelectedPieces().size() == 0) {
@@ -281,6 +331,7 @@ void Settings::confirmSelectedPieces() {
}
bool Settings::increaseDistribution(int size) {
if (!this->loadedPieces) return false;
if (size < 1 || size > this->maximumPiecesSize) return false;
if (this->distributions.at(size) < DISTRIBUTION_MAX) {
@@ -291,6 +342,7 @@ bool Settings::increaseDistribution(int size) {
}
bool Settings::decreaseDistribution(int size) {
if (!this->loadedPieces) return false;
if (size < 1 || size > this->maximumPiecesSize) return false;
if (this->distributions.at(size) > 0) {
@@ -301,6 +353,8 @@ bool Settings::decreaseDistribution(int size) {
}
void Settings::confirmDistribution() {
if (!this->loadedPieces) return;
for (int i = 1; i <= 15; i++) {
this->menu.getPiecesList().changeCustomDistribution(i, (double) 1 / (this->distributions.at(i) + 0.001));
}
@@ -318,6 +372,10 @@ int Settings::getMaximumPiecesSize() const {
return this->maximumPiecesSize;
}
bool Settings::hasLoadedPieces() const {
return this->loadedPieces;
}
int Settings::getKeybindsLayout() const {
return this->chosenKeybinds;
}

View File

@@ -1,21 +1,35 @@
#pragma once
#include "../Core/Menu.h"
#include "StartUpMenu.h"
#include "Keybinds.h"
#include "PiecesType.h"
#include <SFML/Graphics.hpp>
#include <vector>
#include <SFML/Graphics.hpp>
static const int CURRENT_FILE_FORMAT_VERSION = 10;
static const int MAXIMUM_BOARD_WIDTH = 40;
static const int MAXIMUM_BOARD_HEIGHT = 40;
static const int MINIMUM_PIECES_SIZE = 4;
static const int MAXIMUM_PIECES_SIZE = 15;
//#define __JMINOS_RELEASE__
#ifdef __JMINOS_RELEASE__
static const int LOADED_PIECES_SIZE = 15;
#else
static const int LOADED_PIECES_SIZE = 10;
#endif
static const std::pair<PiecesType, int> DEFAULT_SELECTION = {ALL_PIECES, MINIMUM_PIECES_SIZE};
class Settings {
private:
Menu menu;
int maximumPiecesSize;
bool loadedPieces;
std::vector<Keybinds> keybinds;
int chosenKeybinds;
int windowSizeMode;
@@ -25,9 +39,13 @@ class Settings {
std::vector<int> distributions;
public:
Settings(int maximumPiecesSize);
Settings(bool loadPieces);
void loadSettingsFromFile();
private:
bool loadPieces(int maximumPiecesSizeRequest);
public:
void loadSettingsFromFile(bool loadPieces = true);
void saveSettingsToFile() const;
@@ -67,6 +85,8 @@ class Settings {
int getMaximumPiecesSize() const;
bool hasLoadedPieces() const;
int getKeybindsLayout() const;
Gamemode getGamemode() const;

View File

@@ -1,15 +0,0 @@
#include "StartUpMenu.h"
#include "PlayerCursor.h"
StartUpMenu::StartUpMenu() :
playerCursor({LOADED_PIECES_SIZE}) {
this->playerCursor.goToPosition({MINIMUM_PIECES_SIZE, 0});
}
int StartUpMenu::getMaximumPiecesSize() {
return LOADED_PIECES_SIZE;
//TODO
}

View File

@@ -1,28 +0,0 @@
#pragma once
#include "PiecesType.h"
#include "PlayerCursor.h"
static const int MINIMUM_PIECES_SIZE = 4;
static const int MAXIMUM_PIECES_SIZE = 15;
//#define __JMINOS_RELEASE__
#ifdef __JMINOS_RELEASE__
static const int LOADED_PIECES_SIZE = 15;
#else
static const int LOADED_PIECES_SIZE = 10;
#endif
static const std::pair<PiecesType, int> DEFAULT_SELECTION = {ALL_PIECES, MINIMUM_PIECES_SIZE};
class StartUpMenu {
private:
PlayerCursor playerCursor;
public:
StartUpMenu();
int getMaximumPiecesSize();
};

View File

@@ -1,4 +1,3 @@
#include "StartUpMenu.h"
#include "GraphApp.h"
#include "../Pieces/PiecesFiles.h"
@@ -20,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;
@@ -31,10 +41,7 @@ int main() {
}
}
StartUpMenu startUp;
int maximumPiecesSize = startUp.getMaximumPiecesSize();
GraphApp UI(maximumPiecesSize);
GraphApp UI;
UI.run();
return 0;
@@ -47,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);