Skip to content

Commit

Permalink
Merge pull request #27 from LimpingPebble/feat/stone-objects-idendifi…
Browse files Browse the repository at this point in the history
…cations

Feat/stone objects idendifications
  • Loading branch information
amasson42 authored Sep 3, 2024
2 parents 8c25f54 + dd0fc7a commit a7d7c55
Show file tree
Hide file tree
Showing 44 changed files with 200 additions and 200 deletions.
3 changes: 1 addition & 2 deletions Engine/Core/include/Core/Assets/Bundle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Stone::Core::Assets {

class Bundle : public Object {
STONE_OBJECT(Bundle)

public:
Bundle(const Bundle &other) = delete;
Expand All @@ -18,8 +19,6 @@ class Bundle : public Object {

~Bundle() override = default;

const char *getClassName() const override;

std::ostream &writeToStream(std::ostream &stream, bool closing_bracer) const override;

template <typename ResourceType, typename... Args>
Expand Down
1 change: 1 addition & 0 deletions Engine/Core/include/Core/Assets/Resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Bundle;
* @brief Represents a resource.
*/
class Resource : public Object {
STONE_ABSTRACT_OBJECT(Resource)

public:
Resource(const std::shared_ptr<Bundle> &bundle, const std::string &filepath);
Expand Down
4 changes: 2 additions & 2 deletions Engine/Core/include/Core/Image/ImageData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace Stone::Core::Image {
class ImageSource;

class ImageData : public Object {
STONE_OBJECT(ImageData)

public:
ImageData() = delete;
ImageData(const ImageData &other) = delete;

~ImageData() override;

[[nodiscard]] const char *getClassName() const override;

std::ostream &writeToStream(std::ostream &stream, bool closing_bracer) const override;

[[nodiscard]] const Size &getSize() const;
Expand Down
4 changes: 2 additions & 2 deletions Engine/Core/include/Core/Image/ImageSource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ImageData;
* It is used to load the image data when needed and hold the reference to the loaded image data.
*/
class ImageSource : public Assets::Resource {
STONE_OBJECT(ImageSource)

public:
ImageSource(const std::shared_ptr<Assets::Bundle> &bundle, const std::string &filepath,
Channel channels = Channel::RGBA);
Expand All @@ -24,8 +26,6 @@ class ImageSource : public Assets::Resource {

~ImageSource() override = default;

const char *getClassName() const override;

std::ostream &writeToStream(std::ostream &stream, bool closing_bracer) const override;

[[nodiscard]] const std::string &getFilePath() const;
Expand Down
37 changes: 36 additions & 1 deletion Engine/Core/include/Core/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ class Object : public std::enable_shared_from_this<Object> {

uint32_t getId() const;

virtual const char *getClassName() const;
const static char *StaticClassName() {
return "Object";
}

static std::intptr_t StaticHashCode() {
return reinterpret_cast<std::intptr_t>(StaticClassName());
}

virtual const char *getClassName() const = 0;

std::intptr_t getClassHashCode() const {
return reinterpret_cast<std::intptr_t>(getClassName());
}

virtual std::ostream &writeToStream(std::ostream &stream, bool closing_bracer) const;

Expand All @@ -28,4 +40,27 @@ class Object : public std::enable_shared_from_this<Object> {

} // namespace Stone::Core

#define STONE_ABSTRACT_OBJECT(ClassName) \
\
public: \
static const char *StaticClassName() { \
return #ClassName; \
} \
static std::intptr_t StaticHashCode() { \
return reinterpret_cast<std::intptr_t>(StaticClassName()); \
} \
\
private:

#define STONE_OBJECT(ClassName) \
STONE_ABSTRACT_OBJECT(ClassName) \
\
public: \
const char *getClassName() const override { \
return StaticClassName(); \
} \
\
private:


std::ostream &operator<<(std::ostream &os, const Stone::Core::Object &obj);
4 changes: 0 additions & 4 deletions Engine/Core/src/Core/Assets/Bundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ Bundle::Bundle(std::string rootDirectory) : _rootDirectory(std::move(rootDirecto
}
}

const char *Bundle::getClassName() const {
return "Bundle";
}

std::ostream &Bundle::writeToStream(std::ostream &stream, bool closing_bracer) const {
Object::writeToStream(stream, false);
stream << ",root_directory:\"" << _rootDirectory << "\"";
Expand Down
4 changes: 0 additions & 4 deletions Engine/Core/src/Core/Image/ImageData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ ImageData::~ImageData() {
}
}

const char *ImageData::getClassName() const {
return "ImageData";
}

std::ostream &ImageData::writeToStream(std::ostream &stream, bool closing_bracer) const {
stream << "{size:" << _size << ",channels:" << _channels;
if (closing_bracer)
Expand Down
4 changes: 0 additions & 4 deletions Engine/Core/src/Core/Image/ImageSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ ImageSource::ImageSource(const std::shared_ptr<Assets::Bundle> &bundle, const st
: Assets::Resource(bundle, filepath), _channels(channels) {
}

const char *ImageSource::getClassName() const {
return "ImageSource";
}

std::ostream &ImageSource::writeToStream(std::ostream &stream, bool closing_bracer) const {
stream << "{path:" << _filepath << ",channels:" << _channels;
if (closing_bracer)
Expand Down
6 changes: 1 addition & 5 deletions Engine/Core/src/Core/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ uint32_t Object::getId() const {
return _id;
}

const char *Object::getClassName() const {
return "Object";
}

std::ostream &Object::writeToStream(std::ostream &stream, bool closing_bracer) const {
stream << "{id:" << _id;
stream << "{class:\"" << getClassName() << "\",id:" << _id;
if (closing_bracer)
stream << "}";
return stream;
Expand Down
58 changes: 58 additions & 0 deletions Engine/Core/test/test_Object.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "Core/Object.hpp"

#include <gtest/gtest.h>

using namespace Stone;

class MockObject : public Core::Object {
STONE_OBJECT(MockObject)
};

class MockSubObject : public MockObject {
STONE_OBJECT(MockSubObject)
};

TEST(Object, ClassName) {
auto mockObject = std::make_shared<MockObject>();
EXPECT_STREQ(mockObject->getClassName(), "MockObject");

auto mockSubObject = std::make_shared<MockSubObject>();
EXPECT_STREQ(mockSubObject->getClassName(), "MockSubObject");

const void *ptrFromObject = mockObject->getClassName();
const void *ptrFromClass = MockObject::StaticClassName();

EXPECT_EQ(ptrFromObject, ptrFromClass);

EXPECT_EQ(mockObject->getClassName(), MockObject::StaticClassName());
}

TEST(Object, HashCode) {
auto mockObject = std::make_shared<MockObject>();
auto mockSubObject = std::make_shared<MockSubObject>();

EXPECT_EQ(mockObject->getClassHashCode(), MockObject::StaticHashCode());
EXPECT_EQ(mockSubObject->getClassHashCode(), MockSubObject::StaticHashCode());
}

TEST(Object, SwitchClassName) {

auto mockObject = std::make_shared<MockObject>();
auto mockSubObject = std::make_shared<MockSubObject>();

std::map<std::intptr_t, int> classCaster = {
{ Core::Object::StaticHashCode(), 0},
{ MockObject::StaticHashCode(), 1},
{MockSubObject::StaticHashCode(), 2}
};

EXPECT_EQ(classCaster[mockObject->getClassHashCode()], 1);
EXPECT_EQ(classCaster[mockSubObject->getClassHashCode()], 2);
}

TEST(Object, Id) {
auto object = std::make_shared<MockObject>();
auto object2 = std::make_shared<MockObject>();

EXPECT_NE(object->getId(), object2->getId());
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void VulkanRenderer::updateDataForWorld(const std::shared_ptr<Scene::WorldNode>
world->traverseTopDown([&manager](const std::shared_ptr<Scene::Node> &node) {
auto renderElement = std::dynamic_pointer_cast<Scene::IRenderable>(node);
if (renderElement && renderElement->isDirty()) {
renderElement->updateRenderObject(manager);
manager.updateRenderable(node);
}
});
}
Expand Down
1 change: 0 additions & 1 deletion Engine/Scene/include/Scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "Scene/Node/LightNode.hpp"
#include "Scene/Node/MeshNode.hpp"
#include "Scene/Node/Node.hpp"
#include "Scene/Node/NodeMacros.hpp"
#include "Scene/Node/PivotNode.hpp"
#include "Scene/Node/SkeletonNode.hpp"
#include "Scene/Node/SkinMeshNode.hpp"
Expand Down
3 changes: 1 addition & 2 deletions Engine/Scene/include/Scene/Assets/AssetResource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Stone::Scene {
* @brief Represents a resource that contains meshes, textures, and other assets.
*/
class AssetResource : public Core::Assets::Resource {
STONE_OBJECT(AssetResource)

public:
AssetResource() = delete;
Expand All @@ -20,8 +21,6 @@ class AssetResource : public Core::Assets::Resource {

~AssetResource() override = default;

const char *getClassName() const override;

std::ostream &writeToStream(std::ostream &stream, bool closing_bracer) const override;

const std::vector<std::shared_ptr<IMeshObject>> &getMeshes() const;
Expand Down
6 changes: 3 additions & 3 deletions Engine/Scene/include/Scene/Node/CameraNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace Stone::Scene {
* This class is abstract and cannot be instantiated directly. Use `PerspectiveCameraNode` or `OrthographicCameraNode`
*/
class CameraNode : public PivotNode {
public:
STONE_ABSTRACT_NODE(CameraNode);

public:
explicit CameraNode(const std::string &name = "camera");
CameraNode(const CameraNode &other);

Expand All @@ -41,9 +41,9 @@ class CameraNode : public PivotNode {
};

class PerspectiveCameraNode : public CameraNode {
public:
STONE_NODE(PerspectiveCameraNode);

public:
explicit PerspectiveCameraNode(const std::string &name = "perspective_camera");
PerspectiveCameraNode(const PerspectiveCameraNode &other);

Expand All @@ -65,9 +65,9 @@ class PerspectiveCameraNode : public CameraNode {
};

class OrthographicCameraNode : public CameraNode {
public:
STONE_NODE(OrthographicCameraNode);

public:
explicit OrthographicCameraNode(const std::string &name = "orthographic_camera");
OrthographicCameraNode(const OrthographicCameraNode &other);

Expand Down
4 changes: 1 addition & 3 deletions Engine/Scene/include/Scene/Node/InstancedMeshNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
namespace Stone::Scene {

class InstancedMeshNode : public MeshNode {
public:
STONE_NODE(InstancedMeshNode);

public:
explicit InstancedMeshNode(const std::string &name = "instancedmesh");
InstancedMeshNode(const InstancedMeshNode &other) = default;

~InstancedMeshNode() override = default;

void updateRenderObject(RendererObjectManager &manager) override;

std::ostream &writeToStream(std::ostream &stream, bool closing_bracer) const override;

void addInstance(const Transform3D &transform);
Expand Down
12 changes: 6 additions & 6 deletions Engine/Scene/include/Scene/Node/LightNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
namespace Stone::Scene {

class LightNode : public PivotNode {
public:
STONE_ABSTRACT_NODE(LightNode);

public:
explicit LightNode(const std::string &name = "light");
LightNode(const LightNode &other) = default;

Expand All @@ -33,9 +33,9 @@ class LightNode : public PivotNode {
};

class AmbientLightNode : public LightNode {
public:
STONE_NODE(AmbientLightNode);

public:
explicit AmbientLightNode(const std::string &name = "ambientlight");
AmbientLightNode(const AmbientLightNode &other);

Expand All @@ -45,9 +45,9 @@ class AmbientLightNode : public LightNode {
};

class PointLightNode : public LightNode {
public:
STONE_NODE(PointLightNode);

public:
explicit PointLightNode(const std::string &name = "pointlight");
PointLightNode(const PointLightNode &other);

Expand All @@ -67,9 +67,9 @@ class PointLightNode : public LightNode {
};

class CastingLightNode : public LightNode {
public:
STONE_ABSTRACT_NODE(CastingLightNode);

public:
explicit CastingLightNode(const std::string &name = "castinglight");
CastingLightNode(const CastingLightNode &other);

Expand Down Expand Up @@ -104,9 +104,9 @@ class CastingLightNode : public LightNode {
};

class DirectionalLightNode : public CastingLightNode {
public:
STONE_NODE(DirectionalLightNode);

public:
explicit DirectionalLightNode(const std::string &name = "directionallight");
DirectionalLightNode(const DirectionalLightNode &other);

Expand All @@ -128,9 +128,9 @@ class DirectionalLightNode : public CastingLightNode {
};

class SpotLightNode : public CastingLightNode {
public:
STONE_NODE(SpotLightNode);

public:
explicit SpotLightNode(const std::string &name = "spotlight");
SpotLightNode(const SpotLightNode &other);

Expand Down
4 changes: 1 addition & 3 deletions Engine/Scene/include/Scene/Node/MeshNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ class IMeshInterface;
class Material;

class MeshNode : public RenderableNode {
public:
STONE_NODE(MeshNode);

public:
explicit MeshNode(const std::string &name = "mesh");
MeshNode(const MeshNode &other) = default;

~MeshNode() override = default;

void updateRenderObject(RendererObjectManager &manager) override;

std::ostream &writeToStream(std::ostream &stream, bool closing_bracer) const override;

[[nodiscard]] std::shared_ptr<IMeshInterface> getMesh() const;
Expand Down
2 changes: 1 addition & 1 deletion Engine/Scene/include/Scene/Node/Node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class WorldNode;
* the node hierarchy, updating and rendering nodes, and accessing node properties.
*/
class Node : public Core::Object {
public:
STONE_NODE(Node);

public:
explicit Node(const std::string &name = "node");
Node(const Node &other) = default;

Expand Down
Loading

0 comments on commit a7d7c55

Please sign in to comment.