From 30dd323e229b7f1261e7bb29edf8b159bef0000a Mon Sep 17 00:00:00 2001 From: zulianc Date: Sat, 22 Mar 2025 17:41:33 +0100 Subject: [PATCH] trop de trucs oscours --- .../keybinds/{layout5.bin => layout0.bin} | 0 doc/game_logic.md | 1 - src/Core/Action.h | 19 +- src/GraphicalUI/AppMenus/InGameAppMenu.cpp | 96 ++++++++ src/GraphicalUI/AppMenus/InGameAppMenu.h | 21 ++ src/GraphicalUI/AppMenus/MainAppMenu.cpp | 11 +- src/GraphicalUI/GraphApp.cpp | 2 +- src/GraphicalUI/GraphApp.h | 2 +- src/GraphicalUI/Keybinds.h | 2 - src/GraphicalUI/Settings.cpp | 9 +- src/GraphicalUI/Settings.h | 4 +- src/GraphicalUI/main.cpp | 224 ++++++++---------- src/TextUI/TextApp.cpp | 36 ++- 13 files changed, 265 insertions(+), 162 deletions(-) rename data/config/keybinds/{layout5.bin => layout0.bin} (100%) create mode 100644 src/GraphicalUI/AppMenus/InGameAppMenu.cpp create mode 100644 src/GraphicalUI/AppMenus/InGameAppMenu.h diff --git a/data/config/keybinds/layout5.bin b/data/config/keybinds/layout0.bin similarity index 100% rename from data/config/keybinds/layout5.bin rename to data/config/keybinds/layout0.bin diff --git a/doc/game_logic.md b/doc/game_logic.md index 15ff687..7c80d0f 100644 --- a/doc/game_logic.md +++ b/doc/game_logic.md @@ -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: -- Quit the game - Pause - Retry - Hold diff --git a/src/Core/Action.h b/src/Core/Action.h index 65ef9e4..52df0c7 100644 --- a/src/Core/Action.h +++ b/src/Core/Action.h @@ -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 { - QUIT, PAUSE, RETRY, HOLD, @@ -22,8 +21,20 @@ enum Action { }; -static const std::string ACTION_NAMES[] = { // name for each action - "Quit", +static const Action ACTION_LIST_IN_ORDER[] = { // the list of possible actions in a sorted order + 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", "Retry", "Hold", diff --git a/src/GraphicalUI/AppMenus/InGameAppMenu.cpp b/src/GraphicalUI/AppMenus/InGameAppMenu.cpp new file mode 100644 index 0000000..369c471 --- /dev/null +++ b/src/GraphicalUI/AppMenus/InGameAppMenu.cpp @@ -0,0 +1,96 @@ +#include "InGameAppMenu.h" + +#include "AppMenu.h" + +#include +#include +#include + + +InGameAppMenu::InGameAppMenu(std::shared_ptr> menuStack, std::shared_ptr settings, std::shared_ptr renderWindow) : + AppMenu(menuStack, settings, renderWindow), + game(this->settings->getMenu().startGame(this->settings->getGamemode())) + { + + this->paused = false; +} + +void InGameAppMenu::computeFrame() { + std::set 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(); +} diff --git a/src/GraphicalUI/AppMenus/InGameAppMenu.h b/src/GraphicalUI/AppMenus/InGameAppMenu.h new file mode 100644 index 0000000..04b29d4 --- /dev/null +++ b/src/GraphicalUI/AppMenus/InGameAppMenu.h @@ -0,0 +1,21 @@ +#pragma once + +#include "AppMenu.h" + +#include +#include +#include + + +class InGameAppMenu : public AppMenu { + private: + Game game; + bool paused; + + public: + InGameAppMenu(std::shared_ptr> menuStack, std::shared_ptr settings, std::shared_ptr renderWindow); + + void computeFrame(); + + void drawFrame() const; +}; diff --git a/src/GraphicalUI/AppMenus/MainAppMenu.cpp b/src/GraphicalUI/AppMenus/MainAppMenu.cpp index dab1493..60d4aed 100644 --- a/src/GraphicalUI/AppMenus/MainAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/MainAppMenu.cpp @@ -13,9 +13,16 @@ MainAppMenu::MainAppMenu(std::shared_ptr> menuStack, std::sh } void MainAppMenu::computeFrame() { - + if (sf::Keyboard::isKeyPressed(sfKey::Enter)) { + + } + else if (sf::Keyboard::isKeyPressed(sfKey::Escape)) { + this->menuStack->pop(); + } } void MainAppMenu::drawFrame() const { - + this->renderWindow->clear(sf::Color::Black); + + this->renderWindow->display(); } diff --git a/src/GraphicalUI/GraphApp.cpp b/src/GraphicalUI/GraphApp.cpp index 93dcce4..ba7eff2 100644 --- a/src/GraphicalUI/GraphApp.cpp +++ b/src/GraphicalUI/GraphApp.cpp @@ -17,7 +17,7 @@ GraphApp::GraphApp() { this->window = std::make_shared(); } -void GraphApp::startApp() { +void GraphApp::run() { changeVideoMode(*this->window, this->settings->getVideoMode()); this->menuStack->push(MainAppMenu(this->menuStack, this->settings, this->window)); diff --git a/src/GraphicalUI/GraphApp.h b/src/GraphicalUI/GraphApp.h index 4b27c5e..effaac8 100644 --- a/src/GraphicalUI/GraphApp.h +++ b/src/GraphicalUI/GraphApp.h @@ -17,5 +17,5 @@ class GraphApp { public: GraphApp(); - void startApp(); + void run(); }; diff --git a/src/GraphicalUI/Keybinds.h b/src/GraphicalUI/Keybinds.h index f703b16..58da4ce 100644 --- a/src/GraphicalUI/Keybinds.h +++ b/src/GraphicalUI/Keybinds.h @@ -20,8 +20,6 @@ class Keybinds { void saveKeybindsToFile() const; - void resetCustomLayoutFile() const; - void addKey(Action action, sfKey key); void clearKeys(Action action); diff --git a/src/GraphicalUI/Settings.cpp b/src/GraphicalUI/Settings.cpp index 4a0c31d..ad21459 100644 --- a/src/GraphicalUI/Settings.cpp +++ b/src/GraphicalUI/Settings.cpp @@ -21,17 +21,14 @@ Settings::Settings() { } void Settings::loadSettingsFromFile() { + //TODO this->menu.getPiecesList().unselectAll(); this->menu.getPiecesList().selectAllPieces(4); this->windowSizeMode = 2; } void Settings::saveSettingsToFile() const { - -} - -void Settings::resetSettingsFile() const { - + //TODO } bool Settings::selectNextKeybinds() { @@ -122,7 +119,7 @@ int Settings::getWindowSizeMultiplier() const { 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]); } diff --git a/src/GraphicalUI/Settings.h b/src/GraphicalUI/Settings.h index 5c01587..4128434 100644 --- a/src/GraphicalUI/Settings.h +++ b/src/GraphicalUI/Settings.h @@ -24,8 +24,6 @@ class Settings { void saveSettingsToFile() const; - void resetSettingsFile() const; - bool selectNextKeybinds(); bool selectPreviousKeybinds(); @@ -52,7 +50,7 @@ class Settings { int getWindowSizeMultiplier() const; - const sf::VideoMode& getVideoMode() const; + const sf::VideoMode getVideoMode() const; const std::vector>& getSelectedPieces() const; }; diff --git a/src/GraphicalUI/main.cpp b/src/GraphicalUI/main.cpp index 504edd7..e56c6ce 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -1,138 +1,116 @@ -#include -#include "../Core/Menu.h" +#include "GraphApp.h" #include "../Pieces/PiecesFiles.h" -#include +#include -void setToDefaultConfig(); int main() { std::srand(std::time(NULL)); - sf::RenderWindow window(sf::VideoMode({800, 640}), "My window", sf::Style::Titlebar | sf::Style::Close); - window.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().size.x / 2 - 400, sf::VideoMode::getDesktopMode().size.y / 2 - 320)); - + // dev version PiecesFiles pf; - for (int i = 1; i <= 10; i++) { - pf.savePieces(i); + for (int i = 1; i <= MAXIMUM_PIECES_SIZE; 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; - m.getPiecesList().loadPieces(10); - 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(); + // release version + //resetConfigFiles(); - sf::Clock clock; + GraphApp UI; + UI.run(); - sf::Font font; - if (!font.openFromFile("data/fonts/arial.ttf")) { - std::cout << "aaaaaaaaaaaaaa"; - } - sf::Text text(font); - text.setCharacterSize(20); - text.setFillColor(sf::Color::White); + return 0; +} - while (window.isOpen()) { - while (const std::optional event = window.pollEvent()) { - if (event->is()) - window.close(); - } - if (clock.getElapsedTime().asMilliseconds() > 16) { - clock.restart(); - - window.clear(sf::Color::Black); - - std::set actions; - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) { - actions.insert(MOVE_LEFT); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) { - actions.insert(MOVE_RIGHT); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) { - actions.insert(HARD_DROP); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) { - actions.insert(SOFT_DROP); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { - actions.insert(ROTATE_CCW); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::E)) { - actions.insert(ROTATE_CW); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Z)) { - actions.insert(HOLD); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Tab)) { - actions.insert(ROTATE_0); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Num2)) { - actions.insert(ROTATE_180); - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R)) { - game.reset(); - game.start(); - } - game.nextFrame(actions); - - for (int y = game.getBoard().getBaseHeight() + 5; y >= 0; y--) { - for (int x = 0; x < game.getBoard().getWidth(); x++) { - bool isActivePieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.getActivePiecePosition())); - bool isGhostPieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.ghostPiecePosition())); - Block block = (isActivePieceHere || isGhostPieceHere) ? game.getActivePiece()->getBlockType() : 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, (game.getBoard().getBaseHeight() + 10 - y)*20)); - window.draw(cell); - } - } - - if (game.getNextPieces().size() > 0) { - for (int y = 10; y >= 0; y--) { - for (int x = 0; x <= 10; x++) { - Block block = game.getNextPieces().at(0).getBlockType(); - sf::RectangleShape cell(sf::Vector2f(20.f, 20.f)); - cell.setPosition(sf::Vector2f((x + 2 + game.getBoard().getWidth())*20, (game.getBoard().getBaseHeight() - y)*20)); - if (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)); - } - window.draw(cell); - } - } - } - - if (game.getHeldPiece() != nullptr) { - for (int y = 10; y >= 0; y--) { - for (int x = 0; x <= 10; x++) { - Block block = game.getHeldPiece()->getBlockType(); - sf::RectangleShape cell(sf::Vector2f(20.f, 20.f)); - cell.setPosition(sf::Vector2f((x + 12 + game.getBoard().getWidth())*20, (game.getBoard().getBaseHeight() - y)*20)); - if (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)); - } - 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(); - } +void resetConfigFiles() { + resetSettingsFile; + for (int i = 0; i < 5; i++) { + resetKeybindFile(i); + } +} + +void resetSettingsFile() { + +} + +void resetKeybindFile(int layout) { + if (layout < 0 || layout > 4) return; + + std::ofstream layoutFile("data/config/keybinds/layout" + std::to_string(layout) + ".bin", std::ios::trunc | std::ios::binary); + std::map keybinds; + + if (layout != 4) { + keybinds.insert({PAUSE, sfKey::P}); + keybinds.insert({RETRY, sfKey::R}); + } + + if (layout == 0) { + keybinds.insert({MOVE_LEFT, sfKey::Left}); + keybinds.insert({MOVE_RIGHT, sfKey::Right}); + keybinds.insert({SOFT_DROP, sfKey::Down}); + keybinds.insert({HARD_DROP, sfKey::Space}); + keybinds.insert({ROTATE_CW, sfKey::Up}); + keybinds.insert({ROTATE_CCW, sfKey::Z}); + keybinds.insert({ROTATE_180, sfKey::X}); + keybinds.insert({ROTATE_0, sfKey::LShift}); + keybinds.insert({HOLD, sfKey::C}); + } + if (layout == 1) { + keybinds.insert({MOVE_LEFT, sfKey::Z}); + keybinds.insert({MOVE_RIGHT, sfKey::C}); + keybinds.insert({SOFT_DROP, sfKey::X}); + keybinds.insert({HARD_DROP, sfKey::S}); + keybinds.insert({ROTATE_CW, sfKey::M}); + keybinds.insert({ROTATE_CCW, sfKey::Comma}); + keybinds.insert({ROTATE_180, sfKey::J}); + keybinds.insert({ROTATE_0, sfKey::K}); + keybinds.insert({HOLD, sfKey::LShift}); + } + if (layout == 2) { + keybinds.insert({MOVE_LEFT, sfKey::A}); + keybinds.insert({MOVE_RIGHT, sfKey::D}); + keybinds.insert({SOFT_DROP, sfKey::W}); + keybinds.insert({HARD_DROP, sfKey::S}); + keybinds.insert({ROTATE_CW, sfKey::Left}); + keybinds.insert({ROTATE_CCW, sfKey::Right}); + keybinds.insert({ROTATE_180, sfKey::Up}); + keybinds.insert({ROTATE_0, sfKey::Down}); + keybinds.insert({HOLD, sfKey::RShift}); + } + if (layout == 3) { + keybinds.insert({MOVE_LEFT, sfKey::Left}); + keybinds.insert({MOVE_RIGHT, sfKey::Right}); + keybinds.insert({SOFT_DROP, sfKey::Down}); + keybinds.insert({HARD_DROP, sfKey::Up}); + keybinds.insert({ROTATE_CW, sfKey::E}); + keybinds.insert({ROTATE_CCW, sfKey::A}); + keybinds.insert({ROTATE_180, sfKey::Num2}); + keybinds.insert({ROTATE_0, sfKey::Tab}); + keybinds.insert({HOLD, sfKey::Z}); + } + + char contentChar; + for (Action action : ACTION_LIST_IN_ORDER) { + contentChar = action; + layoutFile.write(&contentChar, 1); + + if (keybinds.contains(action)) { + contentChar = (int) keybinds.at(action); + layoutFile.write(&contentChar, 1); + } + + contentChar = 0xFF; + layoutFile.write(&contentChar, 1); } } diff --git a/src/TextUI/TextApp.cpp b/src/TextUI/TextApp.cpp index a6e486e..80ce0d8 100644 --- a/src/TextUI/TextApp.cpp +++ b/src/TextUI/TextApp.cpp @@ -136,7 +136,6 @@ void TextApp::seeKeybinds() const { void TextApp::defaultKeybinds() { this->keybinds.clear(); - this->keybinds.insert({"quit", QUIT}); this->keybinds.insert({"pause", PAUSE}); this->keybinds.insert({"retry", RETRY}); this->keybinds.insert({"h", HOLD}); @@ -171,15 +170,21 @@ void TextApp::startGame() const { std::set playerActions; std::set lastFrameActions; - std::set metaActions; + bool retrying = false; for (std::string action : actions) { - try { - Action playerAction = this->keybinds.at(action); - if (playerAction == PAUSE || playerAction == RETRY || playerAction == quit) { - metaActions.insert(playerAction); - } - else { - if (playerAction == SOFT_DROP || playerAction == MOVE_LEFT || playerAction == MOVE_RIGHT) { + if (action == "quit") { + quit = true; + } + else { + try { + Action playerAction = this->keybinds.at(action); + 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); lastFrameActions.insert(playerAction); } @@ -190,19 +195,12 @@ void TextApp::startGame() const { playerActions.insert(playerAction); } } + catch (std::exception ignored) {} } - catch (std::exception ignored) {} } - if (metaActions.contains(PAUSE)) { - paused = (!paused); - } - - if (!paused) { - if (metaActions.contains(QUIT)) { - quit = true; - } - else if (metaActions.contains(RETRY)) { + if (!paused && !quit) { + if (retrying) { game.reset(); game.start(); }