From e3a58650c8f74c31ef531492d5071ef72009eee3 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Mon, 3 Apr 2023 20:31:50 -0400 Subject: [PATCH] Added 'is_queued_for_deletion' property and function to get full time dilation with physics delta for Nodes from python api (#184) --- crescent_py_api/crescent_api.py | 11 +++++++++ crescent_py_api/crescent_api_internal.py | 8 +++++++ .../src/core/ecs/component/node_component.c | 1 + .../src/core/ecs/component/node_component.h | 1 + engine/src/core/scene/scene_manager.c | 8 +++++++ .../core/scripting/python/cre_py_api_module.c | 23 +++++++++++++++++-- .../core/scripting/python/cre_py_api_module.h | 10 ++++++++ .../scripting/python/crescent_api_source.h | 11 +++++++++ seika/src/rendering/renderer.c | 4 +++- 9 files changed, 74 insertions(+), 3 deletions(-) diff --git a/crescent_py_api/crescent_api.py b/crescent_py_api/crescent_api.py index 0979c5c5d..4dd48db35 100644 --- a/crescent_py_api/crescent_api.py +++ b/crescent_py_api/crescent_api.py @@ -946,6 +946,12 @@ def add_child(self, child_node) -> None: def queue_deletion(self) -> None: crescent_api_internal.node_queue_deletion(entity_id=self.entity_id) + @property + def is_queued_for_deletion(self) -> bool: + return crescent_api_internal.node_is_queued_for_deletion( + entity_id=self.entity_id + ) + def create_event(self, event_id: str) -> None: crescent_api_internal.node_create_event( entity_id=self.entity_id, event_id=event_id @@ -985,6 +991,11 @@ def get_full_time_dilation(self) -> float: entity_id=self.entity_id ) + def get_full_time_dilation_with_physics_delta(self) -> float: + return crescent_api_internal.node_get_full_time_dilation_with_physics_delta( + entity_id=self.entity_id + ) + # 2D class Node2D(Node): diff --git a/crescent_py_api/crescent_api_internal.py b/crescent_py_api/crescent_api_internal.py index 4514cb5d7..8e55a8bde 100644 --- a/crescent_py_api/crescent_api_internal.py +++ b/crescent_py_api/crescent_api_internal.py @@ -331,6 +331,10 @@ def node_queue_deletion(entity_id: int) -> None: pass +def node_is_queued_for_deletion(entity_id: int) -> bool: + return False + + def node_add_child(parent_entity_id: int, child_entity_id: int) -> None: pass @@ -377,6 +381,10 @@ def node_get_full_time_dilation(entity_id: int) -> float: return 1.0 +def node_get_full_time_dilation_with_physics_delta(entity_id: int) -> float: + return 1.0 + + def node2D_set_position(entity_id: int, x: float, y: float) -> None: pass diff --git a/engine/src/core/ecs/component/node_component.c b/engine/src/core/ecs/component/node_component.c index 93935b729..87e8fe32b 100644 --- a/engine/src/core/ecs/component/node_component.c +++ b/engine/src/core/ecs/component/node_component.c @@ -8,6 +8,7 @@ NodeComponent* node_component_create() { NodeComponent* nodeComponent = SE_MEM_ALLOCATE(NodeComponent); nodeComponent->name[0] = '\0'; nodeComponent->type = NodeBaseType_INVALID; + nodeComponent->queuedForDeletion = false; nodeComponent->timeDilation = (NodeTimeDilation) { .value = 1.0f, .cachedFullValue = 1.0f, .cacheInvalid = true }; diff --git a/engine/src/core/ecs/component/node_component.h b/engine/src/core/ecs/component/node_component.h index 38751478b..356df4c9d 100644 --- a/engine/src/core/ecs/component/node_component.h +++ b/engine/src/core/ecs/component/node_component.h @@ -53,6 +53,7 @@ typedef struct NodeTimeDilation { typedef struct NodeComponent { char name[32]; NodeBaseType type; + bool queuedForDeletion; NodeTimeDilation timeDilation; // Called after '_start' is called on an entity SEEvent onSceneTreeEnter; // { data = entity (unsigned int), type = 0 (not used) } diff --git a/engine/src/core/scene/scene_manager.c b/engine/src/core/scene/scene_manager.c index 8961988a8..6f2881f07 100644 --- a/engine/src/core/scene/scene_manager.c +++ b/engine/src/core/scene/scene_manager.c @@ -181,6 +181,14 @@ void cre_queue_destroy_tree_node_entity(SceneTreeNode* treeNode) { } void cre_queue_destroy_tree_node_entity_all(SceneTreeNode* treeNode) { + NodeComponent* nodeComp = cre_component_manager_get_component_unchecked(treeNode->entity, CreComponentDataIndex_NODE); + if (nodeComp) { + if (nodeComp->queuedForDeletion) { + se_logger_warn("Entity '%s' already queued for deletion, skipping queue deletion!", nodeComp->name); + return; + } + nodeComp->queuedForDeletion = true; + } cre_scene_execute_on_all_tree_nodes(treeNode, cre_queue_destroy_tree_node_entity); if (treeNode->parent != NULL) { SE_STATIC_ARRAY_ADD(entitiesToUnlinkParent, treeNode->entity); diff --git a/engine/src/core/scripting/python/cre_py_api_module.c b/engine/src/core/scripting/python/cre_py_api_module.c index e8c009809..98d7b2302 100644 --- a/engine/src/core/scripting/python/cre_py_api_module.c +++ b/engine/src/core/scripting/python/cre_py_api_module.c @@ -713,8 +713,7 @@ PyObject* cre_py_api_camera2D_unfollow_node(PyObject* self, PyObject* args, PyOb // World void py_mark_scene_nodes_time_dilation_flag_dirty(SceneTreeNode* node) { - NodeComponent* nodeComponent = (NodeComponent*) cre_component_manager_get_component_unchecked(node->entity, - CreComponentDataIndex_NODE); + NodeComponent* nodeComponent = (NodeComponent*) cre_component_manager_get_component_unchecked(node->entity, CreComponentDataIndex_NODE); SE_ASSERT(nodeComponent != NULL); nodeComponent->timeDilation.cacheInvalid = true; } @@ -868,6 +867,18 @@ PyObject* cre_py_api_node_queue_deletion(PyObject* self, PyObject* args, PyObjec return NULL; } +PyObject* cre_py_api_node_is_queued_for_deletion(PyObject* self, PyObject* args, PyObject* kwargs) { + CreEntity entity; + if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", crePyApiGenericGetEntityKWList, &entity)) { + NodeComponent* nodeComponent = (NodeComponent*) cre_component_manager_get_component(entity, CreComponentDataIndex_NODE); + if (nodeComponent->queuedForDeletion) { + Py_RETURN_TRUE; + } + Py_RETURN_FALSE; + } + return NULL; +} + PyObject* cre_py_api_node_add_child(PyObject* self, PyObject* args, PyObject* kwargs) { CreEntity parentEntity; CreEntity entity; @@ -990,6 +1001,14 @@ PyObject* cre_py_api_node_get_full_time_dilation(PyObject* self, PyObject* args, return NULL; } +PyObject* cre_py_api_node_get_full_time_dilation_with_physics_delta(PyObject* self, PyObject* args, PyObject* kwargs) { + CreEntity entity; + if (PyArg_ParseTupleAndKeywords(args, kwargs, "i", crePyApiGenericGetEntityKWList, &entity)) { + return Py_BuildValue("f", cre_scene_manager_get_node_full_time_dilation(entity) * CRE_GLOBAL_PHYSICS_DELTA_TIME); + } + return NULL; +} + // Event related stuff void py_api_node_event_callback(void* observerData, NodeEventNotifyPayload* notifyPayload) { PyObject* pyCallbackFunc = (PyObject*) observerData; diff --git a/engine/src/core/scripting/python/cre_py_api_module.h b/engine/src/core/scripting/python/cre_py_api_module.h index 11aba5565..c4fde620d 100644 --- a/engine/src/core/scripting/python/cre_py_api_module.h +++ b/engine/src/core/scripting/python/cre_py_api_module.h @@ -95,6 +95,7 @@ PyObject* cre_py_api_game_properties_get(PyObject* self, PyObject* args); // Node PyObject* cre_py_api_node_new(PyObject* self, PyObject* args, PyObject* kwargs); PyObject* cre_py_api_node_queue_deletion(PyObject* self, PyObject* args, PyObject* kwargs); +PyObject* cre_py_api_node_is_queued_for_deletion(PyObject* self, PyObject* args, PyObject* kwargs); PyObject* cre_py_api_node_add_child(PyObject* self, PyObject* args, PyObject* kwargs); PyObject* cre_py_api_node_get_child(PyObject* self, PyObject* args, PyObject* kwargs); PyObject* cre_py_api_node_get_children(PyObject* self, PyObject* args, PyObject* kwargs); @@ -103,6 +104,7 @@ PyObject* cre_py_api_node_get_name(PyObject* self, PyObject* args, PyObject* kwa PyObject* cre_py_api_node_set_time_dilation(PyObject* self, PyObject* args, PyObject* kwargs); PyObject* cre_py_api_node_get_time_dilation(PyObject* self, PyObject* args, PyObject* kwargs); PyObject* cre_py_api_node_get_full_time_dilation(PyObject* self, PyObject* args, PyObject* kwargs); +PyObject* cre_py_api_node_get_full_time_dilation_with_physics_delta(PyObject* self, PyObject* args, PyObject* kwargs); PyObject* cre_py_api_node_create_event(PyObject* self, PyObject* args, PyObject* kwargs); PyObject* cre_py_api_node_subscribe_to_event(PyObject* self, PyObject* args, PyObject* kwargs); PyObject* cre_py_api_node_broadcast_event(PyObject* self, PyObject* args, PyObject* kwargs); @@ -473,6 +475,10 @@ static struct PyMethodDef crePyApiMethods[] = { "node_queue_deletion", (PyCFunction) cre_py_api_node_queue_deletion, METH_VARARGS | METH_KEYWORDS, "Queues a node for deletion." }, + { + "node_is_queued_for_deletion", (PyCFunction) cre_py_api_node_is_queued_for_deletion, + METH_VARARGS | METH_KEYWORDS, "Returns whether a node is queued for deletion." + }, { "node_add_child", (PyCFunction) cre_py_api_node_add_child, METH_VARARGS | METH_KEYWORDS, "Adds a node to the scene." @@ -505,6 +511,10 @@ static struct PyMethodDef crePyApiMethods[] = { "node_get_full_time_dilation", (PyCFunction) cre_py_api_node_get_full_time_dilation, METH_VARARGS | METH_KEYWORDS, "Returns a node's total time dilation, including global world and parents." }, + { + "node_get_full_time_dilation_with_physics_delta", (PyCFunction) cre_py_api_node_get_full_time_dilation_with_physics_delta, + METH_VARARGS | METH_KEYWORDS, "Returns a node's total time dilation, including global world, parents, and physics delta time." + }, { "node_create_event", (PyCFunction) cre_py_api_node_create_event, METH_VARARGS | METH_KEYWORDS, "Creates an event to be observed by others." diff --git a/engine/src/core/scripting/python/crescent_api_source.h b/engine/src/core/scripting/python/crescent_api_source.h index 47294b9fd..c549287d9 100644 --- a/engine/src/core/scripting/python/crescent_api_source.h +++ b/engine/src/core/scripting/python/crescent_api_source.h @@ -983,6 +983,12 @@ " def queue_deletion(self) -> None:\n"\ " crescent_api_internal.node_queue_deletion(entity_id=self.entity_id)\n"\ "\n"\ +" @property\n"\ +" def is_queued_for_deletion(self) -> bool:\n"\ +" return crescent_api_internal.node_is_queued_for_deletion(\n"\ +" entity_id=self.entity_id\n"\ +" )\n"\ +"\n"\ " def create_event(self, event_id: str) -> None:\n"\ " crescent_api_internal.node_create_event(\n"\ " entity_id=self.entity_id, event_id=event_id\n"\ @@ -1022,6 +1028,11 @@ " entity_id=self.entity_id\n"\ " )\n"\ "\n"\ +" def get_full_time_dilation_with_physics_delta(self) -> float:\n"\ +" return crescent_api_internal.node_get_full_time_dilation_with_physics_delta(\n"\ +" entity_id=self.entity_id\n"\ +" )\n"\ +"\n"\ "\n"\ "# 2D\n"\ "class Node2D(Node):\n"\ diff --git a/seika/src/rendering/renderer.c b/seika/src/rendering/renderer.c index d354c4ef5..8e3aeb74f 100644 --- a/seika/src/rendering/renderer.c +++ b/seika/src/rendering/renderer.c @@ -506,7 +506,9 @@ TextureCoordinates renderer_get_texture_coordinates(const SETexture* texture, co tMin = tMax; tMax = tempTMin; } - return (TextureCoordinates){ sMin, sMax, tMin, tMax }; + return (TextureCoordinates) { + sMin, sMax, tMin, tMax + }; } void renderer_set_shader_instance_params(SEShaderInstance* shaderInstance) {