diff --git a/CMakeLists.txt b/CMakeLists.txt index 89dd24a..4b256b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,9 @@ set(SRC_FILES "src/scene_manager.cpp" "src/gfx_device_vulkan.cpp" + "src/scene.cpp" + "src/object.cpp" + "src/util/files.cpp" ) @@ -32,6 +35,10 @@ set(INCLUDE_FILES "include/scene_manager.hpp" "include/gfx.hpp" "include/gfx_device.hpp" + + "include/scene.hpp" + "include/object.hpp" + "include/util/files.hpp" ) diff --git a/include/engine.hpp b/include/engine.hpp index d23cfec..8112bd7 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -4,10 +4,10 @@ namespace engine { - class Window; - class GFXDevice; - class InputManager; - class SceneManager; + class Window; // "window.hpp" + class GFXDevice; // "gfx_device.hpp" + class InputManager; // "input_manager.hpp" + class SceneManager; // "scene_manager.hpp" class Application { diff --git a/include/gfx.hpp b/include/gfx.hpp index e54dc37..0f4f9d7 100644 --- a/include/gfx.hpp +++ b/include/gfx.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include namespace engine::gfx { diff --git a/include/gfx_device.hpp b/include/gfx_device.hpp index 62a5b64..b202817 100644 --- a/include/gfx_device.hpp +++ b/include/gfx_device.hpp @@ -1,16 +1,14 @@ #pragma once -#include "engine_api.h" - #include "gfx.hpp" #include -struct SDL_Window; +struct SDL_Window; // namespace engine { - class ENGINE_API GFXDevice { + class GFXDevice { public: GFXDevice(const char* appName, const char* appVersion, SDL_Window* window, bool vsync = false); diff --git a/include/input_manager.hpp b/include/input_manager.hpp index 585c5a2..344ef64 100644 --- a/include/input_manager.hpp +++ b/include/input_manager.hpp @@ -11,7 +11,7 @@ namespace engine { - class Window; + class Window; // "window.hpp" enum class InputDevice : int { MOUSE, diff --git a/include/object.hpp b/include/object.hpp index 5b15ddb..2127f66 100644 --- a/include/object.hpp +++ b/include/object.hpp @@ -2,105 +2,79 @@ #include "engine_api.h" +#include "log.hpp" + #include - -#include "transform.hpp" - -#include "components/component.hpp" +#include #include #include #include #include -#include namespace engine { - class Window; - class Input; - class ResourceManager; - class SceneRoot; - class Component; + /* forward declarations */ + class Scene; - namespace components { - class Camera; - class Renderer; - class UI; - class CustomComponent; - } + class Component { - // This object lives until it is deleted by its parent(s) or finally when the "Scene" is destroyed. - // Therefore it is safe to return raw pointers - class ENGINE_API Object { + }; + + struct Transform { + // Scale, rotate (XYZ), translate + glm::vec3 position{ 0.0f }; + glm::quat rotation{}; + glm::vec3 scale{ 1.0f }; + }; + + class Object { public: - Object(std::string name, Object* parent, SceneRoot& root); + Object(const std::string& name, Object* parent, Scene* scene); Object(const Object&) = delete; Object& operator=(const Object&) = delete; ~Object(); - Window& win; - Input& inp; - ResourceManager& res; + /* methods */ - - SceneRoot& root; - - std::string getName(); - - Object* getParent(); - - Object* getChild(std::string name); + Object* getChild(const std::string& name); std::vector getChildren(); - - Object* createChild(std::string name); - void deleteChild(std::string name); + Object* createChild(const std::string& name); + bool deleteChild(const std::string& name); void printTree(int level = 0); // Returns the component of type T // Returns nullptr if the component is not found. template T* getComponent(); - - // returns the component added template T* createComponent(); + template bool deleteComponent(); - template void deleteComponent(); + /* public member variables */ - struct CompList { - std::vector> cameras; - std::vector> renderers; - std::vector> uis; - std::vector> customs; - }; - - // Adds to the provided vector all components of this object and of its children recursively. - // Ignores 'Transform' - void getAllSubComponents(struct CompList& compList, glm::mat4 t); + const std::string name; + + Object* const parent; + Scene* const scene; Transform transform; private: - static int s_object_count; - int m_id = s_object_count; - std::string m_name; + static int s_next_id; + int m_id = s_next_id; + // If nullptr, this is the root object std::list> m_children{}; std::list> m_components{}; - // If nullptr, this is the root object - Object* const m_parent; - struct GameIO m_gameIO; }; - // implementation of template functions + /* implementation of template functions */ template T* Object::getComponent() { - if (std::is_base_of::value == false) { - throw std::runtime_error("getComponent() error: specified type is not a subclass of 'Component'"); - } for (const auto& component : m_components) { T* derived = dynamic_cast(component.get()); if (derived != nullptr) { @@ -113,27 +87,26 @@ namespace engine { template T* Object::createComponent() { if (std::is_base_of::value == false) { - throw std::runtime_error("addComponent() error: specified type is not a subclass of 'Component'"); + ERROR("Object::createComponent(): attempt to create a component with a non-component class"); + return nullptr; } if (getComponent() != nullptr) { - throw std::runtime_error("addComponent() error: attempt to add component of a type already present"); + ERROR("Object::createComponent(): attempt to create a component that already exists on an object"); + return nullptr; } m_components.emplace_back(std::make_unique(this)); return dynamic_cast(m_components.back().get()); } - template void Object::deleteComponent() + template bool Object::deleteComponent() { - if (std::is_base_of::value == false) { - throw std::runtime_error("deleteComponent() error: specified type is not a subclass of 'Component'"); - } for (auto itr = m_components.begin(); itr != m_components.end(); ++itr) { if (dynamic_cast((*itr).get()) != nullptr) { m_components.erase(itr); - return; + return true; } } - throw std::runtime_error("deleteComponent() error: attempt to delete component that is not present."); + return false; } } diff --git a/include/scene.hpp b/include/scene.hpp new file mode 100644 index 0000000..81c73c3 --- /dev/null +++ b/include/scene.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "object.hpp" + +namespace engine { + + class Scene : public Object { + + public: + Scene(); + Scene(const Scene&) = delete; + Scene& operator=(const Scene&) = delete; + ~Scene() = delete; + + private: + + }; + +} \ No newline at end of file diff --git a/include/scene_manager.hpp b/include/scene_manager.hpp index a113a8f..fd5a9ae 100644 --- a/include/scene_manager.hpp +++ b/include/scene_manager.hpp @@ -1,7 +1,12 @@ #pragma once +#include +#include + namespace engine { + class Scene; // "scene.hpp" + class SceneManager { public: @@ -11,6 +16,8 @@ namespace engine { SceneManager& operator=(const SceneManager&) = delete; private: + std::list> m_scenes; + std::list>::iterator m_activeScene{}; }; diff --git a/include/window.hpp b/include/window.hpp index 718cbc7..0215f64 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -12,8 +12,6 @@ #include #include -ENGINE_API extern const uint64_t BILLION; - namespace engine { class ENGINE_API Window { diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 6187aec..8fb8ed1 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -1,5 +1,4 @@ // The implementation of the graphics layer using Vulkan 1.3. -// This uses SDL specific code //#undef ENGINE_BUILD_VULKAN #ifdef ENGINE_BUILD_VULKAN @@ -355,7 +354,7 @@ namespace engine { return surface; } - // returns the index of the queue supporting the requested flags + // returns the queue supporting the requested flags static Queue getQueueSupporting(const std::vector queues, QueueFlags flags) { uint32_t bitmask = static_cast(flags); diff --git a/src/input_manager.cpp b/src/input_manager.cpp index 334d6ec..d4d93d3 100644 --- a/src/input_manager.cpp +++ b/src/input_manager.cpp @@ -12,11 +12,9 @@ namespace engine { m_enabledDevices.fill(true); } - InputManager::~InputManager() - { - } + InputManager::~InputManager() {} - // private methods + /* private methods */ float InputManager::getDeviceAxis(enum InputDevice device, int axis) const { @@ -114,7 +112,6 @@ namespace engine { // OVERLOADS: - // Add a mouse input void InputManager::addInputButton(const std::string& name, inputs::MouseButton button) { addInputButton(name, InputDevice::MOUSE, static_cast(button)); @@ -179,7 +176,6 @@ namespace engine { } } return 0.0f; // instead of throwing an exception, just return nothing - // throw std::runtime_error("Unable to find mapping in input table"); } bool InputManager::getButton(const std::string& buttonName) const diff --git a/src/object.cpp b/src/object.cpp index 46efc52..65bd195 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -1,46 +1,23 @@ #include "object.hpp" -#include "components/camera.hpp" -#include "components/mesh_renderer.hpp" -#include "components/text_ui_renderer.hpp" -#include "components/custom.hpp" - -#include - -#include +#include "log.hpp" namespace engine { - int Object::s_object_count = 0; + int Object::s_next_id = 1000; - Object::Object(std::string name, Object* parent, SceneRoot& root, struct GameIO things) - : m_name(name), m_parent(parent), root(root), - m_gameIO(things), - win(*things.win), - inp(*things.input), - res(*things.resMan) + Object::Object(const std::string& name, Object* parent, Scene* scene) + : name(name), parent(parent), scene(scene) { - s_object_count++; + s_next_id++; } - Object::~Object() - { - } + Object::~Object() {} - std::string Object::getName() - { - return m_name; - } - - Object* Object::getParent() - { - return m_parent; - } - - Object* Object::getChild(std::string name) + Object* Object::getChild(const std::string& name) { for (const auto& child : m_children) { - if (name == child->getName()) { + if (name == child->name) { return child.get(); } } @@ -56,24 +33,25 @@ namespace engine { return newVector; } - Object* Object::createChild(std::string name) + Object* Object::createChild(const std::string& name) { if (getChild(name) != nullptr) { - throw std::runtime_error("Attempt to create child object with existing name"); + ERROR("Attempt to create child object with existing name"); + return nullptr; } - m_children.emplace_back(std::make_unique(name, this, root, m_gameIO)); + m_children.emplace_back(std::make_unique(name, this, scene)); return m_children.back().get(); } - void Object::deleteChild(std::string name) + bool Object::deleteChild(const std::string& name) { for (auto itr = m_children.begin(); itr != m_children.end(); ++itr) { - if ((*itr)->getName() == name) { + if ((*itr)->name == name) { m_children.erase(itr); - return; + return true; } } - throw std::runtime_error("Unable to delete child '" + name + "' as it does not exist"); + return false; } void Object::printTree(int level) @@ -87,17 +65,16 @@ namespace engine { buf += " "; } } - buf += m_name; - INFO(buf); + buf += name; + INFO("{}", buf); for (const auto& child : this->getChildren()) { child->printTree(level + 1); } } +/* void Object::getAllSubComponents(struct CompList& compList, glm::mat4 parentTransform) { - using namespace components; - glm::mat4 objTransform{ 1.0f }; auto t = transform; @@ -136,5 +113,6 @@ namespace engine { child->getAllSubComponents(compList, newTransform); } } +*/ } \ No newline at end of file diff --git a/src/scene.cpp b/src/scene.cpp new file mode 100644 index 0000000..4034dd3 --- /dev/null +++ b/src/scene.cpp @@ -0,0 +1,11 @@ +#include "scene.hpp" + +namespace engine { + + Scene::Scene() + : Object("root", nullptr, this) + { + + } + +} \ No newline at end of file diff --git a/src/scene_manager.cpp b/src/scene_manager.cpp index 4e423b8..a82bbb5 100644 --- a/src/scene_manager.cpp +++ b/src/scene_manager.cpp @@ -2,14 +2,14 @@ namespace engine { + class Scene {}; + SceneManager::SceneManager() { - + m_scenes.emplace_back(std::make_unique()); + m_activeScene = m_scenes.begin(); } - SceneManager::~SceneManager() - { - - } + SceneManager::~SceneManager() {} } \ No newline at end of file diff --git a/src/window.cpp b/src/window.cpp index b499b68..3202e1f 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -5,11 +5,12 @@ #include #include -const uint64_t BILLION = 1000000000; +static const uint64_t BILLION = 1000000000; namespace engine { - Window::Window(const std::string& title, bool resizable, bool fullscreen) : m_title(title), m_resizable(resizable), m_fullscreen(fullscreen) + Window::Window(const std::string& title, bool resizable, bool fullscreen) + : m_title(title), m_resizable(resizable), m_fullscreen(fullscreen) { // init SDL @@ -62,7 +63,7 @@ namespace engine { const int WINDOWED_MIN_HEIGHT = 480; SDL_SetWindowMinimumSize(m_handle, WINDOWED_MIN_WIDTH, WINDOWED_MIN_HEIGHT); - // onResize(m_winSize.x, m_winSize.y); + // onResize(m_winSize.x, m_winSize.y); }