From c58fbbefd5a426c38b1182e9e760f149f0091670 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 12:31:59 +0100 Subject: move some files from `loek/tests` to `loek/audio` --- src/crepe/facade/Sound.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/crepe/facade/Sound.h') diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index 183bd7c..e5b2f19 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -4,11 +4,13 @@ #include #include -#include "../Asset.h" +#include "../Resource.h" namespace crepe { -class Sound { +class SoundContext; + +class Sound : public Resource { public: /** * \brief Pause this sample @@ -64,15 +66,13 @@ public: bool get_looping() const { return this->looping; } public: - Sound(const char * src); - Sound(std::unique_ptr res); - -private: - void load(std::unique_ptr res); + Sound(SoundContext & ctx); + std::unique_ptr clone(const Asset & src) const override; private: SoLoud::Wav sample; SoLoud::handle handle; + SoundContext & context; float volume = 1.0f; bool looping = false; -- cgit v1.2.3 From ab0b4923c4f49e7a28f6d17e994d3e013ca344bb Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 14 Nov 2024 18:04:03 +0100 Subject: more WIP audio system + utilities --- src/crepe/CMakeLists.txt | 2 ++ src/crepe/Resource.cpp | 6 ++++ src/crepe/Resource.h | 12 ++----- src/crepe/api/ResourceManager.cpp | 13 ++++++++ src/crepe/api/ResourceManager.h | 13 ++++---- src/crepe/facade/Sound.cpp | 22 ++++++------- src/crepe/facade/Sound.h | 7 ++-- src/crepe/util/CMakeLists.txt | 2 ++ src/crepe/util/OptionalRef.h | 41 ++++++++++++++++++++++++ src/crepe/util/OptionalRef.hpp | 67 +++++++++++++++++++++++++++++++++++++++ src/test/AssetTest.cpp | 2 +- src/test/CMakeLists.txt | 2 ++ src/test/OptionalRefTest.cpp | 16 ++++++++++ src/test/ResourceManagerTest.cpp | 21 ++++++++++++ 14 files changed, 194 insertions(+), 32 deletions(-) create mode 100644 src/crepe/Resource.cpp create mode 100644 src/crepe/util/OptionalRef.h create mode 100644 src/crepe/util/OptionalRef.hpp create mode 100644 src/test/OptionalRefTest.cpp create mode 100644 src/test/ResourceManagerTest.cpp (limited to 'src/crepe/facade/Sound.h') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 05f86d7..d72d4a0 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources(crepe PUBLIC Component.cpp Collider.cpp Exception.cpp + Resource.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -15,6 +16,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES ValueBroker.hpp Exception.h Exception.hpp + Resource.h ) add_subdirectory(api) diff --git a/src/crepe/Resource.cpp b/src/crepe/Resource.cpp new file mode 100644 index 0000000..e254695 --- /dev/null +++ b/src/crepe/Resource.cpp @@ -0,0 +1,6 @@ +#include "Resource.h" + +using namespace crepe; + +Resource::Resource(const Asset & asset) { } + diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h index dcf3dbd..95b4d06 100644 --- a/src/crepe/Resource.h +++ b/src/crepe/Resource.h @@ -12,16 +12,10 @@ class Asset; * resource (e.g. textures, sounds). */ class Resource { +public: + Resource(const Asset & src); + private: - /** - * \brief Prototype pattern clone function. - * - * \param src Source file of new resource (abstraction for file saved on - * disk) - * - * \returns New instance of concrete resource - */ - virtual std::unique_ptr clone(const Asset & src) const = 0; /** * The resource manager uses \c clone to create new instances of the concrete * resource class. This may be used to inherit references to classes that diff --git a/src/crepe/api/ResourceManager.cpp b/src/crepe/api/ResourceManager.cpp index 17fbd9b..6eb4afd 100644 --- a/src/crepe/api/ResourceManager.cpp +++ b/src/crepe/api/ResourceManager.cpp @@ -2,6 +2,9 @@ #include "ResourceManager.h" +// default resource cache functions +#include "../facade/Sound.h" + using namespace crepe; ResourceManager & ResourceManager::get_instance() { @@ -11,3 +14,13 @@ ResourceManager & ResourceManager::get_instance() { ResourceManager::~ResourceManager() { dbg_trace(); } ResourceManager::ResourceManager() { dbg_trace(); } + +void ResourceManager::clear() { + this->resources.clear(); +} + +template <> +Sound & ResourceManager::cache(const Asset & asset) { + return this->cache(asset); +} + diff --git a/src/crepe/api/ResourceManager.h b/src/crepe/api/ResourceManager.h index a69a9fa..468af16 100644 --- a/src/crepe/api/ResourceManager.h +++ b/src/crepe/api/ResourceManager.h @@ -51,22 +51,23 @@ public: * it is already cached. * \tparam T The type of asset to cache (e.g., texture, sound, etc.). * - * \return A shared pointer to the cached asset. + * \return A reference to the resource * * This template function caches the asset at the given file path. If the - * asset is already cached and `reload` is false, the existing cached version - * will be returned. Otherwise, the asset will be reloaded and added to the + * asset is already cached, the existing instance will be returned. + * Otherwise, the concrete resource will be instantiated and added to the * cache. */ template T & cache(const Asset & asset); - /** - * \brief Clear the resource cache - */ + //! Clear the resource cache void clear(); }; +template <> +Sound & ResourceManager::cache(const Asset & asset); + } // namespace crepe #include "ResourceManager.hpp" diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index 726f11f..4eefcda 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -1,5 +1,3 @@ -#include - #include "../api/Asset.h" #include "../util/Log.h" @@ -9,16 +7,13 @@ using namespace crepe; using namespace std; -Sound::Sound(SoundContext & ctx) : context(ctx) { dbg_trace(); } - -unique_ptr Sound::clone(const Asset & src) const { - auto instance = make_unique(*this); - instance->sample.load(src.get_path().c_str()); - return instance; +Sound::Sound(const Asset & src) : Resource(src) { + this->sample.load(src.get_path().c_str()); + dbg_trace(); } void Sound::play() { - SoundContext & ctx = this->context; + SoundContext & ctx = this->context.get(); if (ctx.engine.getPause(this->handle)) { // resume if paused ctx.engine.setPause(this->handle, false); @@ -30,13 +25,13 @@ void Sound::play() { } void Sound::pause() { - SoundContext & ctx = this->context; + SoundContext & ctx = this->context.get(); if (ctx.engine.getPause(this->handle)) return; ctx.engine.setPause(this->handle, true); } void Sound::rewind() { - SoundContext & ctx = this->context; + SoundContext & ctx = this->context.get(); if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.seek(this->handle, 0); } @@ -44,7 +39,7 @@ void Sound::rewind() { void Sound::set_volume(float volume) { this->volume = volume; - SoundContext & ctx = this->context; + SoundContext & ctx = this->context.get(); if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.setVolume(this->handle, this->volume); } @@ -52,7 +47,8 @@ void Sound::set_volume(float volume) { void Sound::set_looping(bool looping) { this->looping = looping; - SoundContext & ctx = this->context; + SoundContext & ctx = this->context.get(); if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.setLooping(this->handle, this->looping); } + diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index 8342b46..6f8462a 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -4,6 +4,7 @@ #include #include +#include "../util/OptionalRef.h" #include "../Resource.h" namespace crepe { @@ -18,6 +19,7 @@ class SoundContext; */ class Sound : public Resource { public: + Sound(const Asset & src); /** * \brief Pause this sample * @@ -72,13 +74,12 @@ public: bool get_looping() const { return this->looping; } public: - Sound(SoundContext & ctx); - std::unique_ptr clone(const Asset & src) const override; + void set_context(SoundContext & ctx); private: SoLoud::Wav sample; SoLoud::handle handle; - SoundContext & context; + OptionalRef context; float volume = 1.0f; bool looping = false; diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index 4be738a..94ed906 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -9,5 +9,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Log.hpp Proxy.h Proxy.hpp + OptionalRef.h + OptionalRef.hpp ) diff --git a/src/crepe/util/OptionalRef.h b/src/crepe/util/OptionalRef.h new file mode 100644 index 0000000..1ad3a6d --- /dev/null +++ b/src/crepe/util/OptionalRef.h @@ -0,0 +1,41 @@ +#pragma once + +namespace crepe { + +/** + * \brief Optional reference utility + * + * This class doesn't need to know the full definition of \c T to be used. + * + * \tparam T Value type + */ +template +class OptionalRef { +public: + OptionalRef() = default; + OptionalRef(T &); + OptionalRef & operator=(T &); + explicit operator bool() const noexcept; + + void set(T &) noexcept; + T & get() const; + void clear() noexcept; + + OptionalRef(const OptionalRef &); + OptionalRef(OptionalRef &&); + OptionalRef & operator=(const OptionalRef &); + OptionalRef & operator=(OptionalRef &&); + +private: + /** + * \brief Reference to the value of type \c T + * + * \note This raw pointer is *not* managed, and only used as a reference! + */ + T * ref = nullptr; +}; + +} + +#include "OptionalRef.hpp" + diff --git a/src/crepe/util/OptionalRef.hpp b/src/crepe/util/OptionalRef.hpp new file mode 100644 index 0000000..e603a25 --- /dev/null +++ b/src/crepe/util/OptionalRef.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include + +#include "OptionalRef.h" + +namespace crepe { + +template +OptionalRef::OptionalRef(T & ref) { + this->set(ref); +} + +template +OptionalRef::OptionalRef(const OptionalRef & other) { + this->ref = other.ref; +} + +template +OptionalRef::OptionalRef(OptionalRef && other) { + this->ref = other.ref; + other.clear(); +} + +template +OptionalRef & OptionalRef::operator=(const OptionalRef & other) { + this->ref = other.ref; + return *this; +} + +template +OptionalRef & OptionalRef::operator=(OptionalRef && other) { + this->ref = other.ref; + other.clear(); + return *this; +} + +template +T & OptionalRef::get() const { + if (this->ref == nullptr) + throw std::runtime_error("OptionalRef: attempt to dereference nullptr"); + return *this->ref; +} + +template +void OptionalRef::set(T & ref) noexcept { + this->ref = &ref; +} + +template +void OptionalRef::clear() noexcept { + this->ref = nullptr; +} + +template +OptionalRef & OptionalRef::operator=(T & ref) { + this->set(ref); + return *this; +} + +template +OptionalRef::operator bool() const noexcept { + return this->ref == nullptr; +} + +} + diff --git a/src/test/AssetTest.cpp b/src/test/AssetTest.cpp index 324a3f1..563a253 100644 --- a/src/test/AssetTest.cpp +++ b/src/test/AssetTest.cpp @@ -1,7 +1,7 @@ -#include "api/Config.h" #include #include +#include using namespace std; using namespace crepe; diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 5ea90f7..437c296 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -5,5 +5,7 @@ target_sources(test_main PUBLIC ParticleTest.cpp AudioTest.cpp AssetTest.cpp + ResourceManagerTest.cpp + OptionalRefTest.cpp ) diff --git a/src/test/OptionalRefTest.cpp b/src/test/OptionalRefTest.cpp new file mode 100644 index 0000000..65bd816 --- /dev/null +++ b/src/test/OptionalRefTest.cpp @@ -0,0 +1,16 @@ +#include + +#include + +using namespace std; +using namespace crepe; +using namespace testing; + +TEST(OptionalRefTest, Explicit) { + string value = "foo"; + OptionalRef ref; + + EXPECT_FALSE(bool(ref)); + ASSERT_THROW(ref.get(), runtime_error); +} + diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp new file mode 100644 index 0000000..42b6b5d --- /dev/null +++ b/src/test/ResourceManagerTest.cpp @@ -0,0 +1,21 @@ +#include + +#include + +using namespace std; +using namespace crepe; +using namespace testing; + +class ResourceManagerTest : public Test { +public: + ResourceManager & manager = ResourceManager::get_instance(); + + void SetUp() override { + this->manager.clear(); + } +}; + +TEST_F(ResourceManagerTest, Main) { + Sound & sound = this->manager.cache("mwe/audio/sfx1.wav"); +} + -- cgit v1.2.3 From add8724446fdeae1aaec9b07544cf7a5475a9bfe Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 14 Nov 2024 19:57:45 +0100 Subject: ResourceManager working + tested --- src/crepe/Resource.h | 3 +-- src/crepe/api/Asset.cpp | 4 ++++ src/crepe/api/Asset.h | 7 +++++++ src/crepe/api/CMakeLists.txt | 1 - src/crepe/api/ResourceManager.cpp | 27 +++++++++++++++++++++------ src/crepe/api/ResourceManager.h | 4 ---- src/crepe/api/ResourceManager.hpp | 27 --------------------------- src/crepe/facade/Sound.cpp | 5 +++++ src/crepe/facade/Sound.h | 2 +- src/crepe/util/OptionalRef.hpp | 2 +- src/test/OptionalRefTest.cpp | 24 +++++++++++++++++++++++- src/test/ResourceManagerTest.cpp | 36 +++++++++++++++++++++++++++++++++--- src/test/main.cpp | 15 +++++++++++++-- 13 files changed, 109 insertions(+), 48 deletions(-) delete mode 100644 src/crepe/api/ResourceManager.hpp (limited to 'src/crepe/facade/Sound.h') 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 - 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::operator()(const Asset & asset) const noexcept { return std::hash{}(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 + #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(const Asset & asset) { - return this->cache(asset); +template +T & ResourceManager::cache(const Asset & asset) { + dbg_trace(); + static_assert(is_base_of::value, "cache must recieve a derivative class of Resource"); + + if (!this->resources.contains(asset)) + this->resources[asset] = make_unique(asset); + + Resource * resource = this->resources.at(asset).get(); + T * concrete_resource = dynamic_cast(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(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 -#include - -#include "ResourceManager.h" - -namespace crepe { - -template -T & ResourceManager::cache(const Asset & asset) { - using namespace std; - static_assert(is_base_of::value, "cache must recieve a derivative class of Resource"); - - if (!this->resources.contains(asset)) - this->resources[asset] = make_unique(asset); - - Resource * resource = this->resources.at(asset).get(); - T * concrete_resource = dynamic_cast(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 #include #include @@ -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 & OptionalRef::operator=(T & ref) { template OptionalRef::operator bool() const noexcept { - return this->ref == nullptr; + return this->ref != nullptr; } } diff --git a/src/test/OptionalRefTest.cpp b/src/test/OptionalRefTest.cpp index 65bd816..219ccca 100644 --- a/src/test/OptionalRefTest.cpp +++ b/src/test/OptionalRefTest.cpp @@ -9,8 +9,30 @@ using namespace testing; TEST(OptionalRefTest, Explicit) { string value = "foo"; OptionalRef ref; + EXPECT_FALSE(ref); + ASSERT_THROW(ref.get(), runtime_error); + + ref.set(value); + EXPECT_TRUE(ref); + ASSERT_NO_THROW(ref.get()); + + ref.clear(); + EXPECT_FALSE(ref); + ASSERT_THROW(ref.get(), runtime_error); +} + +TEST(OptionalRefTest, Implicit) { + string value = "foo"; + OptionalRef ref = value; + EXPECT_TRUE(ref); + ASSERT_NO_THROW(ref.get()); - EXPECT_FALSE(bool(ref)); + ref.clear(); + EXPECT_FALSE(ref); ASSERT_THROW(ref.get(), runtime_error); + + ref = value; + EXPECT_TRUE(ref); + ASSERT_NO_THROW(ref.get()); } diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp index 42b6b5d..5d1ae7a 100644 --- a/src/test/ResourceManagerTest.cpp +++ b/src/test/ResourceManagerTest.cpp @@ -1,5 +1,7 @@ #include +#include +#include #include using namespace std; @@ -8,14 +10,42 @@ using namespace testing; class ResourceManagerTest : public Test { public: - ResourceManager & manager = ResourceManager::get_instance(); + ResourceManager & resman = ResourceManager::get_instance(); + Config & cfg = Config::get_instance(); void SetUp() override { - this->manager.clear(); + cfg.log.level = Log::Level::TRACE; + resman.clear(); } }; TEST_F(ResourceManagerTest, Main) { - Sound & sound = this->manager.cache("mwe/audio/sfx1.wav"); + 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(path1); + ptr1 = &sound; + } + { + Log::logf(Log::Level::DEBUG, "Get same sound (NO constructor call)"); + Sound & sound = resman.cache(path2); + ptr2 = &sound; + } + EXPECT_EQ(ptr1, ptr2); + + Log::logf(Log::Level::DEBUG, "Clear cache (destructor call)"); + resman.clear(); + + Log::logf(Log::Level::DEBUG, "Get first sound again (constructor call)"); + Sound & sound = resman.cache(path1); + + // NOTE: there is no way (that I know of) to ensure the above statement + // allocates the new Sound instance in a different location than the first, + // so this test was verified using the above print statements. } diff --git a/src/test/main.cpp b/src/test/main.cpp index 241015d..19a8d6e 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -5,11 +5,22 @@ using namespace crepe; using namespace testing; +class GlobalConfigReset : public EmptyTestEventListener { +public: + Config & cfg = Config::get_instance(); + + // This function is called before each test + void OnTestStart(const TestInfo &) override { + cfg.log.level = Log::Level::WARNING; + } +}; + int main(int argc, char ** argv) { InitGoogleTest(&argc, argv); - auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::ERROR; + UnitTest & ut = *UnitTest::GetInstance(); + ut.listeners().Append(new GlobalConfigReset); return RUN_ALL_TESTS(); } + -- cgit v1.2.3 From c59d460f12e1393e0ddbaaa1c6f5522eb12f8ff9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 29 Nov 2024 16:23:52 +0100 Subject: more utility classes for Audio system --- src/crepe/api/AudioSource.h | 11 ++----- src/crepe/facade/Sound.h | 4 +++ src/crepe/system/AudioSystem.cpp | 17 +++++++++-- src/crepe/system/AudioSystem.h | 21 +++++++++++++ src/crepe/util/CMakeLists.txt | 3 ++ src/crepe/util/Private.cpp | 29 ++++++++++++++++++ src/crepe/util/Private.h | 34 +++++++++++++++++++++ src/crepe/util/Private.hpp | 31 +++++++++++++++++++ src/example/CMakeLists.txt | 1 - src/example/asset_manager.cpp | 36 ---------------------- src/test/AudioTest.cpp | 5 ++- src/test/CMakeLists.txt | 1 + src/test/PrivateTest.cpp | 66 ++++++++++++++++++++++++++++++++++++++++ 13 files changed, 210 insertions(+), 49 deletions(-) create mode 100644 src/crepe/util/Private.cpp create mode 100644 src/crepe/util/Private.h create mode 100644 src/crepe/util/Private.hpp delete mode 100644 src/example/asset_manager.cpp create mode 100644 src/test/PrivateTest.cpp (limited to 'src/crepe/facade/Sound.h') diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 0950129..9d76f0b 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -2,6 +2,7 @@ #include "../Component.h" #include "../types.h" +#include "../util/Private.h" #include "GameObject.h" #include "Asset.h" @@ -47,14 +48,8 @@ private: bool rewind = false; private: - //! Value of \c active after last system update - bool last_active = false; - //! Value of \c playing after last system update - bool last_playing = false; - //! Value of \c volume after last system update - float last_volume = 1.0; - //! Value of \c loop after last system update - bool last_loop = false; + //! AudioSystem::ComponentPrivate + Private private_data; }; } // namespace crepe diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index b0b80f8..f33ee58 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -9,6 +9,10 @@ namespace crepe { class SoundContext; +struct SoundHandle { + SoLoud::handle handle; +}; + /** * \brief Sound resource facade * diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 97cf966..0f943be 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -1,6 +1,5 @@ #include "AudioSystem.h" -#include "../api/AudioSource.h" #include "../manager/ComponentManager.h" #include "../manager/ResourceManager.h" #include "../types.h" @@ -13,12 +12,24 @@ void AudioSystem::update() { ResourceManager & resource_manager = this->mediator.resource_manager; RefVector components = component_manager.get_components_by_type(); - for (auto component_ref : components) { - AudioSource & component = component_ref.get(); + for (AudioSource & component : components) { if (!component.active) continue; Sound & sound = resource_manager.get(component.source); + if (component.private_data.empty()) + component.private_data.set(); + auto & data = component.private_data.get(); // TODO: lots of state diffing + + + this->update_private(component, data); } } +void AudioSystem::update_private(const AudioSource & component, ComponentPrivate & data) { + data.last_active = component.active; + data.last_loop = component.loop; + data.last_playing = component.playing; + data.last_volume = component.volume; +} + diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index e037f51..7f41fda 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -1,6 +1,7 @@ #pragma once #include "../facade/SoundContext.h" +#include "../api/AudioSource.h" #include "System.h" @@ -11,6 +12,26 @@ public: using System::System; void update() override; +private: + /** + * \brief Private data stored by AudioSystem on AudioSource component + */ + struct ComponentPrivate { + //! This sample's voice handle + SoLoud::handle handle; + + //! Value of \c active after last system update + bool last_active = false; + //! Value of \c playing after last system update + bool last_playing = false; + //! Value of \c volume after last system update + float last_volume = 1.0; + //! Value of \c loop after last system update + bool last_loop = false; + }; + + void update_private(const AudioSource & component, ComponentPrivate & data); + private: SoundContext context {}; }; diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index 94ed906..f49d851 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -1,6 +1,7 @@ target_sources(crepe PUBLIC LogColor.cpp Log.cpp + Private.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -11,5 +12,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Proxy.hpp OptionalRef.h OptionalRef.hpp + Private.h + Private.hpp ) diff --git a/src/crepe/util/Private.cpp b/src/crepe/util/Private.cpp new file mode 100644 index 0000000..c5b5b30 --- /dev/null +++ b/src/crepe/util/Private.cpp @@ -0,0 +1,29 @@ +#include "Private.h" + +using namespace crepe; + +bool Private::empty() const noexcept { + return this->instance == nullptr; +} + +Private::~Private() { + if (this->instance == nullptr) return; + this->destructor(this->instance); +} + +Private::Private(Private && other) { + *this = std::move(other); +} + +Private & Private::operator=(Private && other) { + // TODO: ideally this function checks for self-assignment + this->instance = other.instance; + this->destructor = other.destructor; + this->type = other.type; + + other.instance = nullptr; + other.destructor = [](void*){}; + + return *this; +} + diff --git a/src/crepe/util/Private.h b/src/crepe/util/Private.h new file mode 100644 index 0000000..fc3728f --- /dev/null +++ b/src/crepe/util/Private.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +namespace crepe { + +class Private { +public: + Private() = default; + ~Private(); + Private(Private &&); + Private & operator=(Private &&); + Private(const Private &) = delete; + Private & operator=(const Private &) = delete; + + template + T & get(); + + template + void set(Args &&... args); + + bool empty() const noexcept; + +private: + std::function destructor; + std::type_index type = typeid(void); + void * instance = nullptr; +}; + +} + +#include "Private.hpp" + diff --git a/src/crepe/util/Private.hpp b/src/crepe/util/Private.hpp new file mode 100644 index 0000000..30c8146 --- /dev/null +++ b/src/crepe/util/Private.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +#include "Private.h" + +namespace crepe { + +template +void Private::set(Args &&... args) { + T * instance = new T(std::forward(args)...); + this->instance = static_cast(instance); + this->destructor = [](void * instance) { + delete static_cast(instance); + }; + this->type = typeid(T); +} + +template +T & Private::get() { + using namespace std; + if (this->empty()) + throw out_of_range("Private: get() called on empty object"); + type_index requested_type = typeid(T); + if (this->type != requested_type) + throw logic_error(format("Private: get() called with [T = {}] (actual is [T = {}])", requested_type.name(), this->type.name())); + return *static_cast(this->instance); +} + +} diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 560e2bc..9c3c550 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -16,7 +16,6 @@ function(add_example target_name) add_dependencies(examples ${target_name}) endfunction() -add_example(asset_manager) add_example(savemgr) add_example(rendering_particle) add_example(gameloop) diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp deleted file mode 100644 index 660b318..0000000 --- a/src/example/asset_manager.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include - -using namespace crepe; - -int main() { - - // this needs to be called before the asset manager otherwise the destructor of sdl is not in - // the right order - { Texture test("asset/texture/img.png"); } - // FIXME: make it so the issue described by the above comment is not possible (i.e. the order - // in which internal classes are instantiated should not impact the way the engine works). - - auto & mgr = AssetManager::get_instance(); - - { - // TODO: [design] the Sound class can't be directly included by the user as it includes - // SoLoud headers. - auto bgm = mgr.cache("mwe/audio/bgm.ogg"); - auto sfx1 = mgr.cache("mwe/audio/sfx1.wav"); - auto sfx2 = mgr.cache("mwe/audio/sfx2.wav"); - - auto img = mgr.cache("asset/texture/img.png"); - auto img1 = mgr.cache("asset/texture/second.png"); - } - - { - auto bgm = mgr.cache("mwe/audio/bgm.ogg"); - auto sfx1 = mgr.cache("mwe/audio/sfx1.wav"); - auto sfx2 = mgr.cache("mwe/audio/sfx2.wav"); - - auto img = mgr.cache("asset/texture/img.png"); - auto img1 = mgr.cache("asset/texture/second.png"); - } -} diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp index afd2672..3be5afa 100644 --- a/src/test/AudioTest.cpp +++ b/src/test/AudioTest.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -13,15 +14,17 @@ class AudioTest : public Test { Mediator mediator; public: ComponentManager component_manager{mediator}; + ResourceManager resource_manager{mediator}; AudioSystem system {mediator}; void SetUp() override { auto & mgr = this->component_manager; GameObject entity = mgr.new_object("name"); - entity.add_component("mwe/audio/sfx1.wav"); + AudioSource & audio_source = entity.add_component("mwe/audio/sfx1.wav"); } }; TEST_F(AudioTest, Default) { + system.update(); } diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 4174926..8c4b855 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -16,4 +16,5 @@ target_sources(test_main PUBLIC Vector2Test.cpp ScriptEventTest.cpp ScriptSceneTest.cpp + PrivateTest.cpp ) diff --git a/src/test/PrivateTest.cpp b/src/test/PrivateTest.cpp new file mode 100644 index 0000000..f0d2b1a --- /dev/null +++ b/src/test/PrivateTest.cpp @@ -0,0 +1,66 @@ +#include + +#include + +using namespace std; +using namespace crepe; +using namespace testing; + +class PrivateTest : public Test { +public: + static unsigned constructors; + static unsigned destructors; + + void SetUp() override { + PrivateTest::constructors = 0; + PrivateTest::destructors = 0; + } + + class TestClass { + public: + TestClass() { PrivateTest::constructors++; } + ~TestClass() { PrivateTest::destructors++; } + }; + class Unrelated {}; +}; +unsigned PrivateTest::constructors; +unsigned PrivateTest::destructors; + +TEST_F(PrivateTest, Empty) { + { + Private foo; + } + + EXPECT_EQ(PrivateTest::constructors, 0); + EXPECT_EQ(PrivateTest::destructors, 0); +} + +TEST_F(PrivateTest, WithObject) { + { + Private foo; + foo.set(); + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 0); + } + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 1); +} + +TEST_F(PrivateTest, EmptyException) { + Private foo; + EXPECT_THROW(foo.get(), std::out_of_range); + + foo.set(); + EXPECT_NO_THROW(foo.get()); +} + +TEST_F(PrivateTest, IncorrectTypeException) { + Private foo; + foo.set(); + + EXPECT_THROW(foo.get(), std::logic_error); + EXPECT_NO_THROW(foo.get()); +} + -- cgit v1.2.3 From 693355f55193cb2ea4c29616073227e37665afc1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 29 Nov 2024 17:30:45 +0100 Subject: more audio system WIP --- src/crepe/facade/Sound.cpp | 64 ++++++++++++++-------------- src/crepe/facade/Sound.h | 66 +++++----------------------- src/crepe/facade/SoundContext.h | 3 +- src/crepe/system/AudioSystem.cpp | 29 ++++++++++--- src/crepe/system/AudioSystem.h | 26 +++++++----- src/crepe/util/Private.cpp | 5 +++ src/crepe/util/Private.h | 6 +-- src/crepe/util/Private.hpp | 4 +- src/test/AudioTest.cpp | 51 +++++++++++++++++++--- src/test/PrivateTest.cpp | 92 ++++++++++++++++++++++++++++++++++++++++ 10 files changed, 229 insertions(+), 117 deletions(-) (limited to 'src/crepe/facade/Sound.h') diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index 52496af..0df1f48 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -13,36 +13,36 @@ Sound::Sound(const Asset & src) : Resource(src) { } Sound::~Sound() { dbg_trace(); } -void Sound::play(SoundContext & ctx) { - if (ctx.engine.getPause(this->handle)) { - // resume if paused - ctx.engine.setPause(this->handle, false); - } else { - // or start new sound - this->handle = ctx.engine.play(this->sample, this->volume); - ctx.engine.setLooping(this->handle, this->looping); - } -} - -void Sound::pause(SoundContext & ctx) { - if (ctx.engine.getPause(this->handle)) return; - ctx.engine.setPause(this->handle, true); -} - -void Sound::rewind(SoundContext & ctx) { - if (!ctx.engine.isValidVoiceHandle(this->handle)) return; - ctx.engine.seek(this->handle, 0); -} - -void Sound::set_volume(SoundContext & ctx, float volume) { - this->volume = volume; - if (!ctx.engine.isValidVoiceHandle(this->handle)) return; - ctx.engine.setVolume(this->handle, this->volume); -} - -void Sound::set_looping(SoundContext & ctx, bool looping) { - this->looping = looping; - if (!ctx.engine.isValidVoiceHandle(this->handle)) return; - ctx.engine.setLooping(this->handle, this->looping); -} +// void Sound::play(SoundContext & ctx) { +// if (ctx.engine.getPause(this->handle)) { +// // resume if paused +// ctx.engine.setPause(this->handle, false); +// } else { +// // or start new sound +// this->handle = ctx.engine.play(this->sample, this->volume); +// ctx.engine.setLooping(this->handle, this->looping); +// } +// } +// +// void Sound::pause(SoundContext & ctx) { +// if (ctx.engine.getPause(this->handle)) return; +// ctx.engine.setPause(this->handle, true); +// } +// +// void Sound::rewind(SoundContext & ctx) { +// if (!ctx.engine.isValidVoiceHandle(this->handle)) return; +// ctx.engine.seek(this->handle, 0); +// } +// +// void Sound::set_volume(SoundContext & ctx, float volume) { +// this->volume = volume; +// if (!ctx.engine.isValidVoiceHandle(this->handle)) return; +// ctx.engine.setVolume(this->handle, this->volume); +// } +// +// void Sound::set_looping(SoundContext & ctx, bool looping) { +// this->looping = looping; +// if (!ctx.engine.isValidVoiceHandle(this->handle)) return; +// ctx.engine.setLooping(this->handle, this->looping); +// } diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index f33ee58..10d7c3c 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -9,10 +9,6 @@ namespace crepe { class SoundContext; -struct SoundHandle { - SoLoud::handle handle; -}; - /** * \brief Sound resource facade * @@ -23,62 +19,20 @@ class Sound : public Resource { public: Sound(const Asset & src); ~Sound(); // dbg_trace - /** - * \brief Pause this sample - * - * Pauses this sound if it is playing, or does nothing if it is already paused. The playhead - * position is saved, such that calling \c play() after this function makes the sound resume. - */ - void pause(SoundContext & ctx); - /** - * \brief Play this sample - * - * Resume playback if this sound is paused, or start from the beginning of the sample. - * - * \note This class only saves a reference to the most recent 'voice' of this sound. Calling - * \c play() while the sound is already playing causes multiple instances of the sample to - * play simultaniously. The sample started last is the one that is controlled afterwards. - */ - void play(SoundContext & ctx); - /** - * \brief Reset playhead position - * - * Resets the playhead position so that calling \c play() after this function makes it play - * from the start of the sample. If the sound is not paused before calling this function, - * this function will stop playback. - */ - void rewind(SoundContext & ctx); - /** - * \brief Set playback volume / gain - * - * \param volume Volume (0 = muted, 1 = full volume) - */ - void set_volume(SoundContext & ctx, float volume); - /** - * \brief Get playback volume / gain - * - * \return Volume - */ - float get_volume() const { return this->volume; } - /** - * \brief Set looping behavior for this sample - * - * \param looping Looping behavior (false = one-shot, true = loop) - */ - void set_looping(SoundContext & ctx, bool looping); - /** - * \brief Get looping behavior - * - * \return true if looping, false if one-shot - */ - bool get_looping() const { return this->looping; } + + class Handle { + private: + SoLoud::handle handle; + float volume = 1.0f; + bool looping = false; + + friend class SoundContext; + }; private: SoLoud::Wav sample; - SoLoud::handle handle; - float volume = 1.0f; - bool looping = false; + friend class SoundContext; }; } // namespace crepe diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index d22ff7a..286ced8 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -22,10 +22,9 @@ public: SoundContext & operator=(const SoundContext &) = delete; SoundContext & operator=(SoundContext &&) = delete; + private: SoLoud::Soloud engine; - //! Sound directly calls methods on \c engine - friend class Sound; }; } // namespace crepe diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 0f943be..191dbbb 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -15,18 +15,35 @@ void AudioSystem::update() { for (AudioSource & component : components) { if (!component.active) continue; - Sound & sound = resource_manager.get(component.source); - if (component.private_data.empty()) - component.private_data.set(); + Sound & resource = resource_manager.get(component.source); + + if (component.private_data.empty()) { + auto & data = component.private_data.set(); + this->update_last(component, data); + } auto & data = component.private_data.get(); - // TODO: lots of state diffing + this->diff_update(component, data, resource); + + this->update_last(component, data); + } +} + +void AudioSystem::diff_update(AudioSource & component, const ComponentPrivate & data, Sound & resource) { + bool update_playing = component.playing != data.last_playing; + bool update_volume = component.volume != data.last_volume; + bool update_loop = component.loop != data.last_loop; + bool update_active = component.active != data.last_active; - this->update_private(component, data); + if (update_active) + if (component.rewind) { + component.playing = false; + // this->context.rewind(resource, data.handle); } + } -void AudioSystem::update_private(const AudioSource & component, ComponentPrivate & data) { +void AudioSystem::update_last(const AudioSource & component, ComponentPrivate & data) { data.last_active = component.active; data.last_loop = component.loop; data.last_playing = component.playing; diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index 7f41fda..4650178 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -1,6 +1,7 @@ #pragma once #include "../facade/SoundContext.h" +#include "../facade/Sound.h" #include "../api/AudioSource.h" #include "System.h" @@ -18,19 +19,22 @@ private: */ struct ComponentPrivate { //! This sample's voice handle - SoLoud::handle handle; - - //! Value of \c active after last system update - bool last_active = false; - //! Value of \c playing after last system update - bool last_playing = false; - //! Value of \c volume after last system update - float last_volume = 1.0; - //! Value of \c loop after last system update - bool last_loop = false; + Sound::Handle handle; + + /** + * \name State diffing variables + * \{ + */ + typeof(AudioSource::active) last_active; + typeof(AudioSource::playing) last_playing; + typeof(AudioSource::volume) last_volume; + typeof(AudioSource::loop) last_loop; + //! \} }; - void update_private(const AudioSource & component, ComponentPrivate & data); + void update_last(const AudioSource & component, ComponentPrivate & data); + + void diff_update(AudioSource & component, const ComponentPrivate & data, Sound & resource); private: SoundContext context {}; diff --git a/src/crepe/util/Private.cpp b/src/crepe/util/Private.cpp index c5b5b30..cb4cb5b 100644 --- a/src/crepe/util/Private.cpp +++ b/src/crepe/util/Private.cpp @@ -27,3 +27,8 @@ Private & Private::operator=(Private && other) { return *this; } +Private::Private(const Private & other) { } +Private & Private::operator=(const Private & other) { + return *this; +} + diff --git a/src/crepe/util/Private.h b/src/crepe/util/Private.h index fc3728f..6dd28bb 100644 --- a/src/crepe/util/Private.h +++ b/src/crepe/util/Private.h @@ -9,16 +9,16 @@ class Private { public: Private() = default; ~Private(); + Private(const Private &); Private(Private &&); + Private & operator=(const Private &); Private & operator=(Private &&); - Private(const Private &) = delete; - Private & operator=(const Private &) = delete; template T & get(); template - void set(Args &&... args); + T & set(Args &&... args); bool empty() const noexcept; diff --git a/src/crepe/util/Private.hpp b/src/crepe/util/Private.hpp index 30c8146..d6ab23f 100644 --- a/src/crepe/util/Private.hpp +++ b/src/crepe/util/Private.hpp @@ -8,13 +8,15 @@ namespace crepe { template -void Private::set(Args &&... args) { +T & Private::set(Args &&... args) { + if (!this->empty()) this->destructor(this->instance); T * instance = new T(std::forward(args)...); this->instance = static_cast(instance); this->destructor = [](void * instance) { delete static_cast(instance); }; this->type = typeid(T); + return *instance; } template diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp index 3be5afa..c6f0097 100644 --- a/src/test/AudioTest.cpp +++ b/src/test/AudioTest.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -7,6 +8,7 @@ #include using namespace std; +using namespace std::chrono_literals; using namespace crepe; using namespace testing; @@ -17,14 +19,51 @@ public: ResourceManager resource_manager{mediator}; AudioSystem system {mediator}; - void SetUp() override { - auto & mgr = this->component_manager; - GameObject entity = mgr.new_object("name"); - AudioSource & audio_source = entity.add_component("mwe/audio/sfx1.wav"); - } +private: + GameObject entity = component_manager.new_object("name"); +public: + AudioSource & bgm = entity.add_component("mwe/audio/bgm.ogg"); + AudioSource & sfx1 = entity.add_component("mwe/audio/sfx1.wav"); + AudioSource & sfx2 = entity.add_component("mwe/audio/sfx2.wav"); + AudioSource & sfx3 = entity.add_component("mwe/audio/sfx3.wav"); + }; TEST_F(AudioTest, Default) { - system.update(); + bool example_done = false; + + future example = async([&](){ + // Start the background track + bgm.play(); + + // Play each sample sequentially while pausing and resuming the background track + this_thread::sleep_for(500ms); + sfx1.play(); + this_thread::sleep_for(500ms); + sfx2.play(); + bgm.stop(); + this_thread::sleep_for(500ms); + sfx3.play(); + bgm.play(); + this_thread::sleep_for(500ms); + + // Play all samples simultaniously + sfx1.play(); + sfx2.play(); + sfx3.play(); + this_thread::sleep_for(1000ms); + }); + + future system_loop = async([&](){ + while (!example_done) { + auto next = chrono::steady_clock::now() + 25ms; + system.update(); + this_thread::sleep_until(next); + } + }); + + example.wait(); + example_done = true; + system_loop.wait(); } diff --git a/src/test/PrivateTest.cpp b/src/test/PrivateTest.cpp index f0d2b1a..0ea67d6 100644 --- a/src/test/PrivateTest.cpp +++ b/src/test/PrivateTest.cpp @@ -64,3 +64,95 @@ TEST_F(PrivateTest, IncorrectTypeException) { EXPECT_NO_THROW(foo.get()); } +TEST_F(PrivateTest, MoveConstructor) { + { + Private foo; + foo.set(); + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 0); + + Private bar(std::move(foo)); + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 0); + } + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 1); +} + +TEST_F(PrivateTest, MoveOperator) { + { + Private foo; + foo.set(); + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 0); + + Private bar = std::move(foo); + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 0); + } + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 1); +} + +TEST_F(PrivateTest, CopyConstructor) { + { + Private foo; + foo.set(); + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 0); + + Private bar(foo); + + EXPECT_TRUE(bar.empty()); + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 0); + } + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 1); +} + +TEST_F(PrivateTest, CopyOperator) { + { + Private foo; + foo.set(); + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 0); + + Private bar = foo; + + EXPECT_TRUE(bar.empty()); + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 0); + } + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 1); +} + +TEST_F(PrivateTest, DoubleAssignment) { + { + Private foo; + foo.set(); + + EXPECT_EQ(PrivateTest::constructors, 1); + EXPECT_EQ(PrivateTest::destructors, 0); + + foo.set(); + + EXPECT_EQ(PrivateTest::constructors, 2); + EXPECT_EQ(PrivateTest::destructors, 1); + } + + EXPECT_EQ(PrivateTest::constructors, 2); + EXPECT_EQ(PrivateTest::destructors, 2); +} + -- cgit v1.2.3 From e4be73051a68b552c44280bbe9836dd4f02972d8 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 29 Nov 2024 20:35:17 +0100 Subject: audio system kinda working --- src/crepe/api/AudioSource.cpp | 1 - src/crepe/api/AudioSource.h | 2 -- src/crepe/api/Config.h | 25 +++++++++++++++++++++++-- src/crepe/facade/Sound.h | 7 +------ src/crepe/facade/SoundContext.cpp | 31 +++++++++++++++++++++++++++++-- src/crepe/facade/SoundContext.h | 10 ++++++++++ src/crepe/system/AudioSystem.cpp | 30 ++++++++++++++++++++++++------ src/crepe/system/AudioSystem.h | 2 +- src/crepe/util/Log.cpp | 2 +- src/crepe/util/Log.h | 16 ---------------- 10 files changed, 89 insertions(+), 37 deletions(-) (limited to 'src/crepe/facade/Sound.h') diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 4baac9a..c646aeb 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -15,6 +15,5 @@ void AudioSource::play(bool looping) { void AudioSource::stop() { this->playing = false; - this->rewind = true; } diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 9d76f0b..8dc1645 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -44,8 +44,6 @@ private: //! If this source is playing audio bool playing = false; - //! Rewind the sample location - bool rewind = false; private: //! AudioSystem::ComponentPrivate diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 693400a..7be506e 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -1,8 +1,10 @@ #pragma once +#include + #include "../util/Log.h" + #include "types.h" -#include namespace crepe { @@ -18,7 +20,20 @@ struct Config final { static Config & get_instance(); //! Logging-related settings - Log::Config log; + 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; //! Save manager struct { @@ -62,6 +77,12 @@ struct Config final { */ std::string root_pattern = ".crepe-root"; } asset; + + //! Audio system settings + struct { + //! Max amount of simultanious voices + unsigned int voices = 32; + } audio; }; } // namespace crepe diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index 10d7c3c..35bccdb 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -20,13 +20,8 @@ public: Sound(const Asset & src); ~Sound(); // dbg_trace - class Handle { - private: + struct Handle { SoLoud::handle handle; - float volume = 1.0f; - bool looping = false; - - friend class SoundContext; }; private: diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index b65dfb2..3e9a3d1 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -6,10 +6,37 @@ using namespace crepe; SoundContext::SoundContext() { dbg_trace(); - engine.init(); + this->engine.init(); + this->engine.setMaxActiveVoiceCount(this->config.audio.voices); } SoundContext::~SoundContext() { dbg_trace(); - engine.deinit(); + this->engine.deinit(); } + +Sound::Handle SoundContext::play(Sound & resource) { + return { + .handle = this->engine.play(resource.sample, this->default_volume), + }; +} + +void SoundContext::stop(Sound::Handle & handle) { + this->engine.stop(handle.handle); +} + +void SoundContext::set_volume(Sound & resource, Sound::Handle & handle, float volume) { + this->engine.setVolume(handle.handle, volume); + this->default_volume = volume; +} + +void SoundContext::set_loop(Sound & resource, Sound::Handle & handle, bool loop) { + this->engine.setLooping(handle.handle, loop); +} + +bool SoundContext::get_playing(Sound::Handle & handle) { + // See Soloud::stopVoice_internal in soloud/src/core/soloud_core_voiceops.cpp for why this is + // the correct method to use here (samples are currently never paused) + return this->engine.isValidVoiceHandle(handle.handle); +} + diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index 286ced8..e02977e 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -2,6 +2,8 @@ #include +#include "../api/Config.h" + #include "Sound.h" namespace crepe { @@ -22,9 +24,17 @@ public: SoundContext & operator=(const SoundContext &) = delete; SoundContext & operator=(SoundContext &&) = delete; + Sound::Handle play(Sound & resource); + void stop(Sound::Handle &); + void set_volume(Sound &, Sound::Handle &, float); + void set_loop(Sound &, Sound::Handle &, bool); + bool get_playing(Sound::Handle &); private: SoLoud::Soloud engine; + float default_volume = 1.0f; + + Config & config = Config::get_instance(); }; } // namespace crepe diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 191dbbb..98aff58 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -20,6 +20,7 @@ void AudioSystem::update() { if (component.private_data.empty()) { auto & data = component.private_data.set(); this->update_last(component, data); + data.last_playing = false; // always start } auto & data = component.private_data.get(); @@ -29,18 +30,35 @@ void AudioSystem::update() { } } -void AudioSystem::diff_update(AudioSource & component, const ComponentPrivate & data, Sound & resource) { +void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource) { bool update_playing = component.playing != data.last_playing; bool update_volume = component.volume != data.last_volume; bool update_loop = component.loop != data.last_loop; bool update_active = component.active != data.last_active; - if (update_active) - if (component.rewind) { - component.playing = false; - // this->context.rewind(resource, data.handle); + if (update_active) { + if (component.active) { + update_playing = true; + if (component.play_on_awake) + component.playing = true; + } else { + this->context.stop(data.handle); + return; + } + } + if (!component.active) return; + if (update_playing) { + if (component.playing) data.handle = this->context.play(resource); + else this->context.stop(data.handle); + } else { + component.playing = this->context.get_playing(data.handle); + } + if (update_volume) { + this->context.set_volume(resource, data.handle, component.volume); + } + if (update_loop) { + this->context.set_loop(resource, data.handle, component.loop); } - } void AudioSystem::update_last(const AudioSource & component, ComponentPrivate & data) { diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index 4650178..3404878 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -34,7 +34,7 @@ private: void update_last(const AudioSource & component, ComponentPrivate & data); - void diff_update(AudioSource & component, const ComponentPrivate & data, Sound & resource); + void diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource); private: SoundContext context {}; diff --git a/src/crepe/util/Log.cpp b/src/crepe/util/Log.cpp index bc86c7e..84d80a8 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 = crepe::Config::get_instance(); + auto & cfg = 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 914145a..fc0bb3a 100644 --- a/src/crepe/util/Log.h +++ b/src/crepe/util/Log.h @@ -77,22 +77,6 @@ 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 -- cgit v1.2.3 From b8194e02679dc88f5c0a240da83a4700ec5200cf Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 30 Nov 2024 17:27:16 +0100 Subject: add doxygen comments + clean up --- src/crepe/Resource.h | 18 +++++------ src/crepe/api/AudioSource.h | 9 ++++-- src/crepe/facade/Sound.cpp | 34 --------------------- src/crepe/facade/Sound.h | 9 ++++-- src/crepe/facade/SoundContext.cpp | 7 ++--- src/crepe/facade/SoundContext.h | 48 +++++++++++++++++++++++++---- src/crepe/manager/ResourceManager.h | 44 ++++++++++++++++++++++----- src/crepe/system/AudioSystem.cpp | 4 +-- src/crepe/system/AudioSystem.h | 29 ++++++++++++++++-- src/crepe/util/Private.h | 60 +++++++++++++++++++++++++++++++++++-- src/crepe/util/Private.hpp | 2 +- src/test/AudioTest.cpp | 16 +++++----- 12 files changed, 197 insertions(+), 83 deletions(-) (limited to 'src/crepe/facade/Sound.h') diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h index a0c8859..a2d65df 100644 --- a/src/crepe/Resource.h +++ b/src/crepe/Resource.h @@ -6,21 +6,19 @@ class ResourceManager; class Asset; /** - * Resource is an interface class used to represent a (deserialized) game - * resource (e.g. textures, sounds). + * \brief Resource interface + * + * Resource is an interface class used to represent a (deserialized) game resource (e.g. + * textures, sounds). Resources are always created from \ref Asset "assets" by ResourceManager. + * + * The game programmer has the ability to use the ResourceManager to keep instances of concrete + * resources between scenes, preventing them from being reinstantiated during a scene + * transition. */ class Resource { public: Resource(const Asset & src); virtual ~Resource() = default; - -private: - /** - * The resource manager uses \c clone to create new instances of the concrete - * resource class. This may be used to inherit references to classes that - * would otherwise need to be implemented as singletons. - */ - friend class ResourceManager; }; } // namespace crepe diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 63b4bc4..330e8e1 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -17,16 +17,19 @@ class AudioSource : public Component { friend class AudioSystem; protected: + /** + * \param source Sound sample to load + */ AudioSource(game_object_id_t id, const Asset & source); - //! Only ComponentManager can create components + //! Only ComponentManager creates components friend class ComponentManager; public: - // But std::unique_ptr needs to be able to destoy this component again + // std::unique_ptr needs to be able to destoy this component virtual ~AudioSource() = default; public: - //! Start or resume this audio source + //! Start this audio source void play(bool looping = false); //! Stop this audio source void stop(); diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index 33a0c47..ad50637 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -2,7 +2,6 @@ #include "../util/Log.h" #include "Sound.h" -#include "SoundContext.h" using namespace crepe; using namespace std; @@ -12,36 +11,3 @@ Sound::Sound(const Asset & src) : Resource(src) { dbg_trace(); } Sound::~Sound() { dbg_trace(); } - -// void Sound::play(SoundContext & ctx) { -// if (ctx.engine.getPause(this->handle)) { -// // resume if paused -// ctx.engine.setPause(this->handle, false); -// } else { -// // or start new sound -// this->handle = ctx.engine.play(this->sample, this->volume); -// ctx.engine.setLooping(this->handle, this->looping); -// } -// } -// -// void Sound::pause(SoundContext & ctx) { -// if (ctx.engine.getPause(this->handle)) return; -// ctx.engine.setPause(this->handle, true); -// } -// -// void Sound::rewind(SoundContext & ctx) { -// if (!ctx.engine.isValidVoiceHandle(this->handle)) return; -// ctx.engine.seek(this->handle, 0); -// } -// -// void Sound::set_volume(SoundContext & ctx, float volume) { -// this->volume = volume; -// if (!ctx.engine.isValidVoiceHandle(this->handle)) return; -// ctx.engine.setVolume(this->handle, this->volume); -// } -// -// void Sound::set_looping(SoundContext & ctx, bool looping) { -// this->looping = looping; -// if (!ctx.engine.isValidVoiceHandle(this->handle)) return; -// ctx.engine.setLooping(this->handle, this->looping); -// } diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index 35bccdb..a78a2a7 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -12,21 +12,24 @@ class SoundContext; /** * \brief Sound resource facade * - * This class is a wrapper around a \c SoLoud::Wav instance, which holds a - * single sample. It is part of the sound facade. + * This class is a wrapper around a \c SoLoud::Wav instance, which holds a single sample. It is + * part of the sound facade. */ class Sound : public Resource { public: Sound(const Asset & src); ~Sound(); // dbg_trace + //! Voice handle wrapper struct Handle { + //! Voice handle (soloud), used by SoundContext SoLoud::handle handle; }; private: + //! Deserialized resource (soloud) SoLoud::Wav sample; - + //! SoundContext uses \c sample friend class SoundContext; }; diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index 470b3cc..8bd7e74 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -17,17 +17,16 @@ SoundContext::~SoundContext() { Sound::Handle SoundContext::play(Sound & resource) { return { - .handle = this->engine.play(resource.sample, this->default_volume), + .handle = this->engine.play(resource.sample, 1.0f), }; } void SoundContext::stop(Sound::Handle & handle) { this->engine.stop(handle.handle); } -void SoundContext::set_volume(Sound & resource, Sound::Handle & handle, float volume) { +void SoundContext::set_volume(Sound::Handle & handle, float volume) { this->engine.setVolume(handle.handle, volume); - this->default_volume = volume; } -void SoundContext::set_loop(Sound & resource, Sound::Handle & handle, bool loop) { +void SoundContext::set_loop(Sound::Handle & handle, bool loop) { this->engine.setLooping(handle.handle, loop); } diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index c651cd5..3bc8be5 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -11,8 +11,8 @@ namespace crepe { /** * \brief Sound engine facade * - * This class is a wrapper around a \c SoLoud::Soloud instance, which provides - * the methods for playing \c Sound instances. It is part of the sound facade. + * This class is a wrapper around a \c SoLoud::Soloud instance, which provides the methods for + * playing \c Sound instances. It is part of the sound facade. */ class SoundContext { public: @@ -24,15 +24,51 @@ public: SoundContext & operator=(const SoundContext &) = delete; SoundContext & operator=(SoundContext &&) = delete; + /** + * \brief Play a sample + * + * Plays a Sound from the beginning of the sample and returns a handle to control it later. + * + * \param resource Sound instance to play + * + * \returns Handle to control this voice + */ virtual Sound::Handle play(Sound & resource); - virtual void stop(Sound::Handle &); - virtual void set_volume(Sound &, Sound::Handle &, float); - virtual void set_loop(Sound &, Sound::Handle &, bool); + /** + * \brief Stop a voice immediately if it is still playing + * + * \note This function does nothing if the handle is invalid or if the sound is already + * stopped / finished playing. + * + * \param handle Voice handle returned by SoundContext::play + */ + virtual void stop(Sound::Handle & handle); + /** + * \brief Change the volume of a voice + * + * \note This function does nothing if the handle is invalid or if the sound is already + * stopped / finished playing. + * + * \param handle Voice handle returned by SoundContext::play + * \param volume New gain value (0=silent, 1=default) + */ + virtual void set_volume(Sound::Handle & handle, float volume); + /** + * \brief Set the looping behavior of a voice + * + * \note This function does nothing if the handle is invalid or if the sound is already + * stopped / finished playing. + * + * \param handle Voice handle returned by SoundContext::play + * \param loop Looping behavior (false=oneshot, true=loop) + */ + virtual void set_loop(Sound::Handle & handle, bool loop); private: + //! Abstracted class SoLoud::Soloud engine; - float default_volume = 1.0f; + //! Config reference Config & config = Config::get_instance(); }; diff --git a/src/crepe/manager/ResourceManager.h b/src/crepe/manager/ResourceManager.h index e7e6abc..84b275d 100644 --- a/src/crepe/manager/ResourceManager.h +++ b/src/crepe/manager/ResourceManager.h @@ -11,13 +11,11 @@ namespace crepe { /** - * \brief The ResourceManager is responsible for storing and managing assets over - * multiple scenes. + * \brief Owner of concrete Resource instances * - * The ResourceManager ensures that assets are loaded once and can be accessed - * across different scenes. It caches assets to avoid reloading them every time - * a scene is loaded. Assets are retained in memory until the ResourceManager is - * destroyed, at which point the cached assets are cleared. + * ResourceManager caches concrete Resource instances per Asset. Concrete resources are + * destroyed at the end of scenes by default, unless the game programmer marks them as + * persistent. */ class ResourceManager : public Manager { public: @@ -25,21 +23,53 @@ public: virtual ~ResourceManager(); // dbg_trace private: + //! Cache entry struct CacheEntry { + //! Concrete resource instance std::unique_ptr resource = nullptr; + //! Prevent ResourceManager::clear from removing this entry bool persistent = false; }; - //! A cache that holds all the assets, accessible by their file path, over multiple scenes. + //! Internal cache std::unordered_map resources; + /** + * \brief Ensure a cache entry exists for this asset and return a mutable reference to it + * + * \param asset Asset the concrete resource is instantiated from + * + * \returns Mutable reference to cache entry + */ CacheEntry & get_entry(const Asset & asset); public: + /** + * \brief Mark a resource as persistent (i.e. used across multiple scenes) + * + * \param asset Asset the concrete resource is instantiated from + * \param persistent Whether this resource is persistent (true=keep, false=destroy) + */ void set_persistent(const Asset & asset, bool persistent); + /** + * \brief Retrieve reference to concrete Resource by Asset + * + * \param asset Asset the concrete resource is instantiated from + * \tparam Resource Concrete derivative of Resource + * + * This class instantiates the concrete resource if it is not yet stored in the internal + * cache, or returns a reference to the cached resource if it already exists. + * + * \returns Reference to concrete resource + * + * \throws std::runtime_error if the \c Resource parameter does not match with the actual + * type of the resource stored in the cache for this Asset + */ template Resource & get(const Asset & asset); + //! Clear non-persistent resources from cache void clear(); + //! Clear all resources from cache regardless of persistence void clear_all(); }; diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 0696b34..26913c0 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -52,10 +52,10 @@ void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, component.oneshot_stop = false; } if (component.volume != data.last_volume) { - context.set_volume(resource, data.handle, component.volume); + context.set_volume(data.handle, component.volume); } if (component.loop != data.last_loop) { - context.set_loop(resource, data.handle, component.loop); + context.set_loop(data.handle, component.loop); } } diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index c941470..4d21883 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -14,9 +14,7 @@ public: void update() override; private: - /** - * \brief Private data stored by AudioSystem on AudioSource component - */ + //! Private data stored by AudioSystem on AudioSource component struct ComponentPrivate { //! This sample's voice handle Sound::Handle handle; @@ -31,14 +29,39 @@ private: //! \} }; + /** + * \brief Update `last_*` members of \c data + * + * Copies all component properties stored for comparison between AudioSystem::update() calls + * + * \param component Source properties + * \param data Destination properties + */ void update_last(const AudioSource & component, ComponentPrivate & data); + /** + * \brief Compare update component + * + * Compares properties of \c component and \c data, and calls SoundContext functions where + * applicable. + * + * \param component AudioSource component to update + * \param data AudioSource's private data + * \param resource Sound instance for AudioSource's Asset + */ void diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource); protected: + /** + * \brief Get SoundContext + * + * SoundContext is retrieved through this function instead of being a direct member of + * AudioSystem to aid with testability. + */ virtual SoundContext & get_context(); private: + //! Actually stores SoundContext if the base AudioSystem::get_context implementation is used Private context; }; diff --git a/src/crepe/util/Private.h b/src/crepe/util/Private.h index 62a2e1a..d725a5e 100644 --- a/src/crepe/util/Private.h +++ b/src/crepe/util/Private.h @@ -5,26 +5,82 @@ namespace crepe { +/** + * \brief Utility for storing type hidden from user + * + * This class can be used to store types which cannot be used in the API directly due to header + * distribution limitations. This class is similar to `std::any`, but provides a method for + * retrieving a mutable reference to the stored object. + */ class Private { public: Private() = default; ~Private(); + /** + * \name Copy + * + * \note These functions do not do anything, resulting in `*this` being an empty (default) + * instance. + * + * \{ + */ Private(const Private &); - Private(Private &&); Private & operator=(const Private &); + //! \} + /** + * \name Move + * + * These functions actually move the stored type if present. + * + * \{ + */ + Private(Private &&); Private & operator=(Private &&); + //! \} + /** + * \brief Get the stored object + * + * \tparam T Type of stored object + * + * \returns Mutable reference to stored object + * + * \throws std::out_of_range if this instance does not contain any object + * \throws std::logic_error if the stored type and requested type differ + */ template - T & get(); + T & get() const; + /** + * \brief Create and store an arbitrary object + * + * \tparam T Type of object + * \tparam Args Perfect forwarding arguments + * \param args Perfect forwarding arguments + * + * All arguments to this function are forwarded using `std::forward` to the constructor of T. + * + * \returns Mutable reference to stored object + * + * \note If this instance already contained an object, this function implicitly destroys the + * previous object. + */ template T & set(Args &&... args); + /** + * \brief Check if this instance contains an object + * + * \returns `true` if this instance is empty, `false` if it contains an object + */ bool empty() const noexcept; private: + //! Wrapper for destructor call of stored object type std::function destructor; + //! Stored object's type std::type_index type = typeid(void); + //! Stored object void * instance = nullptr; }; diff --git a/src/crepe/util/Private.hpp b/src/crepe/util/Private.hpp index 3a87a9f..b2174c0 100644 --- a/src/crepe/util/Private.hpp +++ b/src/crepe/util/Private.hpp @@ -18,7 +18,7 @@ T & Private::set(Args &&... args) { } template -T & Private::get() { +T & Private::get() const { using namespace std; if (this->empty()) throw out_of_range("Private: get() called on empty object"); type_index requested_type = typeid(T); diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp index 14f57bd..7644ab7 100644 --- a/src/test/AudioTest.cpp +++ b/src/test/AudioTest.cpp @@ -18,8 +18,8 @@ private: public: MOCK_METHOD(Sound::Handle, play, (Sound & resource), (override)); MOCK_METHOD(void, stop, (Sound::Handle &), (override)); - MOCK_METHOD(void, set_volume, (Sound &, Sound::Handle &, float), (override)); - MOCK_METHOD(void, set_loop, (Sound &, Sound::Handle &, bool), (override)); + MOCK_METHOD(void, set_volume, (Sound::Handle &, float), (override)); + MOCK_METHOD(void, set_loop, (Sound::Handle &, bool), (override)); }; class TestAudioSystem : public AudioSystem { @@ -48,8 +48,8 @@ public: TEST_F(AudioTest, Default) { EXPECT_CALL(context, play(_)).Times(0); EXPECT_CALL(context, stop(_)).Times(0); - EXPECT_CALL(context, set_volume(_, _, _)).Times(0); - EXPECT_CALL(context, set_loop(_, _, _)).Times(0); + EXPECT_CALL(context, set_volume(_, _)).Times(0); + EXPECT_CALL(context, set_loop(_, _)).Times(0); system.update(); } @@ -95,14 +95,14 @@ TEST_F(AudioTest, Volume) { { InSequence seq; - EXPECT_CALL(context, set_volume(_, _, _)).Times(0); + EXPECT_CALL(context, set_volume(_, _)).Times(0); component.volume += 0.2; } { InSequence seq; - EXPECT_CALL(context, set_volume(_, _, component.volume)).Times(1); + EXPECT_CALL(context, set_volume(_, component.volume)).Times(1); system.update(); } } @@ -113,14 +113,14 @@ TEST_F(AudioTest, Looping) { { InSequence seq; - EXPECT_CALL(context, set_loop(_, _, _)).Times(0); + EXPECT_CALL(context, set_loop(_, _)).Times(0); component.loop = !component.loop; } { InSequence seq; - EXPECT_CALL(context, set_loop(_, _, component.loop)).Times(1); + EXPECT_CALL(context, set_loop(_, component.loop)).Times(1); system.update(); } } -- cgit v1.2.3 From 7a8657dfe019104aced61a5b63e63f61ad919f7a Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 2 Dec 2024 16:13:08 +0100 Subject: remove `Private` --- src/crepe/api/AudioSource.h | 15 +++- src/crepe/facade/Sound.h | 6 -- src/crepe/facade/SoundContext.cpp | 23 +++--- src/crepe/facade/SoundContext.h | 15 +++- src/crepe/facade/SoundHandle.h | 13 ++++ src/crepe/system/AudioSystem.cpp | 44 +++++------ src/crepe/system/AudioSystem.h | 29 ++----- src/crepe/util/CMakeLists.txt | 3 - src/crepe/util/Private.cpp | 27 ------- src/crepe/util/Private.h | 89 ---------------------- src/crepe/util/Private.hpp | 31 -------- src/test/AudioTest.cpp | 8 +- src/test/CMakeLists.txt | 1 - src/test/PrivateTest.cpp | 155 -------------------------------------- 14 files changed, 78 insertions(+), 381 deletions(-) create mode 100644 src/crepe/facade/SoundHandle.h delete mode 100644 src/crepe/util/Private.cpp delete mode 100644 src/crepe/util/Private.h delete mode 100644 src/crepe/util/Private.hpp delete mode 100644 src/test/PrivateTest.cpp (limited to 'src/crepe/facade/Sound.h') diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 330e8e1..7c1f161 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -2,7 +2,7 @@ #include "../Component.h" #include "../types.h" -#include "../util/Private.h" +#include "../facade/SoundHandle.h" #include "Asset.h" #include "GameObject.h" @@ -59,10 +59,17 @@ private: //! Stop this sample bool oneshot_stop = false; //! \} + /** + * \name State diffing variables + * \{ + */ + typeof(active) last_active = false; + typeof(volume) last_volume = volume; + typeof(loop) last_loop = loop; + //! \} + //! This source's voice handle + SoundHandle voice{}; -private: - //! AudioSystem::ComponentPrivate - Private private_data; }; } // namespace crepe diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index a78a2a7..85d141b 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -20,12 +20,6 @@ public: Sound(const Asset & src); ~Sound(); // dbg_trace - //! Voice handle wrapper - struct Handle { - //! Voice handle (soloud), used by SoundContext - SoLoud::handle handle; - }; - private: //! Deserialized resource (soloud) SoLoud::Wav sample; diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index 8bd7e74..d18afc6 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -15,18 +15,23 @@ SoundContext::~SoundContext() { this->engine.deinit(); } -Sound::Handle SoundContext::play(Sound & resource) { - return { - .handle = this->engine.play(resource.sample, 1.0f), - }; +SoundHandle SoundContext::play(Sound & resource) { + SoLoud::handle real_handle = this->engine.play(resource.sample, 1.0f); + SoundHandle handle = this->next_handle; + this->registry[handle] = real_handle; + this->next_handle++; + return handle; } -void SoundContext::stop(Sound::Handle & handle) { this->engine.stop(handle.handle); } +void SoundContext::stop(const SoundHandle & handle) { + this->engine.stop(this->registry[handle]); +} -void SoundContext::set_volume(Sound::Handle & handle, float volume) { - this->engine.setVolume(handle.handle, volume); +void SoundContext::set_volume(const SoundHandle & handle, float volume) { + this->engine.setVolume(this->registry[handle], volume); } -void SoundContext::set_loop(Sound::Handle & handle, bool loop) { - this->engine.setLooping(handle.handle, loop); +void SoundContext::set_loop(const SoundHandle & handle, bool loop) { + this->engine.setLooping(this->registry[handle], loop); } + diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index 3bc8be5..102f928 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -4,6 +4,7 @@ #include "../api/Config.h" +#include "SoundHandle.h" #include "Sound.h" namespace crepe { @@ -33,7 +34,7 @@ public: * * \returns Handle to control this voice */ - virtual Sound::Handle play(Sound & resource); + virtual SoundHandle play(Sound & resource); /** * \brief Stop a voice immediately if it is still playing * @@ -42,7 +43,7 @@ public: * * \param handle Voice handle returned by SoundContext::play */ - virtual void stop(Sound::Handle & handle); + virtual void stop(const SoundHandle & handle); /** * \brief Change the volume of a voice * @@ -52,7 +53,7 @@ public: * \param handle Voice handle returned by SoundContext::play * \param volume New gain value (0=silent, 1=default) */ - virtual void set_volume(Sound::Handle & handle, float volume); + virtual void set_volume(const SoundHandle & handle, float volume); /** * \brief Set the looping behavior of a voice * @@ -62,7 +63,7 @@ public: * \param handle Voice handle returned by SoundContext::play * \param loop Looping behavior (false=oneshot, true=loop) */ - virtual void set_loop(Sound::Handle & handle, bool loop); + virtual void set_loop(const SoundHandle & handle, bool loop); private: //! Abstracted class @@ -70,6 +71,12 @@ private: //! Config reference Config & config = Config::get_instance(); + + //! Sound handle registry + std::unordered_map registry; + //! Unique handle counter + SoundHandle next_handle = 0; + }; } // namespace crepe diff --git a/src/crepe/facade/SoundHandle.h b/src/crepe/facade/SoundHandle.h new file mode 100644 index 0000000..131d28c --- /dev/null +++ b/src/crepe/facade/SoundHandle.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace crepe { + +/** + * \brief Voice handle returned by + */ +typedef size_t SoundHandle; + +} + diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 26913c0..c1cde8b 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -16,56 +16,50 @@ void AudioSystem::update() { for (AudioSource & component : components) { Sound & resource = resource_manager.get(component.source); - if (component.private_data.empty()) { - auto & data = component.private_data.set(); - this->update_last(component, data); - data.last_active = false; - } - auto & data = component.private_data.get(); - - this->diff_update(component, data, resource); + this->diff_update(component, resource); - this->update_last(component, data); + this->update_last(component); } } -void AudioSystem::diff_update(AudioSource & component, ComponentPrivate & data, - Sound & resource) { +void AudioSystem::diff_update(AudioSource & component, Sound & resource) { SoundContext & context = this->get_context(); - if (component.active != data.last_active) { + if (component.active != component.last_active) { if (component.active) { component.oneshot_play = component.play_on_awake; } else { - context.stop(data.handle); + context.stop(component.voice); return; } } if (!component.active) return; if (component.oneshot_play) { - data.handle = context.play(resource); + component.voice = context.play(resource); component.oneshot_play = false; } if (component.oneshot_stop) { - context.stop(data.handle); + context.stop(component.voice); component.oneshot_stop = false; } - if (component.volume != data.last_volume) { - context.set_volume(data.handle, component.volume); + if (component.volume != component.last_volume) { + context.set_volume(component.voice, component.volume); } - if (component.loop != data.last_loop) { - context.set_loop(data.handle, component.loop); + if (component.loop != component.last_loop) { + context.set_loop(component.voice, component.loop); } } -void AudioSystem::update_last(const AudioSource & component, ComponentPrivate & data) { - data.last_active = component.active; - data.last_loop = component.loop; - data.last_volume = component.volume; +void AudioSystem::update_last(AudioSource & component) { + component.last_active = component.active; + component.last_loop = component.loop; + component.last_volume = component.volume; } SoundContext & AudioSystem::get_context() { - if (this->context.empty()) this->context.set(); - return this->context.get(); + if (this->context == nullptr) + this->context = make_unique(); + return *this->context.get(); } + diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h index 4d21883..2ddc443 100644 --- a/src/crepe/system/AudioSystem.h +++ b/src/crepe/system/AudioSystem.h @@ -14,30 +14,14 @@ public: void update() override; private: - //! Private data stored by AudioSystem on AudioSource component - struct ComponentPrivate { - //! This sample's voice handle - Sound::Handle handle; - - /** - * \name State diffing variables - * \{ - */ - typeof(AudioSource::active) last_active; - typeof(AudioSource::volume) last_volume; - typeof(AudioSource::loop) last_loop; - //! \} - }; - /** - * \brief Update `last_*` members of \c data + * \brief Update `last_*` members of \c component * * Copies all component properties stored for comparison between AudioSystem::update() calls * - * \param component Source properties - * \param data Destination properties + * \param component AudioSource component to update */ - void update_last(const AudioSource & component, ComponentPrivate & data); + void update_last(AudioSource & component); /** * \brief Compare update component @@ -46,10 +30,9 @@ private: * applicable. * * \param component AudioSource component to update - * \param data AudioSource's private data * \param resource Sound instance for AudioSource's Asset */ - void diff_update(AudioSource & component, ComponentPrivate & data, Sound & resource); + void diff_update(AudioSource & component, Sound & resource); protected: /** @@ -61,8 +44,8 @@ protected: virtual SoundContext & get_context(); private: - //! Actually stores SoundContext if the base AudioSystem::get_context implementation is used - Private context; + //! SoundContext + std::unique_ptr context = nullptr; }; } // namespace crepe diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index f49d851..94ed906 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -1,7 +1,6 @@ target_sources(crepe PUBLIC LogColor.cpp Log.cpp - Private.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -12,7 +11,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Proxy.hpp OptionalRef.h OptionalRef.hpp - Private.h - Private.hpp ) diff --git a/src/crepe/util/Private.cpp b/src/crepe/util/Private.cpp deleted file mode 100644 index 262620d..0000000 --- a/src/crepe/util/Private.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "Private.h" - -using namespace crepe; - -bool Private::empty() const noexcept { return this->instance == nullptr; } - -Private::~Private() { - if (this->instance == nullptr) return; - this->destructor(this->instance); -} - -Private::Private(Private && other) { *this = std::move(other); } - -Private & Private::operator=(Private && other) { - // TODO: ideally this function checks for self-assignment - this->instance = other.instance; - this->destructor = other.destructor; - this->type = other.type; - - other.instance = nullptr; - other.destructor = [](void *) {}; - - return *this; -} - -Private::Private(const Private & other) {} -Private & Private::operator=(const Private & other) { return *this; } diff --git a/src/crepe/util/Private.h b/src/crepe/util/Private.h deleted file mode 100644 index d725a5e..0000000 --- a/src/crepe/util/Private.h +++ /dev/null @@ -1,89 +0,0 @@ -#pragma once - -#include -#include - -namespace crepe { - -/** - * \brief Utility for storing type hidden from user - * - * This class can be used to store types which cannot be used in the API directly due to header - * distribution limitations. This class is similar to `std::any`, but provides a method for - * retrieving a mutable reference to the stored object. - */ -class Private { -public: - Private() = default; - ~Private(); - /** - * \name Copy - * - * \note These functions do not do anything, resulting in `*this` being an empty (default) - * instance. - * - * \{ - */ - Private(const Private &); - Private & operator=(const Private &); - //! \} - /** - * \name Move - * - * These functions actually move the stored type if present. - * - * \{ - */ - Private(Private &&); - Private & operator=(Private &&); - //! \} - - /** - * \brief Get the stored object - * - * \tparam T Type of stored object - * - * \returns Mutable reference to stored object - * - * \throws std::out_of_range if this instance does not contain any object - * \throws std::logic_error if the stored type and requested type differ - */ - template - T & get() const; - - /** - * \brief Create and store an arbitrary object - * - * \tparam T Type of object - * \tparam Args Perfect forwarding arguments - * \param args Perfect forwarding arguments - * - * All arguments to this function are forwarded using `std::forward` to the constructor of T. - * - * \returns Mutable reference to stored object - * - * \note If this instance already contained an object, this function implicitly destroys the - * previous object. - */ - template - T & set(Args &&... args); - - /** - * \brief Check if this instance contains an object - * - * \returns `true` if this instance is empty, `false` if it contains an object - */ - bool empty() const noexcept; - -private: - //! Wrapper for destructor call of stored object type - std::function destructor; - //! Stored object's type - std::type_index type = typeid(void); - //! Stored object - void * instance = nullptr; -}; - -} // namespace crepe - -#include "Private.hpp" diff --git a/src/crepe/util/Private.hpp b/src/crepe/util/Private.hpp deleted file mode 100644 index b2174c0..0000000 --- a/src/crepe/util/Private.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include - -#include "Private.h" - -namespace crepe { - -template -T & Private::set(Args &&... args) { - if (!this->empty()) this->destructor(this->instance); - T * instance = new T(std::forward(args)...); - this->instance = static_cast(instance); - this->destructor = [](void * instance) { delete static_cast(instance); }; - this->type = typeid(T); - return *instance; -} - -template -T & Private::get() const { - using namespace std; - if (this->empty()) throw out_of_range("Private: get() called on empty object"); - type_index requested_type = typeid(T); - if (this->type != requested_type) - throw logic_error(format("Private: get() called with [T = {}] (actual is [T = {}])", - requested_type.name(), this->type.name())); - return *static_cast(this->instance); -} - -} // namespace crepe diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp index 7644ab7..774fdb8 100644 --- a/src/test/AudioTest.cpp +++ b/src/test/AudioTest.cpp @@ -16,10 +16,10 @@ class AudioTest : public Test { private: class TestSoundContext : public SoundContext { public: - MOCK_METHOD(Sound::Handle, play, (Sound & resource), (override)); - MOCK_METHOD(void, stop, (Sound::Handle &), (override)); - MOCK_METHOD(void, set_volume, (Sound::Handle &, float), (override)); - MOCK_METHOD(void, set_loop, (Sound::Handle &, bool), (override)); + MOCK_METHOD(SoundHandle, play, (Sound & resource), (override)); + MOCK_METHOD(void, stop, (const SoundHandle &), (override)); + MOCK_METHOD(void, set_volume, (const SoundHandle &, float), (override)); + MOCK_METHOD(void, set_loop, (const SoundHandle &, bool), (override)); }; class TestAudioSystem : public AudioSystem { diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 8c4b855..4174926 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -16,5 +16,4 @@ target_sources(test_main PUBLIC Vector2Test.cpp ScriptEventTest.cpp ScriptSceneTest.cpp - PrivateTest.cpp ) diff --git a/src/test/PrivateTest.cpp b/src/test/PrivateTest.cpp deleted file mode 100644 index 454789e..0000000 --- a/src/test/PrivateTest.cpp +++ /dev/null @@ -1,155 +0,0 @@ -#include - -#include - -using namespace std; -using namespace crepe; -using namespace testing; - -class PrivateTest : public Test { -public: - static unsigned constructors; - static unsigned destructors; - - void SetUp() override { - PrivateTest::constructors = 0; - PrivateTest::destructors = 0; - } - - class TestClass { - public: - TestClass() { PrivateTest::constructors++; } - ~TestClass() { PrivateTest::destructors++; } - }; - class Unrelated {}; -}; -unsigned PrivateTest::constructors; -unsigned PrivateTest::destructors; - -TEST_F(PrivateTest, Empty) { - { Private foo; } - - EXPECT_EQ(PrivateTest::constructors, 0); - EXPECT_EQ(PrivateTest::destructors, 0); -} - -TEST_F(PrivateTest, WithObject) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - } - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 1); -} - -TEST_F(PrivateTest, EmptyException) { - Private foo; - EXPECT_THROW(foo.get(), std::out_of_range); - - foo.set(); - EXPECT_NO_THROW(foo.get()); -} - -TEST_F(PrivateTest, IncorrectTypeException) { - Private foo; - foo.set(); - - EXPECT_THROW(foo.get(), std::logic_error); - EXPECT_NO_THROW(foo.get()); -} - -TEST_F(PrivateTest, MoveConstructor) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - - Private bar(std::move(foo)); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - } - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 1); -} - -TEST_F(PrivateTest, MoveOperator) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - - Private bar = std::move(foo); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - } - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 1); -} - -TEST_F(PrivateTest, CopyConstructor) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - - Private bar(foo); - - EXPECT_TRUE(bar.empty()); - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - } - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 1); -} - -TEST_F(PrivateTest, CopyOperator) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - - Private bar = foo; - - EXPECT_TRUE(bar.empty()); - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - } - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 1); -} - -TEST_F(PrivateTest, DoubleAssignment) { - { - Private foo; - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 1); - EXPECT_EQ(PrivateTest::destructors, 0); - - foo.set(); - - EXPECT_EQ(PrivateTest::constructors, 2); - EXPECT_EQ(PrivateTest::destructors, 1); - } - - EXPECT_EQ(PrivateTest::constructors, 2); - EXPECT_EQ(PrivateTest::destructors, 2); -} -- cgit v1.2.3 From 519cc5d16e65ff501d330c03eeb35d2d095ead29 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sun, 8 Dec 2024 19:49:41 +0100 Subject: made sdlcontext not a singleton anymore --- src/crepe/Resource.cpp | 3 ++- src/crepe/Resource.h | 3 ++- src/crepe/api/LoopManager.h | 2 +- src/crepe/api/LoopTimer.cpp | 13 +++++++------ src/crepe/api/LoopTimer.h | 14 +++++--------- src/crepe/api/Sprite.cpp | 9 ++++----- src/crepe/api/Sprite.h | 2 +- src/crepe/api/Texture.cpp | 18 ++++++------------ src/crepe/api/Texture.h | 19 ++++--------------- src/crepe/facade/SDLContext.cpp | 12 +++++------- src/crepe/facade/SDLContext.h | 2 +- src/crepe/facade/Sound.cpp | 2 +- src/crepe/facade/Sound.h | 3 ++- src/crepe/manager/Mediator.h | 4 ++-- src/crepe/manager/ResourceManager.hpp | 2 +- src/crepe/system/InputSystem.cpp | 1 - src/crepe/system/RenderSystem.cpp | 5 +---- src/crepe/system/ScriptSystem.cpp | 2 +- src/example/rendering_particle.cpp | 28 ++++++++++++++++------------ src/test/CMakeLists.txt | 2 +- src/test/ParticleTest.cpp | 3 ++- src/test/Profiling.cpp | 16 ++++++++++------ src/test/RenderSystemTest.cpp | 27 ++++++++++++++++----------- src/test/ResourceManagerTest.cpp | 3 ++- 24 files changed, 93 insertions(+), 102 deletions(-) (limited to 'src/crepe/facade/Sound.h') diff --git a/src/crepe/Resource.cpp b/src/crepe/Resource.cpp index 27b4c4b..85913ed 100644 --- a/src/crepe/Resource.cpp +++ b/src/crepe/Resource.cpp @@ -1,5 +1,6 @@ #include "Resource.h" +#include "manager/Mediator.h" using namespace crepe; -Resource::Resource(const Asset & asset) {} +Resource::Resource(const Asset & asset, Mediator & mediator) {} diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h index eceb15b..d65206b 100644 --- a/src/crepe/Resource.h +++ b/src/crepe/Resource.h @@ -4,6 +4,7 @@ namespace crepe { class ResourceManager; class Asset; +class Mediator; /** * \brief Resource interface @@ -17,7 +18,7 @@ class Asset; */ class Resource { public: - Resource(const Asset & src); + Resource(const Asset & src, Mediator & mediator); virtual ~Resource() = default; Resource(const Resource &) = delete; diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 8a30602..b800f5b 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -102,7 +102,7 @@ private: ResourceManager res_man {mediator}; //! Loop timer \todo no more singletons! - LoopTimer & loop_timer = LoopTimer::get_instance(); + LoopTimer loop_timer {mediator}; private: /** diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index 40fc94e..d6bf451 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -1,16 +1,16 @@ #include #include "../util/Log.h" +#include "facade/SDLContext.h" +#include "manager/Manager.h" #include "LoopTimer.h" using namespace crepe; -LoopTimer::LoopTimer() { dbg_trace(); } - -LoopTimer & LoopTimer::get_instance() { - static LoopTimer instance; - return instance; +LoopTimer::LoopTimer(Mediator & mediator) : Manager(mediator){ + dbg_trace(); + mediator.timer = *this; } void LoopTimer::start() { @@ -66,7 +66,8 @@ void LoopTimer::enforce_frame_rate() { = std::chrono::duration_cast(this->frame_target_time - frame_duration); if (delay_time.count() > 0) { - //SDLContext::get_instance().delay(delay_time.count()); + SDLContext & ctx = this->mediator.sdl_context; + ctx.delay(delay_time.count()); } } diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index 9393439..2a0b2a5 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -1,18 +1,14 @@ #pragma once +#include "manager/Manager.h" #include namespace crepe { -class LoopTimer { -public: - /** - * \brief Get the singleton instance of LoopTimer. - * - * \return A reference to the LoopTimer instance. - */ - static LoopTimer & get_instance(); +class Mediator; +class LoopTimer : public Manager { +public: /** * \brief Get the current delta time for the current frame. * @@ -102,7 +98,7 @@ private: * * Private constructor for singleton pattern to restrict instantiation outside the class. */ - LoopTimer(); + LoopTimer(Mediator & mediator); /** * \brief Update the timer to the current frame. diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index bae5ad9..4cf214c 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,5 +1,4 @@ #include -#include #include "../util/Log.h" #include "api/Asset.h" @@ -11,16 +10,16 @@ using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, const Asset & texture, const Sprite::Data & data) +Sprite::Sprite(game_object_id_t id, const Asset & texture, const ivec2 & size, const Sprite::Data & data) : Component(id), source(texture), data(data) { dbg_trace(); - //this->mask.w = this->texture.get_size().x; - //this->mask.h = this->texture.get_size().y; - //this->aspect_ratio = static_cast(this->mask.w) / this->mask.h; + this->mask.w = size.x; + this->mask.h = size.y; + this->aspect_ratio = static_cast(this->mask.w) / this->mask.h; } Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index ec120c0..9ef9f03 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -75,7 +75,7 @@ public: * \param texture asset of the image * \param ctx all the sprite data */ - Sprite(game_object_id_t id, const Asset & texture, const Data & data); + Sprite(game_object_id_t id, const Asset & texture, const ivec2 & size, const Data & data); ~Sprite(); //! Texture used for the sprite diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 9d8e02d..2ac8606 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -3,27 +3,21 @@ #include "Asset.h" #include "Resource.h" #include "Texture.h" +#include "facade/SDLContext.h" +#include "manager/Mediator.h" #include "types.h" -#include using namespace crepe; using namespace std; -Texture::Texture(const Asset & src) : Resource(src) { +Texture::Texture(const Asset & src, Mediator & mediator) : Resource(src, mediator) { dbg_trace(); + SDLContext & ctx = mediator.sdl_context; + this->texture = ctx.texture_from_path(src.get_path()); + this->size = ctx.get_size(*this); } Texture::~Texture() { dbg_trace(); this->texture.reset(); } - -void Texture::load(std::unique_ptr> texture) { - this->texture = std::move(texture); - this->loaded = true; -} - -ivec2 Texture::get_size() const { - if (this->texture == nullptr) return {}; - return {}; -} diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index f9c7919..4eb1058 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -15,6 +15,7 @@ namespace crepe { class SDLContext; class Animator; +class Mediator; /** * \class Texture @@ -30,31 +31,19 @@ public: * \brief Constructs a Texture from an Asset resource. * \param src Asset with texture data to load. */ - Texture(const Asset & src); + Texture(const Asset & src, Mediator & mediator); /** * \brief Destroys the Texture instance, freeing associated resources. */ ~Texture(); - /** - * \brief Gets the width and height of the texture. - * \return Width and height of the texture in pixels. - */ - ivec2 get_size() const; - -private: - /** - * \brief Loads the texture from an Asset resource. - * \param res Unique pointer to an Asset resource to load the texture from. - */ - void load(std::unique_ptr> texture); - private: //! The texture of the class from the library std::unique_ptr> texture; - bool loaded = false; + // texture size in pixel + ivec2 size; //! Grants SDLContext access to private members. friend class SDLContext; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 85257d6..82c8c50 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -30,7 +30,7 @@ using namespace crepe; using namespace std; -SDLContext::SDLContext(Mediator & mediator) : Manager(mediator){ +SDLContext::SDLContext(Mediator & mediator) : Manager(mediator) { dbg_trace(); mediator.sdl_context = *this; if (SDL_Init(SDL_INIT_VIDEO) != 0) { @@ -232,7 +232,7 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { const Sprite::Data & data = ctx.sprite.data; - vec2 size; + vec2 size = {data.size.x , data.size.y}; if (data.size.x == 0 && data.size.y != 0) { size.x = data.size.y * ctx.sprite.aspect_ratio; } @@ -258,9 +258,6 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { } void SDLContext::draw(const RenderContext & ctx) { - - if (!ctx.texture.loaded) ctx.texture.load(this->texture_from_path(ctx.sprite.source.get_path())); - const Sprite::Data & data = ctx.sprite.data; SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x) @@ -274,11 +271,12 @@ void SDLContext::draw(const RenderContext & ctx) { .img_scale = ctx.scale, }); + cout << dstrect.w << " " << dstrect.h << " " << dstrect.x << " " << dstrect.y << endl; double angle = ctx.angle + data.angle_offset; this->set_color_texture(ctx.texture, ctx.sprite.data.color); - SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.texture.get(), &srcrect, - &dstrect, angle, NULL, render_flip); + int error = SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.texture.get(), + &srcrect, &dstrect, angle, NULL, render_flip); } SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index d95ebec..9676940 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -64,7 +64,7 @@ public: //! rendering data needed to render on screen struct RenderContext { const Sprite & sprite; - Texture & texture; + const Texture & texture; const CameraValues & cam; const vec2 & pos; const double & angle; diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index ad50637..97e455e 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -6,7 +6,7 @@ using namespace crepe; using namespace std; -Sound::Sound(const Asset & src) : Resource(src) { +Sound::Sound(const Asset & src, Mediator & mediator) : Resource(src, mediator) { this->sample.load(src.get_path().c_str()); dbg_trace(); } diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index 85d141b..4a5d692 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -8,6 +8,7 @@ namespace crepe { class SoundContext; +class Mediator; /** * \brief Sound resource facade @@ -17,7 +18,7 @@ class SoundContext; */ class Sound : public Resource { public: - Sound(const Asset & src); + Sound(const Asset & src, Mediator & mediator); ~Sound(); // dbg_trace private: diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index 6a9e113..bb51d6b 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -5,7 +5,6 @@ // TODO: remove these singletons: #include "EventManager.h" #include "SaveManager.h" -#include "api/LoopTimer.h" namespace crepe { @@ -13,6 +12,7 @@ class ComponentManager; class SceneManager; class ResourceManager; class SDLContext; +class LoopTimer; /** * Struct to pass references to classes that would otherwise need to be singletons down to @@ -33,7 +33,7 @@ struct Mediator { OptionalRef save_manager = SaveManager::get_instance(); OptionalRef event_manager = EventManager::get_instance(); OptionalRef resource_manager; - OptionalRef timer = LoopTimer::get_instance(); + OptionalRef timer; }; } // namespace crepe diff --git a/src/crepe/manager/ResourceManager.hpp b/src/crepe/manager/ResourceManager.hpp index 5167d71..cf5c949 100644 --- a/src/crepe/manager/ResourceManager.hpp +++ b/src/crepe/manager/ResourceManager.hpp @@ -13,7 +13,7 @@ T & ResourceManager::get(const Asset & asset) { "cache must recieve a derivative class of Resource"); CacheEntry & entry = this->get_entry(asset); - if (entry.resource == nullptr) entry.resource = make_unique(asset); + if (entry.resource == nullptr) entry.resource = make_unique(asset, this->mediator); T * concrete_resource = dynamic_cast(entry.resource.get()); if (concrete_resource == nullptr) diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index b36ec09..a710ae2 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -9,7 +9,6 @@ using namespace crepe; void InputSystem::update() { - dbg_trace(); ComponentManager & mgr = this->mediator.component_manager; EventManager & event_mgr = this->mediator.event_manager; SDLContext & context = this->mediator.sdl_context; diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index d81d8dd..daf71c5 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -13,8 +13,6 @@ #include "../manager/ComponentManager.h" #include "api/Texture.h" #include "manager/ResourceManager.h" -#include "util/Log.h" - #include "RenderSystem.h" using namespace crepe; @@ -65,7 +63,6 @@ RefVector RenderSystem::sort(RefVector & objs) const { } void RenderSystem::update() { - dbg_trace(); this->clear_screen(); this->render(); this->present_screen(); @@ -108,7 +105,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::Camera const Transform & tm) { SDLContext & ctx = this->mediator.sdl_context; ResourceManager & resource_manager = this->mediator.resource_manager; - Texture & res = resource_manager.get(sprite.source); + const Texture & res = resource_manager.get(sprite.source); ctx.draw(SDLContext::RenderContext{ .sprite = sprite, diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index d6b2ca1..df358e6 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -8,7 +8,7 @@ using namespace std; using namespace crepe; void ScriptSystem::update() { - dbg_trace(); + //dbg_trace(); ComponentManager & mgr = this->mediator.component_manager; RefVector behavior_scripts = mgr.get_components_by_type(); diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 13ea591..87a6eb9 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -52,19 +52,22 @@ public: Color color(255, 255, 255, 255); - Asset img{"asset/texture/img.png"}; + Asset img{"asset/texture/test_ap43.png"}; + + Sprite & test_sprite + = game_object.add_component(img, ivec2{259, 195}, + Sprite::Data{ + .color = color, + .flip = Sprite::FlipSettings{false, false}, + .sorting_in_layer = 2, + .order_in_layer = 2, + .size = {0, 100}, + .angle_offset = 0, + .position_offset = {0, 0}, + }); + + /* - Sprite & test_sprite = game_object.add_component( - img, Sprite::Data{ - .color = color, - .flip = Sprite::FlipSettings{false, false}, - .sorting_in_layer = 2, - .order_in_layer = 2, - .size = {0, 100}, - .angle_offset = 0, - .position_offset = {100, 0}, - }); - auto & anim = game_object.add_component(test_sprite, 4, 4, Animator::Data{ .fps = 1, @@ -72,6 +75,7 @@ public: }); anim.set_anim(2); anim.active = false; + */ auto & cam = game_object.add_component(ivec2{1280, 720}, vec2{400, 400}, Camera::Data{ diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 9f986a0..9d08767 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -15,7 +15,7 @@ target_sources(test_main PUBLIC ValueBrokerTest.cpp DBTest.cpp Vector2Test.cpp - InputTest.cpp + #InputTest.cpp ScriptEventTest.cpp ScriptSceneTest.cpp Profiling.cpp diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 1409c4f..38f4bde 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,3 +1,4 @@ +#include "api/Asset.h" #include #include #include @@ -30,7 +31,7 @@ public: GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 0); Color color(0, 0, 0, 0); - auto s1 = Texture("asset/texture/img.png"); + auto s1 = Asset("asset/texture/img.png"); Sprite & test_sprite = game_object.add_component( s1, Sprite::Data{ .color = color, diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp index c753bca..46da378 100644 --- a/src/test/Profiling.cpp +++ b/src/test/Profiling.cpp @@ -1,4 +1,6 @@ +#include "facade/SDLContext.h" #include "manager/Mediator.h" +#include "manager/ResourceManager.h" #include "system/ParticleSystem.h" #include "system/PhysicsSystem.h" #include "system/RenderSystem.h" @@ -41,7 +43,7 @@ class TestScript : public Script { } }; -class DISABLED_ProfilingTest : public Test { +class ProfilingTest : public Test { public: // Config for test // Minimum amount to let test pass @@ -61,6 +63,8 @@ public: ParticleSystem particle_sys{m}; RenderSystem render_sys{m}; ScriptSystem script_sys{m}; + SDLContext ctx{m}; + ResourceManager resource_manager{m}; // Test data std::map timings; @@ -130,7 +134,7 @@ public: } }; -TEST_F(DISABLED_ProfilingTest, Profiling_1) { +TEST_F(ProfilingTest, Profiling_1) { while (this->total_time / this->average < this->duration) { { @@ -153,7 +157,7 @@ TEST_F(DISABLED_ProfilingTest, Profiling_1) { EXPECT_GE(this->game_object_count, this->min_gameobject_count); } -TEST_F(DISABLED_ProfilingTest, Profiling_2) { +TEST_F(ProfilingTest, Profiling_2) { while (this->total_time / this->average < this->duration) { { @@ -167,7 +171,7 @@ TEST_F(DISABLED_ProfilingTest, Profiling_2) { gameobject.add_component(vec2{0, 0}, vec2{1, 1}); gameobject.add_component().set_script(); - auto img = Texture("asset/texture/square.png"); + auto img = Asset("asset/texture/square.png"); Sprite & test_sprite = gameobject.add_component( img, Sprite::Data{ .color = {0, 0, 0, 0}, @@ -192,7 +196,7 @@ TEST_F(DISABLED_ProfilingTest, Profiling_2) { EXPECT_GE(this->game_object_count, this->min_gameobject_count); } -TEST_F(DISABLED_ProfilingTest, Profiling_3) { +TEST_F(ProfilingTest, Profiling_3) { while (this->total_time / this->average < this->duration) { { @@ -205,7 +209,7 @@ TEST_F(DISABLED_ProfilingTest, Profiling_3) { }); gameobject.add_component(vec2{0, 0}, vec2{1, 1}); gameobject.add_component().set_script(); - auto img = Texture("asset/texture/square.png"); + auto img = Asset("asset/texture/square.png"); Sprite & test_sprite = gameobject.add_component( img, Sprite::Data{ .color = {0, 0, 0, 0}, diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index 205f534..1b2de7a 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -1,3 +1,6 @@ +#include "api/Asset.h" +#include "facade/SDLContext.h" +#include "manager/ResourceManager.h" #include "types.h" #include #include @@ -25,6 +28,8 @@ class RenderSystemTest : public Test { public: ComponentManager mgr{m}; + SDLContext ctx{m}; + ResourceManager resource_manager{m}; RenderSystem sys{m}; GameObject entity1 = this->mgr.new_object("name"); GameObject entity2 = this->mgr.new_object("name"); @@ -32,10 +37,10 @@ public: GameObject entity4 = this->mgr.new_object("name"); void SetUp() override { - auto s1 = Texture("asset/texture/img.png"); - auto s2 = Texture("asset/texture/img.png"); - auto s3 = Texture("asset/texture/img.png"); - auto s4 = Texture("asset/texture/img.png"); + auto s1 = Asset("asset/texture/img.png"); + auto s2 = Asset("asset/texture/img.png"); + auto s3 = Asset("asset/texture/img.png"); + auto s4 = Asset("asset/texture/img.png"); auto & sprite1 = entity1.add_component(s1, Sprite::Data{ .color = Color(0, 0, 0, 0), @@ -45,7 +50,7 @@ public: .size = {10, 10}, }); - ASSERT_NE(sprite1.texture.texture.get(), nullptr); + //ASSERT_NE(sprite1.texture.texture.get(), nullptr); EXPECT_EQ(sprite1.data.order_in_layer, 5); EXPECT_EQ(sprite1.data.sorting_in_layer, 5); auto & sprite2 @@ -55,7 +60,7 @@ public: .sorting_in_layer = 2, .order_in_layer = 1, }); - ASSERT_NE(sprite2.texture.texture.get(), nullptr); + //ASSERT_NE(sprite2.texture.texture.get(), nullptr); EXPECT_EQ(sprite2.data.sorting_in_layer, 2); EXPECT_EQ(sprite2.data.order_in_layer, 1); @@ -66,7 +71,7 @@ public: .sorting_in_layer = 1, .order_in_layer = 2, }); - ASSERT_NE(sprite3.texture.texture.get(), nullptr); + //ASSERT_NE(sprite3.texture.texture.get(), nullptr); EXPECT_EQ(sprite3.data.sorting_in_layer, 1); EXPECT_EQ(sprite3.data.order_in_layer, 2); @@ -77,7 +82,7 @@ public: .sorting_in_layer = 1, .order_in_layer = 1, }); - ASSERT_NE(sprite4.texture.texture.get(), nullptr); + //ASSERT_NE(sprite4.texture.texture.get(), nullptr); EXPECT_EQ(sprite4.data.sorting_in_layer, 1); EXPECT_EQ(sprite4.data.order_in_layer, 1); } @@ -87,8 +92,8 @@ TEST_F(RenderSystemTest, expected_throws) { GameObject entity1 = this->mgr.new_object("NAME"); // no texture img - EXPECT_ANY_THROW({ - auto test = Texture(""); + EXPECT_NO_THROW({ + auto test = Asset(""); auto & sprite1 = entity1.add_component( test, Sprite::Data{ .color = Color(0, 0, 0, 0), @@ -185,7 +190,7 @@ TEST_F(RenderSystemTest, Color) { Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); auto & sprite = this->mgr.get_components_by_id(entity1.id).front().get(); - ASSERT_NE(sprite.texture.texture.get(), nullptr); + //ASSERT_NE(sprite.texture.texture.get(), nullptr); sprite.data.color = Color::GREEN; EXPECT_EQ(sprite.data.color.r, Color::GREEN.r); diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp index 44a5921..e040f54 100644 --- a/src/test/ResourceManagerTest.cpp +++ b/src/test/ResourceManagerTest.cpp @@ -1,3 +1,4 @@ +#include "manager/Mediator.h" #include #define private public @@ -30,7 +31,7 @@ public: public: const unsigned instance; - TestResource(const Asset & src) : Resource(src), instance(this->instances++) {} + TestResource(const Asset & src, Mediator & mediator) : Resource(src, mediator), instance(this->instances++) {} ~TestResource() { this->instances--; } bool operator==(const TestResource & other) const { return this->instance == other.instance; -- cgit v1.2.3