aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/Resource.h3
-rw-r--r--src/crepe/api/Asset.cpp4
-rw-r--r--src/crepe/api/Asset.h7
-rw-r--r--src/crepe/api/CMakeLists.txt1
-rw-r--r--src/crepe/api/ResourceManager.cpp27
-rw-r--r--src/crepe/api/ResourceManager.h4
-rw-r--r--src/crepe/api/ResourceManager.hpp27
-rw-r--r--src/crepe/facade/Sound.cpp5
-rw-r--r--src/crepe/facade/Sound.h2
-rw-r--r--src/crepe/util/OptionalRef.hpp2
10 files changed, 40 insertions, 42 deletions
diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h
index 95b4d06..a0c8859 100644
--- a/src/crepe/Resource.h
+++ b/src/crepe/Resource.h
@@ -1,7 +1,5 @@
#pragma once
-#include <memory>
-
namespace crepe {
class ResourceManager;
@@ -14,6 +12,7 @@ class Asset;
class Resource {
public:
Resource(const Asset & src);
+ virtual ~Resource() = default;
private:
/**
diff --git a/src/crepe/api/Asset.cpp b/src/crepe/api/Asset.cpp
index 1887814..5271cf7 100644
--- a/src/crepe/api/Asset.cpp
+++ b/src/crepe/api/Asset.cpp
@@ -48,6 +48,10 @@ string Asset::whereami() const noexcept {
return path;
}
+bool Asset::operator==(const Asset & other) const noexcept {
+ return this->src == other.src;
+}
+
size_t std::hash<const Asset>::operator()(const Asset & asset) const noexcept {
return std::hash<string>{}(asset.get_path());
};
diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h
index 0f6b0b3..05dccba 100644
--- a/src/crepe/api/Asset.h
+++ b/src/crepe/api/Asset.h
@@ -29,6 +29,13 @@ public:
*/
const std::string & get_path() const noexcept;
+ /**
+ * \brief Comparison operator
+ * \param other Possibly different instance of \c Asset to test equality against
+ * \return True if \c this and \c other are equal
+ */
+ bool operator == (const Asset & other) const noexcept;
+
private:
//! path to asset
const std::string src;
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 70f1527..b452f37 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -37,7 +37,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Color.h
Texture.h
ResourceManager.h
- ResourceManager.hpp
SaveManager.h
Scene.h
Metadata.h
diff --git a/src/crepe/api/ResourceManager.cpp b/src/crepe/api/ResourceManager.cpp
index 6eb4afd..7877ed9 100644
--- a/src/crepe/api/ResourceManager.cpp
+++ b/src/crepe/api/ResourceManager.cpp
@@ -1,11 +1,11 @@
+#include <stdexcept>
+
#include "util/Log.h"
#include "ResourceManager.h"
-// default resource cache functions
-#include "../facade/Sound.h"
-
using namespace crepe;
+using namespace std;
ResourceManager & ResourceManager::get_instance() {
static ResourceManager instance;
@@ -19,8 +19,23 @@ void ResourceManager::clear() {
this->resources.clear();
}
-template <>
-Sound & ResourceManager::cache<Sound>(const Asset & asset) {
- return this->cache<Sound>(asset);
+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/api/ResourceManager.h
index 468af16..efdd5c5 100644
--- a/src/crepe/api/ResourceManager.h
+++ b/src/crepe/api/ResourceManager.h
@@ -65,9 +65,5 @@ public:
void clear();
};
-template <>
-Sound & ResourceManager::cache<Sound>(const Asset & asset);
-
} // namespace crepe
-#include "ResourceManager.hpp"
diff --git a/src/crepe/api/ResourceManager.hpp b/src/crepe/api/ResourceManager.hpp
deleted file mode 100644
index 62cac20..0000000
--- a/src/crepe/api/ResourceManager.hpp
+++ /dev/null
@@ -1,27 +0,0 @@
-#pragma once
-
-#include <stdexcept>
-#include <format>
-
-#include "ResourceManager.h"
-
-namespace crepe {
-
-template <typename T>
-T & ResourceManager::cache(const Asset & asset) {
- using namespace std;
- 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;
-}
-
-} // namespace crepe
diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp
index 4eefcda..b589759 100644
--- a/src/crepe/facade/Sound.cpp
+++ b/src/crepe/facade/Sound.cpp
@@ -11,6 +11,7 @@ Sound::Sound(const Asset & src) : Resource(src) {
this->sample.load(src.get_path().c_str());
dbg_trace();
}
+Sound::~Sound() { dbg_trace(); }
void Sound::play() {
SoundContext & ctx = this->context.get();
@@ -52,3 +53,7 @@ void Sound::set_looping(bool looping) {
ctx.engine.setLooping(this->handle, this->looping);
}
+void Sound::set_context(SoundContext & ctx) {
+ this->context = ctx;
+}
+
diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h
index 6f8462a..94b1996 100644
--- a/src/crepe/facade/Sound.h
+++ b/src/crepe/facade/Sound.h
@@ -1,6 +1,5 @@
#pragma once
-#include <memory>
#include <soloud/soloud.h>
#include <soloud/soloud_wav.h>
@@ -20,6 +19,7 @@ class SoundContext;
class Sound : public Resource {
public:
Sound(const Asset & src);
+ ~Sound(); // dbg_trace
/**
* \brief Pause this sample
*
diff --git a/src/crepe/util/OptionalRef.hpp b/src/crepe/util/OptionalRef.hpp
index e603a25..7b201b0 100644
--- a/src/crepe/util/OptionalRef.hpp
+++ b/src/crepe/util/OptionalRef.hpp
@@ -60,7 +60,7 @@ OptionalRef<T> & OptionalRef<T>::operator=(T & ref) {
template <typename T>
OptionalRef<T>::operator bool() const noexcept {
- return this->ref == nullptr;
+ return this->ref != nullptr;
}
}