From f3eeedc91a04ca0651e0fe78a2119e7e3e38e391 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 25 Sep 2024 17:36:31 +0200 Subject: WIP Audio API + facade --- src/crepe/api/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/crepe/api/CMakeLists.txt (limited to 'src/crepe/api/CMakeLists.txt') diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt new file mode 100644 index 0000000..94617a4 --- /dev/null +++ b/src/crepe/api/CMakeLists.txt @@ -0,0 +1,4 @@ +target_sources(crepe PUBLIC + AudioSource.cpp +) + -- cgit v1.2.3 From 3cb7227c3c9678141ff74915331b706265c380cb Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 28 Sep 2024 17:07:56 +0200 Subject: more WIP audio facade --- src/CMakeLists.txt | 15 +++++++++--- src/crepe/CMakeLists.txt | 6 +++++ src/crepe/Sound.cpp | 19 ++++++++++++++-- src/crepe/Sound.h | 54 ++++++++++++++++++++++++++++++++++++++------ src/crepe/SoundSystem.cpp | 25 ++++++++++++++++++++ src/crepe/SoundSystem.h | 27 ++++++++++++++++++++++ src/crepe/api/CMakeLists.txt | 9 +++++++- src/crepe/api/Resource.cpp | 12 ++++++++++ src/crepe/api/Resource.h | 11 ++++++--- src/dummy_audio.cpp | 37 ++++++++++++++++++++++++++++++ test/CMakeLists.txt | 2 +- 11 files changed, 200 insertions(+), 17 deletions(-) create mode 100644 src/crepe/SoundSystem.cpp create mode 100644 src/crepe/SoundSystem.h create mode 100644 src/crepe/api/Resource.cpp create mode 100644 src/dummy_audio.cpp (limited to 'src/crepe/api/CMakeLists.txt') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cc71435..232d330 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,13 +15,22 @@ project(crepe C CXX) add_library(crepe SHARED) target_include_directories(crepe - SYSTEM INTERFACE . + PUBLIC SYSTEM INTERFACE . ) -# NOTE: all libraries *must* be linked as PRIVATE +# TODO: libraries should be linked as PRIVATE target_link_libraries(crepe - PRIVATE soloud + PUBLIC soloud ) add_subdirectory(crepe) +install( + TARGETS crepe + FILE_SET HEADERS DESTINATION include/crepe +) + + +add_executable(dummy_audio dummy_audio.cpp) +target_link_libraries(dummy_audio PUBLIC crepe) + diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index aa64262..13d9be5 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,5 +1,11 @@ target_sources(crepe PUBLIC Sound.cpp + SoundSystem.cpp +) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES + Sound.h + SoundSystem.h ) add_subdirectory(api) diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index f45a697..d48393c 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -1,9 +1,24 @@ #include "Sound.h" +#include "SoundSystem.h" using namespace crepe; -Sound::Sound(std::unique_ptr res) { - _res = std::move(res); +Sound::Sound(std::unique_ptr res, SoundSystem & system) : system(system) { + this->res = std::move(res); } +void Sound::play() { + if (this->system.engine.getPause(this->handle)) { + // resume if paused + this->system.engine.setPause(this->handle, false); + } else { + // or start new sound + this->handle = this->system.engine.play(this->sample); + } +} + +void Sound::pause() { + if (this->system.engine.getPause(this->handle)) return; + this->system.engine.setPause(this->handle, true); +} diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h index ac4b7f4..71fe390 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/Sound.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include @@ -8,22 +9,61 @@ namespace crepe { -class Sound { -public: - Sound(std::unique_ptr res); - virtual ~Sound() = default; +class SoundSystem; +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 Set looping behavior for this sample + * + * \param looping Looping behavior (false = one-shot, true = loop) + */ void set_looping(bool looping); private: - std::unique_ptr _res; - SoLoud::handle _handle; - bool _paused; + Sound(std::unique_ptr res, SoundSystem & system); + SoundSystem & system; + friend class SoundSystem; + +private: + std::unique_ptr res; + SoLoud::Wav sample; + SoLoud::handle handle; + bool paused; }; } diff --git a/src/crepe/SoundSystem.cpp b/src/crepe/SoundSystem.cpp new file mode 100644 index 0000000..30b0157 --- /dev/null +++ b/src/crepe/SoundSystem.cpp @@ -0,0 +1,25 @@ +#include "SoundSystem.h" +#include + +using namespace crepe; + +SoundSystem SoundSystem::instance { }; + +std::unique_ptr SoundSystem::sound(const std::string & src) { + auto res = std::make_unique(src); + return SoundSystem::sound(std::move(res)); +} + +std::unique_ptr SoundSystem::sound(std::unique_ptr res) { + Sound * out = new Sound(std::move(res), SoundSystem::instance); + return std::unique_ptr(out); +} + +SoundSystem::SoundSystem() { + engine.init(); +} + +SoundSystem::~SoundSystem() { + engine.deinit(); +} + diff --git a/src/crepe/SoundSystem.h b/src/crepe/SoundSystem.h new file mode 100644 index 0000000..23bb00a --- /dev/null +++ b/src/crepe/SoundSystem.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include + +#include "Sound.h" + +namespace crepe { + +class SoundSystem { +public: + static std::unique_ptr sound(const std::string & res); + static std::unique_ptr sound(std::unique_ptr res); + +private: + SoundSystem(); + virtual ~SoundSystem(); + static SoundSystem instance; + +private: + SoLoud::Soloud engine; + friend class Sound; +}; + +} + diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 94617a4..feb03ef 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,4 +1,11 @@ target_sources(crepe PUBLIC - AudioSource.cpp + # AudioSource.cpp + Resource.cpp +) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES + AudioSource.h + Component.h + Resource.h ) diff --git a/src/crepe/api/Resource.cpp b/src/crepe/api/Resource.cpp new file mode 100644 index 0000000..a38900b --- /dev/null +++ b/src/crepe/api/Resource.cpp @@ -0,0 +1,12 @@ +#include "Resource.h" + +using namespace crepe::api; + +Resource::Resource(const std::string & src) : src(src) { + this->file = std::ifstream(src, std::ios::in | std::ios::binary); +} + +const std::istream & Resource::read() { + return this->file; +} + diff --git a/src/crepe/api/Resource.h b/src/crepe/api/Resource.h index 620a10e..2260b1a 100644 --- a/src/crepe/api/Resource.h +++ b/src/crepe/api/Resource.h @@ -1,16 +1,21 @@ #pragma once #include +#include +#include namespace crepe::api { class Resource { public: - Resource(const std::string & source); - virtual ~Resource(); + Resource(const std::string & src); + +public: + const std::istream & read(); private: - std::string _source; + std::string src; + std::ifstream file; }; } diff --git a/src/dummy_audio.cpp b/src/dummy_audio.cpp new file mode 100644 index 0000000..5e0000e --- /dev/null +++ b/src/dummy_audio.cpp @@ -0,0 +1,37 @@ +#include "crepe/SoundSystem.h" + +#include +#include + +using namespace crepe; +using namespace std; +using namespace std::chrono_literals; + +int main() { + auto bgm = SoundSystem::sound("../mwe/audio/bgm.ogg"); + auto sfx1 = SoundSystem::sound("../mwe/audio/sfx1.wav"); + auto sfx2 = SoundSystem::sound("../mwe/audio/sfx2.wav"); + auto sfx3 = SoundSystem::sound("../mwe/audio/sfx3.wav"); + + bgm->play(); + + // play each sample sequentially + this_thread::sleep_for(500ms); + sfx1->play(); + this_thread::sleep_for(500ms); + sfx2->play(); + bgm->pause(); + 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); + + return 0; +} + diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d103b9a..f015570 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -13,7 +13,7 @@ add_subdirectory(../src crepe) add_executable(test dummy.cpp - audio.cpp + # audio.cpp ) target_link_libraries(test -- cgit v1.2.3 From ef44da3f5e9ca533782da5e185e69e28c295d226 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 1 Oct 2024 19:53:56 +0200 Subject: Added resources to crepe --- asset/texture/img.png | Bin 0 -> 92742 bytes src/CMakeLists.txt | 21 +++++++++- src/build.sh | 7 ++++ src/crepe/CMakeLists.txt | 9 +++- src/crepe/api/Audio_asset.cpp | 16 ++++++++ src/crepe/api/Audio_asset.h | 18 ++++++++ src/crepe/api/CMakeLists.txt | 17 ++++++++ src/crepe/api/Image_asset.cpp | 14 +++++++ src/crepe/api/Image_asset.h | 18 ++++++++ src/crepe/api/map_asset.cpp | 12 ++++++ src/crepe/api/map_asset.h | 14 +++++++ src/crepe/api/resource.h | 23 +++++++++++ src/crepe/api/resource_manager.cpp | 25 ++++++++++++ src/crepe/api/resource_manager.h | 59 +++++++++++++++++++++++++++ src/crepe/api/spritesheet.cpp | 16 ++++++++ src/crepe/api/spritesheet.h | 21 ++++++++++ src/crepe/fabricator/CMakeLists.txt | 8 ++++ src/crepe/fabricator/resource_fabricator.cpp | 26 ++++++++++++ src/crepe/fabricator/resource_fabricator.h | 29 +++++++++++++ src/crepe/main.cpp | 3 -- src/dummy_resource_manager.cpp | 23 +++++++++++ 21 files changed, 373 insertions(+), 6 deletions(-) create mode 100644 asset/texture/img.png create mode 100755 src/build.sh create mode 100644 src/crepe/api/Audio_asset.cpp create mode 100644 src/crepe/api/Audio_asset.h create mode 100644 src/crepe/api/CMakeLists.txt create mode 100644 src/crepe/api/Image_asset.cpp create mode 100644 src/crepe/api/Image_asset.h create mode 100644 src/crepe/api/map_asset.cpp create mode 100644 src/crepe/api/map_asset.h create mode 100644 src/crepe/api/resource.h create mode 100644 src/crepe/api/resource_manager.cpp create mode 100644 src/crepe/api/resource_manager.h create mode 100644 src/crepe/api/spritesheet.cpp create mode 100644 src/crepe/api/spritesheet.h create mode 100644 src/crepe/fabricator/CMakeLists.txt create mode 100644 src/crepe/fabricator/resource_fabricator.cpp create mode 100644 src/crepe/fabricator/resource_fabricator.h delete mode 100644 src/crepe/main.cpp create mode 100644 src/dummy_resource_manager.cpp (limited to 'src/crepe/api/CMakeLists.txt') diff --git a/asset/texture/img.png b/asset/texture/img.png new file mode 100644 index 0000000..43b1eca Binary files /dev/null and b/asset/texture/img.png differ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0090188..87330d4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,9 +8,28 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1) set(CMAKE_BUILD_TYPE Debug) add_compile_definitions(DEBUG) +#add_subdirectory(../lib/soloud soloud) + project(crepe C CXX) -add_executable(main) +add_library(crepe SHARED) + +target_include_directories(crepe + PUBLIC SYSTEM INTERFACE . +) + +# TODO: libraries should be linked as PRIVATE +target_link_libraries(crepe + #PUBLIC soloud +) add_subdirectory(crepe) +install( + TARGETS crepe + FILE_SET HEADERS DESTINATION include/crepe +) + + +add_executable(dummy_rm dummy_resource_manager.cpp) +target_link_libraries(dummy_rm PUBLIC crepe) diff --git a/src/build.sh b/src/build.sh new file mode 100755 index 0000000..e987bc1 --- /dev/null +++ b/src/build.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# creates the build dir and runs CMake with Ninja +cmake -B build -G Ninja + +# build the project +cmake --build build \ No newline at end of file diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 392b7d7..68aa072 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,3 +1,8 @@ -target_sources(main PUBLIC - main.cpp +target_sources(crepe PUBLIC ) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES +) + +add_subdirectory(api) +add_subdirectory(fabricator) diff --git a/src/crepe/api/Audio_asset.cpp b/src/crepe/api/Audio_asset.cpp new file mode 100644 index 0000000..a9b04ed --- /dev/null +++ b/src/crepe/api/Audio_asset.cpp @@ -0,0 +1,16 @@ + + + + +#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 new file mode 100644 index 0000000..0b8e48e --- /dev/null +++ b/src/crepe/api/Audio_asset.h @@ -0,0 +1,18 @@ +#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 new file mode 100644 index 0000000..7b16fb1 --- /dev/null +++ b/src/crepe/api/CMakeLists.txt @@ -0,0 +1,17 @@ +target_sources(crepe PUBLIC + Image_asset.cpp + map_asset.cpp + Audio_asset.cpp + spritesheet.cpp + resource_manager.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 +) + diff --git a/src/crepe/api/Image_asset.cpp b/src/crepe/api/Image_asset.cpp new file mode 100644 index 0000000..57431c4 --- /dev/null +++ b/src/crepe/api/Image_asset.cpp @@ -0,0 +1,14 @@ + + +#include "Image_asset.h" +#include + +using namespace crepe::api; + +Texture::Texture(const std::string& content){ + this->m_content = content; +} + + +Texture::~Texture(){ +} diff --git a/src/crepe/api/Image_asset.h b/src/crepe/api/Image_asset.h new file mode 100644 index 0000000..69549af --- /dev/null +++ b/src/crepe/api/Image_asset.h @@ -0,0 +1,18 @@ +#pragma once + + + +#include "resource.h" +#include + +namespace crepe::api { + + +class Texture : public Resource { + +public: + Texture(const std::string&); + ~Texture(); +}; + +} diff --git a/src/crepe/api/map_asset.cpp b/src/crepe/api/map_asset.cpp new file mode 100644 index 0000000..bbabe2b --- /dev/null +++ b/src/crepe/api/map_asset.cpp @@ -0,0 +1,12 @@ + + + + +#include "map_asset.h" + +Map::Map(const std::string& content){ + this->m_content = content; +} + +Map::~Map(){ +} diff --git a/src/crepe/api/map_asset.h b/src/crepe/api/map_asset.h new file mode 100644 index 0000000..a3b994f --- /dev/null +++ b/src/crepe/api/map_asset.h @@ -0,0 +1,14 @@ +#pragma once + +#include "resource.h" +#include + + +using namespace crepe::api; + +class Map : public Resource { + +public: + Map(const std::string& ); + ~Map(); +}; diff --git a/src/crepe/api/resource.h b/src/crepe/api/resource.h new file mode 100644 index 0000000..e6456f9 --- /dev/null +++ b/src/crepe/api/resource.h @@ -0,0 +1,23 @@ +#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 new file mode 100644 index 0000000..a5644ee --- /dev/null +++ b/src/crepe/api/resource_manager.cpp @@ -0,0 +1,25 @@ + + +#include "resource_manager.h" +#include +#include + +using namespace crepe::api; + +ResourceManager* ResourceManager::get_instance(){ + static ResourceManager instance; + return &instance; +} + + +ResourceManager::~ResourceManager(){ + m_resources.clear(); +} + + +void ResourceManager::Unload(const std::string& file_path){ + if(m_resources.find(file_path) != m_resources.end()){ + m_resources.erase(file_path); + } +} + diff --git a/src/crepe/api/resource_manager.h b/src/crepe/api/resource_manager.h new file mode 100644 index 0000000..1b91524 --- /dev/null +++ b/src/crepe/api/resource_manager.h @@ -0,0 +1,59 @@ +#pragma once + + + +#include +#include +#include +#include + + +#include "api/resource.h" +#include "fabricator/resource_fabricator.h" + + + namespace crepe::api{ + +class ResourceManager{ + + +private: + + std::unordered_map< std::string, std::unique_ptr> m_resources; + + +protected: + ResourceManager() = default; + ~ResourceManager(); + +public: + ResourceManager(const ResourceManager &) = delete; + ResourceManager(ResourceManager &&) = delete; + ResourceManager &operator=(const ResourceManager &) = delete; + ResourceManager &operator=(ResourceManager &&) = delete; + + static ResourceManager& get_instance(); + + + +public: + template + T* Load(const std::string& file_path){ + + if (m_resources.find(file_path) != m_resources.end()) { + return static_cast(m_resources[file_path].get()); + } + + std::unique_ptr resource = ResourceFactory::create_resource(file_path); + if (resource) { + m_resources[file_path] = std::move(resource); + return static_cast(m_resources[file_path].get() ); + } + + return nullptr; + } + + void Unload(const std::string& file_path); + +}; +} diff --git a/src/crepe/api/spritesheet.cpp b/src/crepe/api/spritesheet.cpp new file mode 100644 index 0000000..f42a782 --- /dev/null +++ b/src/crepe/api/spritesheet.cpp @@ -0,0 +1,16 @@ + + +#include "spritesheet.h" + +#include + +using namespace crepe::api; + +SpriteSheet::SpriteSheet(const std::string& content){ + this->m_content = content; +} + +SpriteSheet::~SpriteSheet(){ +} + + diff --git a/src/crepe/api/spritesheet.h b/src/crepe/api/spritesheet.h new file mode 100644 index 0000000..7f49156 --- /dev/null +++ b/src/crepe/api/spritesheet.h @@ -0,0 +1,21 @@ +#pragma once + + + + +#include "resource.h" +#include + + +namespace crepe::api { + + + +class SpriteSheet : public Resource{ + +public: + SpriteSheet(const std::string&); + ~SpriteSheet(); + +}; +} diff --git a/src/crepe/fabricator/CMakeLists.txt b/src/crepe/fabricator/CMakeLists.txt new file mode 100644 index 0000000..4fd7eea --- /dev/null +++ b/src/crepe/fabricator/CMakeLists.txt @@ -0,0 +1,8 @@ +target_sources(crepe PUBLIC + resource_fabricator.cpp +) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES + resource_fabricator.h +) + diff --git a/src/crepe/fabricator/resource_fabricator.cpp b/src/crepe/fabricator/resource_fabricator.cpp new file mode 100644 index 0000000..0633a40 --- /dev/null +++ b/src/crepe/fabricator/resource_fabricator.cpp @@ -0,0 +1,26 @@ + + +#include "resource_fabricator.h" +#include +#include +#include +#include + + + + +std::string ResourceFactory::convert_file_to_string(const std::string& path){ + std::ifstream file(path, std::ios::binary | std::ios::ate); + if (!file.is_open()) { + std::cerr << "Failed to open file: " << path << std::endl; + return ""; + } + + std::ifstream::pos_type fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + + std::vector bytes(fileSize); + file.read(bytes.data(), fileSize); + + return std::string(bytes.begin(), bytes.end()); +} diff --git a/src/crepe/fabricator/resource_fabricator.h b/src/crepe/fabricator/resource_fabricator.h new file mode 100644 index 0000000..9299ed3 --- /dev/null +++ b/src/crepe/fabricator/resource_fabricator.h @@ -0,0 +1,29 @@ +#pragma once + + + +#include "api/resource.h" +#include +#include + + + + +class ResourceFactory { + +public: + + template + static std::unique_ptr create_resource(const std::string& file_path){ + + return std::make_unique(convert_file_to_string(file_path)); + } + +private: + static std::string convert_file_to_string(const std::string& path); + +}; + + + + diff --git a/src/crepe/main.cpp b/src/crepe/main.cpp deleted file mode 100644 index 8e9a184..0000000 --- a/src/crepe/main.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include - -int main() { printf("Hello World!\n"); } diff --git a/src/dummy_resource_manager.cpp b/src/dummy_resource_manager.cpp new file mode 100644 index 0000000..214c617 --- /dev/null +++ b/src/dummy_resource_manager.cpp @@ -0,0 +1,23 @@ + + + + +#include "api/Image_asset.h" +#include "api/resource_manager.h" +#include +#include + + + +using namespace crepe; + +int main(){ + + // get instance of resource manager + api::ResourceManager& c_ResMan = api::ResourceManager::get_instance(); + + // make a resouce from the file path + api::Texture* img = c_ResMan.Load("../asset/texture/img.png"); + + std::cout << img->getContent() << std::endl; +} -- cgit v1.2.3 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 --- lib/SDL_image/CMakeLists.txt | 97 ++++++++++++++++++++++++++++++ src/CMakeLists.txt | 5 +- 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 src/dummy_audio.cpp | 2 +- 29 files changed, 468 insertions(+), 270 deletions(-) create mode 100644 lib/SDL_image/CMakeLists.txt 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/api/CMakeLists.txt') diff --git a/lib/SDL_image/CMakeLists.txt b/lib/SDL_image/CMakeLists.txt new file mode 100644 index 0000000..aaccd96 --- /dev/null +++ b/lib/SDL_image/CMakeLists.txt @@ -0,0 +1,97 @@ +cmake_minimum_required(VERSION 3.28) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 20) + +add_compile_definitions(WITH_SDL2) + +add_subdirectory(../sdl2 sdl2) + +project(soloud C CXX) + +add_library(soloud SHARED + src/src/audiosource/ay/chipplayer.cpp + src/src/audiosource/ay/sndbuffer.cpp + src/src/audiosource/ay/sndchip.cpp + src/src/audiosource/ay/sndrender.cpp + src/src/audiosource/ay/soloud_ay.cpp + src/src/audiosource/monotone/soloud_monotone.cpp + src/src/audiosource/noise/soloud_noise.cpp + src/src/audiosource/openmpt/soloud_openmpt.cpp + src/src/audiosource/openmpt/soloud_openmpt_dll.c + src/src/audiosource/sfxr/soloud_sfxr.cpp + src/src/audiosource/speech/darray.cpp + src/src/audiosource/speech/klatt.cpp + src/src/audiosource/speech/resonator.cpp + src/src/audiosource/speech/soloud_speech.cpp + src/src/audiosource/speech/tts.cpp + src/src/audiosource/tedsid/sid.cpp + src/src/audiosource/tedsid/soloud_tedsid.cpp + src/src/audiosource/tedsid/ted.cpp + src/src/audiosource/vic/soloud_vic.cpp + src/src/audiosource/vizsn/soloud_vizsn.cpp + src/src/audiosource/wav/dr_impl.cpp + src/src/audiosource/wav/soloud_wav.cpp + src/src/audiosource/wav/soloud_wavstream.cpp + src/src/audiosource/wav/stb_vorbis.c + + src/src/backend/alsa/soloud_alsa.cpp + src/src/backend/coreaudio/soloud_coreaudio.cpp + src/src/backend/jack/soloud_jack.cpp + src/src/backend/miniaudio/soloud_miniaudio.cpp + src/src/backend/nosound/soloud_nosound.cpp + src/src/backend/null/soloud_null.cpp + src/src/backend/openal/soloud_openal.cpp + src/src/backend/openal/soloud_openal_dll.c + src/src/backend/opensles/soloud_opensles.cpp + src/src/backend/oss/soloud_oss.cpp + src/src/backend/portaudio/soloud_portaudio.cpp + src/src/backend/portaudio/soloud_portaudio_dll.c + src/src/backend/sdl/soloud_sdl1.cpp + src/src/backend/sdl/soloud_sdl1_dll.c + src/src/backend/sdl/soloud_sdl2.cpp + src/src/backend/sdl/soloud_sdl2_dll.c + src/src/backend/sdl2_static/soloud_sdl2_static.cpp + src/src/backend/sdl_static/soloud_sdl_static.cpp + src/src/backend/wasapi/soloud_wasapi.cpp + src/src/backend/winmm/soloud_winmm.cpp + src/src/backend/xaudio2/soloud_xaudio2.cpp + + src/src/core/soloud.cpp + src/src/core/soloud_audiosource.cpp + src/src/core/soloud_bus.cpp + src/src/core/soloud_core_3d.cpp + src/src/core/soloud_core_basicops.cpp + src/src/core/soloud_core_faderops.cpp + src/src/core/soloud_core_filterops.cpp + src/src/core/soloud_core_getters.cpp + src/src/core/soloud_core_setters.cpp + src/src/core/soloud_core_voicegroup.cpp + src/src/core/soloud_core_voiceops.cpp + src/src/core/soloud_fader.cpp + src/src/core/soloud_fft.cpp + src/src/core/soloud_fft_lut.cpp + src/src/core/soloud_file.cpp + src/src/core/soloud_filter.cpp + src/src/core/soloud_misc.cpp + src/src/core/soloud_queue.cpp + src/src/core/soloud_thread.cpp + + # src/src/filter/soloud_bassboostfilter.cpp + # src/src/filter/soloud_biquadresonantfilter.cpp + # src/src/filter/soloud_dcremovalfilter.cpp + # src/src/filter/soloud_duckfilter.cpp + # src/src/filter/soloud_echofilter.cpp + # src/src/filter/soloud_eqfilter.cpp + # src/src/filter/soloud_fftfilter.cpp + # src/src/filter/soloud_flangerfilter.cpp + # src/src/filter/soloud_freeverbfilter.cpp + # src/src/filter/soloud_lofifilter.cpp + # src/src/filter/soloud_robotizefilter.cpp + # src/src/filter/soloud_waveshaperfilter.cpp +) +target_include_directories(soloud PRIVATE src/include) +target_include_directories(soloud SYSTEM INTERFACE src/include) + +target_link_libraries(soloud PRIVATE SDL2) + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cb310af..27e5188 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,7 @@ set(CMAKE_BUILD_TYPE Debug) add_compile_definitions(DEBUG) add_subdirectory(../lib/soloud soloud) +add_subdirectory(../lib/SDL_image SDL_image) project(crepe C CXX) @@ -21,6 +22,8 @@ target_include_directories(crepe # TODO: libraries should be linked as PRIVATE target_link_libraries(crepe PUBLIC soloud + PUBLIC SDL2 + PUBLIC SDL2_image ) add_subdirectory(crepe) @@ -30,5 +33,5 @@ install( FILE_SET HEADERS DESTINATION include/crepe ) -add_executable(dummy_rm dummy_resource_manager.cpp) +add_executable(dummy_rm dummy_audio.cpp) target_link_libraries(dummy_rm PUBLIC 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 diff --git a/src/dummy_audio.cpp b/src/dummy_audio.cpp index 049bb49..211a503 100644 --- a/src/dummy_audio.cpp +++ b/src/dummy_audio.cpp @@ -1,4 +1,4 @@ -#include "crepe/Sound.h" +#include "crepe/facade/Sound.h" #include "crepe/util/log.h" #include -- cgit v1.2.3 From de2c2593f9f272c5151d74af4ff846fdd70a9bc7 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 2 Oct 2024 15:57:59 +0200 Subject: working resource manager and textures and sprite to new standard --- .gitmodules | 4 ++ asset/spritesheet/spritesheet_test.png | Bin 0 -> 616 bytes lib/sdl_image/CMakeLists.txt | 34 ++++++++++++++- lib/sdl_image/src | 1 + src/CMakeLists.txt | 6 +-- src/crepe/api/CMakeLists.txt | 13 +++--- src/crepe/api/Image_asset.cpp | 14 ------- src/crepe/api/Image_asset.h | 18 -------- src/crepe/api/Resource.cpp | 6 ++- src/crepe/api/baseResource.h | 11 +++++ src/crepe/api/game.cpp | 18 ++++++++ src/crepe/api/game.h | 15 +++++++ src/crepe/api/resource_manager.h | 12 +++--- src/crepe/api/spritesheet.cpp | 36 ++++++++++++++-- src/crepe/api/spritesheet.h | 27 +++++++++--- src/crepe/facade/SdlContext.cpp | 73 ++++++++++++++++++++++++++++++++- src/crepe/facade/SdlContext.h | 15 +++++-- src/crepe/facade/Sound.h | 3 +- src/crepe/facade/Texture.cpp | 4 ++ src/crepe/facade/Texture.h | 8 +++- src/dummy_resource_manager.cpp | 31 ++++++++++---- 21 files changed, 273 insertions(+), 76 deletions(-) create mode 100644 asset/spritesheet/spritesheet_test.png create mode 160000 lib/sdl_image/src delete mode 100644 src/crepe/api/Image_asset.cpp delete mode 100644 src/crepe/api/Image_asset.h create mode 100644 src/crepe/api/baseResource.h create mode 100644 src/crepe/api/game.cpp create mode 100644 src/crepe/api/game.h (limited to 'src/crepe/api/CMakeLists.txt') diff --git a/.gitmodules b/.gitmodules index bb860c0..d7a6ebf 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,7 @@ path = lib/sdl2 url = https://github.com/libsdl-org/SDL shallow = true +[submodule "lib/sdl_image/src"] + path = lib/sdl_image/src + url = https://github.com/libsdl-org/SDL_image + branch = SDL2 diff --git a/asset/spritesheet/spritesheet_test.png b/asset/spritesheet/spritesheet_test.png new file mode 100644 index 0000000..d68a72a Binary files /dev/null and b/asset/spritesheet/spritesheet_test.png differ diff --git a/lib/sdl_image/CMakeLists.txt b/lib/sdl_image/CMakeLists.txt index 65f7598..d1bee81 100644 --- a/lib/sdl_image/CMakeLists.txt +++ b/lib/sdl_image/CMakeLists.txt @@ -11,8 +11,38 @@ include_directories(src/include/SDL3_image) project(sdl_image C CXX) add_library(sdl_image SHARED - src/src/IMG.c - src/src/IMG_png.c + src/src/IMG.c + src/src/IMG_WIC.c + src/src/IMG_avif.c + src/src/IMG_bmp.c + src/src/IMG_gif.c + src/src/IMG_jpg.c + src/src/IMG_jxl.c + src/src/IMG_lbm.c + src/src/IMG_pcx.c + src/src/IMG_png.c + src/src/IMG_pnm.c + src/src/IMG_qoi.c + src/src/IMG_stb.c + src/src/IMG_svg.c + src/src/IMG_tga.c + src/src/IMG_tif.c + src/src/IMG_webp.c + src/src/IMG_xcf.c + src/src/IMG_xpm.c + src/src/IMG_xv.c + + + + + + + + + + + + ) target_include_directories(sdl_image PRIVATE src/include/) target_include_directories(sdl_image SYSTEM INTERFACE src/include/) diff --git a/lib/sdl_image/src b/lib/sdl_image/src new file mode 160000 index 0000000..c6c7278 --- /dev/null +++ b/lib/sdl_image/src @@ -0,0 +1 @@ +Subproject commit c6c7278b86b5de1232b10de8f612ed05cf2d11f6 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 40d6644..b47be8f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,7 +9,7 @@ set(CMAKE_BUILD_TYPE Debug) add_compile_definitions(DEBUG) add_subdirectory(../lib/soloud soloud) -add_subdirectory(../lib/sdl_image sdl_image) +add_subdirectory(../lib/sdl_image/src SDL2_image) project(crepe C CXX) @@ -23,7 +23,7 @@ target_include_directories(crepe target_link_libraries(crepe PUBLIC soloud PUBLIC SDL2 - PUBLIC sdl_image + PUBLIC SDL2_image ) add_subdirectory(crepe) @@ -33,5 +33,5 @@ install( FILE_SET HEADERS DESTINATION include/crepe ) -add_executable(dummy_rm dummy_audio.cpp) +add_executable(dummy_rm dummy_resource_manager.cpp) target_link_libraries(dummy_rm PUBLIC crepe) diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 2cf0bcc..96b55cf 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,17 +1,18 @@ target_sources(crepe PUBLIC - #Image_asset.cpp #map_asset.cpp - #spritesheet.cpp - #resource_manager.cpp + spritesheet.cpp + resource_manager.cpp Resource.cpp + game.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES Resource.h - #Image_asset.h + baseResource.h + game.h #map_asset.h - #spritesheet.h - #resource_manager.h + spritesheet.h + resource_manager.h Component.h AudioSource.h ) diff --git a/src/crepe/api/Image_asset.cpp b/src/crepe/api/Image_asset.cpp deleted file mode 100644 index 57431c4..0000000 --- a/src/crepe/api/Image_asset.cpp +++ /dev/null @@ -1,14 +0,0 @@ - - -#include "Image_asset.h" -#include - -using namespace crepe::api; - -Texture::Texture(const std::string& content){ - this->m_content = content; -} - - -Texture::~Texture(){ -} diff --git a/src/crepe/api/Image_asset.h b/src/crepe/api/Image_asset.h deleted file mode 100644 index 0a36b0b..0000000 --- a/src/crepe/api/Image_asset.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - - - -#include "Resource.h" -#include - -namespace crepe::api { - - -class Texture : public Resource { - -public: - Texture(const std::string&); - ~Texture(); -}; - -} diff --git a/src/crepe/api/Resource.cpp b/src/crepe/api/Resource.cpp index 1a647ce..f7f2516 100644 --- a/src/crepe/api/Resource.cpp +++ b/src/crepe/api/Resource.cpp @@ -1,11 +1,15 @@ #include +#include +#include #include "Resource.h" +#include "util/log.h" using namespace crepe::api; Resource::Resource(const std::string & src) { - this->src = std::filesystem::canonical(src); + dbg_trace(); + this->src = std::filesystem::path(src); this->file = std::ifstream(this->src, std::ios::in | std::ios::binary); } diff --git a/src/crepe/api/baseResource.h b/src/crepe/api/baseResource.h new file mode 100644 index 0000000..2513f4d --- /dev/null +++ b/src/crepe/api/baseResource.h @@ -0,0 +1,11 @@ +#pragma once + +namespace crepe::api { + +class BaseResource { +public: + virtual ~BaseResource() = default; +}; + +} + diff --git a/src/crepe/api/game.cpp b/src/crepe/api/game.cpp new file mode 100644 index 0000000..02a0132 --- /dev/null +++ b/src/crepe/api/game.cpp @@ -0,0 +1,18 @@ + + + +#include "game.h" +#include "api/spritesheet.h" +#include "facade/SdlContext.h" +#include "facade/Texture.h" +#include + + +void game::render(std::vector & draw, std::vector & ss){ + auto& ctx = crepe::SdlContext::get_instance(); + + ctx.loop(*draw[0], *ss[0]); +} + + + diff --git a/src/crepe/api/game.h b/src/crepe/api/game.h new file mode 100644 index 0000000..7cde954 --- /dev/null +++ b/src/crepe/api/game.h @@ -0,0 +1,15 @@ +#pragma once + + +#include "api/spritesheet.h" +#include "facade/Texture.h" +#include + +class game { + +public: + game(){} + ~game(){} + + void render(std::vector&, std::vector&); +}; diff --git a/src/crepe/api/resource_manager.h b/src/crepe/api/resource_manager.h index 5b0e0e1..a646d95 100644 --- a/src/crepe/api/resource_manager.h +++ b/src/crepe/api/resource_manager.h @@ -7,19 +7,17 @@ #include #include +#include "api/baseResource.h" -#include "Resource.h" -#include "fabricator/resource_fabricator.h" - - namespace crepe::api{ +namespace crepe::api{ class ResourceManager{ private: - std::unordered_map< std::string, std::unique_ptr> m_resources; + std::unordered_map< std::string, std::unique_ptr> m_resources; protected: @@ -44,10 +42,10 @@ public: return static_cast(m_resources[file_path].get()); } - std::unique_ptr resource = ResourceFactory::create_resource(file_path); + auto resource = std::make_unique(file_path.c_str()); if (resource) { m_resources[file_path] = std::move(resource); - return static_cast(m_resources[file_path].get() ); + return static_cast(m_resources[file_path].get() ); } return nullptr; diff --git a/src/crepe/api/spritesheet.cpp b/src/crepe/api/spritesheet.cpp index f42a782..93a2b65 100644 --- a/src/crepe/api/spritesheet.cpp +++ b/src/crepe/api/spritesheet.cpp @@ -1,16 +1,44 @@ #include "spritesheet.h" +#include "SDL_rect.h" +#include "SDL_render.h" +#include "api/Resource.h" +#include "facade/SdlContext.h" +#include -#include using namespace crepe::api; -SpriteSheet::SpriteSheet(const std::string& content){ - this->m_content = content; +Spritesheet::Spritesheet(const char* src, const int row, const int col){ + this->load(std::make_unique(src), row, col); } -SpriteSheet::~SpriteSheet(){ +Spritesheet::Spritesheet(std::unique_ptr res, const int row, const int col){ + this->load(std::move(res), row, col); } +Spritesheet::~Spritesheet(){ + + if (this->m_spritesheet) { + SDL_DestroyTexture(this->m_spritesheet); + } +} + +void Spritesheet::select_sprite(const int x, const int y){ + m_clip.x = x * m_clip.w; + m_clip.y = y * m_clip.h; +} + +void Spritesheet::draw_selected_sprite(const int x, const int y){ + auto& ctx = SdlContext::get_instance(); + SDL_Rect tmp = { x, y, m_clip.w, m_clip.h}; + SDL_RenderCopy(ctx.m_game_renderer, this->m_spritesheet, &this->m_clip, &tmp); +} + +void Spritesheet::load(std::unique_ptr res, const int row, const int col){ + auto& ctx = SdlContext::get_instance(); + + this->m_spritesheet = ctx.setTextureFromPath(res->canonical(), this->m_clip, row, col); +} diff --git a/src/crepe/api/spritesheet.h b/src/crepe/api/spritesheet.h index e7530c2..7f46296 100644 --- a/src/crepe/api/spritesheet.h +++ b/src/crepe/api/spritesheet.h @@ -4,18 +4,35 @@ #include "Resource.h" -#include +#include "SDL_rect.h" +#include "SDL_render.h" +#include namespace crepe::api { +class Spritesheet{ +public: + Spritesheet(const char * src, const int row , const int col); + Spritesheet(std::unique_ptr res, const int row, const int col); + ~Spritesheet(); -class SpriteSheet : public Resource{ + void select_sprite(const int x, const int y); + void draw_selected_sprite(const int x, const int y); +private: + void load(std::unique_ptr res, const int row, const int col);; + SDL_Texture* get_texture() const; -public: - SpriteSheet(const std::string&); - ~SpriteSheet(); + +private: + + SDL_Texture* m_spritesheet; + SDL_Rect m_clip; + + friend class SdlContext; }; + } + diff --git a/src/crepe/facade/SdlContext.cpp b/src/crepe/facade/SdlContext.cpp index fc68b40..7e2d79f 100644 --- a/src/crepe/facade/SdlContext.cpp +++ b/src/crepe/facade/SdlContext.cpp @@ -1,11 +1,18 @@ #include "SdlContext.h" +#include "SDL_rect.h" +#include "api/spritesheet.h" +#include "facade/Texture.h" +#include "util/log.h" #include #include #include #include #include +#include +#include +#include using namespace crepe; @@ -33,8 +40,11 @@ SdlContext::SdlContext(){ SDL_DestroyWindow(m_game_window); return; } - - IMG_Init(IMG_INIT_PNG); + int imgFlags = IMG_INIT_PNG; + if( !( IMG_Init( imgFlags ) & imgFlags ) ) + { + std::cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl; + } } SdlContext::~SdlContext(){ @@ -45,13 +55,72 @@ SdlContext::~SdlContext(){ SDL_DestroyWindow(m_game_window); } IMG_Quit(); + SDL_Quit(); } +SDL_Texture* SdlContext::setTextureFromPath(const char* path, SDL_Rect& clip, const int row, const int col){ + dbg_trace(); + + SDL_Surface* tmp = IMG_Load(path); + if (!tmp) { + std::cerr << "Error surface " << IMG_GetError << std::endl; + } + + clip.w = tmp->w / col; + clip.h = tmp->h / row; + + SDL_Texture* CreatedTexture = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + + if (!CreatedTexture) { + std::cerr << "Error could not create texture " << IMG_GetError << std::endl; + } + SDL_FreeSurface(tmp); + + return CreatedTexture; +} SDL_Texture* SdlContext::setTextureFromPath(const char* path){ + dbg_trace(); + SDL_Surface* tmp = IMG_Load(path); + if (!tmp) { + std::cerr << "Error surface " << IMG_GetError << std::endl; + } SDL_Texture* CreatedTexture = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + + if (!CreatedTexture) { + std::cerr << "Error could not create texture " << IMG_GetError << std::endl; + } SDL_FreeSurface(tmp); return CreatedTexture; } + + +void SdlContext::loop(const Texture& texture, api::Spritesheet& ss){ + SDL_RenderClear(m_game_renderer); + bool quit = false; + SDL_Event event; + + while (!quit) { + Uint32 ticks = SDL_GetTicks(); + int sprite = (ticks / 100) % 4; + ss.select_sprite(sprite, 0); + + while (SDL_PollEvent(&event) != NULL) { + switch (event.type) { + case SDL_QUIT: + quit = true; + break; + } + + } + + SDL_RenderClear(m_game_renderer); + SDL_RenderCopy(m_game_renderer, texture.get_texture(), NULL, NULL); + ss.draw_selected_sprite(10, 10); + SDL_RenderPresent(m_game_renderer); + } +} + + diff --git a/src/crepe/facade/SdlContext.h b/src/crepe/facade/SdlContext.h index c275300..329a374 100644 --- a/src/crepe/facade/SdlContext.h +++ b/src/crepe/facade/SdlContext.h @@ -1,29 +1,38 @@ #pragma once +#include "SDL_rect.h" #include "Texture.h" +#include "api/spritesheet.h" #include #include -#include namespace crepe { +class Texture; +class Spritesheet; + class SdlContext { +public: + void loop(const Texture& , api::Spritesheet&); + + // singleton + static SdlContext & get_instance(); 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*); + SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col); private: friend class Texture; + friend class api::Spritesheet; SDL_Window* m_game_window; SDL_Renderer* m_game_renderer; diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index ac93991..06e1932 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -6,10 +6,11 @@ #include #include "api/Resource.h" +#include "api/baseResource.h" namespace crepe { -class Sound { +class Sound : public api::BaseResource{ public: /** * \brief Pause this sample diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp index c24312a..220ef2e 100644 --- a/src/crepe/facade/Texture.cpp +++ b/src/crepe/facade/Texture.cpp @@ -25,7 +25,11 @@ Texture::~Texture(){ } } void Texture::load(std::unique_ptr res) { + dbg_trace(); SdlContext& ctx = SdlContext::get_instance(); m_texture = ctx.setTextureFromPath(res->canonical()); } +SDL_Texture* Texture::get_texture() const{ + return m_texture; +} diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h index 3677f6e..a5fcca9 100644 --- a/src/crepe/facade/Texture.h +++ b/src/crepe/facade/Texture.h @@ -1,24 +1,28 @@ #pragma once +#include "api/baseResource.h" +#include "facade/SdlContext.h" #include "api/Resource.h" #include #include - namespace crepe { -class Texture { +class Texture : public api::BaseResource{ public: Texture(const char * src); Texture(std::unique_ptr res); ~Texture(); + SDL_Texture* get_texture() const; private: void load(std::unique_ptr res); private: SDL_Texture* m_texture; + + friend class SdlContext; }; } // namespace crepe diff --git a/src/dummy_resource_manager.cpp b/src/dummy_resource_manager.cpp index 214c617..bb0b7af 100644 --- a/src/dummy_resource_manager.cpp +++ b/src/dummy_resource_manager.cpp @@ -2,22 +2,37 @@ -#include "api/Image_asset.h" +#include "api/game.h" #include "api/resource_manager.h" -#include -#include - - +#include "api/spritesheet.h" +#include "facade/Texture.h" +#include "util/log.h" +#include using namespace crepe; int main(){ + + dbg_trace(); + // get instance of resource manager + //api::ResourceManager& c_ResMan = api::ResourceManager::get_instance(); + + + game engine; + api::ResourceManager& c_ResMan = api::ResourceManager::get_instance(); - // make a resouce from the file path - api::Texture* img = c_ResMan.Load("../asset/texture/img.png"); + auto test = c_ResMan.Load("../asset/texture/img.png"); + + auto img = Texture("../asset/texture/img.png"); + + auto SS = api::Spritesheet("../asset/spritesheet/spritesheet_test.png", 1 , 4); + + std::vector t = {test}; + std::vector s = {&SS}; + + engine.render(t, s); - std::cout << img->getContent() << std::endl; } -- cgit v1.2.3 From 202a9ec288ded78771e1f7e4c711a7612b201b9d Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 5 Oct 2024 14:20:45 +0200 Subject: rename Resource to Asset --- src/crepe/Sound.cpp | 6 +++--- src/crepe/Sound.h | 6 +++--- src/crepe/api/Asset.cpp | 14 ++++++++++++++ src/crepe/api/Asset.h | 24 ++++++++++++++++++++++++ src/crepe/api/AudioSource.cpp | 2 +- src/crepe/api/AudioSource.h | 6 +++--- src/crepe/api/CMakeLists.txt | 4 ++-- src/crepe/api/Resource.cpp | 14 -------------- src/crepe/api/Resource.h | 24 ------------------------ src/test/audio.cpp | 4 ++-- 10 files changed, 52 insertions(+), 52 deletions(-) create mode 100644 src/crepe/api/Asset.cpp create mode 100644 src/crepe/api/Asset.h delete mode 100644 src/crepe/api/Resource.cpp delete mode 100644 src/crepe/api/Resource.h (limited to 'src/crepe/api/CMakeLists.txt') diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index 1758282..c6e87d5 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -5,17 +5,17 @@ using namespace crepe; -Sound::Sound(std::unique_ptr res) { +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)); + this->load(std::make_unique(src)); } -void Sound::load(std::unique_ptr res) { +void Sound::load(std::unique_ptr res) { this->sample.load(res->canonical()); } diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h index ac93991..339dd7c 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/Sound.h @@ -5,7 +5,7 @@ #include -#include "api/Resource.h" +#include "api/Asset.h" namespace crepe { @@ -66,10 +66,10 @@ public: public: Sound(const char * src); - Sound(std::unique_ptr res); + Sound(std::unique_ptr res); private: - void load(std::unique_ptr res); + void load(std::unique_ptr res); private: SoLoud::Wav sample; diff --git a/src/crepe/api/Asset.cpp b/src/crepe/api/Asset.cpp new file mode 100644 index 0000000..92ee50e --- /dev/null +++ b/src/crepe/api/Asset.cpp @@ -0,0 +1,14 @@ +#include + +#include "Asset.h" + +using namespace crepe::api; + +Asset::Asset(const std::string & src) { + this->src = std::filesystem::canonical(src); + this->file = std::ifstream(this->src, std::ios::in | std::ios::binary); +} + +const std::istream & Asset::read() { return this->file; } + +const char * Asset::canonical() { return this->src.c_str(); } diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h new file mode 100644 index 0000000..259c696 --- /dev/null +++ b/src/crepe/api/Asset.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include + +namespace crepe::api { + +class Asset { +public: + Asset(const std::string & src); + +public: + //! Get an input stream to the contents of this resource + const std::istream & read(); + //! Get the canonical path to this resource + const char * canonical(); + +private: + std::string src; + std::ifstream file; +}; + +} // namespace crepe::api diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 4d1b093..656fc46 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -5,7 +5,7 @@ using namespace crepe::api; -AudioSource::AudioSource(std::unique_ptr audio_clip) { +AudioSource::AudioSource(std::unique_ptr audio_clip) { this->_sound = std::make_unique(std::move(audio_clip)); } diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 4300c48..9dfaf46 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -3,7 +3,7 @@ #include #include "Component.h" -#include "Resource.h" +#include "Asset.h" namespace crepe { class Sound; @@ -14,7 +14,7 @@ namespace crepe::api { //! Audio source component class AudioSource : Component { public: - AudioSource(std::unique_ptr audio_clip); + AudioSource(std::unique_ptr audio_clip); virtual ~AudioSource() = default; public: @@ -26,7 +26,7 @@ public: public: //! Sample file location - std::unique_ptr audio_clip; + std::unique_ptr audio_clip; //! TODO: ????? bool play_on_awake; //! Repeat the current audio clip during playback diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index feb03ef..54c7fdc 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,11 +1,11 @@ target_sources(crepe PUBLIC # AudioSource.cpp - Resource.cpp + Asset.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES AudioSource.h Component.h - Resource.h + Asset.h ) diff --git a/src/crepe/api/Resource.cpp b/src/crepe/api/Resource.cpp deleted file mode 100644 index 1a647ce..0000000 --- a/src/crepe/api/Resource.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include - -#include "Resource.h" - -using namespace crepe::api; - -Resource::Resource(const std::string & src) { - this->src = std::filesystem::canonical(src); - this->file = std::ifstream(this->src, std::ios::in | std::ios::binary); -} - -const std::istream & Resource::read() { return this->file; } - -const char * Resource::canonical() { return this->src.c_str(); } diff --git a/src/crepe/api/Resource.h b/src/crepe/api/Resource.h deleted file mode 100644 index f2b2a0e..0000000 --- a/src/crepe/api/Resource.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace crepe::api { - -class Resource { -public: - Resource(const std::string & src); - -public: - //! Get an input stream to the contents of this resource - const std::istream & read(); - //! Get the canonical path to this resource - const char * canonical(); - -private: - std::string src; - std::ifstream file; -}; - -} // namespace crepe::api diff --git a/src/test/audio.cpp b/src/test/audio.cpp index 5bb2607..1d84551 100644 --- a/src/test/audio.cpp +++ b/src/test/audio.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -15,7 +15,7 @@ using namespace crepe::api; // TODO: mock internal audio class TEST(audio, play) { - auto res = std::make_unique("../mwe/audio/bgm.ogg"); + auto res = std::make_unique("../mwe/audio/bgm.ogg"); auto bgm = AudioSource(std::move(res)); bgm.play(); -- cgit v1.2.3 From b99e38badb82c5cc79771a77c5f6ea180c67ee4f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 5 Oct 2024 14:24:39 +0200 Subject: move Asset from crepe::api to crepe --- src/crepe/Asset.cpp | 14 ++++++++++++++ src/crepe/Asset.h | 24 ++++++++++++++++++++++++ src/crepe/CMakeLists.txt | 2 ++ src/crepe/Sound.cpp | 6 +++--- src/crepe/Sound.h | 6 +++--- src/crepe/api/Asset.cpp | 14 -------------- src/crepe/api/Asset.h | 24 ------------------------ src/crepe/api/CMakeLists.txt | 2 -- 8 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 src/crepe/Asset.cpp create mode 100644 src/crepe/Asset.h delete mode 100644 src/crepe/api/Asset.cpp delete mode 100644 src/crepe/api/Asset.h (limited to 'src/crepe/api/CMakeLists.txt') diff --git a/src/crepe/Asset.cpp b/src/crepe/Asset.cpp new file mode 100644 index 0000000..15ddc27 --- /dev/null +++ b/src/crepe/Asset.cpp @@ -0,0 +1,14 @@ +#include + +#include "Asset.h" + +using namespace crepe; + +Asset::Asset(const std::string & src) { + this->src = std::filesystem::canonical(src); + this->file = std::ifstream(this->src, std::ios::in | std::ios::binary); +} + +const std::istream & Asset::read() { return this->file; } + +const char * Asset::canonical() { return this->src.c_str(); } diff --git a/src/crepe/Asset.h b/src/crepe/Asset.h new file mode 100644 index 0000000..736ba44 --- /dev/null +++ b/src/crepe/Asset.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include + +namespace crepe { + +class Asset { +public: + Asset(const std::string & src); + +public: + //! Get an input stream to the contents of this resource + const std::istream & read(); + //! Get the canonical path to this resource + const char * canonical(); + +private: + std::string src; + std::ifstream file; +}; + +} // namespace crepe diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 3a60840..208ba1f 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,9 +1,11 @@ target_sources(crepe PUBLIC + Asset.cpp Sound.cpp SoundContext.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES + Asset.h Sound.h SoundContext.h ) diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index c6e87d5..73ad69c 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -5,17 +5,17 @@ using namespace crepe; -Sound::Sound(std::unique_ptr res) { +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)); + this->load(std::make_unique(src)); } -void Sound::load(std::unique_ptr res) { +void Sound::load(std::unique_ptr res) { this->sample.load(res->canonical()); } diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h index 339dd7c..1ac20a7 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/Sound.h @@ -5,7 +5,7 @@ #include -#include "api/Asset.h" +#include "Asset.h" namespace crepe { @@ -66,10 +66,10 @@ public: public: Sound(const char * src); - Sound(std::unique_ptr res); + Sound(std::unique_ptr res); private: - void load(std::unique_ptr res); + void load(std::unique_ptr res); private: SoLoud::Wav sample; diff --git a/src/crepe/api/Asset.cpp b/src/crepe/api/Asset.cpp deleted file mode 100644 index 92ee50e..0000000 --- a/src/crepe/api/Asset.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include - -#include "Asset.h" - -using namespace crepe::api; - -Asset::Asset(const std::string & src) { - this->src = std::filesystem::canonical(src); - this->file = std::ifstream(this->src, std::ios::in | std::ios::binary); -} - -const std::istream & Asset::read() { return this->file; } - -const char * Asset::canonical() { return this->src.c_str(); } diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h deleted file mode 100644 index 259c696..0000000 --- a/src/crepe/api/Asset.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace crepe::api { - -class Asset { -public: - Asset(const std::string & src); - -public: - //! Get an input stream to the contents of this resource - const std::istream & read(); - //! Get the canonical path to this resource - const char * canonical(); - -private: - std::string src; - std::ifstream file; -}; - -} // namespace crepe::api diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 54c7fdc..9548594 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,11 +1,9 @@ target_sources(crepe PUBLIC # AudioSource.cpp - Asset.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES AudioSource.h Component.h - Asset.h ) -- cgit v1.2.3 From 2969fe8c0fca4826ca129fe12d2e125bb7955c78 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 6 Oct 2024 17:39:15 +0200 Subject: WIP ScriptSystem --- src/crepe/CMakeLists.txt | 4 ++++ src/crepe/ComponentManager.hpp | 14 ++++++++++---- src/crepe/Script.cpp | 7 +++++++ src/crepe/Script.h | 16 ++++++++++++++++ src/crepe/ScriptSystem.cpp | 22 ++++++++++++++++++++++ src/crepe/ScriptSystem.h | 18 ++++++++++++++++++ src/crepe/System.h | 23 +++++++++++++++++++++++ src/crepe/api/BehaviorScript.cpp | 10 ++++++++++ src/crepe/api/BehaviorScript.h | 17 +++++++++++++++++ src/crepe/api/CMakeLists.txt | 5 +++-- src/crepe/api/Component.h | 10 ---------- src/example/CMakeLists.txt | 1 + src/example/script.cpp | 33 +++++++++++++++++++++++++++++++++ 13 files changed, 164 insertions(+), 16 deletions(-) create mode 100644 src/crepe/Script.cpp create mode 100644 src/crepe/Script.h create mode 100644 src/crepe/ScriptSystem.cpp create mode 100644 src/crepe/ScriptSystem.h create mode 100644 src/crepe/System.h create mode 100644 src/crepe/api/BehaviorScript.cpp create mode 100644 src/crepe/api/BehaviorScript.h delete mode 100644 src/crepe/api/Component.h create mode 100644 src/example/script.cpp (limited to 'src/crepe/api/CMakeLists.txt') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index d7d563e..d85aef0 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -8,6 +8,8 @@ target_sources(crepe PUBLIC Collider.cpp Rigidbody.cpp Sprite.cpp + ScriptSystem.cpp + Script.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -22,6 +24,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h Rigidbody.h Sprite.h + System.h + ScriptSystem.h ) add_subdirectory(api) diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 999cdcf..084cd33 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -1,13 +1,17 @@ #pragma once +#include + #include "ComponentManager.h" namespace crepe { -template +template void ComponentManager::add_component(uint32_t id, Args &&... args) { using namespace std; + static_assert(is_base_of::value, "add_component must recieve a derivative class of Component"); + // Determine the type of T (this is used as the key of the unordered_map<>) type_index type = typeid(T); @@ -23,9 +27,11 @@ void ComponentManager::add_component(uint32_t id, Args &&... args) { components[type].resize(id + 1); } - // Create a new component of type T using perfect forwarding and store its - // unique_ptr in the vector<> - components[type][id].push_back(make_unique(forward(args)...)); + // Create a new component of type T (arguments directly forwarded). The + // constructor must be called by ComponentManager. + T * instance = new T(forward(args)...); + // store its unique_ptr in the vector<> + components[type][id].push_back(unique_ptr(instance)); } template diff --git a/src/crepe/Script.cpp b/src/crepe/Script.cpp new file mode 100644 index 0000000..42e3666 --- /dev/null +++ b/src/crepe/Script.cpp @@ -0,0 +1,7 @@ +#include "Script.h" + +using namespace crepe; + +void Script::init() { } +void Script::update() { } + diff --git a/src/crepe/Script.h b/src/crepe/Script.h new file mode 100644 index 0000000..ba4073a --- /dev/null +++ b/src/crepe/Script.h @@ -0,0 +1,16 @@ +#pragma once + +namespace crepe { + +class Script { +protected: + virtual void init(); + virtual void update(); + // NOTE: additional *events* (like unity's OnDisable and OnEnable) should be + // implemented as member methods in derivative user script classes and + // registered in init(), otherwise this class will balloon in size with each + // added event. +}; + +} + diff --git a/src/crepe/ScriptSystem.cpp b/src/crepe/ScriptSystem.cpp new file mode 100644 index 0000000..e301c71 --- /dev/null +++ b/src/crepe/ScriptSystem.cpp @@ -0,0 +1,22 @@ +#include "util/log.h" + +#include "ScriptSystem.h" + +using namespace crepe; + +ScriptSystem::ScriptSystem() { + dbg_trace(); +} +ScriptSystem::~ScriptSystem() { + dbg_trace(); +} + +ScriptSystem & ScriptSystem::get_instance() { + static ScriptSystem instance; + return instance; +} + +void ScriptSystem::update() { + dbg_trace(); +} + diff --git a/src/crepe/ScriptSystem.h b/src/crepe/ScriptSystem.h new file mode 100644 index 0000000..e1ed290 --- /dev/null +++ b/src/crepe/ScriptSystem.h @@ -0,0 +1,18 @@ +#pragma once + +#include "System.h" + +namespace crepe { + +class ScriptSystem : public System { +public: + static ScriptSystem & get_instance(); + virtual void update(); + +private: + ScriptSystem(); + ~ScriptSystem(); +}; + +} + diff --git a/src/crepe/System.h b/src/crepe/System.h new file mode 100644 index 0000000..3fe3d66 --- /dev/null +++ b/src/crepe/System.h @@ -0,0 +1,23 @@ +#pragma once + +namespace crepe { + +class System { +public: + static System & get_instance(); + virtual void update() = 0; + +protected: + System() { }; + virtual ~System() { }; + +private: + // singleton + System(const System &) = delete; + System(System &&) = delete; + System & operator=(const System &) = delete; + System & operator=(System &&) = delete; +}; + +} + diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp new file mode 100644 index 0000000..2dd933e --- /dev/null +++ b/src/crepe/api/BehaviorScript.cpp @@ -0,0 +1,10 @@ +#include "../util/log.h" + +#include "BehaviorScript.h" + +using namespace crepe::api; + +BehaviorScript::BehaviorScript() { + dbg_trace(); +} + diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h new file mode 100644 index 0000000..e9542c1 --- /dev/null +++ b/src/crepe/api/BehaviorScript.h @@ -0,0 +1,17 @@ +#pragma once + +#include "../Script.h" +#include "../Component.h" + +namespace crepe::api { + +class BehaviorScript : public Script, public Component { + // only allow ComponentManager to instantiate scripts + friend class ComponentManager; + +protected: + BehaviorScript(); +}; + +} + diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 9548594..86623de 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -1,9 +1,10 @@ target_sources(crepe PUBLIC # AudioSource.cpp + BehaviorScript.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - AudioSource.h - Component.h + # AudioSource.h + BehaviorScript.h ) diff --git a/src/crepe/api/Component.h b/src/crepe/api/Component.h deleted file mode 100644 index d5e0499..0000000 --- a/src/crepe/api/Component.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -namespace crepe::api { - -class Component { -public: - bool active; -}; - -} // namespace crepe::api diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index eef38fd..6df4ce7 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -14,4 +14,5 @@ endfunction() add_example(audio_internal) add_example(components_internal) +add_example(script) diff --git a/src/example/script.cpp b/src/example/script.cpp new file mode 100644 index 0000000..28605c7 --- /dev/null +++ b/src/example/script.cpp @@ -0,0 +1,33 @@ +/** \file + * + * Standalone example for usage of the script component and system + */ + +#include +#include +#include +#include + +#include + +using namespace crepe; +using namespace std; + +class MyScript : public api::BehaviorScript { + void update() { + dbg_trace(); + } +}; + +int main() { + dbg_trace(); + + auto obj = GameObject(0, "name", "tag", 0); + obj.add_component(); + + auto & sys = ScriptSystem::get_instance(); + sys.update(); // -> MyScript::update + + return 0; +} + -- cgit v1.2.3 From afdd12277a43d3ad7755f028e85c569dece84f0b Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 8 Oct 2024 15:43:45 +0200 Subject: rendering system --- src/CMakeLists.txt | 2 +- src/crepe/CMakeLists.txt | 1 + src/crepe/api/CMakeLists.txt | 5 ++ src/crepe/api/Color.cpp | 53 +++++++++++++++ src/crepe/api/Color.h | 34 ++++++++++ src/crepe/api/Point.h | 14 ++++ src/crepe/api/Sprite.h | 28 ++++++++ src/crepe/api/Transform.h | 13 ++++ src/crepe/api/game.cpp | 26 +++++-- src/crepe/api/game.h | 16 +++-- src/crepe/api/spritesheet.cpp | 6 -- src/crepe/api/spritesheet.h | 4 -- src/crepe/core/CMakeLists.txt | 8 +++ src/crepe/core/renderSystem.cpp | 37 ++++++++++ src/crepe/core/renderSystem.h | 13 ++++ src/crepe/facade/SdlContext.cpp | 145 +++++++++++++++++++++------------------- src/crepe/facade/SdlContext.h | 19 +++--- src/crepe/facade/Texture.cpp | 6 +- src/crepe/facade/Texture.h | 8 ++- src/dummy_rendering.cpp | 16 +++++ 20 files changed, 349 insertions(+), 105 deletions(-) create mode 100644 src/crepe/api/Color.cpp create mode 100644 src/crepe/api/Color.h create mode 100644 src/crepe/api/Point.h create mode 100644 src/crepe/api/Sprite.h create mode 100644 src/crepe/api/Transform.h create mode 100644 src/crepe/core/CMakeLists.txt create mode 100644 src/crepe/core/renderSystem.cpp create mode 100644 src/crepe/core/renderSystem.h create mode 100644 src/dummy_rendering.cpp (limited to 'src/crepe/api/CMakeLists.txt') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3d29a54..a9193fd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -33,6 +33,6 @@ install( FILE_SET HEADERS DESTINATION include/crepe ) -add_executable(dummy_rm dummy_resource_manager.cpp) +add_executable(dummy_rm dummy_rendering.cpp) #add_executable(dummy_rm dummy_audio.cpp) target_link_libraries(dummy_rm PUBLIC crepe) diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 69e67ac..05f14d1 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -8,4 +8,5 @@ add_subdirectory(api) add_subdirectory(util) add_subdirectory(fabricator) add_subdirectory(facade) +add_subdirectory(core) diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 96b55cf..dcac0ae 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources(crepe PUBLIC resource_manager.cpp Resource.cpp game.cpp + Color.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -15,4 +16,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES resource_manager.h Component.h AudioSource.h + Sprite.h + Color.h + Transform.h + Point.h ) diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp new file mode 100644 index 0000000..c73ce6c --- /dev/null +++ b/src/crepe/api/Color.cpp @@ -0,0 +1,53 @@ + + +#include "Color.h" + + +using namespace crepe::api; + +Color Color::white = Color(255,255,255,0); +Color Color::red = Color(255,0,0,0); +Color Color::green = Color(0,255,0,0); +Color Color::blue = Color(0,0,255,0); +Color Color::black = Color(0,0,0,0); +Color Color::cyan = Color(0,255,255,0); +Color Color::yellow = Color(255,255,0,0); +Color Color::magenta= Color(255,0,255,0); + +Color::Color(double red, double green, double blue, double alpha){ + this->a = alpha; + this->r = red; + this->g = green; + this->b = blue; +}; + +const Color& Color::get_white(){ + return Color::white; +}; + +const Color& Color::get_red(){ + return Color::red; +}; +const Color& Color::get_green(){ + return Color::green; +}; +const Color& Color::get_blue(){ + return Color::blue; +}; + +const Color& Color::get_black(){ + return Color::black; +}; + +const Color& Color::get_cyan(){ + return Color::cyan; +}; + +const Color& Color::get_yellow(){ + return Color::yellow; +}; + +const Color& Color::get_magenta(){ + return Color::magenta; +}; + diff --git a/src/crepe/api/Color.h b/src/crepe/api/Color.h new file mode 100644 index 0000000..207434e --- /dev/null +++ b/src/crepe/api/Color.h @@ -0,0 +1,34 @@ +#pragma once + +namespace crepe::api { + +class Color { + +public: + Color(double red, double green, double blue, double alpha); + static const Color & get_white(); + static const Color & get_red(); + static const Color & get_green(); + static const Color & get_blue(); + static const Color & get_cyan(); + static const Color & get_magenta(); + static const Color & get_yellow(); + static const Color & get_black(); + +private: + double r; + double g; + double b; + double a; + + static Color white; + static Color red; + static Color green; + static Color blue; + static Color cyan; + static Color magenta; + static Color yellow; + static Color black; +}; + +} // namespace crepe::api diff --git a/src/crepe/api/Point.h b/src/crepe/api/Point.h new file mode 100644 index 0000000..463aa7c --- /dev/null +++ b/src/crepe/api/Point.h @@ -0,0 +1,14 @@ +#pragma once + + + +namespace crepe::api { + +class Point { +public: + double x; + double y; +}; + + +} diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h new file mode 100644 index 0000000..84eeb83 --- /dev/null +++ b/src/crepe/api/Sprite.h @@ -0,0 +1,28 @@ +#pragma once + +#include "Component.h" +#include "api/Color.h" +#include "facade/Texture.h" +#include + + +namespace crepe::api { + +struct flip_settings{ + bool flipX : 1; + bool flipY : 1; +}; +class Sprite : public Component { + +public: + Sprite(crepe::Texture& image, const Color& color, const flip_settings& flip ) : sprite_image(&image), color(color), flip(flip){} + crepe::Texture* sprite_image; + Color color; + flip_settings flip; + uint8_t sortingLayer; + uint8_t orderInLayer; + + +}; + +} diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h new file mode 100644 index 0000000..d4dfafc --- /dev/null +++ b/src/crepe/api/Transform.h @@ -0,0 +1,13 @@ +#pragma once + +#include "api/Component.h" +#include "api/Point.h" +namespace crepe::api { + +class Transform : public Component { +public: + Point position; // Translation (shift) + double rotation; // Rotation, in radians + double scale; // Multiplication factoh +}; +} // namespace crepe::api diff --git a/src/crepe/api/game.cpp b/src/crepe/api/game.cpp index 02a0132..01920a9 100644 --- a/src/crepe/api/game.cpp +++ b/src/crepe/api/game.cpp @@ -2,17 +2,29 @@ #include "game.h" -#include "api/spritesheet.h" +#include "core/renderSystem.h" #include "facade/SdlContext.h" -#include "facade/Texture.h" -#include -void game::render(std::vector & draw, std::vector & ss){ - auto& ctx = crepe::SdlContext::get_instance(); - - ctx.loop(*draw[0], *ss[0]); +Engine::Engine(int windowHeight, int window_with){ + crepe::SdlContext& ctx = crepe::SdlContext::get_instance(); } +void Engine::loop() { + + bool running = true; + crepe::SdlContext& ctx = crepe::SdlContext::get_instance(); + RenderSystem rendering; + + while (running) { + ctx.handleEvents(running); + + ctx.clearScreen(); + + rendering.render(); + + ctx.presentScreen(); + } +} diff --git a/src/crepe/api/game.h b/src/crepe/api/game.h index 7cde954..64027fa 100644 --- a/src/crepe/api/game.h +++ b/src/crepe/api/game.h @@ -1,15 +1,17 @@ #pragma once -#include "api/spritesheet.h" -#include "facade/Texture.h" -#include -class game { +class Engine{ public: - game(){} - ~game(){} + Engine(int windowWith, int windowHeight); + ~Engine() = default; - void render(std::vector&, std::vector&); + void loop(); + + +private: + int window_height; + int window_width; }; diff --git a/src/crepe/api/spritesheet.cpp b/src/crepe/api/spritesheet.cpp index 93a2b65..7f5da38 100644 --- a/src/crepe/api/spritesheet.cpp +++ b/src/crepe/api/spritesheet.cpp @@ -30,12 +30,6 @@ void Spritesheet::select_sprite(const int x, const int y){ m_clip.y = y * m_clip.h; } -void Spritesheet::draw_selected_sprite(const int x, const int y){ - auto& ctx = SdlContext::get_instance(); - SDL_Rect tmp = { x, y, m_clip.w, m_clip.h}; - SDL_RenderCopy(ctx.m_game_renderer, this->m_spritesheet, &this->m_clip, &tmp); -} - void Spritesheet::load(std::unique_ptr res, const int row, const int col){ auto& ctx = SdlContext::get_instance(); diff --git a/src/crepe/api/spritesheet.h b/src/crepe/api/spritesheet.h index 7f46296..503dcef 100644 --- a/src/crepe/api/spritesheet.h +++ b/src/crepe/api/spritesheet.h @@ -24,14 +24,10 @@ private: void load(std::unique_ptr res, const int row, const int col);; SDL_Texture* get_texture() const; - - private: SDL_Texture* m_spritesheet; SDL_Rect m_clip; - - friend class SdlContext; }; } diff --git a/src/crepe/core/CMakeLists.txt b/src/crepe/core/CMakeLists.txt new file mode 100644 index 0000000..c44f0f6 --- /dev/null +++ b/src/crepe/core/CMakeLists.txt @@ -0,0 +1,8 @@ +target_sources(crepe PUBLIC + renderSystem.cpp +) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES + renderSystem.h +) + diff --git a/src/crepe/core/renderSystem.cpp b/src/crepe/core/renderSystem.cpp new file mode 100644 index 0000000..a06aeba --- /dev/null +++ b/src/crepe/core/renderSystem.cpp @@ -0,0 +1,37 @@ + + + +#include "renderSystem.h" +#include + +#include "api/Color.h" +#include "api/Sprite.h" +#include "api/Transform.h" +#include "facade/SdlContext.h" +#include "facade/Texture.h" + +using namespace crepe::api; + + +static crepe::Texture player("../asset/texture/img.png"); + + +void RenderSystem::render(){ + + Sprite sprite(player, Color::get_red(), {1,1}); + Transform transform ={ + .position = {0,0}, + .rotation = 0, + .scale = 1, + }; + + // this will get changed to ecs getter of componets + crepe::SdlContext& ctx = crepe::SdlContext::get_instance(); + + ctx.draw(sprite, transform); + /* + for(const auto& S : test_objects){ + ctx.draw(S, const api::Transform &) + } + */ +} diff --git a/src/crepe/core/renderSystem.h b/src/crepe/core/renderSystem.h new file mode 100644 index 0000000..9011b30 --- /dev/null +++ b/src/crepe/core/renderSystem.h @@ -0,0 +1,13 @@ + +#pragma once + + + +class RenderSystem { + +public: + RenderSystem() = default; + ~RenderSystem() = default; + + void render(); +}; diff --git a/src/crepe/facade/SdlContext.cpp b/src/crepe/facade/SdlContext.cpp index 7e2d79f..b2043e5 100644 --- a/src/crepe/facade/SdlContext.cpp +++ b/src/crepe/facade/SdlContext.cpp @@ -2,54 +2,88 @@ #include "SdlContext.h" #include "SDL_rect.h" -#include "api/spritesheet.h" +#include "api/Sprite.h" +#include "api/Transform.h" #include "facade/Texture.h" #include "util/log.h" #include +#include #include #include #include -#include +#include #include -#include #include using namespace crepe; - -SdlContext& SdlContext::get_instance(){ +SdlContext & SdlContext::get_instance() { static SdlContext instance; return instance; } +void SdlContext::handleEvents(bool & running) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + running = false; + } + } +} +void SdlContext::clearScreen() { SDL_RenderClear(this->m_game_renderer); } + +void SdlContext::presentScreen() { SDL_RenderPresent(this->m_game_renderer); } -SdlContext::SdlContext(){ - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl; - return; - } +void SdlContext::draw(const api::Sprite & sprite, const api::Transform& transform) { + static SDL_RendererFlip renderFlip + = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flipX) + | (SDL_FLIP_VERTICAL * sprite.flip.flipY)); - 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; - } + // needs maybe camera for position + static SDL_Rect dstrect = { + .x = static_cast(transform.position.x), + .y = static_cast(transform.position.y), + .w = static_cast(sprite.sprite_image->get_rect().w * transform.scale), + .h = static_cast(sprite.sprite_image->get_rect().h * transform.scale), + }; - 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_RenderCopyEx(this->m_game_renderer, sprite.sprite_image->get_texture(), + &sprite.sprite_image->get_rect(), &dstrect, 0, NULL, renderFlip); +} + +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, 1920, 1080, 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; - } - int imgFlags = IMG_INIT_PNG; - if( !( IMG_Init( imgFlags ) & imgFlags ) ) - { - std::cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl; - } + return; + } + int imgFlags = IMG_INIT_PNG; + if (!(IMG_Init(imgFlags) & imgFlags)) { + std::cout << "SDL_image could not initialize! SDL_image Error: " + << IMG_GetError() << std::endl; + } + SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1"); } -SdlContext::~SdlContext(){ - if(m_game_renderer) - SDL_DestroyRenderer(m_game_renderer); +SdlContext::~SdlContext() { + if (m_game_renderer) SDL_DestroyRenderer(m_game_renderer); if (m_game_window) { SDL_DestroyWindow(m_game_window); @@ -58,69 +92,46 @@ SdlContext::~SdlContext(){ SDL_Quit(); } -SDL_Texture* SdlContext::setTextureFromPath(const char* path, SDL_Rect& clip, const int row, const int col){ +SDL_Texture * SdlContext::setTextureFromPath(const char * path, SDL_Rect & clip, + const int row, const int col) { dbg_trace(); - - SDL_Surface* tmp = IMG_Load(path); + + SDL_Surface * tmp = IMG_Load(path); if (!tmp) { std::cerr << "Error surface " << IMG_GetError << std::endl; } - clip.w = tmp->w / col; - clip.h = tmp->h / row; + clip.w = tmp->w / col; + clip.h = tmp->h / row; - SDL_Texture* CreatedTexture = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + SDL_Texture * CreatedTexture + = SDL_CreateTextureFromSurface(m_game_renderer, tmp); if (!CreatedTexture) { - std::cerr << "Error could not create texture " << IMG_GetError << std::endl; + std::cerr << "Error could not create texture " << IMG_GetError + << std::endl; } SDL_FreeSurface(tmp); return CreatedTexture; } -SDL_Texture* SdlContext::setTextureFromPath(const char* path){ +SDL_Texture * SdlContext::setTextureFromPath(const char * path) { dbg_trace(); - - SDL_Surface* tmp = IMG_Load(path); + + SDL_Surface * tmp = IMG_Load(path); if (!tmp) { std::cerr << "Error surface " << IMG_GetError << std::endl; } - SDL_Texture* CreatedTexture = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + SDL_Texture * CreatedTexture + = SDL_CreateTextureFromSurface(m_game_renderer, tmp); if (!CreatedTexture) { - std::cerr << "Error could not create texture " << IMG_GetError << std::endl; + std::cerr << "Error could not create texture " << IMG_GetError + << std::endl; } SDL_FreeSurface(tmp); return CreatedTexture; } - -void SdlContext::loop(const Texture& texture, api::Spritesheet& ss){ - SDL_RenderClear(m_game_renderer); - bool quit = false; - SDL_Event event; - - while (!quit) { - Uint32 ticks = SDL_GetTicks(); - int sprite = (ticks / 100) % 4; - ss.select_sprite(sprite, 0); - - while (SDL_PollEvent(&event) != NULL) { - switch (event.type) { - case SDL_QUIT: - quit = true; - break; - } - - } - - SDL_RenderClear(m_game_renderer); - SDL_RenderCopy(m_game_renderer, texture.get_texture(), NULL, NULL); - ss.draw_selected_sprite(10, 10); - SDL_RenderPresent(m_game_renderer); - } -} - - diff --git a/src/crepe/facade/SdlContext.h b/src/crepe/facade/SdlContext.h index 329a374..c8f1304 100644 --- a/src/crepe/facade/SdlContext.h +++ b/src/crepe/facade/SdlContext.h @@ -1,23 +1,28 @@ #pragma once #include "SDL_rect.h" -#include "Texture.h" -#include "api/spritesheet.h" +#include "api/Sprite.h" +#include "api/Transform.h" #include #include namespace crepe { -class Texture; -class Spritesheet; class SdlContext { public: - void loop(const Texture& , api::Spritesheet&); + + void handleEvents(bool& running); + void clearScreen(); + void presentScreen(); + void draw(const api::Sprite&, const api::Transform&); // singleton static SdlContext & get_instance(); + SDL_Texture* setTextureFromPath(const char*); + SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col); + private: SdlContext(); virtual ~SdlContext(); @@ -27,12 +32,8 @@ private: SdlContext & operator=(const SdlContext &) = delete; SdlContext & operator=(SdlContext &&) = delete; - SDL_Texture* setTextureFromPath(const char*); - SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col); private: - friend class Texture; - friend class api::Spritesheet; SDL_Window* m_game_window; SDL_Renderer* m_game_renderer; diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp index 220ef2e..b4e3aa8 100644 --- a/src/crepe/facade/Texture.cpp +++ b/src/crepe/facade/Texture.cpp @@ -27,9 +27,13 @@ Texture::~Texture(){ void Texture::load(std::unique_ptr res) { dbg_trace(); SdlContext& ctx = SdlContext::get_instance(); - m_texture = ctx.setTextureFromPath(res->canonical()); + m_texture = ctx.setTextureFromPath(res->canonical(), srcrect, 1, 1); } SDL_Texture* Texture::get_texture() const{ return m_texture; } + +SDL_Rect& Texture::get_rect() { + return srcrect; +} diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h index a5fcca9..db2f1f9 100644 --- a/src/crepe/facade/Texture.h +++ b/src/crepe/facade/Texture.h @@ -1,13 +1,15 @@ #pragma once +#include "SDL_rect.h" #include "api/baseResource.h" -#include "facade/SdlContext.h" #include "api/Resource.h" #include #include namespace crepe { + + class Texture : public api::BaseResource{ public: @@ -16,13 +18,13 @@ public: ~Texture(); SDL_Texture* get_texture() const; + SDL_Rect& get_rect() ; private: void load(std::unique_ptr res); private: SDL_Texture* m_texture; - - friend class SdlContext; + SDL_Rect srcrect; }; } // namespace crepe diff --git a/src/dummy_rendering.cpp b/src/dummy_rendering.cpp new file mode 100644 index 0000000..9bbf92d --- /dev/null +++ b/src/dummy_rendering.cpp @@ -0,0 +1,16 @@ + + + + + + + + +#include "api/game.h" +int main(){ + + Engine engine(800,600); + + engine.loop(); + +} -- cgit v1.2.3 From 1f82ffa4d3ee8355215d43bf43edf8cecaca0d1d Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 16 Oct 2024 17:21:04 +0200 Subject: fix user script implementation --- src/crepe/CMakeLists.txt | 1 - src/crepe/ComponentManager.h | 2 +- src/crepe/ComponentManager.hpp | 5 +++-- src/crepe/GameObject.h | 2 +- src/crepe/GameObject.hpp | 4 ++-- src/crepe/Script.cpp | 7 ------- src/crepe/Script.h | 16 ---------------- src/crepe/ScriptSystem.cpp | 27 ++++++++++++++++++++++----- src/crepe/ScriptSystem.h | 12 +++++++++++- src/crepe/api/BehaviorScript.h | 24 ++++++++++++++++++------ src/crepe/api/BehaviorScript.hpp | 18 ++++++++++++++++++ src/crepe/api/CMakeLists.txt | 2 ++ src/crepe/api/Script.cpp | 5 +++++ src/crepe/api/Script.h | 21 +++++++++++++++++++++ src/example/script.cpp | 7 ++++--- 15 files changed, 108 insertions(+), 45 deletions(-) delete mode 100644 src/crepe/Script.cpp delete mode 100644 src/crepe/Script.h create mode 100644 src/crepe/api/BehaviorScript.hpp create mode 100644 src/crepe/api/Script.cpp create mode 100644 src/crepe/api/Script.h (limited to 'src/crepe/api/CMakeLists.txt') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index d85aef0..8323490 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -9,7 +9,6 @@ target_sources(crepe PUBLIC Rigidbody.cpp Sprite.cpp ScriptSystem.cpp - Script.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index eab9b45..38f32e4 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -23,7 +23,7 @@ public: public: //! Add a component of a specific type template - void add_component(uint32_t id, Args &&... args); + T & add_component(uint32_t id, Args &&... args); //! Deletes all components of a specific type and id template void delete_components_by_id(uint32_t id); diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index c872594..e0242a2 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -3,12 +3,11 @@ #include #include "ComponentManager.h" -#include "api/BehaviorScript.h" namespace crepe { template -void ComponentManager::add_component(uint32_t id, Args &&... args) { +T & ComponentManager::add_component(uint32_t id, Args &&... args) { using namespace std; static_assert(is_base_of::value, "add_component must recieve a derivative class of Component"); @@ -33,6 +32,8 @@ void ComponentManager::add_component(uint32_t id, Args &&... args) { T * instance = new T(forward(args)...); // store its unique_ptr in the vector<> components[type][id].push_back(unique_ptr(instance)); + + return *instance; } template diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h index 3588d9a..b5d6399 100644 --- a/src/crepe/GameObject.h +++ b/src/crepe/GameObject.h @@ -10,7 +10,7 @@ public: GameObject(uint32_t id, std::string name, std::string tag, int layer); template - void add_component(Args &&... args); + T & add_component(Args &&... args); uint32_t id; std::string name; diff --git a/src/crepe/GameObject.hpp b/src/crepe/GameObject.hpp index 5966fbf..8cd1abe 100644 --- a/src/crepe/GameObject.hpp +++ b/src/crepe/GameObject.hpp @@ -7,9 +7,9 @@ namespace crepe { template -void GameObject::add_component(Args &&... args) { +T & GameObject::add_component(Args &&... args) { auto & mgr = ComponentManager::get_instance(); - mgr.add_component(id, std::forward(args)...); + return mgr.add_component(id, std::forward(args)...); } } // namespace crepe diff --git a/src/crepe/Script.cpp b/src/crepe/Script.cpp deleted file mode 100644 index 42e3666..0000000 --- a/src/crepe/Script.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "Script.h" - -using namespace crepe; - -void Script::init() { } -void Script::update() { } - diff --git a/src/crepe/Script.h b/src/crepe/Script.h deleted file mode 100644 index ba4073a..0000000 --- a/src/crepe/Script.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -namespace crepe { - -class Script { -protected: - virtual void init(); - virtual void update(); - // NOTE: additional *events* (like unity's OnDisable and OnEnable) should be - // implemented as member methods in derivative user script classes and - // registered in init(), otherwise this class will balloon in size with each - // added event. -}; - -} - diff --git a/src/crepe/ScriptSystem.cpp b/src/crepe/ScriptSystem.cpp index e40909e..0537c16 100644 --- a/src/crepe/ScriptSystem.cpp +++ b/src/crepe/ScriptSystem.cpp @@ -1,12 +1,16 @@ +#include +#include #include + +#include "ScriptSystem.h" #include "ComponentManager.h" #include "api/BehaviorScript.h" -#include "util/fmt.h" +#include "api/Script.h" #include "util/log.h" -#include "ScriptSystem.h" - +using namespace std; using namespace crepe; +using namespace crepe::api; ScriptSystem::ScriptSystem() { dbg_trace(); @@ -24,10 +28,23 @@ void ScriptSystem::update() { using namespace std; dbg_trace(); + forward_list