add objects back

This commit is contained in:
bailwillharr 2022-11-30 00:46:03 +00:00
parent 45a8c7be3c
commit dc2cb22eb8
15 changed files with 121 additions and 135 deletions

View File

@ -16,6 +16,9 @@ set(SRC_FILES
"src/scene_manager.cpp" "src/scene_manager.cpp"
"src/gfx_device_vulkan.cpp" "src/gfx_device_vulkan.cpp"
"src/scene.cpp"
"src/object.cpp"
"src/util/files.cpp" "src/util/files.cpp"
) )
@ -32,6 +35,10 @@ set(INCLUDE_FILES
"include/scene_manager.hpp" "include/scene_manager.hpp"
"include/gfx.hpp" "include/gfx.hpp"
"include/gfx_device.hpp" "include/gfx_device.hpp"
"include/scene.hpp"
"include/object.hpp"
"include/util/files.hpp" "include/util/files.hpp"
) )

View File

@ -4,10 +4,10 @@
namespace engine { namespace engine {
class Window; class Window; // "window.hpp"
class GFXDevice; class GFXDevice; // "gfx_device.hpp"
class InputManager; class InputManager; // "input_manager.hpp"
class SceneManager; class SceneManager; // "scene_manager.hpp"
class Application { class Application {

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <cstddef>
#include <vector> #include <vector>
namespace engine::gfx { namespace engine::gfx {

View File

@ -1,16 +1,14 @@
#pragma once #pragma once
#include "engine_api.h"
#include "gfx.hpp" #include "gfx.hpp"
#include <memory> #include <memory>
struct SDL_Window; struct SDL_Window; // <SDL_video.h>
namespace engine { namespace engine {
class ENGINE_API GFXDevice { class GFXDevice {
public: public:
GFXDevice(const char* appName, const char* appVersion, SDL_Window* window, bool vsync = false); GFXDevice(const char* appName, const char* appVersion, SDL_Window* window, bool vsync = false);

View File

@ -11,7 +11,7 @@
namespace engine { namespace engine {
class Window; class Window; // "window.hpp"
enum class InputDevice : int { enum class InputDevice : int {
MOUSE, MOUSE,

View File

@ -2,105 +2,79 @@
#include "engine_api.h" #include "engine_api.h"
#include "log.hpp"
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include <glm/gtc/quaternion.hpp>
#include "transform.hpp"
#include "components/component.hpp"
#include <list> #include <list>
#include <vector> #include <vector>
#include <string> #include <string>
#include <memory> #include <memory>
#include <stdexcept>
namespace engine { namespace engine {
class Window;
class Input;
class ResourceManager;
class SceneRoot; /* forward declarations */
class Component; class Scene;
namespace components { class Component {
class Camera;
class Renderer;
class UI;
class CustomComponent;
}
// 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: public:
Object(std::string name, Object* parent, SceneRoot& root); Object(const std::string& name, Object* parent, Scene* scene);
Object(const Object&) = delete; Object(const Object&) = delete;
Object& operator=(const Object&) = delete; Object& operator=(const Object&) = delete;
~Object(); ~Object();
Window& win; /* methods */
Input& inp;
ResourceManager& res;
Object* getChild(const std::string& name);
SceneRoot& root;
std::string getName();
Object* getParent();
Object* getChild(std::string name);
std::vector<Object*> getChildren(); std::vector<Object*> getChildren();
Object* createChild(const std::string& name);
Object* createChild(std::string name); bool deleteChild(const std::string& name);
void deleteChild(std::string name);
void printTree(int level = 0); void printTree(int level = 0);
// Returns the component of type T // Returns the component of type T
// Returns nullptr if the component is not found. // Returns nullptr if the component is not found.
template<class T> T* getComponent(); template<class T> T* getComponent();
// returns the component added
template<class T> T* createComponent(); template<class T> T* createComponent();
template<class T> bool deleteComponent();
template<class T> void deleteComponent(); /* public member variables */
struct CompList { const std::string name;
std::vector<std::pair<components::Camera*, glm::mat4>> cameras;
std::vector<std::pair<components::Renderer*, glm::mat4>> renderers; Object* const parent;
std::vector<std::pair<components::UI*, glm::mat4>> uis; Scene* const scene;
std::vector<std::pair<components::CustomComponent*, glm::mat4>> 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);
Transform transform; Transform transform;
private: private:
static int s_object_count; static int s_next_id;
int m_id = s_object_count; int m_id = s_next_id;
std::string m_name;
// If nullptr, this is the root object
std::list<std::unique_ptr<Object>> m_children{}; std::list<std::unique_ptr<Object>> m_children{};
std::list<std::unique_ptr<Component>> m_components{}; std::list<std::unique_ptr<Component>> 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<class T> T* Object::getComponent() template<class T> T* Object::getComponent()
{ {
if (std::is_base_of<Component, T>::value == false) {
throw std::runtime_error("getComponent() error: specified type is not a subclass of 'Component'");
}
for (const auto& component : m_components) { for (const auto& component : m_components) {
T* derived = dynamic_cast<T*>(component.get()); T* derived = dynamic_cast<T*>(component.get());
if (derived != nullptr) { if (derived != nullptr) {
@ -113,27 +87,26 @@ namespace engine {
template <class T> T* Object::createComponent() template <class T> T* Object::createComponent()
{ {
if (std::is_base_of<Component, T>::value == false) { if (std::is_base_of<Component, T>::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<T>() != nullptr) { if (getComponent<T>() != 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<T>(this)); m_components.emplace_back(std::make_unique<T>(this));
return dynamic_cast<T*>(m_components.back().get()); return dynamic_cast<T*>(m_components.back().get());
} }
template<class T> void Object::deleteComponent() template<class T> bool Object::deleteComponent()
{ {
if (std::is_base_of<Component, T>::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) { for (auto itr = m_components.begin(); itr != m_components.end(); ++itr) {
if (dynamic_cast<T*>((*itr).get()) != nullptr) { if (dynamic_cast<T*>((*itr).get()) != nullptr) {
m_components.erase(itr); m_components.erase(itr);
return; return true;
} }
} }
throw std::runtime_error("deleteComponent() error: attempt to delete component that is not present."); return false;
} }
} }

19
include/scene.hpp Normal file
View File

@ -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:
};
}

View File

@ -1,7 +1,12 @@
#pragma once #pragma once
#include <list>
#include <memory>
namespace engine { namespace engine {
class Scene; // "scene.hpp"
class SceneManager { class SceneManager {
public: public:
@ -11,6 +16,8 @@ namespace engine {
SceneManager& operator=(const SceneManager&) = delete; SceneManager& operator=(const SceneManager&) = delete;
private: private:
std::list<std::unique_ptr<Scene>> m_scenes;
std::list<std::unique_ptr<Scene>>::iterator m_activeScene{};
}; };

View File

@ -12,8 +12,6 @@
#include <array> #include <array>
#include <string> #include <string>
ENGINE_API extern const uint64_t BILLION;
namespace engine { namespace engine {
class ENGINE_API Window { class ENGINE_API Window {

View File

@ -1,5 +1,4 @@
// The implementation of the graphics layer using Vulkan 1.3. // The implementation of the graphics layer using Vulkan 1.3.
// This uses SDL specific code
//#undef ENGINE_BUILD_VULKAN //#undef ENGINE_BUILD_VULKAN
#ifdef ENGINE_BUILD_VULKAN #ifdef ENGINE_BUILD_VULKAN
@ -355,7 +354,7 @@ namespace engine {
return surface; 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<Queue> queues, QueueFlags flags) static Queue getQueueSupporting(const std::vector<Queue> queues, QueueFlags flags)
{ {
uint32_t bitmask = static_cast<uint32_t>(flags); uint32_t bitmask = static_cast<uint32_t>(flags);

View File

@ -12,11 +12,9 @@ namespace engine {
m_enabledDevices.fill(true); m_enabledDevices.fill(true);
} }
InputManager::~InputManager() InputManager::~InputManager() {}
{
}
// private methods /* private methods */
float InputManager::getDeviceAxis(enum InputDevice device, int axis) const float InputManager::getDeviceAxis(enum InputDevice device, int axis) const
{ {
@ -114,7 +112,6 @@ namespace engine {
// OVERLOADS: // OVERLOADS:
// Add a mouse input
void InputManager::addInputButton(const std::string& name, inputs::MouseButton button) void InputManager::addInputButton(const std::string& name, inputs::MouseButton button)
{ {
addInputButton(name, InputDevice::MOUSE, static_cast<int>(button)); addInputButton(name, InputDevice::MOUSE, static_cast<int>(button));
@ -179,7 +176,6 @@ namespace engine {
} }
} }
return 0.0f; // instead of throwing an exception, just return nothing 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 bool InputManager::getButton(const std::string& buttonName) const

View File

@ -1,46 +1,23 @@
#include "object.hpp" #include "object.hpp"
#include "components/camera.hpp" #include "log.hpp"
#include "components/mesh_renderer.hpp"
#include "components/text_ui_renderer.hpp"
#include "components/custom.hpp"
#include <log.hpp>
#include <glm/gtc/quaternion.hpp>
namespace engine { 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) Object::Object(const std::string& name, Object* parent, Scene* scene)
: m_name(name), m_parent(parent), root(root), : name(name), parent(parent), scene(scene)
m_gameIO(things),
win(*things.win),
inp(*things.input),
res(*things.resMan)
{ {
s_object_count++; s_next_id++;
} }
Object::~Object() Object::~Object() {}
{
}
std::string Object::getName() Object* Object::getChild(const std::string& name)
{
return m_name;
}
Object* Object::getParent()
{
return m_parent;
}
Object* Object::getChild(std::string name)
{ {
for (const auto& child : m_children) { for (const auto& child : m_children) {
if (name == child->getName()) { if (name == child->name) {
return child.get(); return child.get();
} }
} }
@ -56,24 +33,25 @@ namespace engine {
return newVector; return newVector;
} }
Object* Object::createChild(std::string name) Object* Object::createChild(const std::string& name)
{ {
if (getChild(name) != nullptr) { 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<Object>(name, this, root, m_gameIO)); m_children.emplace_back(std::make_unique<Object>(name, this, scene));
return m_children.back().get(); 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) { for (auto itr = m_children.begin(); itr != m_children.end(); ++itr) {
if ((*itr)->getName() == name) { if ((*itr)->name == name) {
m_children.erase(itr); 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) void Object::printTree(int level)
@ -87,17 +65,16 @@ namespace engine {
buf += " "; buf += " ";
} }
} }
buf += m_name; buf += name;
INFO(buf); INFO("{}", buf);
for (const auto& child : this->getChildren()) { for (const auto& child : this->getChildren()) {
child->printTree(level + 1); child->printTree(level + 1);
} }
} }
/*
void Object::getAllSubComponents(struct CompList& compList, glm::mat4 parentTransform) void Object::getAllSubComponents(struct CompList& compList, glm::mat4 parentTransform)
{ {
using namespace components;
glm::mat4 objTransform{ 1.0f }; glm::mat4 objTransform{ 1.0f };
auto t = transform; auto t = transform;
@ -136,5 +113,6 @@ namespace engine {
child->getAllSubComponents(compList, newTransform); child->getAllSubComponents(compList, newTransform);
} }
} }
*/
} }

11
src/scene.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "scene.hpp"
namespace engine {
Scene::Scene()
: Object("root", nullptr, this)
{
}
}

View File

@ -2,14 +2,14 @@
namespace engine { namespace engine {
class Scene {};
SceneManager::SceneManager() SceneManager::SceneManager()
{ {
m_scenes.emplace_back(std::make_unique<Scene>());
m_activeScene = m_scenes.begin();
} }
SceneManager::~SceneManager() SceneManager::~SceneManager() {}
{
}
} }

View File

@ -5,11 +5,12 @@
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
const uint64_t BILLION = 1000000000; static const uint64_t BILLION = 1000000000;
namespace engine { 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 // init SDL
@ -62,7 +63,7 @@ namespace engine {
const int WINDOWED_MIN_HEIGHT = 480; const int WINDOWED_MIN_HEIGHT = 480;
SDL_SetWindowMinimumSize(m_handle, WINDOWED_MIN_WIDTH, WINDOWED_MIN_HEIGHT); SDL_SetWindowMinimumSize(m_handle, WINDOWED_MIN_WIDTH, WINDOWED_MIN_HEIGHT);
// onResize(m_winSize.x, m_winSize.y); // onResize(m_winSize.x, m_winSize.y);
} }