Skip to content

Change execution options, update to handle entity creation in system execution #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 14, 2023
Merged
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
35 changes: 0 additions & 35 deletions ecsact/runtime/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,6 @@ typedef void (*ecsact_execute_sys_error_callback)(
void* callback_user_data
);

/**
* When an entity is sucessfully created this callback is
* invoked.
*
* @param entity_id the entity id of the created entity
* @param request_id the request ID returned by the create entity callback
* @param callback_user_data the @ref
* ecsact_async_events_collector::error_callback_user_data
*/
typedef void (*ecsact_async_create_entity_callback)(
//
ecsact_entity_id entity_id,
ecsact_async_request_id request_id,
void* callback_user_data
);

typedef struct ecsact_async_events_collector {
/**
* invoked when an async request failed.
Expand All @@ -130,19 +114,6 @@ typedef struct ecsact_async_events_collector {
*/
void* async_error_callback_user_data;

/**
* Invoked when a create entity request succeeds.
* @see ecsact_async_create_entity_callback
* @see ecsact_entity_id
* @see ecsact_async_error
*/
ecsact_async_create_entity_callback async_entity_callback;

/**
* `callback_user_data` passed to `async_entity_callback`
*/
void* async_entity_callback_user_data;

/**
* invoked when a system execution error occurred.
* @see ecsact_execute_sys_error_callback
Expand Down Expand Up @@ -203,11 +174,6 @@ ECSACT_ASYNC_API_FN(ecsact_async_request_id, ecsact_async_connect)
*/
ECSACT_ASYNC_API_FN(void, ecsact_async_disconnect)(void);

/**
* Returns an entity
*/
ECSACT_ASYNC_API_FN(ecsact_async_request_id, ecsact_async_create_entity)(void);

/**
* Gets the current tick
*/
Expand All @@ -218,7 +184,6 @@ ECSACT_ASYNC_API_FN(int32_t, ecsact_async_get_current_tick)(void);
fn(ecsact_async_flush_events, __VA_ARGS__); \
fn(ecsact_async_connect, __VA_ARGS__); \
fn(ecsact_async_disconnect, __VA_ARGS__); \
fn(ecsact_async_create_entity, __VA_ARGS__); \
fn(ecsact_async_get_current_tick, __VA_ARGS__);

#undef ECSACT_ASYNC_API
Expand Down
11 changes: 11 additions & 0 deletions ecsact/runtime/core.hh
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ public:
destroy_entities.push_back(entity_id);
}

inline void push_action(const ecsact_action& action) {
actions.push_back(action);
}

inline void clear() {
options = {};

Expand All @@ -176,6 +180,8 @@ public:

create_entities.clear();
destroy_entities.clear();

actions.clear();
}

inline ecsact_execution_options c() {
Expand Down Expand Up @@ -204,6 +210,9 @@ public:
options.destroy_entities = destroy_entities.data();
options.destroy_entities_length = destroy_entities.size();

options.actions = actions.data();
options.actions_length = actions.size();

return options;
}

Expand All @@ -223,6 +232,8 @@ private:
std::vector<int> entities_component_lengths;
std::vector<ecsact_component*> entities_components;

std::vector<ecsact_action> actions;

ecsact_execution_options options;
};

Expand Down
4 changes: 1 addition & 3 deletions reference/async_reference/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ cc_library(
deps = [
"//:async",
"//:core",
"//reference/async_reference/request_id_factory",
"//reference/async_reference/callbacks:async_callbacks",
"//reference/async_reference/callbacks:execution_callbacks",
"//reference/async_reference/detail/c_execution_options",
"//reference/async_reference/entity_manager",
"//reference/async_reference/request_id_factory",
"//reference/async_reference/tick_manager",
"//reference/async_reference/util",
"//reference/async_reference/util:types",
Expand All @@ -50,7 +49,6 @@ filegroup(
]) + [
"//reference/async_reference/callbacks:callback_source",
"//reference/async_reference/detail/c_execution_options:c_execution_source",
"//reference/async_reference/entity_manager:entity_source",
"//reference/async_reference/tick_manager:tick_source",
"//reference/async_reference/util:util_source",
"//reference/serialize_reference:serialize_source",
Expand Down
14 changes: 0 additions & 14 deletions reference/async_reference/async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,6 @@ void ecsact_async_flush_events(
}
}

ecsact_async_request_id ecsact_async_create_entity() {
auto req_id = request_id_factory.next_id();
if(!reference) {
async_callbacks.add(detail::types::async_error{
.error = ECSACT_ASYNC_ERR_PERMISSION_DENIED,
.request_ids = {req_id},
});
return req_id;
}

reference->create_entity_request(req_id);
return req_id;
}

ecsact_async_request_id ecsact_async_enqueue_execution_options(
const ecsact_execution_options options
) {
Expand Down
17 changes: 0 additions & 17 deletions reference/async_reference/async_reference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ void async_reference::execute_systems() {
);
}
auto options = c_exec_options.c();
entity_manager.process_entities(async_callbacks, *registry_id);

auto exec_lk = exec_callbacks.lock();
auto systems_error =
Expand All @@ -195,22 +194,6 @@ void async_reference::invoke_execution_events(
}
}

void async_reference::create_entity_request(ecsact_async_request_id req_id) {
if(is_connected == false && is_connected_notified == false) {
types::async_error async_err{
.error = ECSACT_ASYNC_ERR_PERMISSION_DENIED,
.request_ids = {req_id},
};

async_callbacks.add(async_err);
is_connected_notified = true;

return;
}

entity_manager.request_entity(req_id);
}

int32_t async_reference::get_current_tick() {
return tick_manager.get_current_tick();
}
Expand Down
3 changes: 0 additions & 3 deletions reference/async_reference/async_reference.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "reference/async_reference/tick_manager/tick_manager.hh"
#include "reference/async_reference/callbacks/execution_callbacks.hh"
#include "reference/async_reference/callbacks/async_callbacks.hh"
#include "reference/async_reference/entity_manager/entity_manager.hh"
#include "request_id_factory/request_id_factory.hh"

namespace ecsact::async_reference::detail {
Expand All @@ -44,7 +43,6 @@ public:

int32_t get_current_tick();

void create_entity_request(ecsact_async_request_id req_id);
void connect(ecsact_async_request_id req_id, const char* connection_string);

void disconnect();
Expand All @@ -56,7 +54,6 @@ private:

tick_manager tick_manager;
execution_callbacks exec_callbacks;
entity_manager entity_manager;
detail::async_callbacks& async_callbacks;

std::thread execution_thread;
Expand Down
9 changes: 0 additions & 9 deletions reference/async_reference/callbacks/async_callbacks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ void async_callbacks::invoke(const ecsact_async_events_collector* async_events
error,
async_events->system_error_callback_user_data
);
} else if constexpr(std::is_same_v<T, types::entity>) {
if(async_events->async_entity_callback == nullptr) {
return;
}
async_events->async_entity_callback(
*error.entity_id,
error.request_id,
async_events->async_entity_callback_user_data
);
}
},
request
Expand Down
76 changes: 75 additions & 1 deletion reference/async_reference/callbacks/execution_callbacks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ execution_callbacks::execution_callbacks() {
collector.init_callback = &execution_callbacks::init_callback;
collector.update_callback = &execution_callbacks::update_callback;
collector.remove_callback = &execution_callbacks::remove_callback;
collector.entity_created_callback =
&execution_callbacks::entity_created_callback;
collector.entity_destroyed_callback =
&execution_callbacks::entity_destroyed_callback;
collector.init_callback_user_data = this;
collector.update_callback_user_data = this;
collector.remove_callback_user_data = this;
collector.entity_created_callback_user_data = this;
collector.entity_destroyed_callback_user_data = this;
}

ecsact_execution_events_collector* execution_callbacks::get_collector() {
Expand All @@ -29,6 +35,8 @@ void execution_callbacks::invoke(
init_callbacks_info.clear();
update_callbacks_info.clear();
remove_callbacks_info.clear();
create_entity_callbacks_info.clear();
destroy_entity_callbacks_info.clear();
}
return;
}
Expand All @@ -37,16 +45,25 @@ void execution_callbacks::invoke(
std::vector<types::callback_info> update_callbacks;
std::vector<types::callback_info> remove_callbacks;

std::vector<types::entity_callback_info> entity_created_callbacks;
std::vector<types::entity_callback_info> entity_destroyed_callbacks;

std::unique_lock lk(execution_m);

init_callbacks = std::move(init_callbacks_info);
update_callbacks = std::move(update_callbacks_info);
remove_callbacks = std::move(remove_callbacks_info);

entity_created_callbacks = std::move(create_entity_callbacks_info);
entity_destroyed_callbacks = std::move(destroy_entity_callbacks_info);

init_callbacks_info.clear();
update_callbacks_info.clear();
remove_callbacks_info.clear();

create_entity_callbacks_info.clear();
destroy_entity_callbacks_info.clear();

lk.unlock();

if(execution_events->init_callback != nullptr) {
Expand Down Expand Up @@ -111,6 +128,26 @@ void execution_callbacks::invoke(
}
}
}

if(execution_events->entity_created_callback != nullptr) {
for(auto& info : entity_created_callbacks) {
execution_events->entity_created_callback(
info.event,
info.entity_id,
execution_events->entity_created_callback_user_data
);
}
}

if(execution_events->entity_destroyed_callback != nullptr) {
for(auto& info : entity_destroyed_callbacks) {
execution_events->entity_destroyed_callback(
info.event,
info.entity_id,
execution_events->entity_destroyed_callback_user_data
);
}
}
}

bool execution_callbacks::has_callbacks() {
Expand All @@ -126,6 +163,14 @@ bool execution_callbacks::has_callbacks() {
return true;
}

if(create_entity_callbacks_info.size() > 0) {
return true;
}

if(destroy_entity_callbacks_info.size() > 0) {
return true;
}

return false;
}

Expand Down Expand Up @@ -199,7 +244,6 @@ void execution_callbacks::remove_callback(
init_cb_info.entity_id == entity_id;
});

// Maybe I can store data from here?
std::erase_if(self->update_callbacks_info, [&](auto& update_cb_info) {
return update_cb_info.component_id == component_id &&
update_cb_info.entity_id == entity_id;
Expand All @@ -226,3 +270,33 @@ void execution_callbacks::remove_callback(
// maybe...?
self->remove_callbacks_info.push_back(info);
}

void execution_callbacks::entity_created_callback(
ecsact_event event,
ecsact_entity_id entity_id,
void* callback_user_data
) {
auto self = static_cast<execution_callbacks*>(callback_user_data);

auto info = types::entity_callback_info{};

info.event = event;
info.entity_id = entity_id;

self->create_entity_callbacks_info.push_back(info);
}

void execution_callbacks::entity_destroyed_callback(
ecsact_event event,
ecsact_entity_id entity_id,
void* callback_user_data
) {
auto self = static_cast<execution_callbacks*>(callback_user_data);

auto info = types::entity_callback_info{};

info.event = event;
info.entity_id = entity_id;

self->destroy_entity_callbacks_info.push_back(info);
}
15 changes: 15 additions & 0 deletions reference/async_reference/callbacks/execution_callbacks.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ private:
std::vector<types::callback_info> update_callbacks_info;
std::vector<types::callback_info> remove_callbacks_info;

std::vector<types::entity_callback_info> create_entity_callbacks_info;
std::vector<types::entity_callback_info> destroy_entity_callbacks_info;

bool has_callbacks();

static void init_callback(
Expand All @@ -59,5 +62,17 @@ private:
const void* component_data,
void* callback_user_data
);

static void entity_created_callback(
ecsact_event event,
ecsact_entity_id entity_id,
void* callback_user_data
);

static void entity_destroyed_callback(
ecsact_event event,
ecsact_entity_id entity_id,
void* callback_user_data
);
};
} // namespace ecsact::async_reference::detail
Loading