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/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"
)

View File

@ -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 {

View File

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

View File

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

View File

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

View File

@ -2,105 +2,79 @@
#include "engine_api.h"
#include "log.hpp"
#include <glm/mat4x4.hpp>
#include "transform.hpp"
#include "components/component.hpp"
#include <glm/gtc/quaternion.hpp>
#include <list>
#include <vector>
#include <string>
#include <memory>
#include <stdexcept>
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<Object*> 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<class T> T* getComponent();
// returns the component added
template<class T> T* createComponent();
template<class T> bool deleteComponent();
template<class T> void deleteComponent();
/* public member variables */
struct CompList {
std::vector<std::pair<components::Camera*, glm::mat4>> cameras;
std::vector<std::pair<components::Renderer*, glm::mat4>> renderers;
std::vector<std::pair<components::UI*, glm::mat4>> uis;
std::vector<std::pair<components::CustomComponent*, glm::mat4>> customs;
};
const std::string name;
// 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);
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<std::unique_ptr<Object>> m_children{};
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()
{
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) {
T* derived = dynamic_cast<T*>(component.get());
if (derived != nullptr) {
@ -113,27 +87,26 @@ namespace engine {
template <class T> T* Object::createComponent()
{
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) {
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));
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) {
if (dynamic_cast<T*>((*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;
}
}

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
#include <list>
#include <memory>
namespace engine {
class Scene; // "scene.hpp"
class SceneManager {
public:
@ -11,6 +16,8 @@ namespace engine {
SceneManager& operator=(const SceneManager&) = delete;
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 <string>
ENGINE_API extern const uint64_t BILLION;
namespace engine {
class ENGINE_API Window {

View File

@ -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<Queue> queues, QueueFlags flags)
{
uint32_t bitmask = static_cast<uint32_t>(flags);

View File

@ -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<int>(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

View File

@ -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 <log.hpp>
#include <glm/gtc/quaternion.hpp>
#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<Object>(name, this, root, m_gameIO));
m_children.emplace_back(std::make_unique<Object>(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);
}
}
*/
}

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 {
class Scene {};
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 <stdexcept>
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