diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/Component.h | 11 | ||||
-rw-r--r-- | src/crepe/Resource.cpp | 6 | ||||
-rw-r--r-- | src/crepe/Resource.h | 26 | ||||
-rw-r--r-- | src/crepe/api/AssetManager.cpp | 17 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.cpp | 20 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.h | 60 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/crepe/api/ResourceManager.cpp | 41 | ||||
-rw-r--r-- | src/crepe/api/ResourceManager.h | 69 | ||||
-rw-r--r-- | src/crepe/facade/Sound.cpp | 28 | ||||
-rw-r--r-- | src/crepe/facade/Sound.h | 17 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.cpp | 5 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.h | 6 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.cpp | 56 | ||||
-rw-r--r-- | src/crepe/system/AudioSystem.h | 21 | ||||
-rw-r--r-- | src/crepe/system/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/example/asset_manager.cpp | 22 | ||||
-rw-r--r-- | src/example/audio_internal.cpp | 4 | ||||
-rw-r--r-- | src/test/AudioTest.cpp | 26 | ||||
-rw-r--r-- | src/test/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/test/ParticleTest.cpp | 2 | ||||
-rw-r--r-- | src/test/ResourceManagerTest.cpp | 52 | ||||
-rw-r--r-- | src/test/main.cpp | 15 |
24 files changed, 451 insertions, 68 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 7e176e7..df15b8f 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -3,6 +3,7 @@ target_sources(crepe PUBLIC ComponentManager.cpp Component.cpp Collider.cpp + Resource.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -12,6 +13,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h ValueBroker.h ValueBroker.hpp + Resource.h ) add_subdirectory(api) diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 5279fb3..2e4ef7d 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -16,7 +16,12 @@ class Component { public: //! Whether the component is active bool active = true; - //! The id of the GameObject this component belongs to + /** + * \brief The id of the GameObject this component belongs to + * + * \note Only systems are supposed to use this member, but since friend + * relations aren't inherited this needs to be public. + */ const game_object_id_t game_object_id; protected: @@ -24,7 +29,7 @@ protected: * \param id The id of the GameObject this component belongs to */ Component(game_object_id_t id); - //! Only the ComponentManager can create components + //! Only ComponentManager can create components friend class ComponentManager; public: @@ -40,6 +45,8 @@ public: * \return The maximum number of instances for this component */ virtual int get_instances_max() const { return -1; } + //! Only ComponentManager needs to know the max instance count + friend class ComponentManager; }; } // namespace crepe 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 new file mode 100644 index 0000000..a0c8859 --- /dev/null +++ b/src/crepe/Resource.h @@ -0,0 +1,26 @@ +#pragma once + +namespace crepe { + +class ResourceManager; +class Asset; + +/** + * Resource is an interface class used to represent a (deserialized) game + * resource (e.g. textures, sounds). + */ +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/AssetManager.cpp b/src/crepe/api/AssetManager.cpp deleted file mode 100644 index 3925758..0000000 --- a/src/crepe/api/AssetManager.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "util/Log.h" - -#include "AssetManager.h" - -using namespace crepe; - -AssetManager & AssetManager::get_instance() { - static AssetManager instance; - return instance; -} - -AssetManager::~AssetManager() { - dbg_trace(); - this->asset_cache.clear(); -} - -AssetManager::AssetManager() { dbg_trace(); } diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp new file mode 100644 index 0000000..4baac9a --- /dev/null +++ b/src/crepe/api/AudioSource.cpp @@ -0,0 +1,20 @@ +#include "AudioSource.h" + +using namespace crepe; +using namespace std; + +AudioSource::AudioSource(game_object_id_t id, const Asset & src) : + Component(id), + source(src) +{ } + +void AudioSource::play(bool looping) { + this->loop = looping; + this->playing = true; +} + +void AudioSource::stop() { + this->playing = false; + this->rewind = true; +} + diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h new file mode 100644 index 0000000..1264790 --- /dev/null +++ b/src/crepe/api/AudioSource.h @@ -0,0 +1,60 @@ +#pragma once + +#include "../Component.h" +#include "../types.h" + +#include "Asset.h" + +namespace crepe { + +class AudioSystem; + +//! Audio source component +class AudioSource : public Component { + //! AudioSource components are handled by AudioSystem + friend class AudioSystem; + +protected: + AudioSource(game_object_id_t id, const Asset & source); + //! Only ComponentManager can create components + friend class ComponentManager; +public: + // But std::unique_ptr needs to be able to destoy this component again + virtual ~AudioSource() = default; + +public: + //! Start or resume this audio source + void play(bool looping = false); + //! Stop this audio source + void stop(); + +public: + //! Play when this component becomes active + bool play_on_awake = false; + //! Repeat the current audio clip during playback + bool loop = false; + //! Normalized volume (0.0 - 1.0) + float volume = 1.0; + +private: + //! This audio source's clip + const Asset source; + + //! If this source is playing audio + bool playing = false; + //! Rewind the sample location + 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; +}; + +} // namespace crepe + diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 6557656..cca0e8b 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,5 +1,5 @@ target_sources(crepe PUBLIC - # AudioSource.cpp + AudioSource.cpp BehaviorScript.cpp GameObject.cpp Rigidbody.cpp @@ -7,7 +7,7 @@ target_sources(crepe PUBLIC Transform.cpp Color.cpp Texture.cpp - AssetManager.cpp + ResourceManager.cpp Sprite.cpp SaveManager.cpp Config.cpp @@ -23,7 +23,7 @@ target_sources(crepe PUBLIC ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - # AudioSource.h + AudioSource.h BehaviorScript.h Config.h Script.h @@ -35,8 +35,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Vector2.h Color.h Texture.h - AssetManager.h - AssetManager.hpp + ResourceManager.h SaveManager.h Scene.h Metadata.h diff --git a/src/crepe/api/ResourceManager.cpp b/src/crepe/api/ResourceManager.cpp new file mode 100644 index 0000000..7877ed9 --- /dev/null +++ b/src/crepe/api/ResourceManager.cpp @@ -0,0 +1,41 @@ +#include <stdexcept> + +#include "util/Log.h" + +#include "ResourceManager.h" + +using namespace crepe; +using namespace std; + +ResourceManager & ResourceManager::get_instance() { + static ResourceManager instance; + return instance; +} + +ResourceManager::~ResourceManager() { dbg_trace(); } +ResourceManager::ResourceManager() { dbg_trace(); } + +void ResourceManager::clear() { + this->resources.clear(); +} + +template <typename T> +T & ResourceManager::cache(const Asset & asset) { + dbg_trace(); + static_assert(is_base_of<Resource, T>::value, "cache must recieve a derivative class of Resource"); + + if (!this->resources.contains(asset)) + this->resources[asset] = make_unique<T>(asset); + + Resource * resource = this->resources.at(asset).get(); + T * concrete_resource = dynamic_cast<T *>(resource); + + if (concrete_resource == nullptr) + throw runtime_error(format("ResourceManager: mismatch between requested type and actual type of resource ({})", asset.get_path())); + + return *concrete_resource; +} + +#include "../facade/Sound.h" +template Sound & ResourceManager::cache(const Asset &); + diff --git a/src/crepe/api/ResourceManager.h b/src/crepe/api/ResourceManager.h new file mode 100644 index 0000000..efdd5c5 --- /dev/null +++ b/src/crepe/api/ResourceManager.h @@ -0,0 +1,69 @@ +#pragma once + +#include <memory> +#include <unordered_map> + +#include "Asset.h" +#include "Resource.h" + +namespace crepe { + +class Sound; + +/** + * \brief The ResourceManager is responsible for storing and managing assets over + * multiple scenes. + * + * 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. + */ +class ResourceManager { + +private: + //! A cache that holds all the assets, accessible by their file path, over multiple scenes. + std::unordered_map<const Asset, std::unique_ptr<Resource>> resources; + +private: + ResourceManager(); // dbg_trace + virtual ~ResourceManager(); // dbg_trace + + ResourceManager(const ResourceManager &) = delete; + ResourceManager(ResourceManager &&) = delete; + ResourceManager & operator=(const ResourceManager &) = delete; + ResourceManager & operator=(ResourceManager &&) = delete; + +public: + /** + * \brief Retrieves the singleton instance of the ResourceManager. + * + * \return A reference to the single instance of the ResourceManager. + */ + static ResourceManager & get_instance(); + +public: + /** + * \brief Caches an asset by loading it from the given file path. + * + * \param file_path The path to the asset file to load. + * \param reload If true, the asset will be reloaded from the file, even if + * it is already cached. + * \tparam T The type of asset to cache (e.g., texture, sound, etc.). + * + * \return A reference to the resource + * + * This template function caches the asset at the given file path. If the + * asset is already cached, the existing instance will be returned. + * Otherwise, the concrete resource will be instantiated and added to the + * cache. + */ + template <typename T> + T & cache(const Asset & asset); + + //! Clear the resource cache + void clear(); +}; + +} // namespace crepe + diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index 4d3abf5..b589759 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -1,3 +1,4 @@ +#include "../api/Asset.h" #include "../util/Log.h" #include "Sound.h" @@ -6,20 +7,14 @@ using namespace crepe; using namespace std; -Sound::Sound(unique_ptr<Asset> res) { +Sound::Sound(const Asset & src) : Resource(src) { + this->sample.load(src.get_path().c_str()); dbg_trace(); - this->load(std::move(res)); } - -Sound::Sound(const char * src) { - dbg_trace(); - this->load(make_unique<Asset>(src)); -} - -void Sound::load(unique_ptr<Asset> res) { this->sample.load(res->get_path().c_str()); } +Sound::~Sound() { dbg_trace(); } void Sound::play() { - SoundContext & ctx = SoundContext::get_instance(); + SoundContext & ctx = this->context.get(); if (ctx.engine.getPause(this->handle)) { // resume if paused ctx.engine.setPause(this->handle, false); @@ -31,13 +26,13 @@ void Sound::play() { } void Sound::pause() { - SoundContext & ctx = SoundContext::get_instance(); + SoundContext & ctx = this->context.get(); if (ctx.engine.getPause(this->handle)) return; ctx.engine.setPause(this->handle, true); } void Sound::rewind() { - SoundContext & ctx = SoundContext::get_instance(); + SoundContext & ctx = this->context.get(); if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.seek(this->handle, 0); } @@ -45,7 +40,7 @@ void Sound::rewind() { void Sound::set_volume(float volume) { this->volume = volume; - SoundContext & ctx = SoundContext::get_instance(); + SoundContext & ctx = this->context.get(); if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.setVolume(this->handle, this->volume); } @@ -53,7 +48,12 @@ void Sound::set_volume(float volume) { void Sound::set_looping(bool looping) { this->looping = looping; - SoundContext & ctx = SoundContext::get_instance(); + SoundContext & ctx = this->context.get(); if (!ctx.engine.isValidVoiceHandle(this->handle)) return; 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 4c68f32..a84aa8c 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -1,21 +1,25 @@ #pragma once -#include <memory> #include <soloud/soloud.h> #include <soloud/soloud_wav.h> -#include "../api/Asset.h" +#include "../util/OptionalRef.h" +#include "../Resource.h" namespace crepe { +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. */ -class Sound { +class Sound : public Resource { public: + Sound(const Asset & src); + ~Sound(); // dbg_trace /** * \brief Pause this sample * @@ -67,15 +71,12 @@ public: bool get_looping() const { return this->looping; } public: - Sound(const char * src); - Sound(std::unique_ptr<Asset> res); - -private: - void load(std::unique_ptr<Asset> res); + void set_context(SoundContext & ctx); private: SoLoud::Wav sample; SoLoud::handle handle; + OptionalRef<SoundContext> context; float volume = 1.0f; bool looping = false; diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index deb2b62..b65dfb2 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -4,11 +4,6 @@ using namespace crepe; -SoundContext & SoundContext::get_instance() { - static SoundContext instance; - return instance; -} - SoundContext::SoundContext() { dbg_trace(); engine.init(); diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index d703c16..d22ff7a 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -13,18 +13,18 @@ namespace crepe { * the methods for playing \c Sound instances. It is part of the sound facade. */ class SoundContext { -private: - // singleton +public: SoundContext(); virtual ~SoundContext(); + SoundContext(const SoundContext &) = delete; SoundContext(SoundContext &&) = delete; SoundContext & operator=(const SoundContext &) = delete; SoundContext & operator=(SoundContext &&) = delete; private: - static SoundContext & get_instance(); SoLoud::Soloud engine; + //! Sound directly calls methods on \c engine friend class Sound; }; diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp new file mode 100644 index 0000000..c8dae9d --- /dev/null +++ b/src/crepe/system/AudioSystem.cpp @@ -0,0 +1,56 @@ +#include "AudioSystem.h" +#include "ComponentManager.h" + +#include "../api/AudioSource.h" + +using namespace crepe; +using namespace std; + +void AudioSystem::update() { + ComponentManager & mgr = this->component_manager; + vector<reference_wrapper<AudioSource>> components = mgr.get_components_by_type<AudioSource>(); + + for (auto component_ref : components) { + AudioSource & component = component_ref.get(); + if (!component.active) continue; + + /** + * How this is supposed to work: + * - Get an instance of Sound for this resource/component combo (Sound + * instance is supposed to be unique per component, even if they use the + * same underlying asset). + * OR + * - Use the same instance of Sound if this is what the cache returns + * (= what the game programmer's wishes to do). + * + * NOT supposed to happen but still the case: + * - Below function call causes assets to be cached unintentionally + * - Cached assets are deleted at the end of a scene (i think?) + * - I'm not sure if the ResourceManager is even supposed to have a public + * `.clear()` method since the control over resource lifetime is + * explicitly handed over to the game programmer by using ResourceManager + * to cache/uncache. I believe the proper methods are supposed to be: + * + * - get() get a reference to resource (used here) + * - clear() clears NON-cached assets + * - cache() marks asset as "do not delete at end of scene" + * - uncache() undoes the above + * + * I think somewhere in the above function calls a unique identifier for + * the Asset/GameObject should be given to make sure the unique instance + * shit works as intended. The resource manager is also used for things + * other than sounds. + * + * Also need to check: + * - Is it an issue if there are multiple AudioSource components playing + * the same sample (= identical Asset), while they are all triggered + * using the same underlying instance of Sound (esp. w/ + * play/pause/retrigger behavior). + */ + Sound & sound = this->resman.cache<Sound>(component.source); + sound.set_context(this->context); + + // TODO: lots of state diffing + } +} + diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h new file mode 100644 index 0000000..d0b4f9a --- /dev/null +++ b/src/crepe/system/AudioSystem.h @@ -0,0 +1,21 @@ +#pragma once + +#include "../facade/SoundContext.h" +#include "../api/ResourceManager.h" + +#include "System.h" + +namespace crepe { + +class AudioSystem : public System { +public: + using System::System; + void update() override; + +private: + SoundContext context {}; + ResourceManager & resman = ResourceManager::get_instance(); +}; + +} // namespace crepe + diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index d658b25..f507b90 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -5,6 +5,7 @@ target_sources(crepe PUBLIC PhysicsSystem.cpp CollisionSystem.cpp RenderSystem.cpp + AudioSystem.cpp AnimatorSystem.cpp ) @@ -14,5 +15,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES PhysicsSystem.h CollisionSystem.h RenderSystem.h + AudioSystem.h AnimatorSystem.h ) diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp index 917b547..660b318 100644 --- a/src/example/asset_manager.cpp +++ b/src/example/asset_manager.cpp @@ -8,7 +8,7 @@ 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"); } + { 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). @@ -17,20 +17,20 @@ int main() { { // TODO: [design] the Sound class can't be directly included by the user as it includes // SoLoud headers. - auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg"); - auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav"); - auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav"); + auto bgm = mgr.cache<Sound>("mwe/audio/bgm.ogg"); + auto sfx1 = mgr.cache<Sound>("mwe/audio/sfx1.wav"); + auto sfx2 = mgr.cache<Sound>("mwe/audio/sfx2.wav"); - auto img = mgr.cache<Texture>("../asset/texture/img.png"); - auto img1 = mgr.cache<Texture>("../asset/texture/second.png"); + auto img = mgr.cache<Texture>("asset/texture/img.png"); + auto img1 = mgr.cache<Texture>("asset/texture/second.png"); } { - auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg"); - auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav"); - auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav"); + auto bgm = mgr.cache<Sound>("mwe/audio/bgm.ogg"); + auto sfx1 = mgr.cache<Sound>("mwe/audio/sfx1.wav"); + auto sfx2 = mgr.cache<Sound>("mwe/audio/sfx2.wav"); - auto img = mgr.cache<Texture>("../asset/texture/img.png"); - auto img1 = mgr.cache<Texture>("../asset/texture/second.png"); + auto img = mgr.cache<Texture>("asset/texture/img.png"); + auto img1 = mgr.cache<Texture>("asset/texture/second.png"); } } diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp index 1647f20..f3bf349 100644 --- a/src/example/audio_internal.cpp +++ b/src/example/audio_internal.cpp @@ -4,7 +4,9 @@ */ #include <crepe/api/Config.h> +#include <crepe/facade/SoundContext.h> #include <crepe/facade/Sound.h> +#include <crepe/Asset.h> #include <crepe/util/Log.h> #include <thread> @@ -24,6 +26,8 @@ int _ = []() { }(); int main() { + SoundContext ctx{}; + Sound sound{ctx}; // Load a background track (Ogg Vorbis) auto bgm = Sound("mwe/audio/bgm.ogg"); // Load three short samples (WAV) diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp new file mode 100644 index 0000000..e181de9 --- /dev/null +++ b/src/test/AudioTest.cpp @@ -0,0 +1,26 @@ +#include <gtest/gtest.h> + +#include <crepe/ComponentManager.h> +#include <crepe/api/AudioSource.h> +#include <crepe/api/GameObject.h> +#include <crepe/system/AudioSystem.h> + +using namespace std; +using namespace crepe; +using namespace testing; + +class AudioTest : public Test { +public: + ComponentManager component_manager{}; + AudioSystem system {component_manager}; + + void SetUp() override { + auto & mgr = this->component_manager; + GameObject entity = mgr.new_object("name"); + entity.add_component<AudioSource>("mwe/audio/sfx1.wav"); + } +}; + +TEST_F(AudioTest, Default) { +} + diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index f722082..437c296 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -3,7 +3,9 @@ target_sources(test_main PUBLIC PhysicsTest.cpp ScriptTest.cpp ParticleTest.cpp + AudioTest.cpp AssetTest.cpp + ResourceManagerTest.cpp OptionalRefTest.cpp ) diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 4e655a9..cfbbc0e 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -29,7 +29,7 @@ public: Color color(0, 0, 0, 0); Sprite test_sprite = game_object.add_component<Sprite>( - make_shared<Texture>("../asset/texture/img.png"), color, + make_shared<Texture>("asset/texture/img.png"), color, FlipSettings{true, true}); game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{ diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp new file mode 100644 index 0000000..3fc9ebd --- /dev/null +++ b/src/test/ResourceManagerTest.cpp @@ -0,0 +1,52 @@ +#include <gtest/gtest.h> + +#include <crepe/util/Log.h> +#include <crepe/api/Config.h> +#include <crepe/api/ResourceManager.h> + +using namespace std; +using namespace crepe; +using namespace testing; + +class ResourceManagerTest : public Test { +public: + ResourceManager & resman = ResourceManager::get_instance(); + Config & cfg = Config::get_instance(); + + void SetUp() override { + resman.clear(); + } +}; + +TEST_F(ResourceManagerTest, Main) { + // NOTE: there is no way (that I know of) to ensure the last cache call + // allocates the new Sound instance in a different location than the first, + // so this test should be verified manually using these print statements and + // debug trace messages. + cfg.log.level = Log::Level::TRACE; + + Asset path1 = "mwe/audio/sfx1.wav"; + Asset path2 = "mwe/audio/sfx1.wav"; + ASSERT_EQ(path1, path2); + + Sound * ptr1 = nullptr; + Sound * ptr2 = nullptr; + { + Log::logf(Log::Level::DEBUG, "Get first sound (constructor call)"); + Sound & sound = resman.cache<Sound>(path1); + ptr1 = &sound; + } + { + Log::logf(Log::Level::DEBUG, "Get same sound (NO constructor call)"); + Sound & sound = resman.cache<Sound>(path2); + ptr2 = &sound; + } + EXPECT_EQ(ptr1, ptr2); + + 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<Sound>(path1); +} + 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(); } + |