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 --- lib/soloud/CMakeLists.txt | 2 +- src/CMakeLists.txt | 13 ++++++++++++- src/crepe/CMakeLists.txt | 7 +++++-- src/crepe/Sound.cpp | 9 +++++++++ src/crepe/Sound.h | 30 ++++++++++++++++++++++++++++++ src/crepe/api/AudioSource.cpp | 25 +++++++++++++++++++++++++ src/crepe/api/AudioSource.h | 42 ++++++++++++++++++++++++++++++++++++++++++ src/crepe/api/CMakeLists.txt | 4 ++++ src/crepe/api/Component.h | 11 +++++++++++ src/crepe/api/Resource.h | 17 +++++++++++++++++ src/crepe/main.cpp | 3 --- src/crepe/util/CMakeLists.txt | 0 src/crepe/util/debug.cpp | 0 src/crepe/util/debug.h | 0 test/CMakeLists.txt | 6 ++++-- test/audio.cpp | 29 +++++++++++++++++++++++++++++ 16 files changed, 189 insertions(+), 9 deletions(-) create mode 100644 src/crepe/Sound.cpp create mode 100644 src/crepe/Sound.h create mode 100644 src/crepe/api/AudioSource.cpp create mode 100644 src/crepe/api/AudioSource.h create mode 100644 src/crepe/api/CMakeLists.txt create mode 100644 src/crepe/api/Component.h create mode 100644 src/crepe/api/Resource.h delete mode 100644 src/crepe/main.cpp create mode 100644 src/crepe/util/CMakeLists.txt create mode 100644 src/crepe/util/debug.cpp create mode 100644 src/crepe/util/debug.h create mode 100644 test/audio.cpp diff --git a/lib/soloud/CMakeLists.txt b/lib/soloud/CMakeLists.txt index 8433281..aaccd96 100644 --- a/lib/soloud/CMakeLists.txt +++ b/lib/soloud/CMakeLists.txt @@ -9,7 +9,7 @@ add_subdirectory(../sdl2 sdl2) project(soloud C CXX) -add_library(soloud STATIC +add_library(soloud SHARED src/src/audiosource/ay/chipplayer.cpp src/src/audiosource/ay/sndbuffer.cpp src/src/audiosource/ay/sndchip.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0090188..cc71435 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,9 +8,20 @@ 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 + SYSTEM INTERFACE . +) + +# NOTE: all libraries *must* be linked as PRIVATE +target_link_libraries(crepe + PRIVATE soloud +) add_subdirectory(crepe) diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 392b7d7..aa64262 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,3 +1,6 @@ -target_sources(main PUBLIC - main.cpp +target_sources(crepe PUBLIC + Sound.cpp ) + +add_subdirectory(api) + diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp new file mode 100644 index 0000000..f45a697 --- /dev/null +++ b/src/crepe/Sound.cpp @@ -0,0 +1,9 @@ +#include "Sound.h" + +using namespace crepe; + +Sound::Sound(std::unique_ptr res) { + _res = std::move(res); +} + + diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h new file mode 100644 index 0000000..ac4b7f4 --- /dev/null +++ b/src/crepe/Sound.h @@ -0,0 +1,30 @@ +#pragma once + +#include + +#include + +#include "api/Resource.h" + +namespace crepe { + +class Sound { +public: + Sound(std::unique_ptr res); + virtual ~Sound() = default; + +public: + void pause(); + void play(); + void rewind(); + void set_volume(float volume); + void set_looping(bool looping); + +private: + std::unique_ptr _res; + SoLoud::handle _handle; + bool _paused; +}; + +} + diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp new file mode 100644 index 0000000..cbde79f --- /dev/null +++ b/src/crepe/api/AudioSource.cpp @@ -0,0 +1,25 @@ +#include "AudioSource.h" + +#include "../Sound.h" +#include + +using namespace crepe::api; + +AudioSource::AudioSource(std::unique_ptr audio_clip) { + this->_sound = std::make_unique(std::move(audio_clip)); +} + +void AudioSource::play() { + return this->play(false); +} + +void AudioSource::play(bool looping) { + this->_sound->set_looping(looping); + this->_sound->play(); +} + +void AudioSource::stop() { + this->_sound->pause(); + this->_sound->rewind(); +} + diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h new file mode 100644 index 0000000..6a038be --- /dev/null +++ b/src/crepe/api/AudioSource.h @@ -0,0 +1,42 @@ +#pragma once + +#include + +#include "Component.h" +#include "Resource.h" + +namespace crepe { +class Sound; +} + +namespace crepe::api { + +//! Audio source component +class AudioSource : Component { +public: + AudioSource(std::unique_ptr audio_clip); + virtual ~AudioSource() = default; + +public: + //! Start or resume this audio source + void play(); + void play(bool looping); + //! Stop this audio source + void stop(); + +public: + //! Sample file location + std::unique_ptr audio_clip; + //! TODO: ????? + bool play_on_awake; + //! Repeat the current audio clip during playback + bool loop; + //! Normalized volume (0.0 - 1.0) + float volume; + +private: + std::unique_ptr _sound; +}; + +} + 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 +) + diff --git a/src/crepe/api/Component.h b/src/crepe/api/Component.h new file mode 100644 index 0000000..2abb461 --- /dev/null +++ b/src/crepe/api/Component.h @@ -0,0 +1,11 @@ +#pragma once + +namespace crepe::api { + +class Component { +public: + bool active; +}; + +} + diff --git a/src/crepe/api/Resource.h b/src/crepe/api/Resource.h new file mode 100644 index 0000000..620a10e --- /dev/null +++ b/src/crepe/api/Resource.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +namespace crepe::api { + +class Resource { +public: + Resource(const std::string & source); + virtual ~Resource(); + +private: + std::string _source; +}; + +} + 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/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/crepe/util/debug.cpp b/src/crepe/util/debug.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/crepe/util/debug.h b/src/crepe/util/debug.h new file mode 100644 index 0000000..e69de29 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 26aa460..d103b9a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,13 +9,15 @@ set(CMAKE_BUILD_TYPE Debug) project(test C CXX) add_subdirectory(../lib/googletest googletest) +add_subdirectory(../src crepe) add_executable(test dummy.cpp + audio.cpp ) target_link_libraries(test - gtest_main - # TODO: add crepe engine + PRIVATE gtest_main + PUBLIC crepe # TODO: this does not work properly ) diff --git a/test/audio.cpp b/test/audio.cpp new file mode 100644 index 0000000..a415919 --- /dev/null +++ b/test/audio.cpp @@ -0,0 +1,29 @@ +#include +#include + +#include +#include + +#include +#include + +using namespace std; +using namespace std::chrono_literals; + +using namespace crepe::api; + +// TODO: mock internal audio class + +TEST(audio, play) { + auto res = std::make_unique("../mwe/audio/bgm.ogg"); + auto bgm = AudioSource(std::move(res)); + + bgm.play(); + + this_thread::sleep_for(2s); + + bgm.stop(); + + ASSERT_TRUE(true); +} + -- cgit v1.2.3 From 86a7bd1aee2ee96079e00e2696a624fd6b642fa0 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 26 Sep 2024 10:38:45 +0200 Subject: update --- .vscode/settings.json | 5 ++++- .vscode/tasks.json | 28 ++++++++++++++++++++++++++++ gameloop/include/gameObject.h | 1 + gameloop/include/timer.h | 2 ++ gameloop/src/loopManager.cpp | 38 +++++++++++++++++++++++++++++--------- gameloop/src/timer.cpp | 9 +++++++-- 6 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 .vscode/tasks.json diff --git a/.vscode/settings.json b/.vscode/settings.json index d265510..af4ae4e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,6 @@ { - "cmake.sourceDirectory": "${workspaceFolder}/src" + "cmake.sourceDirectory": "${workspaceFolder}/src", + "files.associations": { + "*.tcc": "cpp" + } } diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..0fd5d23 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++ build active file", + "command": "/usr/bin/g++", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} diff --git a/gameloop/include/gameObject.h b/gameloop/include/gameObject.h index 0e17991..fc7d026 100644 --- a/gameloop/include/gameObject.h +++ b/gameloop/include/gameObject.h @@ -18,6 +18,7 @@ class GameObject { void setHeight(float value); void setVelX(float value); void setVelY(float value); + int direction; private: std::string name = ""; float x = 0; diff --git a/gameloop/include/timer.h b/gameloop/include/timer.h index a245e5c..ca1e5f3 100644 --- a/gameloop/include/timer.h +++ b/gameloop/include/timer.h @@ -13,6 +13,8 @@ public: double getFixedDeltaTime() const; void setFPS(int FPS); int getFPS() const; + double getGameScale(); + void setGameScale(double); void enforceFrameRate(); double getLag() const; private: diff --git a/gameloop/src/loopManager.cpp b/gameloop/src/loopManager.cpp index f7ce886..dde3cef 100644 --- a/gameloop/src/loopManager.cpp +++ b/gameloop/src/loopManager.cpp @@ -12,7 +12,12 @@ void LoopManager::processInput(){ case SDL_KEYDOWN: if(event.key.keysym.sym == SDLK_ESCAPE){ gameRunning = false; + }else if(event.key.keysym.sym == SDLK_i){ + LoopTimer::getInstance().setGameScale(LoopTimer::getInstance().getGameScale() + 0.1); + }else if(event.key.keysym.sym == SDLK_k){ + LoopTimer::getInstance().setGameScale(LoopTimer::getInstance().getGameScale() - 0.1); } + break; } } @@ -44,10 +49,10 @@ void LoopManager::loop() { void LoopManager::setup(){ gameRunning = window.initWindow(); LoopTimer::getInstance().start(); - LoopTimer::getInstance().setFPS(50); + LoopTimer::getInstance().setFPS(500); - for(int i = 0; i < 2;i++){ - GameObject* square = new GameObject("square2",i*40,i*40,20,20,0,0); + for(int i = 1; i < 3;i++){ + GameObject* square = new GameObject("square2",i*60,i*60,20,20,0,0); objectList.push_back(square); } } @@ -59,14 +64,29 @@ void LoopManager::render(){ } void LoopManager::update() { - fprintf(stderr,"**********normal update********** \n"); + fprintf(stderr, "********** normal update ********** \n"); LoopTimer& timer = LoopTimer::getInstance(); - - float delta= timer.getDeltaTime(); - + + float delta = timer.getDeltaTime(); for (int i = 0; i < objectList.size(); i++) { - objectList[i]->setX(objectList[i]->getX() + 50 * delta); - objectList[i]->setY(objectList[i]->getY() + 50 * delta); + GameObject* obj = objectList[i]; + + // Move the object based on its direction + if (obj->direction == 1) { + obj->setX(obj->getX() + 50 * delta); + } else { + obj->setX(obj->getX() - 50 * delta); + } + + + if (obj->getX() > 500) { + obj->setX(500); + obj->direction = 0; // Switch direction to left + } else if (obj->getX() < 50) { + obj->setX(50); // Clamp the position to the boundary + obj->direction = 1; // Switch direction to right + } } } + diff --git a/gameloop/src/timer.cpp b/gameloop/src/timer.cpp index 1e3045f..03a2dbb 100644 --- a/gameloop/src/timer.cpp +++ b/gameloop/src/timer.cpp @@ -25,7 +25,7 @@ void LoopTimer::update() { if (deltaTime > maximumDeltaTime) { deltaTime = maximumDeltaTime; } - + deltaTime *= gameScale; elapsedTime += deltaTime; lastFrameTime = currentFrameTime; } @@ -53,7 +53,12 @@ void LoopTimer::setFPS(int FPS) { int LoopTimer::getFPS() const { return FPS; } - +void LoopTimer::setGameScale(double value){ + gameScale = value; +}; +double LoopTimer::getGameScale(){ + return gameScale; +} void LoopTimer::enforceFrameRate() { uint64_t currentFrameTime = SDL_GetTicks64(); double frameDuration = (currentFrameTime - lastFrameTime) / 1000.0; -- cgit v1.2.3 From 506090032a07f2f3a74a44d8c8774cbdd252c947 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 28 Sep 2024 14:45:53 +0200 Subject: WIP ExternalProject test --- mwe/dynlink/exec/CMakeLists.txt | 10 +++++++++- mwe/dynlink/exec/main.c | 3 ++- mwe/dynlink/lib/CMakeLists.txt | 16 +++++++++++++++- mwe/dynlink/lib/lib.c | 3 --- mwe/dynlink/lib/lib.h | 3 --- mwe/dynlink/lib/test.c | 3 +++ mwe/dynlink/lib/test.h | 3 +++ 7 files changed, 32 insertions(+), 9 deletions(-) delete mode 100644 mwe/dynlink/lib/lib.c delete mode 100644 mwe/dynlink/lib/lib.h create mode 100644 mwe/dynlink/lib/test.c create mode 100644 mwe/dynlink/lib/test.h diff --git a/mwe/dynlink/exec/CMakeLists.txt b/mwe/dynlink/exec/CMakeLists.txt index 5335f0f..465b45e 100644 --- a/mwe/dynlink/exec/CMakeLists.txt +++ b/mwe/dynlink/exec/CMakeLists.txt @@ -5,12 +5,20 @@ set(CMAKE_CXX_STANDARD 20) project(main C CXX) +add_executable(main main.c) + # Since we have the source code for the test library, we might as well let # CMake use it so it automatically picks up the interface headers and compiles # the library for us. The same can be achieved manually, but is more code. add_subdirectory(../lib test) -add_executable(main main.c) +# include(ExternalProject) +# ExternalProject_Add(test_ext +# SOURCE_DIR ../../lib +# CMAKE_ARGS -DCMAKE_INSTALL_PREFIX="${CMAKE_BINARY_DIR}/ext" +# ) +# add_dependencies(main test_ext) +# find_package(test REQUIRED) # Make sure ld.so (linux) looks in the same folder as the final executable for # the .so dependency diff --git a/mwe/dynlink/exec/main.c b/mwe/dynlink/exec/main.c index 2bbdc20..ea55feb 100644 --- a/mwe/dynlink/exec/main.c +++ b/mwe/dynlink/exec/main.c @@ -1,7 +1,8 @@ -#include +#include int main() { library_function(); return 0; } + diff --git a/mwe/dynlink/lib/CMakeLists.txt b/mwe/dynlink/lib/CMakeLists.txt index 2d15353..7efc91a 100644 --- a/mwe/dynlink/lib/CMakeLists.txt +++ b/mwe/dynlink/lib/CMakeLists.txt @@ -5,6 +5,20 @@ set(CMAKE_CXX_STANDARD 20) project(lib C CXX) -add_library(test SHARED lib.c) +add_library(test SHARED) + target_include_directories(test SYSTEM INTERFACE .) +target_sources(test PUBLIC + test.c +) + +target_sources(test PUBLIC FILE_SET HEADERS FILES + test.h +) + +install( + TARGETS test + FILE_SET HEADERS DESTINATION include +) + diff --git a/mwe/dynlink/lib/lib.c b/mwe/dynlink/lib/lib.c deleted file mode 100644 index c7a78e4..0000000 --- a/mwe/dynlink/lib/lib.c +++ /dev/null @@ -1,3 +0,0 @@ -#include - -void library_function() { printf("%s\n", __PRETTY_FUNCTION__); } diff --git a/mwe/dynlink/lib/lib.h b/mwe/dynlink/lib/lib.h deleted file mode 100644 index 093eadb..0000000 --- a/mwe/dynlink/lib/lib.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void library_function(); diff --git a/mwe/dynlink/lib/test.c b/mwe/dynlink/lib/test.c new file mode 100644 index 0000000..c7a78e4 --- /dev/null +++ b/mwe/dynlink/lib/test.c @@ -0,0 +1,3 @@ +#include + +void library_function() { printf("%s\n", __PRETTY_FUNCTION__); } diff --git a/mwe/dynlink/lib/test.h b/mwe/dynlink/lib/test.h new file mode 100644 index 0000000..093eadb --- /dev/null +++ b/mwe/dynlink/lib/test.h @@ -0,0 +1,3 @@ +#pragma once + +void library_function(); -- 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 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 feea4cbb648d67e46b413880ddbf203c88c2a2b1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 29 Sep 2024 16:15:49 +0200 Subject: implement debug logging functions and fix sound system segfault --- src/crepe/CMakeLists.txt | 1 + src/crepe/Sound.cpp | 17 ++++++++++----- src/crepe/Sound.h | 3 +-- src/crepe/SoundSystem.cpp | 11 ++++++++-- src/crepe/SoundSystem.h | 8 ++++++- src/crepe/util/CMakeLists.txt | 9 ++++++++ src/crepe/util/color.h | 42 +++++++++++++++++++++++++++++++++++ src/crepe/util/debug.cpp | 0 src/crepe/util/debug.h | 0 src/crepe/util/log.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++ src/crepe/util/log.h | 33 ++++++++++++++++++++++++++++ src/dummy_audio.cpp | 3 +++ 12 files changed, 167 insertions(+), 11 deletions(-) create mode 100644 src/crepe/util/color.h delete mode 100644 src/crepe/util/debug.cpp delete mode 100644 src/crepe/util/debug.h create mode 100644 src/crepe/util/log.cpp create mode 100644 src/crepe/util/log.h diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 13d9be5..9f7c91c 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -9,4 +9,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES ) add_subdirectory(api) +add_subdirectory(util) diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index d48393c..94f97af 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -1,24 +1,29 @@ +#include "util/log.h" + #include "Sound.h" #include "SoundSystem.h" using namespace crepe; -Sound::Sound(std::unique_ptr res, SoundSystem & system) : system(system) { +Sound::Sound(std::unique_ptr res) { + dbg_trace(); this->res = std::move(res); } void Sound::play() { - if (this->system.engine.getPause(this->handle)) { + SoundSystem & system = SoundSystem::instance(); + if (system.engine.getPause(this->handle)) { // resume if paused - this->system.engine.setPause(this->handle, false); + system.engine.setPause(this->handle, false); } else { // or start new sound - this->handle = this->system.engine.play(this->sample); + this->handle = system.engine.play(this->sample); } } void Sound::pause() { - if (this->system.engine.getPause(this->handle)) return; - this->system.engine.setPause(this->handle, true); + SoundSystem & system = SoundSystem::instance(); + if (system.engine.getPause(this->handle)) return; + system.engine.setPause(this->handle, true); } diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h index 71fe390..333465b 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/Sound.h @@ -55,9 +55,8 @@ public: void set_looping(bool looping); private: - Sound(std::unique_ptr res, SoundSystem & system); - SoundSystem & system; friend class SoundSystem; + Sound(std::unique_ptr res); private: std::unique_ptr res; diff --git a/src/crepe/SoundSystem.cpp b/src/crepe/SoundSystem.cpp index 30b0157..29bd196 100644 --- a/src/crepe/SoundSystem.cpp +++ b/src/crepe/SoundSystem.cpp @@ -1,9 +1,14 @@ +#include "util/log.h" + #include "SoundSystem.h" #include using namespace crepe; -SoundSystem SoundSystem::instance { }; +SoundSystem & SoundSystem::instance() { + static SoundSystem instance; + return instance; +} std::unique_ptr SoundSystem::sound(const std::string & src) { auto res = std::make_unique(src); @@ -11,15 +16,17 @@ std::unique_ptr SoundSystem::sound(const std::string & src) { } std::unique_ptr SoundSystem::sound(std::unique_ptr res) { - Sound * out = new Sound(std::move(res), SoundSystem::instance); + Sound * out = new Sound(std::move(res)); return std::unique_ptr(out); } SoundSystem::SoundSystem() { + dbg_trace(); engine.init(); } SoundSystem::~SoundSystem() { + dbg_trace(); engine.deinit(); } diff --git a/src/crepe/SoundSystem.h b/src/crepe/SoundSystem.h index 23bb00a..515cb29 100644 --- a/src/crepe/SoundSystem.h +++ b/src/crepe/SoundSystem.h @@ -16,7 +16,13 @@ public: private: SoundSystem(); virtual ~SoundSystem(); - static SoundSystem instance; + + // singleton + static SoundSystem & instance(); + SoundSystem(const SoundSystem &) = delete; + SoundSystem(SoundSystem &&) = delete; + SoundSystem &operator=(const SoundSystem &) = delete; + SoundSystem &operator=(SoundSystem &&) = delete; private: SoLoud::Soloud engine; diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt index e69de29..100f028 100644 --- a/src/crepe/util/CMakeLists.txt +++ b/src/crepe/util/CMakeLists.txt @@ -0,0 +1,9 @@ +target_sources(crepe PUBLIC + log.cpp +) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES + color.h + log.h +) + diff --git a/src/crepe/util/color.h b/src/crepe/util/color.h new file mode 100644 index 0000000..1af6c8f --- /dev/null +++ b/src/crepe/util/color.h @@ -0,0 +1,42 @@ +#pragma once + +namespace crepe::util::color { + +constexpr const char * RESET = "\e[0m"; + +constexpr const char * FG_BLACK = "\e[30m"; +constexpr const char * FG_RED = "\e[31m"; +constexpr const char * FG_GREEN = "\e[32m"; +constexpr const char * FG_YELLOW = "\e[33m"; +constexpr const char * FG_BLUE = "\e[34m"; +constexpr const char * FG_MAGENTA = "\e[35m"; +constexpr const char * FG_CYAN = "\e[36m"; +constexpr const char * FG_WHITE = "\e[37m"; +constexpr const char * BG_BLACK = "\e[40m"; +constexpr const char * BG_RED = "\e[41m"; +constexpr const char * BG_GREEN = "\e[42m"; +constexpr const char * BG_YELLOW = "\e[43m"; +constexpr const char * BG_BLUE = "\e[44m"; +constexpr const char * BG_MAGENTA = "\e[45m"; +constexpr const char * BG_CYAN = "\e[46m"; +constexpr const char * BG_WHITE = "\e[47m"; + +constexpr const char * FG_BLACK_BRIGHT = "\e[90m"; +constexpr const char * FG_RED_BRIGHT = "\e[91m"; +constexpr const char * FG_GREEN_BRIGHT = "\e[92m"; +constexpr const char * FG_YELLOW_BRIGHT = "\e[93m"; +constexpr const char * FG_BLUE_BRIGHT = "\e[94m"; +constexpr const char * FG_MAGENTA_BRIGHT = "\e[95m"; +constexpr const char * FG_CYAN_BRIGHT = "\e[96m"; +constexpr const char * FG_WHITE_BRIGHT = "\e[97m"; +constexpr const char * BG_BLACK_BRIGHT = "\e[100m"; +constexpr const char * BG_RED_BRIGHT = "\e[101m"; +constexpr const char * BG_GREEN_BRIGHT = "\e[102m"; +constexpr const char * BG_YELLOW_BRIGHT = "\e[103m"; +constexpr const char * BG_BLUE_BRIGHT = "\e[104m"; +constexpr const char * BG_MAGENTA_BRIGHT = "\e[105m"; +constexpr const char * BG_CYAN_BRIGHT = "\e[106m"; +constexpr const char * BG_WHITE_BRIGHT = "\e[107m"; + +} + diff --git a/src/crepe/util/debug.cpp b/src/crepe/util/debug.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/crepe/util/debug.h b/src/crepe/util/debug.h deleted file mode 100644 index e69de29..0000000 diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp new file mode 100644 index 0000000..796df49 --- /dev/null +++ b/src/crepe/util/log.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include + +#include "log.h" + +using namespace crepe::util; + +static const char * const LOG_PREFIX[] = { + [log_level::debug] = "[DBG] ", + [log_level::info] = "[INFO] ", + [log_level::warning] = "[WARN] ", + [log_level::error] = "[ERR] ", +}; + +static void va_logf(enum log_level level, va_list args, const std::string fmt) { + va_list args_copy; + va_copy(args_copy, args); + + // prepend log level and ensure newline + std::string format_fixed = LOG_PREFIX[level] + fmt; + if (!format_fixed.ends_with("\n")) format_fixed += "\n"; + + size_t sz = vsnprintf(NULL, 0, format_fixed.c_str(), args_copy) + 1; + char * msg = (char *) malloc(sz); + va_end(args_copy); + + vsnprintf(msg, sz, format_fixed.c_str(), args); + + // TODO: also log to file or smth + printf("%s", msg); + fflush(stdout); + + free(msg); +} + +void crepe::util::logf(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + va_logf(crepe::util::log_level::debug, args, fmt); + va_end(args); +} + +void crepe::util::logf(log_level level, const char * fmt, ...) { + va_list args; + va_start(args, fmt); + va_logf(level, args, fmt); + va_end(args); +} + diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h new file mode 100644 index 0000000..5295cb9 --- /dev/null +++ b/src/crepe/util/log.h @@ -0,0 +1,33 @@ +#pragma once + +// allow user to disable debug macros +#ifndef CREPE_DISABLE_MACROS + +#include "color.h" + +// utility macros +#define _crepe_logf_here(fmt, ...) \ + crepe::util::logf(util::log_level::debug, "%s%s (%s:%d)" fmt "\n", \ + crepe::util::color::FG_WHITE, \ + __PRETTY_FUNCTION__, \ + __FILE_NAME__, \ + __LINE__, \ + crepe::util::color::RESET, \ + __VA_ARGS__) + +#define dbg_logf(fmt, ...) _crepe_logf_here(": " fmt, __VA_ARGS__) +#define dbg_log(str) _crepe_logf_here(": %s", str) +#define dbg_trace() _crepe_logf_here("%s", "") + +#endif + +namespace crepe::util { + +enum log_level { debug, info, warning, error, }; + +void logf(const char * fmt, ...); +void logf(enum log_level level, const char * fmt, ...); + +} + + diff --git a/src/dummy_audio.cpp b/src/dummy_audio.cpp index 5e0000e..fb1e3ab 100644 --- a/src/dummy_audio.cpp +++ b/src/dummy_audio.cpp @@ -1,4 +1,5 @@ #include "crepe/SoundSystem.h" +#include "crepe/util/log.h" #include #include @@ -8,6 +9,8 @@ using namespace std; using namespace std::chrono_literals; int main() { + dbg_trace(); + auto bgm = SoundSystem::sound("../mwe/audio/bgm.ogg"); auto sfx1 = SoundSystem::sound("../mwe/audio/sfx1.wav"); auto sfx2 = SoundSystem::sound("../mwe/audio/sfx2.wav"); -- cgit v1.2.3 From e4a3ef6e324acc8edf9f0797caa244967907a676 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 29 Sep 2024 16:37:12 +0200 Subject: implement audio poc using facade classes --- src/crepe/Sound.cpp | 1 + src/crepe/api/Resource.cpp | 11 +++++++++-- src/crepe/api/Resource.h | 3 +++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index 94f97af..181366a 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -8,6 +8,7 @@ using namespace crepe; Sound::Sound(std::unique_ptr res) { dbg_trace(); this->res = std::move(res); + this->sample.load(this->res->canonical()); } void Sound::play() { diff --git a/src/crepe/api/Resource.cpp b/src/crepe/api/Resource.cpp index a38900b..6bb081d 100644 --- a/src/crepe/api/Resource.cpp +++ b/src/crepe/api/Resource.cpp @@ -1,12 +1,19 @@ +#include + #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); +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 index 2260b1a..2b62ff9 100644 --- a/src/crepe/api/Resource.h +++ b/src/crepe/api/Resource.h @@ -11,7 +11,10 @@ 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; -- cgit v1.2.3 From 90174919ba8c019d4b88915557be0adc3769b3e6 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 29 Sep 2024 17:22:58 +0200 Subject: WIP cleanup (broken, but more like the class diagram) --- src/crepe/Sound.cpp | 10 +++++++++- src/crepe/Sound.h | 10 +++++----- src/crepe/SoundSystem.cpp | 11 ----------- src/crepe/SoundSystem.h | 6 ------ src/dummy_audio.cpp | 29 +++++++++++++++-------------- 5 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index 181366a..09ffd5f 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -7,7 +7,15 @@ using namespace crepe; Sound::Sound(std::unique_ptr res) { dbg_trace(); - this->res = std::move(res); + 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(this->res->canonical()); } diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h index 333465b..163c5b4 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/Sound.h @@ -9,8 +9,6 @@ namespace crepe { -class SoundSystem; - class Sound { public: /** @@ -54,15 +52,17 @@ public: */ void set_looping(bool looping); -private: - friend class SoundSystem; +public: + Sound(const char * src); Sound(std::unique_ptr res); +private: + void load(std::unique_ptr res); + 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 index 29bd196..00f874c 100644 --- a/src/crepe/SoundSystem.cpp +++ b/src/crepe/SoundSystem.cpp @@ -1,7 +1,6 @@ #include "util/log.h" #include "SoundSystem.h" -#include using namespace crepe; @@ -10,16 +9,6 @@ SoundSystem & SoundSystem::instance() { return 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)); - return std::unique_ptr(out); -} - SoundSystem::SoundSystem() { dbg_trace(); engine.init(); diff --git a/src/crepe/SoundSystem.h b/src/crepe/SoundSystem.h index 515cb29..da3927a 100644 --- a/src/crepe/SoundSystem.h +++ b/src/crepe/SoundSystem.h @@ -2,17 +2,11 @@ #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(); diff --git a/src/dummy_audio.cpp b/src/dummy_audio.cpp index fb1e3ab..1249076 100644 --- a/src/dummy_audio.cpp +++ b/src/dummy_audio.cpp @@ -1,5 +1,5 @@ -#include "crepe/SoundSystem.h" #include "crepe/util/log.h" +#include "crepe/Sound.h" #include #include @@ -7,32 +7,33 @@ using namespace crepe; using namespace std; using namespace std::chrono_literals; +using std::make_unique; int main() { dbg_trace(); - 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"); + auto bgm = Sound("../mwe/audio/bgm.ogg"); + auto sfx1 = Sound("../mwe/audio/sfx1.wav"); + auto sfx2 = Sound("../mwe/audio/sfx2.wav"); + auto sfx3 = Sound("../mwe/audio/sfx3.wav"); - bgm->play(); + bgm.play(); // play each sample sequentially this_thread::sleep_for(500ms); - sfx1->play(); + sfx1.play(); this_thread::sleep_for(500ms); - sfx2->play(); - bgm->pause(); + sfx2.play(); + bgm.pause(); this_thread::sleep_for(500ms); - sfx3->play(); - bgm->play(); + sfx3.play(); + bgm.play(); this_thread::sleep_for(500ms); // play all samples simultaniously - sfx1->play(); - sfx2->play(); - sfx3->play(); + sfx1.play(); + sfx2.play(); + sfx3.play(); this_thread::sleep_for(1000ms); return 0; -- cgit v1.2.3 From de288a859a631acf6aac10fad825cafaa3744dc9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 1 Oct 2024 15:21:31 +0200 Subject: fix audio segfault --- src/crepe/Sound.cpp | 2 +- src/crepe/Sound.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index 09ffd5f..136a71d 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -16,7 +16,7 @@ Sound::Sound(const char * src) { } void Sound::load(std::unique_ptr res) { - this->sample.load(this->res->canonical()); + this->sample.load(res->canonical()); } void Sound::play() { diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h index 163c5b4..9da17b9 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/Sound.h @@ -60,7 +60,6 @@ private: void load(std::unique_ptr res); private: - std::unique_ptr res; SoLoud::Wav sample; SoLoud::handle handle; }; -- cgit v1.2.3 From 311e98572c26750e4a4695079fa80ca5648d109e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 1 Oct 2024 17:21:37 +0200 Subject: implement remaining Sound functions --- src/crepe/Sound.cpp | 29 ++++++++++++++++++++++++++--- src/crepe/Sound.h | 15 +++++++++++++++ src/crepe/SoundSystem.cpp | 2 +- src/crepe/SoundSystem.h | 2 +- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index 136a71d..20f1787 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -20,19 +20,42 @@ void Sound::load(std::unique_ptr res) { } void Sound::play() { - SoundSystem & system = SoundSystem::instance(); + SoundSystem & system = SoundSystem::get_instance(); if (system.engine.getPause(this->handle)) { // resume if paused system.engine.setPause(this->handle, false); } else { // or start new sound - this->handle = system.engine.play(this->sample); + this->handle = system.engine.play(this->sample, this->volume); + system.engine.setLooping(this->handle, this->looping); } } void Sound::pause() { - SoundSystem & system = SoundSystem::instance(); + SoundSystem & system = SoundSystem::get_instance(); if (system.engine.getPause(this->handle)) return; system.engine.setPause(this->handle, true); } +void Sound::rewind() { + SoundSystem & system = SoundSystem::get_instance(); + if (!system.engine.isValidVoiceHandle(this->handle)) return; + system.engine.seek(this->handle, 0); +} + +void Sound::set_volume(float volume) { + this->volume = volume; + + SoundSystem & system = SoundSystem::get_instance(); + if (!system.engine.isValidVoiceHandle(this->handle)) return; + system.engine.setVolume(this->handle, this->volume); +} + +void Sound::set_looping(bool looping) { + this->looping = looping; + + SoundSystem & system = SoundSystem::get_instance(); + if (!system.engine.isValidVoiceHandle(this->handle)) return; + system.engine.setLooping(this->handle, this->looping); +} + diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h index 9da17b9..4c51188 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/Sound.h @@ -45,12 +45,24 @@ public: * \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); @@ -62,6 +74,9 @@ private: private: SoLoud::Wav sample; SoLoud::handle handle; + + float volume = 1.0f; + bool looping = false; }; } diff --git a/src/crepe/SoundSystem.cpp b/src/crepe/SoundSystem.cpp index 00f874c..b57e51a 100644 --- a/src/crepe/SoundSystem.cpp +++ b/src/crepe/SoundSystem.cpp @@ -4,7 +4,7 @@ using namespace crepe; -SoundSystem & SoundSystem::instance() { +SoundSystem & SoundSystem::get_instance() { static SoundSystem instance; return instance; } diff --git a/src/crepe/SoundSystem.h b/src/crepe/SoundSystem.h index da3927a..34dd6c5 100644 --- a/src/crepe/SoundSystem.h +++ b/src/crepe/SoundSystem.h @@ -12,7 +12,7 @@ private: virtual ~SoundSystem(); // singleton - static SoundSystem & instance(); + static SoundSystem & get_instance(); SoundSystem(const SoundSystem &) = delete; SoundSystem(SoundSystem &&) = delete; SoundSystem &operator=(const SoundSystem &) = delete; -- cgit v1.2.3 From fe9bf1344ceae0fac9144e4a28fdcdbcb2267850 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 1 Oct 2024 17:23:33 +0200 Subject: rename SoundSystem to SoundContext (1/2) --- src/crepe/SoundContext.cpp | 21 +++++++++++++++++++++ src/crepe/SoundContext.h | 27 +++++++++++++++++++++++++++ src/crepe/SoundSystem.cpp | 21 --------------------- src/crepe/SoundSystem.h | 27 --------------------------- 4 files changed, 48 insertions(+), 48 deletions(-) create mode 100644 src/crepe/SoundContext.cpp create mode 100644 src/crepe/SoundContext.h delete mode 100644 src/crepe/SoundSystem.cpp delete mode 100644 src/crepe/SoundSystem.h diff --git a/src/crepe/SoundContext.cpp b/src/crepe/SoundContext.cpp new file mode 100644 index 0000000..b57e51a --- /dev/null +++ b/src/crepe/SoundContext.cpp @@ -0,0 +1,21 @@ +#include "util/log.h" + +#include "SoundSystem.h" + +using namespace crepe; + +SoundSystem & SoundSystem::get_instance() { + static SoundSystem instance; + return instance; +} + +SoundSystem::SoundSystem() { + dbg_trace(); + engine.init(); +} + +SoundSystem::~SoundSystem() { + dbg_trace(); + engine.deinit(); +} + diff --git a/src/crepe/SoundContext.h b/src/crepe/SoundContext.h new file mode 100644 index 0000000..34dd6c5 --- /dev/null +++ b/src/crepe/SoundContext.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include "Sound.h" + +namespace crepe { + +class SoundSystem { +private: + SoundSystem(); + virtual ~SoundSystem(); + + // singleton + static SoundSystem & get_instance(); + SoundSystem(const SoundSystem &) = delete; + SoundSystem(SoundSystem &&) = delete; + SoundSystem &operator=(const SoundSystem &) = delete; + SoundSystem &operator=(SoundSystem &&) = delete; + +private: + SoLoud::Soloud engine; + friend class Sound; +}; + +} + diff --git a/src/crepe/SoundSystem.cpp b/src/crepe/SoundSystem.cpp deleted file mode 100644 index b57e51a..0000000 --- a/src/crepe/SoundSystem.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "util/log.h" - -#include "SoundSystem.h" - -using namespace crepe; - -SoundSystem & SoundSystem::get_instance() { - static SoundSystem instance; - return instance; -} - -SoundSystem::SoundSystem() { - dbg_trace(); - engine.init(); -} - -SoundSystem::~SoundSystem() { - dbg_trace(); - engine.deinit(); -} - diff --git a/src/crepe/SoundSystem.h b/src/crepe/SoundSystem.h deleted file mode 100644 index 34dd6c5..0000000 --- a/src/crepe/SoundSystem.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include - -#include "Sound.h" - -namespace crepe { - -class SoundSystem { -private: - SoundSystem(); - virtual ~SoundSystem(); - - // singleton - static SoundSystem & get_instance(); - SoundSystem(const SoundSystem &) = delete; - SoundSystem(SoundSystem &&) = delete; - SoundSystem &operator=(const SoundSystem &) = delete; - SoundSystem &operator=(SoundSystem &&) = delete; - -private: - SoLoud::Soloud engine; - friend class Sound; -}; - -} - -- cgit v1.2.3 From 8e67adc6828181b1df7c95da76f32962444fc7fd Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 1 Oct 2024 17:25:06 +0200 Subject: rename SoundSystem to SoundContext (2/2) --- src/crepe/CMakeLists.txt | 4 ++-- src/crepe/Sound.cpp | 36 ++++++++++++++++++------------------ src/crepe/SoundContext.cpp | 10 +++++----- src/crepe/SoundContext.h | 16 ++++++++-------- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 9f7c91c..3a60840 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,11 +1,11 @@ target_sources(crepe PUBLIC Sound.cpp - SoundSystem.cpp + SoundContext.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES Sound.h - SoundSystem.h + SoundContext.h ) add_subdirectory(api) diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index 20f1787..1758282 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -1,7 +1,7 @@ #include "util/log.h" #include "Sound.h" -#include "SoundSystem.h" +#include "SoundContext.h" using namespace crepe; @@ -20,42 +20,42 @@ void Sound::load(std::unique_ptr res) { } void Sound::play() { - SoundSystem & system = SoundSystem::get_instance(); - if (system.engine.getPause(this->handle)) { + SoundContext & ctx = SoundContext::get_instance(); + if (ctx.engine.getPause(this->handle)) { // resume if paused - system.engine.setPause(this->handle, false); + ctx.engine.setPause(this->handle, false); } else { // or start new sound - this->handle = system.engine.play(this->sample, this->volume); - system.engine.setLooping(this->handle, this->looping); + this->handle = ctx.engine.play(this->sample, this->volume); + ctx.engine.setLooping(this->handle, this->looping); } } void Sound::pause() { - SoundSystem & system = SoundSystem::get_instance(); - if (system.engine.getPause(this->handle)) return; - system.engine.setPause(this->handle, true); + SoundContext & ctx = SoundContext::get_instance(); + if (ctx.engine.getPause(this->handle)) return; + ctx.engine.setPause(this->handle, true); } void Sound::rewind() { - SoundSystem & system = SoundSystem::get_instance(); - if (!system.engine.isValidVoiceHandle(this->handle)) return; - system.engine.seek(this->handle, 0); + 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; - SoundSystem & system = SoundSystem::get_instance(); - if (!system.engine.isValidVoiceHandle(this->handle)) return; - system.engine.setVolume(this->handle, this->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; - SoundSystem & system = SoundSystem::get_instance(); - if (!system.engine.isValidVoiceHandle(this->handle)) return; - system.engine.setLooping(this->handle, this->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/SoundContext.cpp b/src/crepe/SoundContext.cpp index b57e51a..cfb7d82 100644 --- a/src/crepe/SoundContext.cpp +++ b/src/crepe/SoundContext.cpp @@ -1,20 +1,20 @@ #include "util/log.h" -#include "SoundSystem.h" +#include "SoundContext.h" using namespace crepe; -SoundSystem & SoundSystem::get_instance() { - static SoundSystem instance; +SoundContext & SoundContext::get_instance() { + static SoundContext instance; return instance; } -SoundSystem::SoundSystem() { +SoundContext::SoundContext() { dbg_trace(); engine.init(); } -SoundSystem::~SoundSystem() { +SoundContext::~SoundContext() { dbg_trace(); engine.deinit(); } diff --git a/src/crepe/SoundContext.h b/src/crepe/SoundContext.h index 34dd6c5..d361d83 100644 --- a/src/crepe/SoundContext.h +++ b/src/crepe/SoundContext.h @@ -6,17 +6,17 @@ namespace crepe { -class SoundSystem { +class SoundContext { private: - SoundSystem(); - virtual ~SoundSystem(); + SoundContext(); + virtual ~SoundContext(); // singleton - static SoundSystem & get_instance(); - SoundSystem(const SoundSystem &) = delete; - SoundSystem(SoundSystem &&) = delete; - SoundSystem &operator=(const SoundSystem &) = delete; - SoundSystem &operator=(SoundSystem &&) = delete; + static SoundContext & get_instance(); + SoundContext(const SoundContext &) = delete; + SoundContext(SoundContext &&) = delete; + SoundContext &operator=(const SoundContext &) = delete; + SoundContext &operator=(SoundContext &&) = delete; private: SoLoud::Soloud engine; -- cgit v1.2.3 From 65eda52aa51017f6f7aad158c4f8b6e91054cf0d Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 1 Oct 2024 17:27:14 +0200 Subject: `make format` --- mwe/audio/soloud/main.cpp | 3 +-- mwe/dynlink/exec/main.c | 1 - src/crepe/Sound.cpp | 1 - src/crepe/Sound.h | 3 +-- src/crepe/SoundContext.cpp | 1 - src/crepe/SoundContext.h | 7 +++---- src/crepe/api/AudioSource.cpp | 5 +---- src/crepe/api/AudioSource.h | 3 +-- src/crepe/api/Component.h | 3 +-- src/crepe/api/Resource.cpp | 9 ++------- src/crepe/api/Resource.h | 5 ++--- src/crepe/util/color.h | 3 +-- src/crepe/util/log.cpp | 1 - src/crepe/util/log.h | 20 ++++++++++---------- src/dummy_audio.cpp | 3 +-- test/audio.cpp | 3 +-- 16 files changed, 25 insertions(+), 46 deletions(-) diff --git a/mwe/audio/soloud/main.cpp b/mwe/audio/soloud/main.cpp index 25ba003..50df0b7 100644 --- a/mwe/audio/soloud/main.cpp +++ b/mwe/audio/soloud/main.cpp @@ -38,8 +38,7 @@ int main() { this_thread::sleep_for(500ms); // play all samples simultaniously - for (unsigned i = 0; i < 3; i++) - soloud.play(sfx[i]); + for (unsigned i = 0; i < 3; i++) soloud.play(sfx[i]); this_thread::sleep_for(1000ms); // stop all audio and exit diff --git a/mwe/dynlink/exec/main.c b/mwe/dynlink/exec/main.c index ea55feb..ff884f2 100644 --- a/mwe/dynlink/exec/main.c +++ b/mwe/dynlink/exec/main.c @@ -5,4 +5,3 @@ int main() { return 0; } - diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index 1758282..e1150ac 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -58,4 +58,3 @@ void Sound::set_looping(bool looping) { 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 index 4c51188..ac93991 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/Sound.h @@ -79,5 +79,4 @@ private: bool looping = false; }; -} - +} // namespace crepe diff --git a/src/crepe/SoundContext.cpp b/src/crepe/SoundContext.cpp index cfb7d82..72047d2 100644 --- a/src/crepe/SoundContext.cpp +++ b/src/crepe/SoundContext.cpp @@ -18,4 +18,3 @@ SoundContext::~SoundContext() { dbg_trace(); engine.deinit(); } - diff --git a/src/crepe/SoundContext.h b/src/crepe/SoundContext.h index d361d83..090966d 100644 --- a/src/crepe/SoundContext.h +++ b/src/crepe/SoundContext.h @@ -15,13 +15,12 @@ private: static SoundContext & get_instance(); SoundContext(const SoundContext &) = delete; SoundContext(SoundContext &&) = delete; - SoundContext &operator=(const SoundContext &) = delete; - SoundContext &operator=(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 cbde79f..4d1b093 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -9,9 +9,7 @@ AudioSource::AudioSource(std::unique_ptr audio_clip) { this->_sound = std::make_unique(std::move(audio_clip)); } -void AudioSource::play() { - return this->play(false); -} +void AudioSource::play() { return this->play(false); } void AudioSource::play(bool looping) { this->_sound->set_looping(looping); @@ -22,4 +20,3 @@ void AudioSource::stop() { this->_sound->pause(); this->_sound->rewind(); } - diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 6a038be..4300c48 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -38,5 +38,4 @@ private: std::unique_ptr _sound; }; -} - +} // namespace crepe::api diff --git a/src/crepe/api/Component.h b/src/crepe/api/Component.h index 2abb461..d5e0499 100644 --- a/src/crepe/api/Component.h +++ b/src/crepe/api/Component.h @@ -7,5 +7,4 @@ public: bool active; }; -} - +} // namespace crepe::api diff --git a/src/crepe/api/Resource.cpp b/src/crepe/api/Resource.cpp index 6bb081d..1a647ce 100644 --- a/src/crepe/api/Resource.cpp +++ b/src/crepe/api/Resource.cpp @@ -9,11 +9,6 @@ Resource::Resource(const std::string & 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(); -} +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 index 2b62ff9..f2b2a0e 100644 --- a/src/crepe/api/Resource.h +++ b/src/crepe/api/Resource.h @@ -1,8 +1,8 @@ #pragma once -#include #include #include +#include namespace crepe::api { @@ -21,5 +21,4 @@ private: std::ifstream file; }; -} - +} // namespace crepe::api diff --git a/src/crepe/util/color.h b/src/crepe/util/color.h index 1af6c8f..066c9d3 100644 --- a/src/crepe/util/color.h +++ b/src/crepe/util/color.h @@ -38,5 +38,4 @@ constexpr const char * BG_MAGENTA_BRIGHT = "\e[105m"; constexpr const char * BG_CYAN_BRIGHT = "\e[106m"; constexpr const char * BG_WHITE_BRIGHT = "\e[107m"; -} - +} // namespace crepe::util::color diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp index 796df49..6829ec3 100644 --- a/src/crepe/util/log.cpp +++ b/src/crepe/util/log.cpp @@ -48,4 +48,3 @@ void crepe::util::logf(log_level level, const char * fmt, ...) { va_logf(level, args, fmt); va_end(args); } - diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index 5295cb9..4cab338 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -8,12 +8,9 @@ // utility macros #define _crepe_logf_here(fmt, ...) \ crepe::util::logf(util::log_level::debug, "%s%s (%s:%d)" fmt "\n", \ - crepe::util::color::FG_WHITE, \ - __PRETTY_FUNCTION__, \ - __FILE_NAME__, \ - __LINE__, \ - crepe::util::color::RESET, \ - __VA_ARGS__) + crepe::util::color::FG_WHITE, __PRETTY_FUNCTION__, \ + __FILE_NAME__, __LINE__, crepe::util::color::RESET, \ + __VA_ARGS__) #define dbg_logf(fmt, ...) _crepe_logf_here(": " fmt, __VA_ARGS__) #define dbg_log(str) _crepe_logf_here(": %s", str) @@ -23,11 +20,14 @@ namespace crepe::util { -enum log_level { debug, info, warning, error, }; +enum log_level { + debug, + info, + warning, + error, +}; void logf(const char * fmt, ...); void logf(enum log_level level, const char * fmt, ...); -} - - +} // namespace crepe::util diff --git a/src/dummy_audio.cpp b/src/dummy_audio.cpp index 1249076..049bb49 100644 --- a/src/dummy_audio.cpp +++ b/src/dummy_audio.cpp @@ -1,5 +1,5 @@ -#include "crepe/util/log.h" #include "crepe/Sound.h" +#include "crepe/util/log.h" #include #include @@ -38,4 +38,3 @@ int main() { return 0; } - diff --git a/test/audio.cpp b/test/audio.cpp index a415919..47c5e84 100644 --- a/test/audio.cpp +++ b/test/audio.cpp @@ -1,8 +1,8 @@ #include #include -#include #include +#include #include #include @@ -26,4 +26,3 @@ TEST(audio, play) { ASSERT_TRUE(true); } - -- cgit v1.2.3 From f8b2f9f3a2557313d37d53042dd36f0063f2ee61 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 1 Oct 2024 18:03:41 +0200 Subject: `make format` --- mwe/audio/soloud/main.cpp | 3 +- mwe/gameloop/include/gameObject.h | 54 +++++++++--------- mwe/gameloop/include/timer.h | 29 +++++----- mwe/gameloop/src/loopManager.cpp | 113 +++++++++++++++++++------------------- mwe/gameloop/src/main.cpp | 1 - mwe/gameloop/src/timer.cpp | 79 +++++++++++--------------- 6 files changed, 131 insertions(+), 148 deletions(-) diff --git a/mwe/audio/soloud/main.cpp b/mwe/audio/soloud/main.cpp index 25ba003..50df0b7 100644 --- a/mwe/audio/soloud/main.cpp +++ b/mwe/audio/soloud/main.cpp @@ -38,8 +38,7 @@ int main() { this_thread::sleep_for(500ms); // play all samples simultaniously - for (unsigned i = 0; i < 3; i++) - soloud.play(sfx[i]); + for (unsigned i = 0; i < 3; i++) soloud.play(sfx[i]); this_thread::sleep_for(1000ms); // stop all audio and exit diff --git a/mwe/gameloop/include/gameObject.h b/mwe/gameloop/include/gameObject.h index fc7d026..abdc9b0 100644 --- a/mwe/gameloop/include/gameObject.h +++ b/mwe/gameloop/include/gameObject.h @@ -1,30 +1,32 @@ #pragma once #include class GameObject { - public: - GameObject(); - GameObject(std::string name, float x, float y, float width, float height, float velX, float velY); - std::string getName() const; - float getX() const; - float getY() const; - float getWidth() const; - float getHeight() const; - float getVelX() const; - float getVelY() const; - void setName(std::string value); - void setX(float value); - void setY(float value); - void setWidth(float value); - void setHeight(float value); - void setVelX(float value); - void setVelY(float value); - int direction; - private: - std::string name = ""; - float x = 0; - float y = 0; - float width = 0; - float height = 0; - float velX = 0; - float velY = 0; +public: + GameObject(); + GameObject(std::string name, float x, float y, float width, float height, + float velX, float velY); + std::string getName() const; + float getX() const; + float getY() const; + float getWidth() const; + float getHeight() const; + float getVelX() const; + float getVelY() const; + void setName(std::string value); + void setX(float value); + void setY(float value); + void setWidth(float value); + void setHeight(float value); + void setVelX(float value); + void setVelY(float value); + int direction; + +private: + std::string name = ""; + float x = 0; + float y = 0; + float width = 0; + float height = 0; + float velX = 0; + float velY = 0; }; diff --git a/mwe/gameloop/include/timer.h b/mwe/gameloop/include/timer.h index ca1e5f3..8273746 100644 --- a/mwe/gameloop/include/timer.h +++ b/mwe/gameloop/include/timer.h @@ -4,29 +4,30 @@ class LoopTimer { public: - static LoopTimer& getInstance(); - void start(); - void update(); - double getDeltaTime() const; - int getCurrentTime() const; + static LoopTimer & getInstance(); + void start(); + void update(); + double getDeltaTime() const; + int getCurrentTime() const; void advanceFixedUpdate(); double getFixedDeltaTime() const; - void setFPS(int FPS); - int getFPS() const; + void setFPS(int FPS); + int getFPS() const; double getGameScale(); void setGameScale(double); - void enforceFrameRate(); + void enforceFrameRate(); double getLag() const; + private: - LoopTimer(); + LoopTimer(); int FPS = 50; double gameScale = 1; double maximumDeltaTime = 0.25; - double deltaTime; + double deltaTime; double frameTargetTime = FPS / 1000; - double fixedDeltaTime = 0.01; - double elapsedTime; - double elapsedFixedTime; + double fixedDeltaTime = 0.01; + double elapsedTime; + double elapsedFixedTime; double time; - uint64_t lastFrameTime; + uint64_t lastFrameTime; }; diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp index dde3cef..0392853 100644 --- a/mwe/gameloop/src/loopManager.cpp +++ b/mwe/gameloop/src/loopManager.cpp @@ -1,92 +1,89 @@ #include "loopManager.h" #include "timer.h" -LoopManager::LoopManager(){ -} -void LoopManager::processInput(){ +LoopManager::LoopManager() {} +void LoopManager::processInput() { SDL_Event event; SDL_PollEvent(&event); - switch(event.type){ + switch (event.type) { case SDL_QUIT: gameRunning = false; break; case SDL_KEYDOWN: - if(event.key.keysym.sym == SDLK_ESCAPE){ + if (event.key.keysym.sym == SDLK_ESCAPE) { gameRunning = false; - }else if(event.key.keysym.sym == SDLK_i){ - LoopTimer::getInstance().setGameScale(LoopTimer::getInstance().getGameScale() + 0.1); - }else if(event.key.keysym.sym == SDLK_k){ - LoopTimer::getInstance().setGameScale(LoopTimer::getInstance().getGameScale() - 0.1); + } else if (event.key.keysym.sym == SDLK_i) { + LoopTimer::getInstance().setGameScale( + LoopTimer::getInstance().getGameScale() + 0.1); + } else if (event.key.keysym.sym == SDLK_k) { + LoopTimer::getInstance().setGameScale( + LoopTimer::getInstance().getGameScale() - 0.1); } - - break; + + break; } } -void LoopManager::fixedUpdate(){ - fprintf(stderr,"fixed update\n"); -} +void LoopManager::fixedUpdate() { fprintf(stderr, "fixed update\n"); } void LoopManager::loop() { - LoopTimer& timer = LoopTimer::getInstance(); - timer.start(); + LoopTimer & timer = LoopTimer::getInstance(); + timer.start(); - while (gameRunning) { - timer.update(); + while (gameRunning) { + timer.update(); - while (timer.getLag() >= timer.getFixedDeltaTime()) { - processInput(); - fixedUpdate(); - timer.advanceFixedUpdate(); - } + while (timer.getLag() >= timer.getFixedDeltaTime()) { + processInput(); + fixedUpdate(); + timer.advanceFixedUpdate(); + } - update(); - render(); + update(); + render(); - timer.enforceFrameRate(); - } + timer.enforceFrameRate(); + } - window.destroyWindow(); + window.destroyWindow(); } -void LoopManager::setup(){ +void LoopManager::setup() { gameRunning = window.initWindow(); LoopTimer::getInstance().start(); LoopTimer::getInstance().setFPS(500); - - for(int i = 1; i < 3;i++){ - GameObject* square = new GameObject("square2",i*60,i*60,20,20,0,0); + + for (int i = 1; i < 3; i++) { + GameObject * square + = new GameObject("square2", i * 60, i * 60, 20, 20, 0, 0); objectList.push_back(square); } } -void LoopManager::render(){ - fprintf(stderr,"**********render********** \n"); - if(gameRunning){ +void LoopManager::render() { + fprintf(stderr, "**********render********** \n"); + if (gameRunning) { window.render(objectList); } } void LoopManager::update() { - fprintf(stderr, "********** normal update ********** \n"); - LoopTimer& timer = LoopTimer::getInstance(); - - float delta = timer.getDeltaTime(); - for (int i = 0; i < objectList.size(); i++) { - GameObject* obj = objectList[i]; - - // Move the object based on its direction - if (obj->direction == 1) { - obj->setX(obj->getX() + 50 * delta); - } else { - obj->setX(obj->getX() - 50 * delta); - } - + fprintf(stderr, "********** normal update ********** \n"); + LoopTimer & timer = LoopTimer::getInstance(); - if (obj->getX() > 500) { - obj->setX(500); - obj->direction = 0; // Switch direction to left - } else if (obj->getX() < 50) { - obj->setX(50); // Clamp the position to the boundary - obj->direction = 1; // Switch direction to right - } - } -} + float delta = timer.getDeltaTime(); + for (int i = 0; i < objectList.size(); i++) { + GameObject * obj = objectList[i]; + // Move the object based on its direction + if (obj->direction == 1) { + obj->setX(obj->getX() + 50 * delta); + } else { + obj->setX(obj->getX() - 50 * delta); + } + if (obj->getX() > 500) { + obj->setX(500); + obj->direction = 0; // Switch direction to left + } else if (obj->getX() < 50) { + obj->setX(50); // Clamp the position to the boundary + obj->direction = 1; // Switch direction to right + } + } +} diff --git a/mwe/gameloop/src/main.cpp b/mwe/gameloop/src/main.cpp index 9f407c4..c0f216a 100644 --- a/mwe/gameloop/src/main.cpp +++ b/mwe/gameloop/src/main.cpp @@ -5,7 +5,6 @@ #include "loopManager.h" #include "timer.h" - int main(int argc, char * args[]) { LoopManager gameLoop; gameLoop.setup(); diff --git a/mwe/gameloop/src/timer.cpp b/mwe/gameloop/src/timer.cpp index 03fb9eb..97baef6 100644 --- a/mwe/gameloop/src/timer.cpp +++ b/mwe/gameloop/src/timer.cpp @@ -1,68 +1,53 @@ #include "timer.h" -LoopTimer::LoopTimer(){} +LoopTimer::LoopTimer() {} -LoopTimer& LoopTimer::getInstance() { - static LoopTimer instance; - return instance; +LoopTimer & LoopTimer::getInstance() { + static LoopTimer instance; + return instance; } void LoopTimer::start() { - lastFrameTime = SDL_GetTicks64(); - elapsedTime = 0; - elapsedFixedTime = 0; - deltaTime = 0; + lastFrameTime = SDL_GetTicks64(); + elapsedTime = 0; + elapsedFixedTime = 0; + deltaTime = 0; } void LoopTimer::update() { - uint64_t currentFrameTime = SDL_GetTicks64(); - deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; + uint64_t currentFrameTime = SDL_GetTicks64(); + deltaTime = (currentFrameTime - lastFrameTime) / 1000.0; - if (deltaTime > maximumDeltaTime) { - deltaTime = maximumDeltaTime; - } + if (deltaTime > maximumDeltaTime) { + deltaTime = maximumDeltaTime; + } deltaTime *= gameScale; - elapsedTime += deltaTime; - lastFrameTime = currentFrameTime; + elapsedTime += deltaTime; + lastFrameTime = currentFrameTime; } -double LoopTimer::getDeltaTime() const { - return deltaTime; -} -int LoopTimer::getCurrentTime() const { - return SDL_GetTicks(); -} +double LoopTimer::getDeltaTime() const { return deltaTime; } +int LoopTimer::getCurrentTime() const { return SDL_GetTicks(); } -void LoopTimer::advanceFixedUpdate() { - elapsedFixedTime += fixedDeltaTime; -} +void LoopTimer::advanceFixedUpdate() { elapsedFixedTime += fixedDeltaTime; } -double LoopTimer::getFixedDeltaTime() const { - return fixedDeltaTime; -} +double LoopTimer::getFixedDeltaTime() const { return fixedDeltaTime; } void LoopTimer::setFPS(int FPS) { - this->FPS = FPS; - frameTargetTime = 1.0 / FPS; + this->FPS = FPS; + frameTargetTime = 1.0 / FPS; } -int LoopTimer::getFPS() const { - return FPS; -} -void LoopTimer::setGameScale(double value){ - gameScale = value; -}; -double LoopTimer::getGameScale(){ - return gameScale; -} +int LoopTimer::getFPS() const { return FPS; } +void LoopTimer::setGameScale(double value) { gameScale = value; }; +double LoopTimer::getGameScale() { return gameScale; } void LoopTimer::enforceFrameRate() { - uint64_t currentFrameTime = SDL_GetTicks64(); - double frameDuration = (currentFrameTime - lastFrameTime) / 1000.0; + uint64_t currentFrameTime = SDL_GetTicks64(); + double frameDuration = (currentFrameTime - lastFrameTime) / 1000.0; - if (frameDuration < frameTargetTime) { - uint32_t delayTime = (uint32_t)((frameTargetTime - frameDuration) * 1000.0); - SDL_Delay(delayTime); - } -} -double LoopTimer::getLag() const { - return elapsedTime - elapsedFixedTime; + if (frameDuration < frameTargetTime) { + uint32_t delayTime + = (uint32_t) ((frameTargetTime - frameDuration) * 1000.0); + SDL_Delay(delayTime); + } } +double LoopTimer::getLag() const { return elapsedTime - elapsedFixedTime; } -- cgit v1.2.3 From 4ec01101ddc04f4f30d0aa0b4f3f71fa74e0e6f3 Mon Sep 17 00:00:00 2001 From: Max-001 <80035972+Max-001@users.noreply.github.com> Date: Sat, 5 Oct 2024 12:25:10 +0200 Subject: Added some time testing for comparison --- .vscode/settings.json | 6 ++- mwe/ecs-homemade/inc/Components.h | 6 ++- mwe/ecs-homemade/src/Components.cpp | 4 +- mwe/ecs-homemade/src/main.cpp | 104 +++++++++--------------------------- 4 files changed, 36 insertions(+), 84 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 52c6107..27ae565 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -54,6 +54,10 @@ "cinttypes": "cpp", "typeindex": "cpp", "typeinfo": "cpp", - "variant": "cpp" + "variant": "cpp", + "chrono": "cpp", + "ratio": "cpp", + "iomanip": "cpp", + "sstream": "cpp" } } diff --git a/mwe/ecs-homemade/inc/Components.h b/mwe/ecs-homemade/inc/Components.h index 2645a45..98c5fe7 100644 --- a/mwe/ecs-homemade/inc/Components.h +++ b/mwe/ecs-homemade/inc/Components.h @@ -1,5 +1,7 @@ #pragma once +#include + class Component { public: Component(); @@ -9,7 +11,9 @@ public: class Sprite : public Component { public: - void Render(); + Sprite(std::string path); + + std::string mPath; }; class Rigidbody : public Component { diff --git a/mwe/ecs-homemade/src/Components.cpp b/mwe/ecs-homemade/src/Components.cpp index 5f10bc5..69b5eaa 100644 --- a/mwe/ecs-homemade/src/Components.cpp +++ b/mwe/ecs-homemade/src/Components.cpp @@ -3,9 +3,7 @@ Component::Component() : mActive(true) {} -void Sprite::Render() { - std::cout << "Rendering sprite" << std::endl; -} +Sprite::Sprite(std::string path) : mPath(path) {} Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) : mMass(mass), mGravityScale(gravityScale), mBodyType(bodyType) {} diff --git a/mwe/ecs-homemade/src/main.cpp b/mwe/ecs-homemade/src/main.cpp index 9e230e5..122311a 100644 --- a/mwe/ecs-homemade/src/main.cpp +++ b/mwe/ecs-homemade/src/main.cpp @@ -1,108 +1,54 @@ #include #include #include +#include #include "ComponentManager.h" #include "GameObjectMax.h" #include "Components.h" int main() { - GameObject gameObect0(0, "Name: 0", "Tag: 0", 0); //Entity 0 - GameObject gameObect1(1, "Name: 1", "Tag: 1", 1); //Entity 1 - GameObject gameObect2(2, "Name: 2", "Tag: 2", 2); //Entity 2 - GameObject gameObect3(3, "Name: 3", "Tag: 3", 3); //Entity 3 - GameObject gameObect4(4, "Name: 4", "Tag: 4", 4); //Entity 4 - GameObject gameObect5(5, "Name: 5", "Tag: 5", 5); //Entity 5 - GameObject gameObect6(6, "Name: 6", "Tag: 6", 6); //Entity 6 - GameObject gameObect7(7, "Name: 7", "Tag: 7", 7); //Entity 7 + auto startAdding = std::chrono::high_resolution_clock::now(); - gameObect0.AddComponent(); //Add a sprite to entity0 - gameObect0.AddComponent(1, 2, 3); //Also add a rigidbody to entity0 - gameObect0.AddComponent(3, 2, 1); //Add a second rigidbody to entity0 + GameObject* gameObject[100000]; - gameObect1.AddComponent(4, 5, 6); //Only add a rigidbody to entity1 + for(int i = 0; i < 100000; ++i) { + gameObject[i] = new GameObject(i, "Name", "Tag", 0); - gameObect2.AddComponent(); //Add four sprites to entity2 - gameObect2.AddComponent(); - gameObect2.AddComponent(); - gameObect2.AddComponent(); - gameObect2.AddComponent(10, 100, 500); //Add four rigidbodies to entity2 - gameObect2.AddComponent(10, 100, 501); - gameObect2.AddComponent(10, 100, 502); - gameObect2.AddComponent(10, 100, 500); - - //Add non components to entity3, entity4, entity5 and entity6 - - gameObect7.AddComponent(); //Add a sprite to entity 7 - gameObect7.AddComponent(30); //Add a colder to entity 7 - - //The entities are now initialized - //Now I will demonstrate some ways of retreiving/getting components - - std::cout << "Finding all sprites of entity 0" << std::endl; - std::vector> spriteOfEntity0 = ComponentManager::GetInstance().GetComponentsByID(gameObect0.mId); - for(Sprite& spriteEntity0 : spriteOfEntity0) { - std::cout << "Sprite of entity 0: " << spriteEntity0.mActive << std::endl; - } - std::cout << std::endl; - - std::cout << "Finding all rigidbodies of entity 0" << std::endl; - std::vector> rigidbodyOfEntity0 = ComponentManager::GetInstance().GetComponentsByID(gameObect0.mId); - for(Rigidbody& rigidbodyEntity0 : rigidbodyOfEntity0) { - std::cout << "Rigidbody of entity 0: " << rigidbodyEntity0.mMass << " " << rigidbodyEntity0.mGravityScale << " " << rigidbodyEntity0.mBodyType << std::endl; - - rigidbodyEntity0.mMass = 15; - } - std::cout << std::endl; - - std::cout << "Finding all coliders of entity 0" << std::endl; - std::vector> coliderOfEntity0 = ComponentManager::GetInstance().GetComponentsByID(gameObect0.mId); - for(Colider& coliderEntity0 : coliderOfEntity0) { - std::cout << "Colider of entity 0: " << coliderEntity0.mSize << std::endl; - } - std::cout << std::endl; - - std::cout << "Finding all sprites of entity 3" << std::endl; - std::vector> spriteOfEntity3 = ComponentManager::GetInstance().GetComponentsByID(gameObect3.mId); - for(Sprite& spriteEntity3 : spriteOfEntity3) { - std::cout << "Sprite of entity 3: " << spriteEntity3.mActive << std::endl; + gameObject[i]->AddComponent("C:/Test"); + gameObject[i]->AddComponent(0, 0, i); + gameObject[i]->AddComponent(i); } - std::cout << std::endl; + + auto stopAdding = std::chrono::high_resolution_clock::now(); - std::cout << "Finding all rigidbodies of entity 3" << std::endl; - std::vector> rigidbodyOfEntity3 = ComponentManager::GetInstance().GetComponentsByID(gameObect3.mId); - for(Rigidbody& rigidbodyEntity3 : rigidbodyOfEntity3) { - std::cout << "Rigidbody of entity 3: " << rigidbodyEntity3.mMass << " " << rigidbodyEntity3.mGravityScale << " " << rigidbodyEntity3.mBodyType << std::endl; - } - std::cout << std::endl; + //This is what systems would do: - std::cout << "Finding all sprites of all entities" << std::endl; std::vector, std::uint32_t>> sprites = ComponentManager::GetInstance().GetComponentsByType(); for(auto& [sprite, id] : sprites) { - std::cout << "Sprite of id: " << id << ": " << sprite.get().mActive << std::endl; + std::cout << sprite.get().mPath << std::endl; } std::cout << std::endl; - std::cout << "Finding all coliders of all entities" << std::endl; + std::vector, std::uint32_t>> rigidBodies = ComponentManager::GetInstance().GetComponentsByType(); + for(auto& [rigidbody, id] : rigidBodies) { + std::cout << rigidbody.get().mMass << " " << rigidbody.get().mGravityScale << " " << rigidbody.get().mBodyType << std::endl; + } + std::vector, std::uint32_t>> coliders = ComponentManager::GetInstance().GetComponentsByType(); for(auto& [colider, id] : coliders) { - std::cout << "Colder of id: " << id << ": " << colider.get().mSize << std::endl; + std::cout << colider.get().mSize << std::endl; } std::cout << std::endl; - std::cout << "Finding all rigidbodies of all entities" << std::endl; - std::vector, std::uint32_t>> rigidBodies = ComponentManager::GetInstance().GetComponentsByType(); - for(auto& [rigidbody, id] : rigidBodies) { - std::cout << "Rigidbody of id: " << id << ": " << rigidbody.get().mMass << " " << rigidbody.get().mGravityScale << " " << rigidbody.get().mBodyType << std::endl; + auto stopLooping = std::chrono::high_resolution_clock::now(); - rigidbody.get().mMass = -1; + for (int i = 0; i < 100000; ++i) { + delete gameObject[i]; } - std::cout << std::endl; - std::cout << "Finding all rigidbodies of all entities for the second time (after changing mMass to -1)" << std::endl; - std::vector, std::uint32_t>> rigidBodies2 = ComponentManager::GetInstance().GetComponentsByType(); - for(auto& [rigidbody2, id2] : rigidBodies2) { - std::cout << "Rigidbody of id: " << id2 << ": " << rigidbody2.get().mMass << " " << rigidbody2.get().mGravityScale << " " << rigidbody2.get().mBodyType << std::endl; - } - std::cout << std::endl; + auto Addtime = std::chrono::duration_cast(stopAdding - startAdding); + auto LoopTime = std::chrono::duration_cast(stopLooping - stopAdding); + std::cout << "AddTime: " << Addtime.count() << " us" << std::endl; + std::cout << "LoopTime: " << LoopTime.count() << " us" << std::endl; } -- cgit v1.2.3 From a5fa49f39473a8d2dc535145cb34866967ec10ab Mon Sep 17 00:00:00 2001 From: Max-001 <80035972+Max-001@users.noreply.github.com> Date: Sat, 5 Oct 2024 12:31:50 +0200 Subject: Improved test --- mwe/ecs-homemade/src/main.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mwe/ecs-homemade/src/main.cpp b/mwe/ecs-homemade/src/main.cpp index 122311a..c78e085 100644 --- a/mwe/ecs-homemade/src/main.cpp +++ b/mwe/ecs-homemade/src/main.cpp @@ -26,20 +26,20 @@ int main() { std::vector, std::uint32_t>> sprites = ComponentManager::GetInstance().GetComponentsByType(); for(auto& [sprite, id] : sprites) { - std::cout << sprite.get().mPath << std::endl; + //std::cout << sprite.get().mPath << std::endl; } - std::cout << std::endl; + //std::cout << std::endl; std::vector, std::uint32_t>> rigidBodies = ComponentManager::GetInstance().GetComponentsByType(); for(auto& [rigidbody, id] : rigidBodies) { - std::cout << rigidbody.get().mMass << " " << rigidbody.get().mGravityScale << " " << rigidbody.get().mBodyType << std::endl; + //std::cout << rigidbody.get().mMass << " " << rigidbody.get().mGravityScale << " " << rigidbody.get().mBodyType << std::endl; } - + //std::cout << std::endl; + std::vector, std::uint32_t>> coliders = ComponentManager::GetInstance().GetComponentsByType(); for(auto& [colider, id] : coliders) { - std::cout << colider.get().mSize << std::endl; + //std::cout << colider.get().mSize << std::endl; } - std::cout << std::endl; auto stopLooping = std::chrono::high_resolution_clock::now(); -- cgit v1.2.3 From 765550bce8a81c6f0c79c0083b14ef68e0c900b2 Mon Sep 17 00:00:00 2001 From: Max-001 <80035972+Max-001@users.noreply.github.com> Date: Sat, 5 Oct 2024 12:36:26 +0200 Subject: Removed the std::pair to improve test --- mwe/ecs-homemade/inc/ComponentManager.h | 2 +- mwe/ecs-homemade/inc/ComponentManager.hpp | 10 +++++----- mwe/ecs-homemade/src/main.cpp | 14 +++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/mwe/ecs-homemade/inc/ComponentManager.h b/mwe/ecs-homemade/inc/ComponentManager.h index 893aa56..1a58b01 100644 --- a/mwe/ecs-homemade/inc/ComponentManager.h +++ b/mwe/ecs-homemade/inc/ComponentManager.h @@ -29,7 +29,7 @@ public: template std::vector> GetComponentsByID(std::uint32_t id) const; //Get a vector<> of all components at specific type and id template - std::vector, std::uint32_t>> GetComponentsByType() const; //Get a vector<> of all components of a specific type + std::vector> GetComponentsByType() const; //Get a vector<> of all components of a specific type private: static ComponentManager mInstance; //Singleton diff --git a/mwe/ecs-homemade/inc/ComponentManager.hpp b/mwe/ecs-homemade/inc/ComponentManager.hpp index 53dfddd..720ee79 100644 --- a/mwe/ecs-homemade/inc/ComponentManager.hpp +++ b/mwe/ecs-homemade/inc/ComponentManager.hpp @@ -61,11 +61,11 @@ std::vector> ComponentManager::GetComponentsByID(std:: } template -std::vector, std::uint32_t>> ComponentManager::GetComponentsByType() const { +std::vector> ComponentManager::GetComponentsByType() const { std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>) - std::vector, std::uint32_t>> componentVector; //Create an empty vector<> - std::uint32_t id = 0; //Set the id to 0 (the id will also be stored in the returned vector<>) + std::vector> componentVector; //Create an empty vector<> + //std::uint32_t id = 0; //Set the id to 0 (the id will also be stored in the returned vector<>) if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>) @@ -76,11 +76,11 @@ std::vector, std::uint32_t>> ComponentManage T* castedComponent = static_cast(componentPtr.get()); //Cast the unique_ptr to a raw pointer if (castedComponent) { //Ensure that the cast was successful - componentVector.emplace_back(std::ref(*castedComponent), id); //Pair the dereferenced raw pointer and the id and add it to the vector<> + componentVector.emplace_back(std::ref(*castedComponent)); //Pair the dereferenced raw pointer and the id and add it to the vector<> } } - ++id; //Increase the id (the id will also be stored in the returned vector<>) + //++id; //Increase the id (the id will also be stored in the returned vector<>) } } diff --git a/mwe/ecs-homemade/src/main.cpp b/mwe/ecs-homemade/src/main.cpp index c78e085..41f7d6d 100644 --- a/mwe/ecs-homemade/src/main.cpp +++ b/mwe/ecs-homemade/src/main.cpp @@ -24,20 +24,20 @@ int main() { //This is what systems would do: - std::vector, std::uint32_t>> sprites = ComponentManager::GetInstance().GetComponentsByType(); - for(auto& [sprite, id] : sprites) { + std::vector> sprites = ComponentManager::GetInstance().GetComponentsByType(); + for(Sprite& sprite : sprites) { //std::cout << sprite.get().mPath << std::endl; } //std::cout << std::endl; - std::vector, std::uint32_t>> rigidBodies = ComponentManager::GetInstance().GetComponentsByType(); - for(auto& [rigidbody, id] : rigidBodies) { + std::vector> rigidBodies = ComponentManager::GetInstance().GetComponentsByType(); + for(Rigidbody& rigidbody : rigidBodies) { //std::cout << rigidbody.get().mMass << " " << rigidbody.get().mGravityScale << " " << rigidbody.get().mBodyType << std::endl; } //std::cout << std::endl; - - std::vector, std::uint32_t>> coliders = ComponentManager::GetInstance().GetComponentsByType(); - for(auto& [colider, id] : coliders) { + + std::vector> coliders = ComponentManager::GetInstance().GetComponentsByType(); + for(Colider& colider : coliders) { //std::cout << colider.get().mSize << std::endl; } -- cgit v1.2.3 From f4bb6d57cc88a7e25b3a5f43faafa49a7f500b7c Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 5 Oct 2024 14:16:57 +0200 Subject: restructure unit tests and dummies --- readme.md | 3 ++- src/CMakeLists.txt | 11 +++++++--- src/crepe/Sound.cpp | 1 + src/dummy_audio.cpp | 40 ------------------------------------ src/example/CMakeLists.txt | 3 +++ src/example/audio_internal.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++ src/readme.md | 8 ++++++++ src/test/CMakeLists.txt | 5 +++++ src/test/audio.cpp | 29 ++++++++++++++++++++++++++ src/test/dummy.cpp | 4 ++++ test/CMakeLists.txt | 23 --------------------- test/audio.cpp | 28 ------------------------- test/dummy.cpp | 3 --- 13 files changed, 106 insertions(+), 98 deletions(-) delete mode 100644 src/dummy_audio.cpp create mode 100644 src/example/CMakeLists.txt create mode 100644 src/example/audio_internal.cpp create mode 100644 src/readme.md create mode 100644 src/test/CMakeLists.txt create mode 100644 src/test/audio.cpp create mode 100644 src/test/dummy.cpp delete mode 100644 test/CMakeLists.txt delete mode 100644 test/audio.cpp delete mode 100644 test/dummy.cpp diff --git a/readme.md b/readme.md index f3aab09..5351dfe 100644 --- a/readme.md +++ b/readme.md @@ -7,7 +7,8 @@ This repository contains: |`lib/`|third-party libraries as git submodules| |`mwe/`|minimal working examples and proof-of-concepts| |`src/crepe/`|game engine source code| -|`test/`|game engine unit tests| +|`src/test/`|unit tests| +|`src/example`|standalone examples using game engine| ## Compilation diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 232d330..62ca9a0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,10 +9,12 @@ set(CMAKE_BUILD_TYPE Debug) add_compile_definitions(DEBUG) add_subdirectory(../lib/soloud soloud) +add_subdirectory(../lib/googletest googletest) project(crepe C CXX) add_library(crepe SHARED) +add_executable(test_main EXCLUDE_FROM_ALL) target_include_directories(crepe PUBLIC SYSTEM INTERFACE . @@ -24,13 +26,16 @@ target_link_libraries(crepe ) add_subdirectory(crepe) +add_subdirectory(test) +add_subdirectory(example) install( TARGETS crepe FILE_SET HEADERS DESTINATION include/crepe ) - -add_executable(dummy_audio dummy_audio.cpp) -target_link_libraries(dummy_audio PUBLIC crepe) +target_link_libraries(test_main + PRIVATE gtest_main + PUBLIC crepe +) diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index e1150ac..1758282 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -58,3 +58,4 @@ void Sound::set_looping(bool looping) { if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.setLooping(this->handle, this->looping); } + diff --git a/src/dummy_audio.cpp b/src/dummy_audio.cpp deleted file mode 100644 index 049bb49..0000000 --- a/src/dummy_audio.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "crepe/Sound.h" -#include "crepe/util/log.h" - -#include -#include - -using namespace crepe; -using namespace std; -using namespace std::chrono_literals; -using std::make_unique; - -int main() { - dbg_trace(); - - auto bgm = Sound("../mwe/audio/bgm.ogg"); - auto sfx1 = Sound("../mwe/audio/sfx1.wav"); - auto sfx2 = Sound("../mwe/audio/sfx2.wav"); - auto sfx3 = 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/src/example/CMakeLists.txt b/src/example/CMakeLists.txt new file mode 100644 index 0000000..bcc9271 --- /dev/null +++ b/src/example/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable(audio_internal EXCLUDE_FROM_ALL audio_internal.cpp) +target_link_libraries(audio_internal PUBLIC crepe) + diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp new file mode 100644 index 0000000..09bd55c --- /dev/null +++ b/src/example/audio_internal.cpp @@ -0,0 +1,46 @@ +/** \file + * + * Standalone example for usage of the internal \c Sound class. + */ + +#include +#include + +#include +#include + +using namespace crepe; +using namespace std; +using namespace std::chrono_literals; +using std::make_unique; + +int main() { + dbg_trace(); + + auto bgm = Sound("../mwe/audio/bgm.ogg"); + auto sfx1 = Sound("../mwe/audio/sfx1.wav"); + auto sfx2 = Sound("../mwe/audio/sfx2.wav"); + auto sfx3 = 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/src/readme.md b/src/readme.md new file mode 100644 index 0000000..1c5d3a2 --- /dev/null +++ b/src/readme.md @@ -0,0 +1,8 @@ +# engine source + +This folder contains the crêpe engine source files, unit tests, and some toy +examples. The only target built by default by the CMakeLists.txt in this folder +is the crêpe shared library object. Unit tests can be built by explicitly +specifying the target `test_main` when running the build command. Each source +file in the example/ folder corresponds to a CMake target as well. + diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt new file mode 100644 index 0000000..0d316d6 --- /dev/null +++ b/src/test/CMakeLists.txt @@ -0,0 +1,5 @@ +target_sources(test_main PUBLIC + dummy.cpp + # audio.cpp +) + diff --git a/src/test/audio.cpp b/src/test/audio.cpp new file mode 100644 index 0000000..5bb2607 --- /dev/null +++ b/src/test/audio.cpp @@ -0,0 +1,29 @@ +#include +#include + +#include +#include + +#include +#include + +using namespace std; +using namespace std::chrono_literals; + +using namespace crepe::api; + +// TODO: mock internal audio class + +TEST(audio, play) { + auto res = std::make_unique("../mwe/audio/bgm.ogg"); + auto bgm = AudioSource(std::move(res)); + + bgm.play(); + + this_thread::sleep_for(2s); + + bgm.stop(); + + ASSERT_TRUE(true); +} + diff --git a/src/test/dummy.cpp b/src/test/dummy.cpp new file mode 100644 index 0000000..7f4c083 --- /dev/null +++ b/src/test/dummy.cpp @@ -0,0 +1,4 @@ +#include + +TEST(dummy, foo) { ASSERT_TRUE(1); } + diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index f015570..0000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,23 +0,0 @@ -cmake_minimum_required(VERSION 3.28) - -set(CMAKE_C_STANDARD 11) -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_EXPORT_COMPILE_COMMANDS 1) - -set(CMAKE_BUILD_TYPE Debug) - -project(test C CXX) - -add_subdirectory(../lib/googletest googletest) -add_subdirectory(../src crepe) - -add_executable(test - dummy.cpp - # audio.cpp -) - -target_link_libraries(test - PRIVATE gtest_main - PUBLIC crepe # TODO: this does not work properly -) - diff --git a/test/audio.cpp b/test/audio.cpp deleted file mode 100644 index 47c5e84..0000000 --- a/test/audio.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -#include -#include - -#include -#include - -using namespace std; -using namespace std::chrono_literals; - -using namespace crepe::api; - -// TODO: mock internal audio class - -TEST(audio, play) { - auto res = std::make_unique("../mwe/audio/bgm.ogg"); - auto bgm = AudioSource(std::move(res)); - - bgm.play(); - - this_thread::sleep_for(2s); - - bgm.stop(); - - ASSERT_TRUE(true); -} diff --git a/test/dummy.cpp b/test/dummy.cpp deleted file mode 100644 index a00a9c6..0000000 --- a/test/dummy.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include - -TEST(dummy, foo) { ASSERT_TRUE(1); } -- 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 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 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 c40332b990b025d2bd7b94e41184d7f0547da203 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 5 Oct 2024 14:33:43 +0200 Subject: add doxygen comments to Asset class --- src/crepe/Asset.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/crepe/Asset.h b/src/crepe/Asset.h index 736ba44..0cb5834 100644 --- a/src/crepe/Asset.h +++ b/src/crepe/Asset.h @@ -6,8 +6,17 @@ namespace crepe { +/** + * \brief Asset location helper + * + * This class is used to locate and canonicalize paths to game asset files, and + * should *always* be used when retrieving files from disk. + */ class Asset { public: + /** + * \param src Unique identifier to asset + */ Asset(const std::string & src); public: -- cgit v1.2.3 From e54ebc7e74f3ca94de3467a03397c0aad2aed079 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 5 Oct 2024 14:50:49 +0200 Subject: `make format` + clang-tidy --- makefile | 3 +-- src/crepe/Sound.cpp | 1 - src/crepe/api/AudioSource.h | 2 +- src/crepe/util/log.h | 3 +++ src/example/audio_internal.cpp | 1 - src/makefile | 6 ++++++ src/test/audio.cpp | 23 ++--------------------- src/test/dummy.cpp | 1 - 8 files changed, 13 insertions(+), 27 deletions(-) create mode 100644 src/makefile diff --git a/makefile b/makefile index 737e7a8..dd7c587 100644 --- a/makefile +++ b/makefile @@ -6,6 +6,5 @@ doxygen: Doxyfile FORCE FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') format: FORCE clang-format -i $(FMT) -# clang tidy doesn't work that well :/ -# clang-tidy --fix-errors $(FMT) + $(MAKE) -C src $@ diff --git a/src/crepe/Sound.cpp b/src/crepe/Sound.cpp index 73ad69c..64fa281 100644 --- a/src/crepe/Sound.cpp +++ b/src/crepe/Sound.cpp @@ -58,4 +58,3 @@ void Sound::set_looping(bool looping) { if (!ctx.engine.isValidVoiceHandle(this->handle)) return; ctx.engine.setLooping(this->handle, this->looping); } - diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 9dfaf46..2002d1a 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -2,8 +2,8 @@ #include -#include "Component.h" #include "Asset.h" +#include "Component.h" namespace crepe { class Sound; diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index 4cab338..bfe7291 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -12,9 +12,12 @@ __FILE_NAME__, __LINE__, crepe::util::color::RESET, \ __VA_ARGS__) +// very illegal global function-style macros +// NOLINTBEGIN #define dbg_logf(fmt, ...) _crepe_logf_here(": " fmt, __VA_ARGS__) #define dbg_log(str) _crepe_logf_here(": %s", str) #define dbg_trace() _crepe_logf_here("%s", "") +// NOLINTEND #endif diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp index 09bd55c..1199e2d 100644 --- a/src/example/audio_internal.cpp +++ b/src/example/audio_internal.cpp @@ -43,4 +43,3 @@ int main() { return 0; } - diff --git a/src/makefile b/src/makefile new file mode 100644 index 0000000..c1ef601 --- /dev/null +++ b/src/makefile @@ -0,0 +1,6 @@ +.PHONY: FORCE + +FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') +format: FORCE + clang-tidy -p build/compile_commands.json --fix-errors $(FMT) + diff --git a/src/test/audio.cpp b/src/test/audio.cpp index 1d84551..d6ff689 100644 --- a/src/test/audio.cpp +++ b/src/test/audio.cpp @@ -1,29 +1,10 @@ #include -#include - -#include -#include - -#include -#include using namespace std; using namespace std::chrono_literals; -using namespace crepe::api; +// using namespace crepe; // TODO: mock internal audio class -TEST(audio, play) { - auto res = std::make_unique("../mwe/audio/bgm.ogg"); - auto bgm = AudioSource(std::move(res)); - - bgm.play(); - - this_thread::sleep_for(2s); - - bgm.stop(); - - ASSERT_TRUE(true); -} - +TEST(audio, play) { ASSERT_TRUE(true); } diff --git a/src/test/dummy.cpp b/src/test/dummy.cpp index 7f4c083..a00a9c6 100644 --- a/src/test/dummy.cpp +++ b/src/test/dummy.cpp @@ -1,4 +1,3 @@ #include TEST(dummy, foo) { ASSERT_TRUE(1); } - -- cgit v1.2.3 From b5a5d9b1d4725f3001486c9bbd69263ed0918303 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 5 Oct 2024 14:57:26 +0200 Subject: update code style --- contributing.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/contributing.md b/contributing.md index eec5bc0..2ff411a 100644 --- a/contributing.md +++ b/contributing.md @@ -22,6 +22,14 @@ - When using libraries of which the header include order is important, make sure to separate the include statements using a blank line (clang-format may sort include statements, but does not sort across empty lines). +- All engine-related code is implemented under the `crepe` namespace, + user-facing APIs under `crepe::api` (the folder structure should also reflect + this). +- `using namespace` may not be used in header files, only in source files. +- Do not (indirectly) include private dependency headers in API header files, + as these are no longer accessible when the engine is installed +- Getter and setter functions are appropriately prefixed with `get_` and + `set_`. ## CMakeLists specific -- cgit v1.2.3 From 7c9d3452c99fcb89ea6df55755e90f741b23cf10 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 6 Oct 2024 13:22:10 +0200 Subject: copy homemade ECS to crepe + correct code style --- .clang-format | 1 + contributing.md | 5 + mwe/ecs-homemade/inc/ComponentManager.h | 43 +++++---- mwe/ecs-homemade/inc/ComponentManager.hpp | 154 +++++++++++++++++++----------- mwe/ecs-homemade/inc/GameObjectMax.h | 3 +- mwe/ecs-homemade/inc/GameObjectMax.hpp | 5 +- mwe/ecs-homemade/src/ComponentManager.cpp | 20 ++-- mwe/ecs-homemade/src/Components.cpp | 3 +- mwe/ecs-homemade/src/GameObjectMax.cpp | 4 +- mwe/ecs-homemade/src/main.cpp | 35 ++++--- src/crepe/CMakeLists.txt | 8 ++ src/crepe/ComponentManager.cpp | 25 +++++ src/crepe/ComponentManager.h | 55 +++++++++++ src/crepe/ComponentManager.hpp | 134 ++++++++++++++++++++++++++ src/crepe/Components.cpp | 13 +++ src/crepe/Components.h | 39 ++++++++ src/crepe/GameObject.cpp | 9 ++ src/crepe/GameObject.h | 22 +++++ src/crepe/GameObject.hpp | 15 +++ 19 files changed, 487 insertions(+), 106 deletions(-) create mode 100644 src/crepe/ComponentManager.cpp create mode 100644 src/crepe/ComponentManager.h create mode 100644 src/crepe/ComponentManager.hpp create mode 100644 src/crepe/Components.cpp create mode 100644 src/crepe/Components.h create mode 100644 src/crepe/GameObject.cpp create mode 100644 src/crepe/GameObject.h create mode 100644 src/crepe/GameObject.hpp diff --git a/.clang-format b/.clang-format index 5d233eb..d2918db 100644 --- a/.clang-format +++ b/.clang-format @@ -22,6 +22,7 @@ AlignTrailingComments: ReflowComments: false AlignEscapedNewlines: DontAlign BreakBeforeBinaryOperators: All +BreakTemplateDeclarations: Yes ... # vim: ft=yaml diff --git a/contributing.md b/contributing.md index 2ff411a..933f1bf 100644 --- a/contributing.md +++ b/contributing.md @@ -17,6 +17,11 @@ # Code style - ASCII only +- Explanatory comments are placed above the line(s) they are explaining +- Source files should only contain comments that plainly state what the code is + supposed to do +- Explanatory comments in headers may be used to clarify implementation design + decisions - Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make format` in the root folder of this repository to format all sources files) - When using libraries of which the header include order is important, make diff --git a/mwe/ecs-homemade/inc/ComponentManager.h b/mwe/ecs-homemade/inc/ComponentManager.h index 1a58b01..d368516 100644 --- a/mwe/ecs-homemade/inc/ComponentManager.h +++ b/mwe/ecs-homemade/inc/ComponentManager.h @@ -2,39 +2,44 @@ #include "Components.h" #include -#include -#include -#include #include +#include +#include #include +#include class ComponentManager { public: - static ComponentManager& GetInstance(); //Singleton + static ComponentManager & GetInstance(); //Singleton - ComponentManager(const ComponentManager&) = delete; //Singleton - ComponentManager(ComponentManager&&) = delete; //Singleton - ComponentManager& operator=(const ComponentManager&) = delete; //Singleton - ComponentManager& operator=(ComponentManager&&) = delete; //Singleton + ComponentManager(const ComponentManager &) = delete; //Singleton + ComponentManager(ComponentManager &&) = delete; //Singleton + ComponentManager & operator=(const ComponentManager &) = delete; //Singleton + ComponentManager & operator=(ComponentManager &&) = delete; //Singleton template - void AddComponent(std::uint32_t id, Args&&... args); //Add a component of a specific type + void AddComponent(std::uint32_t id, + Args &&... args); //Add a component of a specific type template - void DeleteComponentsById(std::uint32_t id); //Deletes all components of a specific type and id + void DeleteComponentsById( + std::uint32_t id); //Deletes all components of a specific type and id template - void DeleteComponents(); //Deletes all components of a specific type - void DeleteAllComponentsOfId(std::uint32_t id); //Deletes all components of a specific id - void DeleteAllComponents(); //Deletes all components + void DeleteComponents(); //Deletes all components of a specific type + void DeleteAllComponentsOfId( + std::uint32_t id); //Deletes all components of a specific id + void DeleteAllComponents(); //Deletes all components template - std::vector> GetComponentsByID(std::uint32_t id) const; //Get a vector<> of all components at specific type and id + std::vector> GetComponentsByID(std::uint32_t id) + const; //Get a vector<> of all components at specific type and id template - std::vector> GetComponentsByType() const; //Get a vector<> of all components of a specific type + std::vector> GetComponentsByType() + const; //Get a vector<> of all components of a specific type private: - static ComponentManager mInstance; //Singleton + static ComponentManager mInstance; //Singleton - ComponentManager(); //Singleton + ComponentManager(); //Singleton /* * The std::unordered_map>>> below might seem a bit strange, let me explain this structure: @@ -42,7 +47,9 @@ private: * The first std::vector<> stores another vector<>. This first vector<> is to bind the entity's id to a component. * The second std::vector<> stores unique_ptrs. Each component can be gathered via an unique_ptr. This second vector<> allows multiple components of the same std::type_index for one entity (id). */ - std::unordered_map>>> mComponents; + std::unordered_map>>> + mComponents; }; #include "ComponentManager.hpp" diff --git a/mwe/ecs-homemade/inc/ComponentManager.hpp b/mwe/ecs-homemade/inc/ComponentManager.hpp index 720ee79..161ce71 100644 --- a/mwe/ecs-homemade/inc/ComponentManager.hpp +++ b/mwe/ecs-homemade/inc/ComponentManager.hpp @@ -1,88 +1,126 @@ template -void ComponentManager::AddComponent(std::uint32_t id, Args&&... args) { - std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>) - - if (mComponents.find(type) == mComponents.end()) { //Check if this component type is already in the unordered_map<> - mComponents[type] = std::vector>>(); //If not, create a new (empty) vector<> of vector> - } +void ComponentManager::AddComponent(std::uint32_t id, Args &&... args) { + std::type_index type = typeid( + T); //Determine the type of T (this is used as the key of the unordered_map<>) + + if (mComponents.find(type) + == mComponents + .end()) { //Check if this component type is already in the unordered_map<> + mComponents[type] = std::vector>>(); //If not, create a new (empty) vector<> of vector> + } - if (id >= mComponents[type].size()) { //Resize the vector<> if the id is greater than the current size - mComponents[type].resize(id + 1); //Initialize new slots to nullptr (resize does automatically init to nullptr) - } + if (id + >= mComponents[type] + .size()) { //Resize the vector<> if the id is greater than the current size + mComponents[type].resize( + id + + 1); //Initialize new slots to nullptr (resize does automatically init to nullptr) + } - mComponents[type][id].push_back(std::make_unique(std::forward(args)...)); //Create a new component of type T using perfect forwarding and store its unique_ptr in the vector<> + mComponents[type][id].push_back(std::make_unique(std::forward( + args)...)); //Create a new component of type T using perfect forwarding and store its unique_ptr in the vector<> } template void ComponentManager::DeleteComponentsById(std::uint32_t id) { - std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>) - - if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>) - std::vector>>& componentArray = mComponents[type]; //Get the correct vector<> - - if (id < componentArray.size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<> - componentArray[id].clear(); //Clear the whole vector<> of this specific type and id - } + std::type_index type = typeid( + T); //Determine the type of T (this is used as the key of the unordered_map<>) + + if (mComponents.find(type) + != mComponents.end()) { //Find the type (in the unordered_map<>) + std::vector>> & componentArray + = mComponents[type]; //Get the correct vector<> + + if (id + < componentArray + .size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<> + componentArray[id] + .clear(); //Clear the whole vector<> of this specific type and id + } } } -template -void ComponentManager::DeleteComponents() { - std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>) +template void ComponentManager::DeleteComponents() { + std::type_index type = typeid( + T); //Determine the type of T (this is used as the key of the unordered_map<>) - if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>) - mComponents[type].clear(); //Clear the whole vector<> of this specific type + if (mComponents.find(type) + != mComponents.end()) { //Find the type (in the unordered_map<>) + mComponents[type] + .clear(); //Clear the whole vector<> of this specific type } } template -std::vector> ComponentManager::GetComponentsByID(std::uint32_t id) const { - std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>) - - std::vector> componentVector; //Create an empty vector<> - - if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>) - - const std::vector>>& componentArray = mComponents.at(type); //Get the correct vector<> - - if (id < componentArray.size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<> - for (const std::unique_ptr& componentPtr : componentArray[id]) { //Loop trough the whole vector<> - T* castedComponent = static_cast(componentPtr.get()); //Cast the unique_ptr to a raw pointer - - if (castedComponent) { //Ensure that the cast was successful - componentVector.push_back(*castedComponent); //Add the dereferenced raw pointer to the vector<> - } - } - } - } +std::vector> +ComponentManager::GetComponentsByID(std::uint32_t id) const { + std::type_index type = typeid( + T); //Determine the type of T (this is used as the key of the unordered_map<>) + + std::vector> + componentVector; //Create an empty vector<> + + if (mComponents.find(type) + != mComponents.end()) { //Find the type (in the unordered_map<>) + + const std::vector>> & + componentArray + = mComponents.at(type); //Get the correct vector<> + + if (id + < componentArray + .size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<> + for (const std::unique_ptr & componentPtr : + componentArray[id]) { //Loop trough the whole vector<> + T * castedComponent = static_cast( + componentPtr.get()); //Cast the unique_ptr to a raw pointer + + if (castedComponent) { //Ensure that the cast was successful + componentVector.push_back( + *castedComponent); //Add the dereferenced raw pointer to the vector<> + } + } + } + } - return componentVector; //Return the vector<> + return componentVector; //Return the vector<> } template -std::vector> ComponentManager::GetComponentsByType() const { - std::type_index type = typeid(T); //Determine the type of T (this is used as the key of the unordered_map<>) +std::vector> +ComponentManager::GetComponentsByType() const { + std::type_index type = typeid( + T); //Determine the type of T (this is used as the key of the unordered_map<>) - std::vector> componentVector; //Create an empty vector<> + std::vector> + componentVector; //Create an empty vector<> //std::uint32_t id = 0; //Set the id to 0 (the id will also be stored in the returned vector<>) - if (mComponents.find(type) != mComponents.end()) { //Find the type (in the unordered_map<>) + if (mComponents.find(type) + != mComponents.end()) { //Find the type (in the unordered_map<>) - const std::vector>>& componentArray = mComponents.at(type); //Get the correct vector<> + const std::vector>> & + componentArray + = mComponents.at(type); //Get the correct vector<> - for (const std::vector>& component : componentArray) { //Loop through the whole vector<> - for (const std::unique_ptr& componentPtr : component) { //Loop trough the whole vector<> - T* castedComponent = static_cast(componentPtr.get()); //Cast the unique_ptr to a raw pointer + for (const std::vector> & component : + componentArray) { //Loop through the whole vector<> + for (const std::unique_ptr & componentPtr : + component) { //Loop trough the whole vector<> + T * castedComponent = static_cast( + componentPtr.get()); //Cast the unique_ptr to a raw pointer - if (castedComponent) { //Ensure that the cast was successful - componentVector.emplace_back(std::ref(*castedComponent)); //Pair the dereferenced raw pointer and the id and add it to the vector<> - } - } + if (castedComponent) { //Ensure that the cast was successful + componentVector.emplace_back(std::ref( + *castedComponent)); //Pair the dereferenced raw pointer and the id and add it to the vector<> + } + } //++id; //Increase the id (the id will also be stored in the returned vector<>) - } - } + } + } - return componentVector; //Return the vector<> + return componentVector; //Return the vector<> } diff --git a/mwe/ecs-homemade/inc/GameObjectMax.h b/mwe/ecs-homemade/inc/GameObjectMax.h index f0bcec9..c88a834 100644 --- a/mwe/ecs-homemade/inc/GameObjectMax.h +++ b/mwe/ecs-homemade/inc/GameObjectMax.h @@ -7,8 +7,7 @@ class GameObject { public: GameObject(std::uint32_t id, std::string name, std::string tag, int layer); - template - void AddComponent(Args&&... args); + template void AddComponent(Args &&... args); std::uint32_t mId; std::string mName; diff --git a/mwe/ecs-homemade/inc/GameObjectMax.hpp b/mwe/ecs-homemade/inc/GameObjectMax.hpp index 1e952ba..91d51ea 100644 --- a/mwe/ecs-homemade/inc/GameObjectMax.hpp +++ b/mwe/ecs-homemade/inc/GameObjectMax.hpp @@ -1,6 +1,7 @@ #include "ComponentManager.h" template -void GameObject::AddComponent(Args&&... args) { - ComponentManager::GetInstance().AddComponent(mId, std::forward(args)...); +void GameObject::AddComponent(Args &&... args) { + ComponentManager::GetInstance().AddComponent( + mId, std::forward(args)...); } diff --git a/mwe/ecs-homemade/src/ComponentManager.cpp b/mwe/ecs-homemade/src/ComponentManager.cpp index 16cc2b6..536c152 100644 --- a/mwe/ecs-homemade/src/ComponentManager.cpp +++ b/mwe/ecs-homemade/src/ComponentManager.cpp @@ -2,20 +2,22 @@ ComponentManager ComponentManager::mInstance; -ComponentManager& ComponentManager::GetInstance() { - return mInstance; -} +ComponentManager & ComponentManager::GetInstance() { return mInstance; } ComponentManager::ComponentManager() {} void ComponentManager::DeleteAllComponentsOfId(std::uint32_t id) { - for(auto& [type, componentArray] : mComponents) { //Loop through all the types (in the unordered_map<>) - if (id < componentArray.size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<> - componentArray[id].clear(); //Clear the components at this specific id - } - } + for (auto & [type, componentArray] : + mComponents) { //Loop through all the types (in the unordered_map<>) + if (id + < componentArray + .size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<> + componentArray[id] + .clear(); //Clear the components at this specific id + } + } } void ComponentManager::DeleteAllComponents() { - mComponents.clear(); //Clear the whole unordered_map<> + mComponents.clear(); //Clear the whole unordered_map<> } diff --git a/mwe/ecs-homemade/src/Components.cpp b/mwe/ecs-homemade/src/Components.cpp index 69b5eaa..c8347b3 100644 --- a/mwe/ecs-homemade/src/Components.cpp +++ b/mwe/ecs-homemade/src/Components.cpp @@ -5,6 +5,7 @@ Component::Component() : mActive(true) {} Sprite::Sprite(std::string path) : mPath(path) {} -Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) : mMass(mass), mGravityScale(gravityScale), mBodyType(bodyType) {} +Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) + : mMass(mass), mGravityScale(gravityScale), mBodyType(bodyType) {} Colider::Colider(int size) : mSize(size) {} diff --git a/mwe/ecs-homemade/src/GameObjectMax.cpp b/mwe/ecs-homemade/src/GameObjectMax.cpp index 62c41de..b0c5af7 100644 --- a/mwe/ecs-homemade/src/GameObjectMax.cpp +++ b/mwe/ecs-homemade/src/GameObjectMax.cpp @@ -2,4 +2,6 @@ #include "ComponentManager.h" -GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, int layer) : mId(id), mName(name), mTag(tag), mActive(true), mLayer(layer) {} +GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, + int layer) + : mId(id), mName(name), mTag(tag), mActive(true), mLayer(layer) {} diff --git a/mwe/ecs-homemade/src/main.cpp b/mwe/ecs-homemade/src/main.cpp index 41f7d6d..330e154 100644 --- a/mwe/ecs-homemade/src/main.cpp +++ b/mwe/ecs-homemade/src/main.cpp @@ -1,54 +1,59 @@ +#include +#include #include #include -#include -#include #include "ComponentManager.h" -#include "GameObjectMax.h" #include "Components.h" +#include "GameObjectMax.h" int main() { auto startAdding = std::chrono::high_resolution_clock::now(); - GameObject* gameObject[100000]; + GameObject * gameObject[100000]; - for(int i = 0; i < 100000; ++i) { + for (int i = 0; i < 100000; ++i) { gameObject[i] = new GameObject(i, "Name", "Tag", 0); gameObject[i]->AddComponent("C:/Test"); gameObject[i]->AddComponent(0, 0, i); gameObject[i]->AddComponent(i); } - + auto stopAdding = std::chrono::high_resolution_clock::now(); //This is what systems would do: - std::vector> sprites = ComponentManager::GetInstance().GetComponentsByType(); - for(Sprite& sprite : sprites) { + std::vector> sprites + = ComponentManager::GetInstance().GetComponentsByType(); + for (Sprite & sprite : sprites) { //std::cout << sprite.get().mPath << std::endl; } //std::cout << std::endl; - std::vector> rigidBodies = ComponentManager::GetInstance().GetComponentsByType(); - for(Rigidbody& rigidbody : rigidBodies) { + std::vector> rigidBodies + = ComponentManager::GetInstance().GetComponentsByType(); + for (Rigidbody & rigidbody : rigidBodies) { //std::cout << rigidbody.get().mMass << " " << rigidbody.get().mGravityScale << " " << rigidbody.get().mBodyType << std::endl; } //std::cout << std::endl; - std::vector> coliders = ComponentManager::GetInstance().GetComponentsByType(); - for(Colider& colider : coliders) { + std::vector> coliders + = ComponentManager::GetInstance().GetComponentsByType(); + for (Colider & colider : coliders) { //std::cout << colider.get().mSize << std::endl; } auto stopLooping = std::chrono::high_resolution_clock::now(); for (int i = 0; i < 100000; ++i) { - delete gameObject[i]; + delete gameObject[i]; } - auto Addtime = std::chrono::duration_cast(stopAdding - startAdding); - auto LoopTime = std::chrono::duration_cast(stopLooping - stopAdding); + auto Addtime = std::chrono::duration_cast( + stopAdding - startAdding); + auto LoopTime = std::chrono::duration_cast( + stopLooping - stopAdding); std::cout << "AddTime: " << Addtime.count() << " us" << std::endl; std::cout << "LoopTime: " << LoopTime.count() << " us" << std::endl; } diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 208ba1f..5840208 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -2,12 +2,20 @@ target_sources(crepe PUBLIC Asset.cpp Sound.cpp SoundContext.cpp + ComponentManager.cpp + Components.cpp + GameObject.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES Asset.h Sound.h SoundContext.h + ComponentManager.h + ComponentManager.hpp + Components.h + GameObject.h + GameObject.hpp ) add_subdirectory(api) diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp new file mode 100644 index 0000000..b7a1bea --- /dev/null +++ b/src/crepe/ComponentManager.cpp @@ -0,0 +1,25 @@ +#include "ComponentManager.h" + +using namespace crepe; + +ComponentManager & ComponentManager::get_instance() { + static ComponentManager instance; + return instance; +} + +void ComponentManager::DeleteAllComponentsOfId(std::uint32_t id) { + // Loop through all the types (in the unordered_map<>) + for (auto & [type, componentArray] : components) { + // Make sure that the id (that we are looking for) is within the boundaries of the vector<> + if (id < componentArray.size()) { + // Clear the components at this specific id + componentArray[id].clear(); + } + } +} + +void ComponentManager::DeleteAllComponents() { + // Clear the whole unordered_map<> + components.clear(); +} + diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h new file mode 100644 index 0000000..003cd0b --- /dev/null +++ b/src/crepe/ComponentManager.h @@ -0,0 +1,55 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "Components.h" + +namespace crepe { + +class ComponentManager { +public: + // Singleton + static ComponentManager & get_instance(); + ComponentManager(const ComponentManager &) = delete; + ComponentManager(ComponentManager &&) = delete; + ComponentManager & operator=(const ComponentManager &) = delete; + ComponentManager & operator=(ComponentManager &&) = delete; + +public: + //! Add a component of a specific type + template void AddComponent(std::uint32_t id, Args &&... args); + //! Deletes all components of a specific type and id + template void DeleteComponentsById(std::uint32_t id); + //! Deletes all components of a specific type + template void DeleteComponents(); + //! Deletes all components of a specific id + void DeleteAllComponentsOfId(std::uint32_t id); + //! Deletes all components + void DeleteAllComponents(); + + //! Get a vector<> of all components at specific type and id + template std::vector> GetComponentsByID(std::uint32_t id) const; + //! Get a vector<> of all components of a specific type + template std::vector> GetComponentsByType() const; + +private: + ComponentManager() = default; + + /* + * The std::unordered_map>>> below might seem a bit strange, let me explain this structure: + * The std::unordered_map<> has a key and value. The key is a std::type_index and the value is a std::vector. So, a new std::vector will be created for each new std::type_index. + * The first std::vector<> stores another vector<>. This first vector<> is to bind the entity's id to a component. + * The second std::vector<> stores unique_ptrs. Each component can be gathered via an unique_ptr. This second vector<> allows multiple components of the same std::type_index for one entity (id). + */ + std::unordered_map>>> components; +}; + +} + +// #include "ComponentManager.hpp" + diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp new file mode 100644 index 0000000..602490e --- /dev/null +++ b/src/crepe/ComponentManager.hpp @@ -0,0 +1,134 @@ +#pragma once + +#include "ComponentManager.h" + +namespace crepe { + +template +void ComponentManager::AddComponent(std::uint32_t id, Args &&... args) { + // Determine the type of T (this is used as the key of the unordered_map<>) + std::type_index type = typeid(T); + + // Check if this component type is already in the unordered_map<> + if (components.find(type) == components.end()) { + //If not, create a new (empty) vector<> of vector> + components[type] = std::vector>>(); + } + + // Resize the vector<> if the id is greater than the current size + if (id >= components[type].size()) { + // Initialize new slots to nullptr (resize does automatically init to nullptr) + 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(std::make_unique(std::forward(args)...)); +} + +template +void ComponentManager::DeleteComponentsById(std::uint32_t id) { + // Determine the type of T (this is used as the key of the unordered_map<>) + std::type_index type = typeid(T); + + // Find the type (in the unordered_map<>) + if (components.find(type) != components.end()) { + // Get the correct vector<> + std::vector>> & componentArray = components[type]; + + // Make sure that the id (that we are looking for) is within the boundaries of the vector<> + if (id < componentArray.size()) { + // Clear the whole vector<> of this specific type and id + componentArray[id].clear(); + } + } +} + +template +void ComponentManager::DeleteComponents() { + // Determine the type of T (this is used as the key of the unordered_map<>) + std::type_index type = typeid(T); + + // Find the type (in the unordered_map<>) + if (components.find(type) != components.end()) { + // Clear the whole vector<> of this specific type + components[type].clear(); + } +} + +template +std::vector> +ComponentManager::GetComponentsByID(std::uint32_t id) const { + // Determine the type of T (this is used as the key of the unordered_map<>) + std::type_index type = typeid(T); + + // Create an empty vector<> + std::vector> componentVector; + + // Find the type (in the unordered_map<>) + if (components.find(type) != components.end()) { + // Get the correct vector<> + const std::vector>> & componentArray = components.at(type); + + // Make sure that the id (that we are looking for) is within the boundaries of the vector<> + if (id < componentArray .size()) { + // Loop trough the whole vector<> + for (const std::unique_ptr & componentPtr : componentArray[id]) { + // Cast the unique_ptr to a raw pointer + T * castedComponent = static_cast(componentPtr.get()); + + // Ensure that the cast was successful + if (castedComponent) { + // Add the dereferenced raw pointer to the vector<> + componentVector.push_back(*castedComponent); + } + } + } + } + + // Return the vector<> + return componentVector; +} + +template +std::vector> +ComponentManager::GetComponentsByType() const { + // Determine the type of T (this is used as the key of the unordered_map<>) + std::type_index type = typeid(T); + + // Create an empty vector<> + std::vector> componentVector; + // Set the id to 0 (the id will also be stored in the returned vector<>) + // std::uint32_t id = 0; + + // Find the type (in the unordered_map<>) + if (components.find(type) != components.end()) { + + // Get the correct vector<> + const std::vector>> & componentArray = components.at(type); + + // Loop through the whole vector<> + for (const std::vector> & component : componentArray) { + // Loop trough the whole vector<> + for (const std::unique_ptr & componentPtr : component) { + // Cast the unique_ptr to a raw pointer + T * castedComponent = static_cast(componentPtr.get()); + + // Ensure that the cast was successful + if (castedComponent) { + // Pair the dereferenced raw pointer and the id and add it to the vector<> + componentVector.emplace_back(std::ref(*castedComponent)); + } + } + + // Increase the id (the id will also be stored in the returned vector<>) + //++id; + } + } + + // Return the vector<> + return componentVector; +} + +} + diff --git a/src/crepe/Components.cpp b/src/crepe/Components.cpp new file mode 100644 index 0000000..9760daa --- /dev/null +++ b/src/crepe/Components.cpp @@ -0,0 +1,13 @@ +#include "Components.h" +#include + +using namespace crepe; + +Component::Component() : mActive(true) {} + +Sprite::Sprite(std::string path) : mPath(path) {} + +Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) + : mMass(mass), mGravityScale(gravityScale), mBodyType(bodyType) {} + +Colider::Colider(int size) : mSize(size) {} diff --git a/src/crepe/Components.h b/src/crepe/Components.h new file mode 100644 index 0000000..7cb6fbb --- /dev/null +++ b/src/crepe/Components.h @@ -0,0 +1,39 @@ +#pragma once + +#include + +namespace crepe { + +class Component { +public: + Component(); + + bool mActive; +}; + +// TODO: these should be in separate files + +class Sprite : public Component { +public: + Sprite(std::string path); + + std::string mPath; +}; + +class Rigidbody : public Component { +public: + Rigidbody(int mass, int gravityScale, int bodyType); + + int mMass; + int mGravityScale; + int mBodyType; +}; + +class Colider : public Component { +public: + Colider(int size); + + int mSize; +}; + +} diff --git a/src/crepe/GameObject.cpp b/src/crepe/GameObject.cpp new file mode 100644 index 0000000..5ac9d7a --- /dev/null +++ b/src/crepe/GameObject.cpp @@ -0,0 +1,9 @@ +#include "GameObject.h" + +#include "ComponentManager.h" + +using namespace crepe; + +GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, + int layer) + : mId(id), mName(name), mTag(tag), mActive(true), mLayer(layer) {} diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h new file mode 100644 index 0000000..71ef60d --- /dev/null +++ b/src/crepe/GameObject.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +namespace crepe { + +class GameObject { +public: + GameObject(std::uint32_t id, std::string name, std::string tag, int layer); + + template void AddComponent(Args &&... args); + + std::uint32_t mId; + std::string mName; + std::string mTag; + bool mActive; + int mLayer; +}; + +} + diff --git a/src/crepe/GameObject.hpp b/src/crepe/GameObject.hpp new file mode 100644 index 0000000..5a672cf --- /dev/null +++ b/src/crepe/GameObject.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "GameObject.h" + +#include "ComponentManager.h" + +namespace crepe { + +template +void GameObject::AddComponent(Args &&... args) { + ComponentManager::get_instance().AddComponent(mId, std::forward(args)...); +} + +} + -- cgit v1.2.3 From 509fb6ebdd27bc75375c1c51024ea906e25032c0 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 6 Oct 2024 13:45:05 +0200 Subject: WIP more code style enforcements --- .clang-format | 2 +- .clang-tidy | 43 +++++++++++++++++++++---------- .editorconfig | 2 +- mwe/ecs-homemade/inc/ComponentManager.hpp | 3 ++- mwe/ecs-homemade/inc/GameObjectMax.h | 3 ++- src/crepe/ComponentManager.cpp | 5 ++-- src/crepe/ComponentManager.h | 27 ++++++++++++------- src/crepe/ComponentManager.hpp | 38 ++++++++++++++++----------- src/crepe/Components.cpp | 9 ++++--- src/crepe/Components.h | 18 ++++++------- src/crepe/GameObject.cpp | 2 +- src/crepe/GameObject.h | 16 ++++++------ src/crepe/GameObject.hpp | 6 ++--- src/crepe/api/AudioSource.h | 2 +- 14 files changed, 104 insertions(+), 72 deletions(-) diff --git a/.clang-format b/.clang-format index d2918db..3ae6c28 100644 --- a/.clang-format +++ b/.clang-format @@ -22,7 +22,7 @@ AlignTrailingComments: ReflowComments: false AlignEscapedNewlines: DontAlign BreakBeforeBinaryOperators: All -BreakTemplateDeclarations: Yes +AlwaysBreakTemplateDeclarations: Yes ... # vim: ft=yaml diff --git a/.clang-tidy b/.clang-tidy index b242e3f..0ec997d 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,18 +1,33 @@ Checks: '-*,readability-identifier-naming' CheckOptions: - - { key: readability-identifier-naming.EnumCase, value: lower_case } - - { key: readability-identifier-naming.GlobalFunctionCase, value: lower_case } - - { key: readability-identifier-naming.ClassCase, value: CamelCase } - - { key: readability-identifier-naming.ClassMethodCase, value: lower_case } - - { key: readability-identifier-naming.ClassMethodPrefix, value: '' } - - { key: readability-identifier-naming.ClassMemberCase, value: lower_case } - - { key: readability-identifier-naming.ClassMemberPrefix, value: '' } - - { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE } - - { key: readability-identifier-naming.GlobalConstantIgnoredRegexp, value: _.* } - - { key: readability-identifier-naming.GlobalVariableCase, value: lower_case } - - { key: readability-identifier-naming.GlobalVariableIgnoredRegexp, value: _.* } - - { key: readability-identifier-naming.MacroDefinitionCase, value: UPPER_CASE } - - { key: readability-identifier-naming.MacroDefinitionIgnoredRegexp, value: _.* } - - { key: readability-identifier-naming.StructCase, value: lower_case } + - key: 'readability-identifier-naming.EnumCase' + value: 'lower_case' + - key: 'readability-identifier-naming.GlobalFunctionCase' + value: 'lower_case' + - key: 'readability-identifier-naming.ClassCase' + value: 'CamelCase' + - key: 'readability-identifier-naming.MethodCase' + value: 'lower_case' + - key: 'readability-identifier-naming.MethodPrefix' + value: '' + - key: 'readability-identifier-naming.MemberCase' + value: 'lower_case' + - key: 'readability-identifier-naming.MemberPrefix' + value: '' + - key: 'readability-identifier-naming.GlobalConstantCase' + value: 'UPPER_CASE' + - key: 'readability-identifier-naming.GlobalConstantIgnoredRegexp' + value: '_.*' + - key: 'readability-identifier-naming.GlobalVariableCase' + value: 'lower_case' + - key: 'readability-identifier-naming.GlobalVariableIgnoredRegexp' + value: '_.*' + - key: 'readability-identifier-naming.MacroDefinitionCase' + value: 'UPPER_CASE' + - key: 'readability-identifier-naming.MacroDefinitionIgnoredRegexp' + value: '_.*' + - key: 'readability-identifier-naming.StructCase' + value: 'lower_case' # vim: ft=yaml + diff --git a/.editorconfig b/.editorconfig index 1bd7da9..df2cf5e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,7 +5,7 @@ indent_style = tab end_of_line = lf insert_final_newline = true -[*.{md,yml}] +[{*.{md,yml},.clang-*}] indent_style = space indent_size = 2 diff --git a/mwe/ecs-homemade/inc/ComponentManager.hpp b/mwe/ecs-homemade/inc/ComponentManager.hpp index 161ce71..d6da8e8 100644 --- a/mwe/ecs-homemade/inc/ComponentManager.hpp +++ b/mwe/ecs-homemade/inc/ComponentManager.hpp @@ -42,7 +42,8 @@ void ComponentManager::DeleteComponentsById(std::uint32_t id) { } } -template void ComponentManager::DeleteComponents() { +template +void ComponentManager::DeleteComponents() { std::type_index type = typeid( T); //Determine the type of T (this is used as the key of the unordered_map<>) diff --git a/mwe/ecs-homemade/inc/GameObjectMax.h b/mwe/ecs-homemade/inc/GameObjectMax.h index c88a834..3029053 100644 --- a/mwe/ecs-homemade/inc/GameObjectMax.h +++ b/mwe/ecs-homemade/inc/GameObjectMax.h @@ -7,7 +7,8 @@ class GameObject { public: GameObject(std::uint32_t id, std::string name, std::string tag, int layer); - template void AddComponent(Args &&... args); + template + void AddComponent(Args &&... args); std::uint32_t mId; std::string mName; diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index b7a1bea..b080c9d 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -7,7 +7,7 @@ ComponentManager & ComponentManager::get_instance() { return instance; } -void ComponentManager::DeleteAllComponentsOfId(std::uint32_t id) { +void ComponentManager::delete_all_components_of_id(std::uint32_t id) { // Loop through all the types (in the unordered_map<>) for (auto & [type, componentArray] : components) { // Make sure that the id (that we are looking for) is within the boundaries of the vector<> @@ -18,8 +18,7 @@ void ComponentManager::DeleteAllComponentsOfId(std::uint32_t id) { } } -void ComponentManager::DeleteAllComponents() { +void ComponentManager::delete_all_components() { // Clear the whole unordered_map<> components.clear(); } - diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 003cd0b..b88ad27 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -22,20 +22,26 @@ public: public: //! Add a component of a specific type - template void AddComponent(std::uint32_t id, Args &&... args); + template + void add_component(std::uint32_t id, Args &&... args); //! Deletes all components of a specific type and id - template void DeleteComponentsById(std::uint32_t id); + template + void delete_components_by_id(std::uint32_t id); //! Deletes all components of a specific type - template void DeleteComponents(); + template + void delete_components(); //! Deletes all components of a specific id - void DeleteAllComponentsOfId(std::uint32_t id); + void delete_all_components_of_id(std::uint32_t id); //! Deletes all components - void DeleteAllComponents(); + void delete_all_components(); //! Get a vector<> of all components at specific type and id - template std::vector> GetComponentsByID(std::uint32_t id) const; + template + std::vector> + get_components_by_id(std::uint32_t id) const; //! Get a vector<> of all components of a specific type - template std::vector> GetComponentsByType() const; + template + std::vector> get_components_by_type() const; private: ComponentManager() = default; @@ -46,10 +52,11 @@ private: * The first std::vector<> stores another vector<>. This first vector<> is to bind the entity's id to a component. * The second std::vector<> stores unique_ptrs. Each component can be gathered via an unique_ptr. This second vector<> allows multiple components of the same std::type_index for one entity (id). */ - std::unordered_map>>> components; + std::unordered_map>>> + components; }; -} +} // namespace crepe // #include "ComponentManager.hpp" - diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 602490e..30b0e38 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -5,14 +5,15 @@ namespace crepe { template -void ComponentManager::AddComponent(std::uint32_t id, Args &&... args) { +void ComponentManager::add_component(std::uint32_t id, Args &&... args) { // Determine the type of T (this is used as the key of the unordered_map<>) std::type_index type = typeid(T); // Check if this component type is already in the unordered_map<> if (components.find(type) == components.end()) { //If not, create a new (empty) vector<> of vector> - components[type] = std::vector>>(); + components[type] + = std::vector>>(); } // Resize the vector<> if the id is greater than the current size @@ -23,18 +24,20 @@ void ComponentManager::AddComponent(std::uint32_t id, Args &&... args) { // Create a new component of type T using perfect forwarding and store its // unique_ptr in the vector<> - components[type][id].push_back(std::make_unique(std::forward(args)...)); + components[type][id].push_back( + std::make_unique(std::forward(args)...)); } template -void ComponentManager::DeleteComponentsById(std::uint32_t id) { +void ComponentManager::delete_components_by_id(std::uint32_t id) { // Determine the type of T (this is used as the key of the unordered_map<>) std::type_index type = typeid(T); // Find the type (in the unordered_map<>) if (components.find(type) != components.end()) { // Get the correct vector<> - std::vector>> & componentArray = components[type]; + std::vector>> & componentArray + = components[type]; // Make sure that the id (that we are looking for) is within the boundaries of the vector<> if (id < componentArray.size()) { @@ -45,7 +48,7 @@ void ComponentManager::DeleteComponentsById(std::uint32_t id) { } template -void ComponentManager::DeleteComponents() { +void ComponentManager::delete_components() { // Determine the type of T (this is used as the key of the unordered_map<>) std::type_index type = typeid(T); @@ -58,7 +61,7 @@ void ComponentManager::DeleteComponents() { template std::vector> -ComponentManager::GetComponentsByID(std::uint32_t id) const { +ComponentManager::get_components_by_id(std::uint32_t id) const { // Determine the type of T (this is used as the key of the unordered_map<>) std::type_index type = typeid(T); @@ -68,12 +71,15 @@ ComponentManager::GetComponentsByID(std::uint32_t id) const { // Find the type (in the unordered_map<>) if (components.find(type) != components.end()) { // Get the correct vector<> - const std::vector>> & componentArray = components.at(type); + const std::vector>> & + componentArray + = components.at(type); // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < componentArray .size()) { + if (id < componentArray.size()) { // Loop trough the whole vector<> - for (const std::unique_ptr & componentPtr : componentArray[id]) { + for (const std::unique_ptr & componentPtr : + componentArray[id]) { // Cast the unique_ptr to a raw pointer T * castedComponent = static_cast(componentPtr.get()); @@ -92,7 +98,7 @@ ComponentManager::GetComponentsByID(std::uint32_t id) const { template std::vector> -ComponentManager::GetComponentsByType() const { +ComponentManager::get_components_by_type() const { // Determine the type of T (this is used as the key of the unordered_map<>) std::type_index type = typeid(T); @@ -105,10 +111,13 @@ ComponentManager::GetComponentsByType() const { if (components.find(type) != components.end()) { // Get the correct vector<> - const std::vector>> & componentArray = components.at(type); + const std::vector>> & + componentArray + = components.at(type); // Loop through the whole vector<> - for (const std::vector> & component : componentArray) { + for (const std::vector> & component : + componentArray) { // Loop trough the whole vector<> for (const std::unique_ptr & componentPtr : component) { // Cast the unique_ptr to a raw pointer @@ -130,5 +139,4 @@ ComponentManager::GetComponentsByType() const { return componentVector; } -} - +} // namespace crepe diff --git a/src/crepe/Components.cpp b/src/crepe/Components.cpp index 9760daa..4a43692 100644 --- a/src/crepe/Components.cpp +++ b/src/crepe/Components.cpp @@ -3,11 +3,12 @@ using namespace crepe; -Component::Component() : mActive(true) {} +Component::Component() : m_active(true) {} -Sprite::Sprite(std::string path) : mPath(path) {} +Sprite::Sprite(std::string path) : m_path(path) {} Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) - : mMass(mass), mGravityScale(gravityScale), mBodyType(bodyType) {} + : m_mass(mass), m_gravity_scale(gravityScale), m_body_type(bodyType) {} + +Collider::Collider(int size) : m_size(size) {} -Colider::Colider(int size) : mSize(size) {} diff --git a/src/crepe/Components.h b/src/crepe/Components.h index 7cb6fbb..1ad9ef2 100644 --- a/src/crepe/Components.h +++ b/src/crepe/Components.h @@ -8,7 +8,7 @@ class Component { public: Component(); - bool mActive; + bool m_active; }; // TODO: these should be in separate files @@ -17,23 +17,23 @@ class Sprite : public Component { public: Sprite(std::string path); - std::string mPath; + std::string m_path; }; class Rigidbody : public Component { public: Rigidbody(int mass, int gravityScale, int bodyType); - int mMass; - int mGravityScale; - int mBodyType; + int m_mass; + int m_gravity_scale; + int m_body_type; }; -class Colider : public Component { +class Collider : public Component { public: - Colider(int size); + Collider(int size); - int mSize; + int m_size; }; -} +} // namespace crepe diff --git a/src/crepe/GameObject.cpp b/src/crepe/GameObject.cpp index 5ac9d7a..16dc62f 100644 --- a/src/crepe/GameObject.cpp +++ b/src/crepe/GameObject.cpp @@ -6,4 +6,4 @@ using namespace crepe; GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, int layer) - : mId(id), mName(name), mTag(tag), mActive(true), mLayer(layer) {} + : m_id(id), m_name(name), m_tag(tag), m_active(true), m_layer(layer) {} diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h index 71ef60d..f0f5ea0 100644 --- a/src/crepe/GameObject.h +++ b/src/crepe/GameObject.h @@ -9,14 +9,14 @@ class GameObject { public: GameObject(std::uint32_t id, std::string name, std::string tag, int layer); - template void AddComponent(Args &&... args); + template + void add_component(Args &&... args); - std::uint32_t mId; - std::string mName; - std::string mTag; - bool mActive; - int mLayer; + std::uint32_t m_id; + std::string m_name; + std::string m_tag; + bool m_active; + int m_layer; }; -} - +} // namespace crepe diff --git a/src/crepe/GameObject.hpp b/src/crepe/GameObject.hpp index 5a672cf..1152ddf 100644 --- a/src/crepe/GameObject.hpp +++ b/src/crepe/GameObject.hpp @@ -8,8 +8,8 @@ namespace crepe { template void GameObject::AddComponent(Args &&... args) { - ComponentManager::get_instance().AddComponent(mId, std::forward(args)...); -} - + ComponentManager::get_instance().AddComponent( + mId, std::forward(args)...); } +} // namespace crepe diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 2002d1a..2d26cda 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -35,7 +35,7 @@ public: float volume; private: - std::unique_ptr _sound; + std::unique_ptr sound; }; } // namespace crepe::api -- cgit v1.2.3 From f8bc2a06a90c0ee172054db7ae2e1fdae09d14a3 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 6 Oct 2024 14:10:52 +0200 Subject: code standard v3 --- .clang-tidy | 10 +++--- src/CMakeLists.txt | 3 -- src/crepe/ComponentManager.h | 2 -- src/crepe/ComponentManager.hpp | 74 ++++++++++++++++++++++-------------------- src/crepe/Components.cpp | 11 +++---- src/crepe/Components.h | 12 +++---- src/crepe/GameObject.cpp | 4 +-- src/crepe/GameObject.h | 12 +++---- src/crepe/GameObject.hpp | 6 ++-- src/crepe/api/AudioSource.cpp | 10 +++--- src/crepe/util/log.cpp | 10 +++--- src/crepe/util/log.h | 10 +++--- 12 files changed, 81 insertions(+), 83 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 0ec997d..214a5ab 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -14,13 +14,15 @@ CheckOptions: value: 'lower_case' - key: 'readability-identifier-naming.MemberPrefix' value: '' - - key: 'readability-identifier-naming.GlobalConstantCase' + - key: 'readability-identifier-naming.ConstantCase' value: 'UPPER_CASE' - - key: 'readability-identifier-naming.GlobalConstantIgnoredRegexp' + - key: 'readability-identifier-naming.ConstantIgnoredRegexp' value: '_.*' - - key: 'readability-identifier-naming.GlobalVariableCase' + - key: 'readability-identifier-naming.ConstantParameterCase' value: 'lower_case' - - key: 'readability-identifier-naming.GlobalVariableIgnoredRegexp' + - key: 'readability-identifier-naming.VariableCase' + value: 'lower_case' + - key: 'readability-identifier-naming.VariableIgnoredRegexp' value: '_.*' - key: 'readability-identifier-naming.MacroDefinitionCase' value: 'UPPER_CASE' diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 62ca9a0..49d65a6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,10 +3,7 @@ cmake_minimum_required(VERSION 3.28) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 20) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) - -# enable debug features set(CMAKE_BUILD_TYPE Debug) -add_compile_definitions(DEBUG) add_subdirectory(../lib/soloud soloud) add_subdirectory(../lib/googletest googletest) diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index b88ad27..cf6edad 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -58,5 +58,3 @@ private: }; } // namespace crepe - -// #include "ComponentManager.hpp" diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 30b0e38..999cdcf 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -5,15 +5,16 @@ namespace crepe { template -void ComponentManager::add_component(std::uint32_t id, Args &&... args) { +void ComponentManager::add_component(uint32_t id, Args &&... args) { + using namespace std; + // Determine the type of T (this is used as the key of the unordered_map<>) - std::type_index type = typeid(T); + type_index type = typeid(T); // Check if this component type is already in the unordered_map<> if (components.find(type) == components.end()) { //If not, create a new (empty) vector<> of vector> - components[type] - = std::vector>>(); + components[type] = vector>>(); } // Resize the vector<> if the id is greater than the current size @@ -24,25 +25,26 @@ void ComponentManager::add_component(std::uint32_t id, Args &&... args) { // Create a new component of type T using perfect forwarding and store its // unique_ptr in the vector<> - components[type][id].push_back( - std::make_unique(std::forward(args)...)); + components[type][id].push_back(make_unique(forward(args)...)); } template -void ComponentManager::delete_components_by_id(std::uint32_t id) { +void ComponentManager::delete_components_by_id(uint32_t id) { + using namespace std; + // Determine the type of T (this is used as the key of the unordered_map<>) - std::type_index type = typeid(T); + type_index type = typeid(T); // Find the type (in the unordered_map<>) if (components.find(type) != components.end()) { // Get the correct vector<> - std::vector>> & componentArray + vector>> & component_array = components[type]; // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < componentArray.size()) { + if (id < component_array.size()) { // Clear the whole vector<> of this specific type and id - componentArray[id].clear(); + component_array[id].clear(); } } } @@ -61,72 +63,74 @@ void ComponentManager::delete_components() { template std::vector> -ComponentManager::get_components_by_id(std::uint32_t id) const { +ComponentManager::get_components_by_id(uint32_t id) const { + using namespace std; + // Determine the type of T (this is used as the key of the unordered_map<>) - std::type_index type = typeid(T); + type_index type = typeid(T); // Create an empty vector<> - std::vector> componentVector; + vector> component_vector; // Find the type (in the unordered_map<>) if (components.find(type) != components.end()) { // Get the correct vector<> - const std::vector>> & - componentArray + const vector>> & component_array = components.at(type); // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < componentArray.size()) { + if (id < component_array.size()) { // Loop trough the whole vector<> - for (const std::unique_ptr & componentPtr : - componentArray[id]) { + for (const unique_ptr & component_ptr : + component_array[id]) { // Cast the unique_ptr to a raw pointer - T * castedComponent = static_cast(componentPtr.get()); + T * casted_component = static_cast(component_ptr.get()); // Ensure that the cast was successful - if (castedComponent) { + if (casted_component) { // Add the dereferenced raw pointer to the vector<> - componentVector.push_back(*castedComponent); + component_vector.push_back(*casted_component); } } } } // Return the vector<> - return componentVector; + return component_vector; } template std::vector> ComponentManager::get_components_by_type() const { + using namespace std; + // Determine the type of T (this is used as the key of the unordered_map<>) - std::type_index type = typeid(T); + type_index type = typeid(T); // Create an empty vector<> - std::vector> componentVector; + vector> component_vector; // Set the id to 0 (the id will also be stored in the returned vector<>) - // std::uint32_t id = 0; + // uint32_t id = 0; // Find the type (in the unordered_map<>) if (components.find(type) != components.end()) { // Get the correct vector<> - const std::vector>> & - componentArray + const vector>> & component_array = components.at(type); // Loop through the whole vector<> - for (const std::vector> & component : - componentArray) { + for (const vector> & component : + component_array) { // Loop trough the whole vector<> - for (const std::unique_ptr & componentPtr : component) { + for (const unique_ptr & component_ptr : component) { // Cast the unique_ptr to a raw pointer - T * castedComponent = static_cast(componentPtr.get()); + T * casted_component = static_cast(component_ptr.get()); // Ensure that the cast was successful - if (castedComponent) { + if (casted_component) { // Pair the dereferenced raw pointer and the id and add it to the vector<> - componentVector.emplace_back(std::ref(*castedComponent)); + component_vector.emplace_back(ref(*casted_component)); } } @@ -136,7 +140,7 @@ ComponentManager::get_components_by_type() const { } // Return the vector<> - return componentVector; + return component_vector; } } // namespace crepe diff --git a/src/crepe/Components.cpp b/src/crepe/Components.cpp index 4a43692..f27c7a4 100644 --- a/src/crepe/Components.cpp +++ b/src/crepe/Components.cpp @@ -1,14 +1,13 @@ #include "Components.h" -#include using namespace crepe; +using namespace std; -Component::Component() : m_active(true) {} +Component::Component() : active(true) {} -Sprite::Sprite(std::string path) : m_path(path) {} +Sprite::Sprite(string path) : path(path) {} Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) - : m_mass(mass), m_gravity_scale(gravityScale), m_body_type(bodyType) {} - -Collider::Collider(int size) : m_size(size) {} + : mass(mass), gravity_scale(gravityScale), body_type(bodyType) {} +Collider::Collider(int size) : size(size) {} diff --git a/src/crepe/Components.h b/src/crepe/Components.h index 1ad9ef2..139599c 100644 --- a/src/crepe/Components.h +++ b/src/crepe/Components.h @@ -8,7 +8,7 @@ class Component { public: Component(); - bool m_active; + bool active; }; // TODO: these should be in separate files @@ -17,23 +17,23 @@ class Sprite : public Component { public: Sprite(std::string path); - std::string m_path; + std::string path; }; class Rigidbody : public Component { public: Rigidbody(int mass, int gravityScale, int bodyType); - int m_mass; - int m_gravity_scale; - int m_body_type; + int mass; + int gravity_scale; + int body_type; }; class Collider : public Component { public: Collider(int size); - int m_size; + int size; }; } // namespace crepe diff --git a/src/crepe/GameObject.cpp b/src/crepe/GameObject.cpp index 16dc62f..304f75a 100644 --- a/src/crepe/GameObject.cpp +++ b/src/crepe/GameObject.cpp @@ -1,9 +1,7 @@ #include "GameObject.h" -#include "ComponentManager.h" - using namespace crepe; GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, int layer) - : m_id(id), m_name(name), m_tag(tag), m_active(true), m_layer(layer) {} + : id(id), name(name), tag(tag), active(true), layer(layer) {} diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h index f0f5ea0..114990c 100644 --- a/src/crepe/GameObject.h +++ b/src/crepe/GameObject.h @@ -7,16 +7,16 @@ namespace crepe { class GameObject { public: - GameObject(std::uint32_t id, std::string name, std::string tag, int layer); + GameObject(uint32_t id, std::string name, std::string tag, int layer); template void add_component(Args &&... args); - std::uint32_t m_id; - std::string m_name; - std::string m_tag; - bool m_active; - int m_layer; + uint32_t id; + std::string name; + std::string tag; + bool active; + int layer; }; } // namespace crepe diff --git a/src/crepe/GameObject.hpp b/src/crepe/GameObject.hpp index 1152ddf..5966fbf 100644 --- a/src/crepe/GameObject.hpp +++ b/src/crepe/GameObject.hpp @@ -7,9 +7,9 @@ namespace crepe { template -void GameObject::AddComponent(Args &&... args) { - ComponentManager::get_instance().AddComponent( - mId, std::forward(args)...); +void GameObject::add_component(Args &&... args) { + auto & mgr = ComponentManager::get_instance(); + mgr.add_component(id, std::forward(args)...); } } // namespace crepe diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 656fc46..b512d27 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -6,17 +6,17 @@ using namespace crepe::api; AudioSource::AudioSource(std::unique_ptr audio_clip) { - this->_sound = std::make_unique(std::move(audio_clip)); + this->sound = std::make_unique(std::move(audio_clip)); } void AudioSource::play() { return this->play(false); } void AudioSource::play(bool looping) { - this->_sound->set_looping(looping); - this->_sound->play(); + this->sound->set_looping(looping); + this->sound->play(); } void AudioSource::stop() { - this->_sound->pause(); - this->_sound->rewind(); + this->sound->pause(); + this->sound->rewind(); } diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp index 6829ec3..f91d52c 100644 --- a/src/crepe/util/log.cpp +++ b/src/crepe/util/log.cpp @@ -8,10 +8,10 @@ using namespace crepe::util; static const char * const LOG_PREFIX[] = { - [log_level::debug] = "[DBG] ", - [log_level::info] = "[INFO] ", - [log_level::warning] = "[WARN] ", - [log_level::error] = "[ERR] ", + [log_level::DEBUG] = "[DBG] ", + [log_level::INFO] = "[INFO] ", + [log_level::WARNING] = "[WARN] ", + [log_level::ERROR] = "[ERR] ", }; static void va_logf(enum log_level level, va_list args, const std::string fmt) { @@ -38,7 +38,7 @@ static void va_logf(enum log_level level, va_list args, const std::string fmt) { void crepe::util::logf(const char * fmt, ...) { va_list args; va_start(args, fmt); - va_logf(crepe::util::log_level::debug, args, fmt); + va_logf(crepe::util::log_level::DEBUG, args, fmt); va_end(args); } diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index bfe7291..2b0fbe1 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -7,7 +7,7 @@ // utility macros #define _crepe_logf_here(fmt, ...) \ - crepe::util::logf(util::log_level::debug, "%s%s (%s:%d)" fmt "\n", \ + crepe::util::logf(util::log_level::DEBUG, "%s%s (%s:%d)" fmt "\n", \ crepe::util::color::FG_WHITE, __PRETTY_FUNCTION__, \ __FILE_NAME__, __LINE__, crepe::util::color::RESET, \ __VA_ARGS__) @@ -24,10 +24,10 @@ namespace crepe::util { enum log_level { - debug, - info, - warning, - error, + DEBUG, + INFO, + WARNING, + ERROR, }; void logf(const char * fmt, ...); -- cgit v1.2.3 From 39815f58e3842bb28e644e83111a619bd1374855 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 6 Oct 2024 15:12:01 +0200 Subject: add components_internal example --- src/crepe/ComponentManager.cpp | 2 +- src/crepe/ComponentManager.h | 11 +++++--- src/crepe/GameObject.cpp | 2 +- src/crepe/GameObject.h | 3 ++ src/example/CMakeLists.txt | 18 ++++++++++-- src/example/components_internal.cpp | 56 +++++++++++++++++++++++++++++++++++++ 6 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 src/example/components_internal.cpp diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index b080c9d..9a3fec7 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -7,7 +7,7 @@ ComponentManager & ComponentManager::get_instance() { return instance; } -void ComponentManager::delete_all_components_of_id(std::uint32_t id) { +void ComponentManager::delete_all_components_of_id(uint32_t id) { // Loop through all the types (in the unordered_map<>) for (auto & [type, componentArray] : components) { // Make sure that the id (that we are looking for) is within the boundaries of the vector<> diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index cf6edad..9e559dd 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -23,22 +23,22 @@ public: public: //! Add a component of a specific type template - void add_component(std::uint32_t id, Args &&... args); + void add_component(uint32_t id, Args &&... args); //! Deletes all components of a specific type and id template - void delete_components_by_id(std::uint32_t id); + void delete_components_by_id(uint32_t id); //! Deletes all components of a specific type template void delete_components(); //! Deletes all components of a specific id - void delete_all_components_of_id(std::uint32_t id); + void delete_all_components_of_id(uint32_t id); //! Deletes all components void delete_all_components(); //! Get a vector<> of all components at specific type and id template std::vector> - get_components_by_id(std::uint32_t id) const; + get_components_by_id(uint32_t id) const; //! Get a vector<> of all components of a specific type template std::vector> get_components_by_type() const; @@ -58,3 +58,6 @@ private: }; } // namespace crepe + +#include "ComponentManager.hpp" + diff --git a/src/crepe/GameObject.cpp b/src/crepe/GameObject.cpp index 304f75a..de3beb6 100644 --- a/src/crepe/GameObject.cpp +++ b/src/crepe/GameObject.cpp @@ -2,6 +2,6 @@ using namespace crepe; -GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, +GameObject::GameObject(uint32_t id, std::string name, std::string tag, int layer) : id(id), name(name), tag(tag), active(true), layer(layer) {} diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h index 114990c..d5a7c36 100644 --- a/src/crepe/GameObject.h +++ b/src/crepe/GameObject.h @@ -20,3 +20,6 @@ public: }; } // namespace crepe + +#include "GameObject.hpp" + diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index bcc9271..eef38fd 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -1,3 +1,17 @@ -add_executable(audio_internal EXCLUDE_FROM_ALL audio_internal.cpp) -target_link_libraries(audio_internal PUBLIC crepe) +# add_example(target_name [SOURCES...]) +function(add_example target_name) + # if SOURCES is not specified + if(NOT ARGV1) + # A .cpp file with target_name exists, and should be used + set(sources ${target_name}.cpp) + else() + set(sources ${ARGV}) + endif() + + add_executable(${target_name} EXCLUDE_FROM_ALL ${sources}) + target_link_libraries(${target_name} PUBLIC crepe) +endfunction() + +add_example(audio_internal) +add_example(components_internal) diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp new file mode 100644 index 0000000..821a7de --- /dev/null +++ b/src/example/components_internal.cpp @@ -0,0 +1,56 @@ +/** \file + * + * Standalone example for usage of the internal ECS + */ + +#include +#include + +#include +#include +#include +#include + +using namespace crepe; +using namespace std; + +#define OBJ_COUNT 100000 + +int main() { + dbg_trace(); + + auto & mgr = ComponentManager::get_instance(); + + auto start_adding = chrono::high_resolution_clock::now(); + + GameObject * game_object[OBJ_COUNT]; + + for (int i = 0; i < OBJ_COUNT; ++i) { + game_object[i] = new GameObject(i, "Name", "Tag", 0); + + game_object[i]->add_component("test"); + game_object[i]->add_component(0, 0, i); + game_object[i]->add_component(i); + } + + auto stop_adding = chrono::high_resolution_clock::now(); + + auto sprites = mgr.get_components_by_type(); + for (auto sprite : sprites) { + assert(sprite.get().path == "test"); + } + + auto stop_looping = chrono::high_resolution_clock::now(); + + for (int i = 0; i < OBJ_COUNT; ++i) { + delete game_object[i]; + } + + auto add_time = chrono::duration_cast(stop_adding - start_adding); + auto loop_time = chrono::duration_cast(stop_looping - stop_adding); + printf("add time: %ldus\n", add_time.count()); + printf("loop time: %ldus\n", loop_time.count()); + + return 0; +} + -- cgit v1.2.3 From aa8755148cbca29b584f7a146636b506070717f9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 6 Oct 2024 15:28:12 +0200 Subject: move components to separate files --- contributing.md | 1 + src/crepe/CMakeLists.txt | 10 ++++++++-- src/crepe/Collider.cpp | 6 ++++++ src/crepe/Collider.h | 15 ++++++++++++++ src/crepe/Component.cpp | 6 ++++++ src/crepe/Component.h | 15 ++++++++++++++ src/crepe/ComponentManager.h | 2 +- src/crepe/Components.cpp | 13 ------------- src/crepe/Components.h | 39 ------------------------------------- src/crepe/Rigidbody.cpp | 7 +++++++ src/crepe/Rigidbody.h | 16 +++++++++++++++ src/crepe/Sprite.cpp | 9 +++++++++ src/crepe/Sprite.h | 17 ++++++++++++++++ src/example/components_internal.cpp | 5 ++++- 14 files changed, 105 insertions(+), 56 deletions(-) create mode 100644 src/crepe/Collider.cpp create mode 100644 src/crepe/Collider.h create mode 100644 src/crepe/Component.cpp create mode 100644 src/crepe/Component.h delete mode 100644 src/crepe/Components.cpp delete mode 100644 src/crepe/Components.h create mode 100644 src/crepe/Rigidbody.cpp create mode 100644 src/crepe/Rigidbody.h create mode 100644 src/crepe/Sprite.cpp create mode 100644 src/crepe/Sprite.h diff --git a/contributing.md b/contributing.md index 933f1bf..b79f7a7 100644 --- a/contributing.md +++ b/contributing.md @@ -17,6 +17,7 @@ # Code style - ASCII only +- Class names are always singular - Explanatory comments are placed above the line(s) they are explaining - Source files should only contain comments that plainly state what the code is supposed to do diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 5840208..d7d563e 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -3,8 +3,11 @@ target_sources(crepe PUBLIC Sound.cpp SoundContext.cpp ComponentManager.cpp - Components.cpp + Component.cpp GameObject.cpp + Collider.cpp + Rigidbody.cpp + Sprite.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -13,9 +16,12 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SoundContext.h ComponentManager.h ComponentManager.hpp - Components.h + Component.h GameObject.h GameObject.hpp + Collider.h + Rigidbody.h + Sprite.h ) add_subdirectory(api) diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp new file mode 100644 index 0000000..b7040eb --- /dev/null +++ b/src/crepe/Collider.cpp @@ -0,0 +1,6 @@ +#include "Collider.h" + +using namespace crepe; + +Collider::Collider(int size) : size(size) {} + diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h new file mode 100644 index 0000000..666386d --- /dev/null +++ b/src/crepe/Collider.h @@ -0,0 +1,15 @@ +#pragma once + +#include "Component.h" + +namespace crepe { + +class Collider : public Component { +public: + Collider(int size); + + int size; +}; + +} + diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp new file mode 100644 index 0000000..9a7731a --- /dev/null +++ b/src/crepe/Component.cpp @@ -0,0 +1,6 @@ +#include "Component.h" + +using namespace crepe; + +Component::Component() : active(true) {} + diff --git a/src/crepe/Component.h b/src/crepe/Component.h new file mode 100644 index 0000000..6e23d93 --- /dev/null +++ b/src/crepe/Component.h @@ -0,0 +1,15 @@ +#pragma once + +namespace crepe { + +class Component { +public: + Component(); + // TODO: shouldn't this constructor be deleted because this class will never + // directly be instantiated? + + bool active; +}; + +} // namespace crepe + diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 9e559dd..9463558 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -7,7 +7,7 @@ #include #include -#include "Components.h" +#include "Component.h" namespace crepe { diff --git a/src/crepe/Components.cpp b/src/crepe/Components.cpp deleted file mode 100644 index f27c7a4..0000000 --- a/src/crepe/Components.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "Components.h" - -using namespace crepe; -using namespace std; - -Component::Component() : active(true) {} - -Sprite::Sprite(string path) : path(path) {} - -Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) - : mass(mass), gravity_scale(gravityScale), body_type(bodyType) {} - -Collider::Collider(int size) : size(size) {} diff --git a/src/crepe/Components.h b/src/crepe/Components.h deleted file mode 100644 index 139599c..0000000 --- a/src/crepe/Components.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include - -namespace crepe { - -class Component { -public: - Component(); - - bool active; -}; - -// TODO: these should be in separate files - -class Sprite : public Component { -public: - Sprite(std::string path); - - std::string path; -}; - -class Rigidbody : public Component { -public: - Rigidbody(int mass, int gravityScale, int bodyType); - - int mass; - int gravity_scale; - int body_type; -}; - -class Collider : public Component { -public: - Collider(int size); - - int size; -}; - -} // namespace crepe diff --git a/src/crepe/Rigidbody.cpp b/src/crepe/Rigidbody.cpp new file mode 100644 index 0000000..1518d28 --- /dev/null +++ b/src/crepe/Rigidbody.cpp @@ -0,0 +1,7 @@ +#include "Rigidbody.h" + +using namespace crepe; + +Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) + : mass(mass), gravity_scale(gravityScale), body_type(bodyType) {} + diff --git a/src/crepe/Rigidbody.h b/src/crepe/Rigidbody.h new file mode 100644 index 0000000..67edb03 --- /dev/null +++ b/src/crepe/Rigidbody.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Component.h" + +namespace crepe { + +class Rigidbody : public Component { +public: + Rigidbody(int mass, int gravityScale, int bodyType); + + int mass; + int gravity_scale; + int body_type; +}; + +} diff --git a/src/crepe/Sprite.cpp b/src/crepe/Sprite.cpp new file mode 100644 index 0000000..c835d83 --- /dev/null +++ b/src/crepe/Sprite.cpp @@ -0,0 +1,9 @@ +#include + +#include "Sprite.h" + +using namespace crepe; +using namespace std; + +Sprite::Sprite(string path) : path(path) {} + diff --git a/src/crepe/Sprite.h b/src/crepe/Sprite.h new file mode 100644 index 0000000..029064b --- /dev/null +++ b/src/crepe/Sprite.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include "Component.h" + +namespace crepe { + +class Sprite : public Component { +public: + Sprite(std::string path); + + std::string path; +}; + +} + diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp index 821a7de..4246dd7 100644 --- a/src/example/components_internal.cpp +++ b/src/example/components_internal.cpp @@ -9,7 +9,10 @@ #include #include #include -#include +#include +#include +#include +#include using namespace crepe; using namespace std; -- cgit v1.2.3 From 8a509ad4c5e15fbf7eaade1c8bf4834f0d0069c5 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 6 Oct 2024 15:29:39 +0200 Subject: `make format` --- src/crepe/Collider.cpp | 1 - src/crepe/Collider.h | 3 +-- src/crepe/Component.cpp | 1 - src/crepe/Component.h | 1 - src/crepe/ComponentManager.h | 1 - src/crepe/GameObject.h | 1 - src/crepe/Rigidbody.cpp | 1 - src/crepe/Rigidbody.h | 2 +- src/crepe/Sprite.cpp | 1 - src/crepe/Sprite.h | 3 +-- src/example/components_internal.cpp | 47 +++++++++++++++++++------------------ 11 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp index b7040eb..3f12afd 100644 --- a/src/crepe/Collider.cpp +++ b/src/crepe/Collider.cpp @@ -3,4 +3,3 @@ using namespace crepe; Collider::Collider(int size) : size(size) {} - diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h index 666386d..120da05 100644 --- a/src/crepe/Collider.h +++ b/src/crepe/Collider.h @@ -11,5 +11,4 @@ public: int size; }; -} - +} // namespace crepe diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 9a7731a..d14159c 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -3,4 +3,3 @@ using namespace crepe; Component::Component() : active(true) {} - diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 6e23d93..16a4ce5 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -12,4 +12,3 @@ public: }; } // namespace crepe - diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 9463558..2ab9dc8 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -60,4 +60,3 @@ private: } // namespace crepe #include "ComponentManager.hpp" - diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h index d5a7c36..3588d9a 100644 --- a/src/crepe/GameObject.h +++ b/src/crepe/GameObject.h @@ -22,4 +22,3 @@ public: } // namespace crepe #include "GameObject.hpp" - diff --git a/src/crepe/Rigidbody.cpp b/src/crepe/Rigidbody.cpp index 1518d28..495d908 100644 --- a/src/crepe/Rigidbody.cpp +++ b/src/crepe/Rigidbody.cpp @@ -4,4 +4,3 @@ using namespace crepe; Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) : mass(mass), gravity_scale(gravityScale), body_type(bodyType) {} - diff --git a/src/crepe/Rigidbody.h b/src/crepe/Rigidbody.h index 67edb03..63a8877 100644 --- a/src/crepe/Rigidbody.h +++ b/src/crepe/Rigidbody.h @@ -13,4 +13,4 @@ public: int body_type; }; -} +} // namespace crepe diff --git a/src/crepe/Sprite.cpp b/src/crepe/Sprite.cpp index c835d83..a5a5e68 100644 --- a/src/crepe/Sprite.cpp +++ b/src/crepe/Sprite.cpp @@ -6,4 +6,3 @@ using namespace crepe; using namespace std; Sprite::Sprite(string path) : path(path) {} - diff --git a/src/crepe/Sprite.h b/src/crepe/Sprite.h index 029064b..143e702 100644 --- a/src/crepe/Sprite.h +++ b/src/crepe/Sprite.h @@ -13,5 +13,4 @@ public: std::string path; }; -} - +} // namespace crepe diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp index 4246dd7..54ce295 100644 --- a/src/example/components_internal.cpp +++ b/src/example/components_internal.cpp @@ -6,13 +6,13 @@ #include #include -#include +#include +#include #include #include -#include -#include #include -#include +#include +#include using namespace crepe; using namespace std; @@ -24,36 +24,37 @@ int main() { auto & mgr = ComponentManager::get_instance(); - auto start_adding = chrono::high_resolution_clock::now(); + auto start_adding = chrono::high_resolution_clock::now(); - GameObject * game_object[OBJ_COUNT]; + GameObject * game_object[OBJ_COUNT]; - for (int i = 0; i < OBJ_COUNT; ++i) { - game_object[i] = new GameObject(i, "Name", "Tag", 0); + for (int i = 0; i < OBJ_COUNT; ++i) { + game_object[i] = new GameObject(i, "Name", "Tag", 0); - game_object[i]->add_component("test"); - game_object[i]->add_component(0, 0, i); - game_object[i]->add_component(i); - } + game_object[i]->add_component("test"); + game_object[i]->add_component(0, 0, i); + game_object[i]->add_component(i); + } - auto stop_adding = chrono::high_resolution_clock::now(); + auto stop_adding = chrono::high_resolution_clock::now(); - auto sprites = mgr.get_components_by_type(); - for (auto sprite : sprites) { + auto sprites = mgr.get_components_by_type(); + for (auto sprite : sprites) { assert(sprite.get().path == "test"); - } + } - auto stop_looping = chrono::high_resolution_clock::now(); + auto stop_looping = chrono::high_resolution_clock::now(); - for (int i = 0; i < OBJ_COUNT; ++i) { - delete game_object[i]; - } + for (int i = 0; i < OBJ_COUNT; ++i) { + delete game_object[i]; + } - auto add_time = chrono::duration_cast(stop_adding - start_adding); - auto loop_time = chrono::duration_cast(stop_looping - stop_adding); + auto add_time = chrono::duration_cast(stop_adding + - start_adding); + auto loop_time = chrono::duration_cast(stop_looping + - stop_adding); printf("add time: %ldus\n", add_time.count()); printf("loop time: %ldus\n", loop_time.count()); return 0; } - -- 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 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 c6cbde11c080c994577a7ebe4b966c25870dec1f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 7 Oct 2024 12:21:53 +0200 Subject: add clangd extension recommendation --- .vscode/extensions.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 5b380e1..977c0ca 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,5 +1,6 @@ { "recommendations": [ + "llvm-vs-code-extensions.vscode-clangd", "EditorConfig.EditorConfig", "ms-vscode.cmake-tools" ] -- cgit v1.2.3 From c43acf19e375830f30396936a82e351d576110ef Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 8 Oct 2024 13:41:57 +0200 Subject: update contributing.md --- contributing.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contributing.md b/contributing.md index b79f7a7..4fab937 100644 --- a/contributing.md +++ b/contributing.md @@ -36,6 +36,8 @@ as these are no longer accessible when the engine is installed - Getter and setter functions are appropriately prefixed with `get_` and `set_`. +- Header files declare either a single class or symbols within a single + namespace. ## CMakeLists specific -- cgit v1.2.3 From 515aab5ab7e3281d2d77177724aae42cffc6faae Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 8 Oct 2024 13:47:44 +0200 Subject: fix feedback --- src/crepe/api/AudioSource.cpp | 10 +++++----- src/crepe/api/AudioSource.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 656fc46..b512d27 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -6,17 +6,17 @@ using namespace crepe::api; AudioSource::AudioSource(std::unique_ptr audio_clip) { - this->_sound = std::make_unique(std::move(audio_clip)); + this->sound = std::make_unique(std::move(audio_clip)); } void AudioSource::play() { return this->play(false); } void AudioSource::play(bool looping) { - this->_sound->set_looping(looping); - this->_sound->play(); + this->sound->set_looping(looping); + this->sound->play(); } void AudioSource::stop() { - this->_sound->pause(); - this->_sound->rewind(); + this->sound->pause(); + this->sound->rewind(); } diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 2002d1a..2d26cda 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -35,7 +35,7 @@ public: float volume; private: - std::unique_ptr _sound; + std::unique_ptr sound; }; } // namespace crepe::api -- cgit v1.2.3 From 08d0b07c19edfe8c81dfe4e21c0d4c1ef128b628 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 9 Oct 2024 14:23:35 +0200 Subject: update contributing.md --- contributing.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/contributing.md b/contributing.md index 2ff411a..9f87ec5 100644 --- a/contributing.md +++ b/contributing.md @@ -30,6 +30,27 @@ as these are no longer accessible when the engine is installed - Getter and setter functions are appropriately prefixed with `get_` and `set_`. +- Doxygen commands are used with a backslash instead of an at-sign (i.e. + `\brief` instead of `@brief`) +- A singleton's instance is always accessed using a getter function that + instantiates its own class as a static variable within the getter function + scope, instead of storing the instance as a member variable directly: + + ```cpp + class Bad { + static Bad instance; + Bad & get_instance() { return instance; } + }; + + class Good { + Good & get_instance() { + static Good instance; + return instance; + } + }; + ``` +- Member variable default values should be directly defined in the class + declaration instead of using the constructor. ## CMakeLists specific -- cgit v1.2.3 From bbf55eaabd09f6d2a98fad9c0a68db83c140f3e1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 10 Oct 2024 07:29:17 +0200 Subject: use find_package instead of add_subdirectory --- lib/soloud/CMakeLists.txt | 97 ----------------------------------------------- src/CMakeLists.txt | 11 +++--- src/crepe/Sound.h | 4 +- src/crepe/SoundContext.h | 2 +- 4 files changed, 9 insertions(+), 105 deletions(-) delete mode 100644 lib/soloud/CMakeLists.txt diff --git a/lib/soloud/CMakeLists.txt b/lib/soloud/CMakeLists.txt deleted file mode 100644 index aaccd96..0000000 --- a/lib/soloud/CMakeLists.txt +++ /dev/null @@ -1,97 +0,0 @@ -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 49d65a6..09c60bd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,11 +5,12 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) set(CMAKE_BUILD_TYPE Debug) -add_subdirectory(../lib/soloud soloud) -add_subdirectory(../lib/googletest googletest) - project(crepe C CXX) +find_package(SDL2 REQUIRED) +find_package(SoLoud REQUIRED) +find_package(GTest REQUIRED) + add_library(crepe SHARED) add_executable(test_main EXCLUDE_FROM_ALL) @@ -17,9 +18,9 @@ target_include_directories(crepe PUBLIC SYSTEM INTERFACE . ) -# TODO: libraries should be linked as PRIVATE target_link_libraries(crepe - PUBLIC soloud + PRIVATE soloud + PRIVATE SDL2 ) add_subdirectory(crepe) diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h index 1ac20a7..b7cfbb8 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/Sound.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include diff --git a/src/crepe/SoundContext.h b/src/crepe/SoundContext.h index 090966d..d3123d2 100644 --- a/src/crepe/SoundContext.h +++ b/src/crepe/SoundContext.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include "Sound.h" -- cgit v1.2.3 From 44208336d80dd63735215ee2cafb3e73fa03f930 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 10 Oct 2024 07:30:16 +0200 Subject: move soloud/src to soloud --- .gitmodules | 2 +- lib/soloud | 1 + lib/soloud/src | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) create mode 160000 lib/soloud delete mode 160000 lib/soloud/src diff --git a/.gitmodules b/.gitmodules index bb860c0..7dd39b2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,7 +3,7 @@ url = https://github.com/google/googletest shallow = true [submodule "lib/soloud/src"] - path = lib/soloud/src + path = lib/soloud url = https://github.com/jarikomppa/soloud shallow = true [submodule "lib/sdl2"] diff --git a/lib/soloud b/lib/soloud new file mode 160000 index 0000000..e82fd32 --- /dev/null +++ b/lib/soloud @@ -0,0 +1 @@ +Subproject commit e82fd32c1f62183922f08c14c814a02b58db1873 diff --git a/lib/soloud/src b/lib/soloud/src deleted file mode 160000 index e82fd32..0000000 --- a/lib/soloud/src +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e82fd32c1f62183922f08c14c814a02b58db1873 -- cgit v1.2.3 From 579824011d5e8776e2079d6624a39535517760ff Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 10 Oct 2024 08:01:27 +0200 Subject: update readmes --- readme.md | 37 ++++++++++++++++++++++++++++++++----- src/readme.md | 31 ++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 5351dfe..24bb66f 100644 --- a/readme.md +++ b/readme.md @@ -8,18 +8,45 @@ This repository contains: |`mwe/`|minimal working examples and proof-of-concepts| |`src/crepe/`|game engine source code| |`src/test/`|unit tests| -|`src/example`|standalone examples using game engine| +|`src/example/`|standalone examples using game engine| ## Compilation This repository uses CMake (the makefile in the root of this repository is for -running auxiliary tasks only). Make sure you have initialized and updated the -submodules before compiling. +running auxiliary tasks only). See [src/readme.md](src/readme.md) for detailed +building instructions. + +## Installing libraries + +The expected library (source) versions are included in this repository as git +submodules. Follow these steps for manually building one of the required +libraries from source: + +1. Ensure the git submodules are initialized: + ``` + $ git submodule update --init --recursive --depth 1 + ``` +2. `cd` into the library source folder: + ``` + $ cd lib/googletest + or + $ cd lib/sdl2 + or + $ cd lib/soloud/contrib + ``` +3. Configure the build, run the build and install: + ``` + $ cmake -B build -G Ninja + $ ninja -C build + # ninja -C build install + ``` ## Documentation -API documentation is done using Doxygen. To generate the docs, run `make -doxygen`. +API documentation is done using Doxygen. To generate the docs, run +``` +$ make doxygen +``` ## Code style diff --git a/src/readme.md b/src/readme.md index 1c5d3a2..a4a71e7 100644 --- a/src/readme.md +++ b/src/readme.md @@ -2,7 +2,32 @@ This folder contains the crêpe engine source files, unit tests, and some toy examples. The only target built by default by the CMakeLists.txt in this folder -is the crêpe shared library object. Unit tests can be built by explicitly -specifying the target `test_main` when running the build command. Each source -file in the example/ folder corresponds to a CMake target as well. +is the crêpe shared library object. + +Examples (using Ninja): + +``` +$ cmake -B build -G Ninja +$ ninja -C build +``` + +Unit tests can be built by explicitly specifying the target `test_main` when +running the build command: + +``` +$ ninja -C build test_main +``` + +Each source file in the example/ folder corresponds to a CMake target as well: + +``` +$ ninja -C build audio_internal components_internal +``` + +For installing crêpe system-wide after building (install must be run with +elevated privileges): + +``` +# ninja -C build install +``` -- cgit v1.2.3