Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -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)$).*$'
Comment thread
MichalSzandar marked this conversation as resolved.
FormatStyle: 'file'
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
3 changes: 2 additions & 1 deletion include/parser/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Parser {

private:
static std::shared_ptr<SceneObject> buildSceneObject(const NeuronIDE::SceneObject& protoObj);
static std::unique_ptr<Component> buildComponent(const NeuronIDE::Component& protoComp);
static std::unique_ptr<Component> buildComponent(const NeuronIDE::Component& protoComp,
const std::shared_ptr<SceneObject>& owner);
};

#endif // PARSER_HPP
5 changes: 5 additions & 0 deletions include/scene/Scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <vector>

class SceneObject;
class SDL_Renderer;
struct Context;

class Scene {
private:
Expand All @@ -19,6 +21,9 @@ class Scene {

const std::string& getExperimentName() const { return experimentName; }
const std::vector<std::shared_ptr<SceneObject>>& getObjects() const { return objects; }

void update(const Context& ctx);
void render(SDL_Renderer* renderer);
};

#endif // SCENE_HPP
7 changes: 6 additions & 1 deletion include/scene/SceneObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <vector>

class Component;
class SDL_Renderer;
struct Context;

class SceneObject {
public:
Expand All @@ -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<Component> comp);

void update(const Context& ctx);
void render(SDL_Renderer* renderer);
};

#endif // SCENEOBJECT_HPP
13 changes: 7 additions & 6 deletions include/scene/components/BlinkComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define BLINKCOMPONENT_HPP

#include <iostream>
#include <memory>

#include "Component.hpp"

Expand All @@ -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<SceneObject> owner, double freq)
: Component(owner), blinkFrequencyHz(freq) {}
void setFrequency(double freq);

static std::unique_ptr<Component> createBlinker(const NeuronIDE::Component& protoComp);
void update(const Context& context) override;
void render(SDL_Renderer* renderer) override;

static std::unique_ptr<Component> createBlinker(const NeuronIDE::Component& protoComp,
const std::shared_ptr<SceneObject>& owner);

private:
double blinkFrequencyHz = 0.0;
Expand Down
15 changes: 14 additions & 1 deletion include/scene/components/Component.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
#ifndef COMPONENT_HPP
#define COMPONENT_HPP

#include <memory>

class SceneObject;
class SDL_Renderer;
struct Context;

class Component {
public:
Component() = default;
Component() = delete;
Component(std::shared_ptr<SceneObject> owner) : owner(owner) {}
virtual ~Component() = default;

Component(const Component&) = default;
Component(Component&&) = default;
Component& operator=(const Component&) = default;
Component& operator=(Component&&) = default;
Comment on lines 16 to 19

virtual void update(const Context& context) = 0;
virtual void render(SDL_Renderer* renderer) = 0;

protected:
std::weak_ptr<SceneObject> owner;
};

#endif // COMPONENT_HPP
7 changes: 5 additions & 2 deletions include/scene/components/ComponentRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class Component;
namespace NeuronIDE {
class Component;
}
class SceneObject;

using ComponentCreatorFunc = std::function<std::unique_ptr<Component>(const NeuronIDE::Component&)>;
using ComponentCreatorFunc = std::function<std::unique_ptr<Component>(
const NeuronIDE::Component&, const std::shared_ptr<SceneObject>&)>;

class ComponentRegistry {
public:
Expand All @@ -27,7 +29,8 @@ class ComponentRegistry {

void registerCreator(int typeId, ComponentCreatorFunc creator);

std::unique_ptr<Component> build(const NeuronIDE::Component& protoComp);
std::unique_ptr<Component> build(const NeuronIDE::Component& protoComp,
const std::shared_ptr<SceneObject>& owner);

private:
ComponentRegistry() = default;
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
Expand Down
7 changes: 4 additions & 3 deletions src/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ std::shared_ptr<SceneObject> Parser::buildSceneObject(const NeuronIDE::SceneObje
}
seenComponentTypes.insert(typeId);

auto comp = buildComponent(protoComp);
auto comp = buildComponent(protoComp, obj);
if (comp) {
obj->addComponent(std::move(comp));
}
Expand All @@ -61,6 +61,7 @@ std::shared_ptr<SceneObject> Parser::buildSceneObject(const NeuronIDE::SceneObje
return obj;
}

std::unique_ptr<Component> Parser::buildComponent(const NeuronIDE::Component& protoComp) {
return ComponentRegistry::instance().build(protoComp);
std::unique_ptr<Component> Parser::buildComponent(const NeuronIDE::Component& protoComp,
const std::shared_ptr<SceneObject>& owner) {
return ComponentRegistry::instance().build(protoComp, owner);
}
17 changes: 17 additions & 0 deletions src/scene/Scene.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Comment thread
MichalSzandar marked this conversation as resolved.
14 changes: 13 additions & 1 deletion src/scene/SceneObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Component> 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);
}
}
14 changes: 12 additions & 2 deletions src/scene/components/BlinkComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@

void BlinkComponent::setFrequency(double freq) { blinkFrequencyHz = freq; }

std::unique_ptr<Component> BlinkComponent::createBlinker(const NeuronIDE::Component& protoComp) {
return std::make_unique<BlinkComponent>(protoComp.blinker().blink_frequency_hz());
std::unique_ptr<Component> BlinkComponent::createBlinker(
const NeuronIDE::Component& protoComp, const std::shared_ptr<SceneObject>& owner) {
return std::make_unique<BlinkComponent>(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)
5 changes: 3 additions & 2 deletions src/scene/components/ComponentRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ void ComponentRegistry::registerCreator(int typeId, ComponentCreatorFunc creator
creators[typeId] = std::move(creator);
}

std::unique_ptr<Component> ComponentRegistry::build(const NeuronIDE::Component& protoComp) {
std::unique_ptr<Component> ComponentRegistry::build(const NeuronIDE::Component& protoComp,
const std::shared_ptr<SceneObject>& owner) {
auto activeCase = protoComp.component_type_case();

int typeId = static_cast<int>(activeCase);

auto it = creators.find(typeId);
if (it != creators.end()) {
return it->second(protoComp);
return it->second(protoComp, owner);
}

return nullptr;
Expand Down
Loading