diff --git a/.clang-tidy b/.clang-tidy index 0842f8d..3def4bd 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,4 @@ Checks: 'bugprone-*,cppcoreguidelines-*,performance-*,readability-*,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-array-to-pointer-decay' WarningsAsErrors: '' -HeaderFilterRegex: '.*' -ExcludeHeaderFilterRegex: '.*\.(pb\.h|pb\.cc)$' +HeaderFilterRegex: '^(?!.*\.(pb\.h|pb\.cc)$).*$' FormatStyle: 'file' \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 326ad2e..cd61016 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.14) project(NeuronIDE CXX) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) enable_testing() diff --git a/include/parser/Parser.hpp b/include/parser/Parser.hpp index d19fb98..26bf0dd 100644 --- a/include/parser/Parser.hpp +++ b/include/parser/Parser.hpp @@ -22,7 +22,8 @@ class Parser { private: static std::shared_ptr buildSceneObject(const NeuronIDE::SceneObject& protoObj); - static std::unique_ptr buildComponent(const NeuronIDE::Component& protoComp); + static std::unique_ptr buildComponent(const NeuronIDE::Component& protoComp, + const std::shared_ptr& owner); }; #endif // PARSER_HPP \ No newline at end of file diff --git a/include/scene/Scene.hpp b/include/scene/Scene.hpp index 77c6be5..d0c1025 100644 --- a/include/scene/Scene.hpp +++ b/include/scene/Scene.hpp @@ -6,6 +6,8 @@ #include class SceneObject; +class SDL_Renderer; +struct Context; class Scene { private: @@ -19,6 +21,9 @@ class Scene { const std::string& getExperimentName() const { return experimentName; } const std::vector>& getObjects() const { return objects; } + + void update(const Context& ctx); + void render(SDL_Renderer* renderer); }; #endif // SCENE_HPP \ No newline at end of file diff --git a/include/scene/SceneObject.hpp b/include/scene/SceneObject.hpp index bf6ef92..df63ea9 100644 --- a/include/scene/SceneObject.hpp +++ b/include/scene/SceneObject.hpp @@ -7,6 +7,8 @@ #include class Component; +class SDL_Renderer; +struct Context; class SceneObject { public: @@ -21,9 +23,12 @@ class SceneObject { SceneObject(std::string n, bool visible = true); - void setTransform(Transform t); + void setTransform(const Transform& transform); void addComponent(std::unique_ptr comp); + + void update(const Context& ctx); + void render(SDL_Renderer* renderer); }; #endif // SCENEOBJECT_HPP \ No newline at end of file diff --git a/include/scene/components/BlinkComponent.hpp b/include/scene/components/BlinkComponent.hpp index 59816b2..b4814e9 100644 --- a/include/scene/components/BlinkComponent.hpp +++ b/include/scene/components/BlinkComponent.hpp @@ -2,7 +2,6 @@ #define BLINKCOMPONENT_HPP #include -#include #include "Component.hpp" @@ -12,13 +11,15 @@ class Component; class BlinkComponent : public Component { public: - BlinkComponent(double freq) : blinkFrequencyHz(freq) { - std::cout << " + [BlinkComponent] Utworzono z czestotliwoscia: " << blinkFrequencyHz - << "Hz\n"; - } + BlinkComponent(std::shared_ptr owner, double freq) + : Component(owner), blinkFrequencyHz(freq) {} void setFrequency(double freq); - static std::unique_ptr createBlinker(const NeuronIDE::Component& protoComp); + void update(const Context& context) override; + void render(SDL_Renderer* renderer) override; + + static std::unique_ptr createBlinker(const NeuronIDE::Component& protoComp, + const std::shared_ptr& owner); private: double blinkFrequencyHz = 0.0; diff --git a/include/scene/components/Component.hpp b/include/scene/components/Component.hpp index 1b15b63..e17f0c6 100644 --- a/include/scene/components/Component.hpp +++ b/include/scene/components/Component.hpp @@ -1,15 +1,28 @@ #ifndef COMPONENT_HPP #define COMPONENT_HPP +#include + +class SceneObject; +class SDL_Renderer; +struct Context; + class Component { public: - Component() = default; + Component() = delete; + Component(std::shared_ptr owner) : owner(owner) {} virtual ~Component() = default; Component(const Component&) = default; Component(Component&&) = default; Component& operator=(const Component&) = default; Component& operator=(Component&&) = default; + + virtual void update(const Context& context) = 0; + virtual void render(SDL_Renderer* renderer) = 0; + + protected: + std::weak_ptr owner; }; #endif // COMPONENT_HPP \ No newline at end of file diff --git a/include/scene/components/ComponentRegistry.hpp b/include/scene/components/ComponentRegistry.hpp index da89f8a..668dc4a 100644 --- a/include/scene/components/ComponentRegistry.hpp +++ b/include/scene/components/ComponentRegistry.hpp @@ -9,8 +9,10 @@ class Component; namespace NeuronIDE { class Component; } +class SceneObject; -using ComponentCreatorFunc = std::function(const NeuronIDE::Component&)>; +using ComponentCreatorFunc = std::function( + const NeuronIDE::Component&, const std::shared_ptr&)>; class ComponentRegistry { public: @@ -27,7 +29,8 @@ class ComponentRegistry { void registerCreator(int typeId, ComponentCreatorFunc creator); - std::unique_ptr build(const NeuronIDE::Component& protoComp); + std::unique_ptr build(const NeuronIDE::Component& protoComp, + const std::shared_ptr& owner); private: ComponentRegistry() = default; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ced725f..62cd34d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,7 @@ add_library(runtime_core OBJECT scene/components/ComponentRegistry.cpp scene/components/BlinkComponent.cpp scene/SceneObject.cpp + scene/Scene.cpp ${PROTO_SRCS} ${PROTO_HDRS} ) diff --git a/src/parser/Parser.cpp b/src/parser/Parser.cpp index 79de0bc..235dba1 100644 --- a/src/parser/Parser.cpp +++ b/src/parser/Parser.cpp @@ -52,7 +52,7 @@ std::shared_ptr Parser::buildSceneObject(const NeuronIDE::SceneObje } seenComponentTypes.insert(typeId); - auto comp = buildComponent(protoComp); + auto comp = buildComponent(protoComp, obj); if (comp) { obj->addComponent(std::move(comp)); } @@ -61,6 +61,7 @@ std::shared_ptr Parser::buildSceneObject(const NeuronIDE::SceneObje return obj; } -std::unique_ptr Parser::buildComponent(const NeuronIDE::Component& protoComp) { - return ComponentRegistry::instance().build(protoComp); +std::unique_ptr Parser::buildComponent(const NeuronIDE::Component& protoComp, + const std::shared_ptr& owner) { + return ComponentRegistry::instance().build(protoComp, owner); } \ No newline at end of file diff --git a/src/scene/Scene.cpp b/src/scene/Scene.cpp new file mode 100644 index 0000000..9360b5c --- /dev/null +++ b/src/scene/Scene.cpp @@ -0,0 +1,17 @@ +#include "scene/Scene.hpp" + +#include "scene/SceneObject.hpp" + +void Scene::update(const Context& ctx) { + for (const auto& obj : objects) { + obj->update(ctx); + } +} + +void Scene::render(SDL_Renderer* renderer) { + for (const auto& obj : objects) { + if (obj->isVisible) { + obj->render(renderer); + } + } +} \ No newline at end of file diff --git a/src/scene/SceneObject.cpp b/src/scene/SceneObject.cpp index 06d7dae..5a6d948 100644 --- a/src/scene/SceneObject.cpp +++ b/src/scene/SceneObject.cpp @@ -8,8 +8,20 @@ SceneObject::SceneObject(std::string n, bool visible) : name(std::move(n)), isVi std::cout << " [SceneObject] Utworzono obiekt: " << name << "\n"; } -void SceneObject::setTransform(Transform t) { transform = t; } +void SceneObject::setTransform(const Transform& transform) { this->transform = transform; } void SceneObject::addComponent(std::unique_ptr comp) { components.push_back(std::move(comp)); +} + +void SceneObject::update(const Context& ctx) { + for (const auto& comp : components) { + comp->update(ctx); + } +} + +void SceneObject::render(SDL_Renderer* renderer) { + for (const auto& comp : components) { + comp->render(renderer); + } } \ No newline at end of file diff --git a/src/scene/components/BlinkComponent.cpp b/src/scene/components/BlinkComponent.cpp index 3830efa..dbd9503 100644 --- a/src/scene/components/BlinkComponent.cpp +++ b/src/scene/components/BlinkComponent.cpp @@ -5,8 +5,18 @@ void BlinkComponent::setFrequency(double freq) { blinkFrequencyHz = freq; } -std::unique_ptr BlinkComponent::createBlinker(const NeuronIDE::Component& protoComp) { - return std::make_unique(protoComp.blinker().blink_frequency_hz()); +std::unique_ptr BlinkComponent::createBlinker( + const NeuronIDE::Component& protoComp, const std::shared_ptr& owner) { + return std::make_unique(owner, protoComp.blinker().blink_frequency_hz()); +} + +void BlinkComponent::update(const Context& context) { + // TODO: implement blinking logic based on blinkFrequencyHz and context.timestamp +} + +void BlinkComponent::render(SDL_Renderer* renderer) { + // This component does not render anything itself, it only controls visibility of the owner + // object. } REGISTER_COMPONENT(NeuronIDE::Component::kBlinker, BlinkComponent::createBlinker) \ No newline at end of file diff --git a/src/scene/components/ComponentRegistry.cpp b/src/scene/components/ComponentRegistry.cpp index 52faf24..b3419fc 100644 --- a/src/scene/components/ComponentRegistry.cpp +++ b/src/scene/components/ComponentRegistry.cpp @@ -13,14 +13,15 @@ void ComponentRegistry::registerCreator(int typeId, ComponentCreatorFunc creator creators[typeId] = std::move(creator); } -std::unique_ptr ComponentRegistry::build(const NeuronIDE::Component& protoComp) { +std::unique_ptr ComponentRegistry::build(const NeuronIDE::Component& protoComp, + const std::shared_ptr& owner) { auto activeCase = protoComp.component_type_case(); int typeId = static_cast(activeCase); auto it = creators.find(typeId); if (it != creators.end()) { - return it->second(protoComp); + return it->second(protoComp, owner); } return nullptr;