From e0ea870fdfcfbe9e3f0e47215bb809d4353d88e2 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 1 Oct 2024 21:22:30 +0200 Subject: removed submodule and updating resource_manager --- src/crepe/CMakeLists.txt | 5 +- src/crepe/Sound.cpp | 60 ---------------------- src/crepe/Sound.h | 82 ------------------------------ src/crepe/SoundContext.cpp | 20 -------- src/crepe/SoundContext.h | 26 ---------- src/crepe/api/AudioSource.cpp | 2 +- src/crepe/api/Audio_asset.cpp | 16 ------ src/crepe/api/Audio_asset.h | 18 ------- src/crepe/api/CMakeLists.txt | 21 ++++---- src/crepe/api/Image_asset.h | 2 +- src/crepe/api/map_asset.h | 2 +- src/crepe/api/resource.h | 23 --------- src/crepe/api/resource_manager.cpp | 4 +- src/crepe/api/resource_manager.h | 2 +- src/crepe/api/spritesheet.h | 2 +- src/crepe/fabricator/resource_fabricator.h | 2 +- src/crepe/facade/CMakeLists.txt | 13 +++++ src/crepe/facade/SdlContext.cpp | 57 +++++++++++++++++++++ src/crepe/facade/SdlContext.h | 33 ++++++++++++ src/crepe/facade/Sound.cpp | 60 ++++++++++++++++++++++ src/crepe/facade/Sound.h | 82 ++++++++++++++++++++++++++++++ src/crepe/facade/SoundContext.cpp | 20 ++++++++ src/crepe/facade/SoundContext.h | 26 ++++++++++ src/crepe/facade/Texture.cpp | 31 +++++++++++ src/crepe/facade/Texture.h | 25 +++++++++ src/crepe/facade/touch | 0 26 files changed, 366 insertions(+), 268 deletions(-) delete mode 100644 src/crepe/Sound.cpp delete mode 100644 src/crepe/Sound.h delete mode 100644 src/crepe/SoundContext.cpp delete mode 100644 src/crepe/SoundContext.h delete mode 100644 src/crepe/api/Audio_asset.cpp delete mode 100644 src/crepe/api/Audio_asset.h delete mode 100644 src/crepe/api/resource.h create mode 100644 src/crepe/facade/CMakeLists.txt create mode 100644 src/crepe/facade/SdlContext.cpp create mode 100644 src/crepe/facade/SdlContext.h create mode 100644 src/crepe/facade/Sound.cpp create mode 100644 src/crepe/facade/Sound.h create mode 100644 src/crepe/facade/SoundContext.cpp create mode 100644 src/crepe/facade/SoundContext.h create mode 100644 src/crepe/facade/Texture.cpp create mode 100644 src/crepe/facade/Texture.h create mode 100644 src/crepe/facade/touch (limited to 'src/crepe') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index ed6ebe5..69e67ac 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,14 +1,11 @@ target_sources(crepe PUBLIC - Sound.cpp - SoundContext.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - Sound.h - SoundContext.h ) add_subdirectory(api) add_subdirectory(util) add_subdirectory(fabricator) +add_subdirectory(facade) diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp deleted file mode 100644 index e1150ac..0000000 --- a/src/crepe/Sound.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "util/log.h" - -#include "Sound.h" -#include "SoundContext.h" - -using namespace crepe; - -Sound::Sound(std::unique_ptr res) { - dbg_trace(); - this->load(std::move(res)); -} - -Sound::Sound(const char * src) { - dbg_trace(); - this->load(std::make_unique(src)); -} - -void Sound::load(std::unique_ptr res) { - this->sample.load(res->canonical()); -} - -void Sound::play() { - SoundContext & ctx = SoundContext::get_instance(); - 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 = SoundContext::get_instance(); - if (ctx.engine.getPause(this->handle)) return; - ctx.engine.setPause(this->handle, true); -} - -void Sound::rewind() { - SoundContext & ctx = SoundContext::get_instance(); - if (!ctx.engine.isValidVoiceHandle(this->handle)) return; - ctx.engine.seek(this->handle, 0); -} - -void Sound::set_volume(float volume) { - this->volume = volume; - - SoundContext & ctx = SoundContext::get_instance(); - if (!ctx.engine.isValidVoiceHandle(this->handle)) return; - ctx.engine.setVolume(this->handle, this->volume); -} - -void Sound::set_looping(bool looping) { - this->looping = looping; - - SoundContext & ctx = SoundContext::get_instance(); - if (!ctx.engine.isValidVoiceHandle(this->handle)) return; - ctx.engine.setLooping(this->handle, this->looping); -} diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h deleted file mode 100644 index ac93991..0000000 --- a/src/crepe/Sound.h +++ /dev/null @@ -1,82 +0,0 @@ -#pragma once - -#include -#include - -#include - -#include "api/Resource.h" - -namespace crepe { - -class Sound { -public: - /** - * \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(); - /** - * \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(); - /** - * \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(); - /** - * \brief Set playback volume / gain - * - * \param volume Volume (0 = muted, 1 = full volume) - */ - void set_volume(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(bool looping); - /** - * \brief Get looping behavior - * - * \return true if looping, false if one-shot - */ - bool get_looping() const { return this->looping; } - -public: - Sound(const char * src); - Sound(std::unique_ptr res); - -private: - void load(std::unique_ptr res); - -private: - SoLoud::Wav sample; - SoLoud::handle handle; - - float volume = 1.0f; - bool looping = false; -}; - -} // namespace crepe diff --git a/src/crepe/SoundContext.cpp b/src/crepe/SoundContext.cpp deleted file mode 100644 index 72047d2..0000000 --- a/src/crepe/SoundContext.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "util/log.h" - -#include "SoundContext.h" - -using namespace crepe; - -SoundContext & SoundContext::get_instance() { - static SoundContext instance; - return instance; -} - -SoundContext::SoundContext() { - dbg_trace(); - engine.init(); -} - -SoundContext::~SoundContext() { - dbg_trace(); - engine.deinit(); -} diff --git a/src/crepe/SoundContext.h b/src/crepe/SoundContext.h deleted file mode 100644 index 090966d..0000000 --- a/src/crepe/SoundContext.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include - -#include "Sound.h" - -namespace crepe { - -class SoundContext { -private: - SoundContext(); - virtual ~SoundContext(); - - // singleton - static SoundContext & get_instance(); - SoundContext(const SoundContext &) = delete; - SoundContext(SoundContext &&) = delete; - SoundContext & operator=(const SoundContext &) = delete; - SoundContext & operator=(SoundContext &&) = delete; - -private: - SoLoud::Soloud engine; - friend class Sound; -}; - -} // namespace crepe diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 4d1b093..f0d708a 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -1,6 +1,6 @@ #include "AudioSource.h" -#include "../Sound.h" +#include "facade/Sound.h" #include using namespace crepe::api; diff --git a/src/crepe/api/Audio_asset.cpp b/src/crepe/api/Audio_asset.cpp deleted file mode 100644 index a9b04ed..0000000 --- a/src/crepe/api/Audio_asset.cpp +++ /dev/null @@ -1,16 +0,0 @@ - - - - -#include "Audio_asset.h" -#include - - -using namespace crepe::api; - -Audio::Audio(const std::string& content){ - this->m_content = content; -} - -Audio::~Audio(){ -} diff --git a/src/crepe/api/Audio_asset.h b/src/crepe/api/Audio_asset.h deleted file mode 100644 index 0b8e48e..0000000 --- a/src/crepe/api/Audio_asset.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - - -#include "resource.h" -#include - - -namespace crepe::api { - - -class Audio : public Resource { - -public: - Audio(const std::string&); - ~Audio(); - -}; -} diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 4f3fada..2cf0bcc 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,18 +1,17 @@ target_sources(crepe PUBLIC - Image_asset.cpp - map_asset.cpp - Audio_asset.cpp - spritesheet.cpp - resource_manager.cpp + #Image_asset.cpp + #map_asset.cpp + #spritesheet.cpp + #resource_manager.cpp + Resource.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - resource.h - Image_asset.h - map_asset.h - Audio_asset.h - spritesheet.h - resource_manager.h + Resource.h + #Image_asset.h + #map_asset.h + #spritesheet.h + #resource_manager.h Component.h AudioSource.h ) diff --git a/src/crepe/api/Image_asset.h b/src/crepe/api/Image_asset.h index 69549af..0a36b0b 100644 --- a/src/crepe/api/Image_asset.h +++ b/src/crepe/api/Image_asset.h @@ -2,7 +2,7 @@ -#include "resource.h" +#include "Resource.h" #include namespace crepe::api { diff --git a/src/crepe/api/map_asset.h b/src/crepe/api/map_asset.h index a3b994f..a4f3df7 100644 --- a/src/crepe/api/map_asset.h +++ b/src/crepe/api/map_asset.h @@ -1,6 +1,6 @@ #pragma once -#include "resource.h" +#include "Resource.h" #include diff --git a/src/crepe/api/resource.h b/src/crepe/api/resource.h deleted file mode 100644 index e6456f9..0000000 --- a/src/crepe/api/resource.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - - -#include - -namespace crepe::api { - -class Resource{ - -public: - - virtual ~Resource() =default; - - const std::string& getContent() const{ - return this->m_content; - } - -protected: - std::string m_content; -}; - - -} diff --git a/src/crepe/api/resource_manager.cpp b/src/crepe/api/resource_manager.cpp index a5644ee..0ecdae5 100644 --- a/src/crepe/api/resource_manager.cpp +++ b/src/crepe/api/resource_manager.cpp @@ -6,9 +6,9 @@ using namespace crepe::api; -ResourceManager* ResourceManager::get_instance(){ +ResourceManager& ResourceManager::get_instance(){ static ResourceManager instance; - return &instance; + return instance; } diff --git a/src/crepe/api/resource_manager.h b/src/crepe/api/resource_manager.h index 1b91524..5b0e0e1 100644 --- a/src/crepe/api/resource_manager.h +++ b/src/crepe/api/resource_manager.h @@ -8,7 +8,7 @@ #include -#include "api/resource.h" +#include "Resource.h" #include "fabricator/resource_fabricator.h" diff --git a/src/crepe/api/spritesheet.h b/src/crepe/api/spritesheet.h index 7f49156..e7530c2 100644 --- a/src/crepe/api/spritesheet.h +++ b/src/crepe/api/spritesheet.h @@ -3,7 +3,7 @@ -#include "resource.h" +#include "Resource.h" #include diff --git a/src/crepe/fabricator/resource_fabricator.h b/src/crepe/fabricator/resource_fabricator.h index 9299ed3..2b0030d 100644 --- a/src/crepe/fabricator/resource_fabricator.h +++ b/src/crepe/fabricator/resource_fabricator.h @@ -2,7 +2,7 @@ -#include "api/resource.h" +#include "api/Resource.h" #include #include diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt new file mode 100644 index 0000000..1263683 --- /dev/null +++ b/src/crepe/facade/CMakeLists.txt @@ -0,0 +1,13 @@ +target_sources(crepe PUBLIC + Sound.cpp + SoundContext.cpp + Texture.cpp + SdlContext.cpp +) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES + Sound.h + SoundContext.h + Texture.h + SdlContext.h +) diff --git a/src/crepe/facade/SdlContext.cpp b/src/crepe/facade/SdlContext.cpp new file mode 100644 index 0000000..fc68b40 --- /dev/null +++ b/src/crepe/facade/SdlContext.cpp @@ -0,0 +1,57 @@ + + +#include "SdlContext.h" +#include +#include +#include +#include +#include + +using namespace crepe; + + +SdlContext& SdlContext::get_instance(){ + static SdlContext instance; + return instance; +} + + +SdlContext::SdlContext(){ + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl; + return; + } + + m_game_window = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN); + if (!m_game_window) { + std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl; + } + + m_game_renderer = SDL_CreateRenderer(m_game_window, -1, SDL_RENDERER_ACCELERATED); + if (!m_game_renderer) { + std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl; + SDL_DestroyWindow(m_game_window); + return; + } + + IMG_Init(IMG_INIT_PNG); +} + +SdlContext::~SdlContext(){ + if(m_game_renderer) + SDL_DestroyRenderer(m_game_renderer); + + if (m_game_window) { + SDL_DestroyWindow(m_game_window); + } + IMG_Quit(); +} + + +SDL_Texture* SdlContext::setTextureFromPath(const char* path){ + SDL_Surface* tmp = IMG_Load(path); + SDL_Texture* CreatedTexture = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + SDL_FreeSurface(tmp); + + return CreatedTexture; +} diff --git a/src/crepe/facade/SdlContext.h b/src/crepe/facade/SdlContext.h new file mode 100644 index 0000000..c275300 --- /dev/null +++ b/src/crepe/facade/SdlContext.h @@ -0,0 +1,33 @@ +#pragma once + +#include "Texture.h" +#include +#include +#include + +namespace crepe { + +class SdlContext { + +private: + SdlContext(); + virtual ~SdlContext(); + + // singleton + static SdlContext & get_instance(); + SdlContext(const SdlContext &) = delete; + SdlContext(SdlContext &&) = delete; + SdlContext & operator=(const SdlContext &) = delete; + SdlContext & operator=(SdlContext &&) = delete; + + SDL_Texture* setTextureFromPath(const char*); + +private: + friend class Texture; + + SDL_Window* m_game_window; + SDL_Renderer* m_game_renderer; +}; + +} // + diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp new file mode 100644 index 0000000..e1150ac --- /dev/null +++ b/src/crepe/facade/Sound.cpp @@ -0,0 +1,60 @@ +#include "util/log.h" + +#include "Sound.h" +#include "SoundContext.h" + +using namespace crepe; + +Sound::Sound(std::unique_ptr res) { + dbg_trace(); + this->load(std::move(res)); +} + +Sound::Sound(const char * src) { + dbg_trace(); + this->load(std::make_unique(src)); +} + +void Sound::load(std::unique_ptr res) { + this->sample.load(res->canonical()); +} + +void Sound::play() { + SoundContext & ctx = SoundContext::get_instance(); + 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 = SoundContext::get_instance(); + if (ctx.engine.getPause(this->handle)) return; + ctx.engine.setPause(this->handle, true); +} + +void Sound::rewind() { + SoundContext & ctx = SoundContext::get_instance(); + if (!ctx.engine.isValidVoiceHandle(this->handle)) return; + ctx.engine.seek(this->handle, 0); +} + +void Sound::set_volume(float volume) { + this->volume = volume; + + SoundContext & ctx = SoundContext::get_instance(); + if (!ctx.engine.isValidVoiceHandle(this->handle)) return; + ctx.engine.setVolume(this->handle, this->volume); +} + +void Sound::set_looping(bool looping) { + this->looping = looping; + + SoundContext & ctx = SoundContext::get_instance(); + 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 new file mode 100644 index 0000000..ac93991 --- /dev/null +++ b/src/crepe/facade/Sound.h @@ -0,0 +1,82 @@ +#pragma once + +#include +#include + +#include + +#include "api/Resource.h" + +namespace crepe { + +class Sound { +public: + /** + * \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(); + /** + * \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(); + /** + * \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(); + /** + * \brief Set playback volume / gain + * + * \param volume Volume (0 = muted, 1 = full volume) + */ + void set_volume(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(bool looping); + /** + * \brief Get looping behavior + * + * \return true if looping, false if one-shot + */ + bool get_looping() const { return this->looping; } + +public: + Sound(const char * src); + Sound(std::unique_ptr res); + +private: + void load(std::unique_ptr res); + +private: + SoLoud::Wav sample; + SoLoud::handle handle; + + float volume = 1.0f; + bool looping = false; +}; + +} // namespace crepe diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp new file mode 100644 index 0000000..72047d2 --- /dev/null +++ b/src/crepe/facade/SoundContext.cpp @@ -0,0 +1,20 @@ +#include "util/log.h" + +#include "SoundContext.h" + +using namespace crepe; + +SoundContext & SoundContext::get_instance() { + static SoundContext instance; + return instance; +} + +SoundContext::SoundContext() { + dbg_trace(); + engine.init(); +} + +SoundContext::~SoundContext() { + dbg_trace(); + engine.deinit(); +} diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h new file mode 100644 index 0000000..090966d --- /dev/null +++ b/src/crepe/facade/SoundContext.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include "Sound.h" + +namespace crepe { + +class SoundContext { +private: + SoundContext(); + virtual ~SoundContext(); + + // singleton + static SoundContext & get_instance(); + SoundContext(const SoundContext &) = delete; + SoundContext(SoundContext &&) = delete; + SoundContext & operator=(const SoundContext &) = delete; + SoundContext & operator=(SoundContext &&) = delete; + +private: + SoLoud::Soloud engine; + friend class Sound; +}; + +} // namespace crepe diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp new file mode 100644 index 0000000..c24312a --- /dev/null +++ b/src/crepe/facade/Texture.cpp @@ -0,0 +1,31 @@ + + +#include "util/log.h" + +#include "Texture.h" +#include "SdlContext.h" +#include + +using namespace crepe; + +Texture::Texture(std::unique_ptr res) { + dbg_trace(); + this->load(std::move(res)); +} + +Texture::Texture(const char * src) { + dbg_trace(); + this->load(std::make_unique(src)); +} + +Texture::~Texture(){ + dbg_trace(); + if(this->m_texture){ + SDL_DestroyTexture(m_texture); + } +} +void Texture::load(std::unique_ptr res) { + SdlContext& ctx = SdlContext::get_instance(); + m_texture = ctx.setTextureFromPath(res->canonical()); +} + diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h new file mode 100644 index 0000000..3677f6e --- /dev/null +++ b/src/crepe/facade/Texture.h @@ -0,0 +1,25 @@ +#pragma once + +#include "api/Resource.h" +#include +#include + + +namespace crepe { + +class Texture { + +public: + Texture(const char * src); + Texture(std::unique_ptr res); + ~Texture(); + +private: + void load(std::unique_ptr res); + +private: + SDL_Texture* m_texture; +}; + +} // namespace crepe + diff --git a/src/crepe/facade/touch b/src/crepe/facade/touch new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3