diff --git a/assets/Abbieshire.ttf b/assets/Abbieshire.ttf deleted file mode 100644 index 9d27037df4fdd1d4936714ecd031bae1edfee7b3..0000000000000000000000000000000000000000 Binary files a/assets/Abbieshire.ttf and /dev/null differ diff --git a/assets/calibril.ttf b/assets/calibril.ttf new file mode 100644 index 0000000000000000000000000000000000000000..2ba5d3355a658fcfc6f221ea2e0581dafd9e140c Binary files /dev/null and b/assets/calibril.ttf differ diff --git a/source/BasicAbstractions/Font.h b/source/BasicAbstractions/Font.h index e9bd0d6efdd95ca4b5239794ea74b1504f97924e..3f38a7809dc23832af9d23e1eaa864d8a7d24b81 100644 --- a/source/BasicAbstractions/Font.h +++ b/source/BasicAbstractions/Font.h @@ -3,15 +3,15 @@ class MyFont { public: - static MyFont& Instance() { + static sf::Font& Instance() { static MyFont instance; - return instance; + return instance.m_font; } MyFont(const MyFont&) = delete; MyFont& operator=(const MyFont&) = delete; private: MyFont() { - if (!m_font.loadFromFile(config::FONT_FILE) + if (!m_font.loadFromFile(config::FONT_FILE)) throw std::runtime_error("Failed to load font"); } ~MyFont() = default; diff --git a/source/Configuration.h b/source/Configuration.h index dda7228a355d32516daa5ee5b81fc72d8a9beb61..4fc5e2d7d492722aab172619fd86f290bf2d1546 100644 --- a/source/Configuration.h +++ b/source/Configuration.h @@ -8,7 +8,7 @@ namespace config { const sf::Vector2f BUTTON_SIZE = { 250, 100 }; const size_t BUTTON_FONT_SIZE = static_cast<size_t>(BUTTON_SIZE.y / 1.5f); constexpr float BUTTON_FRAME_THICKNESS = 2.0f; - constexpr char FONT_FILE[] = "Calibri-Light.ttf"; + constexpr char FONT_FILE[] = "calibril.ttf"; constexpr char SELECT_LEVEL_TITLE[] = "Select Level"; const sf::VideoMode SELECT_LEVEL_VIDEO_MODE{ 400, 600 }; constexpr char BUTTON_TEXT_EASY[] = "Easy"; @@ -16,34 +16,34 @@ namespace config { constexpr char BUTTON_TEXT_HARD[] = "Hard"; constexpr char BUTTON_TEXT_EXIT[] = "Exit"; // РРіСЂР°: - const sf::VideoMode GAME_VIDEO_MODE{ 1080, 720 }; - constexpr char EASY_GAME_TITLE[] = "Level: Easy"; - constexpr char MEDIUM_GAME_TITLE[] = "Level: Medium"; - constexpr char HARD_GAME_TITLE[] = "Level: Hard"; - constexpr float EASY_GAME_ENEMY_RATIO = 0.0f; - constexpr float MEDIUM_GAME_ENEMY_RATIO = 0.03f; - constexpr float HARD_GAME_ENEMY_RATIO = 0.07f; - constexpr float ROOM_SIZE = 50; - constexpr float GAME_ENEMY_SIZE = ROOM_SIZE * 0.7; - constexpr float GAME_FOOD_SIZE = ROOM_SIZE * 0.2; + // const sf::VideoMode GAME_VIDEO_MODE{ 1080, 720 }; + // constexpr char EASY_GAME_TITLE[] = "Level: Easy"; + // constexpr char MEDIUM_GAME_TITLE[] = "Level: Medium"; + // constexpr char HARD_GAME_TITLE[] = "Level: Hard"; + // constexpr float EASY_GAME_ENEMY_RATIO = 0.0f; + // constexpr float MEDIUM_GAME_ENEMY_RATIO = 0.03f; + // constexpr float HARD_GAME_ENEMY_RATIO = 0.07f; + // constexpr float ROOM_SIZE = 50; + // constexpr float GAME_ENEMY_SIZE = ROOM_SIZE * 0.7; + // constexpr float GAME_FOOD_SIZE = ROOM_SIZE * 0.2; // Пакмэн: - constexpr float GAME_PACMAN_SIZE = ROOM_SIZE * 0.8; - constexpr sf::Keyboard::Key KEY_LEFT = sf::Keyboard::A; - constexpr sf::Keyboard::Key KEY_RIGHT = sf::Keyboard::D; - constexpr sf::Keyboard::Key KEY_UP = sf::Keyboard::W; - constexpr sf::Keyboard::Key KEY_DOWN = sf::Keyboard::S; + // constexpr float GAME_PACMAN_SIZE = ROOM_SIZE * 0.8; + // constexpr sf::Keyboard::Key KEY_LEFT = sf::Keyboard::A; + // constexpr sf::Keyboard::Key KEY_RIGHT = sf::Keyboard::D; + // constexpr sf::Keyboard::Key KEY_UP = sf::Keyboard::W; + // constexpr sf::Keyboard::Key KEY_DOWN = sf::Keyboard::S; // Цвета: const sf::Color BUTTON_COLOR_TEXT{ 0, 0, 0 }; const sf::Color BUTTON_COLOR_FILL{ 180, 180, 180 }; const sf::Color BUTTON_COLOR_SELECTION{ 255, 180, 180 }; const sf::Color BUTTON_COLOR_FRAME{ 0, 0, 0 }; const sf::Color SELECT_LEVEL_BACKGROUND_COLOR{ 230,230,230 }; - const sf::Color GAME_COLOR_BACKGROUND_INGAME{ 230,230,230 }; - const sf::Color GAME_COLOR_BACKGROUND_WIN{ 0, 255, 0 }; - const sf::Color GAME_COLOR_BACKGROUND_LOST{ 255, 0, 0 }; - const sf::Color GAME_COLOR_PACMAN{ 250, 150, 0 }; - const sf::Color GAME_COLOR_ROOM{ 255, 255, 255 }; - const sf::Color GAME_COLOR_WALL{ 0, 0, 0 }; - const sf::Color GAME_FOOD_COLOR{ 0, 200, 100 }; - const sf::Color GAME_ENEMY_COLOR{ 255, 50, 0 }; + // const sf::Color GAME_COLOR_BACKGROUND_INGAME{ 230,230,230 }; + // const sf::Color GAME_COLOR_BACKGROUND_WIN{ 0, 255, 0 }; + // const sf::Color GAME_COLOR_BACKGROUND_LOST{ 255, 0, 0 }; + // const sf::Color GAME_COLOR_PACMAN{ 250, 150, 0 }; + // const sf::Color GAME_COLOR_ROOM{ 255, 255, 255 }; + // const sf::Color GAME_COLOR_WALL{ 0, 0, 0 }; + // const sf::Color GAME_FOOD_COLOR{ 0, 200, 100 }; + // const sf::Color GAME_ENEMY_COLOR{ 255, 50, 0 }; } \ No newline at end of file diff --git a/source/States/SelectState/SelectState.cpp b/source/States/SelectState/SelectState.cpp index 3ca9b30b98598de42bab7cad21a89142869e2563..d849f725b89c87bd0853d01065b5426f6fe0646e 100644 --- a/source/States/SelectState/SelectState.cpp +++ b/source/States/SelectState/SelectState.cpp @@ -1,6 +1,72 @@ #include "SelectState.h" #include "../ExitState/ExitState.h" #include "../../Configuration.h" +#include "../../BasicAbstractions/Font.h" + +void Button::draw_into(sf::RenderWindow& window) const { + window.draw(m_rectangle); + window.draw(m_text); +} + +void Button::set(const sf::Vector2f pos, const sf::Vector2f size, const std::string& text, const size_t font_size) {//, std::shared_ptr<ISelectCommand> ptr_command) { + m_rectangle.setSize(size); + m_rectangle.setPosition(pos); + m_rectangle.setFillColor(config::BUTTON_COLOR_FILL); + m_rectangle.setOutlineThickness(config::BUTTON_FRAME_THICKNESS); + m_rectangle.setOutlineColor(config::BUTTON_COLOR_FRAME); + m_text.setFont(MyFont::Instance()); + m_text.setCharacterSize(font_size); + m_text.setFillColor(config::BUTTON_COLOR_TEXT); + m_text.setString(text); + const sf::FloatRect text_bounds = m_text.getLocalBounds(); + const sf::Vector2f button_pos = m_rectangle.getPosition(); + const sf::Vector2f button_size = m_rectangle.getSize(); + m_text.setPosition( + round(button_pos.x + (button_size.x - text_bounds.width) / 2 - text_bounds.left), + round(button_pos.y + (button_size.y - text_bounds.height) / 2 - text_bounds.top) + ); +} + +void Button::select() { + m_rectangle.setFillColor(config::BUTTON_COLOR_SELECTION); +} + +void Button::unselect() { + m_rectangle.setFillColor(config::BUTTON_COLOR_FILL); +} + +bool Button::is_position_in(const sf::Vector2f pos) const { + return pos.x >= m_rectangle.getPosition().x && pos.x <= (m_rectangle.getPosition().x + m_rectangle.getSize().x) + && pos.y >= m_rectangle.getPosition().y && pos.y <= (m_rectangle.getPosition().y + m_rectangle.getSize().y); +} + +Menu::Menu(IStateManager& state_manager) { + const float pos_left = (static_cast<float>(config::SELECT_LEVEL_VIDEO_MODE.width) - config::BUTTON_SIZE.x) / 2; + const float pos_diff = (static_cast<float>(config::SELECT_LEVEL_VIDEO_MODE.height) - config::BUTTON_SIZE.y * 4) / 10; + const float pos_top = (static_cast<float>(config::SELECT_LEVEL_VIDEO_MODE.height) - config::BUTTON_SIZE.y*4 - pos_diff*3) / 2; + m_buttons[0].set(sf::Vector2f{pos_left, pos_top}, + config::BUTTON_SIZE, config::BUTTON_TEXT_EASY, config::BUTTON_FONT_SIZE); + m_buttons[1].set(sf::Vector2f{pos_left, pos_top + config::BUTTON_SIZE.y + pos_diff}, + config::BUTTON_SIZE, config::BUTTON_TEXT_MEDIUM, config::BUTTON_FONT_SIZE); + m_buttons[2].set(sf::Vector2f{pos_left, pos_top + 2*(config::BUTTON_SIZE.y + pos_diff)}, + config::BUTTON_SIZE, config::BUTTON_TEXT_HARD, config::BUTTON_FONT_SIZE); + m_buttons[3].set(sf::Vector2f{pos_left, pos_top + 3*(config::BUTTON_SIZE.y + pos_diff)}, + config::BUTTON_SIZE, config::BUTTON_TEXT_EXIT, config::BUTTON_FONT_SIZE); +} + +void Menu::process_mouse(const sf::Vector2f pos, bool is_pressed) { + for (auto& button : m_buttons) { + if (button.is_position_in(pos)) + button.select(); + else button.unselect(); + } +} + +void Menu::draw_into(sf::RenderWindow& window) const { + for (auto& button : m_buttons) { + button.draw_into(window); + } +} void SelectState::event_handling() { sf::Event event{}; @@ -13,11 +79,13 @@ void SelectState::event_handling() { } void SelectState::update() { - + m_menu.process_mouse(m_window.mapPixelToCoords(sf::Mouse::getPosition(m_window)), true); } void SelectState::render() { - + m_window.clear(config::SELECT_LEVEL_BACKGROUND_COLOR); + m_menu.draw_into(m_window); + m_window.display(); } bool SelectState::do_step() { diff --git a/source/States/SelectState/SelectState.h b/source/States/SelectState/SelectState.h index 62edaa0425ad52631dd9109530361804d3ed72e7..1c793286c525feb64f88b736b71f9211ee367162 100644 --- a/source/States/SelectState/SelectState.h +++ b/source/States/SelectState/SelectState.h @@ -7,23 +7,23 @@ class Button: public IDrawable { public: Button() = default; - void set(sf::Vector2f pos, sf::Vector2f size, std::string text, size_t font_size, std::shared_ptr<ISelectCommand> ptr_command) {} - bool select() {} - bool unselect() {} - bool is_position_in(sf::Vector2f pos) {} - void push() {} - void draw_into(sf::RenderWindow& window) const override {} + void set(sf::Vector2f pos, sf::Vector2f size, const std::string& text, size_t font_size);//, std::shared_ptr<ISelectCommand> ptr_command); + void select(); + void unselect(); + bool is_position_in(sf::Vector2f pos) const; + // void push(); + void draw_into(sf::RenderWindow& window) const override; private: sf::Text m_text; sf::RectangleShape m_rectangle; - std::shared_ptr<ISelectCommand> m_ptr_command; + //std::shared_ptr<ISelectCommand> m_ptr_command; /// @todo shared_ptr? }; class Menu: public IDrawable { public: - explicit Menu(IStateManager& state_manager) {} - void process_mouse(sf::Vector2f pos, bool is_pressed) {} - void draw_into(sf::RenderWindow& window) const override {} + explicit Menu(IStateManager& state_manager); + void process_mouse(sf::Vector2f pos, bool is_pressed); + void draw_into(sf::RenderWindow& window) const override; private: std::array<Button, 4> m_buttons; };