Skip to content

Commit

Permalink
fix ecs
Browse files Browse the repository at this point in the history
  • Loading branch information
broken-bytes committed Mar 4, 2024
1 parent d21ae05 commit 1a2559f
Show file tree
Hide file tree
Showing 33 changed files with 377 additions and 313 deletions.
2 changes: 1 addition & 1 deletion editor/app/src/editor/Editor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace kyanite::editor {
engineThread = std::thread([this]() {
kyanitemain(true);
});
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
kyaniteeditormain();

try {
Expand Down
20 changes: 19 additions & 1 deletion editor/core/include/editor/core/Bridge_EditorCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,30 @@ extern "C" {
* @param p_open A pointer to a boolean that will be set to true if the window is open
* @param flags The flags to use for the window
*/
EXPORTED void EditorCore_BeginWindow(const char* title, bool* p_open, int flags);
EXPORTED void EditorCore_BeginWindow(
const char* title,
int flags,
int64_t id,
void (*callback)(int64_t)
);

/**
* @brief End a window
*/
EXPORTED void EditorCore_EndWindow();

/**
* @brief Draw a button
* @param label The label of the button
* @return True if the button was clicked
*/
EXPORTED bool EditorCore_Button(const char* label);

/**
* @brief Draw a label
* @param label The label to draw
*/
EXPORTED void EditorCore_Label(const char* label);
#ifdef __cplusplus
}
#endif
9 changes: 5 additions & 4 deletions editor/core/include/editor/core/EditorCore.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ namespace kyanite::editor::core {
/**
* @brief Begin a window
* @param title The title of the window
* @param open A pointer to a boolean that indicates if the window is open
* @param flags The flags to use
* @param id The id of the window. Needed for the callback
* @param callback The callback to call when the window is closed
*/
EXPORTED auto BeginWindow(std::string title, bool* open, int flags) -> void;
EXPORTED auto BeginWindow(std::string title, int flags, int64_t id, void(*callback)(int64_t)) -> void;

/**
* @brief End a window
Expand All @@ -43,14 +44,14 @@ namespace kyanite::editor::core {
* @param label The label to display
* @param action The callback to call when the button is pressed
*/
EXPORTED auto DrawButton(NativePointer window, std::string label, std::function<void()> action) -> void;
EXPORTED auto Button(std::string label) -> bool;

/**
* @brief Draws a label
* @param window The window to draw the label in
* @param label The label to display
*/
EXPORTED auto DrawLabel(NativePointer window, std::string label) -> void;
EXPORTED auto Label(std::string label) -> void;

/**
* @brief Clears the window
Expand Down
17 changes: 15 additions & 2 deletions editor/core/src/editor/core/Bridge_EditorCore.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@ void EditorCore_DrawDefaultDocking() {
kyanite::editor::core::DrawDefaultImGuiDocking();
}

void EditorCore_BeginWindow(const char* title, bool* p_open, int flags) {
kyanite::editor::core::BeginWindow(title, p_open, flags);
void EditorCore_BeginWindow(
const char* title,
int flags,
int64_t id,
void (*callback)(int64_t)
) {
kyanite::editor::core::BeginWindow(title, flags, id, callback);
}

void EditorCore_EndWindow() {
kyanite::editor::core::EndWindow();
}

bool EditorCore_Button(const char* label) {
return kyanite::editor::core::Button(label);
}

void EditorCore_Label(const char* label) {
kyanite::editor::core::Label(label);
}

23 changes: 13 additions & 10 deletions editor/core/src/editor/core/EditorCore.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,29 @@ namespace kyanite::editor::core {
ImGui::SetCurrentContext(context);
}

auto BeginWindow(std::string title, bool* open, int flags) -> void {
Rendering_StartWindow(title.c_str(), open);
auto BeginWindow(std::string title, int flags, int64_t id, void (*callback)(int64_t)) -> void {
bool isOpen = true;
ImGui::Begin(title.c_str(), &isOpen);

if(!isOpen) {
callback(id);
}
}

auto EndWindow() -> void {
Rendering_EndWindow();
ImGui::End();
}

auto DrawReferenceSelector(NativePointer window, std::string label, std::function<void(std::string)> onReferenceSet) -> void {

}

auto DrawButton(NativePointer window, std::string label, std::function<void()> action) -> void {

auto Button(std::string label) -> bool {
return ImGui::Button(label.c_str());
}

auto DrawLabel(NativePointer window, std::string label) -> void {

auto Label(std::string label) -> void {
ImGui::Text(label.c_str());
}

auto Clear(NativePointer window) -> void {
Expand All @@ -50,9 +55,7 @@ namespace kyanite::editor::core {
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoBackground |
ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoNavFocus |
ImGuiWindowFlags_NoNav
ImGuiWindowFlags_NoBringToFrontOnFocus
);
ImGuiID dockId = ImGui::GetID("MyDockSpace");
static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode;
Expand Down
15 changes: 15 additions & 0 deletions engine/ecs/include/ecs/Bridge_ECS.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ extern "C" {
*/
EXPORTED uint64_t ECS_CreateEntity(const char* name);

/**
* @brief Sets the parent of an entity
* @param entity The entity to set the parent of
* @param parent The parent to set
*/
EXPORTED void ECS_SetParent(uint64_t entity, uint64_t parent);

/**
* @brief Destroys an entity
*
Expand Down Expand Up @@ -55,6 +62,14 @@ extern "C" {
*/
EXPORTED void ECS_RemoveComponent(uint64_t entity, uint64_t component);

/**
* @brief Gets a component from an entity
* @param entity The entity to get the component from
* @param component The component to get
* @return The component
*/
EXPORTED NativePointer ECS_GetComponent(uint64_t entity, uint64_t component);

/**
* @brief Registers a component type
* @param size The size of the component type
Expand Down
15 changes: 15 additions & 0 deletions engine/ecs/include/ecs/EntityRegistry.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ namespace ecs::EntityRegistry {
*/
auto CreateEntity(std::string name) -> ecs_entity_t;

/**
* @brief Sets the parent of an entity
* @param entity The entity to set the parent of
* @param parent The parent to set
*/
auto SetParent(ecs_entity_t entity, ecs_entity_t parent) -> void;

/**
* @brief Destroys an entity
* @param entity The entity to destroy
Expand Down Expand Up @@ -71,6 +78,14 @@ namespace ecs::EntityRegistry {
*/
auto RemoveComponent(ecs_entity_t entity, ecs_entity_t component) -> void;

/**
* @brief Gets a component from an entity
* @param entity The entity to get the component from
* @param component The component to get
* @return The component
*/
auto GetComponent(ecs_entity_t entity, ecs_entity_t component) -> const void*;

/**
* @brief Advances the ecs world by delta time
* @param delta The delta time
Expand Down
11 changes: 11 additions & 0 deletions engine/ecs/src/Bridge_ECS.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ uint64_t ECS_CreateEntity(const char* name) {
return ecs::EntityRegistry::CreateEntity(name);
}

void ECS_SetParent(uint64_t entity, uint64_t parent) {
ecs::EntityRegistry::SetParent(entity, parent);
}

void ECS_DestroyEntity(uint64_t entity) {
ecs::EntityRegistry::DestroyEntity(entity);
}
Expand All @@ -38,6 +42,13 @@ void ECS_RemoveComponent(uint64_t entity, uint64_t component) {

}

NativePointer ECS_GetComponent(uint64_t entity, uint64_t component) {
auto data = ecs::EntityRegistry::GetComponent(entity, component);
const auto native = reinterpret_cast<const NativePointer*>(data);

return const_cast<NativePointer*>(native);
}

uint64_t ECS_RegisterComponent(const char* name, size_t size, size_t alignment) {
return ecs::EntityRegistry::CreateComponent(name, size, alignment);
}
Expand Down
39 changes: 27 additions & 12 deletions engine/ecs/src/EntityRegistry.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
#include <flecs/addons/rest.h>

#include <memory>
#include <shared_mutex>
#include <mutex>
#include <Windows.h>

namespace ecs::EntityRegistry {
flecs::world world;
std::mutex worldLock;
std::mutex writeLock;
std::shared_mutex readLock;

auto Init(bool debugServer) -> void {

// If we are in debug mode, we want to start the debug server and load all component metadata
if (debugServer) {
world.set<flecs::Rest>({ });
Expand All @@ -40,17 +41,26 @@ namespace ecs::EntityRegistry {
}

auto GetRegistry() -> ecs_world_t* {
std::scoped_lock lock { worldLock };
std::shared_lock<std::shared_mutex> lock { readLock };
return world;
}

auto CreateEntity(std::string name) -> ecs_entity_t {
std::scoped_lock lock { worldLock };
return world.entity(name.c_str());
std::scoped_lock lock { writeLock };

return world.entity(name.c_str()).id();
}

auto SetParent(ecs_entity_t entity, ecs_entity_t parent) -> void {
std::scoped_lock lock { writeLock };
// Remove the current parent
world.entity(entity).remove(flecs::ChildOf);
// Add the new parent
world.entity(entity).add(flecs::ChildOf, parent);
}

auto DestroyEntity(ecs_entity_t entity) -> void {
std::scoped_lock lock{ worldLock };
std::scoped_lock lock{ writeLock };
world.entity(entity).destruct();
}

Expand All @@ -64,30 +74,35 @@ namespace ecs::EntityRegistry {
ecs_entity_desc_t entityDesc = {};
entityDesc.name = name.c_str();

std::scoped_lock lock{ worldLock };
std::scoped_lock lock { writeLock };

desc.entity = ecs_entity_init(world, &entityDesc);

return ecs_component_init(world, &desc);
}

auto AddComponent(ecs_entity_t entity, ecs_entity_t component) -> void {
std::scoped_lock lock{ worldLock };
std::scoped_lock lock { writeLock };
ecs_add_id(world, entity, component);
}

auto SetComponent(ecs_entity_t entity, ecs_entity_t component, void* data) -> void {
std::scoped_lock lock{ worldLock };
std::scoped_lock lock { writeLock };
world.entity(entity).set_ptr(component, data);
}

auto RemoveComponent(ecs_entity_t entity, ecs_entity_t component) -> void {
std::scoped_lock lock{ worldLock };
std::scoped_lock lock { writeLock };
ecs_remove_id(world, entity, component);
}

auto GetComponent(ecs_entity_t entity, ecs_entity_t component) -> const void* {
std::shared_lock<std::shared_mutex> lock { readLock };
return world.entity(entity).get(component);
}

auto Update(float delta) -> void {
std::scoped_lock lock{ worldLock };
std::scoped_lock lock { writeLock };
world.progress();
}

Expand All @@ -96,7 +111,7 @@ namespace ecs::EntityRegistry {
ecs_entity_desc_t entityDesc = {};
entityDesc.name = name.c_str();

std::scoped_lock lock{ worldLock };
std::scoped_lock lock { writeLock };
entityDesc.add[0] = ecs_pair(EcsDependsOn, EcsOnUpdate);
ecs_entity_t system = ecs_entity_init(world, &entityDesc);
desc.entity = system;
Expand Down
22 changes: 12 additions & 10 deletions engine/input/src/Input.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,18 @@ namespace kyanite::engine::input {
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);

if(event.type == SDL_QUIT) {
SystemEvent quitEvent = {};
quitEvent.type = SystemEventType::Quit;

Event* event = new Event();
event->type = EventType::System;
event->data.system = quitEvent;

for(auto& subscriber: inputEventSubscribers) {
subscriber(event);
if(event.type == SDL_WINDOWEVENT) {
if(event.window.event == SDL_WINDOWEVENT_CLOSE) {
SystemEvent quitEvent = {};
quitEvent.type = SystemEventType::Quit;

Event* event = new Event();
event->type = EventType::System;
event->data.system = quitEvent;

for(auto& subscriber: inputEventSubscribers) {
subscriber(event);
}
}
}

Expand Down
Loading

0 comments on commit 1a2559f

Please sign in to comment.