aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-26 20:03:41 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-26 20:03:41 +0100
commita685e5f743786cc6499e7ce8973bb78a83d101f7 (patch)
tree21d7aeb768912bef8f98436d13acb4423cffd8f0
parent92e3fbda73128e65a31b3760b3fa4bd0147debe3 (diff)
big WIP
-rw-r--r--src/crepe/CMakeLists.txt2
-rw-r--r--src/crepe/ResourceManager.cpp36
-rw-r--r--src/crepe/ResourceManager.h (renamed from src/crepe/api/ResourceManager.h)49
-rw-r--r--src/crepe/api/AudioSource.h1
-rw-r--r--src/crepe/api/CMakeLists.txt2
-rw-r--r--src/crepe/api/Config.h27
-rw-r--r--src/crepe/api/ResourceManager.cpp41
-rw-r--r--src/crepe/system/AudioSystem.cpp2
-rw-r--r--src/crepe/system/AudioSystem.h4
-rw-r--r--src/crepe/util/Log.cpp2
-rw-r--r--src/crepe/util/Log.h16
-rw-r--r--src/test/EventTest.cpp1
-rw-r--r--src/test/ResourceManagerTest.cpp115
-rw-r--r--src/test/main.cpp12
14 files changed, 176 insertions, 134 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt
index df15b8f..0313dfa 100644
--- a/src/crepe/CMakeLists.txt
+++ b/src/crepe/CMakeLists.txt
@@ -3,6 +3,7 @@ target_sources(crepe PUBLIC
ComponentManager.cpp
Component.cpp
Collider.cpp
+ ResourceManager.cpp
Resource.cpp
)
@@ -13,6 +14,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Collider.h
ValueBroker.h
ValueBroker.hpp
+ ResourceManager.h
Resource.h
)
diff --git a/src/crepe/ResourceManager.cpp b/src/crepe/ResourceManager.cpp
new file mode 100644
index 0000000..111b9e0
--- /dev/null
+++ b/src/crepe/ResourceManager.cpp
@@ -0,0 +1,36 @@
+#include <stdexcept>
+
+#include "util/Log.h"
+
+#include "ResourceManager.h"
+
+using namespace crepe;
+using namespace std;
+
+ResourceManager::~ResourceManager() { dbg_trace(); }
+ResourceManager::ResourceManager() { dbg_trace(); }
+
+void ResourceManager::clear() {
+ this->resources.clear();
+}
+
+// template <typename T>
+// T & ResourceManager::cache(const Asset & asset) {
+// dbg_trace();
+// static_assert(is_base_of<Resource, T>::value, "cache must recieve a derivative class of Resource");
+//
+// if (!this->resources.contains(asset))
+// this->resources[asset] = make_unique<T>(asset);
+//
+// Resource * resource = this->resources.at(asset).get();
+// T * concrete_resource = dynamic_cast<T *>(resource);
+//
+// if (concrete_resource == nullptr)
+// throw runtime_error(format("ResourceManager: mismatch between requested type and actual type of resource ({})", asset.get_path()));
+//
+// return *concrete_resource;
+// }
+//
+// #include "facade/Sound.h"
+// template Sound & ResourceManager::cache(const Asset &);
+
diff --git a/src/crepe/api/ResourceManager.h b/src/crepe/ResourceManager.h
index efdd5c5..26a86a8 100644
--- a/src/crepe/api/ResourceManager.h
+++ b/src/crepe/ResourceManager.h
@@ -3,12 +3,13 @@
#include <memory>
#include <unordered_map>
-#include "Asset.h"
+#include "api/Asset.h"
+
+#include "Component.h"
#include "Resource.h"
namespace crepe {
-class Sound;
/**
* \brief The ResourceManager is responsible for storing and managing assets over
@@ -20,27 +21,19 @@ class Sound;
* destroyed, at which point the cached assets are cleared.
*/
class ResourceManager {
-
-private:
- //! A cache that holds all the assets, accessible by their file path, over multiple scenes.
- std::unordered_map<const Asset, std::unique_ptr<Resource>> resources;
-
-private:
+public:
ResourceManager(); // dbg_trace
virtual ~ResourceManager(); // dbg_trace
- ResourceManager(const ResourceManager &) = delete;
- ResourceManager(ResourceManager &&) = delete;
- ResourceManager & operator=(const ResourceManager &) = delete;
- ResourceManager & operator=(ResourceManager &&) = delete;
+private:
+ template <typename Resource>
+ Resource & get_internal(const Component & component, const Asset & asset);
-public:
- /**
- * \brief Retrieves the singleton instance of the ResourceManager.
- *
- * \return A reference to the single instance of the ResourceManager.
- */
- static ResourceManager & get_instance();
+ template <typename Resource>
+ const Asset & get_source(const Component & component) const;
+
+ //! A cache that holds all the assets, accessible by their file path, over multiple scenes.
+ std::unordered_map<const Asset, std::unique_ptr<Resource>> resources;
public:
/**
@@ -58,12 +51,26 @@ public:
* Otherwise, the concrete resource will be instantiated and added to the
* cache.
*/
- template <typename T>
- T & cache(const Asset & asset);
+ template <typename Resource>
+ void cache(const Asset & asset, bool persistent = false);
+
+ template <typename Component>
+ void cache(const Component & component, bool persistent = false);
+
+ // void resman.cache<Resource>(Asset, Lifetime);
+ // void resman.cache(Component, Asset, Lifetime);
+
+ template <typename Resource, typename Component>
+ Resource & get(const Component & component);
//! Clear the resource cache
void clear();
};
+class Sound;
+class AudioSource;
+template <>
+Sound & ResourceManager::get(const AudioSource & component);
+
} // namespace crepe
diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h
index 1264790..0950129 100644
--- a/src/crepe/api/AudioSource.h
+++ b/src/crepe/api/AudioSource.h
@@ -3,6 +3,7 @@
#include "../Component.h"
#include "../types.h"
+#include "GameObject.h"
#include "Asset.h"
namespace crepe {
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index a2e21fa..ad82924 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -7,7 +7,6 @@ target_sources(crepe PUBLIC
Transform.cpp
Color.cpp
Texture.cpp
- ResourceManager.cpp
Sprite.cpp
SaveManager.cpp
Config.cpp
@@ -39,7 +38,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Vector2.hpp
Color.h
Texture.h
- ResourceManager.h
SaveManager.h
Scene.h
Metadata.h
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index 0c9d116..5bd6913 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -11,35 +11,12 @@ namespace crepe {
* modified *before* execution is handed over from the game programmer to the engine (i.e. the
* main loop is started).
*/
-class Config final {
-public:
+struct Config final {
//! Retrieve handle to global Config instance
static Config & get_instance();
-private:
- Config() = default;
- ~Config() = default;
- Config(const Config &) = default;
- Config(Config &&) = default;
- Config & operator=(const Config &) = default;
- Config & operator=(Config &&) = default;
-
-public:
//! Logging-related settings
- struct {
- /**
- * \brief Log level
- *
- * Only messages with equal or higher priority than this value will be logged.
- */
- Log::Level level = Log::Level::INFO;
- /**
- * \brief Colored log output
- *
- * Enables log coloring using ANSI escape codes.
- */
- bool color = true;
- } log;
+ Log::Config log;
//! Save manager
struct {
diff --git a/src/crepe/api/ResourceManager.cpp b/src/crepe/api/ResourceManager.cpp
deleted file mode 100644
index 7877ed9..0000000
--- a/src/crepe/api/ResourceManager.cpp
+++ /dev/null
@@ -1,41 +0,0 @@
-#include <stdexcept>
-
-#include "util/Log.h"
-
-#include "ResourceManager.h"
-
-using namespace crepe;
-using namespace std;
-
-ResourceManager & ResourceManager::get_instance() {
- static ResourceManager instance;
- return instance;
-}
-
-ResourceManager::~ResourceManager() { dbg_trace(); }
-ResourceManager::ResourceManager() { dbg_trace(); }
-
-void ResourceManager::clear() {
- this->resources.clear();
-}
-
-template <typename T>
-T & ResourceManager::cache(const Asset & asset) {
- dbg_trace();
- static_assert(is_base_of<Resource, T>::value, "cache must recieve a derivative class of Resource");
-
- if (!this->resources.contains(asset))
- this->resources[asset] = make_unique<T>(asset);
-
- Resource * resource = this->resources.at(asset).get();
- T * concrete_resource = dynamic_cast<T *>(resource);
-
- if (concrete_resource == nullptr)
- throw runtime_error(format("ResourceManager: mismatch between requested type and actual type of resource ({})", asset.get_path()));
-
- return *concrete_resource;
-}
-
-#include "../facade/Sound.h"
-template Sound & ResourceManager::cache(const Asset &);
-
diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp
index b7ac1f2..6c30b85 100644
--- a/src/crepe/system/AudioSystem.cpp
+++ b/src/crepe/system/AudioSystem.cpp
@@ -47,7 +47,7 @@ void AudioSystem::update() {
* using the same underlying instance of Sound (esp. w/
* play/pause/retrigger behavior).
*/
- Sound & sound = this->resman.cache<Sound>(component.source);
+ // Sound & sound = this->resource_manager.get<Sound>(component);
// TODO: lots of state diffing
}
diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h
index d0b4f9a..d3d5aeb 100644
--- a/src/crepe/system/AudioSystem.h
+++ b/src/crepe/system/AudioSystem.h
@@ -1,7 +1,7 @@
#pragma once
#include "../facade/SoundContext.h"
-#include "../api/ResourceManager.h"
+#include "../ResourceManager.h"
#include "System.h"
@@ -14,7 +14,7 @@ public:
private:
SoundContext context {};
- ResourceManager & resman = ResourceManager::get_instance();
+ ResourceManager resource_manager {};
};
} // namespace crepe
diff --git a/src/crepe/util/Log.cpp b/src/crepe/util/Log.cpp
index 84d80a8..bc86c7e 100644
--- a/src/crepe/util/Log.cpp
+++ b/src/crepe/util/Log.cpp
@@ -25,7 +25,7 @@ string Log::prefix(const Level & level) {
}
void Log::log(const Level & level, const string & msg) {
- auto & cfg = Config::get_instance();
+ auto & cfg = crepe::Config::get_instance();
if (level < cfg.log.level) return;
string out = Log::prefix(level) + msg;
diff --git a/src/crepe/util/Log.h b/src/crepe/util/Log.h
index fc0bb3a..914145a 100644
--- a/src/crepe/util/Log.h
+++ b/src/crepe/util/Log.h
@@ -77,6 +77,22 @@ private:
* \return Colored message severity prefix string
*/
static std::string prefix(const Level & level);
+
+public:
+ struct Config {
+ /**
+ * \brief Log level
+ *
+ * Only messages with equal or higher priority than this value will be logged.
+ */
+ Level level = INFO;
+ /**
+ * \brief Colored log output
+ *
+ * Enables log coloring using ANSI escape codes.
+ */
+ bool color = true;
+ };
};
} // namespace crepe
diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp
index b0e6c9c..a21a851 100644
--- a/src/test/EventTest.cpp
+++ b/src/test/EventTest.cpp
@@ -37,7 +37,6 @@ public:
TEST_F(EventManagerTest, EventSubscription) {
EventHandler<KeyPressEvent> key_handler = [](const KeyPressEvent & e) {
- std::cout << "Key Event Triggered" << std::endl;
return true;
};
diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp
index 3fc9ebd..f57c419 100644
--- a/src/test/ResourceManagerTest.cpp
+++ b/src/test/ResourceManagerTest.cpp
@@ -1,52 +1,101 @@
#include <gtest/gtest.h>
+#define private public
+#define protected public
+
#include <crepe/util/Log.h>
-#include <crepe/api/Config.h>
-#include <crepe/api/ResourceManager.h>
+#include <crepe/ResourceManager.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/Component.h>
+#include <crepe/ComponentManager.h>
using namespace std;
using namespace crepe;
using namespace testing;
+class TestComponent : public Component {
+public:
+ TestComponent(game_object_id_t id, const Asset & asset)
+ : Component(id),
+ source(asset) {}
+ const Asset source;
+};
+
+class TestResource : public Resource {
+public:
+ static unsigned instances;
+
+public:
+ const unsigned instance;
+ TestResource(const Asset & src)
+ : Resource(src),
+ instance(this->instances++) { }
+};
+unsigned TestResource::instances = 0;
+
+template <>
+TestResource & ResourceManager::get(const TestComponent & component) {
+ return this->get_internal<TestResource>(component, component.source);
+}
+
class ResourceManagerTest : public Test {
public:
- ResourceManager & resman = ResourceManager::get_instance();
- Config & cfg = Config::get_instance();
+ ResourceManager resource_manager{};
+
+ static constexpr const char * ASSET_LOCATION = "asset/texture/img.png";
+
+ TestComponent a{0, ASSET_LOCATION};
+ TestComponent b{1, ASSET_LOCATION};
+private:
void SetUp() override {
- resman.clear();
+ TestResource::instances = 0;
}
};
-TEST_F(ResourceManagerTest, Main) {
- // NOTE: there is no way (that I know of) to ensure the last cache call
- // allocates the new Sound instance in a different location than the first,
- // so this test should be verified manually using these print statements and
- // debug trace messages.
- cfg.log.level = Log::Level::TRACE;
-
- Asset path1 = "mwe/audio/sfx1.wav";
- Asset path2 = "mwe/audio/sfx1.wav";
- ASSERT_EQ(path1, path2);
-
- Sound * ptr1 = nullptr;
- Sound * ptr2 = nullptr;
- {
- Log::logf(Log::Level::DEBUG, "Get first sound (constructor call)");
- Sound & sound = resman.cache<Sound>(path1);
- ptr1 = &sound;
- }
- {
- Log::logf(Log::Level::DEBUG, "Get same sound (NO constructor call)");
- Sound & sound = resman.cache<Sound>(path2);
- ptr2 = &sound;
- }
- EXPECT_EQ(ptr1, ptr2);
+TEST_F(ResourceManagerTest, Uncached) {
+ TestResource & res_1 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_2 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_3 = resource_manager.get<TestResource>(b); // 2
+ TestResource & res_4 = resource_manager.get<TestResource>(b); // 2
+
+ ASSERT_EQ(res_1, res_2);
+ ASSERT_EQ(res_3, res_4);
+ EXPECT_NE(res_1, res_3);
+
+ EXPECT_EQ(TestResource::instances, 2);
+}
+
+// TODO: per GameObject / Component
+TEST_F(ResourceManagerTest, PerComponent) {
+ resource_manager.cache(a);
+ resource_manager.cache(b);
+
+ TestResource & res_1 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_2 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_3 = resource_manager.get<TestResource>(b); // 2
+ TestResource & res_4 = resource_manager.get<TestResource>(b); // 2
+
+ ASSERT_EQ(res_1, res_2);
+ ASSERT_EQ(res_3, res_4);
+ EXPECT_NE(res_1, res_3);
+
+ EXPECT_EQ(TestResource::instances, 2);
+}
+
+TEST_F(ResourceManagerTest, PerAsset) {
+ resource_manager.cache(ASSET_LOCATION);
+ EXPECT_EQ(TestResource::instances, 1);
+
+ TestResource & res_1 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_2 = resource_manager.get<TestResource>(a); // 1
+ TestResource & res_3 = resource_manager.get<TestResource>(b); // 1
+ TestResource & res_4 = resource_manager.get<TestResource>(b); // 1
- Log::logf(Log::Level::DEBUG, "Clear cache (destructor call)");
- resman.clear();
+ EXPECT_EQ(res_1, res_2);
+ EXPECT_EQ(res_2, res_3);
+ EXPECT_EQ(res_3, res_4);
- Log::logf(Log::Level::DEBUG, "Get first sound again (constructor call)");
- Sound & sound = resman.cache<Sound>(path1);
+ EXPECT_EQ(TestResource::instances, 1);
}
diff --git a/src/test/main.cpp b/src/test/main.cpp
index e03a989..54f74fd 100644
--- a/src/test/main.cpp
+++ b/src/test/main.cpp
@@ -1,8 +1,4 @@
#include <gtest/gtest.h>
-
-#define protected public
-#define private public
-
#include <crepe/api/Config.h>
using namespace crepe;
@@ -11,12 +7,14 @@ using namespace testing;
class GlobalConfigReset : public EmptyTestEventListener {
public:
Config & cfg = Config::get_instance();
- Config cfg_default = Config();
// This function is called before each test
void OnTestStart(const TestInfo &) override {
- cfg = cfg_default;
- cfg.log.level = Log::Level::WARNING;
+ cfg = {
+ .log = {
+ .level = Log::Level::WARNING,
+ },
+ };
}
};