From ace974504e410eb7d98ca021d75511d8f84b9bdc Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 7 Nov 2024 20:58:54 +0100 Subject: start cleanup --- src/makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/makefile b/src/makefile index b9c44c8..ee189e3 100644 --- a/src/makefile +++ b/src/makefile @@ -95,7 +95,7 @@ LOEK += test/audio.cpp LOEK += test/dummy.cpp JARO += test/PhysicsTest.cpp -FMT := $(JARO) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 +FMT := $(LOEK) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 format: FORCE clang-tidy -p build/compile_commands.json --fix-errors $(FMT) -- cgit v1.2.3 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 --- .clang-format | 1 + mwe/ecs-homemade/src/Components.cpp | 4 +- mwe/ecs-homemade/src/GameObjectMax.cpp | 6 ++- .../inc/ContiguousContainer.hpp | 3 +- mwe/ecs-memory-efficient/src/Components.cpp | 4 +- mwe/ecs-memory-efficient/src/GameObjectMax.cpp | 6 ++- mwe/events/include/customTypes.h | 4 +- mwe/events/include/eventHandler.h | 3 +- mwe/events/src/event.cpp | 31 +++++++++---- mwe/events/src/main.cpp | 4 +- mwe/events/src/uiObject.cpp | 13 ++++-- mwe/gameloop/src/gameObject.cpp | 7 ++- src/crepe/Exception.cpp | 5 +- src/crepe/Exception.h | 3 +- src/crepe/ValueBroker.h | 7 +-- src/crepe/ValueBroker.hpp | 11 ++--- 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 +- src/crepe/facade/DB.cpp | 25 +++++----- src/crepe/facade/DB.h | 7 ++- src/crepe/util/Proxy.h | 7 ++- src/crepe/util/Proxy.hpp | 9 ++-- src/example/db.cpp | 4 +- src/example/proxy.cpp | 20 ++++---- src/example/savemgr.cpp | 13 +++--- src/makefile | 2 - 34 files changed, 179 insertions(+), 118 deletions(-) diff --git a/.clang-format b/.clang-format index 3ae6c28..8ce4033 100644 --- a/.clang-format +++ b/.clang-format @@ -23,6 +23,7 @@ ReflowComments: false AlignEscapedNewlines: DontAlign BreakBeforeBinaryOperators: All AlwaysBreakTemplateDeclarations: Yes +PackConstructorInitializers: CurrentLine ... # vim: ft=yaml diff --git a/mwe/ecs-homemade/src/Components.cpp b/mwe/ecs-homemade/src/Components.cpp index de8753e..0d62bd5 100644 --- a/mwe/ecs-homemade/src/Components.cpp +++ b/mwe/ecs-homemade/src/Components.cpp @@ -6,7 +6,9 @@ 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) {} + : 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 b0c5af7..753c8e2 100644 --- a/mwe/ecs-homemade/src/GameObjectMax.cpp +++ b/mwe/ecs-homemade/src/GameObjectMax.cpp @@ -4,4 +4,8 @@ GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, int layer) - : mId(id), mName(name), mTag(tag), mActive(true), mLayer(layer) {} + : mId(id), + mName(name), + mTag(tag), + mActive(true), + mLayer(layer) {} diff --git a/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp b/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp index 408d5aa..ff8fde4 100644 --- a/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp +++ b/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp @@ -1,5 +1,6 @@ template -ContiguousContainer::ContiguousContainer() : mSize(0), mCapacity(10) { +ContiguousContainer::ContiguousContainer() : mSize(0), + mCapacity(10) { // Allocate memory for 10 objects initially mData = static_cast(malloc(mCapacity * sizeof(T))); if (!mData) { diff --git a/mwe/ecs-memory-efficient/src/Components.cpp b/mwe/ecs-memory-efficient/src/Components.cpp index c8347b3..2ec8609 100644 --- a/mwe/ecs-memory-efficient/src/Components.cpp +++ b/mwe/ecs-memory-efficient/src/Components.cpp @@ -6,6 +6,8 @@ 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) {} + : mMass(mass), + mGravityScale(gravityScale), + mBodyType(bodyType) {} Colider::Colider(int size) : mSize(size) {} diff --git a/mwe/ecs-memory-efficient/src/GameObjectMax.cpp b/mwe/ecs-memory-efficient/src/GameObjectMax.cpp index b0c5af7..753c8e2 100644 --- a/mwe/ecs-memory-efficient/src/GameObjectMax.cpp +++ b/mwe/ecs-memory-efficient/src/GameObjectMax.cpp @@ -4,4 +4,8 @@ GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, int layer) - : mId(id), mName(name), mTag(tag), mActive(true), mLayer(layer) {} + : mId(id), + mName(name), + mTag(tag), + mActive(true), + mLayer(layer) {} diff --git a/mwe/events/include/customTypes.h b/mwe/events/include/customTypes.h index a5d8dc9..415b989 100644 --- a/mwe/events/include/customTypes.h +++ b/mwe/events/include/customTypes.h @@ -33,6 +33,8 @@ struct Collision { // Constructor to initialize a Collision Collision(int idA, int idB, const Vector2 & point, const Vector2 & normal, float depth) - : objectIdA(idA), objectIdB(idB), contactPoint(point), + : objectIdA(idA), + objectIdB(idB), + contactPoint(point), contactNormal(normal) {} }; diff --git a/mwe/events/include/eventHandler.h b/mwe/events/include/eventHandler.h index aa8f63b..3a83b15 100644 --- a/mwe/events/include/eventHandler.h +++ b/mwe/events/include/eventHandler.h @@ -24,7 +24,8 @@ class EventHandlerWrapper : public IEventHandlerWrapper { public: explicit EventHandlerWrapper(const EventHandler & handler, const bool destroyOnSuccess = false) - : m_handler(handler), m_handlerType(m_handler.target_type().name()), + : m_handler(handler), + m_handlerType(m_handler.target_type().name()), m_destroyOnSuccess(destroyOnSuccess) { // std::cout << m_handlerType << std::endl; } diff --git a/mwe/events/src/event.cpp b/mwe/events/src/event.cpp index 0c9f3ed..8ffa0b1 100644 --- a/mwe/events/src/event.cpp +++ b/mwe/events/src/event.cpp @@ -23,7 +23,9 @@ void Event::markHandled() { isHandled = true; } // KeyPressedEvent class methods KeyPressedEvent::KeyPressedEvent(int keycode) - : Event("KeyPressedEvent"), key(keycode), repeatCount(0) {} + : Event("KeyPressedEvent"), + key(keycode), + repeatCount(0) {} Keycode KeyPressedEvent::getKeyCode() const { return key; } @@ -31,13 +33,16 @@ int KeyPressedEvent::getRepeatCount() const { return repeatCount; } // KeyReleasedEvent class methods KeyReleasedEvent::KeyReleasedEvent(int keycode) - : Event("KeyReleasedEvent"), key(keycode) {} + : Event("KeyReleasedEvent"), + key(keycode) {} Keycode KeyReleasedEvent::getKeyCode() const { return key; } // MousePressedEvent class methods MousePressedEvent::MousePressedEvent(int mouseX, int mouseY) - : Event("MousePressedEvent"), mouseX(mouseX), mouseY(mouseY) {} + : Event("MousePressedEvent"), + mouseX(mouseX), + mouseY(mouseY) {} std::pair MousePressedEvent::getMousePosition() const { return {mouseX, mouseY}; @@ -45,26 +50,36 @@ std::pair MousePressedEvent::getMousePosition() const { //Collision event CollisionEvent::CollisionEvent(Collision collision) - : collisionData(collision), Event("CollisionEvent") {} + : collisionData(collision), + Event("CollisionEvent") {} Collision CollisionEvent::getCollisionData() const { return this->collisionData; } TextSubmitEvent::TextSubmitEvent(std::string text) - : text(text), Event("TextSubmitEvent") {} + : text(text), + Event("TextSubmitEvent") {} std::string TextSubmitEvent::getText() const { return this->text; } MouseReleasedEvent::MouseReleasedEvent(int x, int y, MouseButton button) - : mouseX(x), mouseY(y), button(button), Event("MouseReleased") {} + : mouseX(x), + mouseY(y), + button(button), + Event("MouseReleased") {} std::pair MouseReleasedEvent::getMousePosition() const { return {mouseX, mouseY}; } MouseClickEvent::MouseClickEvent(int x, int y, MouseButton button) - : mouseX(x), mouseY(y), button(button), Event("MouseClickEvent") {} + : mouseX(x), + mouseY(y), + button(button), + Event("MouseClickEvent") {} MouseMovedEvent::MouseMovedEvent(int x, int y) - : mouseX(x), mouseY(y), Event("MouseMovedEvent") {} + : mouseX(x), + mouseY(y), + Event("MouseMovedEvent") {} std::pair MouseClickEvent::getMousePosition() const { return {mouseX, mouseY}; } diff --git a/mwe/events/src/main.cpp b/mwe/events/src/main.cpp index d49cf74..f4e7390 100644 --- a/mwe/events/src/main.cpp +++ b/mwe/events/src/main.cpp @@ -11,7 +11,9 @@ class PlayerDamagedEvent : public Event { public: PlayerDamagedEvent(int damage, int playerID) - : Event("PlayerDamaged"), damage(damage), playerID(playerID) {} + : Event("PlayerDamaged"), + damage(damage), + playerID(playerID) {} REGISTER_EVENT_TYPE(PlayerDamagedEvent); diff --git a/mwe/events/src/uiObject.cpp b/mwe/events/src/uiObject.cpp index 8405469..947d1a2 100644 --- a/mwe/events/src/uiObject.cpp +++ b/mwe/events/src/uiObject.cpp @@ -7,7 +7,9 @@ UIObject::UIObject(int width, int height) : width(width), height(height) {} Button::Button(int width, int height) : UIObject(width, height) {} Text::Text(int width, int height) - : UIObject(width, height), size(12), font(nullptr), + : UIObject(width, height), + size(12), + font(nullptr), color{255, 255, 255} { // Default size and color alignment.horizontal = Alignment::Horizontal::CENTER; alignment.vertical = Alignment::Vertical::MIDDLE; @@ -15,8 +17,13 @@ Text::Text(int width, int height) } TextInput::TextInput(int width, int height) - : UIObject(width, height), textBuffer(""), placeholder(""), isActive(false), - textColor{255, 255, 255}, backgroundColor{0, 0, 0}, maxLength(100), + : UIObject(width, height), + textBuffer(""), + placeholder(""), + isActive(false), + textColor{255, 255, 255}, + backgroundColor{0, 0, 0}, + maxLength(100), font(nullptr) { alignment.horizontal = Alignment::Horizontal::LEFT; alignment.vertical = Alignment::Vertical::TOP; diff --git a/mwe/gameloop/src/gameObject.cpp b/mwe/gameloop/src/gameObject.cpp index 78217c4..b33dc78 100644 --- a/mwe/gameloop/src/gameObject.cpp +++ b/mwe/gameloop/src/gameObject.cpp @@ -26,5 +26,10 @@ void GameObject::setVelY(float value) { velY = value; } GameObject::GameObject(std::string name, float x, float y, float width, float height, float velX, float velY) - : name(name), x(x), y(y), width(width), height(height), velX(velX), + : name(name), + x(x), + y(y), + width(width), + height(height), + velX(velX), velY(velY) {} diff --git a/src/crepe/Exception.cpp b/src/crepe/Exception.cpp index f27d5a8..dab8f2e 100644 --- a/src/crepe/Exception.cpp +++ b/src/crepe/Exception.cpp @@ -6,9 +6,7 @@ using namespace std; using namespace crepe; -const char * Exception::what() { - return error.c_str(); -} +const char * Exception::what() { return error.c_str(); } Exception::Exception(const char * fmt, ...) { va_list args; @@ -16,4 +14,3 @@ Exception::Exception(const char * fmt, ...) { this->error = va_stringf(args, fmt); va_end(args); } - diff --git a/src/crepe/Exception.h b/src/crepe/Exception.h index e4a7bb8..6473043 100644 --- a/src/crepe/Exception.h +++ b/src/crepe/Exception.h @@ -17,7 +17,6 @@ protected: Exception() = default; //! Formatted error message std::string error; - }; -} +} // namespace crepe diff --git a/src/crepe/ValueBroker.h b/src/crepe/ValueBroker.h index 88988b4..d844d6a 100644 --- a/src/crepe/ValueBroker.h +++ b/src/crepe/ValueBroker.h @@ -23,10 +23,12 @@ public: virtual const T & get(); typedef std::function setter_t; - typedef std::function getter_t; + typedef std::function getter_t; + private: setter_t setter; getter_t getter; + public: /** * \param setter Function that sets the variable @@ -35,7 +37,6 @@ public: ValueBroker(const setter_t & setter, const getter_t & getter); }; -} +} // namespace crepe #include "ValueBroker.hpp" - diff --git a/src/crepe/ValueBroker.hpp b/src/crepe/ValueBroker.hpp index 0d08333..5c3bed9 100644 --- a/src/crepe/ValueBroker.hpp +++ b/src/crepe/ValueBroker.hpp @@ -5,11 +5,9 @@ namespace crepe { template -ValueBroker::ValueBroker(const setter_t & setter, const getter_t & getter) : - setter(setter), - getter(getter) - { -} +ValueBroker::ValueBroker(const setter_t & setter, const getter_t & getter) + : setter(setter), + getter(getter) {} template const T & ValueBroker::get() { @@ -21,5 +19,4 @@ void ValueBroker::set(const T & value) { this->setter(value); } -} - +} // namespace crepe 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(); } diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp index c885560..405f7c4 100644 --- a/src/crepe/facade/DB.cpp +++ b/src/crepe/facade/DB.cpp @@ -1,7 +1,7 @@ #include -#include "util/log.h" #include "Exception.h" +#include "util/log.h" #include "DB.h" @@ -16,20 +16,20 @@ DB::DB(const string & path) { libdb::DB * db; if ((ret = libdb::db_create(&db, NULL, 0)) != 0) throw Exception("db_create: %s", libdb::db_strerror(ret)); - this->db = { db, [] (libdb::DB * db) { db->close(db, 0); } }; + this->db = {db, [](libdb::DB * db) { db->close(db, 0); }}; // load or create database file - if ((ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL, libdb::DB_BTREE, DB_CREATE, 0)) != 0) - throw Exception("db->open: %s", libdb::db_strerror(ret)); + ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL, + libdb::DB_BTREE, DB_CREATE, 0); + if (ret != 0) throw Exception("db->open: %s", libdb::db_strerror(ret)); // create cursor libdb::DBC * cursor; - if ((ret = this->db->cursor(this->db.get(), NULL, &cursor, 0)) != 0) - throw Exception("db->cursor: %s", libdb::db_strerror(ret)); - this->cursor = { cursor, [] (libdb::DBC * cursor) { cursor->close(cursor); } }; + ret = this->db->cursor(this->db.get(), NULL, &cursor, 0); + if (ret != 0) throw Exception("db->cursor: %s", libdb::db_strerror(ret)); + this->cursor = {cursor, [](libdb::DBC * cursor) { cursor->close(cursor); }}; } - libdb::DBT DB::to_thing(const string & thing) const noexcept { libdb::DBT thang; memset(&thang, 0, sizeof(libdb::DBT)); @@ -44,17 +44,15 @@ string DB::get(const string & key) { memset(&db_val, 0, sizeof(libdb::DBT)); int ret = this->cursor->get(this->cursor.get(), &db_key, &db_val, DB_FIRST); - if (ret != 0) - throw Exception("cursor->get: %s", libdb::db_strerror(ret)); - return { static_cast(db_val.data), db_val.size }; + if (ret != 0) throw Exception("cursor->get: %s", libdb::db_strerror(ret)); + return {static_cast(db_val.data), db_val.size}; } void DB::set(const string & key, const string & value) { libdb::DBT db_key = this->to_thing(key); libdb::DBT db_val = this->to_thing(value); int ret = this->db->put(this->db.get(), NULL, &db_key, &db_val, 0); - if (ret != 0) - throw Exception("cursor->get: %s", libdb::db_strerror(ret)); + if (ret != 0) throw Exception("cursor->get: %s", libdb::db_strerror(ret)); } bool DB::has(const std::string & key) noexcept { @@ -65,4 +63,3 @@ bool DB::has(const std::string & key) noexcept { } return true; } - diff --git a/src/crepe/facade/DB.h b/src/crepe/facade/DB.h index b62a974..7c757a2 100644 --- a/src/crepe/facade/DB.h +++ b/src/crepe/facade/DB.h @@ -1,14 +1,14 @@ #pragma once -#include #include #include +#include namespace libdb { extern "C" { #include } -} +} // namespace libdb namespace crepe { @@ -71,5 +71,4 @@ private: libdb::DBT to_thing(const std::string & thing) const noexcept; }; -} - +} // namespace crepe diff --git a/src/crepe/util/Proxy.h b/src/crepe/util/Proxy.h index fbfed0c..f84e462 100644 --- a/src/crepe/util/Proxy.h +++ b/src/crepe/util/Proxy.h @@ -16,9 +16,9 @@ template class Proxy { public: //! Set operator - Proxy & operator = (const T &); + Proxy & operator=(const T &); //! Get operator - operator const T & (); + operator const T &(); public: Proxy(ValueBroker); @@ -27,7 +27,6 @@ private: ValueBroker broker; }; -} +} // namespace crepe #include "Proxy.hpp" - diff --git a/src/crepe/util/Proxy.hpp b/src/crepe/util/Proxy.hpp index 4aec9e9..b9923db 100644 --- a/src/crepe/util/Proxy.hpp +++ b/src/crepe/util/Proxy.hpp @@ -5,18 +5,17 @@ namespace crepe { template -Proxy::Proxy(ValueBroker broker) : broker(broker) { } +Proxy::Proxy(ValueBroker broker) : broker(broker) {} template -Proxy & Proxy::operator = (const T & val) { +Proxy & Proxy::operator=(const T & val) { this->broker.set(val); return *this; } template -Proxy::operator const T & () { +Proxy::operator const T &() { return this->broker.get(); } -} - +} // namespace crepe diff --git a/src/example/db.cpp b/src/example/db.cpp index c046421..8c06a84 100644 --- a/src/example/db.cpp +++ b/src/example/db.cpp @@ -1,12 +1,12 @@ -#include #include +#include #include using namespace crepe; using namespace std; // run before main -static auto _ = [] () { +static auto _ = []() { auto & cfg = Config::get_instance(); cfg.log.level = LogLevel::TRACE; return 0; diff --git a/src/example/proxy.cpp b/src/example/proxy.cpp index 9f54f96..0afff41 100644 --- a/src/example/proxy.cpp +++ b/src/example/proxy.cpp @@ -5,15 +5,15 @@ #include #include -#include #include +#include using namespace std; using namespace crepe; -void test_ro_ref(const int & val) { } -void test_rw_ref(int & val) { } -void test_ro_val(int val) { } +void test_ro_ref(const int & val) {} +void test_rw_ref(int & val) {} +void test_ro_val(int val) {} int main() { auto & cfg = Config::get_instance(); @@ -21,18 +21,19 @@ int main() { int real_value = 0; - ValueBroker broker { - [&real_value] (const int & target) { - dbg_logf("set %s to %s", to_string(real_value).c_str(), to_string(target).c_str()); + ValueBroker broker{ + [&real_value](const int & target) { + dbg_logf("set %s to %s", to_string(real_value).c_str(), + to_string(target).c_str()); real_value = target; }, - [&real_value] () -> const int & { + [&real_value]() -> const int & { dbg_logf("get %s", to_string(real_value).c_str()); return real_value; }, }; - Proxy proxy { broker }; + Proxy proxy{broker}; broker.set(54); proxy = 84; @@ -43,4 +44,3 @@ int main() { return 0; } - diff --git a/src/example/savemgr.cpp b/src/example/savemgr.cpp index c8dd2bc..436fb5a 100644 --- a/src/example/savemgr.cpp +++ b/src/example/savemgr.cpp @@ -4,21 +4,21 @@ */ #include -#include -#include -#include #include +#include +#include +#include using namespace crepe; // unrelated setup code -int _ = [] () { +int _ = []() { // make sure all log messages get printed auto & cfg = Config::get_instance(); cfg.log.level = LogLevel::TRACE; return 0; // satisfy compiler -} (); +}(); int main() { const char * key = "mygame.test"; @@ -27,7 +27,7 @@ int main() { dbg_logf("has key = %s", mgr.has(key) ? "true" : "false"); ValueBroker prop = mgr.get(key, 0); - Proxy val = mgr.get(key, 0); + Proxy val = mgr.get(key, 0); dbg_logf("val = %d", mgr.get(key).get()); prop.set(1); @@ -42,4 +42,3 @@ int main() { return 0; } - diff --git a/src/makefile b/src/makefile index ee189e3..d9c5a57 100644 --- a/src/makefile +++ b/src/makefile @@ -32,8 +32,6 @@ TODO += crepe/Position.h TODO += crepe/api/AssetManager.cpp TODO += crepe/api/AssetManager.h TODO += crepe/api/AssetManager.hpp -LOEK += crepe/api/AudioSource.cpp -LOEK += crepe/api/AudioSource.h LOEK += crepe/api/BehaviorScript.cpp LOEK += crepe/api/BehaviorScript.h LOEK += crepe/api/BehaviorScript.hpp -- 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 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 067247360d68042ad5466b802399338c14d7dc58 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 8 Nov 2024 11:38:16 +0100 Subject: fix code standard issue in Exception --- src/crepe/Exception.cpp | 2 +- src/crepe/Exception.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crepe/Exception.cpp b/src/crepe/Exception.cpp index dab8f2e..bfdbcdd 100644 --- a/src/crepe/Exception.cpp +++ b/src/crepe/Exception.cpp @@ -6,7 +6,7 @@ using namespace std; using namespace crepe; -const char * Exception::what() { return error.c_str(); } +const char * Exception::what() const noexcept { return error.c_str(); } Exception::Exception(const char * fmt, ...) { va_list args; diff --git a/src/crepe/Exception.h b/src/crepe/Exception.h index 6473043..80af068 100644 --- a/src/crepe/Exception.h +++ b/src/crepe/Exception.h @@ -11,7 +11,7 @@ public: //! printf Exception(const char * fmt, ...); //! Get formatted error message - const char * what(); + const char * what() const noexcept; protected: Exception() = default; -- 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(-) 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 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