diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d2bae15180a3cb061977cf8adec38bb1036eb81..6b08caf2d312fc7ac801d6eceee9304b947de03d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,5 +7,10 @@ set(BUILD_SHARED_LIBS FALSE) FetchContent_Declare(sfml GIT_REPOSITORY https://github.com/SFML/SFML.git GIT_TAG 2.6.1) FetchContent_MakeAvailable(sfml) -add_executable(pac-man source/main.cpp) +add_executable(pac-man + "source/main.cpp" + "source/Application.cpp" + "source/State/SelectState.cpp") + +target_include_directories(pac-man PUBLIC include/) target_link_libraries(pac-man PUBLIC sfml-window sfml-graphics sfml-system) \ No newline at end of file diff --git a/include/Application.h b/include/Application.h new file mode 100644 index 0000000000000000000000000000000000000000..9abda4a0837bc1b7f9a323c69072b71db0cb46d6 --- /dev/null +++ b/include/Application.h @@ -0,0 +1,15 @@ +#pragma once + +#include <States/IState.h> + +class Application: public IStateManager{ +private: + void set_next_state(std::unique_ptr<IState>&& ptr_state) override; + void apply_differ_state_change() noexcept; +private: + std::unique_ptr<IState> m_ptr_state_next; + std::unique_ptr<IState> m_ptr_state_current; +public: + Application(); + int run(); +}; \ No newline at end of file diff --git a/include/States/ExitState.h b/include/States/ExitState.h new file mode 100644 index 0000000000000000000000000000000000000000..eb8fbe6ef6d1c6435ee64cb109b656fc814591d0 --- /dev/null +++ b/include/States/ExitState.h @@ -0,0 +1,8 @@ +// +// Created by РРіРѕСЂСЊ on 22.03.2025. +// + +#ifndef PAC_MAN_EXITSTATE_H +#define PAC_MAN_EXITSTATE_H + +#endif //PAC_MAN_EXITSTATE_H diff --git a/include/States/GameState.h b/include/States/GameState.h new file mode 100644 index 0000000000000000000000000000000000000000..3a8b34ac0d468d6b96222f6a238b9ac92613603e --- /dev/null +++ b/include/States/GameState.h @@ -0,0 +1,8 @@ +// +// Created by РРіРѕСЂСЊ on 22.03.2025. +// + +#ifndef PAC_MAN_GAMESTATE_H +#define PAC_MAN_GAMESTATE_H + +#endif //PAC_MAN_GAMESTATE_H diff --git a/include/States/IState.h b/include/States/IState.h new file mode 100644 index 0000000000000000000000000000000000000000..01387b909892235a0943dc4c82dad59dc1e556ed --- /dev/null +++ b/include/States/IState.h @@ -0,0 +1,19 @@ +#pragma once + +#include <memory> + +struct IStateManager; + +class IState { +public: + IState(IStateManager& state_manager): m_state_manager(state_manager) {} + virtual bool do_step() = 0; + virtual ~IState() = default; +protected: + IStateManager& m_state_manager; +}; + +struct IStateManager { + virtual void set_next_state(std::unique_ptr<IState>&& state) = 0; + virtual ~IStateManager() = default; +}; \ No newline at end of file diff --git a/include/States/SelectState.h b/include/States/SelectState.h new file mode 100644 index 0000000000000000000000000000000000000000..9b90bc7090d55ab010cc4e1758647a12e93209e5 --- /dev/null +++ b/include/States/SelectState.h @@ -0,0 +1,6 @@ +#pragma once + +class SelectState: public IState { +public: + SelectState(state_manager, video_mode, window_title); +}; \ No newline at end of file diff --git a/source/Application.cpp b/source/Application.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9f7d9e40ebd6707f05a4969ea9f10821163ad90 --- /dev/null +++ b/source/Application.cpp @@ -0,0 +1,28 @@ +#include "Application.h" + +#include <iostream> + +void Application::set_next_state(std::unique_ptr<IState>&& ptr_state) { + m_ptr_state_next = std::move(ptr_state); +} + +void Application::apply_differ_state_change() noexcept { + if (m_ptr_state_next) + m_ptr_state_current = std::move(m_ptr_state_next); +} + +int Application::run() { + try { + while (m_ptr_state_current->do_step()) + apply_differ_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 diff --git a/source/Application/Application.cpp b/source/Application/Application.cpp deleted file mode 100644 index e6b9145d7aa8c05ae70e90cf76c1313b989a7e8a..0000000000000000000000000000000000000000 --- a/source/Application/Application.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include Application.h - -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; -} diff --git a/source/Application/Application.h b/source/Application/Application.h deleted file mode 100644 index 95adca8da4e034e258d30b4ce4f2deafd57ee6e4..0000000000000000000000000000000000000000 --- a/source/Application/Application.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -class Application: public IStateManager{ -public: - int run(); -private: - void set_next_stage(State* ptr_state); - void apply_differ_state_change(); -private: - IState m_ptr_state_next; - IState m_ptr_state_current; -}; \ No newline at end of file diff --git a/source/State/ExitState.cpp b/source/State/ExitState.cpp new file mode 100644 index 0000000000000000000000000000000000000000..637a979a911067cb935bb0abd7e065d1f848be68 --- /dev/null +++ b/source/State/ExitState.cpp @@ -0,0 +1,3 @@ +// +// Created by РРіРѕСЂСЊ on 22.03.2025. +// diff --git a/source/State/GameState.cpp b/source/State/GameState.cpp new file mode 100644 index 0000000000000000000000000000000000000000..637a979a911067cb935bb0abd7e065d1f848be68 --- /dev/null +++ b/source/State/GameState.cpp @@ -0,0 +1,3 @@ +// +// Created by РРіРѕСЂСЊ on 22.03.2025. +// diff --git a/source/State/IState.h b/source/State/IState.h deleted file mode 100644 index 655ccfdc83cf7320f852abceea6579bd3bd1c903..0000000000000000000000000000000000000000 --- a/source/State/IState.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -class IState { -public: - IState(IStateManger& state manager) {} - bool do_step(); -protected: - IStateManager m_state_manager; -}; - diff --git a/source/State/IStateManager.h b/source/State/IStateManager.h deleted file mode 100644 index 0a63f2b6181d18d8b9beb16460756f5b0393a98e..0000000000000000000000000000000000000000 --- a/source/State/IStateManager.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -class IStateManager { -public: - void set_next_state(IState* state); -}; \ No newline at end of file diff --git a/source/State/SelectState.cpp b/source/State/SelectState.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa238619fdb234c2a87864481c919a1d2a9a5f29 --- /dev/null +++ b/source/State/SelectState.cpp @@ -0,0 +1 @@ +#include "SelectState.h" \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 71938c9c19afb69573467a39a4b56ab5f74cd4ca..8bd8815d43903cda2da3bc5e9810b9830a3f3029 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,4 +1,6 @@ -#include "SFML/Graphics.hpp" +//#include "SFML/Graphics.hpp" + +#include <Application.h> //int main() { // sf::RenderWindow window(sf::VideoMode(500, 500), "SFML works!"); // создаем РѕРєРЅРѕ 500 РЅР° 500 пикселей