Compare commits

...

3 commits

Author SHA1 Message Date
maelstrom 4b441adff3 some datamodel shit 2024-10-09 16:08:41 +02:00
maelstrom 3df83a13b4 This might could work?? 2024-10-01 22:59:47 +02:00
maelstrom ddd89a2b6d Groundwork for datamodel 2024-10-01 22:08:21 +02:00
9 changed files with 177 additions and 5 deletions

20
src/core/value.h Normal file
View file

@ -0,0 +1,20 @@
#pragma once
#include <string>
enum ValueType {
OB_NIL = 0,
OB_BOOLEAN,
OB_INTEGER,
OB_FLOAT,
OB_STRING,
};
struct Value {
ValueType type;
union {
bool b;
int i;
float f;
std::string str;
};
};

View file

@ -0,0 +1,53 @@
#include "instance.h"
#include "instancetype.h"
#include <algorithm>
#include <memory>
#include <optional>
#include <vector>
InstanceType TYPE_INSTANCE {
.superClass = nullptr,
.className = "Instance",
.flags = OB_INST_NOT_CREATABLE,
};
std::optional<std::shared_ptr<Instance>> Instance::GetParent() {
if (!this->Parent.has_value() || this->Parent.value().expired())
return std::nullopt;
return this->Parent.value().lock();
}
void Instance::SetParent(std::optional<std::shared_ptr<Instance>> newParent) {
// Remove this instance from the existing parent
if (this->Parent.has_value() && !this->Parent.value().expired()) {
std::shared_ptr<Instance> parent = this->Parent.value().lock();
parent->children.erase(std::remove_if(parent->children.begin(), parent->children.end(), [this](std::shared_ptr<Instance> ptr) {
return ptr.get() == this;
}));
}
// Add self to
if (newParent.has_value()) {
newParent.value()->children.push_back(this->shared_from_this());
}
this->Parent = newParent;
}
std::vector<std::shared_ptr<Instance>> Instance::GetChildren() {
return this->children;
}
std::unique_ptr<Instance> Instance::CloneInternal() {
// TODO: Implement refs
std::unique_ptr<Instance> clone = this->__copy();
clone->children = std::vector<std::shared_ptr<Instance>>();
clone->children.reserve(this->children.size());
for (std::shared_ptr<Instance> child : this->children) {
clone->children.push_back(child->CloneInternal());
}
return clone;
}

39
src/datamodel/instance.h Normal file
View file

@ -0,0 +1,39 @@
#pragma once
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "instancetype.h"
#define INSTANCE_COPY_IMPL(CLASS_NAME) std::unique_ptr<Instance> CLASS_NAME::__copy() { return std::make_unique<CLASS_NAME>(CLASS_NAME(*this)); }
#define INSTANCE_PRELUDE(CLASS_NAME) protected: virtual std::unique_ptr<Instance> __copy() override; virtual InstanceType& GetType() const override;
// #define INSTANCE_TYPE(CLASS_NAME, TYPE_DEFINITION) InstanceType& CLASS_NAME::GetType() const { \
// static InstanceType type = TYPE_DEFINITION; \
// return type; \
// };
#define INSTANCE_TYPE(CLASS_NAME, TYPE_CONSTANT) InstanceType& CLASS_NAME::GetType() const { return TYPE_CONSTANT; };
extern InstanceType TYPE_INSTANCE;
// The base class for all objects in the data model
class Instance : std::enable_shared_from_this<Instance> {
private:
std::optional<std::weak_ptr<Instance>> Parent;
std::vector<std::shared_ptr<Instance>> children;
protected:
virtual std::unique_ptr<Instance> __copy();
public:
std::string Name;
std::optional<std::shared_ptr<Instance>> GetParent();
void SetParent(std::optional<std::shared_ptr<Instance>> newParent);
std::vector<std::shared_ptr<Instance>> GetChildren();
virtual void Init();
std::unique_ptr<Instance> CloneInternal();
virtual InstanceType& GetType() const = 0;
virtual Value GetProperty(std::string key);
virtual void SetProperty(std::string key, Value newValue);
virtual Value CallMethod(std::string key, std::vector<Value> arguments);
};

View file

@ -0,0 +1,39 @@
#pragma once
#include <map>
#include <string>
#include <vector>
#include "../core/value.h"
enum PropertyFlags {
OB_PROP_READ_ONLY = 1, // The property cannot be set
// OB_PROP_CALLBACK = 2, // The proeprty is get/set through a function instead of directly to a field
};
enum MethodFlags {
// OB_METHOD_OVERLOADED = 1, // The method has multiple definitions with different parameters suffixed by __ followed by a number
};
enum InstanceFlags {
OB_INST_NOT_CREATABLE = 1,
};
struct PropertyInfo {
PropertyFlags flags;
void* fieldPointer;
ValueType valueType;
};
struct MethodInfo {
MethodFlags flags;
void* functionPointer;
ValueType returnType;
std::vector<ValueType> parameterTypes;
};
struct InstanceType {
InstanceType* superClass = nullptr;
std::string className;
InstanceFlags flags;
std::map<std::string, PropertyInfo> properties;
std::map<std::string, MethodInfo> methods;
};

15
src/datamodel/part.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "part.h"
#include "instance.h"
#include "instancetype.h"
#include <map>
INSTANCE_COPY_IMPL(Part)
InstanceType TYPE_PART {
.superClass = &TYPE_INSTANCE,
.className = "Part",
.properties = std::map<std::string, PropertyInfo>(),
.methods = std::map<std::string, MethodInfo>(),
};
INSTANCE_TYPE(Part, TYPE_PART)

View file

@ -3,11 +3,17 @@
#include <glm/fwd.hpp>
#include <glm/gtc/quaternion.hpp>
#include <reactphysics3d/body/RigidBody.h>
#include "rendering/material.h"
#include "../rendering/material.h"
#include "instance.h"
#include "instancetype.h"
namespace rp = reactphysics3d;
struct Part {
extern InstanceType TYPE_PART;
class Part : public Instance {
INSTANCE_PRELUDE(Part)
public:
glm::vec3 position;
glm::quat rotation = glm::identity<glm::quat>();
glm::vec3 scale;

View file

@ -8,7 +8,7 @@
#include <stdio.h>
#include <vector>
#include "part.h"
#include "datamodel/part.h"
#include "rendering/renderer.h"
#include "physics/simulation.h"
#include "camera.h"

View file

@ -1,6 +1,6 @@
#pragma once
#include "../part.h"
#include "../datamodel/part.h"
void simulationInit();
void syncPartPhysics(Part& part);

View file

@ -12,7 +12,7 @@
#include "mesh.h"
#include "defaultmeshes.h"
#include "../camera.h"
#include "../part.h"
#include "../datamodel/part.h"
#include "renderer.h"