From 45a8c7be3c8bc3891b328667062bf0b070411a78 Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Tue, 29 Nov 2022 14:22:03 +0000 Subject: [PATCH] begin adding SceneManager --- CMakeLists.txt | 7 ++- include/engine.hpp | 28 ++++++---- include/{input.hpp => input_manager.hpp} | 12 ++--- include/scene_manager.hpp | 17 ++++++ src/engine.cpp | 21 ++++---- src/{input.cpp => input_manager.cpp} | 68 ++++++++++++------------ src/scene_manager.cpp | 15 ++++++ 7 files changed, 103 insertions(+), 65 deletions(-) rename include/{input.hpp => input_manager.hpp} (91%) create mode 100644 include/scene_manager.hpp rename src/{input.cpp => input_manager.cpp} (61%) create mode 100644 src/scene_manager.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 650f227..89dd24a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,8 +12,10 @@ project(engine LANGUAGES CXX set(SRC_FILES "src/engine.cpp" "src/window.cpp" - "src/input.cpp" + "src/input_manager.cpp" + "src/scene_manager.cpp" "src/gfx_device_vulkan.cpp" + "src/util/files.cpp" ) @@ -26,7 +28,8 @@ set(INCLUDE_FILES "include/window.hpp" "include/inputs/keyboard.hpp" "include/inputs/mouse.hpp" - "include/input.hpp" + "include/input_manager.hpp" + "include/scene_manager.hpp" "include/gfx.hpp" "include/gfx_device.hpp" "include/util/files.hpp" diff --git a/include/engine.hpp b/include/engine.hpp index 94eb4af..d23cfec 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -1,31 +1,37 @@ #pragma once +#include + namespace engine { class Window; class GFXDevice; - class Input; + class InputManager; + class SceneManager; class Application { + public: Application(const char* appName, const char* appVersion); - + ~Application(); Application(const Application&) = delete; Application& operator=(const Application&) = delete; - ~Application(); - + /* methods */ void gameLoop(); - Window* window() { return m_win; } - GFXDevice* gfx() { return m_gfx; } - Input* input() { return m_input; } + /* getters */ + Window* window() { return m_window.get(); } + GFXDevice* gfx() { return m_gfx.get(); } + InputManager* inputManager() { return m_inputManager.get(); } + SceneManager* sceneManager() { return m_sceneManager.get(); } private: - Window* m_win; - GFXDevice* m_gfx; - Input* m_input; + std::unique_ptr m_window; + std::unique_ptr m_gfx; + std::unique_ptr m_inputManager; + std::unique_ptr m_sceneManager; }; -} +} \ No newline at end of file diff --git a/include/input.hpp b/include/input_manager.hpp similarity index 91% rename from include/input.hpp rename to include/input_manager.hpp index 86ca3d2..585c5a2 100644 --- a/include/input.hpp +++ b/include/input_manager.hpp @@ -21,15 +21,15 @@ namespace engine { }; // This class should be used to get platform/input-device independent input - class ENGINE_API Input { + class ENGINE_API InputManager { public: // requires a window reference to get input from - Input(const Window& win); - Input(const Input&) = delete; - Input& operator=(const Input&) = delete; - ~Input(); + InputManager(const Window* win); + InputManager(const InputManager&) = delete; + InputManager& operator=(const InputManager&) = delete; + ~InputManager(); // Add a mouse input void addInputButton(const std::string& name, inputs::MouseButton button); @@ -68,7 +68,7 @@ namespace engine { int low; }; - const Window& m_win; + const Window* const m_win; std::vector m_buttonEntries; std::vector m_axisEntries; diff --git a/include/scene_manager.hpp b/include/scene_manager.hpp new file mode 100644 index 0000000..a113a8f --- /dev/null +++ b/include/scene_manager.hpp @@ -0,0 +1,17 @@ +#pragma once + +namespace engine { + + class SceneManager { + + public: + SceneManager(); + ~SceneManager(); + SceneManager(const SceneManager&) = delete; + SceneManager& operator=(const SceneManager&) = delete; + + private: + + }; + +} \ No newline at end of file diff --git a/src/engine.cpp b/src/engine.cpp index 2d3c4f1..4c5941c 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -4,7 +4,8 @@ #include "window.hpp" #include "gfx_device.hpp" -#include "input.hpp" +#include "input_manager.hpp" +#include "scene_manager.hpp" // To allow the FPS-limiter to put the thread to sleep #include @@ -13,17 +14,13 @@ namespace engine { Application::Application(const char* appName, const char* appVersion) { - m_win = new Window(appName, true, true); - m_gfx = new GFXDevice(appName, appVersion, m_win->getHandle()); - m_input = new Input(*m_win); + m_window = std::make_unique(appName, true, true); + m_gfx = std::make_unique(appName, appVersion, m_window->getHandle()); + m_inputManager = std::make_unique(window()); + m_sceneManager = std::make_unique(); } - Application::~Application() - { - delete m_input; - delete m_gfx; - delete m_win; - } + Application::~Application() {} void Application::gameLoop() { @@ -35,7 +32,7 @@ namespace engine { auto endFrame = beginFrame + FRAMETIME_LIMIT; // single-threaded game loop - while (m_win->isRunning()) { + while (m_window->isRunning()) { /* logic */ @@ -43,7 +40,7 @@ namespace engine { m_gfx->renderFrame(); /* poll events */ - m_win->getInputAndEvents(); + m_window->getInputAndEvents(); /* fps limiter */ std::this_thread::sleep_until(endFrame); diff --git a/src/input.cpp b/src/input_manager.cpp similarity index 61% rename from src/input.cpp rename to src/input_manager.cpp index be1b9fa..334d6ec 100644 --- a/src/input.cpp +++ b/src/input_manager.cpp @@ -1,4 +1,4 @@ -#include "input.hpp" +#include "input_manager.hpp" #include "window.hpp" @@ -7,30 +7,30 @@ namespace engine { - Input::Input(const Window& win) : m_win(win) + InputManager::InputManager(const Window* win) : m_win(win) { m_enabledDevices.fill(true); } - Input::~Input() + InputManager::~InputManager() { } // private methods - float Input::getDeviceAxis(enum InputDevice device, int axis) const + float InputManager::getDeviceAxis(enum InputDevice device, int axis) const { switch (device) { case InputDevice::MOUSE: switch (static_cast(axis)) { case inputs::MouseAxis::X: - return static_cast(m_win.getMouseDX()); + return static_cast(m_win->getMouseDX()); case inputs::MouseAxis::Y: - return static_cast(m_win.getMouseDY()); + return static_cast(m_win->getMouseDY()); case inputs::MouseAxis::X_SCR: - return m_win.getMouseScrollX(); + return m_win->getMouseScrollX(); case inputs::MouseAxis::Y_SCR: - return m_win.getMouseScrollY(); + return m_win->getMouseScrollY(); default: break; } break; @@ -43,13 +43,13 @@ namespace engine { throw std::runtime_error("Error getting device axis"); } - bool Input::getDeviceButton(enum InputDevice device, int button) const + bool InputManager::getDeviceButton(enum InputDevice device, int button) const { switch (device) { case InputDevice::MOUSE: - return m_win.getButton(static_cast(button)); + return m_win->getButton(static_cast(button)); case InputDevice::KEYBOARD: - return m_win.getKey(static_cast(button)); + return m_win->getKey(static_cast(button)); case InputDevice::CONTROLLER: break; default: break; @@ -57,13 +57,13 @@ namespace engine { throw std::runtime_error("Error getting device button"); } - bool Input::getDeviceButtonDown(enum InputDevice device, int button) const + bool InputManager::getDeviceButtonDown(enum InputDevice device, int button) const { switch (device) { case InputDevice::MOUSE: - return m_win.getButtonPress(static_cast(button)); + return m_win->getButtonPress(static_cast(button)); case InputDevice::KEYBOARD: - return m_win.getKeyPress(static_cast(button)); + return m_win->getKeyPress(static_cast(button)); case InputDevice::CONTROLLER: break; default: break; @@ -71,13 +71,13 @@ namespace engine { throw std::runtime_error("Error getting device button"); } - bool Input::getDeviceButtonUp(enum InputDevice device, int button) const + bool InputManager::getDeviceButtonUp(enum InputDevice device, int button) const { switch (device) { case InputDevice::MOUSE: - return m_win.getButtonRelease(static_cast(button)); + return m_win->getButtonRelease(static_cast(button)); case InputDevice::KEYBOARD: - return m_win.getKeyRelease(static_cast(button)); + return m_win->getKeyRelease(static_cast(button)); case InputDevice::CONTROLLER: break; default: break; @@ -85,7 +85,7 @@ namespace engine { throw std::runtime_error("Error getting device button"); } - float Input::getButtonAxis(enum InputDevice device, int high, int low) const + float InputManager::getButtonAxis(enum InputDevice device, int high, int low) const { float value = 0.0f; if (getDeviceButton(device, high)) value += 1.0f; @@ -97,17 +97,17 @@ namespace engine { // public methods - void Input::addInputButton(const std::string& name, InputDevice device, int button) + void InputManager::addInputButton(const std::string& name, InputDevice device, int button) { m_buttonEntries.push_back({ name, device, button }); } - void Input::addInputAxis(const std::string& name, InputDevice device, int axis) + void InputManager::addInputAxis(const std::string& name, InputDevice device, int axis) { m_axisEntries.push_back({ name, device, axis, false, 0, 0 }); } - void Input::addInputButtonAsAxis(const std::string& name, InputDevice device, int high, int low) + void InputManager::addInputButtonAsAxis(const std::string& name, InputDevice device, int high, int low) { m_axisEntries.push_back({ name, device, 0, true, high, low }); } @@ -115,56 +115,56 @@ namespace engine { // OVERLOADS: // Add a mouse input - void Input::addInputButton(const std::string& name, inputs::MouseButton button) + void InputManager::addInputButton(const std::string& name, inputs::MouseButton button) { addInputButton(name, InputDevice::MOUSE, static_cast(button)); } - void Input::addInputAxis(const std::string& name, inputs::MouseAxis axis) + void InputManager::addInputAxis(const std::string& name, inputs::MouseAxis axis) { addInputAxis(name, InputDevice::MOUSE, static_cast(axis)); } - void Input::addInputButtonAsAxis(const std::string& name, inputs::MouseButton high, inputs::MouseButton low) + void InputManager::addInputButtonAsAxis(const std::string& name, inputs::MouseButton high, inputs::MouseButton low) { addInputButtonAsAxis(name, InputDevice::MOUSE, static_cast(high), static_cast(low)); } - void Input::addInputButton(const std::string& name, inputs::Key button) + void InputManager::addInputButton(const std::string& name, inputs::Key button) { addInputButton(name, InputDevice::KEYBOARD, static_cast(button)); } - void Input::addInputButtonAsAxis(const std::string& name, inputs::Key high, inputs::Key low) + void InputManager::addInputButtonAsAxis(const std::string& name, inputs::Key high, inputs::Key low) { addInputButtonAsAxis(name, InputDevice::KEYBOARD, static_cast(high), static_cast(low)); } - void Input::delInputButton(int index) + void InputManager::delInputButton(int index) { std::vector::iterator it = m_buttonEntries.begin(); std::advance(it, index); m_buttonEntries.erase(it); } - void Input::delInputAxis(int index) + void InputManager::delInputAxis(int index) { std::vector::iterator it = m_axisEntries.begin(); std::advance(it, index); m_axisEntries.erase(it); } - void Input::setDeviceActive(enum InputDevice device, bool active) + void InputManager::setDeviceActive(enum InputDevice device, bool active) { m_enabledDevices[static_cast(device)] = active; } - bool Input::getDeviceActive(enum InputDevice device) const + bool InputManager::getDeviceActive(enum InputDevice device) const { return m_enabledDevices[static_cast(device)]; } - float Input::getAxis(const std::string& axisName) const + float InputManager::getAxis(const std::string& axisName) const { for (const AxisEntry& e : m_axisEntries) { if (e.name == axisName) { @@ -182,7 +182,7 @@ namespace engine { // throw std::runtime_error("Unable to find mapping in input table"); } - bool Input::getButton(const std::string& buttonName) const + bool InputManager::getButton(const std::string& buttonName) const { bool isDown = false; @@ -199,7 +199,7 @@ namespace engine { return isDown; } - bool Input::getButtonPress(const std::string& buttonName) const + bool InputManager::getButtonPress(const std::string& buttonName) const { bool isPressed = false; @@ -216,7 +216,7 @@ namespace engine { return isPressed; } - bool Input::getButtonRelease(const std::string& buttonName) const + bool InputManager::getButtonRelease(const std::string& buttonName) const { bool isReleased = false; diff --git a/src/scene_manager.cpp b/src/scene_manager.cpp new file mode 100644 index 0000000..4e423b8 --- /dev/null +++ b/src/scene_manager.cpp @@ -0,0 +1,15 @@ +#include "scene_manager.hpp" + +namespace engine { + + SceneManager::SceneManager() + { + + } + + SceneManager::~SceneManager() + { + + } + +} \ No newline at end of file