Commit df67003c authored by Дорожкин Дмитрий Михайлович's avatar Дорожкин Дмитрий Михайлович :speech_balloon:
Browse files

well at least it somehow works

parent ebc8f9e1
No related merge requests found
Showing with 202 additions and 30 deletions
+202 -30
......@@ -13,4 +13,5 @@ set(BUILD_SHARED_LIBS OFF) # Отключаем динамическую лин
FetchContent_Declare(SFML GIT_REPOSITORY https://github.com/SFML/SFML.git GIT_TAG 2.6.1)
FetchContent_MakeAvailable(SFML)
target_link_libraries(application PRIVATE sfml-window sfml-audio sfml-graphics sfml-system)
\ No newline at end of file
target_link_libraries(application PRIVATE sfml-window sfml-audio sfml-graphics sfml-system)
target_compile_definitions(application PUBLIC ASSETS_PATH="${CMAKE_CURRENT_SOURCE_DIR}/assets/")
\ No newline at end of file
File added
#include "application.hpp"
#include "config.hpp"
#include <iostream>
// int Application::run() {
// try {
// while (m_ptr_state_current->do_step())
// apply_deffer_state_change();
// }
// catch (std::exception& ex) {
// std::cout << ex.what() << '\n';
// return 1;
// }
// catch (...) {
// std::cout << "Unknown exception\n";
// return 2;
// }
// return 0;
// }
\ No newline at end of file
Application::Application(): m_ptr_state_current(std::make_unique<SelectState>(*this, config::SELECT_LEVEL_VIDEO_MODE, config::SELECT_LEVEL_TITLE)){
}
int Application::run() {
try {
while (m_ptr_state_current->do_step())
apply_deffer_state_change();
}
catch (std::exception& ex) {
std::cout << ex.what() << '\n';
return 1;
}
catch (...) {
std::cout << "Unknown exception\n";
return 2;
}
return 0;
}
void Application::set_next_state(std::unique_ptr<IState> ptr_state) {
m_ptr_state_next.swap(ptr_state);
}
void Application::apply_deffer_state_change() {
if (m_ptr_state_next) {
m_ptr_state_current = std::move(m_ptr_state_next);
}
}
......@@ -4,11 +4,12 @@
class Application: public IStateManager {
public:
~Application() override;
~Application() override = default;
Application();
int run();
private:
void set_next_state(IState* ptr_state) override;
void set_next_state(std::unique_ptr<IState> ptr_state) override;
void apply_deffer_state_change();
std::unique_ptr<IState> m_ptr_state_next;
std::unique_ptr<IState> m_ptr_state_current;
std::unique_ptr<IState> m_ptr_state_next = nullptr;
};
#include "SFML/Graphics.hpp"
namespace config {
// Общее:
const unsigned int FRAME_RATE_LIMIT = 60;
// Меню:
const sf::Vector2f BUTTON_SIZE = { 250, 100 };
const size_t BUTTON_FONT_SIZE = static_cast<size_t>(BUTTON_SIZE.y / 1.5f);
const float BUTTON_FRAME_THICKNESS = 2.0f;
const char FONT_FILE[] = "MedodicaRegular.otf";
const char SELECT_LEVEL_TITLE[] = "Select Level";
const sf::VideoMode SELECT_LEVEL_VIDEO_MODE{ 400, 600 };
const char BUTTON_TEXT_EASY[] = "Easy";
const char BUTTON_TEXT_MEDIUM[] = "Medium";
const char BUTTON_TEXT_HARD[] = "Hard";
const char BUTTON_TEXT_EXIT[] = "Exit";
// Игра:
const sf::VideoMode GAME_VIDEO_MODE{ 1080, 720 };
const char EASY_GAME_TITLE[] = "Level: Easy";
const char MEDIUM_GAME_TITLE[] = "Level: Medium";
const char HARD_GAME_TITLE[] = "Level: Hard";
const float EASY_GAME_ENEMY_RATIO = 0.0f;
const float MEDIUM_GAME_ENEMY_RATIO = 0.03f;
const float HARD_GAME_ENEMY_RATIO = 0.07f;
const float ROOM_SIZE = 50;
const float GAME_ENEMY_SIZE = ROOM_SIZE * 0.7;
const float GAME_FOOD_SIZE = ROOM_SIZE * 0.2;
// Пакмэн:
const float GAME_PACMAN_SIZE = ROOM_SIZE * 0.8;
const sf::Keyboard::Key KEY_LEFT = sf::Keyboard::A;
const sf::Keyboard::Key KEY_RIGHT = sf::Keyboard::D;
const sf::Keyboard::Key KEY_UP = sf::Keyboard::W;
const 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 };
}
\ No newline at end of file
#include "drawable.hpp"
#include "config.hpp"
sf::Font& MyFont::Instance() {
static MyFont font;
return font.m_font;
}
MyFont::MyFont() {
if (!m_font.loadFromFile(ASSETS_PATH + std::string(config::FONT_FILE)))
throw std::runtime_error("Failed to load font");
}
void Button::set(sf::Vector2f pos, sf::Vector2f button_size, std::string text,
size_t font_size, ISelectCommand& ptr_command) {
m_rectangle.setSize(button_size);
m_rectangle.setOrigin(button_size / 2.0f);
m_rectangle.setPosition(pos);
m_rectangle.setOutlineThickness(config::BUTTON_FRAME_THICKNESS);
m_text.setString(text);
m_text.setFont(MyFont::Instance());
auto bounds = m_text.getLocalBounds();
m_text.setOrigin(bounds.getPosition() + bounds.getSize() / 2.0f);
m_text.setPosition(pos);
}
#pragma once
\ No newline at end of file
#pragma once
#include <SFML/Graphics.hpp>
#include <array>
#include "states.hpp"
class MyFont {
public:
static sf::Font& Instance();
private:
sf::Font m_font;
MyFont();
};
class ISelectCommand {
public:
virtual ~ISelectCommand() = default;
virtual void execute() = 0;
};
struct IDrawable {
virtual void draw_into(sf::RenderWindow& window) const = 0;
virtual ~IDrawable() = default;
};
class Button {
public:
Button();
void set(sf::Vector2f pos, sf::Vector2f button_size, std::string text,
size_t font_size, ISelectCommand& ptr_command);
void select();
void unselect();
bool is_position_in(sf::Vector2f pos);
void push();
private:
sf::RectangleShape m_rectangle;
sf::Text m_text;
ISelectCommand* m_command;
};
class Menu: public IDrawable {
public:
Menu(IStateManager& stateManager);
void process_mouse(sf::Vector2f pos, bool is_pressed);
private:
std::array<Button*, 4> drawables;
};
\ No newline at end of file
......@@ -2,6 +2,6 @@
#include "application.hpp"
int main() {
// Application app;
// return app.run();
Application app;
return app.run();
}
\ No newline at end of file
#include "states.hpp"
IState::IState(IStateManager& state_manager): m_state_manager(state_manager) {}
bool SelectState::do_step() {
event_handling();
update();
render();
return true;
}
SelectState::SelectState(IStateManager& state_manager,
sf::VideoMode video_mode, std::string window_title):
IWindowKeeper(video_mode, window_title), IState(state_manager) {}
void SelectState::event_handling() {
sf::Event event;
while (m_window.pollEvent(event)) {
}
}
void SelectState::update() {
}
void SelectState::render() {
}
......@@ -6,22 +6,28 @@ class IState;
class IStateManager {
public:
virtual ~IStateManager() = default;
virtual void set_next_state(IState* state);
virtual void set_next_state(std::unique_ptr<IState> state) = 0;
};
class IState {
public:
virtual ~IState();
virtual ~IState() = default;
IState(IStateManager& state_manager);
virtual bool do_step();
virtual bool do_step() = 0;
protected:
IStateManager& m_state_manager;
};
class SelectState: public IState, public IWindowKeeper {
public:
~SelectState() override = default;
bool do_step() override;
SelectState(IStateManager& state_manager,
sf::VideoMode video_mode, std::string window_title);
private:
void event_handling() override;
void update() override;
void render() override;
};
class GameState : public IState, public IWindowKeeper {
......
#include "window.hpp"
IWindowKeeper::IWindowKeeper(sf::VideoMode video_mode, std::string window_title):
m_window(video_mode, window_title) {}
......@@ -4,11 +4,11 @@
class IWindowKeeper {
public:
virtual ~IWindowKeeper();
virtual ~IWindowKeeper() = default;
IWindowKeeper(sf::VideoMode video_mode, std::string window_title);
protected:
void event_handling();
void update();
void render();
sf::RenderWindow window;
virtual void event_handling() = 0;
virtual void update() = 0;
virtual void render() = 0;
sf::RenderWindow m_window;
};
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment