trop de trucs oscours

This commit is contained in:
2025-03-22 17:41:33 +01:00
parent d87ddcdc22
commit 30dd323e22
13 changed files with 265 additions and 162 deletions

View File

@@ -7,7 +7,6 @@ We will only talk about pieces and not polyominoes. In this project, pieces are
Each frame, the UI will translate the user's input into a series of action to apply to the game. The list of action is the following: Each frame, the UI will translate the user's input into a series of action to apply to the game. The list of action is the following:
- Quit the game
- Pause - Pause
- Retry - Retry
- Hold - Hold

View File

@@ -4,10 +4,9 @@
/** /**
* The list of actions that can be taken by the player * The list of in-game actions that can be taken by the player
*/ */
enum Action { enum Action {
QUIT,
PAUSE, PAUSE,
RETRY, RETRY,
HOLD, HOLD,
@@ -22,8 +21,20 @@ enum Action {
}; };
static const std::string ACTION_NAMES[] = { // name for each action static const Action ACTION_LIST_IN_ORDER[] = { // the list of possible actions in a sorted order
"Quit", MOVE_LEFT,
MOVE_RIGHT,
SOFT_DROP,
HARD_DROP,
ROTATE_CW,
ROTATE_CCW,
ROTATE_180,
ROTATE_0,
HOLD,
PAUSE,
RETRY
};
static const std::string ACTION_NAMES[] = { // name representation for each actions
"Pause", "Pause",
"Retry", "Retry",
"Hold", "Hold",

View File

@@ -0,0 +1,96 @@
#include "InGameAppMenu.h"
#include "AppMenu.h"
#include <stack>
#include <memory>
#include <SFML/Graphics.hpp>
InGameAppMenu::InGameAppMenu(std::shared_ptr<std::stack<AppMenu>> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow) :
AppMenu(menuStack, settings, renderWindow),
game(this->settings->getMenu().startGame(this->settings->getGamemode()))
{
this->paused = false;
}
void InGameAppMenu::computeFrame() {
std::set<Action> actions;
for (Action action : ACTION_LIST_IN_ORDER) {
for (sfKey key : this->settings->getKeybinds().getKeybinds(action)) {
if (sf::Keyboard::isKeyPressed(key)) {
actions.insert(action);
}
}
}
if (actions.contains(RETRY)) {
this->game.reset();
this->game.start();
}
if (actions.contains(PAUSE)) {
this->paused = (!this->paused);
}
if (!paused) {
this->game.nextFrame(actions);
}
if (sf::Keyboard::isKeyPressed(sfKey::Escape)) {
this->menuStack->pop();
}
}
void InGameAppMenu::drawFrame() const {
this->renderWindow->clear(sf::Color::Black);
for (int y = this->game.getBoard().getBaseHeight() + 5; y >= 0; y--) {
for (int x = 0; x < this->game.getBoard().getWidth(); x++) {
bool isActivePieceHere = (this->game.getActivePiece() != nullptr) && (this->game.getActivePiece()->getPositions().contains(Position{x, y} - this->game.getActivePiecePosition()));
bool isGhostPieceHere = (this->game.getActivePiece() != nullptr) && (this->game.getActivePiece()->getPositions().contains(Position{x, y} - this->game.ghostPiecePosition()));
Block block = (isActivePieceHere || isGhostPieceHere) ? this->game.getActivePiece()->getBlockType() : this->game.getBoard().getBlock(Position{x, y});
sf::RectangleShape cell(sf::Vector2f(20.f, 20.f));
cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue, (isGhostPieceHere && !isActivePieceHere) ? 150 : 255));
cell.setPosition(sf::Vector2f(x*20, (this->game.getBoard().getBaseHeight() + 10 - y)*20));
this->renderWindow->draw(cell);
}
}
if (this->game.getNextPieces().size() > 0) {
for (int y = 10; y >= 0; y--) {
for (int x = 0; x <= 10; x++) {
Block block = this->game.getNextPieces().at(0).getBlockType();
sf::RectangleShape cell(sf::Vector2f(20.f, 20.f));
cell.setPosition(sf::Vector2f((x + 2 + this->game.getBoard().getWidth())*20, (this->game.getBoard().getBaseHeight() - y)*20));
if (this->game.getNextPieces().at(0).getPositions().contains(Position({x, y}))) {
cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue));
}
else {
cell.setFillColor(sf::Color(0, 0, 0));
}
this->renderWindow->draw(cell);
}
}
}
if (this->game.getHeldPiece() != nullptr) {
for (int y = 10; y >= 0; y--) {
for (int x = 0; x <= 10; x++) {
Block block = this->game.getHeldPiece()->getBlockType();
sf::RectangleShape cell(sf::Vector2f(20.f, 20.f));
cell.setPosition(sf::Vector2f((x + 12 + this->game.getBoard().getWidth())*20, (this->game.getBoard().getBaseHeight() - y)*20));
if (this->game.getHeldPiece()->getPositions().contains(Position({x, y}))) {
cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue));
}
else {
cell.setFillColor(sf::Color(0, 0, 0));
}
this->renderWindow->draw(cell);
}
}
}
this->renderWindow->display();
}

View File

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

View File

@@ -13,9 +13,16 @@ MainAppMenu::MainAppMenu(std::shared_ptr<std::stack<AppMenu>> menuStack, std::sh
} }
void MainAppMenu::computeFrame() { void MainAppMenu::computeFrame() {
if (sf::Keyboard::isKeyPressed(sfKey::Enter)) {
}
else if (sf::Keyboard::isKeyPressed(sfKey::Escape)) {
this->menuStack->pop();
}
} }
void MainAppMenu::drawFrame() const { void MainAppMenu::drawFrame() const {
this->renderWindow->clear(sf::Color::Black);
this->renderWindow->display();
} }

View File

@@ -17,7 +17,7 @@ GraphApp::GraphApp() {
this->window = std::make_shared<sf::RenderWindow>(); this->window = std::make_shared<sf::RenderWindow>();
} }
void GraphApp::startApp() { void GraphApp::run() {
changeVideoMode(*this->window, this->settings->getVideoMode()); changeVideoMode(*this->window, this->settings->getVideoMode());
this->menuStack->push(MainAppMenu(this->menuStack, this->settings, this->window)); this->menuStack->push(MainAppMenu(this->menuStack, this->settings, this->window));

View File

@@ -17,5 +17,5 @@ class GraphApp {
public: public:
GraphApp(); GraphApp();
void startApp(); void run();
}; };

View File

@@ -20,8 +20,6 @@ class Keybinds {
void saveKeybindsToFile() const; void saveKeybindsToFile() const;
void resetCustomLayoutFile() const;
void addKey(Action action, sfKey key); void addKey(Action action, sfKey key);
void clearKeys(Action action); void clearKeys(Action action);

View File

@@ -21,17 +21,14 @@ Settings::Settings() {
} }
void Settings::loadSettingsFromFile() { void Settings::loadSettingsFromFile() {
//TODO
this->menu.getPiecesList().unselectAll(); this->menu.getPiecesList().unselectAll();
this->menu.getPiecesList().selectAllPieces(4); this->menu.getPiecesList().selectAllPieces(4);
this->windowSizeMode = 2; this->windowSizeMode = 2;
} }
void Settings::saveSettingsToFile() const { void Settings::saveSettingsToFile() const {
//TODO
}
void Settings::resetSettingsFile() const {
} }
bool Settings::selectNextKeybinds() { bool Settings::selectNextKeybinds() {
@@ -122,7 +119,7 @@ int Settings::getWindowSizeMultiplier() const {
return WINDOW_SIZE_MULTIPLIERS[this->windowSizeMode]; return WINDOW_SIZE_MULTIPLIERS[this->windowSizeMode];
} }
const sf::VideoMode& Settings::getVideoMode() const { const sf::VideoMode Settings::getVideoMode() const {
return sf::VideoMode(BASE_WINDOW_SIZE * (unsigned int) WINDOW_SIZE_MULTIPLIERS[this->windowSizeMode]); return sf::VideoMode(BASE_WINDOW_SIZE * (unsigned int) WINDOW_SIZE_MULTIPLIERS[this->windowSizeMode]);
} }

View File

@@ -24,8 +24,6 @@ class Settings {
void saveSettingsToFile() const; void saveSettingsToFile() const;
void resetSettingsFile() const;
bool selectNextKeybinds(); bool selectNextKeybinds();
bool selectPreviousKeybinds(); bool selectPreviousKeybinds();
@@ -52,7 +50,7 @@ class Settings {
int getWindowSizeMultiplier() const; int getWindowSizeMultiplier() const;
const sf::VideoMode& getVideoMode() const; const sf::VideoMode getVideoMode() const;
const std::vector<std::pair<PiecesType, int>>& getSelectedPieces() const; const std::vector<std::pair<PiecesType, int>>& getSelectedPieces() const;
}; };

View File

@@ -1,138 +1,116 @@
#include <SFML/Graphics.hpp> #include "GraphApp.h"
#include "../Core/Menu.h"
#include "../Pieces/PiecesFiles.h" #include "../Pieces/PiecesFiles.h"
#include <iostream> #include <fstream>
void setToDefaultConfig();
int main() { int main() {
std::srand(std::time(NULL)); std::srand(std::time(NULL));
sf::RenderWindow window(sf::VideoMode({800, 640}), "My window", sf::Style::Titlebar | sf::Style::Close); // dev version
window.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().size.x / 2 - 400, sf::VideoMode::getDesktopMode().size.y / 2 - 320));
PiecesFiles pf; PiecesFiles pf;
for (int i = 1; i <= 10; i++) { for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) {
pf.savePieces(i); if (!std::filesystem::exists("data/pieces/" + std::to_string(i) + "minos.bin")) {
std::cout << "pieces files for size " << i << " not found, generating..." << std::endl;
pf.savePieces(i);
}
}
if (!std::filesystem::exists("data/config/settings.bin")) {
resetSettingsFile();
}
for (int i = 0; i < 5; i++) {
if (!std::filesystem::exists("data/config/keybinds/layout" + std::to_string(i) + ".bin")) {
resetKeybindFile(i);
}
} }
Menu m; // release version
m.getPiecesList().loadPieces(10); //resetConfigFiles();
m.getPiecesList().selectAllPieces(4);
m.setBoardWidth(10);
m.getPlayerControls().setDAS(6);
m.getPlayerControls().setARR(0);
m.getPlayerControls().setSDR(0);
Game game = m.startGame(SPRINT);
game.start();
sf::Clock clock; GraphApp UI;
UI.run();
sf::Font font; return 0;
if (!font.openFromFile("data/fonts/arial.ttf")) { }
std::cout << "aaaaaaaaaaaaaa";
}
sf::Text text(font);
text.setCharacterSize(20);
text.setFillColor(sf::Color::White);
while (window.isOpen()) {
while (const std::optional event = window.pollEvent()) {
if (event->is<sf::Event::Closed>())
window.close();
}
if (clock.getElapsedTime().asMilliseconds() > 16) { void resetConfigFiles() {
clock.restart(); resetSettingsFile;
for (int i = 0; i < 5; i++) {
window.clear(sf::Color::Black); resetKeybindFile(i);
}
std::set<Action> actions; }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
actions.insert(MOVE_LEFT); void resetSettingsFile() {
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) { }
actions.insert(MOVE_RIGHT);
} void resetKeybindFile(int layout) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) { if (layout < 0 || layout > 4) return;
actions.insert(HARD_DROP);
} std::ofstream layoutFile("data/config/keybinds/layout" + std::to_string(layout) + ".bin", std::ios::trunc | std::ios::binary);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) { std::map<Action, sfKey> keybinds;
actions.insert(SOFT_DROP);
} if (layout != 4) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { keybinds.insert({PAUSE, sfKey::P});
actions.insert(ROTATE_CCW); keybinds.insert({RETRY, sfKey::R});
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::E)) {
actions.insert(ROTATE_CW); if (layout == 0) {
} keybinds.insert({MOVE_LEFT, sfKey::Left});
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Z)) { keybinds.insert({MOVE_RIGHT, sfKey::Right});
actions.insert(HOLD); keybinds.insert({SOFT_DROP, sfKey::Down});
} keybinds.insert({HARD_DROP, sfKey::Space});
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Tab)) { keybinds.insert({ROTATE_CW, sfKey::Up});
actions.insert(ROTATE_0); keybinds.insert({ROTATE_CCW, sfKey::Z});
} keybinds.insert({ROTATE_180, sfKey::X});
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Num2)) { keybinds.insert({ROTATE_0, sfKey::LShift});
actions.insert(ROTATE_180); keybinds.insert({HOLD, sfKey::C});
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R)) { if (layout == 1) {
game.reset(); keybinds.insert({MOVE_LEFT, sfKey::Z});
game.start(); keybinds.insert({MOVE_RIGHT, sfKey::C});
} keybinds.insert({SOFT_DROP, sfKey::X});
game.nextFrame(actions); keybinds.insert({HARD_DROP, sfKey::S});
keybinds.insert({ROTATE_CW, sfKey::M});
for (int y = game.getBoard().getBaseHeight() + 5; y >= 0; y--) { keybinds.insert({ROTATE_CCW, sfKey::Comma});
for (int x = 0; x < game.getBoard().getWidth(); x++) { keybinds.insert({ROTATE_180, sfKey::J});
bool isActivePieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.getActivePiecePosition())); keybinds.insert({ROTATE_0, sfKey::K});
bool isGhostPieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.ghostPiecePosition())); keybinds.insert({HOLD, sfKey::LShift});
Block block = (isActivePieceHere || isGhostPieceHere) ? game.getActivePiece()->getBlockType() : game.getBoard().getBlock(Position{x, y}); }
if (layout == 2) {
sf::RectangleShape cell(sf::Vector2f(20.f, 20.f)); keybinds.insert({MOVE_LEFT, sfKey::A});
cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue, (isGhostPieceHere && !isActivePieceHere) ? 150 : 255)); keybinds.insert({MOVE_RIGHT, sfKey::D});
cell.setPosition(sf::Vector2f(x*20, (game.getBoard().getBaseHeight() + 10 - y)*20)); keybinds.insert({SOFT_DROP, sfKey::W});
window.draw(cell); keybinds.insert({HARD_DROP, sfKey::S});
} keybinds.insert({ROTATE_CW, sfKey::Left});
} keybinds.insert({ROTATE_CCW, sfKey::Right});
keybinds.insert({ROTATE_180, sfKey::Up});
if (game.getNextPieces().size() > 0) { keybinds.insert({ROTATE_0, sfKey::Down});
for (int y = 10; y >= 0; y--) { keybinds.insert({HOLD, sfKey::RShift});
for (int x = 0; x <= 10; x++) { }
Block block = game.getNextPieces().at(0).getBlockType(); if (layout == 3) {
sf::RectangleShape cell(sf::Vector2f(20.f, 20.f)); keybinds.insert({MOVE_LEFT, sfKey::Left});
cell.setPosition(sf::Vector2f((x + 2 + game.getBoard().getWidth())*20, (game.getBoard().getBaseHeight() - y)*20)); keybinds.insert({MOVE_RIGHT, sfKey::Right});
if (game.getNextPieces().at(0).getPositions().contains(Position({x, y}))) { keybinds.insert({SOFT_DROP, sfKey::Down});
cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue)); keybinds.insert({HARD_DROP, sfKey::Up});
} keybinds.insert({ROTATE_CW, sfKey::E});
else { keybinds.insert({ROTATE_CCW, sfKey::A});
cell.setFillColor(sf::Color(0, 0, 0)); keybinds.insert({ROTATE_180, sfKey::Num2});
} keybinds.insert({ROTATE_0, sfKey::Tab});
window.draw(cell); keybinds.insert({HOLD, sfKey::Z});
} }
}
} char contentChar;
for (Action action : ACTION_LIST_IN_ORDER) {
if (game.getHeldPiece() != nullptr) { contentChar = action;
for (int y = 10; y >= 0; y--) { layoutFile.write(&contentChar, 1);
for (int x = 0; x <= 10; x++) {
Block block = game.getHeldPiece()->getBlockType(); if (keybinds.contains(action)) {
sf::RectangleShape cell(sf::Vector2f(20.f, 20.f)); contentChar = (int) keybinds.at(action);
cell.setPosition(sf::Vector2f((x + 12 + game.getBoard().getWidth())*20, (game.getBoard().getBaseHeight() - y)*20)); layoutFile.write(&contentChar, 1);
if (game.getHeldPiece()->getPositions().contains(Position({x, y}))) { }
cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue));
} contentChar = 0xFF;
else { layoutFile.write(&contentChar, 1);
cell.setFillColor(sf::Color(0, 0, 0));
}
window.draw(cell);
}
}
}
text.setPosition(sf::Vector2f(12*20, (game.getBoard().getBaseHeight() - 5)*20));
text.setString(sf::String(std::to_string(game.getClearedLines())));
window.draw(text);
window.display();
}
} }
} }

View File

@@ -136,7 +136,6 @@ void TextApp::seeKeybinds() const {
void TextApp::defaultKeybinds() { void TextApp::defaultKeybinds() {
this->keybinds.clear(); this->keybinds.clear();
this->keybinds.insert({"quit", QUIT});
this->keybinds.insert({"pause", PAUSE}); this->keybinds.insert({"pause", PAUSE});
this->keybinds.insert({"retry", RETRY}); this->keybinds.insert({"retry", RETRY});
this->keybinds.insert({"h", HOLD}); this->keybinds.insert({"h", HOLD});
@@ -171,15 +170,21 @@ void TextApp::startGame() const {
std::set<Action> playerActions; std::set<Action> playerActions;
std::set<Action> lastFrameActions; std::set<Action> lastFrameActions;
std::set<Action> metaActions; bool retrying = false;
for (std::string action : actions) { for (std::string action : actions) {
try { if (action == "quit") {
Action playerAction = this->keybinds.at(action); quit = true;
if (playerAction == PAUSE || playerAction == RETRY || playerAction == quit) { }
metaActions.insert(playerAction); else {
} try {
else { Action playerAction = this->keybinds.at(action);
if (playerAction == SOFT_DROP || playerAction == MOVE_LEFT || playerAction == MOVE_RIGHT) { if (playerAction == RETRY) {
retrying = true;
}
else if (playerAction == PAUSE) {
paused = (!paused);
}
else if (playerAction == SOFT_DROP || playerAction == MOVE_LEFT || playerAction == MOVE_RIGHT) {
playerActions.insert(playerAction); playerActions.insert(playerAction);
lastFrameActions.insert(playerAction); lastFrameActions.insert(playerAction);
} }
@@ -190,19 +195,12 @@ void TextApp::startGame() const {
playerActions.insert(playerAction); playerActions.insert(playerAction);
} }
} }
catch (std::exception ignored) {}
} }
catch (std::exception ignored) {}
} }
if (metaActions.contains(PAUSE)) { if (!paused && !quit) {
paused = (!paused); if (retrying) {
}
if (!paused) {
if (metaActions.contains(QUIT)) {
quit = true;
}
else if (metaActions.contains(RETRY)) {
game.reset(); game.reset();
game.start(); game.start();
} }