Move everything into namespace

This commit is contained in:
Bailey Harrison 2022-10-06 11:30:44 +01:00
parent c2ec967a89
commit 81031c9c10
33 changed files with 581 additions and 531 deletions

View File

@ -11,7 +11,7 @@
#include <glm/glm.hpp>
#include <glm/ext.hpp>
namespace components {
namespace engine::components {
class ENGINE_API Camera : public Component {

View File

@ -5,14 +5,13 @@
namespace engine {
class Window;
class Input;
}
class Object;
class ResourceManager;
class Object;
class ResourceManager;
class ENGINE_API Component {
class ENGINE_API Component {
public:
public:
enum class TypeEnum {
TRANSFORM,
@ -32,15 +31,17 @@ public:
Object& parent;
protected:
protected:
engine::Window& win;
engine::Input& inp;
ResourceManager& res;
private:
private:
static int s_next_component_id;
int m_id = s_next_component_id;
TypeEnum m_type;
};
};
}

View File

@ -6,7 +6,7 @@
#include <glm/mat4x4.hpp>
namespace components {
namespace engine::components {
class ENGINE_API CustomComponent : public Component {

View File

@ -12,7 +12,7 @@
#include <string>
#include <memory>
namespace components {
namespace engine::components {
class ENGINE_API Renderer : public Component {

View File

@ -9,7 +9,7 @@
#include <glm/mat4x4.hpp>
namespace components {
namespace engine::components {
class ENGINE_API UI : public Component {

View File

@ -3,7 +3,7 @@
#include <cstdint>
#include <cstddef>
namespace gfx {
namespace engine {
enum class ShaderType {
VERTEX,

View File

@ -2,7 +2,7 @@
// Keyboard scancodes, taken from SDL_scancode.h
namespace inputs {
namespace engine::inputs {
enum class Key : int {
UNKNOWN = 0,

View File

@ -1,6 +1,6 @@
#pragma once
namespace inputs {
namespace engine::inputs {
enum class MouseButton : int {
M_LEFT,

View File

@ -15,31 +15,31 @@
namespace engine {
class Window;
class Input;
}
class ResourceManager;
class SceneRoot;
class Component;
class ResourceManager;
namespace components {
class SceneRoot;
class Component;
namespace components {
class Transform;
class Camera;
class Renderer;
class UI;
class CustomComponent;
}
}
struct GameIO {
engine::Window * const win;
engine::Input * const input;
ResourceManager * const resMan;
};
struct GameIO {
engine::Window* const win;
engine::Input* const input;
ResourceManager* const resMan;
};
// 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 {
// 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 {
public:
public:
Object(std::string name, Object* parent, SceneRoot& root, struct GameIO things);
Object(const Object&) = delete;
Object& operator=(const Object&) = delete;
@ -85,7 +85,7 @@ public:
Transform transform;
private:
private:
static int s_object_count;
int m_id = s_object_count;
std::string m_name;
@ -97,12 +97,12 @@ private:
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'");
}
@ -113,10 +113,10 @@ template<class T> T* Object::getComponent()
}
}
return nullptr;
}
}
template <class T> T* Object::createComponent()
{
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'");
}
@ -125,10 +125,10 @@ template <class T> T* Object::createComponent()
}
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> void 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'");
}
@ -139,4 +139,6 @@ template<class T> void Object::deleteComponent()
}
}
throw std::runtime_error("deleteComponent() error: attempt to delete component that is not present.");
}
}

View File

@ -9,9 +9,11 @@
// Doesn't own resources, only holds weak_ptrs
class ENGINE_API ResourceManager {
namespace engine {
public:
class ENGINE_API ResourceManager {
public:
ResourceManager();
ResourceManager(const ResourceManager&) = delete;
ResourceManager& operator=(const ResourceManager&) = delete;
@ -30,26 +32,26 @@ public:
std::filesystem::path getFilePath(const std::string& name);
private:
private:
std::filesystem::path m_resourcesPath;
std::unordered_map<std::string, std::weak_ptr<Resource>> m_resources;
};
};
template <class T>
std::shared_ptr<T> ResourceManager::create(const std::string& name)
{
template <class T>
std::shared_ptr<T> ResourceManager::create(const std::string& name)
{
if (std::is_base_of<Resource, T>::value == false) {
throw std::runtime_error("ResourceManager::create() error: specified type is not a subclass of 'Resource'");
}
auto resource = std::make_shared<T>(getFilePath(name));
m_resources.emplace(name, std::dynamic_pointer_cast<Resource>(resource));
return resource;
}
}
template <class T>
std::shared_ptr<T> ResourceManager::get(const std::string& name)
{
template <class T>
std::shared_ptr<T> ResourceManager::get(const std::string& name)
{
if (std::is_base_of<Resource, T>::value == false) {
throw std::runtime_error("ResourceManager::get() error: specified type is not a subclass of 'Resource'");
@ -78,4 +80,6 @@ std::shared_ptr<T> ResourceManager::get(const std::string& name)
return create<T>(name);
}
}

View File

@ -10,7 +10,7 @@
#include <map>
namespace resources {
namespace engine::resources {
class ENGINE_API Font : public Resource {

View File

@ -20,7 +20,7 @@ struct Vertex {
glm::vec2 uv;
};
namespace resources {
namespace engine::resources {
class ENGINE_API Mesh : public Resource {

View File

@ -5,9 +5,11 @@
#include <string>
#include <filesystem>
class ENGINE_API Resource {
namespace engine {
public:
class ENGINE_API Resource {
public:
Resource(const std::filesystem::path& resPath, const std::string& type);
Resource(const Resource&) = delete;
Resource& operator=(const Resource&) = delete;
@ -15,10 +17,12 @@ public:
std::string getType();
protected:
protected:
std::filesystem::path m_resourcePath;
private:
private:
const std::string m_type;
};
};
}

View File

@ -11,7 +11,7 @@
#include <string>
#include <map>
namespace resources {
namespace engine::resources {
class ENGINE_API Shader : public Resource {
@ -40,7 +40,7 @@ public:
static void invalidate()
{
s_activeProgram = -1;
s_activeProgram = std::numeric_limits<GLuint>::max();
}
private:

View File

@ -6,7 +6,7 @@
#include <glad/glad.h>
namespace resources {
namespace engine::resources {
class ENGINE_API Texture : public Resource {

View File

@ -6,10 +6,12 @@
#include <filesystem>
// Holds everything you would expect to find in a game scene
class ENGINE_API SceneRoot : public Object {
namespace engine {
public:
// Holds everything you would expect to find in a game scene
class ENGINE_API SceneRoot : public Object {
public:
// create a new empty scene
SceneRoot(struct GameIO things);
SceneRoot(const std::filesystem::path& file, struct GameIO things);
@ -23,7 +25,9 @@ public:
void activateCam(int id);
void deactivateCam(int id);
private:
private:
std::vector<int> m_activeCameras{};
};
};
}

View File

@ -6,12 +6,16 @@
#include <glm/vec3.hpp>
#include <glm/ext/quaternion_float.hpp>
struct ENGINE_API Transform {
namespace engine {
// Scale, rotate (XYZ), translate
struct ENGINE_API Transform {
glm::vec3 position{0.0f};
// Scale, rotate (XYZ), translate
glm::vec3 position{ 0.0f };
glm::quat rotation{};
glm::vec3 scale{1.0f};
glm::vec3 scale{ 1.0f };
};
};
}

View File

@ -22,7 +22,7 @@ namespace engine {
class ENGINE_API Window {
public:
Window(const std::string& title);
Window(const std::string& title, bool resizable = true);
Window(const Window&) = delete;
Window& operator=(const Window&) = delete;
~Window();
@ -52,7 +52,7 @@ namespace engine {
// Returns true if the window should remain open
bool isRunning() const;
void setFullscreen(bool fullscreen, bool exclusive = true);
void setFullscreen(bool fullscreen, bool exclusive = false);
void toggleFullscreen();
bool isFullscreen() const;
@ -139,6 +139,8 @@ namespace engine {
std::string m_title;
bool m_resizable;
bool m_fullscreen = false;
bool m_justResized = false;
bool m_keyboardFocus = true;

View File

@ -13,7 +13,7 @@ static const std::string VIEW_MAT_UNIFORM = "viewMat";
static const std::string PROJ_MAT_UNIFORM = "projMat";
static const std::string WINDOW_SIZE_UNIFORM = "windowSize";
namespace components {
namespace engine::components {
glm::vec4 Camera::s_clearColor{-999.0f, -999.0f, -999.0f, -999.0f};

View File

@ -4,27 +4,31 @@
#include <iostream>
int Component::s_next_component_id = 0;
namespace engine {
Component::Component(Object* parent, TypeEnum type) :
int Component::s_next_component_id = 0;
Component::Component(Object* parent, TypeEnum type) :
m_type(type), parent(*parent),
win(parent->win),
inp(parent->inp),
res(parent->res)
{
{
s_next_component_id++;
}
}
Component::~Component()
{
}
Component::~Component()
{
}
int Component::getID()
{
int Component::getID()
{
return m_id;
}
}
Component::TypeEnum Component::getType()
{
Component::TypeEnum Component::getType()
{
return m_type;
}
}

View File

@ -1,6 +1,6 @@
#include "components/custom.hpp"
namespace components {
namespace engine::components {
CustomComponent::CustomComponent(Object* parent) : Component(parent, TypeEnum::CUSTOM)
{

View File

@ -6,7 +6,7 @@
#include <iostream>
namespace components {
namespace engine::components {
Renderer::Renderer(Object* parent) : Component(parent, TypeEnum::RENDERER)
{

View File

@ -4,7 +4,7 @@
#include "resource_manager.hpp"
#include "resources/texture.hpp"
namespace components {
namespace engine::components {
UI::UI(Object* parent) : Component(parent, TypeEnum::UI)
{

View File

@ -7,7 +7,7 @@ namespace engine {
Application::Application(const char* appName, const char* appVersion)
{
m_win = std::make_unique<Window>(appName);
m_win = std::make_unique<Window>(appName, true);
m_gfx = std::make_unique<GFXDevice>(appName, appVersion, m_win->getHandle());
}
@ -34,12 +34,7 @@ namespace engine {
}
if (m_win->getKeyPress(inputs::Key::F11)) {
if (m_win->isFullscreen()) {
m_win->setFullscreen(false);
}
else {
m_win->setFullscreen(true, false); // borderless window
}
m_win->toggleFullscreen();
}
if (m_win->getKeyPress(inputs::Key::ESCAPE)) {
m_win->setCloseFlag();

View File

@ -7,62 +7,64 @@
#include <log.hpp>
int Object::s_object_count = 0;
namespace engine {
Object::Object(std::string name, Object* parent, SceneRoot& root, struct GameIO things)
int Object::s_object_count = 0;
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)
{
{
s_object_count++;
}
}
Object::~Object()
{
}
Object::~Object()
{
}
std::string Object::getName()
{
std::string Object::getName()
{
return m_name;
}
}
Object* Object::getParent()
{
Object* Object::getParent()
{
return m_parent;
}
}
Object* Object::getChild(std::string name)
{
Object* Object::getChild(std::string name)
{
for (const auto& child : m_children) {
if (name == child->getName()) {
return child.get();
}
}
return nullptr;
}
}
std::vector<Object*> Object::getChildren()
{
std::vector<Object*> Object::getChildren()
{
std::vector<Object*> newVector{};
for (const auto& child : m_children) {
newVector.push_back(child.get());
}
return newVector;
}
}
Object* Object::createChild(std::string name)
{
Object* Object::createChild(std::string name)
{
if (getChild(name) != nullptr) {
throw std::runtime_error("Attempt to create child object with existing name");
}
m_children.emplace_back(std::make_unique<Object>(name, this, root, m_gameIO));
return m_children.back().get();
}
}
void Object::deleteChild(std::string name)
{
void Object::deleteChild(std::string name)
{
for (auto itr = m_children.begin(); itr != m_children.end(); ++itr) {
if ((*itr)->getName() == name) {
m_children.erase(itr);
@ -70,30 +72,31 @@ void Object::deleteChild(std::string name)
}
}
throw std::runtime_error("Unable to delete child '" + name + "' as it does not exist");
}
}
void Object::printTree(int level)
{
void Object::printTree(int level)
{
std::string buf;
for (int i = 0; i < level; i++) {
if (i+1 == level) {
if (i + 1 == level) {
buf += "\\_______";
} else {
}
else {
buf += " ";
}
}
buf += m_name;
INFO(buf);
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;
@ -130,4 +133,6 @@ void Object::getAllSubComponents(struct CompList& compList, glm::mat4 parentTran
for (const auto& child : m_children) {
child->getAllSubComponents(compList, newTransform);
}
}
}

View File

@ -8,8 +8,10 @@
#include "log.hpp"
ResourceManager::ResourceManager()
{
namespace engine {
ResourceManager::ResourceManager()
{
#ifdef _MSC_VER
CHAR exeDirBuf[MAX_PATH + 1];
GetModuleFileNameA(NULL, exeDirBuf, MAX_PATH + 1);
@ -21,7 +23,8 @@ ResourceManager::ResourceManager()
if (std::filesystem::is_directory(cwd / "res")) {
m_resourcesPath = cwd / "res";
} else {
}
else {
m_resourcesPath = cwd.parent_path() / "share" / "sdltest";
}
@ -32,10 +35,10 @@ ResourceManager::ResourceManager()
if (std::filesystem::is_directory(m_resourcesPath) == false) {
throw std::runtime_error("Unable to determine resources location. CWD: " + cwd.string());
}
}
}
std::unique_ptr<std::string> ResourceManager::getResourcesListString()
{
std::unique_ptr<std::string> ResourceManager::getResourcesListString()
{
auto bufPtr = std::make_unique<std::string>();
std::string& buf = *bufPtr;
int maxLength = 0;
@ -51,11 +54,11 @@ std::unique_ptr<std::string> ResourceManager::getResourcesListString()
buf += std::to_string(ptr.use_count()) + "\n";
}
return bufPtr;
}
}
std::vector<std::weak_ptr<Resource>> ResourceManager::getAllResourcesOfType(const std::string& type)
{
std::vector<std::weak_ptr<Resource>> ResourceManager::getAllResourcesOfType(const std::string& type)
{
std::vector<std::weak_ptr<Resource>> resources;
for (const auto& [name, ptr] : m_resources) {
if (ptr.expired() == false) {
@ -65,11 +68,13 @@ std::vector<std::weak_ptr<Resource>> ResourceManager::getAllResourcesOfType(cons
}
}
return resources;
}
}
// private
// private
std::filesystem::path ResourceManager::getFilePath(const std::string& name)
{
std::filesystem::path ResourceManager::getFilePath(const std::string& name)
{
return m_resourcesPath / name;
}
}

View File

@ -3,7 +3,7 @@
#include <ft2build.h>
#include FT_FREETYPE_H
namespace resources {
namespace engine::resources {
Font::Font(const std::filesystem::path& resPath) : Resource(resPath, "font")
{

View File

@ -1,6 +1,6 @@
#include "resources/mesh.hpp"
namespace resources {
namespace engine::resources {
struct MeshFileHeader {
unsigned int vertex_count;

View File

@ -2,19 +2,23 @@
#include <log.hpp>
Resource::Resource(const std::filesystem::path& resPath, const std::string& type) : m_resourcePath(resPath), m_type(type)
{
namespace engine {
Resource::Resource(const std::filesystem::path& resPath, const std::string& type) : m_resourcePath(resPath), m_type(type)
{
if (m_type != "mesh")
TRACE("Creating {} resource: {}", type, resPath.filename().string());
}
}
Resource::~Resource()
{
Resource::~Resource()
{
if (m_type != "mesh")
TRACE("Destroyed {} resource: {}", m_type, m_resourcePath.filename().string());
}
}
std::string Resource::getType()
{
std::string Resource::getType()
{
return m_type;
}
}

View File

@ -50,7 +50,7 @@ static GLuint compile(const char *path, GLenum type)
} return handle;
}
namespace resources {
namespace engine::resources {
// I've got to do this because of GL's stupid state machine
GLuint Shader::s_activeProgram = 0;

View File

@ -7,7 +7,7 @@
#include <vector>
namespace resources {
namespace engine::resources {
// -1 means invalid / no bind
GLuint Texture::s_binded_texture = -1;
@ -28,7 +28,7 @@ static bool readPNG(const std::string& path, std::vector<uint8_t>& texbuf, int *
return false;
}
const size_t size = x * y * n;
const size_t size = (size_t)x * (size_t)y * (size_t)n;
texbuf.resize(size);
memcpy(texbuf.data(), data, size);

View File

@ -14,33 +14,35 @@
#include "log.hpp"
SceneRoot::SceneRoot(struct GameIO things) : Object("root", nullptr, *this, things)
{
}
namespace engine {
SceneRoot::SceneRoot(const std::filesystem::path& file, struct GameIO things) : SceneRoot(things)
{
SceneRoot::SceneRoot(struct GameIO things) : Object("root", nullptr, *this, things)
{
}
SceneRoot::SceneRoot(const std::filesystem::path& file, struct GameIO things) : SceneRoot(things)
{
// TODO: make this a resource
//loadFromSceneFile(file);
}
}
SceneRoot::~SceneRoot()
{
}
SceneRoot::~SceneRoot()
{
}
// private methods
// private methods
// public methods
// public methods
void SceneRoot::updateStuff()
{
void SceneRoot::updateStuff()
{
using namespace components;
using namespace glm;
struct CompList compList{};
struct CompList compList {};
getAllSubComponents(compList, glm::mat4{1.0f});
getAllSubComponents(compList, glm::mat4{ 1.0f });
// update
@ -67,19 +69,19 @@ void SceneRoot::updateStuff()
c->render(t);
}
}
}
void SceneRoot::activateCam(int id)
{
void SceneRoot::activateCam(int id)
{
auto& v = m_activeCameras;
if (std::find(v.begin(), v.end(), id) == v.end()) {
v.push_back(id);
}
}
}
void SceneRoot::deactivateCam(int id)
{
void SceneRoot::deactivateCam(int id)
{
auto& v = m_activeCameras;
for (auto it = v.begin(); it != v.end(); it++) {
@ -87,4 +89,6 @@ void SceneRoot::deactivateCam(int id)
v.erase(it);
}
}
}
}

View File

@ -9,7 +9,7 @@ const uint64_t BILLION = 1000000000;
namespace engine {
Window::Window(const std::string& title) : m_title(title)
Window::Window(const std::string& title, bool resizable) : m_title(title), m_resizable(resizable)
{
// init SDL
@ -26,13 +26,23 @@ namespace engine {
m_lastFrameStamp = m_startTime - 1;
m_avgFpsStart = m_startTime;
Uint32 windowFlags = SDL_WINDOW_SHOWN;
#ifdef ENGINE_BUILD_VULKAN
windowFlags |= SDL_WINDOW_VULKAN;
#endif
if (m_resizable) {
windowFlags |= SDL_WINDOW_RESIZABLE;
}
// create the window
m_handle = SDL_CreateWindow(
m_title.c_str(),
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
static_cast<int>(m_winSize.x),
static_cast<int>(m_winSize.y),
SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN);
windowFlags);
if (m_handle == NULL) {
SDL_Quit();
throw std::runtime_error("Unable to create window: " + std::string(SDL_GetError()));
@ -278,6 +288,7 @@ namespace engine {
void Window::setFullscreen(bool fullscreen, bool exclusive)
{
if (m_resizable) {
if (SDL_SetWindowFullscreen(m_handle, fullscreen ? (exclusive ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_FULLSCREEN_DESKTOP) : 0) != 0) {
throw std::runtime_error("Unable to set window to fullscreen/windowed");
}
@ -288,6 +299,7 @@ namespace engine {
onResize(width, height);
}
}
}
void Window::toggleFullscreen()
{