From dc93b1e9b3e05dfd3e271aaccbee1210180a6906 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 7 Nov 2024 21:19:01 +0100 Subject: update clang-format and run `make format` over my (loek)s files --- src/crepe/api/CircleCollider.h | 3 ++- src/crepe/api/Config.cpp | 1 - src/crepe/api/Config.h | 6 ++--- src/crepe/api/Metadata.cpp | 4 ++- src/crepe/api/ParticleEmitter.cpp | 11 +++++--- src/crepe/api/Rigidbody.cpp | 3 ++- src/crepe/api/SaveManager.cpp | 53 ++++++++++++++++++++++++++------------- src/crepe/api/SaveManager.h | 7 +++--- src/crepe/api/Sprite.cpp | 5 +++- src/crepe/api/Transform.cpp | 5 +++- 10 files changed, 64 insertions(+), 34 deletions(-) (limited to 'src/crepe/api') diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h index caa7e43..e77a592 100644 --- a/src/crepe/api/CircleCollider.h +++ b/src/crepe/api/CircleCollider.h @@ -6,7 +6,8 @@ namespace crepe { class CircleCollider : public Collider { public: CircleCollider(game_object_id_t game_object_id, int radius) - : Collider(game_object_id), radius(radius) {} + : Collider(game_object_id), + radius(radius) {} int radius; }; diff --git a/src/crepe/api/Config.cpp b/src/crepe/api/Config.cpp index d6206da..0100bcc 100644 --- a/src/crepe/api/Config.cpp +++ b/src/crepe/api/Config.cpp @@ -6,4 +6,3 @@ Config & Config::get_instance() { static Config instance; return instance; } - diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 56e3af5..8c9e643 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -7,6 +7,7 @@ namespace crepe { class Config { private: Config() = default; + public: ~Config() = default; @@ -16,8 +17,8 @@ public: // singleton Config(const Config &) = delete; Config(Config &&) = delete; - Config & operator = (const Config &) = delete; - Config & operator = (Config &&) = delete; + Config & operator=(const Config &) = delete; + Config & operator=(Config &&) = delete; public: //! Logging-related settings @@ -60,4 +61,3 @@ public: }; } // namespace crepe - diff --git a/src/crepe/api/Metadata.cpp b/src/crepe/api/Metadata.cpp index 76f11d7..d421de5 100644 --- a/src/crepe/api/Metadata.cpp +++ b/src/crepe/api/Metadata.cpp @@ -4,4 +4,6 @@ using namespace crepe; using namespace std; Metadata::Metadata(game_object_id_t id, const string & name, const string & tag) - : Component(id), name(name), tag(tag) {} + : Component(id), + name(name), + tag(tag) {} diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index 3b2e2f2..0bc2197 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -11,9 +11,14 @@ ParticleEmitter::ParticleEmitter(game_object_id_t id, uint32_t max_particles, uint32_t speed_offset, uint32_t angle, uint32_t angleOffset, float begin_lifespan, float end_lifespan) - : Component(id), max_particles(max_particles), emission_rate(emission_rate), - speed(speed), speed_offset(speed_offset), position{0, 0}, - begin_lifespan(begin_lifespan), end_lifespan(end_lifespan) { + : Component(id), + max_particles(max_particles), + emission_rate(emission_rate), + speed(speed), + speed_offset(speed_offset), + position{0, 0}, + begin_lifespan(begin_lifespan), + end_lifespan(end_lifespan) { std::srand( static_cast(std::time(nullptr))); // initialize random seed std::cout << "Create emitter" << std::endl; diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp index cbf1325..3bf1c5b 100644 --- a/src/crepe/api/Rigidbody.cpp +++ b/src/crepe/api/Rigidbody.cpp @@ -3,7 +3,8 @@ using namespace crepe; crepe::Rigidbody::Rigidbody(uint32_t game_object_id, const Data & data) - : Component(game_object_id), data(data) {} + : Component(game_object_id), + data(data) {} void crepe::Rigidbody::add_force_linear(const Vector2 & force) { this->data.linear_velocity += force; diff --git a/src/crepe/api/SaveManager.cpp b/src/crepe/api/SaveManager.cpp index 23587e4..43276c5 100644 --- a/src/crepe/api/SaveManager.cpp +++ b/src/crepe/api/SaveManager.cpp @@ -2,8 +2,8 @@ #include "../util/log.h" #include "Config.h" -#include "ValueBroker.h" #include "SaveManager.h" +#include "ValueBroker.h" using namespace std; using namespace crepe; @@ -65,17 +65,33 @@ string SaveManager::deserialize(const string & value) const noexcept { return value; } -template <> uint8_t SaveManager::deserialize(const string & value) const noexcept { return deserialize(value); } -template <> int8_t SaveManager::deserialize(const string & value) const noexcept { return deserialize(value); } -template <> uint16_t SaveManager::deserialize(const string & value) const noexcept { return deserialize(value); } -template <> int16_t SaveManager::deserialize(const string & value) const noexcept { return deserialize(value); } -template <> uint32_t SaveManager::deserialize(const string & value) const noexcept { return deserialize(value); } -template <> int32_t SaveManager::deserialize(const string & value) const noexcept { return deserialize(value); } - -SaveManager::SaveManager() { - dbg_trace(); +template <> +uint8_t SaveManager::deserialize(const string & value) const noexcept { + return deserialize(value); +} +template <> +int8_t SaveManager::deserialize(const string & value) const noexcept { + return deserialize(value); +} +template <> +uint16_t SaveManager::deserialize(const string & value) const noexcept { + return deserialize(value); +} +template <> +int16_t SaveManager::deserialize(const string & value) const noexcept { + return deserialize(value); +} +template <> +uint32_t SaveManager::deserialize(const string & value) const noexcept { + return deserialize(value); +} +template <> +int32_t SaveManager::deserialize(const string & value) const noexcept { + return deserialize(value); } +SaveManager::SaveManager() { dbg_trace(); } + SaveManager & SaveManager::get_instance() { dbg_trace(); static SaveManager instance; @@ -118,17 +134,19 @@ template void SaveManager::set(const string &, const double &); template ValueBroker SaveManager::get(const string & key, const T & default_value) { - if (!this->has(key)) - this->set(key, default_value); + if (!this->has(key)) this->set(key, default_value); return this->get(key); } template ValueBroker SaveManager::get(const string &, const uint8_t &); template ValueBroker SaveManager::get(const string &, const int8_t &); -template ValueBroker SaveManager::get(const string &, const uint16_t &); +template ValueBroker SaveManager::get(const string &, + const uint16_t &); template ValueBroker SaveManager::get(const string &, const int16_t &); -template ValueBroker SaveManager::get(const string &, const uint32_t &); +template ValueBroker SaveManager::get(const string &, + const uint32_t &); template ValueBroker SaveManager::get(const string &, const int32_t &); -template ValueBroker SaveManager::get(const string &, const uint64_t &); +template ValueBroker SaveManager::get(const string &, + const uint64_t &); template ValueBroker SaveManager::get(const string &, const int64_t &); template ValueBroker SaveManager::get(const string &, const float &); template ValueBroker SaveManager::get(const string &, const double &); @@ -138,8 +156,8 @@ template ValueBroker SaveManager::get(const string & key) { T value; return { - [this, key] (const T & target) { this->set(key, target); }, - [this, key, value] () mutable -> const T & { + [this, key](const T & target) { this->set(key, target); }, + [this, key, value]() mutable -> const T & { value = this->deserialize(this->get_db().get(key)); return value; }, @@ -156,4 +174,3 @@ template ValueBroker SaveManager::get(const string &); template ValueBroker SaveManager::get(const string &); template ValueBroker SaveManager::get(const string &); template ValueBroker SaveManager::get(const string &); - diff --git a/src/crepe/api/SaveManager.h b/src/crepe/api/SaveManager.h index 3073656..4be85fb 100644 --- a/src/crepe/api/SaveManager.h +++ b/src/crepe/api/SaveManager.h @@ -93,8 +93,8 @@ public: static SaveManager & get_instance(); SaveManager(const SaveManager &) = delete; SaveManager(SaveManager &&) = delete; - SaveManager & operator = (const SaveManager &) = delete; - SaveManager & operator = (SaveManager &&) = delete; + SaveManager & operator=(const SaveManager &) = delete; + SaveManager & operator=(SaveManager &&) = delete; private: /** @@ -110,5 +110,4 @@ private: static DB & get_db(); }; -} - +} // namespace crepe diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index d3465c7..88a442b 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -12,7 +12,10 @@ using namespace crepe; Sprite::Sprite(game_object_id_t id, shared_ptr image, const Color & color, const FlipSettings & flip) - : Component(id), color(color), flip(flip), sprite_image(image) { + : Component(id), + color(color), + flip(flip), + sprite_image(image) { dbg_trace(); } diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index a244bc5..e401120 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -6,6 +6,9 @@ using namespace crepe; Transform::Transform(game_object_id_t id, const Vector2 & point, double rotation, double scale) - : Component(id), position(point), rotation(rotation), scale(scale) { + : Component(id), + position(point), + rotation(rotation), + scale(scale) { dbg_trace(); } -- cgit v1.2.3 From 7b63eb310dfd299b6d26169cb0981c9bfaf160b6 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 7 Nov 2024 21:21:32 +0100 Subject: update cleanup --- src/crepe/api/AudioSource.cpp | 23 ---------------------- src/crepe/api/AudioSource.h | 39 ------------------------------------ src/makefile | 46 ------------------------------------------- 3 files changed, 108 deletions(-) delete mode 100644 src/crepe/api/AudioSource.cpp delete mode 100644 src/crepe/api/AudioSource.h (limited to 'src/crepe/api') diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp deleted file mode 100644 index 63fd0d7..0000000 --- a/src/crepe/api/AudioSource.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include - -#include "../facade/Sound.h" - -#include "AudioSource.h" - -using namespace crepe; - -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 deleted file mode 100644 index 1e24ae8..0000000 --- a/src/crepe/api/AudioSource.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include - -#include "../Asset.h" -#include "../Component.h" - -namespace crepe { - -class Sound; - -//! Audio source component -class AudioSource : public 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; -}; - -} // namespace crepe diff --git a/src/makefile b/src/makefile index d9c5a57..778e2c9 100644 --- a/src/makefile +++ b/src/makefile @@ -15,83 +15,37 @@ # pull request. Make sure to ask someone to review the code standards for # each ENTIRE FILE in this pull request. -LOEK += crepe/Asset.cpp -LOEK += crepe/Asset.h TODO += crepe/Collider.cpp TODO += crepe/Collider.h -MAX += crepe/Component.cpp -MAX += crepe/Component.h -MAX += crepe/ComponentManager.cpp -MAX += crepe/ComponentManager.h -MAX += crepe/ComponentManager.hpp -MAX += crepe/api/Metadata.cpp -MAX += crepe/api/Metadata.h TODO += crepe/Particle.cpp TODO += crepe/Particle.h TODO += crepe/Position.h TODO += crepe/api/AssetManager.cpp TODO += crepe/api/AssetManager.h TODO += crepe/api/AssetManager.hpp -LOEK += crepe/api/BehaviorScript.cpp -LOEK += crepe/api/BehaviorScript.h -LOEK += crepe/api/BehaviorScript.hpp TODO += crepe/api/CircleCollider.h TODO += crepe/api/Color.cpp TODO += crepe/api/Color.h -LOEK += crepe/api/Config.h -MAX += crepe/api/GameObject.cpp -MAX += crepe/api/GameObject.h -MAX += crepe/api/GameObject.hpp TODO += crepe/api/ParticleEmitter.cpp TODO += crepe/api/ParticleEmitter.h TODO += crepe/api/Vector2.h TODO += crepe/api/Vector2.cpp -JARO += crepe/api/Rigidbody.cpp -JARO += crepe/api/Rigidbody.h -LOEK += crepe/api/Script.cpp -LOEK += crepe/api/Script.h -LOEK += crepe/api/Script.hpp TODO += crepe/api/Sprite.cpp TODO += crepe/api/Sprite.h TODO += crepe/api/Texture.cpp TODO += crepe/api/Texture.h -MAX += crepe/api/Transform.cpp -MAX += crepe/api/Transform.h TODO += crepe/facade/SDLContext.cpp TODO += crepe/facade/SDLContext.h -LOEK += crepe/facade/Sound.cpp -LOEK += crepe/facade/Sound.h -LOEK += crepe/facade/SoundContext.cpp -LOEK += crepe/facade/SoundContext.h TODO += crepe/system/CollisionSystem.cpp TODO += crepe/system/CollisionSystem.h TODO += crepe/system/ParticleSystem.cpp TODO += crepe/system/ParticleSystem.h -JARO += crepe/system/PhysicsSystem.cpp -JARO += crepe/system/PhysicsSystem.h TODO += crepe/system/RenderSystem.cpp TODO += crepe/system/RenderSystem.h -LOEK += crepe/system/ScriptSystem.cpp -LOEK += crepe/system/ScriptSystem.h -LOEK += crepe/system/System.h -LOEK += crepe/util/LogColor.cpp -LOEK += crepe/util/LogColor.h -LOEK += crepe/util/fmt.cpp -LOEK += crepe/util/fmt.h -LOEK += crepe/util/log.cpp -LOEK += crepe/util/log.h TODO += example/asset_manager.cpp -LOEK += example/audio_internal.cpp TODO += example/components_internal.cpp -MAX += example/ecs.cpp -LOEK += example/log.cpp TODO += example/particle.cpp -JARO += example/physics.cpp TODO += example/rendering.cpp -LOEK += example/script.cpp -LOEK += test/audio.cpp -LOEK += test/dummy.cpp -JARO += test/PhysicsTest.cpp FMT := $(LOEK) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 format: FORCE -- cgit v1.2.3 From 0e9a4b0e9b4ba92290c106cda7164607c499940c Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 12 Nov 2024 15:16:39 +0100 Subject: `make format` --- src/crepe/Exception.cpp | 1 - src/crepe/ValueBroker.hpp | 3 ++- src/crepe/api/Animator.cpp | 5 ++++- src/crepe/api/Camera.cpp | 3 ++- src/crepe/facade/DB.cpp | 1 - src/makefile | 52 +--------------------------------------------- 6 files changed, 9 insertions(+), 56 deletions(-) (limited to 'src/crepe/api') diff --git a/src/crepe/Exception.cpp b/src/crepe/Exception.cpp index a1fa765..bfdbcdd 100644 --- a/src/crepe/Exception.cpp +++ b/src/crepe/Exception.cpp @@ -14,4 +14,3 @@ Exception::Exception(const char * fmt, ...) { this->error = va_stringf(args, fmt); va_end(args); } - diff --git a/src/crepe/ValueBroker.hpp b/src/crepe/ValueBroker.hpp index 927142f..5c3bed9 100644 --- a/src/crepe/ValueBroker.hpp +++ b/src/crepe/ValueBroker.hpp @@ -6,7 +6,8 @@ namespace crepe { template ValueBroker::ValueBroker(const setter_t & setter, const getter_t & getter) - : setter(setter), getter(getter) {} + : setter(setter), + getter(getter) {} template const T & ValueBroker::get() { diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 8b396af..58fee2a 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -10,7 +10,10 @@ using namespace crepe; Animator::Animator(uint32_t id, Sprite & ss, int row, int col, int col_animator) - : Component(id), spritesheet(ss), row(row), col(col) { + : Component(id), + spritesheet(ss), + row(row), + col(col) { dbg_trace(); animator_rect = spritesheet.sprite_rect; diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 820a6a8..6355a03 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -10,7 +10,8 @@ using namespace crepe; Camera::Camera(uint32_t id, const Color & bg_color) - : Component(id), bg_color(bg_color) { + : Component(id), + bg_color(bg_color) { dbg_trace(); } diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp index 0805a08..405f7c4 100644 --- a/src/crepe/facade/DB.cpp +++ b/src/crepe/facade/DB.cpp @@ -63,4 +63,3 @@ bool DB::has(const std::string & key) noexcept { } return true; } - diff --git a/src/makefile b/src/makefile index 778e2c9..5f80204 100644 --- a/src/makefile +++ b/src/makefile @@ -1,56 +1,6 @@ .PHONY: FORCE -# STEPS FOR BIG CLEANUP -# -# 1. Change TODO to your name (in capitals) for each file in the list below -# that is yours (or you are going to fix) -# 2. Update the name between parentheses below this list (see comment) to your -# name -# 3. Create a git commit at this point (ensure `git status` reports "working -# tree clean") -# 4. Run `make format` in the REPOSITORY ROOT DIRECTORY (NOT HERE), and start -# fixing reported errors or miscorrections manually until everything works -# again. -# 5. Once everything is working again, create another git commit, and create a -# pull request. Make sure to ask someone to review the code standards for -# each ENTIRE FILE in this pull request. - -TODO += crepe/Collider.cpp -TODO += crepe/Collider.h -TODO += crepe/Particle.cpp -TODO += crepe/Particle.h -TODO += crepe/Position.h -TODO += crepe/api/AssetManager.cpp -TODO += crepe/api/AssetManager.h -TODO += crepe/api/AssetManager.hpp -TODO += crepe/api/CircleCollider.h -TODO += crepe/api/Color.cpp -TODO += crepe/api/Color.h -TODO += crepe/api/ParticleEmitter.cpp -TODO += crepe/api/ParticleEmitter.h -TODO += crepe/api/Vector2.h -TODO += crepe/api/Vector2.cpp -TODO += crepe/api/Sprite.cpp -TODO += crepe/api/Sprite.h -TODO += crepe/api/Texture.cpp -TODO += crepe/api/Texture.h -TODO += crepe/facade/SDLContext.cpp -TODO += crepe/facade/SDLContext.h -TODO += crepe/system/CollisionSystem.cpp -TODO += crepe/system/CollisionSystem.h -TODO += crepe/system/ParticleSystem.cpp -TODO += crepe/system/ParticleSystem.h -TODO += crepe/system/RenderSystem.cpp -TODO += crepe/system/RenderSystem.h -TODO += example/asset_manager.cpp -TODO += example/components_internal.cpp -TODO += example/particle.cpp -TODO += example/rendering.cpp - -FMT := $(LOEK) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 +FMT := $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') format: FORCE clang-tidy -p build/compile_commands.json --fix-errors $(FMT) -# FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') -# TODO: re-enable linter after all corrections - -- cgit v1.2.3 From 3e94ecb3dac5003a3d58210ed1a4d1f1cb2083d1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 12 Nov 2024 22:43:32 +0100 Subject: add script unit tests + major refactoring --- src/CMakeLists.txt | 1 - src/crepe/Asset.cpp | 18 ++++++---- src/crepe/Asset.h | 2 +- src/crepe/Collider.cpp | 1 - src/crepe/Collider.h | 2 +- src/crepe/Component.cpp | 4 +-- src/crepe/Component.h | 21 ++++++----- src/crepe/ComponentManager.cpp | 16 +++++---- src/crepe/ComponentManager.h | 27 +++++++-------- src/crepe/ComponentManager.hpp | 5 ++- src/crepe/Resource.h | 33 ++++++++++++++++++ src/crepe/api/Animator.cpp | 4 +-- src/crepe/api/Animator.h | 3 +- src/crepe/api/BehaviorScript.cpp | 4 +++ src/crepe/api/BehaviorScript.h | 20 +++++++++-- src/crepe/api/BehaviorScript.hpp | 4 +-- src/crepe/api/CMakeLists.txt | 10 +++--- src/crepe/api/Camera.cpp | 7 ++-- src/crepe/api/Camera.h | 2 +- src/crepe/api/GameObject.cpp | 9 ++--- src/crepe/api/GameObject.h | 17 ++++++--- src/crepe/api/GameObject.hpp | 2 +- src/crepe/api/Metadata.cpp | 4 +-- src/crepe/api/Metadata.h | 2 +- src/crepe/api/ParticleEmitter.cpp | 4 +-- src/crepe/api/ParticleEmitter.h | 2 +- src/crepe/api/Rigidbody.cpp | 4 +-- src/crepe/api/Rigidbody.h | 2 +- src/crepe/api/Script.cpp | 1 + src/crepe/api/Script.h | 15 ++++++-- src/crepe/api/Script.hpp | 6 ++-- src/crepe/api/Sprite.cpp | 4 +-- src/crepe/api/Sprite.h | 2 +- src/crepe/api/Transform.cpp | 4 +-- src/crepe/api/Transform.h | 2 +- src/crepe/facade/Sound.cpp | 31 ++++++++--------- src/crepe/facade/Sound.h | 14 ++++---- src/crepe/facade/SoundContext.cpp | 6 +--- src/crepe/facade/SoundContext.h | 13 ++++--- src/crepe/system/CMakeLists.txt | 19 +++++----- src/crepe/system/ScriptSystem.cpp | 23 ++++++++---- src/crepe/system/ScriptSystem.h | 5 +-- src/crepe/system/System.cpp | 6 ++++ src/crepe/system/System.h | 7 +++- src/test/CMakeLists.txt | 6 ++-- src/test/PhysicsTest.cpp | 4 ++- src/test/ScriptTest.cpp | 73 +++++++++++++++++++++++++++++++++++++++ src/test/audio.cpp | 10 ------ src/test/dummy.cpp | 3 -- src/test/main.cpp | 15 ++++++++ 50 files changed, 338 insertions(+), 161 deletions(-) create mode 100644 src/crepe/Resource.h create mode 100644 src/crepe/system/System.cpp create mode 100644 src/test/ScriptTest.cpp delete mode 100644 src/test/audio.cpp delete mode 100644 src/test/dummy.cpp create mode 100644 src/test/main.cpp (limited to 'src/crepe/api') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e4922df..445a8b2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,6 +38,5 @@ install( target_link_libraries(test_main PRIVATE gtest - PRIVATE gtest_main PUBLIC crepe ) diff --git a/src/crepe/Asset.cpp b/src/crepe/Asset.cpp index 8a2a11c..3d4df53 100644 --- a/src/crepe/Asset.cpp +++ b/src/crepe/Asset.cpp @@ -1,16 +1,20 @@ #include #include "Asset.h" +#include "Exception.h" using namespace crepe; +using namespace std; -Asset::Asset(const std::string & src) { - // FIXME: restore this - // this->src = std::filesystem::canonical(src); - this->src = src; - this->file = std::ifstream(this->src, std::ios::in | std::ios::binary); +Asset::Asset(const string & src) : src(src) { + try { + this->src = filesystem::canonical(src); + } catch (filesystem::filesystem_error & e) { + throw Exception("Asset error: %s", e.what()); + } + this->file = ifstream(this->src, ios::in | ios::binary); } -const std::istream & Asset::read() { return this->file; } +const istream & Asset::read() { return this->file; } -const char * Asset::canonical() { return this->src.c_str(); } +const char * Asset::canonical() const { return this->src.c_str(); } diff --git a/src/crepe/Asset.h b/src/crepe/Asset.h index 0cb5834..d4e4ba1 100644 --- a/src/crepe/Asset.h +++ b/src/crepe/Asset.h @@ -23,7 +23,7 @@ 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(); + const char * canonical() const; private: std::string src; diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp index bbec488..b408609 100644 --- a/src/crepe/Collider.cpp +++ b/src/crepe/Collider.cpp @@ -2,4 +2,3 @@ using namespace crepe; -Collider::Collider(game_object_id_t id) : Component(id) {} diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h index 827f83d..a1e9095 100644 --- a/src/crepe/Collider.h +++ b/src/crepe/Collider.h @@ -6,7 +6,7 @@ namespace crepe { class Collider : public Component { public: - Collider(game_object_id_t id); + using Component::Component; int size; }; diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 73c2d07..c133739 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -1,6 +1,6 @@ #include "Component.h" -#include "types.h" using namespace crepe; -Component::Component(game_object_id_t id) : game_object_id(id) {} +Component::Component(const Data & data) : data(data) {} + diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 0fe60b2..c6d72df 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -2,8 +2,6 @@ #include "types.h" -#include - namespace crepe { class ComponentManager; @@ -15,13 +13,21 @@ class ComponentManager; * interface for all components. */ class Component { +public: + struct Data { + //! The ID of the GameObject this component belongs to + const game_object_id_t id; + //! The manager of this component + ComponentManager & component_manager; + }; + protected: - //! Only the ComponentManager can create components - friend class crepe::ComponentManager; /** - * \param id The id of the GameObject this component belongs to + * \param base Data */ - Component(game_object_id_t id); + Component(const Data & base); + //! Only the ComponentManager can create components + friend class crepe::ComponentManager; public: virtual ~Component() = default; @@ -37,8 +43,7 @@ public: virtual int get_instances_max() const { return -1; } public: - //! The id of the GameObject this component belongs to - const game_object_id_t game_object_id; + Data data; //! Whether the component is active bool active = true; }; diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 85149c8..f6acc1a 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -1,13 +1,10 @@ +#include "api/GameObject.h" #include "util/log.h" #include "ComponentManager.h" using namespace crepe; - -ComponentManager & ComponentManager::get_instance() { - static ComponentManager instance; - return instance; -} +using namespace std; void ComponentManager::delete_all_components_of_id(game_object_id_t id) { // Loop through all the types (in the unordered_map<>) @@ -26,5 +23,12 @@ void ComponentManager::delete_all_components() { } ComponentManager::ComponentManager() { dbg_trace(); } - ComponentManager::~ComponentManager() { dbg_trace(); } + +GameObject & ComponentManager::new_object(const string & name, const string & tag, const Vector2 & position, double rotation, double scale) { + GameObject * object = new GameObject(*this, this->next_id, name, tag, position, rotation, scale); + this->objects.push_front(unique_ptr(object)); + this->next_id++; + return *object; +} + diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index c8c196c..e37bc4a 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -1,15 +1,18 @@ #pragma once -#include #include #include #include #include +#include #include "Component.h" +#include "api/Vector2.h" namespace crepe { +class GameObject; + /** * \brief Manages all components * @@ -18,18 +21,10 @@ namespace crepe { */ class ComponentManager { public: - /** - * \brief Get the instance of the ComponentManager - * - * \return The instance of the ComponentManager - */ - static ComponentManager & get_instance(); - ComponentManager(const ComponentManager &) = delete; - ComponentManager(ComponentManager &&) = delete; - ComponentManager & operator=(const ComponentManager &) = delete; - ComponentManager & operator=(ComponentManager &&) = delete; - ~ComponentManager(); + ComponentManager(); // dbg_trace + ~ComponentManager(); // dbg_trace +public: /** * \brief Add a component to the ComponentManager * @@ -100,8 +95,8 @@ public: template std::vector> get_components_by_type() const; -private: - ComponentManager(); + // TODO: doxygen + GameObject & new_object(const std::string & name, const std::string & tag = "", const Vector2 & position = { 0, 0 }, double rotation = 0, double scale = 0); private: /** @@ -118,6 +113,10 @@ private: std::unordered_map>>> components; + + //! ID of next GameObject + game_object_id_t next_id = 0; + std::forward_list> objects; }; } // namespace crepe diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 98efb49..89a8536 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -31,7 +31,10 @@ T & ComponentManager::add_component(game_object_id_t id, Args &&... args) { // Create a new component of type T (arguments directly forwarded). The // constructor must be called by ComponentManager. - T * instance_ptr = new T(id, forward(args)...); + T * instance_ptr = new T(Component::Data { + .id = id, + .component_manager = *this, + }, forward(args)...); if (instance_ptr == nullptr) throw std::bad_alloc(); T & instance_ref = *instance_ptr; diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h new file mode 100644 index 0000000..dcf3dbd --- /dev/null +++ b/src/crepe/Resource.h @@ -0,0 +1,33 @@ +#pragma once + +#include + +namespace crepe { + +class ResourceManager; +class Asset; + +/** + * Resource is an interface class used to represent a (deserialized) game + * resource (e.g. textures, sounds). + */ +class Resource { +private: + /** + * \brief Prototype pattern clone function. + * + * \param src Source file of new resource (abstraction for file saved on + * disk) + * + * \returns New instance of concrete resource + */ + virtual std::unique_ptr clone(const Asset & src) const = 0; + /** + * The resource manager uses \c clone to create new instances of the concrete + * resource class. This may be used to inherit references to classes that + * would otherwise need to be implemented as singletons. + */ + friend class ResourceManager; +}; + +} // namespace crepe diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 58fee2a..ad60981 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -9,8 +9,8 @@ using namespace crepe; -Animator::Animator(uint32_t id, Sprite & ss, int row, int col, int col_animator) - : Component(id), +Animator::Animator(const Component::Data & data, Sprite & ss, int row, int col, int col_animator) + : Component(data), spritesheet(ss), row(row), col(col) { diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index def0240..f66dc1a 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -6,6 +6,7 @@ #include "Sprite.h" namespace crepe { + class AnimatorSystem; class SDLContext; @@ -35,7 +36,7 @@ public: * * This constructor sets up the Animator with the given parameters, and initializes the animation system. */ - Animator(uint32_t id, Sprite & spritesheet, int row, int col, + Animator(const Component::Data & data, Sprite & spritesheet, int row, int col, int col_animate); ~Animator(); // dbg_trace diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index e69de29..ce1cfde 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -0,0 +1,4 @@ +#include "BehaviorScript.h" + +using namespace crepe; + diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 6b1fec7..8d21a72 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -12,19 +12,35 @@ class Script; class BehaviorScript : public Component { protected: - friend class crepe::ComponentManager; using Component::Component; + //! Only ComponentManager is allowed to instantiate BehaviorScript + friend class ComponentManager; public: virtual ~BehaviorScript() = default; public: + /** + * \brief Set the concrete script of this component + * + * \tparam T Concrete script type (derived from \c crepe::Script) + * + * \returns Reference to BehaviorScript component (`*this`) + */ template BehaviorScript & set_script(); protected: - friend class crepe::ScriptSystem; + //! ScriptSystem needs direct access to the script instance + friend class ScriptSystem; + //! Flag to indicate if script->init() has been called already + bool initialized = false; std::unique_ptr