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)$).*$'
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
12 changes: 12 additions & 0 deletions include/data_structures/Context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef CONTEXT_HPP
#define CONTEXT_HPP

#include <string>
#include <vector>

struct Context {
double timestamp;
std::vector<std::string> markers;
};

#endif // CONTEXT_HPP
11 changes: 11 additions & 0 deletions include/data_structures/EEGData.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef EEGDATA_HPP
#define EEGDATA_HPP

#include <vector>

struct EEGData {
double timestamp;
std::vector<double> channelValues;
};

#endif // EEGDATA_HPP
11 changes: 11 additions & 0 deletions include/data_structures/Marker.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef MARKER_HPP
#define MARKER_HPP

#include <string>

struct Marker {
std::string name;
double timestamp;
};

#endif // MARKER_HPP
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
28 changes: 28 additions & 0 deletions include/renderer/Renderer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef RENDERER_HPP
#define RENDERER_HPP

#include <SDL2/SDL.h>
#include <concurrentqueue.h>

#include <memory>
#include <thread>

class Scene;
struct Marker;

class Renderer {
public:
Renderer() = default;
~Renderer() = default;

void render(const std::stop_token& stoken);

private:
std::unique_ptr<SDL_Window> window;
std::shared_ptr<SDL_Renderer> sdlRenderer;
std::weak_ptr<Scene> currentScene;
std::shared_ptr<moodycamel::ConcurrentQueue<Marker>> markerQueue;
std::jthread renderThread;
};

#endif // RENDERER_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;

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
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ add_library(runtime_core OBJECT
scene/components/ComponentRegistry.cpp
scene/components/BlinkComponent.cpp
scene/SceneObject.cpp
scene/Scene.cpp
renderer/Renderer.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);
}
45 changes: 45 additions & 0 deletions src/renderer/Renderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "renderer/Renderer.hpp"

#include "data_structures/Context.hpp"
#include "data_structures/Marker.hpp"
#include "lsl_cpp.h"
#include "scene/Scene.hpp"

void Renderer::render(const std::stop_token& stoken) {
std::vector<std::string> currentFrameMarkers;
auto lastTime = std::chrono::high_resolution_clock::now();

while (!stoken.stop_requested()) {
auto currentTime = std::chrono::high_resolution_clock::now();
double deltaTime = std::chrono::duration<double>(currentTime - lastTime).count();
lastTime = currentTime;

currentFrameMarkers.clear();
Context ctx{deltaTime, currentFrameMarkers};

SDL_Event event;
while (SDL_PollEvent(&event) == 1) {
if (event.type == SDL_QUIT) {
return;
}
}

if (auto scene = currentScene.lock()) {
scene->update(ctx);
}

SDL_RenderClear(sdlRenderer.get());

if (auto scene = currentScene.lock()) {
scene->render(sdlRenderer.get());
}

SDL_RenderPresent(sdlRenderer.get());

double exactTime = lsl::local_clock();

for (const auto& markerName : currentFrameMarkers) {
markerQueue->enqueue(Marker{markerName, exactTime});
}
}
}
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);
}
}
}
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