From 13261370b1666ab1fd82bdd652981eb724848853 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Tue, 17 Sep 2024 22:20:24 -0400 Subject: [PATCH 01/54] Initial commit. --- .../src/core/ecs/systems/script_ec_system.c | 28 +++++++++---------- .../python/pocketpy/pkpy_script_context.c | 1 + .../python/pocketpy/pkpy_script_context.h | 1 + 3 files changed, 16 insertions(+), 14 deletions(-) create mode 100644 engine/src/core/scripting/python/pocketpy/pkpy_script_context.c create mode 100644 engine/src/core/scripting/python/pocketpy/pkpy_script_context.h diff --git a/engine/src/core/ecs/systems/script_ec_system.c b/engine/src/core/ecs/systems/script_ec_system.c index 5a7be1613..13b6af252 100644 --- a/engine/src/core/ecs/systems/script_ec_system.c +++ b/engine/src/core/ecs/systems/script_ec_system.c @@ -29,25 +29,25 @@ static size_t scriptContextsCount = 0; void cre_script_ec_system_create_and_register() { SkaECSSystemTemplate systemTemplate = ska_ecs_system_create_default_template("Script"); - systemTemplate.on_ec_system_register = on_ec_system_registered; - systemTemplate.on_ec_system_destroy = on_ec_system_destroyed; - systemTemplate.on_entity_registered_func = on_entity_registered; - systemTemplate.on_entity_unregistered_func = on_entity_unregistered; - systemTemplate.on_entity_start_func = on_entity_start; - systemTemplate.on_entity_end_func = on_entity_end; - systemTemplate.pre_update_all_func = on_pre_update_all; - systemTemplate.post_update_all_func = on_post_update_all; - systemTemplate.update_func = script_system_instance_update; - systemTemplate.fixed_update_func = script_system_instance_fixed_update; - systemTemplate.network_callback_func = network_callback; + // systemTemplate.on_ec_system_register = on_ec_system_registered; + // systemTemplate.on_ec_system_destroy = on_ec_system_destroyed; + // systemTemplate.on_entity_registered_func = on_entity_registered; + // systemTemplate.on_entity_unregistered_func = on_entity_unregistered; + // systemTemplate.on_entity_start_func = on_entity_start; + // systemTemplate.on_entity_end_func = on_entity_end; + // systemTemplate.pre_update_all_func = on_pre_update_all; + // systemTemplate.post_update_all_func = on_post_update_all; + // systemTemplate.update_func = script_system_instance_update; + // systemTemplate.fixed_update_func = script_system_instance_fixed_update; + // systemTemplate.network_callback_func = network_callback; SKA_ECS_SYSTEM_REGISTER_FROM_TEMPLATE(&systemTemplate, Transform2DComponent, ScriptComponent); } void on_ec_system_registered(SkaECSSystem* system) { // Python Context - scriptContexts[ScriptContextType_PYTHON] = cre_pkpy_script_context_create(); - scriptContextsCount++; - SKA_ASSERT(scriptContexts[ScriptContextType_PYTHON] != NULL); + // scriptContexts[ScriptContextType_PYTHON] = cre_pkpy_script_context_create(); + // scriptContextsCount++; + // SKA_ASSERT(scriptContexts[ScriptContextType_PYTHON] != NULL); // Native Context scriptContexts[ScriptContextType_NATIVE] = cre_native_create_script_context(); scriptContextsCount++; diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c new file mode 100644 index 000000000..3507f3f03 --- /dev/null +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -0,0 +1 @@ +#include "pkpy_script_context.h" diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.h b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.h new file mode 100644 index 000000000..6f70f09be --- /dev/null +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.h @@ -0,0 +1 @@ +#pragma once From dac3c3b283855c26d77a071140355a87a1874044 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Wed, 18 Sep 2024 18:53:51 -0400 Subject: [PATCH 02/54] A bunch of changes. --- Dependencies.cmake | 4 +- .../core/ecs/components/script_component.c | 4 +- .../core/ecs/components/script_component.h | 12 +--- engine/src/core/ecs/ecs_manager.c | 2 +- .../src/core/ecs/systems/script_ec_system.c | 41 +++++++----- engine/src/core/json/json_file_loader_scene.c | 2 +- .../scripting/native/native_script_context.c | 62 +++++++++++-------- .../scripting/native/native_script_context.h | 4 +- .../python/pocketpy/api/cre_pkpy_api.c | 3 +- .../python/pocketpy/api/cre_pkpy_api.h | 3 +- .../python/pocketpy/api/cre_pkpy_api_node.c | 3 +- .../python/pocketpy/api/cre_pkpy_api_node.h | 3 +- .../pocketpy/cre_pkpy_entity_instance_cache.c | 3 +- .../pocketpy/cre_pkpy_entity_instance_cache.h | 3 +- .../pocketpy/cre_pkpy_node_event_manager.c | 3 +- .../pocketpy/cre_pkpy_node_event_manager.h | 3 +- .../python/pocketpy/cre_pkpy_script_context.c | 3 +- .../python/pocketpy/cre_pkpy_script_context.h | 3 +- .../scripting/python/pocketpy/cre_pkpy_util.c | 3 +- .../scripting/python/pocketpy/cre_pkpy_util.h | 3 +- .../python/pocketpy/pkpy_script_context.c | 37 +++++++++++ .../python/pocketpy/pkpy_script_context.h | 4 ++ engine/src/core/scripting/script_context.c | 36 ++++++----- engine/src/core/scripting/script_context.h | 34 +++++++++- 24 files changed, 192 insertions(+), 86 deletions(-) diff --git a/Dependencies.cmake b/Dependencies.cmake index e3ae01cee..7f8ea0bb8 100644 --- a/Dependencies.cmake +++ b/Dependencies.cmake @@ -20,9 +20,11 @@ if (NOT TARGET pocketpy) FetchContent_Declare( pocketpy_content GIT_REPOSITORY https://github.com/blueloveTH/pocketpy.git - GIT_TAG v1.4.5 +# GIT_TAG v1.4.5 + GIT_TAG 4aab227b53ceaca69e839978f6fb7d321ad5cdd5 ) FetchContent_MakeAvailable(pocketpy_content) + target_include_directories(pocketpy PUBLIC ${pocketpy_content_SOURCE_DIR} ${pocketpy_content_SOURCE_DIR}/include) endif () # https://github.com/DaveGamble/cJSON diff --git a/engine/src/core/ecs/components/script_component.c b/engine/src/core/ecs/components/script_component.c index 66153b280..ab8ba8250 100644 --- a/engine/src/core/ecs/components/script_component.c +++ b/engine/src/core/ecs/components/script_component.c @@ -9,11 +9,11 @@ ScriptComponent* script_component_create(const char* path, const char* name) { ScriptComponent* scriptComponent = SKA_MEM_ALLOCATE(ScriptComponent); ska_strcpy(scriptComponent->classPath, path); ska_strcpy(scriptComponent->className, name); - scriptComponent->contextType = ScriptContextType_NONE; + scriptComponent->contextType = CreScriptContextType_NONE; return scriptComponent; } -ScriptComponent* script_component_create_ex(const char* path, const char* name, ScriptContextType contextType) { +ScriptComponent* script_component_create_ex(const char* path, const char* name, CreScriptContextType contextType) { ScriptComponent* scriptComponent = script_component_create(path, name); scriptComponent->contextType = contextType; return scriptComponent; diff --git a/engine/src/core/ecs/components/script_component.h b/engine/src/core/ecs/components/script_component.h index feeeea542..d31819f97 100644 --- a/engine/src/core/ecs/components/script_component.h +++ b/engine/src/core/ecs/components/script_component.h @@ -4,24 +4,18 @@ extern "C" { #endif -typedef enum ScriptContextType { - ScriptContextType_NONE = -1, // INVALID - ScriptContextType_PYTHON = 0, - ScriptContextType_NATIVE = 1, // C/C++ - - ScriptContextType_TOTAL_TYPES = 2 -} ScriptContextType; +#include "src/core/scripting/script_context.h" // TODO: Give a way for components to clean up themselves (e.g. call '[component_name]_component_delete' when deleted) // TODO: (Part2) This way we can have less memory overhead by having flexibility for objects to clean themselves up typedef struct ScriptComponent { char classPath[96]; char className[32]; - ScriptContextType contextType; + CreScriptContextType contextType; } ScriptComponent; ScriptComponent* script_component_create(const char* path, const char* name); -ScriptComponent* script_component_create_ex(const char* path, const char* name, ScriptContextType contextType); +ScriptComponent* script_component_create_ex(const char* path, const char* name, CreScriptContextType contextType); void script_component_delete(ScriptComponent* scriptComponent); ScriptComponent* script_component_copy(const ScriptComponent* scriptComponent); diff --git a/engine/src/core/ecs/ecs_manager.c b/engine/src/core/ecs/ecs_manager.c index 6cc02ee1f..b1fd8bb74 100644 --- a/engine/src/core/ecs/ecs_manager.c +++ b/engine/src/core/ecs/ecs_manager.c @@ -122,7 +122,7 @@ void cre_ecs_manager_enable_fps_display_entity(bool enabled, const char* fontUID ska_ecs_component_manager_set_component(currentFpsEntity, TEXT_LABEL_COMPONENT_INDEX, textLabelComponent); // Script Component ScriptComponent* scriptComponent = script_component_create("main", "FpsDisplay"); - scriptComponent->contextType = ScriptContextType_NATIVE; + scriptComponent->contextType = CreScriptContextType_NATIVE; ska_ecs_component_manager_set_component(currentFpsEntity, SCRIPT_COMPONENT_INDEX, scriptComponent); // Update systems ska_ecs_system_update_entity_signature_with_systems(currentFpsEntity); diff --git a/engine/src/core/ecs/systems/script_ec_system.c b/engine/src/core/ecs/systems/script_ec_system.c index 13b6af252..0ec7d5c8a 100644 --- a/engine/src/core/ecs/systems/script_ec_system.c +++ b/engine/src/core/ecs/systems/script_ec_system.c @@ -8,7 +8,9 @@ #include "../components/script_component.h" #include "../../scene/scene_manager.h" #include "../../scripting/script_context.h" -#include "../../scripting/python/pocketpy/cre_pkpy_script_context.h" +// #include "../../scripting/python/pocketpy/cre_pkpy_script_context.h" +#include "../../../../../cmake-build-release-mingw/_deps/seika_content-src/seika/logger.h" +#include "../../scripting/python/pocketpy/pkpy_script_context.h" #include "../../scripting/native/native_script_context.h" #include "../../scripting/native/internal_classes/fps_display_class.h" @@ -24,7 +26,7 @@ static void script_system_instance_update(SkaECSSystem* system, f32 deltaTime); static void script_system_instance_fixed_update(SkaECSSystem* system, f32 deltaTime); static void network_callback(SkaECSSystem* system, const char* message); -static CREScriptContext* scriptContexts[ScriptContextType_TOTAL_TYPES]; +static CREScriptContext* scriptContexts[CreScriptContextType_TOTAL_TYPES]; static size_t scriptContextsCount = 0; void cre_script_ec_system_create_and_register() { @@ -44,21 +46,28 @@ void cre_script_ec_system_create_and_register() { } void on_ec_system_registered(SkaECSSystem* system) { - // Python Context - // scriptContexts[ScriptContextType_PYTHON] = cre_pkpy_script_context_create(); - // scriptContextsCount++; - // SKA_ASSERT(scriptContexts[ScriptContextType_PYTHON] != NULL); - // Native Context - scriptContexts[ScriptContextType_NATIVE] = cre_native_create_script_context(); - scriptContextsCount++; - // Register internal classed - cre_native_class_register_new_class(fps_display_native_class_create_new()); + CREScriptContextTemplate templates[8]; + size_t templateCount = 0; + + templates[templateCount++] = cre_pkpy_get_script_context_template(); + templates[templateCount++] = cre_native_get_script_context_template(); + + for (size_t i = 0; i < templateCount; i++) { + const CREScriptContextTemplate* template = &templates[i]; + if (scriptContexts[template->contextType] != NULL) { + ska_logger_warn("Attempted to override script context type '%d', ignoring!", template->contextType); + continue; + } + scriptContexts[template->contextType] = cre_script_context_create_from_template(template); + scriptContexts[template->contextType]->on_script_context_init(scriptContexts[template->contextType]); + scriptContextsCount++; + } } void on_ec_system_destroyed(SkaECSSystem* system) { for (size_t i = 0; i < scriptContextsCount; i++) { - if (scriptContexts[i]->on_script_context_destroy != NULL) { - scriptContexts[i]->on_script_context_destroy(); + if (scriptContexts[i]->on_script_context_finalize != NULL) { + scriptContexts[i]->on_script_context_finalize(scriptContexts[i]); } } scriptContextsCount = 0; @@ -66,7 +75,7 @@ void on_ec_system_destroyed(SkaECSSystem* system) { void on_entity_registered(SkaECSSystem* system, SkaEntity entity) { const ScriptComponent* scriptComponent = (ScriptComponent*)ska_ecs_component_manager_get_component(entity, SCRIPT_COMPONENT_INDEX); - SKA_ASSERT(scriptComponent->contextType != ScriptContextType_NONE); + SKA_ASSERT(scriptComponent->contextType != CreScriptContextType_NONE); const CREScriptContext* scriptContext = scriptContexts[scriptComponent->contextType]; SKA_ASSERT(scriptContext != NULL); SKA_ASSERT(scriptContext->on_create_instance != NULL); @@ -80,7 +89,7 @@ void on_entity_unregistered(SkaECSSystem* system, SkaEntity entity) { void on_entity_start(SkaECSSystem* system, SkaEntity entity) { const ScriptComponent* scriptComponent = (ScriptComponent*)ska_ecs_component_manager_get_component(entity, SCRIPT_COMPONENT_INDEX); - SKA_ASSERT_FMT(scriptComponent->contextType != ScriptContextType_NONE, "Invalid context type '%d' for entity '%d'", scriptComponent->contextType, entity); + SKA_ASSERT_FMT(scriptComponent->contextType != CreScriptContextType_NONE, "Invalid context type '%d' for entity '%d'", scriptComponent->contextType, entity); scriptContexts[scriptComponent->contextType]->on_start(entity); } @@ -127,5 +136,5 @@ void script_system_instance_fixed_update(SkaECSSystem* system, f32 deltaTime) { void network_callback(SkaECSSystem* system, const char* message) { // Hard coding python for now TODO: Keep an array of script contexts that contain this callback - scriptContexts[ScriptContextType_PYTHON]->on_network_callback(message); + scriptContexts[CreScriptContextType_PYTHON]->on_network_callback(message); } diff --git a/engine/src/core/json/json_file_loader_scene.c b/engine/src/core/json/json_file_loader_scene.c index 56486cf5e..73b698c18 100644 --- a/engine/src/core/json/json_file_loader_scene.c +++ b/engine/src/core/json/json_file_loader_scene.c @@ -341,7 +341,7 @@ static void cre_json_script_create_or_set_default(JsonSceneNode* node, cJSON* co json_get_string(componentJson, "class_path"), json_get_string(componentJson, "class_name") ); - scriptComponent->contextType = ScriptContextType_PYTHON; + scriptComponent->contextType = CreScriptContextType_PYTHON; node->components[SCRIPT_COMPONENT_INDEX] = scriptComponent; } else { // No override for scripts for now... diff --git a/engine/src/core/scripting/native/native_script_context.c b/engine/src/core/scripting/native/native_script_context.c index d01c8cd15..95caf3acf 100644 --- a/engine/src/core/scripting/native/native_script_context.c +++ b/engine/src/core/scripting/native/native_script_context.c @@ -8,7 +8,7 @@ #include #include "native_script_class.h" -#include "../script_context.h" +#include "internal_classes/fps_display_class.h" #define MAX_NATIVE_CLASSES 4 #define MAX_NATIVE_CLASS_ENTITIES 8 @@ -20,7 +20,8 @@ void native_on_start(SkaEntity entity); void native_on_update_instance(SkaEntity entity, f32 deltaTime); void native_on_fixed_update_instance(SkaEntity entity, f32 deltaTime); void native_on_end(SkaEntity entity); -void native_on_script_context_destroy(); +static void on_script_context_init(CREScriptContext* scriptContext); +static void on_script_context_finalize(CREScriptContext* scriptContext); // Script Cache SkaStringHashMap* classCache = NULL; @@ -28,26 +29,47 @@ SkaHashMap* entityToClassName = NULL; CREScriptContext* native_script_context = NULL; -CREScriptContext* cre_native_create_script_context() { - SKA_ASSERT(native_script_context == NULL); - CREScriptContext* scriptContext = cre_script_context_create(); - scriptContext->on_create_instance = native_on_create_instance; - scriptContext->on_delete_instance = native_on_delete_instance; - scriptContext->on_start = native_on_start; - scriptContext->on_update_instance = native_on_update_instance; - scriptContext->on_fixed_update_instance = native_on_fixed_update_instance; - scriptContext->on_end = native_on_end; - scriptContext->on_script_context_destroy = native_on_script_context_destroy; +CREScriptContextTemplate cre_native_get_script_context_template() { + return (CREScriptContextTemplate) { + .contextType = CreScriptContextType_NATIVE, + .on_script_context_init = on_script_context_init, + .on_script_context_finalize = on_script_context_finalize, + .on_create_instance = native_on_create_instance, + .on_delete_instance = native_on_delete_instance, + .on_start = native_on_start, + .on_pre_update_all = NULL, + .on_post_update_all = NULL, + .on_update_instance = native_on_update_instance, + .on_fixed_update_instance = native_on_fixed_update_instance, + .on_end = native_on_end, + .on_network_callback = NULL, + }; +} +void on_script_context_init(CREScriptContext* context) { SKA_ASSERT(classCache == NULL); classCache = ska_string_hash_map_create(MAX_NATIVE_CLASSES); SKA_ASSERT(entityToClassName == NULL); entityToClassName = ska_hash_map_create(sizeof(SkaEntity), sizeof(CRENativeScriptClass **), MAX_NATIVE_CLASS_ENTITIES); - native_script_context = scriptContext; + // Register internal classes + cre_native_class_register_new_class(fps_display_native_class_create_new()); + + native_script_context = context; +} + +void on_script_context_finalize(CREScriptContext* context) { + SKA_ASSERT(classCache != NULL); + SKA_ASSERT(entityToClassName != NULL); + SKA_ASSERT(native_script_context != NULL); + + native_script_context = NULL; - return scriptContext; + ska_string_hash_map_destroy(classCache); + ska_hash_map_destroy(entityToClassName); + classCache = NULL; + entityToClassName = NULL; } void cre_native_class_register_new_class(CRENativeScriptClass* scriptClass) { @@ -119,16 +141,6 @@ void native_on_fixed_update_instance(SkaEntity entity, float deltaTime) { void native_on_end(SkaEntity entity) { SKA_ASSERT(ska_hash_map_has(entityToClassName, &entity)); - CRENativeScriptClass* scriptClassRef = (CRENativeScriptClass*) *(CRENativeScriptClass**) ska_hash_map_get( - entityToClassName, &entity); + CRENativeScriptClass* scriptClassRef = (CRENativeScriptClass*) *(CRENativeScriptClass**) ska_hash_map_get(entityToClassName, &entity); scriptClassRef->on_end_func(scriptClassRef); } - -void native_on_script_context_destroy() { - native_script_context = NULL; - - ska_string_hash_map_destroy(classCache); - ska_hash_map_destroy(entityToClassName); - classCache = NULL; - entityToClassName = NULL; -} diff --git a/engine/src/core/scripting/native/native_script_context.h b/engine/src/core/scripting/native/native_script_context.h index a03aeb607..12b4c0583 100644 --- a/engine/src/core/scripting/native/native_script_context.h +++ b/engine/src/core/scripting/native/native_script_context.h @@ -1,6 +1,8 @@ #pragma once +#include "../script_context.h" + struct CRENativeScriptClass; -struct CREScriptContext* cre_native_create_script_context(); +CREScriptContextTemplate cre_native_get_script_context_template(); void cre_native_class_register_new_class(struct CRENativeScriptClass* scriptClass); diff --git a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.c b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.c index e845cdf91..e319f2ea2 100644 --- a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.c +++ b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.c @@ -1,5 +1,5 @@ #include "cre_pkpy_api.h" - +/* #include #include @@ -1332,3 +1332,4 @@ int cre_pkpy_api_client_send(pkpy_vm* vm) { ska_udp_client_send_message(message); return 0; } +*/ diff --git a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.h b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.h index fce90f0a9..24743046d 100644 --- a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.h +++ b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.h @@ -1,5 +1,6 @@ #pragma once - +/* struct pkpy_vm_handle; void cre_pkpy_api_load_internal_modules(struct pkpy_vm_handle* vm); +*/ \ No newline at end of file diff --git a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.c b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.c index 960c56ee4..d90d5f4ef 100644 --- a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.c +++ b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.c @@ -1,5 +1,5 @@ #include "cre_pkpy_api_node.h" - +/* #include #include @@ -1267,3 +1267,4 @@ int32 cre_pkpy_api_particles2d_set_spread(pkpy_vm* vm) { particles2dComponent->spread = (f32)pySpread; return 0; } +*/ diff --git a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.h b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.h index e38d235f1..2e7b59596 100644 --- a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.h +++ b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.h @@ -1,5 +1,5 @@ #pragma once - +/* #include struct pkpy_vm_handle; @@ -107,3 +107,4 @@ int32 cre_pkpy_api_particles2d_get_initial_velocity(struct pkpy_vm_handle* vm); int32 cre_pkpy_api_particles2d_set_initial_velocity(struct pkpy_vm_handle* vm); int32 cre_pkpy_api_particles2d_get_spread(struct pkpy_vm_handle* vm); int32 cre_pkpy_api_particles2d_set_spread(struct pkpy_vm_handle* vm); +*/ \ No newline at end of file diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.c b/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.c index 5f355752a..044302009 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.c +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.c @@ -9,7 +9,7 @@ #include "../../../ecs/ecs_globals.h" #include "../../../ecs/components/node_component.h" #include "../../../ecs/components/script_component.h" - +/* static bool entity_instance_cache_is_initialized = false; void cre_pkpy_entity_instance_cache_initialize(pkpy_vm* vm) { @@ -103,3 +103,4 @@ void cre_pkpy_entity_instance_cache_push_entity_instance(pkpy_vm* vm, SkaEntity pkpy_eval(vm, entityStringBuffer); SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); } +*/ \ No newline at end of file diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.h index a4668dd34..ceb784d36 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.h @@ -1,5 +1,5 @@ #pragma once - +/* #include #include @@ -13,3 +13,4 @@ bool cre_pkpy_entity_instance_cache_create_new_entity_if_nonexistent(pkpy_vm* vm void cre_pkpy_entity_instance_cache_remove_entity(pkpy_vm* vm, SkaEntity entity); bool cre_pkpy_entity_instance_cache_has_entity(pkpy_vm* vm, SkaEntity entity); void cre_pkpy_entity_instance_cache_push_entity_instance(pkpy_vm* vm, SkaEntity entity); +*/ \ No newline at end of file diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.c b/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.c index 501e62329..3b228822a 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.c +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.c @@ -1,5 +1,5 @@ #include "cre_pkpy_node_event_manager.h" - +/* #include #include "cre_pkpy_util.h" @@ -89,3 +89,4 @@ void cre_pkpy_node_event_manager_broadcast_event_string(pkpy_vm* vm, int32 owner SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); pkpy_pop_top(vm); } +*/ diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.h index e3d4b11de..55e9d30da 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.h @@ -1,5 +1,5 @@ #pragma once - +/* #include #include @@ -20,3 +20,4 @@ void cre_pkpy_node_event_manager_broadcast_event_bool(struct pkpy_vm_handle* vm, void cre_pkpy_node_event_manager_broadcast_event_int(struct pkpy_vm_handle* vm, int32 ownerId, const char* eventName, int32 value); void cre_pkpy_node_event_manager_broadcast_event_float(struct pkpy_vm_handle* vm, int32 ownerId, const char* eventName, f32 value); void cre_pkpy_node_event_manager_broadcast_event_string(struct pkpy_vm_handle* vm, int32 ownerId, const char* eventName, const char* value); +*/ diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.c index 0223d29ee..55de2ecb3 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.c @@ -1,5 +1,5 @@ #include "cre_pkpy_script_context.h" - +/* #include #include @@ -297,3 +297,4 @@ void pkpy_node_event_animated_sprite_animation_finished(SkaSubjectNotifyPayload* const AnimatedSpriteAnimationFinishedPayload* eventPayload = (AnimatedSpriteAnimationFinishedPayload*)payload->data; cre_pkpy_node_event_manager_broadcast_event_string(cre_pkpy_vm, (int)eventPayload->entity, CRE_PKPY_NODE_EVENT_NAME_ANIMATION_FINISHED, eventPayload->animation->name); } +*/ diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.h index acb00af5f..3107ba675 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.h @@ -1,5 +1,5 @@ #pragma once - +/* #include struct CREScriptContext* cre_pkpy_script_context_create(); @@ -11,3 +11,4 @@ void cre_pkpy_script_context_on_network_udp_server_client_connected(); void cre_pkpy_script_context_create_instance(SkaEntity entity, const char* classPath, const char* className); void cre_pkpy_script_context_create_instance_if_nonexistent(SkaEntity entity, const char* classPath, const char* className); void cre_pkpy_script_context_create_instance_if_nonexistent_and_push_entity_instance(SkaEntity entity); +*/ diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.c b/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.c index d618de1ab..ce58a57d0 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.c +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.c @@ -1,5 +1,5 @@ #include "cre_pkpy_util.h" - +/* #include #include @@ -67,3 +67,4 @@ void cre_pkpy_util_create_from_string(pkpy_vm* vm, const char* moduleName, const pkpy_pop_top(vm); SKA_ASSERT(stackBeforePush == pkpy_stack_size(vm)); } +*/ diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.h index 4b49102d1..9f70e9e9a 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.h @@ -1,5 +1,5 @@ #pragma once - +/* #include #include @@ -21,3 +21,4 @@ char* cre_pkpy_util_get_error_message(pkpy_vm* vm); bool cre_pkpy_util_print_error_message(pkpy_vm* vm); void cre_pkpy_util_create_module(pkpy_vm* vm, CrePPModule* module); void cre_pkpy_util_create_from_string(pkpy_vm* vm, const char* moduleName, const char* moduleSource); +*/ diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index 3507f3f03..d72c40d3f 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -1 +1,38 @@ #include "pkpy_script_context.h" + +#include + +#include + +static CREScriptContext* scriptContext = NULL; + +//--- Script Context Interface ---// +static void pkpy_sc_on_script_context_init(CREScriptContext* context); +static void pkpy_sc_on_script_context_finalize(CREScriptContext* context); + +CREScriptContextTemplate cre_pkpy_get_script_context_template() { + return (CREScriptContextTemplate){ + .contextType = CreScriptContextType_PYTHON, + .on_script_context_init = pkpy_sc_on_script_context_init, + .on_script_context_finalize = pkpy_sc_on_script_context_finalize, + .on_create_instance = NULL, + .on_delete_instance = NULL, + .on_start = NULL, + .on_pre_update_all = NULL, + .on_post_update_all = NULL, + .on_update_instance = NULL, + .on_fixed_update_instance = NULL, + .on_end = NULL, + .on_network_callback = NULL, + }; +} + +void pkpy_sc_on_script_context_init(CREScriptContext* context) { + py_initialize(); + scriptContext = context; +} + +void pkpy_sc_on_script_context_finalize(CREScriptContext* context) { + scriptContext = NULL; + py_finalize(); +} diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.h b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.h index 6f70f09be..e269517b5 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.h +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.h @@ -1 +1,5 @@ #pragma once + +#include "../../script_context.h" + +CREScriptContextTemplate cre_pkpy_get_script_context_template(); diff --git a/engine/src/core/scripting/script_context.c b/engine/src/core/scripting/script_context.c index 7e27ff301..a67c33158 100644 --- a/engine/src/core/scripting/script_context.c +++ b/engine/src/core/scripting/script_context.c @@ -3,19 +3,25 @@ #include CREScriptContext* cre_script_context_create() { - CREScriptContext* newScriptContext = SKA_MEM_ALLOCATE(CREScriptContext); - newScriptContext->on_create_instance = NULL; - newScriptContext->on_delete_instance = NULL; - newScriptContext->on_start = NULL; - newScriptContext->on_pre_update_all = NULL; - newScriptContext->on_post_update_all = NULL; - newScriptContext->on_update_instance = NULL; - newScriptContext->on_fixed_update_instance = NULL; - newScriptContext->on_end = NULL; - newScriptContext->on_network_callback = NULL; -// newScriptContext->on_entity_subscribe_to_network_callback = NULL; - newScriptContext->on_script_context_destroy = NULL; - newScriptContext->updateEntityCount = 0; - newScriptContext->fixedUpdateEntityCount = 0; - return newScriptContext; + return SKA_MEM_ALLOCATE(CREScriptContext); +} + +CREScriptContext* cre_script_context_create_from_template(const CREScriptContextTemplate* template) { + CREScriptContext* scriptContext = cre_script_context_create(); + scriptContext->on_script_context_init = template->on_script_context_init; + scriptContext->on_script_context_finalize = template->on_script_context_finalize; + scriptContext->on_create_instance = template->on_create_instance; + scriptContext->on_delete_instance = template->on_delete_instance; + scriptContext->on_start = template->on_start; + scriptContext->on_pre_update_all = template->on_pre_update_all; + scriptContext->on_post_update_all = template->on_post_update_all; + scriptContext->on_update_instance = template->on_update_instance; + scriptContext->on_fixed_update_instance = template->on_fixed_update_instance; + scriptContext->on_end = template->on_end; + scriptContext->on_network_callback = template->on_network_callback; + return scriptContext; +} + +void cre_script_context_destroy(CREScriptContext* scriptContext) { + SKA_MEM_FREE(scriptContext); } diff --git a/engine/src/core/scripting/script_context.h b/engine/src/core/scripting/script_context.h index 5ecc19b0b..ffa274761 100644 --- a/engine/src/core/scripting/script_context.h +++ b/engine/src/core/scripting/script_context.h @@ -16,16 +16,29 @@ typedef void (*OnFixedUpdateInstance) (SkaEntity, f32); typedef void (*OnNetworkCallback) (const char*); -typedef void (*OnScriptContextDestroy) (); +typedef void (*OnScriptContextInit) (struct CREScriptContext*); +typedef void (*OnScriptContextFinalize) (struct CREScriptContext*); // TODO: Make network callbacks not specific to python struct _object; // PyObject typedef void (*OnEntitySubscribeToNetworkCallback) (SkaEntity, struct _object*, const char*); +typedef enum CreScriptContextType { + CreScriptContextType_NONE = -1, // INVALID + CreScriptContextType_PYTHON = 0, + CreScriptContextType_NATIVE = 1, // C/C++ + + CreScriptContextType_TOTAL_TYPES = 2 +} CreScriptContextType; + // Generic script context to be used as an interface for game scripting // Note: Node event logics must be handled manually within the script context, refer to 'node_event.h' for the api. // Also check out other script contexts for examples. typedef struct CREScriptContext { + // Called when the script context is created + OnScriptContextInit on_script_context_init; + // Called when the script context is destroyed + OnScriptContextFinalize on_script_context_finalize; // Called when a new entity instance is requested to be created once a node has entered the scene. // If using the same function to dynamically create instance from the script side, be sure to check if the instance // Isn't already created @@ -45,8 +58,6 @@ typedef struct CREScriptContext { // The main network callback for forwarding network data to the script context OnNetworkCallback on_network_callback; // OnEntitySubscribeToNetworkCallback on_entity_subscribe_to_network_callback; - // Called when the script context is destroyed - OnScriptContextDestroy on_script_context_destroy; // We could have a validation step on the script contexts to check if the update, fixed_update, etc... funcs exists // in the class within the scripting language. For now, the script context is responsible for entity and entity count // even though it's not used in the script ec system @@ -56,4 +67,21 @@ typedef struct CREScriptContext { SkaEntity fixedUpdateEntities[SKA_MAX_ENTITIES]; } CREScriptContext; +typedef struct CREScriptContextTemplate { + CreScriptContextType contextType; + OnScriptContextInit on_script_context_init; + OnScriptContextFinalize on_script_context_finalize; + OnCreateInstance on_create_instance; + OnDeleteInstance on_delete_instance; + OnStart on_start; + OnPreUpdateAll on_pre_update_all; + OnPostUpdateAll on_post_update_all; + OnUpdateInstance on_update_instance; + OnFixedUpdateInstance on_fixed_update_instance; + OnEnd on_end; + OnNetworkCallback on_network_callback; +} CREScriptContextTemplate; + CREScriptContext* cre_script_context_create(); +CREScriptContext* cre_script_context_create_from_template(const CREScriptContextTemplate* template); +void cre_script_context_destroy(CREScriptContext* scriptContext); From 5383d9e3035aa4bb8aa1a4e074f2e1882bddbc4d Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Wed, 18 Sep 2024 19:21:14 -0400 Subject: [PATCH 03/54] Updates. --- .../python/pocketpy/pkpy_script_context.c | 77 +++++++++++++++---- 1 file changed, 64 insertions(+), 13 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index d72c40d3f..065473acd 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -3,36 +3,87 @@ #include #include +#include +#include + +#include "seika/memory.h" static CREScriptContext* scriptContext = NULL; +static py_Name startFunctionName; +static py_Name processFunctionName; +static py_Name fixedProcessFunctionName; +static py_Name endFunctionName; + +static char* pkpy_import_file(const char* path); //--- Script Context Interface ---// -static void pkpy_sc_on_script_context_init(CREScriptContext* context); -static void pkpy_sc_on_script_context_finalize(CREScriptContext* context); +static void pkpy_init(CREScriptContext* context); +static void pkpy_finalize(CREScriptContext* context); +static void pkpy_create_instance(SkaEntity entity, const char* classPath, const char* className); +static void pkpy_delete_instance(SkaEntity entity); +static void pkpy_on_start(SkaEntity entity); +static void pkpy_on_end(SkaEntity entity); +static void pkpy_on_update(SkaEntity entity, f32 deltaTime); +static void pkpy_on_fixed_update(SkaEntity entity, f32 deltaTime); +static void pkpy_network_callback(const char* message); + CREScriptContextTemplate cre_pkpy_get_script_context_template() { return (CREScriptContextTemplate){ .contextType = CreScriptContextType_PYTHON, - .on_script_context_init = pkpy_sc_on_script_context_init, - .on_script_context_finalize = pkpy_sc_on_script_context_finalize, - .on_create_instance = NULL, - .on_delete_instance = NULL, - .on_start = NULL, + .on_script_context_init = pkpy_init, + .on_script_context_finalize = pkpy_finalize, + .on_create_instance = pkpy_create_instance, + .on_delete_instance = pkpy_delete_instance, + .on_start = pkpy_on_start, .on_pre_update_all = NULL, .on_post_update_all = NULL, - .on_update_instance = NULL, - .on_fixed_update_instance = NULL, - .on_end = NULL, - .on_network_callback = NULL, + .on_update_instance = pkpy_on_update, + .on_fixed_update_instance = pkpy_on_fixed_update, + .on_end = pkpy_on_end, + .on_network_callback = pkpy_network_callback, }; } -void pkpy_sc_on_script_context_init(CREScriptContext* context) { +void pkpy_init(CREScriptContext* context) { py_initialize(); + // Cache function names + startFunctionName = py_name("_start"); + processFunctionName = py_name("_process"); + fixedProcessFunctionName = py_name("_fixed_process"); + endFunctionName = py_name("_end"); + // Setup callbacks + py_Callbacks* callbacks = py_callbacks(); + callbacks->importfile = pkpy_import_file; + scriptContext = context; } -void pkpy_sc_on_script_context_finalize(CREScriptContext* context) { +void pkpy_finalize(CREScriptContext* context) { scriptContext = NULL; py_finalize(); } + +void pkpy_create_instance(SkaEntity entity, const char* classPath, const char* className) {} + +void pkpy_delete_instance(SkaEntity entity) {} + +void pkpy_on_start(SkaEntity entity) {} + +void pkpy_on_end(SkaEntity entity) {} + +void pkpy_on_update(SkaEntity entity, f32 deltaTime) {} + +void pkpy_on_fixed_update(SkaEntity entity, f32 deltaTime) {} + +void pkpy_network_callback(const char* message) {} + +char* pkpy_import_file(const char* path) { + // Use built in asset loader to load script instead of pkpy's default + char* moduleString = ska_asset_file_loader_read_file_contents_as_string(path, NULL); + if (!moduleString) { + ska_logger_error("Failed to load pkpy module at path'%s'", path); + return NULL; + } + return moduleString; +} From da9e08f53aa03a7cc090d7420c40038c81d4fe42 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Wed, 18 Sep 2024 19:22:00 -0400 Subject: [PATCH 04/54] Clean. --- engine/src/core/scripting/python/pocketpy/pkpy_script_context.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index 065473acd..536c2d652 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -6,8 +6,6 @@ #include #include -#include "seika/memory.h" - static CREScriptContext* scriptContext = NULL; static py_Name startFunctionName; static py_Name processFunctionName; From bbc161db1fd472b598c3d80e0bcd9395ca822ee6 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Wed, 18 Sep 2024 19:23:59 -0400 Subject: [PATCH 05/54] Clean. --- .../scripting/native/native_script_context.c | 24 +++++++++---------- .../python/pocketpy/pkpy_script_context.c | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/engine/src/core/scripting/native/native_script_context.c b/engine/src/core/scripting/native/native_script_context.c index 95caf3acf..1a33dd175 100644 --- a/engine/src/core/scripting/native/native_script_context.c +++ b/engine/src/core/scripting/native/native_script_context.c @@ -14,14 +14,14 @@ #define MAX_NATIVE_CLASS_ENTITIES 8 // --- Script Context Interface --- // -void native_on_create_instance(SkaEntity entity, const char* classPath, const char* className); -void native_on_delete_instance(SkaEntity entity); -void native_on_start(SkaEntity entity); -void native_on_update_instance(SkaEntity entity, f32 deltaTime); -void native_on_fixed_update_instance(SkaEntity entity, f32 deltaTime); -void native_on_end(SkaEntity entity); -static void on_script_context_init(CREScriptContext* scriptContext); -static void on_script_context_finalize(CREScriptContext* scriptContext); +static void native_init(CREScriptContext* scriptContext); +static void native_finalize(CREScriptContext* scriptContext); +static void native_on_create_instance(SkaEntity entity, const char* classPath, const char* className); +static void native_on_delete_instance(SkaEntity entity); +static void native_on_start(SkaEntity entity); +static void native_on_update_instance(SkaEntity entity, f32 deltaTime); +static void native_on_fixed_update_instance(SkaEntity entity, f32 deltaTime); +static void native_on_end(SkaEntity entity); // Script Cache SkaStringHashMap* classCache = NULL; @@ -32,8 +32,8 @@ CREScriptContext* native_script_context = NULL; CREScriptContextTemplate cre_native_get_script_context_template() { return (CREScriptContextTemplate) { .contextType = CreScriptContextType_NATIVE, - .on_script_context_init = on_script_context_init, - .on_script_context_finalize = on_script_context_finalize, + .on_script_context_init = native_init, + .on_script_context_finalize = native_finalize, .on_create_instance = native_on_create_instance, .on_delete_instance = native_on_delete_instance, .on_start = native_on_start, @@ -46,7 +46,7 @@ CREScriptContextTemplate cre_native_get_script_context_template() { }; } -void on_script_context_init(CREScriptContext* context) { +void native_init(CREScriptContext* context) { SKA_ASSERT(classCache == NULL); classCache = ska_string_hash_map_create(MAX_NATIVE_CLASSES); @@ -59,7 +59,7 @@ void on_script_context_init(CREScriptContext* context) { native_script_context = context; } -void on_script_context_finalize(CREScriptContext* context) { +void native_finalize(CREScriptContext* context) { SKA_ASSERT(classCache != NULL); SKA_ASSERT(entityToClassName != NULL); SKA_ASSERT(native_script_context != NULL); diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index 536c2d652..a1bdb66ab 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -27,7 +27,7 @@ static void pkpy_network_callback(const char* message); CREScriptContextTemplate cre_pkpy_get_script_context_template() { - return (CREScriptContextTemplate){ + return (CREScriptContextTemplate) { .contextType = CreScriptContextType_PYTHON, .on_script_context_init = pkpy_init, .on_script_context_finalize = pkpy_finalize, From c12b9d7a8d2fe67ebf52cc3228cbc60e5d841141 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sat, 21 Sep 2024 22:25:38 -0400 Subject: [PATCH 06/54] Testing out stuff. --- .../python/pocketpy/pkpy_script_context.c | 88 +++++++++++++++++-- 1 file changed, 83 insertions(+), 5 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index a1bdb66ab..adf60a05b 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -6,6 +6,57 @@ #include #include +//--- Entity Instance Cache ---// +typedef struct CreEntityInstanceCache { + py_Ref cacheDict; + py_Ref instances[SKA_MAX_ENTITIES]; +} CreEntityInstanceCache; + +static CreEntityInstanceCache entityInstanceCache = {0}; +static char entityCacheStringBuffer[16]; + +static void instance_cache_init() { + py_newdict(entityInstanceCache.cacheDict); +} + +static void instance_cache_finalize() { + memset(entityInstanceCache.instances, 0, sizeof(entityInstanceCache.instances)); +} + +static py_Ref instance_cache_add(SkaEntity entity,const char* classPath, const char* className) { + // Early out if entity already exists + if (entityInstanceCache.instances[entity] != NULL) { + ska_logger_warn("Attempting to add entity '%u' from '%s.%s'!", entity, classPath, className); + return entityInstanceCache.instances[entity]; + } + const bool wasImportSuccessful = py_import(classPath) == 1; + SKA_ASSERT(wasImportSuccessful); + py_Ref module = py_getmodule(classPath); + SKA_ASSERT_FMT(module, "module '%s' not found!", classPath); + static char callInstanceBuffer[96]; + sprintf_s(callInstanceBuffer, sizeof(callInstanceBuffer), "%s(%d)", className, entity); + py_eval(callInstanceBuffer, module); + py_Ref instanceRef = py_retval(); + SKA_ASSERT_FMT(instanceRef, "Unable to create instance from '%s.%s'!", classPath, className); + entityInstanceCache.instances[entity] = instanceRef; + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "%u", entity); + py_setdict(entityInstanceCache.cacheDict, py_name(entityCacheStringBuffer), instanceRef); + return instanceRef; +} + +static void instance_cache_remove(SkaEntity entity) { + if (entityInstanceCache.instances[entity]) { + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "%u", entity); + py_deldict(entityInstanceCache.cacheDict, py_name(entityCacheStringBuffer)); + entityInstanceCache.instances[entity] = NULL; + } +} + +static py_Ref instance_cache_get(SkaEntity entity) { + return entityInstanceCache.instances[entity]; +} + + static CREScriptContext* scriptContext = NULL; static py_Name startFunctionName; static py_Name processFunctionName; @@ -45,6 +96,7 @@ CREScriptContextTemplate cre_pkpy_get_script_context_template() { void pkpy_init(CREScriptContext* context) { py_initialize(); + instance_cache_init(); // Cache function names startFunctionName = py_name("_start"); processFunctionName = py_name("_process"); @@ -59,18 +111,44 @@ void pkpy_init(CREScriptContext* context) { void pkpy_finalize(CREScriptContext* context) { scriptContext = NULL; + instance_cache_finalize(); py_finalize(); } -void pkpy_create_instance(SkaEntity entity, const char* classPath, const char* className) {} +void pkpy_create_instance(SkaEntity entity, const char* classPath, const char* className) { + instance_cache_add(entity, classPath, className); +} -void pkpy_delete_instance(SkaEntity entity) {} +void pkpy_delete_instance(SkaEntity entity) { + instance_cache_remove(entity); +} -void pkpy_on_start(SkaEntity entity) {} +void pkpy_on_start(SkaEntity entity) { + py_Ref self = instance_cache_get(entity); + SKA_ASSERT(self); + if (py_getattr(self, startFunctionName)) { + py_Ref func = py_retval(); + py_call(func, 1, self); + } +} -void pkpy_on_end(SkaEntity entity) {} +void pkpy_on_end(SkaEntity entity) { + py_Ref self = instance_cache_get(entity); + SKA_ASSERT(self); + if (py_getattr(self, endFunctionName)) { + py_Ref func = py_retval(); + py_call(func, 1, self); + } +} -void pkpy_on_update(SkaEntity entity, f32 deltaTime) {} +void pkpy_on_update(SkaEntity entity, f32 deltaTime) { + py_Ref self = instance_cache_get(entity); + SKA_ASSERT(self); + if (py_getattr(self, endFunctionName)) { + py_Ref func = py_retval(); + py_call(func, 1, self); + } +} void pkpy_on_fixed_update(SkaEntity entity, f32 deltaTime) {} From d1f04753118b224b94c92358df2efb51edc87ce9 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Thu, 26 Sep 2024 21:30:52 -0400 Subject: [PATCH 07/54] Test. --- Dependencies.cmake | 2 +- .../src/core/ecs/systems/script_ec_system.c | 22 +- .../scripting/python/pocketpy/api/pkpy_api.c | 214 +++++++++++++++++ .../scripting/python/pocketpy/api/pkpy_api.h | 3 + .../python/pocketpy/api/pkpy_api_impl.c | 217 +++++++++++++++++ .../python/pocketpy/api/pkpy_api_impl.h | 221 ++++++++++++++++++ .../python/pocketpy/pkpy_script_context.c | 137 ++++++++--- .../scripting/python/pocketpy/pkpy_util.c | 19 ++ .../scripting/python/pocketpy/pkpy_util.h | 21 ++ engine/src/main.c | 30 ++- 10 files changed, 839 insertions(+), 47 deletions(-) create mode 100644 engine/src/core/scripting/python/pocketpy/api/pkpy_api.c create mode 100644 engine/src/core/scripting/python/pocketpy/api/pkpy_api.h create mode 100644 engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c create mode 100644 engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.h create mode 100644 engine/src/core/scripting/python/pocketpy/pkpy_util.c create mode 100644 engine/src/core/scripting/python/pocketpy/pkpy_util.h diff --git a/Dependencies.cmake b/Dependencies.cmake index 7f8ea0bb8..0f1aab707 100644 --- a/Dependencies.cmake +++ b/Dependencies.cmake @@ -21,7 +21,7 @@ if (NOT TARGET pocketpy) pocketpy_content GIT_REPOSITORY https://github.com/blueloveTH/pocketpy.git # GIT_TAG v1.4.5 - GIT_TAG 4aab227b53ceaca69e839978f6fb7d321ad5cdd5 + GIT_TAG v2.0.0 ) FetchContent_MakeAvailable(pocketpy_content) target_include_directories(pocketpy PUBLIC ${pocketpy_content_SOURCE_DIR} ${pocketpy_content_SOURCE_DIR}/include) diff --git a/engine/src/core/ecs/systems/script_ec_system.c b/engine/src/core/ecs/systems/script_ec_system.c index 0ec7d5c8a..c4056ec5b 100644 --- a/engine/src/core/ecs/systems/script_ec_system.c +++ b/engine/src/core/ecs/systems/script_ec_system.c @@ -31,17 +31,17 @@ static size_t scriptContextsCount = 0; void cre_script_ec_system_create_and_register() { SkaECSSystemTemplate systemTemplate = ska_ecs_system_create_default_template("Script"); - // systemTemplate.on_ec_system_register = on_ec_system_registered; - // systemTemplate.on_ec_system_destroy = on_ec_system_destroyed; - // systemTemplate.on_entity_registered_func = on_entity_registered; - // systemTemplate.on_entity_unregistered_func = on_entity_unregistered; - // systemTemplate.on_entity_start_func = on_entity_start; - // systemTemplate.on_entity_end_func = on_entity_end; - // systemTemplate.pre_update_all_func = on_pre_update_all; - // systemTemplate.post_update_all_func = on_post_update_all; - // systemTemplate.update_func = script_system_instance_update; - // systemTemplate.fixed_update_func = script_system_instance_fixed_update; - // systemTemplate.network_callback_func = network_callback; + systemTemplate.on_ec_system_register = on_ec_system_registered; + systemTemplate.on_ec_system_destroy = on_ec_system_destroyed; + systemTemplate.on_entity_registered_func = on_entity_registered; + systemTemplate.on_entity_unregistered_func = on_entity_unregistered; + systemTemplate.on_entity_start_func = on_entity_start; + systemTemplate.on_entity_end_func = on_entity_end; + systemTemplate.pre_update_all_func = on_pre_update_all; + systemTemplate.post_update_all_func = on_post_update_all; + systemTemplate.update_func = script_system_instance_update; + systemTemplate.fixed_update_func = script_system_instance_fixed_update; + systemTemplate.network_callback_func = network_callback; SKA_ECS_SYSTEM_REGISTER_FROM_TEMPLATE(&systemTemplate, Transform2DComponent, ScriptComponent); } diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api.c new file mode 100644 index 000000000..757231cac --- /dev/null +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api.c @@ -0,0 +1,214 @@ +#include "pkpy_api.h" + +#include + +#include "pkpy_api_impl.h" +#include "../pkpy_util.h" +#include "../cre_pkpy.h" +#include "../cre_pkpy_api_source.h" + +void cre_pkpy_api_load_internal_modules() { + // Load crescent internal first + cre_pkpy_util_create_module(&(CrePPModule) { + .name = "crescent_internal", + .functions = { + // Shader Instance + {.signature = "shader_instance_delete(shader_id: int) -> bool", .function = cre_pkpy_api_shader_instance_delete}, + {.signature = "shader_instance_create_bool_param(shader_id: int, initial_value: bool) -> None", .function = cre_pkpy_api_shader_instance_create_bool_param}, + {.signature = "shader_instance_set_bool_param(shader_id: int, name: str, value: bool) -> None", .function = cre_pkpy_api_shader_instance_set_bool_param}, + {.signature = "shader_instance_get_bool_param(shader_id: int, name: str) -> bool", .function = cre_pkpy_api_shader_instance_get_bool_param}, + {.signature = "shader_instance_create_int_param(shader_id: int, initial_value: int) -> None", .function = cre_pkpy_api_shader_instance_create_int_param}, + {.signature = "shader_instance_set_int_param(shader_id: int, name: str, value: int) -> None", .function = cre_pkpy_api_shader_instance_set_int_param}, + {.signature = "shader_instance_get_int_param(shader_id: int, name: str) -> int", .function = cre_pkpy_api_shader_instance_get_int_param}, + {.signature = "shader_instance_create_float_param(shader_id: int, initial_value: float) -> None", .function = cre_pkpy_api_shader_instance_create_float_param}, + {.signature = "shader_instance_set_float_param(shader_id: int, name: str, value: float) -> None", .function = cre_pkpy_api_shader_instance_set_float_param}, + {.signature = "shader_instance_get_float_param(shader_id: int, name: str) -> float", .function = cre_pkpy_api_shader_instance_get_float_param}, + {.signature = "shader_instance_create_float2_param(shader_id: int, initial_value_x: float, initial_value_y: float) -> None", .function = cre_pkpy_api_shader_instance_create_float2_param}, + {.signature = "shader_instance_set_float2_param(shader_id: int, name: str, value_x: float, value_y: float) -> None", .function = cre_pkpy_api_shader_instance_set_float2_param}, + {.signature = "shader_instance_get_float2_param(shader_id: int, name: str) -> Tuple[float, float]", .function = cre_pkpy_api_shader_instance_get_float2_param}, + {.signature = "shader_instance_create_float3_param(shader_id: int, initial_value_x: float, initial_value_y: float, initial_value_z: float) -> None", .function = cre_pkpy_api_shader_instance_create_float3_param}, + {.signature = "shader_instance_set_float3_param(shader_id: int, name: str, value_x: float, value_y: float, value_z: float) -> None", .function = cre_pkpy_api_shader_instance_set_float3_param}, + {.signature = "shader_instance_get_float3_param(shader_id: int, name: str) -> Tuple[float, float, float]", .function = cre_pkpy_api_shader_instance_get_float3_param}, + {.signature = "shader_instance_create_float4_param(shader_id: int, initial_value_x: float, initial_value_y: float, initial_value_z: float, initial_value_w: float) -> None", .function = cre_pkpy_api_shader_instance_create_float4_param}, + {.signature = "shader_instance_set_float4_param(shader_id: int, name: str, value_x: float, value_y: float, value_z: float, value_w: float) -> None", .function = cre_pkpy_api_shader_instance_set_float4_param}, + {.signature = "shader_instance_get_float4_param(shader_id: int, name: str) -> Tuple[float, float, float, float]", .function = cre_pkpy_api_shader_instance_get_float4_param}, + // Shader Util + {.signature = "shader_util_compile_shader(shader_path: str) -> int", .function = cre_pkpy_api_shader_util_compile_shader}, + {.signature = "shader_util_compile_shader_raw(vertex_path, fragment_path) -> int", .function = cre_pkpy_api_shader_util_compile_shader_raw}, + {.signature = "shader_util_set_screen_shader(shader_path: str) -> bool", .function = cre_pkpy_api_shader_util_set_screen_shader}, + {.signature = "shader_util_get_current_screen_shader() -> int", .function = cre_pkpy_api_shader_util_get_current_screen_shader}, + {.signature = "shader_util_reset_screen_shader_to_default() -> None", .function = cre_pkpy_api_shader_util_reset_screen_shader_to_default}, + // Engine + {.signature = "engine_exit(code: int) -> None", .function = cre_pkpy_api_engine_exit}, + {.signature = "engine_set_target_fps(fps: int) -> None", .function = cre_pkpy_api_engine_set_target_fps}, + {.signature = "engine_get_target_fps() -> int", .function = cre_pkpy_api_engine_get_target_fps}, + {.signature = "engine_get_average_fps() -> int", .function = cre_pkpy_api_engine_get_average_fps}, + {.signature = "engine_set_fps_display_enabled(enabled: bool, font_uid: str, position_x: float, position_y: float) -> None", .function = cre_pkpy_api_engine_set_fps_display_enabled}, + {.signature = "engine_get_global_physics_delta_time() -> float", .function = cre_pkpy_api_engine_get_global_physics_delta_time}, + // Input + {.signature = "input_is_key_pressed(key: int) -> bool", .function = cre_pkpy_api_input_is_key_pressed}, + {.signature = "input_is_key_just_pressed(key: int) -> bool", .function = cre_pkpy_api_input_is_key_just_pressed}, + {.signature = "input_is_key_just_released(key: int) -> bool", .function = cre_pkpy_api_input_is_key_just_released}, + {.signature = "input_add_action(name: str, value_key: int, device_id: int) -> None", .function = cre_pkpy_api_input_add_action}, + {.signature = "input_is_action_pressed(name: str) -> bool", .function = cre_pkpy_api_input_is_action_pressed}, + {.signature = "input_is_action_just_pressed(name: str) -> bool", .function = cre_pkpy_api_input_is_action_just_pressed}, + {.signature = "input_is_action_just_released(name: str) -> bool", .function = cre_pkpy_api_input_is_action_just_released}, + {.signature = "input_start_gamepad_vibration(device_id: int, weak_magnitude: float, strong_magnitude: float, duration: float) -> None", .function = cre_pkpy_api_input_start_gamepad_vibration}, + {.signature = "input_stop_gamepad_vibration(device_id: int) -> None", .function = cre_pkpy_api_input_stop_gamepad_vibration}, + {.signature = "input_mouse_get_position() -> Tuple[float, float]", .function = cre_pkpy_api_input_mouse_get_position}, + {.signature = "input_mouse_get_world_position() -> Tuple[float, float]", .function = cre_pkpy_api_input_mouse_get_world_position}, + // Node + {.signature = "node_new(class_path: str, class_name: str, node_type_flag: int) -> \"Node\"", .function = cre_pkpy_api_node_new}, + {.signature = "node_get_name(entity_id: int) -> str", .function = cre_pkpy_api_node_get_name}, + {.signature = "node_add_child(parent_entity_id: int, child_entity_id: int) -> None", .function = cre_pkpy_api_node_add_child}, + {.signature = "node_get_child(parent_entity_id: int, child_entity_name: str) -> Optional[\"Node\"]", .function = cre_pkpy_api_node_get_child}, + {.signature = "node_get_children(entity_id: int) -> Tuple[\"Node\", ...]", .function = cre_pkpy_api_node_get_children}, + {.signature = "node_get_parent(child_entity_id: int) -> Optional[\"Node\"]", .function = cre_pkpy_api_node_get_parent}, + {.signature = "node_queue_deletion(entity_id: int) -> None", .function = cre_pkpy_api_node_queue_deletion}, + {.signature = "node_is_queued_for_deletion(entity_id: int) -> bool", .function = cre_pkpy_api_node_is_queued_for_deletion}, + {.signature = "node_set_time_dilation(entity_id: int, dilation: float) -> None", .function = cre_pkpy_api_node_set_time_dilation}, + {.signature = "node_get_time_dilation(entity_id: int) -> float", .function = cre_pkpy_api_node_get_time_dilation}, + {.signature = "node_get_total_time_dilation(entity_id: int) -> float", .function = cre_pkpy_api_node_get_total_time_dilation}, + // Node2D + {.signature = "node2d_set_position(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_node2d_set_position}, + {.signature = "node2d_add_to_position(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_node2d_add_to_position}, + {.signature = "node2d_get_position(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_node2d_get_position}, + {.signature = "node2d_get_global_position(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_node2d_get_global_position}, + {.signature = "node2d_set_scale(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_node2d_set_scale}, + {.signature = "node2d_add_to_scale(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_node2d_add_to_scale}, + {.signature = "node2d_get_scale(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_node2d_get_scale}, + {.signature = "node2d_set_rotation(entity_id: int, rotation: float) -> None", .function = cre_pkpy_api_node2d_set_rotation}, + {.signature = "node2d_add_to_rotation(entity_id: int, rotation: float) -> None", .function = cre_pkpy_api_node2d_add_to_rotation}, + {.signature = "node2d_get_rotation(entity_id: int) -> float", .function = cre_pkpy_api_node2d_get_rotation}, + {.signature = "node2d_set_z_index(entity_id: int, z_index: int) -> None", .function = cre_pkpy_api_node2d_set_z_index}, + {.signature = "node2d_get_z_index(entity_id: int) -> int", .function = cre_pkpy_api_node2d_get_z_index}, + {.signature = "node2d_set_z_index_relative_to_parent(entity_id: int, is_relative: bool) -> None", .function = cre_pkpy_api_node2d_set_z_index_relative_to_parent}, + {.signature = "node2d_get_z_index_relative_to_parent(entity_id: int) -> bool", .function = cre_pkpy_api_node2d_get_z_index_relative_to_parent}, + {.signature = "node2d_set_ignore_camera(entity_id: int, ignore_camera: bool) -> None", .function = cre_pkpy_api_node2d_set_ignore_camera}, + {.signature = "node2d_get_ignore_camera(entity_id: int) -> bool", .function = cre_pkpy_api_node2d_get_ignore_camera}, + // Sprite + {.signature = "sprite_get_texture(entity_id: int) -> Tuple[str, str, str, bool]", .function = cre_pkpy_api_sprite_get_texture}, + {.signature = "sprite_set_texture(entity_id: int, file_path: str) -> None", .function = cre_pkpy_api_sprite_set_texture}, + {.signature = "sprite_get_draw_source(entity_id: int) -> Tuple[float, float, float, float]", .function = cre_pkpy_api_sprite_get_draw_source}, + {.signature = "sprite_set_draw_source(entity_id: int, x: float, y: float, w: float, h: float) -> None", .function = cre_pkpy_api_sprite_set_draw_source}, + {.signature = "sprite_get_flip_h(entity_id: int) -> bool", .function = cre_pkpy_api_sprite_get_flip_h}, + {.signature = "sprite_set_flip_h(entity_id: int, flip_h: bool) -> None", .function = cre_pkpy_api_sprite_set_flip_h}, + {.signature = "sprite_get_flip_v(entity_id: int) -> bool", .function = cre_pkpy_api_sprite_get_flip_v}, + {.signature = "sprite_set_flip_v(entity_id: int, flip_v: bool) -> None", .function = cre_pkpy_api_sprite_set_flip_v}, + {.signature = "sprite_get_modulate(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_sprite_get_modulate}, + {.signature = "sprite_set_modulate(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_sprite_set_modulate}, + {.signature = "sprite_get_origin(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_sprite_get_origin}, + {.signature = "sprite_set_origin(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_sprite_set_origin}, + {.signature = "sprite_get_shader_instance(entity_id: int) -> int", .function = cre_pkpy_api_sprite_get_shader_instance}, + {.signature = "sprite_set_shader_instance(entity_id: int, shader_instance_id: int) -> None", .function = cre_pkpy_api_sprite_set_shader_instance}, + // Animated Sprite + {.signature = "animated_sprite_play(entity_id: int, animation_name: str) -> bool", .function = cre_pkpy_api_animated_sprite_play}, + {.signature = "animated_sprite_stop(entity_id: int) -> None", .function = cre_pkpy_api_animated_sprite_stop}, + {.signature = "animated_sprite_set_current_animation_frame(entity_id: int, frame: int) -> None", .function = cre_pkpy_api_animated_sprite_set_current_animation_frame}, + {.signature = "animated_sprite_add_animation(entity_id: int, name: str, speed: float, does_loop: bool, frame_count: int, *args) -> None", .function = cre_pkpy_api_animated_sprite_add_animation}, + {.signature = "animated_sprite_get_stagger_animation_start_times(entity_id: int) -> bool", .function = cre_pkpy_api_animated_sprite_get_stagger_animation_start_times}, + {.signature = "animated_sprite_set_stagger_animation_start_times(entity_id: int, stagger: bool) -> None", .function = cre_pkpy_api_animated_sprite_set_stagger_animation_start_times}, + {.signature = "animated_sprite_get_flip_h(entity_id: int) -> bool", .function = cre_pkpy_api_animated_sprite_get_flip_h}, + {.signature = "animated_sprite_set_flip_h(entity_id: int, flip_h: bool) -> None", .function = cre_pkpy_api_animated_sprite_set_flip_h}, + {.signature = "animated_sprite_get_flip_v(entity_id: int) -> bool", .function = cre_pkpy_api_animated_sprite_get_flip_v}, + {.signature = "animated_sprite_set_flip_v(entity_id: int, flip_v: bool) -> None", .function = cre_pkpy_api_animated_sprite_set_flip_v}, + {.signature = "animated_sprite_get_modulate(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_animated_sprite_get_modulate}, + {.signature = "animated_sprite_set_modulate(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_animated_sprite_set_modulate}, + {.signature = "animated_sprite_get_origin(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_animated_sprite_get_origin}, + {.signature = "animated_sprite_set_origin(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_animated_sprite_set_origin}, + {.signature = "animated_sprite_get_shader_instance(entity_id: int) -> int", .function = cre_pkpy_api_animated_sprite_get_shader_instance}, + {.signature = "animated_sprite_set_shader_instance(entity_id: int, shader_instance_id: int) -> None", .function = cre_pkpy_api_animated_sprite_set_shader_instance}, + // Text Label + {.signature = "text_label_get_text(entity_id: int) -> str", .function = cre_pkpy_api_text_label_get_text}, + {.signature = "text_label_set_text(entity_id: int, text: str) -> None", .function = cre_pkpy_api_text_label_set_text}, + {.signature = "text_label_get_color(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_text_label_get_color}, + {.signature = "text_label_set_color(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_text_label_set_color}, + {.signature = "text_label_set_font_uid(entity_id: int, uid: str) -> None", .function = cre_pkpy_api_text_label_set_font_uid}, + // Collider2D + {.signature = "collider2d_get_extents(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_collider2d_get_extents}, + {.signature = "collider2d_set_extents(entity_id: int, w: float, h: float)", .function = cre_pkpy_api_collider2d_set_extents}, + {.signature = "collider2d_get_color(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_collider2d_get_color}, + {.signature = "collider2d_set_color(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_collider2d_set_color}, + // Color Rect + {.signature = "color_rect_get_size(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_color_rect_get_size}, + {.signature = "color_rect_set_size(entity_id: int, w: float, h: float) -> None", .function = cre_pkpy_api_color_rect_set_size}, + {.signature = "color_rect_get_color(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_color_rect_get_color}, + {.signature = "color_rect_set_color(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_color_rect_set_color}, + // Parallax + {.signature = "parallax_get_scroll_speed(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_parallax_get_scroll_speed}, + {.signature = "parallax_set_scroll_speed(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_parallax_set_scroll_speed}, + // Particles2D + {.signature = "particles2d_get_amount(entity_id: int) -> int", .function = cre_pkpy_api_particles2d_get_amount}, + {.signature = "particles2d_set_amount(entity_id: int, amount: int) -> None", .function = cre_pkpy_api_particles2d_set_amount}, + {.signature = "particles2d_get_life_time(entity_id: int) -> float", .function = cre_pkpy_api_particles2d_get_life_time}, + {.signature = "particles2d_set_life_time(entity_id: int, life_time: float) -> None", .function = cre_pkpy_api_particles2d_set_life_time}, + {.signature = "particles2d_set_damping(entity_id: int, damping: float) -> None", .function = cre_pkpy_api_particles2d_set_damping}, + {.signature = "particles2d_get_damping(entity_id: int) -> float", .function = cre_pkpy_api_particles2d_get_damping}, + {.signature = "particles2d_set_explosiveness(entity_id: int, explosiveness: float) -> None", .function = cre_pkpy_api_particles2d_set_explosiveness}, + {.signature = "particles2d_get_explosiveness(entity_id: int) -> float", .function = cre_pkpy_api_particles2d_get_explosiveness}, + {.signature = "particles2d_get_color(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_particles2d_get_color}, + {.signature = "particles2d_set_color(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_particles2d_set_color}, + {.signature = "particles2d_get_initial_velocity(entity_id: int) -> Tuple[float, float, float, float]", .function = cre_pkpy_api_particles2d_get_initial_velocity}, + {.signature = "particles2d_set_initial_velocity(entity_id: int, min_x: float, min_y: float, max_x: float, max_y: float) -> None", .function = cre_pkpy_api_particles2d_set_initial_velocity}, + {.signature = "particles2d_get_spread(entity_id: int) -> float", .function = cre_pkpy_api_particles2d_get_spread}, + {.signature = "particles2d_set_spread(entity_id: int, spread: float) -> None", .function = cre_pkpy_api_particles2d_set_spread}, + // Scene Tree + {.signature = "scene_tree_change_scene(path: str) -> None", .function = cre_pkpy_api_scene_tree_change_scene}, + {.signature = "scene_tree_get_root()", .function = cre_pkpy_api_scene_tree_get_root}, + // Scene Manager + {.signature = "_scene_manager_process_queued_creation_entities() -> None", .function = cre_pkpy_api_scene_manager_process_queued_creation_entities}, + {.signature = "_scene_manager_process_queued_scene_change() -> None", .function = cre_pkpy_api_scene_manager_process_queued_scene_change}, + // Game Properties + {.signature = "game_properties_get() -> Tuple[str, int, int, int, int, int, str, bool]", .function = cre_pkpy_api_game_properties_get}, + // Camera2D + {.signature = "camera2d_set_position(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_set_position}, + {.signature = "camera2d_add_to_position(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_add_to_position}, + {.signature = "camera2d_get_position() -> Tuple[float, float]", .function = cre_pkpy_api_camera2d_get_position}, + {.signature = "camera2d_set_offset(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_set_offset}, + {.signature = "camera2d_add_to_offset(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_add_to_offset}, + {.signature = "camera2d_get_offset() -> Tuple[float, float]", .function = cre_pkpy_api_camera2d_get_offset}, + {.signature = "camera2d_set_zoom(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_set_zoom}, + {.signature = "camera2d_add_to_zoom(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_add_to_zoom}, + {.signature = "camera2d_get_zoom() -> Tuple[float, float]", .function = cre_pkpy_api_camera2d_get_zoom}, + {.signature = "camera2d_set_boundary(x: float, y: float, w: float, h: float) -> None", .function = cre_pkpy_api_camera2d_set_boundary}, + {.signature = "camera2d_get_boundary() -> Tuple[float, float, float, float]", .function = cre_pkpy_api_camera2d_get_boundary}, + {.signature = "camera2d_follow_node(entity_id: int) -> None", .function = cre_pkpy_api_camera2d_follow_node}, + {.signature = "camera2d_unfollow_node(entity_id: int) -> None", .function = cre_pkpy_api_camera2d_unfollow_node}, + // World + {.signature = "world_set_time_dilation(time_dilation: float) -> None", .function = cre_pkpy_api_world_set_time_dilation}, + {.signature = "world_get_time_dilation() -> float", .function = cre_pkpy_api_world_get_time_dilation}, + {.signature = "world_get_delta_time() -> float", .function = cre_pkpy_api_world_get_delta_time}, + {.signature = "world_get_variable_delta_time() -> float", .function = cre_pkpy_api_world_get_variable_delta_time}, + // Audio Source + {.signature = "audio_source_set_pitch(path: str, pitch: float) -> None", .function = cre_pkpy_api_audio_source_set_pitch}, + {.signature = "audio_source_get_pitch(path: str) -> float", .function = cre_pkpy_api_audio_source_get_pitch}, + // Audio Manager + {.signature = "audio_manager_play_sound(path: str, loops: bool) -> None", .function = cre_pkpy_api_audio_manager_play_sound}, + {.signature = "audio_manager_stop_sound(path: str) -> None", .function = cre_pkpy_api_audio_manager_stop_sound}, + // Game Config + {.signature = "game_config_save(path: str, json_text: str, encryption_key: str) -> bool", .function = cre_pkpy_api_game_config_save}, + {.signature = "game_config_load(path, encryption_key) -> str", .function = cre_pkpy_api_game_config_load}, + // Packed Scene + {.signature = "packed_scene_create_instance(scene_cache_id: int) -> \"Node\"", .function = cre_pkpy_api_packed_scene_create_instance}, + {.signature = "packed_scene_load(path: str) -> int", .function = cre_pkpy_api_packed_scene_load}, + // Collision Handler + {.signature = "collision_handler_process_collisions(entity_id: float) -> Tuple[\"Node\", ...]", .function = cre_pkpy_api_collision_handler_process_collisions}, + {.signature = "collision_handler_process_mouse_collisions(pos_offset_x: float, pos_offset_y: float, collision_size_w: float, collision_size_h: float) -> Tuple[\"Node\", ...]", .function = cre_pkpy_api_collision_handler_process_mouse_collisions}, + // Network + {.signature = "network_is_server() -> bool", .function = cre_pkpy_api_network_is_server}, + // Server + {.signature = "server_start(port: int) -> None", .function = cre_pkpy_api_server_start}, + {.signature = "server_stop() -> None", .function = cre_pkpy_api_server_stop}, + {.signature = "server_send(message: str) -> None", .function = cre_pkpy_api_server_send}, + // Client + {.signature = "client_start(host: str, port: int) -> None", .function = cre_pkpy_api_client_start}, + {.signature = "client_stop() -> None", .function = cre_pkpy_api_client_stop}, + {.signature = "client_send(message: str) -> None", .function = cre_pkpy_api_client_send}, + + { NULL, NULL }, + } + }); + + // Now load the front facing crecent module + cre_pkpy_util_create_from_string(CRE_PKPY_MODULE_NAME_CRESCENT, CRE_PKPY_CRESCENT_SOURCE); +} diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api.h b/engine/src/core/scripting/python/pocketpy/api/pkpy_api.h new file mode 100644 index 000000000..183803ea0 --- /dev/null +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api.h @@ -0,0 +1,3 @@ +#pragma once + +void cre_pkpy_api_load_internal_modules(); diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c new file mode 100644 index 000000000..b1e778edf --- /dev/null +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -0,0 +1,217 @@ +#include "pkpy_api_impl.h" + +// Shader Instance +bool cre_pkpy_api_shader_instance_delete(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_create_bool_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_set_bool_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_get_bool_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_create_int_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_set_int_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_get_int_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_create_float_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_set_float_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_get_float_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_create_float2_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_set_float2_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_get_float2_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_create_float3_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_set_float3_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_get_float3_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_create_float4_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_set_float4_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_get_float4_param(int argc, py_StackRef argv) { return true; } + +// Shader Util +bool cre_pkpy_api_shader_util_compile_shader(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_util_compile_shader_raw(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_util_set_screen_shader(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_util_get_current_screen_shader(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_util_reset_screen_shader_to_default(int argc, py_StackRef argv) { return true; } + +// Engine +bool cre_pkpy_api_engine_exit(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_engine_set_target_fps(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_engine_get_target_fps(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_engine_get_average_fps(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_engine_set_fps_display_enabled(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_engine_get_global_physics_delta_time(int argc, py_StackRef argv) { return true; } + +// Input +bool cre_pkpy_api_input_is_key_pressed(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_input_is_key_just_pressed(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_input_is_key_just_released(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_input_add_action(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_input_is_action_pressed(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_input_is_action_just_pressed(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_input_is_action_just_released(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_input_start_gamepad_vibration(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_input_stop_gamepad_vibration(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_input_mouse_get_position(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_input_mouse_get_world_position(int argc, py_StackRef argv) { return true; } + +// Scene Tree +bool cre_pkpy_api_scene_tree_change_scene(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_scene_tree_get_root(int argc, py_StackRef argv) { return true; } + +// Scene Manager +bool cre_pkpy_api_scene_manager_process_queued_creation_entities(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_scene_manager_process_queued_scene_change(int argc, py_StackRef argv) { return true; } + +// Game Properties +bool cre_pkpy_api_game_properties_get(int argc, py_StackRef argv) { return true; } + +// Camera2D +bool cre_pkpy_api_camera2d_set_position(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_add_to_position(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_get_position(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_set_offset(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_add_to_offset(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_get_offset(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_set_zoom(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_add_to_zoom(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_get_zoom(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_set_boundary(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_get_boundary(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_follow_node(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_unfollow_node(int argc, py_StackRef argv) { return true; } + +// World +bool cre_pkpy_api_world_set_time_dilation(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_world_get_time_dilation(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_world_get_delta_time(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_world_get_variable_delta_time(int argc, py_StackRef argv) { return true; } + +// Audio Source +bool cre_pkpy_api_audio_source_set_pitch(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_audio_source_get_pitch(int argc, py_StackRef argv) { return true; } + +// Audio Manager +bool cre_pkpy_api_audio_manager_play_sound(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_audio_manager_stop_sound(int argc, py_StackRef argv) { return true; } + +// Game Config +bool cre_pkpy_api_game_config_save(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_game_config_load(int argc, py_StackRef argv) { return true; } + +// Packed Scene +bool cre_pkpy_api_packed_scene_create_instance(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_packed_scene_load(int argc, py_StackRef argv) { return true; } + +// Collision Handler +bool cre_pkpy_api_collision_handler_process_collisions(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_collision_handler_process_mouse_collisions(int argc, py_StackRef argv) { return true; } + +// Network +bool cre_pkpy_api_network_is_server(int argc, py_StackRef argv) { return true; } +// Server +bool cre_pkpy_api_server_start(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_server_stop(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_server_send(int argc, py_StackRef argv) { return true; } +// Client +bool cre_pkpy_api_client_start(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_client_stop(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_client_send(int argc, py_StackRef argv) { return true; } + +// Node +bool cre_pkpy_api_node_new(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node_get_name(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node_add_child(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node_get_child(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node_get_children(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node_get_parent(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node_queue_deletion(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node_is_queued_for_deletion(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node_set_time_dilation(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node_get_time_dilation(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node_get_total_time_dilation(int argc, py_StackRef argv) { return true; } + +// Node2D +bool cre_pkpy_api_node2d_set_position(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_add_to_position(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_get_position(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_get_global_position(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_set_scale(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_add_to_scale(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_get_scale(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_set_rotation(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_add_to_rotation(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_get_rotation(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_set_z_index(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_get_z_index(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_set_z_index_relative_to_parent(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_get_z_index_relative_to_parent(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_set_ignore_camera(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_node2d_get_ignore_camera(int argc, py_StackRef argv) { return true; } + +// Sprite +bool cre_pkpy_api_sprite_get_texture(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_set_texture(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_get_draw_source(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_set_draw_source(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_get_flip_h(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_set_flip_h(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_get_flip_v(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_set_flip_v(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_get_modulate(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_set_modulate(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_get_origin(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_set_origin(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_get_shader_instance(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_set_shader_instance(int argc, py_StackRef argv) { return true; } + +// Animated Sprite +bool cre_pkpy_api_animated_sprite_play(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_stop(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_set_current_animation_frame(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_add_animation(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_get_stagger_animation_start_times(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_set_stagger_animation_start_times(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_get_flip_h(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_set_flip_h(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_get_flip_v(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_set_flip_v(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_get_modulate(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_set_modulate(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_get_origin(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_set_origin(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_get_shader_instance(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_set_shader_instance(int argc, py_StackRef argv) { return true; } + +// Text Label +bool cre_pkpy_api_text_label_get_text(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_text_label_set_text(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_text_label_get_color(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_text_label_set_color(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_text_label_set_font_uid(int argc, py_StackRef argv) { return true; } + +// Collider2D +bool cre_pkpy_api_collider2d_get_extents(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_collider2d_set_extents(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_collider2d_get_color(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_collider2d_set_color(int argc, py_StackRef argv) { return true; } + +// Color Rect +bool cre_pkpy_api_color_rect_get_size(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_color_rect_set_size(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_color_rect_get_color(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_color_rect_set_color(int argc, py_StackRef argv) { return true; } + +// Parallax +bool cre_pkpy_api_parallax_get_scroll_speed(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_parallax_set_scroll_speed(int argc, py_StackRef argv) { return true; } + +// Particles2D +bool cre_pkpy_api_particles2d_get_amount(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_set_amount(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_get_life_time(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_set_life_time(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_get_damping(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_set_damping(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_get_explosiveness(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_set_explosiveness(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_get_color(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_set_color(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_get_initial_velocity(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_set_initial_velocity(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_get_spread(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_set_spread(int argc, py_StackRef argv) { return true; } diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.h b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.h new file mode 100644 index 000000000..57be3b8cc --- /dev/null +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.h @@ -0,0 +1,221 @@ +#pragma once + +#include + +#include + +// Shader Instance +bool cre_pkpy_api_shader_instance_delete(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_create_bool_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_set_bool_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_get_bool_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_create_int_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_set_int_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_get_int_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_create_float_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_set_float_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_get_float_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_create_float2_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_set_float2_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_get_float2_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_create_float3_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_set_float3_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_get_float3_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_create_float4_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_set_float4_param(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_instance_get_float4_param(int argc, py_StackRef argv); + +// Shader Util +bool cre_pkpy_api_shader_util_compile_shader(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_util_compile_shader_raw(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_util_set_screen_shader(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_util_get_current_screen_shader(int argc, py_StackRef argv); +bool cre_pkpy_api_shader_util_reset_screen_shader_to_default(int argc, py_StackRef argv); + +// Engine +bool cre_pkpy_api_engine_exit(int argc, py_StackRef argv); +bool cre_pkpy_api_engine_set_target_fps(int argc, py_StackRef argv); +bool cre_pkpy_api_engine_get_target_fps(int argc, py_StackRef argv); +bool cre_pkpy_api_engine_get_average_fps(int argc, py_StackRef argv); +bool cre_pkpy_api_engine_set_fps_display_enabled(int argc, py_StackRef argv); +bool cre_pkpy_api_engine_get_global_physics_delta_time(int argc, py_StackRef argv); + +// Input +bool cre_pkpy_api_input_is_key_pressed(int argc, py_StackRef argv); +bool cre_pkpy_api_input_is_key_just_pressed(int argc, py_StackRef argv); +bool cre_pkpy_api_input_is_key_just_released(int argc, py_StackRef argv); +bool cre_pkpy_api_input_add_action(int argc, py_StackRef argv); +bool cre_pkpy_api_input_is_action_pressed(int argc, py_StackRef argv); +bool cre_pkpy_api_input_is_action_just_pressed(int argc, py_StackRef argv); +bool cre_pkpy_api_input_is_action_just_released(int argc, py_StackRef argv); +bool cre_pkpy_api_input_start_gamepad_vibration(int argc, py_StackRef argv); +bool cre_pkpy_api_input_stop_gamepad_vibration(int argc, py_StackRef argv); +bool cre_pkpy_api_input_mouse_get_position(int argc, py_StackRef argv); +bool cre_pkpy_api_input_mouse_get_world_position(int argc, py_StackRef argv); + +// Scene Tree +bool cre_pkpy_api_scene_tree_change_scene(int argc, py_StackRef argv); +bool cre_pkpy_api_scene_tree_get_root(int argc, py_StackRef argv); + +// Scene Manager +bool cre_pkpy_api_scene_manager_process_queued_creation_entities(int argc, py_StackRef argv); +bool cre_pkpy_api_scene_manager_process_queued_scene_change(int argc, py_StackRef argv); + +// Game Properties +bool cre_pkpy_api_game_properties_get(int argc, py_StackRef argv); + +// Camera2D +bool cre_pkpy_api_camera2d_set_position(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_add_to_position(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_get_position(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_set_offset(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_add_to_offset(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_get_offset(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_set_zoom(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_add_to_zoom(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_get_zoom(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_set_boundary(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_get_boundary(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_follow_node(int argc, py_StackRef argv); +bool cre_pkpy_api_camera2d_unfollow_node(int argc, py_StackRef argv); + +// World +bool cre_pkpy_api_world_set_time_dilation(int argc, py_StackRef argv); +bool cre_pkpy_api_world_get_time_dilation(int argc, py_StackRef argv); +bool cre_pkpy_api_world_get_delta_time(int argc, py_StackRef argv); +bool cre_pkpy_api_world_get_variable_delta_time(int argc, py_StackRef argv); + +// Audio Source +bool cre_pkpy_api_audio_source_set_pitch(int argc, py_StackRef argv); +bool cre_pkpy_api_audio_source_get_pitch(int argc, py_StackRef argv); + +// Audio Manager +bool cre_pkpy_api_audio_manager_play_sound(int argc, py_StackRef argv); +bool cre_pkpy_api_audio_manager_stop_sound(int argc, py_StackRef argv); + +// Game Config +bool cre_pkpy_api_game_config_save(int argc, py_StackRef argv); +bool cre_pkpy_api_game_config_load(int argc, py_StackRef argv); + +// Packed Scene +bool cre_pkpy_api_packed_scene_create_instance(int argc, py_StackRef argv); +bool cre_pkpy_api_packed_scene_load(int argc, py_StackRef argv); + +// Collision Handler +bool cre_pkpy_api_collision_handler_process_collisions(int argc, py_StackRef argv); +bool cre_pkpy_api_collision_handler_process_mouse_collisions(int argc, py_StackRef argv); + +// Network +bool cre_pkpy_api_network_is_server(int argc, py_StackRef argv); +// Server +bool cre_pkpy_api_server_start(int argc, py_StackRef argv); +bool cre_pkpy_api_server_stop(int argc, py_StackRef argv); +bool cre_pkpy_api_server_send(int argc, py_StackRef argv); +// Client +bool cre_pkpy_api_client_start(int argc, py_StackRef argv); +bool cre_pkpy_api_client_stop(int argc, py_StackRef argv); +bool cre_pkpy_api_client_send(int argc, py_StackRef argv); + +// Node +bool cre_pkpy_api_node_new(int argc, py_StackRef argv); +bool cre_pkpy_api_node_get_name(int argc, py_StackRef argv); +bool cre_pkpy_api_node_add_child(int argc, py_StackRef argv); +bool cre_pkpy_api_node_get_child(int argc, py_StackRef argv); +bool cre_pkpy_api_node_get_children(int argc, py_StackRef argv); +bool cre_pkpy_api_node_get_parent(int argc, py_StackRef argv); +bool cre_pkpy_api_node_queue_deletion(int argc, py_StackRef argv); +bool cre_pkpy_api_node_is_queued_for_deletion(int argc, py_StackRef argv); +bool cre_pkpy_api_node_set_time_dilation(int argc, py_StackRef argv); +bool cre_pkpy_api_node_get_time_dilation(int argc, py_StackRef argv); +bool cre_pkpy_api_node_get_total_time_dilation(int argc, py_StackRef argv); + +// Node2D +bool cre_pkpy_api_node2d_set_position(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_add_to_position(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_get_position(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_get_global_position(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_set_scale(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_add_to_scale(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_get_scale(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_set_rotation(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_add_to_rotation(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_get_rotation(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_set_z_index(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_get_z_index(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_set_z_index_relative_to_parent(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_get_z_index_relative_to_parent(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_set_ignore_camera(int argc, py_StackRef argv); +bool cre_pkpy_api_node2d_get_ignore_camera(int argc, py_StackRef argv); + +// Sprite +bool cre_pkpy_api_sprite_get_texture(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_set_texture(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_get_draw_source(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_set_draw_source(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_get_flip_h(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_set_flip_h(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_get_flip_v(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_set_flip_v(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_get_modulate(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_set_modulate(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_get_origin(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_set_origin(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_get_shader_instance(int argc, py_StackRef argv); +bool cre_pkpy_api_sprite_set_shader_instance(int argc, py_StackRef argv); + +// Animated Sprite +bool cre_pkpy_api_animated_sprite_play(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_stop(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_set_current_animation_frame(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_add_animation(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_get_stagger_animation_start_times(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_set_stagger_animation_start_times(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_get_flip_h(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_set_flip_h(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_get_flip_v(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_set_flip_v(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_get_modulate(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_set_modulate(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_get_origin(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_set_origin(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_get_shader_instance(int argc, py_StackRef argv); +bool cre_pkpy_api_animated_sprite_set_shader_instance(int argc, py_StackRef argv); + +// Text Label +bool cre_pkpy_api_text_label_get_text(int argc, py_StackRef argv); +bool cre_pkpy_api_text_label_set_text(int argc, py_StackRef argv); +bool cre_pkpy_api_text_label_get_color(int argc, py_StackRef argv); +bool cre_pkpy_api_text_label_set_color(int argc, py_StackRef argv); +bool cre_pkpy_api_text_label_set_font_uid(int argc, py_StackRef argv); + +// Collider2D +bool cre_pkpy_api_collider2d_get_extents(int argc, py_StackRef argv); +bool cre_pkpy_api_collider2d_set_extents(int argc, py_StackRef argv); +bool cre_pkpy_api_collider2d_get_color(int argc, py_StackRef argv); +bool cre_pkpy_api_collider2d_set_color(int argc, py_StackRef argv); + +// Color Rect +bool cre_pkpy_api_color_rect_get_size(int argc, py_StackRef argv); +bool cre_pkpy_api_color_rect_set_size(int argc, py_StackRef argv); +bool cre_pkpy_api_color_rect_get_color(int argc, py_StackRef argv); +bool cre_pkpy_api_color_rect_set_color(int argc, py_StackRef argv); + +// Parallax +bool cre_pkpy_api_parallax_get_scroll_speed(int argc, py_StackRef argv); +bool cre_pkpy_api_parallax_set_scroll_speed(int argc, py_StackRef argv); + +// Particles2D +bool cre_pkpy_api_particles2d_get_amount(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_set_amount(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_get_life_time(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_set_life_time(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_get_damping(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_set_damping(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_get_explosiveness(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_set_explosiveness(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_get_color(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_set_color(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_get_initial_velocity(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_set_initial_velocity(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_get_spread(int argc, py_StackRef argv); +bool cre_pkpy_api_particles2d_set_spread(int argc, py_StackRef argv); diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index adf60a05b..723f80347 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -1,22 +1,32 @@ #include "pkpy_script_context.h" #include +#include #include #include #include +#include "pkpy_util.h" +#include "api/pkpy_api.h" + +#define PY_ASSERT_NO_EXC() SKA_ASSERT_FMT(!py_checkexc(false), "PKPY Error:\n%s", py_formatexc()); + //--- Entity Instance Cache ---// + typedef struct CreEntityInstanceCache { - py_Ref cacheDict; + py_Name name; py_Ref instances[SKA_MAX_ENTITIES]; } CreEntityInstanceCache; static CreEntityInstanceCache entityInstanceCache = {0}; -static char entityCacheStringBuffer[16]; +static char entityCacheStringBuffer[48]; static void instance_cache_init() { - py_newdict(entityInstanceCache.cacheDict); + // entityInstanceCache.name = py_name("_inst_cache"); + // py_Ref cacheDict = py_retval(); + // py_newdict(cacheDict); + // py_setglobal(entityInstanceCache.name, cacheDict); } static void instance_cache_finalize() { @@ -33,29 +43,73 @@ static py_Ref instance_cache_add(SkaEntity entity,const char* classPath, const c SKA_ASSERT(wasImportSuccessful); py_Ref module = py_getmodule(classPath); SKA_ASSERT_FMT(module, "module '%s' not found!", classPath); - static char callInstanceBuffer[96]; - sprintf_s(callInstanceBuffer, sizeof(callInstanceBuffer), "%s(%d)", className, entity); - py_eval(callInstanceBuffer, module); - py_Ref instanceRef = py_retval(); + + // snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "%s.%s(%u)", classPath, className, entity); + // py_eval(entityCacheStringBuffer, NULL); + // snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u = %s.%s(%u)", entity, classPath, className, entity); + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "from %s import %s", classPath, className); + py_exec(entityCacheStringBuffer, "
", EXEC_MODE, NULL); + PY_ASSERT_NO_EXC(); + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u = %s(%u)", entity, className, entity); + py_exec(entityCacheStringBuffer, "
", EXEC_MODE, NULL); + PY_ASSERT_NO_EXC(); + + // py_Type instType = py_gettype(classPath, py_name(className)); + // SKA_ASSERT(instType); + // py_Ref instanceRef = py_retval(); + // py_newobject(instanceRef, instType, 1, 0); + // py_Ref pyEntity = py_pushtmp(); + // py_newint(pyEntity, entity); + // py_setslot(instanceRef, 0, pyEntity); + + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); + // py_Ref instanceRef = py_peek(-1); + py_Ref instanceRef = py_getglobal(py_name(entityCacheStringBuffer)); + // py_pop(); + + // static char callInstanceBuffer[96]; + // sprintf_s(callInstanceBuffer, sizeof(callInstanceBuffer), "%s(%d)", className, entity); + // py_eval(callInstanceBuffer, module); + // py_Ref instanceRef = py_retval(); SKA_ASSERT_FMT(instanceRef, "Unable to create instance from '%s.%s'!", classPath, className); entityInstanceCache.instances[entity] = instanceRef; - snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "%u", entity); - py_setdict(entityInstanceCache.cacheDict, py_name(entityCacheStringBuffer), instanceRef); + // snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); + // py_setglobal(py_name(entityCacheStringBuffer), instanceRef); + // py_Ref cacheDict = py_getglobal(entityInstanceCache.name); + // SKA_ASSERT(py_isdict(cacheDict)); + // py_setdict(cacheDict, py_name(entityCacheStringBuffer), instanceRef); + // py_pop(); + return instanceRef; } static void instance_cache_remove(SkaEntity entity) { if (entityInstanceCache.instances[entity]) { - snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "%u", entity); - py_deldict(entityInstanceCache.cacheDict, py_name(entityCacheStringBuffer)); + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); + // py_Ref cacheDict = py_getglobal(entityInstanceCache.name); + // SKA_ASSERT(cacheDict); + // py_deldict(cacheDict, py_name(entityCacheStringBuffer)); + py_setglobal(py_name(entityCacheStringBuffer), py_None); entityInstanceCache.instances[entity] = NULL; } } static py_Ref instance_cache_get(SkaEntity entity) { - return entityInstanceCache.instances[entity]; + // return entityInstanceCache.instances[entity]; + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); + return py_getglobal(py_name(entityCacheStringBuffer)); } +static py_Ref instance_cache_get_checked(SkaEntity entity) { + SKA_ASSERT(entityInstanceCache.instances[entity]); + return instance_cache_get(entity); + // py_Ref cacheDict = py_getglobal(entityInstanceCache.name); + // SKA_ASSERT(py_isdict(cacheDict)); + // py_dict_getitem_by_str(cacheDict, entityCacheStringBuffer); + // return entityInstanceCache.instances[entity]; + + // return py_retval(); +} static CREScriptContext* scriptContext = NULL; static py_Name startFunctionName; @@ -66,6 +120,7 @@ static py_Name endFunctionName; static char* pkpy_import_file(const char* path); //--- Script Context Interface ---// + static void pkpy_init(CREScriptContext* context); static void pkpy_finalize(CREScriptContext* context); static void pkpy_create_instance(SkaEntity entity, const char* classPath, const char* className); @@ -106,6 +161,9 @@ void pkpy_init(CREScriptContext* context) { py_Callbacks* callbacks = py_callbacks(); callbacks->importfile = pkpy_import_file; + // Import internal modules + cre_pkpy_api_load_internal_modules(); + scriptContext = context; } @@ -124,17 +182,26 @@ void pkpy_delete_instance(SkaEntity entity) { } void pkpy_on_start(SkaEntity entity) { - py_Ref self = instance_cache_get(entity); - SKA_ASSERT(self); + py_Ref self = instance_cache_get_checked(entity); if (py_getattr(self, startFunctionName)) { - py_Ref func = py_retval(); - py_call(func, 1, self); + // py_exec("_e_1._start()", "__main__", EXEC_MODE, NULL); + // py_exec("print(_e_1.entity_id)", "__main__", EXEC_MODE, NULL); + py_smartexec("_._start()", NULL, self); + // py_Ref func = py_retval(); + // const char* typeName = py_tpname(func->type); + // py_Type funcType = func->type; + // py_pushmethod(startFunctionName); + // py_push(func); + // py_pushnil(); + // py_vectorcall(0, 0); + PY_ASSERT_NO_EXC(); + } else { + py_clearexc(NULL); } } void pkpy_on_end(SkaEntity entity) { - py_Ref self = instance_cache_get(entity); - SKA_ASSERT(self); + py_Ref self = instance_cache_get_checked(entity); if (py_getattr(self, endFunctionName)) { py_Ref func = py_retval(); py_call(func, 1, self); @@ -142,24 +209,38 @@ void pkpy_on_end(SkaEntity entity) { } void pkpy_on_update(SkaEntity entity, f32 deltaTime) { - py_Ref self = instance_cache_get(entity); - SKA_ASSERT(self); - if (py_getattr(self, endFunctionName)) { + py_Ref self = instance_cache_get_checked(entity); + if (py_getattr(self, processFunctionName)) { py_Ref func = py_retval(); - py_call(func, 1, self); + py_Ref pyDeltaTime = NULL; + py_newfloat(pyDeltaTime, deltaTime); + py_push(func); + py_pushnil(); + py_push(pyDeltaTime); + py_vectorcall(2, 0); } } -void pkpy_on_fixed_update(SkaEntity entity, f32 deltaTime) {} +void pkpy_on_fixed_update(SkaEntity entity, f32 deltaTime) { + py_Ref self = instance_cache_get_checked(entity); + if (py_getattr(self, fixedProcessFunctionName)) { + py_Ref func = py_retval(); + py_Ref pyDeltaTime = NULL; + py_newfloat(pyDeltaTime, deltaTime); + py_push(func); + py_pushnil(); + py_push(pyDeltaTime); + py_vectorcall(2, 0); + } +} -void pkpy_network_callback(const char* message) {} +void pkpy_network_callback(const char* message) { + SKA_ASSERT_FMT(false, "TODO: Implement!"); +} char* pkpy_import_file(const char* path) { // Use built in asset loader to load script instead of pkpy's default char* moduleString = ska_asset_file_loader_read_file_contents_as_string(path, NULL); - if (!moduleString) { - ska_logger_error("Failed to load pkpy module at path'%s'", path); - return NULL; - } + SKA_ASSERT_FMT(moduleString, "Failed to load pkpy module at path '%s'", path); return moduleString; } diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_util.c b/engine/src/core/scripting/python/pocketpy/pkpy_util.c new file mode 100644 index 000000000..c31cdca83 --- /dev/null +++ b/engine/src/core/scripting/python/pocketpy/pkpy_util.c @@ -0,0 +1,19 @@ +#include "pkpy_util.h" + +void cre_pkpy_util_create_module(CrePPModule* module) { + py_Ref newModule = py_newmodule(module->name); + SKA_ASSERT(newModule); + for (size_t i = 0; i < CRE_PKPY_MODULE_FUNCTION_LIMIT; i++) { + const CrePPFunction* func = &module->functions[i]; + if (!func->signature || !func->function) { break; } + py_bind(newModule, func->signature, func->function); + // py_bindfunc(newModule, func->signature, func->function); + } +} + +void cre_pkpy_util_create_from_string(const char* moduleName, const char* moduleSource) { + py_Ref newModule = py_newmodule(moduleName); + SKA_ASSERT(newModule); + // py_exec(moduleSource, moduleName, EXEC_MODE, newModule); + py_exec(moduleSource, moduleName, EXEC_MODE, NULL); +} diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_util.h b/engine/src/core/scripting/python/pocketpy/pkpy_util.h new file mode 100644 index 000000000..4e9b6a2b5 --- /dev/null +++ b/engine/src/core/scripting/python/pocketpy/pkpy_util.h @@ -0,0 +1,21 @@ +#pragma once + +#include + +#include +#include + +#define CRE_PKPY_MODULE_FUNCTION_LIMIT 256 + +typedef struct CrePPFunction { + const char* signature; + py_CFunction function; +} CrePPFunction; + +typedef struct CrePPModule { + const char* name; + CrePPFunction functions[CRE_PKPY_MODULE_FUNCTION_LIMIT]; +} CrePPModule; + +void cre_pkpy_util_create_module(CrePPModule* module); +void cre_pkpy_util_create_from_string(const char* moduleName, const char* moduleSource); diff --git a/engine/src/main.c b/engine/src/main.c index c497de57b..1bfc6cd2f 100644 --- a/engine/src/main.c +++ b/engine/src/main.c @@ -1,17 +1,33 @@ #include -#include +// #include + +#include #include "core/core.h" + int main(int argv, char** args) { - if (!cre_initialize(argv, args)) { - return EXIT_FAILURE; - } + // if (!cre_initialize(argv, args)) { + // return EXIT_FAILURE; + // } + // + // while (cre_is_running()) { + // cre_update(); + // } + // + // return cre_shutdown(); + + py_initialize(); - while (cre_is_running()) { - cre_update(); + py_exec("class A:\n def start(self): print('start')\n\na = A()", "main.py", EXEC_MODE, NULL); + py_Ref self = py_getglobal(py_name("a")); + if (py_getattr(self, py_name("start"))) { + py_push(py_retval()); + py_pushnil(); + py_vectorcall(0, 0); } - return cre_shutdown(); + py_finalize(); + return EXIT_SUCCESS; } From db5579182a145db515cb8d68bbf3a28dcaa190bd Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Thu, 26 Sep 2024 21:37:53 -0400 Subject: [PATCH 08/54] Clean. --- .../core/scripting/python/pocketpy/pkpy_script_context.c | 8 ++++---- engine/src/main.c | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index 723f80347..c549618d6 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -48,10 +48,10 @@ static py_Ref instance_cache_add(SkaEntity entity,const char* classPath, const c // py_eval(entityCacheStringBuffer, NULL); // snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u = %s.%s(%u)", entity, classPath, className, entity); snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "from %s import %s", classPath, className); - py_exec(entityCacheStringBuffer, "
", EXEC_MODE, NULL); + py_exec(entityCacheStringBuffer, "main.py", EXEC_MODE, NULL); PY_ASSERT_NO_EXC(); snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u = %s(%u)", entity, className, entity); - py_exec(entityCacheStringBuffer, "
", EXEC_MODE, NULL); + py_exec(entityCacheStringBuffer, "main.py", EXEC_MODE, NULL); PY_ASSERT_NO_EXC(); // py_Type instType = py_gettype(classPath, py_name(className)); @@ -184,9 +184,9 @@ void pkpy_delete_instance(SkaEntity entity) { void pkpy_on_start(SkaEntity entity) { py_Ref self = instance_cache_get_checked(entity); if (py_getattr(self, startFunctionName)) { - // py_exec("_e_1._start()", "__main__", EXEC_MODE, NULL); + py_exec("_e_1._start()", "main.py", EXEC_MODE, NULL); // py_exec("print(_e_1.entity_id)", "__main__", EXEC_MODE, NULL); - py_smartexec("_._start()", NULL, self); + // py_smartexec("_._start()", NULL, self); // py_Ref func = py_retval(); // const char* typeName = py_tpname(func->type); // py_Type funcType = func->type; diff --git a/engine/src/main.c b/engine/src/main.c index 1bfc6cd2f..6e9a1cc2e 100644 --- a/engine/src/main.c +++ b/engine/src/main.c @@ -20,7 +20,12 @@ int main(int argv, char** args) { py_initialize(); - py_exec("class A:\n def start(self): print('start')\n\na = A()", "main.py", EXEC_MODE, NULL); + py_Ref aModule = py_newmodule("a"); + + py_exec("class A:\n def start(self): print('start')\n", "a.py", EXEC_MODE, aModule); + + py_exec("from a import A\n\na = A()", "main.py", EXEC_MODE, NULL); + py_Ref self = py_getglobal(py_name("a")); if (py_getattr(self, py_name("start"))) { py_push(py_retval()); From cd412c74ac22fd7910cde27dbb8cbbb33985531c Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Thu, 26 Sep 2024 21:52:02 -0400 Subject: [PATCH 09/54] Test. --- .../python/pocketpy/pkpy_script_context.c | 51 +++---------------- .../scripting/python/pocketpy/pkpy_util.c | 3 +- engine/src/main.c | 34 +++---------- 3 files changed, 14 insertions(+), 74 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index c549618d6..3f1d71850 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -15,19 +15,13 @@ //--- Entity Instance Cache ---// typedef struct CreEntityInstanceCache { - py_Name name; py_Ref instances[SKA_MAX_ENTITIES]; } CreEntityInstanceCache; static CreEntityInstanceCache entityInstanceCache = {0}; static char entityCacheStringBuffer[48]; -static void instance_cache_init() { - // entityInstanceCache.name = py_name("_inst_cache"); - // py_Ref cacheDict = py_retval(); - // py_newdict(cacheDict); - // py_setglobal(entityInstanceCache.name, cacheDict); -} +static void instance_cache_init() {} static void instance_cache_finalize() { memset(entityInstanceCache.instances, 0, sizeof(entityInstanceCache.instances)); @@ -39,14 +33,11 @@ static py_Ref instance_cache_add(SkaEntity entity,const char* classPath, const c ska_logger_warn("Attempting to add entity '%u' from '%s.%s'!", entity, classPath, className); return entityInstanceCache.instances[entity]; } - const bool wasImportSuccessful = py_import(classPath) == 1; - SKA_ASSERT(wasImportSuccessful); - py_Ref module = py_getmodule(classPath); - SKA_ASSERT_FMT(module, "module '%s' not found!", classPath); - - // snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "%s.%s(%u)", classPath, className, entity); - // py_eval(entityCacheStringBuffer, NULL); - // snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u = %s.%s(%u)", entity, classPath, className, entity); + // const bool wasImportSuccessful = py_import(classPath) == 1; + // SKA_ASSERT(wasImportSuccessful); + // py_Ref module = py_getmodule(classPath); + // SKA_ASSERT_FMT(module, "module '%s' not found!", classPath); + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "from %s import %s", classPath, className); py_exec(entityCacheStringBuffer, "main.py", EXEC_MODE, NULL); PY_ASSERT_NO_EXC(); @@ -54,31 +45,11 @@ static py_Ref instance_cache_add(SkaEntity entity,const char* classPath, const c py_exec(entityCacheStringBuffer, "main.py", EXEC_MODE, NULL); PY_ASSERT_NO_EXC(); - // py_Type instType = py_gettype(classPath, py_name(className)); - // SKA_ASSERT(instType); - // py_Ref instanceRef = py_retval(); - // py_newobject(instanceRef, instType, 1, 0); - // py_Ref pyEntity = py_pushtmp(); - // py_newint(pyEntity, entity); - // py_setslot(instanceRef, 0, pyEntity); - snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); - // py_Ref instanceRef = py_peek(-1); py_Ref instanceRef = py_getglobal(py_name(entityCacheStringBuffer)); - // py_pop(); - // static char callInstanceBuffer[96]; - // sprintf_s(callInstanceBuffer, sizeof(callInstanceBuffer), "%s(%d)", className, entity); - // py_eval(callInstanceBuffer, module); - // py_Ref instanceRef = py_retval(); SKA_ASSERT_FMT(instanceRef, "Unable to create instance from '%s.%s'!", classPath, className); entityInstanceCache.instances[entity] = instanceRef; - // snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); - // py_setglobal(py_name(entityCacheStringBuffer), instanceRef); - // py_Ref cacheDict = py_getglobal(entityInstanceCache.name); - // SKA_ASSERT(py_isdict(cacheDict)); - // py_setdict(cacheDict, py_name(entityCacheStringBuffer), instanceRef); - // py_pop(); return instanceRef; } @@ -86,16 +57,12 @@ static py_Ref instance_cache_add(SkaEntity entity,const char* classPath, const c static void instance_cache_remove(SkaEntity entity) { if (entityInstanceCache.instances[entity]) { snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); - // py_Ref cacheDict = py_getglobal(entityInstanceCache.name); - // SKA_ASSERT(cacheDict); - // py_deldict(cacheDict, py_name(entityCacheStringBuffer)); py_setglobal(py_name(entityCacheStringBuffer), py_None); entityInstanceCache.instances[entity] = NULL; } } static py_Ref instance_cache_get(SkaEntity entity) { - // return entityInstanceCache.instances[entity]; snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); return py_getglobal(py_name(entityCacheStringBuffer)); } @@ -103,12 +70,6 @@ static py_Ref instance_cache_get(SkaEntity entity) { static py_Ref instance_cache_get_checked(SkaEntity entity) { SKA_ASSERT(entityInstanceCache.instances[entity]); return instance_cache_get(entity); - // py_Ref cacheDict = py_getglobal(entityInstanceCache.name); - // SKA_ASSERT(py_isdict(cacheDict)); - // py_dict_getitem_by_str(cacheDict, entityCacheStringBuffer); - // return entityInstanceCache.instances[entity]; - - // return py_retval(); } static CREScriptContext* scriptContext = NULL; diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_util.c b/engine/src/core/scripting/python/pocketpy/pkpy_util.c index c31cdca83..a586e351f 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_util.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_util.c @@ -14,6 +14,5 @@ void cre_pkpy_util_create_module(CrePPModule* module) { void cre_pkpy_util_create_from_string(const char* moduleName, const char* moduleSource) { py_Ref newModule = py_newmodule(moduleName); SKA_ASSERT(newModule); - // py_exec(moduleSource, moduleName, EXEC_MODE, newModule); - py_exec(moduleSource, moduleName, EXEC_MODE, NULL); + py_exec(moduleSource, moduleName, EXEC_MODE, newModule); } diff --git a/engine/src/main.c b/engine/src/main.c index 6e9a1cc2e..08528bce3 100644 --- a/engine/src/main.c +++ b/engine/src/main.c @@ -1,38 +1,18 @@ #include -// #include - -#include +#include #include "core/core.h" int main(int argv, char** args) { - // if (!cre_initialize(argv, args)) { - // return EXIT_FAILURE; - // } - // - // while (cre_is_running()) { - // cre_update(); - // } - // - // return cre_shutdown(); - - py_initialize(); - - py_Ref aModule = py_newmodule("a"); - - py_exec("class A:\n def start(self): print('start')\n", "a.py", EXEC_MODE, aModule); - - py_exec("from a import A\n\na = A()", "main.py", EXEC_MODE, NULL); + if (!cre_initialize(argv, args)) { + return EXIT_FAILURE; + } - py_Ref self = py_getglobal(py_name("a")); - if (py_getattr(self, py_name("start"))) { - py_push(py_retval()); - py_pushnil(); - py_vectorcall(0, 0); + while (cre_is_running()) { + cre_update(); } - py_finalize(); - return EXIT_SUCCESS; + return cre_shutdown(); } From 62ffda0ec26901557669c4b6aaac2caa3731286b Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Thu, 26 Sep 2024 22:04:47 -0400 Subject: [PATCH 10/54] Clean up> --- .../python/pocketpy/pkpy_script_context.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index 3f1d71850..8ee53ef48 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -47,6 +47,7 @@ static py_Ref instance_cache_add(SkaEntity entity,const char* classPath, const c snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); py_Ref instanceRef = py_getglobal(py_name(entityCacheStringBuffer)); + PY_ASSERT_NO_EXC(); SKA_ASSERT_FMT(instanceRef, "Unable to create instance from '%s.%s'!", classPath, className); entityInstanceCache.instances[entity] = instanceRef; @@ -58,13 +59,16 @@ static void instance_cache_remove(SkaEntity entity) { if (entityInstanceCache.instances[entity]) { snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); py_setglobal(py_name(entityCacheStringBuffer), py_None); + PY_ASSERT_NO_EXC(); entityInstanceCache.instances[entity] = NULL; } } static py_Ref instance_cache_get(SkaEntity entity) { snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); - return py_getglobal(py_name(entityCacheStringBuffer)); + py_Ref instance = py_getglobal(py_name(entityCacheStringBuffer)); + PY_ASSERT_NO_EXC(); + return instance; } static py_Ref instance_cache_get_checked(SkaEntity entity) { @@ -145,16 +149,9 @@ void pkpy_delete_instance(SkaEntity entity) { void pkpy_on_start(SkaEntity entity) { py_Ref self = instance_cache_get_checked(entity); if (py_getattr(self, startFunctionName)) { - py_exec("_e_1._start()", "main.py", EXEC_MODE, NULL); - // py_exec("print(_e_1.entity_id)", "__main__", EXEC_MODE, NULL); - // py_smartexec("_._start()", NULL, self); - // py_Ref func = py_retval(); - // const char* typeName = py_tpname(func->type); - // py_Type funcType = func->type; - // py_pushmethod(startFunctionName); - // py_push(func); - // py_pushnil(); - // py_vectorcall(0, 0); + py_push(py_retval()); + py_pushnil(); + py_vectorcall(0, 0); PY_ASSERT_NO_EXC(); } else { py_clearexc(NULL); From b2105cdcf5ec99a35970608d19065b8aa5392d8b Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Thu, 26 Sep 2024 22:15:02 -0400 Subject: [PATCH 11/54] Clean up. --- .../python/pocketpy/pkpy_script_context.c | 30 +++++++++++-------- .../scripting/python/pocketpy/pkpy_util.c | 1 - 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index 8ee53ef48..094996795 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -33,10 +33,6 @@ static py_Ref instance_cache_add(SkaEntity entity,const char* classPath, const c ska_logger_warn("Attempting to add entity '%u' from '%s.%s'!", entity, classPath, className); return entityInstanceCache.instances[entity]; } - // const bool wasImportSuccessful = py_import(classPath) == 1; - // SKA_ASSERT(wasImportSuccessful); - // py_Ref module = py_getmodule(classPath); - // SKA_ASSERT_FMT(module, "module '%s' not found!", classPath); snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "from %s import %s", classPath, className); py_exec(entityCacheStringBuffer, "main.py", EXEC_MODE, NULL); @@ -161,34 +157,44 @@ void pkpy_on_start(SkaEntity entity) { void pkpy_on_end(SkaEntity entity) { py_Ref self = instance_cache_get_checked(entity); if (py_getattr(self, endFunctionName)) { - py_Ref func = py_retval(); - py_call(func, 1, self); + py_push(py_retval()); + py_pushnil(); + py_vectorcall(0, 0); + PY_ASSERT_NO_EXC(); + } else { + py_clearexc(NULL); } } void pkpy_on_update(SkaEntity entity, f32 deltaTime) { py_Ref self = instance_cache_get_checked(entity); if (py_getattr(self, processFunctionName)) { - py_Ref func = py_retval(); py_Ref pyDeltaTime = NULL; py_newfloat(pyDeltaTime, deltaTime); - py_push(func); + + py_push(py_retval()); py_pushnil(); py_push(pyDeltaTime); - py_vectorcall(2, 0); + py_vectorcall(1, 0); + PY_ASSERT_NO_EXC(); + } else { + py_clearexc(NULL); } } void pkpy_on_fixed_update(SkaEntity entity, f32 deltaTime) { py_Ref self = instance_cache_get_checked(entity); if (py_getattr(self, fixedProcessFunctionName)) { - py_Ref func = py_retval(); py_Ref pyDeltaTime = NULL; py_newfloat(pyDeltaTime, deltaTime); - py_push(func); + + py_push(py_retval()); py_pushnil(); py_push(pyDeltaTime); - py_vectorcall(2, 0); + py_vectorcall(1, 0); + PY_ASSERT_NO_EXC(); + } else { + py_clearexc(NULL); } } diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_util.c b/engine/src/core/scripting/python/pocketpy/pkpy_util.c index a586e351f..a1a777568 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_util.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_util.c @@ -7,7 +7,6 @@ void cre_pkpy_util_create_module(CrePPModule* module) { const CrePPFunction* func = &module->functions[i]; if (!func->signature || !func->function) { break; } py_bind(newModule, func->signature, func->function); - // py_bindfunc(newModule, func->signature, func->function); } } From 20119653fa81a0b8f5d6ca6a9d79b6e32ca16180 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Fri, 27 Sep 2024 23:21:35 -0400 Subject: [PATCH 12/54] Starting to implement python api. --- engine/CMakeLists.txt | 1 + .../python/pocketpy/api/pkpy_api_impl.c | 181 ++++++++++++++++-- 2 files changed, 163 insertions(+), 19 deletions(-) diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 689755616..434e2a0c0 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -17,6 +17,7 @@ endif () file(GLOB_RECURSE CRESCENT_SRC src/core/*.c src/core/*.h) add_library(${PROJECT_NAME} STATIC ${CRESCENT_SRC}) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src) if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") target_link_libraries(${PROJECT_NAME} PUBLIC Ws2_32 seika cJSON pocketpy) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index b1e778edf..cc6346948 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -1,25 +1,168 @@ #include "pkpy_api_impl.h" +#include +#include +#include + // Shader Instance -bool cre_pkpy_api_shader_instance_delete(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_create_bool_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_set_bool_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_get_bool_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_create_int_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_set_int_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_get_int_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_create_float_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_set_float_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_get_float_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_create_float2_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_set_float2_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_get_float2_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_create_float3_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_set_float3_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_get_float3_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_create_float4_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_set_float4_param(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_instance_get_float4_param(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_instance_delete(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 pyShaderId = py_toint(py_arg(0)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + bool hasDeletedInstance = false; + if (shaderInstance) { + ska_shader_cache_remove_instance(shaderId); + ska_shader_instance_destroy(shaderInstance); + } + py_newbool(py_retval(), hasDeletedInstance); + return true; +} + +bool cre_pkpy_api_shader_instance_create_bool_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const bool value = py_tobool(py_arg(2)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + ska_shader_instance_param_create_bool(shaderInstance, paramName, value); + return true; +} + +bool cre_pkpy_api_shader_instance_set_bool_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const bool value = py_tobool(py_arg(2)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + ska_shader_instance_param_update_bool(shaderInstance, paramName, value); + return true; +} + +bool cre_pkpy_api_shader_instance_get_bool_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const bool value = ska_shader_instance_param_get_bool(shaderInstance, paramName); + py_newbool(py_retval(), value); + return true; +} + +bool cre_pkpy_api_shader_instance_create_int_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const py_i64 value = py_toint(py_arg(2)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + ska_shader_instance_param_create_int(shaderInstance, paramName, (int32)value); + return true; +} + +bool cre_pkpy_api_shader_instance_set_int_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const py_i64 value = py_toint(py_arg(2)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + ska_shader_instance_param_update_int(shaderInstance, paramName, (int32)value); + return true; +} + +bool cre_pkpy_api_shader_instance_get_int_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const int32 value = ska_shader_instance_param_get_int(shaderInstance, paramName); + py_newint(py_retval(), value); + return true; +} + +bool cre_pkpy_api_shader_instance_create_float_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const f64 value = py_tofloat(py_arg(2)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + ska_shader_instance_param_create_float(shaderInstance, paramName, (f32)value); + return true; +} + +bool cre_pkpy_api_shader_instance_set_float_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const f64 value = py_tofloat(py_arg(2)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + ska_shader_instance_param_update_float(shaderInstance, paramName, (f32)value); + return true; +} + +bool cre_pkpy_api_shader_instance_get_float_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const f32 value = ska_shader_instance_param_get_float(shaderInstance, paramName); + py_newfloat(py_retval(), value); + return true; +} + +bool cre_pkpy_api_shader_instance_create_float2_param(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_shader_instance_set_float2_param(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_shader_instance_get_float2_param(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_shader_instance_create_float3_param(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_shader_instance_set_float3_param(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_shader_instance_get_float3_param(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_shader_instance_create_float4_param(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_shader_instance_set_float4_param(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_shader_instance_get_float4_param(int argc, py_StackRef argv) { + return true; +} // Shader Util bool cre_pkpy_api_shader_util_compile_shader(int argc, py_StackRef argv) { return true; } From 73feba056e8c5b9c4d95e3f0323fdccbfe434768 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sat, 28 Sep 2024 10:00:07 -0400 Subject: [PATCH 13/54] Adding pkpy tuple example. --- .../python/pocketpy/api/pkpy_api_impl.c | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index cc6346948..24137eb54 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -129,14 +129,46 @@ bool cre_pkpy_api_shader_instance_get_float_param(int argc, py_StackRef argv) { } bool cre_pkpy_api_shader_instance_create_float2_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(4); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const f64 valueX = py_tofloat(py_arg(2)); + const f64 valueY = py_tofloat(py_arg(3)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const SkaVector2 vecValue = (SkaVector2){ .x = (f32)valueX, .y = (f32)valueY }; + ska_shader_instance_param_create_float2(shaderInstance, paramName, vecValue); return true; } bool cre_pkpy_api_shader_instance_set_float2_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(4); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const f64 valueX = py_tofloat(py_arg(2)); + const f64 valueY = py_tofloat(py_arg(3)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const SkaVector2 vecValue = (SkaVector2){ .x = (f32)valueX, .y = (f32)valueY }; + ska_shader_instance_param_update_float2(shaderInstance, paramName, vecValue); return true; } bool cre_pkpy_api_shader_instance_get_float2_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const SkaVector2 value = ska_shader_instance_param_get_float2(shaderInstance, paramName); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, value.x); + py_newfloat(pyY, value.y); return true; } From 4e0c13468168780be7985ac3a734b0297287cd40 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sat, 28 Sep 2024 10:27:27 -0400 Subject: [PATCH 14/54] Finished untested shader api. --- .../python/pocketpy/api/pkpy_api_impl.c | 130 +++++++++++++++++- 1 file changed, 125 insertions(+), 5 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 24137eb54..0e704b551 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -1,9 +1,12 @@ #include "pkpy_api_impl.h" +#include +#include #include #include #include + // Shader Instance bool cre_pkpy_api_shader_instance_delete(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); @@ -173,35 +176,152 @@ bool cre_pkpy_api_shader_instance_get_float2_param(int argc, py_StackRef argv) { } bool cre_pkpy_api_shader_instance_create_float3_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const f64 valueX = py_tofloat(py_arg(2)); + const f64 valueY = py_tofloat(py_arg(3)); + const f64 valueZ = py_tofloat(py_arg(4)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const SkaVector3 vecValue = (SkaVector3){ .x = (f32)valueX, .y = (f32)valueY, .z = (f32)valueZ }; + ska_shader_instance_param_create_float3(shaderInstance, paramName, vecValue); return true; } bool cre_pkpy_api_shader_instance_set_float3_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const f64 valueX = py_tofloat(py_arg(2)); + const f64 valueY = py_tofloat(py_arg(3)); + const f64 valueZ = py_tofloat(py_arg(4)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const SkaVector3 vecValue = (SkaVector3){ .x = (f32)valueX, .y = (f32)valueY, .z = (f32)valueZ }; + ska_shader_instance_param_update_float3(shaderInstance, paramName, vecValue); return true; } bool cre_pkpy_api_shader_instance_get_float3_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const SkaVector3 value = ska_shader_instance_param_get_float3(shaderInstance, paramName); + py_newtuple(py_retval(), 3); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_Ref pyZ = py_tuple_getitem(py_retval(), 2); + py_newfloat(pyX, value.x); + py_newfloat(pyY, value.y); + py_newfloat(pyZ, value.z); return true; } bool cre_pkpy_api_shader_instance_create_float4_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(6); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const f64 valueX = py_tofloat(py_arg(2)); + const f64 valueY = py_tofloat(py_arg(3)); + const f64 valueZ = py_tofloat(py_arg(4)); + const f64 valueW = py_tofloat(py_arg(5)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const SkaVector4 vecValue = (SkaVector4){ .x = (f32)valueX, .y = (f32)valueY, .z = (f32)valueZ, .w = (f32)valueW }; + ska_shader_instance_param_create_float4(shaderInstance, paramName, vecValue); return true; } bool cre_pkpy_api_shader_instance_set_float4_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(6); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + const f64 valueX = py_tofloat(py_arg(2)); + const f64 valueY = py_tofloat(py_arg(3)); + const f64 valueZ = py_tofloat(py_arg(4)); + const f64 valueW = py_tofloat(py_arg(5)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const SkaVector4 vecValue = (SkaVector4){ .x = (f32)valueX, .y = (f32)valueY, .z = (f32)valueZ, .w = (f32)valueW }; + ska_shader_instance_param_update_float4(shaderInstance, paramName, vecValue); return true; } bool cre_pkpy_api_shader_instance_get_float4_param(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const py_i64 pyShaderId = py_toint(py_arg(0)); + const char* paramName = py_tostr(py_arg(1)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + const SkaVector4 value = ska_shader_instance_param_get_float4(shaderInstance, paramName); + py_newtuple(py_retval(), 4); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_Ref pyZ = py_tuple_getitem(py_retval(), 2); + py_Ref pyW = py_tuple_getitem(py_retval(), 3); + py_newfloat(pyX, value.x); + py_newfloat(pyY, value.y); + py_newfloat(pyZ, value.z); + py_newfloat(pyW, value.w); return true; } // Shader Util -bool cre_pkpy_api_shader_util_compile_shader(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_util_compile_shader_raw(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_util_set_screen_shader(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_util_get_current_screen_shader(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_shader_util_reset_screen_shader_to_default(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_shader_util_compile_shader(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const char* shaderPath = py_tostr(py_arg(0)); + + const SkaShaderInstanceId newId = ska_shader_cache_create_instance_and_add(shaderPath); + SKA_ASSERT_FMT(newId != SKA_SHADER_INSTANCE_INVALID_ID, "Invalid shader id reading from path '%s'", shaderPath); + py_newint(py_retval(), newId); + return true; +} + +bool cre_pkpy_api_shader_util_compile_shader_raw(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const char* vertexPath = py_tostr(py_arg(0)); + const char* fragmentPath = py_tostr(py_arg(1)); + + const SkaShaderInstanceId newId = ska_shader_cache_create_instance_and_add_from_raw(vertexPath, fragmentPath); + SKA_ASSERT_FMT(newId != SKA_SHADER_INSTANCE_INVALID_ID, "Invalid shader id reading from paths: vertex = '%s', fragment = '%s'", vertexPath, fragmentPath); + py_newint(py_retval(), newId); + return true; +} + +bool cre_pkpy_api_shader_util_set_screen_shader(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 pyShaderId = py_toint(py_arg(0)); + + const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); + bool hasSetShaderInstance = false; + if (shaderInstance) { + ska_frame_buffer_set_screen_shader(shaderInstance); + hasSetShaderInstance = true; + } + py_newbool(py_retval(), hasSetShaderInstance); + return true; +} + +// TODO: Keep track of current screen shader +bool cre_pkpy_api_shader_util_get_current_screen_shader(int argc, py_StackRef argv) { + py_newint(py_retval(), 0); + return true; +} + +bool cre_pkpy_api_shader_util_reset_screen_shader_to_default(int argc, py_StackRef argv) { + ska_frame_buffer_reset_to_default_screen_shader(); + return true; +} // Engine bool cre_pkpy_api_engine_exit(int argc, py_StackRef argv) { return true; } From c033891a259f4f02b69d49e6cac0f2e2228df229 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 29 Sep 2024 00:20:40 -0400 Subject: [PATCH 15/54] More api. --- .../python/pocketpy/api/pkpy_api_impl.c | 55 +++++++++++++++++-- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 0e704b551..194abd7c3 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -6,6 +6,9 @@ #include #include +#include "core/engine_context.h" +#include "core/ecs/ecs_manager.h" + // Shader Instance bool cre_pkpy_api_shader_instance_delete(int argc, py_StackRef argv) { @@ -324,12 +327,52 @@ bool cre_pkpy_api_shader_util_reset_screen_shader_to_default(int argc, py_StackR } // Engine -bool cre_pkpy_api_engine_exit(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_engine_set_target_fps(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_engine_get_target_fps(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_engine_get_average_fps(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_engine_set_fps_display_enabled(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_engine_get_global_physics_delta_time(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_engine_exit(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 pyExitCode = py_toint(py_arg(0)); + + CREEngineContext* engineContext = cre_engine_context_get(); + engineContext->isRunning = false; + engineContext->exitCode = (int32)pyExitCode; + return true; +} + +bool cre_pkpy_api_engine_set_target_fps(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 pyTargetFPS = py_toint(py_arg(0)); + + CREEngineContext* engineContext = cre_engine_context_get(); + engineContext->targetFPS = (int32)pyTargetFPS; + return true; +} + +bool cre_pkpy_api_engine_get_target_fps(int argc, py_StackRef argv) { + CREEngineContext* engineContext = cre_engine_context_get(); + py_newint(py_retval(), engineContext->targetFPS); + return true; +} + +bool cre_pkpy_api_engine_get_average_fps(int argc, py_StackRef argv) { + CREEngineContext* engineContext = cre_engine_context_get(); + py_newint(py_retval(), (py_i64)engineContext->stats.averageFPS); + return true; +} + +bool cre_pkpy_api_engine_set_fps_display_enabled(int argc, py_StackRef argv) { + PY_CHECK_ARGC(4); + const bool isEnabled = py_tobool(py_arg(0)); + const char* fontUID = py_tostr(py_arg(1)); + const double pyPosX = py_tofloat(py_arg(2)); + const double pyPosY = py_tofloat(py_arg(3)); + + cre_ecs_manager_enable_fps_display_entity(isEnabled, fontUID, (f32)pyPosX, (f32)pyPosY); + return true; +} + +bool cre_pkpy_api_engine_get_global_physics_delta_time(int argc, py_StackRef argv) { + py_newfloat(py_retval(), CRE_GLOBAL_PHYSICS_DELTA_TIME); + return true; +} // Input bool cre_pkpy_api_input_is_key_pressed(int argc, py_StackRef argv) { return true; } From be7b3f3b2dddd95e21e6f53f0e3dd1fede69e297 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 29 Sep 2024 00:39:38 -0400 Subject: [PATCH 16/54] Clean. --- .../python/pocketpy/api/pkpy_api_impl.c | 153 ++++++++++++++++-- 1 file changed, 140 insertions(+), 13 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 194abd7c3..d06edb571 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -1,15 +1,37 @@ #include "pkpy_api_impl.h" #include +#include +#include #include #include #include #include #include "core/engine_context.h" +#include "core/game_properties.h" +#include "core/camera/camera.h" +#include "core/camera/camera_manager.h" #include "core/ecs/ecs_manager.h" +// Helper functions +static inline SkaVector2 cre_pkpy_api_helper_mouse_get_global_position(const SkaVector2* offset) { + SkaMouse* globalMouse = ska_input_get_mouse(); + const CRECamera2D* camera = cre_camera_manager_get_current_camera(); + CREGameProperties* gameProps = cre_game_props_get(); + SkaRenderContext* renderContext = ska_render_context_get(); + const SkaVector2 mouse_pixel_coord = { + ska_math_map_to_range(globalMouse->position.x, 0.0f, (float) renderContext->windowWidth, 0.0f, (float) gameProps->resolutionWidth), + ska_math_map_to_range(globalMouse->position.y, 0.0f, (float) renderContext->windowHeight, 0.0f, (float) gameProps->resolutionHeight) +}; + const SkaVector2 mouseWorldPos = { + (camera->viewport.x + camera->offset.x + mouse_pixel_coord.x + offset->x) * camera->zoom.x, + (camera->viewport.y + camera->offset.y + mouse_pixel_coord.y + offset->y) * camera->zoom.y +}; + return mouseWorldPos; +} + // Shader Instance bool cre_pkpy_api_shader_instance_delete(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); @@ -362,8 +384,8 @@ bool cre_pkpy_api_engine_set_fps_display_enabled(int argc, py_StackRef argv) { PY_CHECK_ARGC(4); const bool isEnabled = py_tobool(py_arg(0)); const char* fontUID = py_tostr(py_arg(1)); - const double pyPosX = py_tofloat(py_arg(2)); - const double pyPosY = py_tofloat(py_arg(3)); + const py_f64 pyPosX = py_tofloat(py_arg(2)); + const py_f64 pyPosY = py_tofloat(py_arg(3)); cre_ecs_manager_enable_fps_display_entity(isEnabled, fontUID, (f32)pyPosX, (f32)pyPosY); return true; @@ -375,17 +397,122 @@ bool cre_pkpy_api_engine_get_global_physics_delta_time(int argc, py_StackRef arg } // Input -bool cre_pkpy_api_input_is_key_pressed(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_input_is_key_just_pressed(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_input_is_key_just_released(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_input_add_action(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_input_is_action_pressed(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_input_is_action_just_pressed(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_input_is_action_just_released(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_input_start_gamepad_vibration(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_input_stop_gamepad_vibration(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_input_mouse_get_position(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_input_mouse_get_world_position(int argc, py_StackRef argv) { return true; } + +bool cre_pkpy_api_input_is_key_pressed(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 pyKey = py_toint(py_arg(0)); + + const bool isPressed = ska_input_is_key_pressed((SkaInputKey)pyKey, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX); + py_newbool(py_retval(), isPressed); + return true; +} + +bool cre_pkpy_api_input_is_key_just_pressed(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 pyKey = py_toint(py_arg(0)); + + const bool isPressed = ska_input_is_key_just_pressed((SkaInputKey)pyKey, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX); + py_newbool(py_retval(), isPressed); + return true; +} + +bool cre_pkpy_api_input_is_key_just_released(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 pyKey = py_toint(py_arg(0)); + + const bool isPressed = ska_input_is_key_just_released((SkaInputKey)pyKey, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX); + py_newbool(py_retval(), isPressed); + return true; +} + +bool cre_pkpy_api_input_add_action(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + const char* actionName = py_tostr(py_arg(0)); + const py_i64 pyValueKey = py_toint(py_arg(1)); + const py_i64 pyDeviceId = py_toint(py_arg(2)); + + ska_input_add_input_action( + actionName, + (SkaInputActionValue[]){ { .key = (SkaInputKey)pyValueKey, .strengthThreshold = 0.5f }, { SkaInputKey_INVALID } }, + (SkaInputDeviceIndex)pyDeviceId + ); + return true; +} + +bool cre_pkpy_api_input_is_action_pressed(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const char* actionName = py_tostr(py_arg(0)); + + // TODO: Probably should take device index as a param + const SkaInputDeviceIndex deviceIndex = SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX; + const SkaInputActionHandle handle = ska_input_find_input_action_handle(actionName, deviceIndex); + const bool isPressed = handle != SKA_INPUT_INVALID_INPUT_ACTION_HANDLE ? ska_input_is_input_action_pressed(handle, deviceIndex) : false; + py_newbool(py_retval(), isPressed); + return true; +} + +bool cre_pkpy_api_input_is_action_just_pressed(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const char* actionName = py_tostr(py_arg(0)); + + const SkaInputDeviceIndex deviceIndex = SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX; + const SkaInputActionHandle handle = ska_input_find_input_action_handle(actionName, deviceIndex); + const bool isPressed = handle != SKA_INPUT_INVALID_INPUT_ACTION_HANDLE ? ska_input_is_input_action_just_pressed(handle, deviceIndex) : false; + py_newbool(py_retval(), isPressed); + return true; +} + +bool cre_pkpy_api_input_is_action_just_released(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const char* actionName = py_tostr(py_arg(0)); + + const SkaInputDeviceIndex deviceIndex = SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX; + const SkaInputActionHandle handle = ska_input_find_input_action_handle(actionName, deviceIndex); + const bool isReleased = handle != SKA_INPUT_INVALID_INPUT_ACTION_HANDLE ? ska_input_is_input_action_just_released(handle, deviceIndex) : false; + py_newbool(py_retval(), isReleased); + return true; +} + +// TODO: Finish implementing +bool cre_pkpy_api_input_start_gamepad_vibration(int argc, py_StackRef argv) { + PY_CHECK_ARGC(4); + const py_i64 pyDeviceId = py_toint(py_arg(0)); + const py_f64 pyWeakMagnitude = py_tofloat(py_arg(1)); + const py_f64 pyStrongMagnitude = py_tofloat(py_arg(2)); + const py_f64 pyDurationSeconds = py_tofloat(py_arg(3)); + + // ska_input_gamepad_start_vibration((int32)pyDeviceId, (f32)pyWeakMagnitude, (f32)pyStrongMagnitude, (f32)pyDurationSeconds); + return true; +} + +// TODO: Finish implementing +bool cre_pkpy_api_input_stop_gamepad_vibration(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 pyDeviceId = py_toint(py_arg(0)); + + // ska_input_gamepad_stop_vibration((SkaInputDeviceIndex)pyDeviceId); + return true; +} + +bool cre_pkpy_api_input_mouse_get_position(int argc, py_StackRef argv) { + const SkaMouse* globalMouse = ska_input_get_mouse(); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, (py_f64)globalMouse->position.x); + py_newfloat(pyY, (py_f64)globalMouse->position.y); + return true; +} + +bool cre_pkpy_api_input_mouse_get_world_position(int argc, py_StackRef argv) { + const SkaVector2 mouseWorldPosition = cre_pkpy_api_helper_mouse_get_global_position(&SKA_VECTOR2_ZERO); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, (py_f64)mouseWorldPosition.x); + py_newfloat(pyY, (py_f64)mouseWorldPosition.y); + return true; +} // Scene Tree bool cre_pkpy_api_scene_tree_change_scene(int argc, py_StackRef argv) { return true; } From c1d779c5838b391fcc30deb2fe1759bcd7187b40 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Mon, 30 Sep 2024 21:57:27 -0400 Subject: [PATCH 17/54] Adding separate file for instance cache. --- .../python/pocketpy/api/pkpy_api_impl.c | 18 ++++- .../python/pocketpy/pkpy_instance_cache.c | 63 +++++++++++++++ .../python/pocketpy/pkpy_instance_cache.h | 12 +++ .../python/pocketpy/pkpy_script_context.c | 81 +++---------------- .../scripting/python/pocketpy/pkpy_util.h | 3 +- 5 files changed, 102 insertions(+), 75 deletions(-) create mode 100644 engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c create mode 100644 engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.h diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index d06edb571..5c76f9b48 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -13,6 +13,7 @@ #include "core/camera/camera.h" #include "core/camera/camera_manager.h" #include "core/ecs/ecs_manager.h" +#include "core/scene/scene_manager.h" // Helper functions @@ -515,8 +516,21 @@ bool cre_pkpy_api_input_mouse_get_world_position(int argc, py_StackRef argv) { } // Scene Tree -bool cre_pkpy_api_scene_tree_change_scene(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_scene_tree_get_root(int argc, py_StackRef argv) { return true; } + +bool cre_pkpy_api_scene_tree_change_scene(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const char* scenePath = py_tostr(py_arg(0)); + + cre_scene_manager_queue_scene_change(scenePath); + return true; +} + +bool cre_pkpy_api_scene_tree_get_root(int argc, py_StackRef argv) { + SceneTreeNode* rootNode = cre_scene_manager_get_active_scene_root(); + SKA_ASSERT(rootNode != NULL); + + return true; +} // Scene Manager bool cre_pkpy_api_scene_manager_process_queued_creation_entities(int argc, py_StackRef argv) { return true; } diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c b/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c new file mode 100644 index 000000000..5ff2601d2 --- /dev/null +++ b/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c @@ -0,0 +1,63 @@ +#include "pkpy_instance_cache.h" + +#include + +#include "pkpy_util.h" + +typedef struct CreEntityInstanceCache { + py_Ref instances[SKA_MAX_ENTITIES]; +} CreEntityInstanceCache; + +static CreEntityInstanceCache entityInstanceCache = {0}; +static char entityCacheStringBuffer[48]; + +void cre_pkpy_instance_cache_init() {} + +void cre_pkpy_instance_cache_finalize() { + memset(entityInstanceCache.instances, 0, sizeof(entityInstanceCache.instances)); +} + +py_Ref cre_pkpy_instance_cache_add(SkaEntity entity,const char* classPath, const char* className) { + // Early out if entity already exists + if (entityInstanceCache.instances[entity] != NULL) { + ska_logger_warn("Attempting to add entity '%u' from '%s.%s'!", entity, classPath, className); + return entityInstanceCache.instances[entity]; + } + + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "from %s import %s", classPath, className); + py_exec(entityCacheStringBuffer, "main.py", EXEC_MODE, NULL); + PY_ASSERT_NO_EXC(); + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u = %s(%u)", entity, className, entity); + py_exec(entityCacheStringBuffer, "main.py", EXEC_MODE, NULL); + PY_ASSERT_NO_EXC(); + + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); + py_Ref instanceRef = py_getglobal(py_name(entityCacheStringBuffer)); + PY_ASSERT_NO_EXC(); + + SKA_ASSERT_FMT(instanceRef, "Unable to create instance from '%s.%s'!", classPath, className); + entityInstanceCache.instances[entity] = instanceRef; + + return instanceRef; +} + +void cre_pkpy_instance_cache_remove(SkaEntity entity) { + if (entityInstanceCache.instances[entity]) { + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); + py_setglobal(py_name(entityCacheStringBuffer), py_None); + PY_ASSERT_NO_EXC(); + entityInstanceCache.instances[entity] = NULL; + } +} + +py_Ref cre_pkpy_instance_cache_get(SkaEntity entity) { + snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); + py_Ref instance = py_getglobal(py_name(entityCacheStringBuffer)); + PY_ASSERT_NO_EXC(); + return instance; +} + +py_Ref cre_pkpy_instance_cache_get_checked(SkaEntity entity) { + SKA_ASSERT(entityInstanceCache.instances[entity]); + return cre_pkpy_instance_cache_get(entity); +} diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.h b/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.h new file mode 100644 index 000000000..29c03d9b0 --- /dev/null +++ b/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#include + +void cre_pkpy_instance_cache_init(); +void cre_pkpy_instance_cache_finalize(); +py_Ref cre_pkpy_instance_cache_add(SkaEntity entity,const char* classPath, const char* className); +void cre_pkpy_instance_cache_remove(SkaEntity entity); +py_Ref cre_pkpy_instance_cache_get(SkaEntity entity); +py_Ref cre_pkpy_instance_cache_get_checked(SkaEntity entity); diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index 094996795..b47594ecd 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -1,77 +1,14 @@ #include "pkpy_script_context.h" #include -#include -#include #include #include #include "pkpy_util.h" +#include "pkpy_instance_cache.h" #include "api/pkpy_api.h" -#define PY_ASSERT_NO_EXC() SKA_ASSERT_FMT(!py_checkexc(false), "PKPY Error:\n%s", py_formatexc()); - -//--- Entity Instance Cache ---// - -typedef struct CreEntityInstanceCache { - py_Ref instances[SKA_MAX_ENTITIES]; -} CreEntityInstanceCache; - -static CreEntityInstanceCache entityInstanceCache = {0}; -static char entityCacheStringBuffer[48]; - -static void instance_cache_init() {} - -static void instance_cache_finalize() { - memset(entityInstanceCache.instances, 0, sizeof(entityInstanceCache.instances)); -} - -static py_Ref instance_cache_add(SkaEntity entity,const char* classPath, const char* className) { - // Early out if entity already exists - if (entityInstanceCache.instances[entity] != NULL) { - ska_logger_warn("Attempting to add entity '%u' from '%s.%s'!", entity, classPath, className); - return entityInstanceCache.instances[entity]; - } - - snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "from %s import %s", classPath, className); - py_exec(entityCacheStringBuffer, "main.py", EXEC_MODE, NULL); - PY_ASSERT_NO_EXC(); - snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u = %s(%u)", entity, className, entity); - py_exec(entityCacheStringBuffer, "main.py", EXEC_MODE, NULL); - PY_ASSERT_NO_EXC(); - - snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); - py_Ref instanceRef = py_getglobal(py_name(entityCacheStringBuffer)); - PY_ASSERT_NO_EXC(); - - SKA_ASSERT_FMT(instanceRef, "Unable to create instance from '%s.%s'!", classPath, className); - entityInstanceCache.instances[entity] = instanceRef; - - return instanceRef; -} - -static void instance_cache_remove(SkaEntity entity) { - if (entityInstanceCache.instances[entity]) { - snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); - py_setglobal(py_name(entityCacheStringBuffer), py_None); - PY_ASSERT_NO_EXC(); - entityInstanceCache.instances[entity] = NULL; - } -} - -static py_Ref instance_cache_get(SkaEntity entity) { - snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); - py_Ref instance = py_getglobal(py_name(entityCacheStringBuffer)); - PY_ASSERT_NO_EXC(); - return instance; -} - -static py_Ref instance_cache_get_checked(SkaEntity entity) { - SKA_ASSERT(entityInstanceCache.instances[entity]); - return instance_cache_get(entity); -} - static CREScriptContext* scriptContext = NULL; static py_Name startFunctionName; static py_Name processFunctionName; @@ -112,7 +49,7 @@ CREScriptContextTemplate cre_pkpy_get_script_context_template() { void pkpy_init(CREScriptContext* context) { py_initialize(); - instance_cache_init(); + cre_pkpy_instance_cache_init(); // Cache function names startFunctionName = py_name("_start"); processFunctionName = py_name("_process"); @@ -130,20 +67,20 @@ void pkpy_init(CREScriptContext* context) { void pkpy_finalize(CREScriptContext* context) { scriptContext = NULL; - instance_cache_finalize(); + cre_pkpy_instance_cache_finalize(); py_finalize(); } void pkpy_create_instance(SkaEntity entity, const char* classPath, const char* className) { - instance_cache_add(entity, classPath, className); + cre_pkpy_instance_cache_add(entity, classPath, className); } void pkpy_delete_instance(SkaEntity entity) { - instance_cache_remove(entity); + cre_pkpy_instance_cache_remove(entity); } void pkpy_on_start(SkaEntity entity) { - py_Ref self = instance_cache_get_checked(entity); + py_Ref self = cre_pkpy_instance_cache_get_checked(entity); if (py_getattr(self, startFunctionName)) { py_push(py_retval()); py_pushnil(); @@ -155,7 +92,7 @@ void pkpy_on_start(SkaEntity entity) { } void pkpy_on_end(SkaEntity entity) { - py_Ref self = instance_cache_get_checked(entity); + py_Ref self = cre_pkpy_instance_cache_get_checked(entity); if (py_getattr(self, endFunctionName)) { py_push(py_retval()); py_pushnil(); @@ -167,7 +104,7 @@ void pkpy_on_end(SkaEntity entity) { } void pkpy_on_update(SkaEntity entity, f32 deltaTime) { - py_Ref self = instance_cache_get_checked(entity); + py_Ref self = cre_pkpy_instance_cache_get_checked(entity); if (py_getattr(self, processFunctionName)) { py_Ref pyDeltaTime = NULL; py_newfloat(pyDeltaTime, deltaTime); @@ -183,7 +120,7 @@ void pkpy_on_update(SkaEntity entity, f32 deltaTime) { } void pkpy_on_fixed_update(SkaEntity entity, f32 deltaTime) { - py_Ref self = instance_cache_get_checked(entity); + py_Ref self = cre_pkpy_instance_cache_get_checked(entity); if (py_getattr(self, fixedProcessFunctionName)) { py_Ref pyDeltaTime = NULL; py_newfloat(pyDeltaTime, deltaTime); diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_util.h b/engine/src/core/scripting/python/pocketpy/pkpy_util.h index 4e9b6a2b5..e06459ee7 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_util.h +++ b/engine/src/core/scripting/python/pocketpy/pkpy_util.h @@ -2,11 +2,12 @@ #include -#include #include #define CRE_PKPY_MODULE_FUNCTION_LIMIT 256 +#define PY_ASSERT_NO_EXC() SKA_ASSERT_FMT(!py_checkexc(false), "PKPY Error:\n%s", py_formatexc()); + typedef struct CrePPFunction { const char* signature; py_CFunction function; From ecaa66ea7408c87dd1e4e358e9cd1e8d3ef6e5ea Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Mon, 30 Sep 2024 22:40:09 -0400 Subject: [PATCH 18/54] More pkpy api stuff. --- engine/src/core/ecs/ecs_manager.c | 4 +- .../python/pocketpy/api/pkpy_api_impl.c | 67 +++++++++++++++++-- .../python/pocketpy/pkpy_instance_cache.c | 37 +++++++++- .../python/pocketpy/pkpy_instance_cache.h | 6 +- 4 files changed, 103 insertions(+), 11 deletions(-) diff --git a/engine/src/core/ecs/ecs_manager.c b/engine/src/core/ecs/ecs_manager.c index b1fd8bb74..9ccbe09e1 100644 --- a/engine/src/core/ecs/ecs_manager.c +++ b/engine/src/core/ecs/ecs_manager.c @@ -81,8 +81,8 @@ void cre_ecs_manager_initialize() { cre_font_rendering_ec_system_create_and_register(); cre_parallax_ec_system_create_and_register(); cre_particle_ec_system_create_and_register(); - cre_script_ec_system_create_and_register(); cre_tilemap_ec_system_create_and_register(); + cre_script_ec_system_create_and_register(); } void cre_ecs_manager_initialize_ex(SkaTexture* colorRectTexture, SkaTexture* particle2DSquareTexture) { @@ -97,8 +97,8 @@ void cre_ecs_manager_initialize_ex(SkaTexture* colorRectTexture, SkaTexture* par cre_font_rendering_ec_system_create_and_register(); cre_parallax_ec_system_create_and_register(); cre_particle_ec_system_create_and_register_ex(particle2DSquareTexture); - cre_script_ec_system_create_and_register(); cre_tilemap_ec_system_create_and_register(); + cre_script_ec_system_create_and_register(); } void cre_ecs_manager_enable_fps_display_entity(bool enabled, const char* fontUID, f32 positionX, f32 positionY) { diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 5c76f9b48..f833be443 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -14,7 +14,7 @@ #include "core/camera/camera_manager.h" #include "core/ecs/ecs_manager.h" #include "core/scene/scene_manager.h" - +#include "core/scripting/python/pocketpy/pkpy_instance_cache.h" // Helper functions static inline SkaVector2 cre_pkpy_api_helper_mouse_get_global_position(const SkaVector2* offset) { @@ -33,7 +33,10 @@ static inline SkaVector2 cre_pkpy_api_helper_mouse_get_global_position(const Ska return mouseWorldPos; } +// TODO: Check parameter types to ensure the correct parameters are passed into the api functions + // Shader Instance + bool cre_pkpy_api_shader_instance_delete(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); const py_i64 pyShaderId = py_toint(py_arg(0)); @@ -302,6 +305,7 @@ bool cre_pkpy_api_shader_instance_get_float4_param(int argc, py_StackRef argv) { } // Shader Util + bool cre_pkpy_api_shader_util_compile_shader(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); const char* shaderPath = py_tostr(py_arg(0)); @@ -350,6 +354,7 @@ bool cre_pkpy_api_shader_util_reset_screen_shader_to_default(int argc, py_StackR } // Engine + bool cre_pkpy_api_engine_exit(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); const py_i64 pyExitCode = py_toint(py_arg(0)); @@ -528,18 +533,52 @@ bool cre_pkpy_api_scene_tree_change_scene(int argc, py_StackRef argv) { bool cre_pkpy_api_scene_tree_get_root(int argc, py_StackRef argv) { SceneTreeNode* rootNode = cre_scene_manager_get_active_scene_root(); SKA_ASSERT(rootNode != NULL); - + py_Ref rootInstance = cre_pkpy_instance_cache_add2(rootNode->entity); + py_assign(py_retval(), rootInstance); return true; } // Scene Manager -bool cre_pkpy_api_scene_manager_process_queued_creation_entities(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_scene_manager_process_queued_scene_change(int argc, py_StackRef argv) { return true; } + +bool cre_pkpy_api_scene_manager_process_queued_creation_entities(int argc, py_StackRef argv) { + cre_scene_manager_process_queued_creation_entities(); + return true; +} + +bool cre_pkpy_api_scene_manager_process_queued_scene_change(int argc, py_StackRef argv) { + cre_scene_manager_process_queued_scene_change(); + return true; +} // Game Properties -bool cre_pkpy_api_game_properties_get(int argc, py_StackRef argv) { return true; } + +bool cre_pkpy_api_game_properties_get(int argc, py_StackRef argv) { + const CREGameProperties* gameProps = cre_game_props_get(); + SKA_ASSERT(gameProps->gameTitle); + SKA_ASSERT(gameProps->initialScenePath); + py_newtuple(py_retval(), 8); + py_Ref pyGameTitle = py_tuple_getitem(py_retval(), 0); + py_Ref pyResolutionWidth = py_tuple_getitem(py_retval(), 1); + py_Ref pyResolutionHeight = py_tuple_getitem(py_retval(), 2); + py_Ref pyWindowWidth = py_tuple_getitem(py_retval(), 3); + py_Ref pyWindowHeight = py_tuple_getitem(py_retval(), 4); + py_Ref pyTargetFPS = py_tuple_getitem(py_retval(), 5); + py_Ref pyInitialScenePath = py_tuple_getitem(py_retval(), 6); + py_Ref pyAreColliderVisible = py_tuple_getitem(py_retval(), 7); + + py_newstr(pyGameTitle, gameProps->gameTitle); + py_newint(pyResolutionWidth, gameProps->resolutionWidth); + py_newint(pyResolutionHeight, gameProps->resolutionHeight); + py_newint(pyWindowWidth, gameProps->windowWidth); + py_newint(pyWindowHeight, gameProps->windowHeight); + py_newint(pyTargetFPS, gameProps->targetFPS); + py_newstr(pyInitialScenePath, gameProps->initialScenePath); + py_newbool(pyAreColliderVisible, gameProps->areCollidersVisible); + return true; +} // Camera2D + bool cre_pkpy_api_camera2d_set_position(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_camera2d_add_to_position(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_camera2d_get_position(int argc, py_StackRef argv) { return true; } @@ -555,34 +594,43 @@ bool cre_pkpy_api_camera2d_follow_node(int argc, py_StackRef argv) { return true bool cre_pkpy_api_camera2d_unfollow_node(int argc, py_StackRef argv) { return true; } // World + bool cre_pkpy_api_world_set_time_dilation(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_world_get_time_dilation(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_world_get_delta_time(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_world_get_variable_delta_time(int argc, py_StackRef argv) { return true; } // Audio Source + bool cre_pkpy_api_audio_source_set_pitch(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_audio_source_get_pitch(int argc, py_StackRef argv) { return true; } // Audio Manager + bool cre_pkpy_api_audio_manager_play_sound(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_audio_manager_stop_sound(int argc, py_StackRef argv) { return true; } // Game Config + bool cre_pkpy_api_game_config_save(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_game_config_load(int argc, py_StackRef argv) { return true; } // Packed Scene + bool cre_pkpy_api_packed_scene_create_instance(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_packed_scene_load(int argc, py_StackRef argv) { return true; } // Collision Handler + bool cre_pkpy_api_collision_handler_process_collisions(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_collision_handler_process_mouse_collisions(int argc, py_StackRef argv) { return true; } // Network + bool cre_pkpy_api_network_is_server(int argc, py_StackRef argv) { return true; } + // Server + bool cre_pkpy_api_server_start(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_server_stop(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_server_send(int argc, py_StackRef argv) { return true; } @@ -592,6 +640,7 @@ bool cre_pkpy_api_client_stop(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_client_send(int argc, py_StackRef argv) { return true; } // Node + bool cre_pkpy_api_node_new(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_node_get_name(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_node_add_child(int argc, py_StackRef argv) { return true; } @@ -605,6 +654,7 @@ bool cre_pkpy_api_node_get_time_dilation(int argc, py_StackRef argv) { return tr bool cre_pkpy_api_node_get_total_time_dilation(int argc, py_StackRef argv) { return true; } // Node2D + bool cre_pkpy_api_node2d_set_position(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_node2d_add_to_position(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_node2d_get_position(int argc, py_StackRef argv) { return true; } @@ -623,6 +673,7 @@ bool cre_pkpy_api_node2d_set_ignore_camera(int argc, py_StackRef argv) { return bool cre_pkpy_api_node2d_get_ignore_camera(int argc, py_StackRef argv) { return true; } // Sprite + bool cre_pkpy_api_sprite_get_texture(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_sprite_set_texture(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_sprite_get_draw_source(int argc, py_StackRef argv) { return true; } @@ -639,6 +690,7 @@ bool cre_pkpy_api_sprite_get_shader_instance(int argc, py_StackRef argv) { retur bool cre_pkpy_api_sprite_set_shader_instance(int argc, py_StackRef argv) { return true; } // Animated Sprite + bool cre_pkpy_api_animated_sprite_play(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_animated_sprite_stop(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_animated_sprite_set_current_animation_frame(int argc, py_StackRef argv) { return true; } @@ -657,6 +709,7 @@ bool cre_pkpy_api_animated_sprite_get_shader_instance(int argc, py_StackRef argv bool cre_pkpy_api_animated_sprite_set_shader_instance(int argc, py_StackRef argv) { return true; } // Text Label + bool cre_pkpy_api_text_label_get_text(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_text_label_set_text(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_text_label_get_color(int argc, py_StackRef argv) { return true; } @@ -664,22 +717,26 @@ bool cre_pkpy_api_text_label_set_color(int argc, py_StackRef argv) { return true bool cre_pkpy_api_text_label_set_font_uid(int argc, py_StackRef argv) { return true; } // Collider2D + bool cre_pkpy_api_collider2d_get_extents(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_collider2d_set_extents(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_collider2d_get_color(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_collider2d_set_color(int argc, py_StackRef argv) { return true; } // Color Rect + bool cre_pkpy_api_color_rect_get_size(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_color_rect_set_size(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_color_rect_get_color(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_color_rect_set_color(int argc, py_StackRef argv) { return true; } // Parallax + bool cre_pkpy_api_parallax_get_scroll_speed(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_parallax_set_scroll_speed(int argc, py_StackRef argv) { return true; } // Particles2D + bool cre_pkpy_api_particles2d_get_amount(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_particles2d_set_amount(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_particles2d_get_life_time(int argc, py_StackRef argv) { return true; } diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c b/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c index 5ff2601d2..b962dbce5 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c @@ -1,8 +1,14 @@ #include "pkpy_instance_cache.h" #include +#include +#include "cre_pkpy.h" #include "pkpy_util.h" +#include "core/scripting/script_context.h" +#include "core/ecs/ecs_globals.h" +#include "core/ecs/components/node_component.h" +#include "core/ecs/components/script_component.h" typedef struct CreEntityInstanceCache { py_Ref instances[SKA_MAX_ENTITIES]; @@ -19,9 +25,9 @@ void cre_pkpy_instance_cache_finalize() { py_Ref cre_pkpy_instance_cache_add(SkaEntity entity,const char* classPath, const char* className) { // Early out if entity already exists - if (entityInstanceCache.instances[entity] != NULL) { + if (cre_pkpy_instance_cache_has(entity)) { ska_logger_warn("Attempting to add entity '%u' from '%s.%s'!", entity, classPath, className); - return entityInstanceCache.instances[entity]; + return cre_pkpy_instance_cache_get(entity); } snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "from %s import %s", classPath, className); @@ -41,6 +47,27 @@ py_Ref cre_pkpy_instance_cache_add(SkaEntity entity,const char* classPath, const return instanceRef; } +py_Ref cre_pkpy_instance_cache_add2(SkaEntity entity) { + // Early out if entity already exists + if (cre_pkpy_instance_cache_has(entity)) { + const ScriptComponent* scriptComponent = (ScriptComponent*)ska_ecs_component_manager_get_component(entity, SCRIPT_COMPONENT_INDEX); + ska_logger_warn("Attempting to add entity '%u' from '%s.%s'!", entity, scriptComponent->classPath, scriptComponent->className); + return cre_pkpy_instance_cache_get(entity); + } + + const NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); + ScriptComponent* scriptComponent = (ScriptComponent*)ska_ecs_component_manager_get_component_unchecked(entity, SCRIPT_COMPONENT_INDEX); + if (!scriptComponent) { + // If an entity doesn't have a script component at this point, it should just be a plain base class from the crescent module. + // Since that is the case, just create a script component so that we can clean up within the script context when the entity + // needs to leave the instance cache. + const char* baseClassName = node_get_base_type_string(nodeComponent->type); + scriptComponent = script_component_create_ex(CRE_PKPY_MODULE_NAME_CRESCENT, baseClassName, CreScriptContextType_PYTHON); + ska_ecs_component_manager_set_component(entity, SCRIPT_COMPONENT_INDEX, scriptComponent); + } + return cre_pkpy_instance_cache_add(entity, scriptComponent->classPath, scriptComponent->className); +} + void cre_pkpy_instance_cache_remove(SkaEntity entity) { if (entityInstanceCache.instances[entity]) { snprintf(entityCacheStringBuffer, sizeof(entityCacheStringBuffer), "_e_%u", entity); @@ -58,6 +85,10 @@ py_Ref cre_pkpy_instance_cache_get(SkaEntity entity) { } py_Ref cre_pkpy_instance_cache_get_checked(SkaEntity entity) { - SKA_ASSERT(entityInstanceCache.instances[entity]); + SKA_ASSERT(cre_pkpy_instance_cache_has(entity)); return cre_pkpy_instance_cache_get(entity); } + +bool cre_pkpy_instance_cache_has(SkaEntity entity) { + return entityInstanceCache.instances[entity] != NULL; +} diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.h b/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.h index 29c03d9b0..92b064ebd 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.h +++ b/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.h @@ -6,7 +6,11 @@ void cre_pkpy_instance_cache_init(); void cre_pkpy_instance_cache_finalize(); -py_Ref cre_pkpy_instance_cache_add(SkaEntity entity,const char* classPath, const char* className); +// Creates a new instance for the entity, if the entity already exists will return the existing instance instead +py_Ref cre_pkpy_instance_cache_add(SkaEntity entity, const char* classPath, const char* className); +// Same as 'cre_pkpy_instance_cache_add' but will figure out the class info and create a script component if nonexistant +py_Ref cre_pkpy_instance_cache_add2(SkaEntity entity); void cre_pkpy_instance_cache_remove(SkaEntity entity); py_Ref cre_pkpy_instance_cache_get(SkaEntity entity); py_Ref cre_pkpy_instance_cache_get_checked(SkaEntity entity); +bool cre_pkpy_instance_cache_has(SkaEntity entity); From 63b92f4e0f3d717b2592885b9988d2f49a25db1d Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Tue, 1 Oct 2024 21:28:20 -0400 Subject: [PATCH 19/54] More api stuff. --- .../python/pocketpy/api/pkpy_api_impl.c | 151 ++++++++++++++++-- 1 file changed, 138 insertions(+), 13 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index f833be443..b39ca2122 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -579,19 +579,144 @@ bool cre_pkpy_api_game_properties_get(int argc, py_StackRef argv) { // Camera2D -bool cre_pkpy_api_camera2d_set_position(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_add_to_position(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_get_position(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_set_offset(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_add_to_offset(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_get_offset(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_set_zoom(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_add_to_zoom(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_get_zoom(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_set_boundary(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_get_boundary(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_follow_node(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_camera2d_unfollow_node(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_camera2d_set_position(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const f64 posX = py_tofloat(py_arg(0)); + const f64 posY = py_tofloat(py_arg(1)); + + CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + camera2D->viewport = (SkaVector2){ .x = (f32)posX, .y = (f32)posY }; + cre_camera2d_clamp_viewport_to_boundary(camera2D); + return true; +} + +bool cre_pkpy_api_camera2d_add_to_position(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const f64 posX = py_tofloat(py_arg(0)); + const f64 posY = py_tofloat(py_arg(1)); + + CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + camera2D->viewport = (SkaVector2){ .x = camera2D->viewport.x + (f32)posX, .y = camera2D->viewport.y + (f32)posY }; + cre_camera2d_clamp_viewport_to_boundary(camera2D); + return true; +} + +bool cre_pkpy_api_camera2d_get_position(int argc, py_StackRef argv) { + const CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, camera2D->viewport.x); + py_newfloat(pyY, camera2D->viewport.y); + return true; +} + +bool cre_pkpy_api_camera2d_set_offset(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const f64 offsetX = py_tofloat(py_arg(0)); + const f64 offsetY = py_tofloat(py_arg(1)); + + CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + camera2D->offset = (SkaVector2){ .x = (f32)offsetX, .y = (f32)offsetY }; + return true; +} + +bool cre_pkpy_api_camera2d_add_to_offset(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const f64 offsetX = py_tofloat(py_arg(0)); + const f64 offsetY = py_tofloat(py_arg(1)); + + CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + camera2D->offset = (SkaVector2){ .x = camera2D->offset.x + (f32)offsetX, .y = camera2D->offset.y + (f32)offsetY }; + return true; +} + +bool cre_pkpy_api_camera2d_get_offset(int argc, py_StackRef argv) { + const CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, camera2D->offset.x); + py_newfloat(pyY, camera2D->offset.y); + return true; +} + +bool cre_pkpy_api_camera2d_set_zoom(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const f64 zoomX = py_tofloat(py_arg(0)); + const f64 zoomY = py_tofloat(py_arg(1)); + + CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + camera2D->zoom = (SkaVector2){ .x = (f32)zoomX, .y = (f32)zoomY }; + return true; +} + +bool cre_pkpy_api_camera2d_add_to_zoom(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const f64 zoomX = py_tofloat(py_arg(0)); + const f64 zoomY = py_tofloat(py_arg(1)); + + CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + camera2D->zoom = (SkaVector2){ .x = camera2D->zoom.x + (f32)zoomX, .y = camera2D->zoom.y + (f32)zoomY }; + return true; +} + +bool cre_pkpy_api_camera2d_get_zoom(int argc, py_StackRef argv) { + const CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, camera2D->zoom.x); + py_newfloat(pyY, camera2D->zoom.y); + return true; +} + +bool cre_pkpy_api_camera2d_set_boundary(int argc, py_StackRef argv) { + PY_CHECK_ARGC(4); + const f64 x = py_tofloat(py_arg(0)); + const f64 y = py_tofloat(py_arg(1)); + const f64 w = py_tofloat(py_arg(2)); + const f64 h = py_tofloat(py_arg(3)); + + CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + camera2D->boundary = (SkaRect2){ .x = (f32)x, .y = (f32)y, .w = (f32)w, .h = (f32)h }; + cre_camera2d_clamp_viewport_to_boundary(camera2D); + return true; +} + +bool cre_pkpy_api_camera2d_get_boundary(int argc, py_StackRef argv) { + const CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + py_newtuple(py_retval(), 4); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_Ref pyW = py_tuple_getitem(py_retval(), 2); + py_Ref pyH = py_tuple_getitem(py_retval(), 3); + py_newfloat(pyX, camera2D->boundary.x); + py_newfloat(pyY, camera2D->boundary.y); + py_newfloat(pyW, camera2D->boundary.w); + py_newfloat(pyH, camera2D->boundary.h); + return true; +} + +bool cre_pkpy_api_camera2d_follow_node(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + cre_camera2d_follow_entity(camera2D, entity); + return true; +} + +bool cre_pkpy_api_camera2d_unfollow_node(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); + cre_camera2d_unfollow_entity(camera2D, entity); + return true; +} // World From 9df855b2c7013b523683580190eb1714c7bd487f Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Tue, 1 Oct 2024 21:34:55 -0400 Subject: [PATCH 20/54] More things. --- .../python/pocketpy/api/pkpy_api_impl.c | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index b39ca2122..119a235bf 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -10,8 +10,10 @@ #include "core/engine_context.h" #include "core/game_properties.h" +#include "core/world.h" #include "core/camera/camera.h" #include "core/camera/camera_manager.h" +#include "core/ecs/ecs_globals.h" #include "core/ecs/ecs_manager.h" #include "core/scene/scene_manager.h" #include "core/scripting/python/pocketpy/pkpy_instance_cache.h" @@ -720,10 +722,34 @@ bool cre_pkpy_api_camera2d_unfollow_node(int argc, py_StackRef argv) { // World -bool cre_pkpy_api_world_set_time_dilation(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_world_get_time_dilation(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_world_get_delta_time(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_world_get_variable_delta_time(int argc, py_StackRef argv) { return true; } +static void mark_scene_nodes_time_dilation_flag_dirty(SceneTreeNode* node) { + NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(node->entity, NODE_COMPONENT_INDEX); + nodeComponent->timeDilation.cacheInvalid = true; +} + +bool cre_pkpy_api_world_set_time_dilation(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const f64 timeDilation = py_tofloat(py_arg(0)); + + cre_world_set_time_dilation(timeDilation); + cre_scene_manager_execute_on_root_and_child_nodes(mark_scene_nodes_time_dilation_flag_dirty); + return true; +} + +bool cre_pkpy_api_world_get_time_dilation(int argc, py_StackRef argv) { + py_newfloat(py_retval(), (f64)cre_world_get_time_dilation()); + return true; +} + +bool cre_pkpy_api_world_get_delta_time(int argc, py_StackRef argv) { + py_newfloat(py_retval(), (f64)(cre_world_get_time_dilation() * CRE_GLOBAL_PHYSICS_DELTA_TIME)); + return true; +} + +bool cre_pkpy_api_world_get_variable_delta_time(int argc, py_StackRef argv) { + py_newfloat(py_retval(), (f64)cre_world_get_frame_delta_time()); + return true; +} // Audio Source @@ -759,7 +785,9 @@ bool cre_pkpy_api_network_is_server(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_server_start(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_server_stop(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_server_send(int argc, py_StackRef argv) { return true; } + // Client + bool cre_pkpy_api_client_start(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_client_stop(int argc, py_StackRef argv) { return true; } bool cre_pkpy_api_client_send(int argc, py_StackRef argv) { return true; } From e92afe7f07e3cf779dc9830ac73f7e973201a63c Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Tue, 1 Oct 2024 22:17:21 -0400 Subject: [PATCH 21/54] Update. --- .../python/pocketpy/api/pkpy_api_impl.c | 53 +++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 119a235bf..a017e28b8 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -1,7 +1,10 @@ #include "pkpy_api_impl.h" #include +#include #include +#include +#include #include #include #include @@ -17,6 +20,7 @@ #include "core/ecs/ecs_manager.h" #include "core/scene/scene_manager.h" #include "core/scripting/python/pocketpy/pkpy_instance_cache.h" +#include "seika/audio/audio.h" // Helper functions static inline SkaVector2 cre_pkpy_api_helper_mouse_get_global_position(const SkaVector2* offset) { @@ -753,13 +757,54 @@ bool cre_pkpy_api_world_get_variable_delta_time(int argc, py_StackRef argv) { // Audio Source -bool cre_pkpy_api_audio_source_set_pitch(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_audio_source_get_pitch(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_audio_source_set_pitch(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const char* path = py_tostr(py_arg(0)); + const f64 pitch = py_tofloat(py_arg(1)); + + if (ska_asset_manager_has_audio_source(path)) { + SkaAudioSource* audioSource = ska_asset_manager_get_audio_source(path); + audioSource->pitch = pitch; + } else { + ska_logger_warn("Tried to set non-existent audio source's pitch at '%s'", path); + } + return true; +} + +bool cre_pkpy_api_audio_source_get_pitch(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const char* path = py_tostr(py_arg(0)); + + f64 pitch; + if (ska_asset_manager_has_audio_source(path)) { + SkaAudioSource* audioSource = ska_asset_manager_get_audio_source(path); + pitch = (f64)audioSource->pitch; + } else { + ska_logger_warn("Tried to get non-existent audio source's pitch at '%s'", path); + pitch = 1.0; + } + py_newfloat(py_retval(), pitch); + return true; +} // Audio Manager -bool cre_pkpy_api_audio_manager_play_sound(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_audio_manager_stop_sound(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_audio_manager_play_sound(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const char* path = py_tostr(py_arg(0)); + const bool loops = py_tobool(py_arg(1)); + + ska_audio_manager_play_sound(path, loops); + return true; +} + +bool cre_pkpy_api_audio_manager_stop_sound(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const char* path = py_tostr(py_arg(0)); + + ska_audio_manager_stop_sound(path); + return true; +} // Game Config From fbc55e9e7c21eeb5d9f8253f22ce01d3e006c06a Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Tue, 1 Oct 2024 22:34:21 -0400 Subject: [PATCH 22/54] Updates. --- .../python/pocketpy/api/pkpy_api_impl.c | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index a017e28b8..6cd3e5a90 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -1,9 +1,13 @@ #include "pkpy_api_impl.h" #include +#include #include +#include #include +#include #include +#include #include #include #include @@ -20,7 +24,6 @@ #include "core/ecs/ecs_manager.h" #include "core/scene/scene_manager.h" #include "core/scripting/python/pocketpy/pkpy_instance_cache.h" -#include "seika/audio/audio.h" // Helper functions static inline SkaVector2 cre_pkpy_api_helper_mouse_get_global_position(const SkaVector2* offset) { @@ -735,7 +738,7 @@ bool cre_pkpy_api_world_set_time_dilation(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); const f64 timeDilation = py_tofloat(py_arg(0)); - cre_world_set_time_dilation(timeDilation); + cre_world_set_time_dilation((f32)timeDilation); cre_scene_manager_execute_on_root_and_child_nodes(mark_scene_nodes_time_dilation_flag_dirty); return true; } @@ -808,8 +811,39 @@ bool cre_pkpy_api_audio_manager_stop_sound(int argc, py_StackRef argv) { // Game Config -bool cre_pkpy_api_game_config_save(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_game_config_load(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_game_config_save(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + const char* path = py_tostr(py_arg(0)); + const char* jsonData = py_tostr(py_arg(1)); + const char* encryptionKey = py_tostr(py_arg(2)); + + const CREGameProperties* gameProps = cre_game_props_get(); + char* validGameTitle = ska_strdup(gameProps->gameTitle); + ska_str_to_lower_and_underscore_whitespace(validGameTitle); + char* fullSavePath = ska_fs_get_user_save_path("crescent", validGameTitle, path); + const bool success = ska_fs_write_to_file(fullSavePath, jsonData); + SKA_MEM_FREE(fullSavePath); + SKA_MEM_FREE(validGameTitle); + py_newbool(py_retval(), success); + return true; +} + +bool cre_pkpy_api_game_config_load(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + const char* path = py_tostr(py_arg(0)); + const char* encryptionKey = py_tostr(py_arg(1)); + + const CREGameProperties* gameProps = cre_game_props_get(); + char* validGameTitle = ska_strdup(gameProps->gameTitle); + ska_str_to_lower_and_underscore_whitespace(validGameTitle); + char* fullSavePath = ska_fs_get_user_save_path("crescent", validGameTitle, path); + char* fileContents = ska_asset_file_loader_read_file_contents_as_string(fullSavePath, NULL); + py_newstr(py_retval(), fileContents); + SKA_MEM_FREE(validGameTitle); + SKA_MEM_FREE(fullSavePath); + SKA_MEM_FREE(fileContents); + return true; +} // Packed Scene From 1e13038fe0445d88a97dec471459c22def161be5 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Tue, 1 Oct 2024 22:42:42 -0400 Subject: [PATCH 23/54] Update. --- .../python/pocketpy/api/pkpy_api_impl.c | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 6cd3e5a90..c733b41c2 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -22,7 +22,9 @@ #include "core/camera/camera_manager.h" #include "core/ecs/ecs_globals.h" #include "core/ecs/ecs_manager.h" +#include "core/ecs/components/script_component.h" #include "core/scene/scene_manager.h" +#include "core/scene/scene_template_cache.h" #include "core/scripting/python/pocketpy/pkpy_instance_cache.h" // Helper functions @@ -847,8 +849,32 @@ bool cre_pkpy_api_game_config_load(int argc, py_StackRef argv) { // Packed Scene -bool cre_pkpy_api_packed_scene_create_instance(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_packed_scene_load(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_packed_scene_create_instance(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 cacheId = py_toint(py_arg(0)); + + JsonSceneNode* sceneNode = cre_scene_template_cache_get_scene((CreSceneCacheId)cacheId); + SceneTreeNode* rootNode = cre_scene_manager_stage_scene_nodes_from_json(sceneNode); + + py_Ref newInstance; + if (sceneNode->components[SCRIPT_COMPONENT_INDEX] != NULL) { + ScriptComponent* scriptComp = (ScriptComponent*)sceneNode->components[SCRIPT_COMPONENT_INDEX]; + newInstance = cre_pkpy_instance_cache_add(rootNode->entity, scriptComp->classPath, scriptComp->className); + } else { + newInstance = cre_pkpy_instance_cache_add2(rootNode->entity); + } + py_assign(py_retval(), newInstance); + return true; +} + +bool cre_pkpy_api_packed_scene_load(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const char* path = py_tostr(py_arg(0)); + + const CreSceneCacheId cacheId = cre_scene_template_cache_load_scene(path); + py_newint(py_retval(), (py_i64)cacheId); + return true; +} // Collision Handler From 7b6cd3058a553bef2829c72bc571947b5d1f8865 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Tue, 1 Oct 2024 22:57:51 -0400 Subject: [PATCH 24/54] Have rough implementations of api calls except for nodes. --- .../python/pocketpy/api/pkpy_api_impl.c | 92 +++++++++++++++++-- 1 file changed, 83 insertions(+), 9 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index c733b41c2..bce36a4c5 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include #include @@ -23,6 +25,7 @@ #include "core/ecs/ecs_globals.h" #include "core/ecs/ecs_manager.h" #include "core/ecs/components/script_component.h" +#include "core/physics/collision/collision.h" #include "core/scene/scene_manager.h" #include "core/scene/scene_template_cache.h" #include "core/scripting/python/pocketpy/pkpy_instance_cache.h" @@ -878,24 +881,95 @@ bool cre_pkpy_api_packed_scene_load(int argc, py_StackRef argv) { // Collision Handler -bool cre_pkpy_api_collision_handler_process_collisions(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_collision_handler_process_mouse_collisions(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_collision_handler_process_collisions(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const CollisionResult collisionResult = cre_collision_process_entity_collisions(entity); + py_newtuple(py_retval(), (int)collisionResult.collidedEntityCount); + for (size_t i = 0; i < collisionResult.collidedEntityCount; i++) { + const SkaEntity collidedEntity = collisionResult.collidedEntities[i]; + py_Ref pyNode = py_tuple_getitem(py_retval(), (int)i); + py_assign(pyNode, cre_pkpy_instance_cache_add2(collidedEntity)); + } + return true; +} + +bool cre_pkpy_api_collision_handler_process_mouse_collisions(int argc, py_StackRef argv) { + PY_CHECK_ARGC(4); + const f64 offsetX = py_tofloat(py_arg(0)); + const f64 offsetY = py_tofloat(py_arg(1)); + const f64 sizeW = py_tofloat(py_arg(2)); + const f64 sizeH = py_tofloat(py_arg(3)); + + const SkaVector2 positionOffset = { .x = (f32)offsetX, .y = (f32)offsetY }; + const SkaVector2 mouseWorldPos = cre_pkpy_api_helper_mouse_get_global_position(&positionOffset); + const SkaRect2 collisionRect = { mouseWorldPos.x, mouseWorldPos.y, (f32)sizeW, (f32)sizeH }; + const CollisionResult collisionResult = cre_collision_process_mouse_collisions(&collisionRect); + py_newtuple(py_retval(), (int)collisionResult.collidedEntityCount); + for (size_t i = 0; i < collisionResult.collidedEntityCount; i++) { + const SkaEntity collidedEntity = collisionResult.collidedEntities[i]; + py_Ref pyNode = py_tuple_getitem(py_retval(), (int)i); + py_assign(pyNode, cre_pkpy_instance_cache_add2(collidedEntity)); + } + return true; +} // Network -bool cre_pkpy_api_network_is_server(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_network_is_server(int argc, py_StackRef argv) { + py_newbool(py_retval(), ska_network_is_server()); + return true; +} // Server -bool cre_pkpy_api_server_start(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_server_stop(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_server_send(int argc, py_StackRef argv) { return true; } + +bool cre_pkpy_api_server_start(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 port = py_toint(py_arg(0)); + + ska_udp_server_initialize((int32)port, ska_ecs_system_event_network_callback); + return true; +} + +bool cre_pkpy_api_server_stop(int argc, py_StackRef argv) { + ska_udp_server_finalize(); + return true; +} + +bool cre_pkpy_api_server_send(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const char* message = py_tostr(py_arg(0)); + + ska_udp_server_send_message(message); + return true; +} // Client -bool cre_pkpy_api_client_start(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_client_stop(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_client_send(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_client_start(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const char* host = py_tostr(py_arg(0)); + const py_i64 port = py_toint(py_arg(1)); + + ska_udp_client_initialize(host, (int32)port, ska_ecs_system_event_network_callback); + return true; +} + +bool cre_pkpy_api_client_stop(int argc, py_StackRef argv) { + ska_udp_client_finalize(); + return true; +} + +bool cre_pkpy_api_client_send(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const char* message = py_tostr(py_arg(0)); + + ska_udp_client_send_message(message); + return true; +} // Node From c5221b05bca13a251688d5b05fcc6bbcd59c78bf Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Wed, 2 Oct 2024 20:47:16 -0400 Subject: [PATCH 25/54] Using list instead of tuple to return collisions. --- crescent_py_api/pocketpy/crescent.py | 16 ++-------------- crescent_py_api/pocketpy/crescent_internal.py | 6 +++--- .../python/pocketpy/api/pkpy_api_impl.c | 10 ++++------ .../python/pocketpy/cre_pkpy_api_source.h | 16 ++-------------- 4 files changed, 11 insertions(+), 37 deletions(-) diff --git a/crescent_py_api/pocketpy/crescent.py b/crescent_py_api/pocketpy/crescent.py index 8a7563562..4cee6e847 100644 --- a/crescent_py_api/pocketpy/crescent.py +++ b/crescent_py_api/pocketpy/crescent.py @@ -1476,13 +1476,7 @@ class CollisionHandler: """ @staticmethod def process_collisions(collider: Collider2D) -> List[Node]: - collided_entities = crescent_internal.collision_handler_process_collisions(collider.entity_id) - if collided_entities: - if isinstance(collided_entities, tuple): - return list(collided_entities) - elif isinstance(collided_entities, Node): - return [collided_entities] - return [] + return crescent_internal.collision_handler_process_collisions(collider.entity_id) @staticmethod def process_mouse_collisions(pos_offset: Optional[Vector2] = None, collision_size: Optional[Size2D] = None) -> List[Node]: @@ -1490,13 +1484,7 @@ def process_mouse_collisions(pos_offset: Optional[Vector2] = None, collision_siz pos_offset = Vector2.ZERO() if not collision_size: collision_size = Size2D(1.0, 1.0) - collided_entities = crescent_internal.collision_handler_process_mouse_collisions(pos_offset.x, pos_offset.y, collision_size.w, collision_size.h) - if collided_entities: - if isinstance(collided_entities, tuple): - return list(collided_entities) - elif isinstance(collided_entities, Node): - return [collided_entities] - return [] + return crescent_internal.collision_handler_process_mouse_collisions(pos_offset.x, pos_offset.y, collision_size.w, collision_size.h) # Work around for singleton until class methods are in diff --git a/crescent_py_api/pocketpy/crescent_internal.py b/crescent_py_api/pocketpy/crescent_internal.py index 717332086..0ae40f577 100644 --- a/crescent_py_api/pocketpy/crescent_internal.py +++ b/crescent_py_api/pocketpy/crescent_internal.py @@ -1,4 +1,4 @@ -from typing import Tuple, Optional +from typing import Tuple, Optional, List # Mock class for engine's internal c python module @@ -673,11 +673,11 @@ def packed_scene_load(path: str) -> int: # --- Collision Handler --- # -def collision_handler_process_collisions(entity_id: float) -> Tuple["Node", ...]: +def collision_handler_process_collisions(entity_id: float) -> List["Node"]: return () -def collision_handler_process_mouse_collisions(pos_offset_x: float, pos_offset_y: float, collision_size_w: float, collision_size_h: float) -> Tuple["Node", ...]: +def collision_handler_process_mouse_collisions(pos_offset_x: float, pos_offset_y: float, collision_size_w: float, collision_size_h: float) -> List["Node"]: return () diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index bce36a4c5..758d0f27a 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -887,11 +887,10 @@ bool cre_pkpy_api_collision_handler_process_collisions(int argc, py_StackRef arg const SkaEntity entity = (SkaEntity)entityId; const CollisionResult collisionResult = cre_collision_process_entity_collisions(entity); - py_newtuple(py_retval(), (int)collisionResult.collidedEntityCount); + py_newlistn(py_retval(), (int)collisionResult.collidedEntityCount); for (size_t i = 0; i < collisionResult.collidedEntityCount; i++) { const SkaEntity collidedEntity = collisionResult.collidedEntities[i]; - py_Ref pyNode = py_tuple_getitem(py_retval(), (int)i); - py_assign(pyNode, cre_pkpy_instance_cache_add2(collidedEntity)); + py_list_setitem(py_retval(), (int)i, cre_pkpy_instance_cache_add2(collidedEntity)); } return true; } @@ -907,11 +906,10 @@ bool cre_pkpy_api_collision_handler_process_mouse_collisions(int argc, py_StackR const SkaVector2 mouseWorldPos = cre_pkpy_api_helper_mouse_get_global_position(&positionOffset); const SkaRect2 collisionRect = { mouseWorldPos.x, mouseWorldPos.y, (f32)sizeW, (f32)sizeH }; const CollisionResult collisionResult = cre_collision_process_mouse_collisions(&collisionRect); - py_newtuple(py_retval(), (int)collisionResult.collidedEntityCount); + py_newlistn(py_retval(), (int)collisionResult.collidedEntityCount); for (size_t i = 0; i < collisionResult.collidedEntityCount; i++) { const SkaEntity collidedEntity = collisionResult.collidedEntities[i]; - py_Ref pyNode = py_tuple_getitem(py_retval(), (int)i); - py_assign(pyNode, cre_pkpy_instance_cache_add2(collidedEntity)); + py_list_setitem(py_retval(), (int)i, cre_pkpy_instance_cache_add2(collidedEntity)); } return true; } diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h index 16440277b..589f9f509 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h @@ -1479,13 +1479,7 @@ " \"\"\"\n"\ " @staticmethod\n"\ " def process_collisions(collider: Collider2D) -> List[Node]:\n"\ -" collided_entities = crescent_internal.collision_handler_process_collisions(collider.entity_id)\n"\ -" if collided_entities:\n"\ -" if isinstance(collided_entities, tuple):\n"\ -" return list(collided_entities)\n"\ -" elif isinstance(collided_entities, Node):\n"\ -" return [collided_entities]\n"\ -" return []\n"\ +" return crescent_internal.collision_handler_process_collisions(collider.entity_id)\n"\ "\n"\ " @staticmethod\n"\ " def process_mouse_collisions(pos_offset: Optional[Vector2] = None, collision_size: Optional[Size2D] = None) -> List[Node]:\n"\ @@ -1493,13 +1487,7 @@ " pos_offset = Vector2.ZERO()\n"\ " if not collision_size:\n"\ " collision_size = Size2D(1.0, 1.0)\n"\ -" collided_entities = crescent_internal.collision_handler_process_mouse_collisions(pos_offset.x, pos_offset.y, collision_size.w, collision_size.h)\n"\ -" if collided_entities:\n"\ -" if isinstance(collided_entities, tuple):\n"\ -" return list(collided_entities)\n"\ -" elif isinstance(collided_entities, Node):\n"\ -" return [collided_entities]\n"\ -" return []\n"\ +" return crescent_internal.collision_handler_process_mouse_collisions(pos_offset.x, pos_offset.y, collision_size.w, collision_size.h)\n"\ "\n"\ "\n"\ "# Work around for singleton until class methods are in\n"\ From 4f44840dcff9b9681fc2d5b986f0c271e4c17e58 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Wed, 2 Oct 2024 21:19:54 -0400 Subject: [PATCH 26/54] Starting on nodes. --- .../python/pocketpy/api/pkpy_api_impl.c | 202 +++++++++++++++++- .../python/pocketpy/pkpy_instance_cache.c | 4 +- 2 files changed, 193 insertions(+), 13 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 758d0f27a..0666757f1 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -24,11 +24,19 @@ #include "core/camera/camera_manager.h" #include "core/ecs/ecs_globals.h" #include "core/ecs/ecs_manager.h" +#include "core/ecs/components/animated_sprite_component.h" +#include "core/ecs/components/color_rect_component.h" +#include "core/ecs/components/parallax_component.h" +#include "core/ecs/components/particles2d_component.h" #include "core/ecs/components/script_component.h" +#include "core/ecs/components/sprite_component.h" +#include "core/ecs/components/text_label_component.h" +#include "core/ecs/components/tilemap_component.h" #include "core/physics/collision/collision.h" #include "core/scene/scene_manager.h" #include "core/scene/scene_template_cache.h" #include "core/scripting/python/pocketpy/pkpy_instance_cache.h" +#include "seika/flag_utils.h" // Helper functions static inline SkaVector2 cre_pkpy_api_helper_mouse_get_global_position(const SkaVector2* offset) { @@ -971,17 +979,189 @@ bool cre_pkpy_api_client_send(int argc, py_StackRef argv) { // Node -bool cre_pkpy_api_node_new(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node_get_name(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node_add_child(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node_get_child(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node_get_children(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node_get_parent(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node_queue_deletion(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node_is_queued_for_deletion(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node_set_time_dilation(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node_get_time_dilation(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node_get_total_time_dilation(int argc, py_StackRef argv) { return true; } +static void set_node_component_from_type(SkaEntity entity, const char* classPath, const char* className, NodeBaseType baseType) { + + // Set components that should be set for a base node (that has invoked .new() from scripting) + ska_ecs_component_manager_set_component(entity, NODE_COMPONENT_INDEX, node_component_create_ex(className, baseType)); + ska_ecs_component_manager_set_component(entity, SCRIPT_COMPONENT_INDEX, script_component_create_ex(classPath, className, CreScriptContextType_PYTHON)); + + const NodeBaseInheritanceType inheritanceType = node_get_type_inheritance(baseType); + + if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_NODE2D)) { + ska_ecs_component_manager_set_component(entity, TRANSFORM2D_COMPONENT_INDEX, transform2d_component_create()); + } + if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_SPRITE)) { + ska_ecs_component_manager_set_component(entity, SPRITE_COMPONENT_INDEX, sprite_component_create()); + } + if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_ANIMATED_SPRITE)) { + ska_ecs_component_manager_set_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX, animated_sprite_component_create()); + } + if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_TEXT_LABEL)) { + ska_ecs_component_manager_set_component(entity, TEXT_LABEL_COMPONENT_INDEX, text_label_component_create()); + } + if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_COLLIDER2D)) { + ska_ecs_component_manager_set_component(entity, COLLIDER2D_COMPONENT_INDEX, collider2d_component_create()); + } + if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_COLOR_RECT)) { + ska_ecs_component_manager_set_component(entity, COLOR_RECT_COMPONENT_INDEX, color_rect_component_create()); + } + if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_PARALLAX)) { + ska_ecs_component_manager_set_component(entity, PARALLAX_COMPONENT_INDEX, parallax_component_create()); + } + if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_PARTICLES2D)) { + ska_ecs_component_manager_set_component(entity, PARTICLES2D_COMPONENT_INDEX, particles2d_component_create()); + } + if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_TILEMAP)) { + ska_ecs_component_manager_set_component(entity, TILEMAP_COMPONENT_INDEX, tilemap_component_create()); + } +} + +bool cre_pkpy_api_node_new(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + const char* classPath = py_tostr(py_arg(0)); + const char* className = py_tostr(py_arg(1)); + const py_i64 nodeTypeFlag = py_toint(py_arg(2)); + + const SkaEntity entity = ska_ecs_entity_create(); + py_Ref newInstance = cre_pkpy_instance_cache_add(entity, classPath, className); + SceneTreeNode* newNode = cre_scene_tree_create_tree_node(entity, NULL); + cre_scene_manager_stage_child_node_to_be_added_later(newNode); + set_node_component_from_type(entity, classPath, className, (NodeBaseType)nodeTypeFlag); + py_assign(py_retval(), newInstance); + return true; +} + +bool cre_pkpy_api_node_get_name(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); + py_newstr(py_retval(), nodeComponent->name); + return true; +} + +bool cre_pkpy_api_node_add_child(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const py_i64 childEntityId = py_toint(py_arg(0)); + const py_i64 parentEntityId = py_toint(py_arg(1)); + + cre_scene_manager_add_node_as_child((SkaEntity)childEntityId, (SkaEntity)parentEntityId); + return true; +} + +bool cre_pkpy_api_node_get_child(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const py_i64 parentEntityId = py_toint(py_arg(0)); + const char* childName = py_tostr(py_arg(1)); + + const SkaEntity childEntity = cre_scene_manager_get_entity_child_by_name((SkaEntity)parentEntityId, childName); + if (childEntity != SKA_NULL_ENTITY) { + py_assign(py_retval(), cre_pkpy_instance_cache_add2(childEntity)); + } else { + ska_logger_warn("Unable to find child '%s' from entity '%u'", childName, parentEntityId); + py_newnone(py_retval()); + } + return true; +} + +bool cre_pkpy_api_node_get_children(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + if (cre_scene_manager_has_entity_tree_node(entity)) { + const SceneTreeNode* parentTreeNode = cre_scene_manager_get_entity_tree_node(entity); + py_newlistn(py_retval(), (int)parentTreeNode->childCount); + for (size_t i = 0; i < parentTreeNode->childCount; i++) { + const SceneTreeNode* childTreeNode = parentTreeNode->children[i]; + py_list_setitem(py_retval(), (int)i, cre_pkpy_instance_cache_add2(childTreeNode->entity)); + } + } else { + ska_logger_warn("Doesn't have tree node when trying to get children from %u", entity); + py_newlist(py_retval()); + } + + return true; +} + +bool cre_pkpy_api_node_get_parent(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 childEntityId = py_toint(py_arg(0)); + + const SkaEntity childEntity = (SkaEntity)childEntityId; + if (cre_scene_manager_has_entity_tree_node(childEntity)) { + const SceneTreeNode* treeNode = cre_scene_manager_get_entity_tree_node(childEntity); + if (treeNode->parent) { + py_assign(py_retval(), cre_pkpy_instance_cache_add2(treeNode->parent->entity)); + } else { + py_newnone(py_retval()); + } + } + return true; +} + +bool cre_pkpy_api_node_queue_deletion(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); + if (!nodeComponent->queuedForDeletion) { + if (cre_scene_manager_has_entity_tree_node(entity)) { + SceneTreeNode* node = cre_scene_manager_get_entity_tree_node(entity); + cre_queue_destroy_tree_node_entity_all(node); + } else { + ska_logger_warn("Entity not found in tree, skipping queue deletion."); + } + } else { + ska_logger_warn("Node '%s' already queued for deletion!", nodeComponent->name); + } + return true; +} + +bool cre_pkpy_api_node_is_queued_for_deletion(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); + py_newbool(py_retval(), nodeComponent->queuedForDeletion); + return true; +} + +bool cre_pkpy_api_node_set_time_dilation(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 timeDilation = py_tofloat(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); + nodeComponent->timeDilation.value = (f32)timeDilation; + cre_scene_manager_invalidate_time_dilation_nodes_with_children(entity); + return true; +} + +bool cre_pkpy_api_node_get_time_dilation(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); + const f64 timeDilation = (f64)nodeComponent->timeDilation.value; + py_newfloat(py_retval(), timeDilation); + return true; +} + +bool cre_pkpy_api_node_get_total_time_dilation(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const f64 totalTimeDilation = (f64)cre_scene_manager_get_node_full_time_dilation(entity); + py_newfloat(py_retval(), totalTimeDilation); + return true; +} // Node2D diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c b/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c index b962dbce5..e220e7a92 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_instance_cache.c @@ -26,7 +26,7 @@ void cre_pkpy_instance_cache_finalize() { py_Ref cre_pkpy_instance_cache_add(SkaEntity entity,const char* classPath, const char* className) { // Early out if entity already exists if (cre_pkpy_instance_cache_has(entity)) { - ska_logger_warn("Attempting to add entity '%u' from '%s.%s'!", entity, classPath, className); + ska_logger_info("Attempted to add entity '%u' from '%s.%s', but it already exists!", entity, classPath, className); return cre_pkpy_instance_cache_get(entity); } @@ -51,7 +51,7 @@ py_Ref cre_pkpy_instance_cache_add2(SkaEntity entity) { // Early out if entity already exists if (cre_pkpy_instance_cache_has(entity)) { const ScriptComponent* scriptComponent = (ScriptComponent*)ska_ecs_component_manager_get_component(entity, SCRIPT_COMPONENT_INDEX); - ska_logger_warn("Attempting to add entity '%u' from '%s.%s'!", entity, scriptComponent->classPath, scriptComponent->className); + ska_logger_info("Attempted to add entity '%u' from '%s.%s', but it already exists!", entity, scriptComponent->classPath, scriptComponent->className); return cre_pkpy_instance_cache_get(entity); } From b3560775867c2692fe1725a42a5a07f40cb75879 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Wed, 2 Oct 2024 21:33:29 -0400 Subject: [PATCH 27/54] Checking arg types. --- .../python/pocketpy/api/pkpy_api_impl.c | 71 ++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 0666757f1..8c614a90a 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -61,6 +61,7 @@ static inline SkaVector2 cre_pkpy_api_helper_mouse_get_global_position(const Ska bool cre_pkpy_api_shader_instance_delete(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 pyShaderId = py_toint(py_arg(0)); const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; @@ -76,6 +77,7 @@ bool cre_pkpy_api_shader_instance_delete(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_create_bool_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_bool); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const bool value = py_tobool(py_arg(2)); @@ -88,6 +90,7 @@ bool cre_pkpy_api_shader_instance_create_bool_param(int argc, py_StackRef argv) bool cre_pkpy_api_shader_instance_set_bool_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_bool); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const bool value = py_tobool(py_arg(2)); @@ -100,6 +103,7 @@ bool cre_pkpy_api_shader_instance_set_bool_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_get_bool_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); @@ -112,6 +116,7 @@ bool cre_pkpy_api_shader_instance_get_bool_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_create_int_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_int); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const py_i64 value = py_toint(py_arg(2)); @@ -124,6 +129,7 @@ bool cre_pkpy_api_shader_instance_create_int_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_set_int_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_int); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const py_i64 value = py_toint(py_arg(2)); @@ -136,6 +142,7 @@ bool cre_pkpy_api_shader_instance_set_int_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_get_int_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); @@ -148,6 +155,7 @@ bool cre_pkpy_api_shader_instance_get_int_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_create_float_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_float); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const f64 value = py_tofloat(py_arg(2)); @@ -160,6 +168,7 @@ bool cre_pkpy_api_shader_instance_create_float_param(int argc, py_StackRef argv) bool cre_pkpy_api_shader_instance_set_float_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_float); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const f64 value = py_tofloat(py_arg(2)); @@ -172,6 +181,7 @@ bool cre_pkpy_api_shader_instance_set_float_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_get_float_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); @@ -184,6 +194,7 @@ bool cre_pkpy_api_shader_instance_get_float_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_create_float2_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(4); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const f64 valueX = py_tofloat(py_arg(2)); @@ -198,6 +209,7 @@ bool cre_pkpy_api_shader_instance_create_float2_param(int argc, py_StackRef argv bool cre_pkpy_api_shader_instance_set_float2_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(4); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const f64 valueX = py_tofloat(py_arg(2)); @@ -212,6 +224,7 @@ bool cre_pkpy_api_shader_instance_set_float2_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_get_float2_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); @@ -228,6 +241,7 @@ bool cre_pkpy_api_shader_instance_get_float2_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_create_float3_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); PY_CHECK_ARG_TYPE(4, tp_float); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const f64 valueX = py_tofloat(py_arg(2)); @@ -243,6 +257,7 @@ bool cre_pkpy_api_shader_instance_create_float3_param(int argc, py_StackRef argv bool cre_pkpy_api_shader_instance_set_float3_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); PY_CHECK_ARG_TYPE(4, tp_float); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const f64 valueX = py_tofloat(py_arg(2)); @@ -258,6 +273,7 @@ bool cre_pkpy_api_shader_instance_set_float3_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_get_float3_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); @@ -276,6 +292,7 @@ bool cre_pkpy_api_shader_instance_get_float3_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_create_float4_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(6); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); PY_CHECK_ARG_TYPE(4, tp_float); PY_CHECK_ARG_TYPE(5, tp_float); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const f64 valueX = py_tofloat(py_arg(2)); @@ -292,6 +309,7 @@ bool cre_pkpy_api_shader_instance_create_float4_param(int argc, py_StackRef argv bool cre_pkpy_api_shader_instance_set_float4_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(6); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); PY_CHECK_ARG_TYPE(4, tp_float); PY_CHECK_ARG_TYPE(5, tp_float); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); const f64 valueX = py_tofloat(py_arg(2)); @@ -308,6 +326,7 @@ bool cre_pkpy_api_shader_instance_set_float4_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_instance_get_float4_param(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); const py_i64 pyShaderId = py_toint(py_arg(0)); const char* paramName = py_tostr(py_arg(1)); @@ -330,6 +349,7 @@ bool cre_pkpy_api_shader_instance_get_float4_param(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_util_compile_shader(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_str); const char* shaderPath = py_tostr(py_arg(0)); const SkaShaderInstanceId newId = ska_shader_cache_create_instance_and_add(shaderPath); @@ -340,6 +360,7 @@ bool cre_pkpy_api_shader_util_compile_shader(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_util_compile_shader_raw(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_str); PY_CHECK_ARG_TYPE(1, tp_str); const char* vertexPath = py_tostr(py_arg(0)); const char* fragmentPath = py_tostr(py_arg(1)); @@ -351,6 +372,7 @@ bool cre_pkpy_api_shader_util_compile_shader_raw(int argc, py_StackRef argv) { bool cre_pkpy_api_shader_util_set_screen_shader(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 pyShaderId = py_toint(py_arg(0)); const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; @@ -379,6 +401,7 @@ bool cre_pkpy_api_shader_util_reset_screen_shader_to_default(int argc, py_StackR bool cre_pkpy_api_engine_exit(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 pyExitCode = py_toint(py_arg(0)); CREEngineContext* engineContext = cre_engine_context_get(); @@ -410,6 +433,7 @@ bool cre_pkpy_api_engine_get_average_fps(int argc, py_StackRef argv) { bool cre_pkpy_api_engine_set_fps_display_enabled(int argc, py_StackRef argv) { PY_CHECK_ARGC(4); + PY_CHECK_ARG_TYPE(0, tp_bool); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); const bool isEnabled = py_tobool(py_arg(0)); const char* fontUID = py_tostr(py_arg(1)); const py_f64 pyPosX = py_tofloat(py_arg(2)); @@ -428,6 +452,7 @@ bool cre_pkpy_api_engine_get_global_physics_delta_time(int argc, py_StackRef arg bool cre_pkpy_api_input_is_key_pressed(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 pyKey = py_toint(py_arg(0)); const bool isPressed = ska_input_is_key_pressed((SkaInputKey)pyKey, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX); @@ -437,6 +462,7 @@ bool cre_pkpy_api_input_is_key_pressed(int argc, py_StackRef argv) { bool cre_pkpy_api_input_is_key_just_pressed(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 pyKey = py_toint(py_arg(0)); const bool isPressed = ska_input_is_key_just_pressed((SkaInputKey)pyKey, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX); @@ -446,6 +472,7 @@ bool cre_pkpy_api_input_is_key_just_pressed(int argc, py_StackRef argv) { bool cre_pkpy_api_input_is_key_just_released(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 pyKey = py_toint(py_arg(0)); const bool isPressed = ska_input_is_key_just_released((SkaInputKey)pyKey, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX); @@ -455,6 +482,7 @@ bool cre_pkpy_api_input_is_key_just_released(int argc, py_StackRef argv) { bool cre_pkpy_api_input_add_action(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_str); PY_CHECK_ARG_TYPE(1, tp_int); PY_CHECK_ARG_TYPE(2, tp_int); const char* actionName = py_tostr(py_arg(0)); const py_i64 pyValueKey = py_toint(py_arg(1)); const py_i64 pyDeviceId = py_toint(py_arg(2)); @@ -469,6 +497,7 @@ bool cre_pkpy_api_input_add_action(int argc, py_StackRef argv) { bool cre_pkpy_api_input_is_action_pressed(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_str); const char* actionName = py_tostr(py_arg(0)); // TODO: Probably should take device index as a param @@ -481,6 +510,7 @@ bool cre_pkpy_api_input_is_action_pressed(int argc, py_StackRef argv) { bool cre_pkpy_api_input_is_action_just_pressed(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_str); const char* actionName = py_tostr(py_arg(0)); const SkaInputDeviceIndex deviceIndex = SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX; @@ -492,6 +522,7 @@ bool cre_pkpy_api_input_is_action_just_pressed(int argc, py_StackRef argv) { bool cre_pkpy_api_input_is_action_just_released(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_str); const char* actionName = py_tostr(py_arg(0)); const SkaInputDeviceIndex deviceIndex = SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX; @@ -504,6 +535,7 @@ bool cre_pkpy_api_input_is_action_just_released(int argc, py_StackRef argv) { // TODO: Finish implementing bool cre_pkpy_api_input_start_gamepad_vibration(int argc, py_StackRef argv) { PY_CHECK_ARGC(4); + PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); const py_i64 pyDeviceId = py_toint(py_arg(0)); const py_f64 pyWeakMagnitude = py_tofloat(py_arg(1)); const py_f64 pyStrongMagnitude = py_tofloat(py_arg(2)); @@ -516,6 +548,7 @@ bool cre_pkpy_api_input_start_gamepad_vibration(int argc, py_StackRef argv) { // TODO: Finish implementing bool cre_pkpy_api_input_stop_gamepad_vibration(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 pyDeviceId = py_toint(py_arg(0)); // ska_input_gamepad_stop_vibration((SkaInputDeviceIndex)pyDeviceId); @@ -546,6 +579,7 @@ bool cre_pkpy_api_input_mouse_get_world_position(int argc, py_StackRef argv) { bool cre_pkpy_api_scene_tree_change_scene(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_str); const char* scenePath = py_tostr(py_arg(0)); cre_scene_manager_queue_scene_change(scenePath); @@ -603,6 +637,7 @@ bool cre_pkpy_api_game_properties_get(int argc, py_StackRef argv) { bool cre_pkpy_api_camera2d_set_position(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(1, tp_float); const f64 posX = py_tofloat(py_arg(0)); const f64 posY = py_tofloat(py_arg(1)); @@ -614,6 +649,7 @@ bool cre_pkpy_api_camera2d_set_position(int argc, py_StackRef argv) { bool cre_pkpy_api_camera2d_add_to_position(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(1, tp_float); const f64 posX = py_tofloat(py_arg(0)); const f64 posY = py_tofloat(py_arg(1)); @@ -635,6 +671,7 @@ bool cre_pkpy_api_camera2d_get_position(int argc, py_StackRef argv) { bool cre_pkpy_api_camera2d_set_offset(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(1, tp_float); const f64 offsetX = py_tofloat(py_arg(0)); const f64 offsetY = py_tofloat(py_arg(1)); @@ -645,6 +682,7 @@ bool cre_pkpy_api_camera2d_set_offset(int argc, py_StackRef argv) { bool cre_pkpy_api_camera2d_add_to_offset(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(1, tp_float); const f64 offsetX = py_tofloat(py_arg(0)); const f64 offsetY = py_tofloat(py_arg(1)); @@ -665,6 +703,7 @@ bool cre_pkpy_api_camera2d_get_offset(int argc, py_StackRef argv) { bool cre_pkpy_api_camera2d_set_zoom(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(1, tp_float); const f64 zoomX = py_tofloat(py_arg(0)); const f64 zoomY = py_tofloat(py_arg(1)); @@ -675,6 +714,7 @@ bool cre_pkpy_api_camera2d_set_zoom(int argc, py_StackRef argv) { bool cre_pkpy_api_camera2d_add_to_zoom(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(1, tp_float); const f64 zoomX = py_tofloat(py_arg(0)); const f64 zoomY = py_tofloat(py_arg(1)); @@ -695,6 +735,7 @@ bool cre_pkpy_api_camera2d_get_zoom(int argc, py_StackRef argv) { bool cre_pkpy_api_camera2d_set_boundary(int argc, py_StackRef argv) { PY_CHECK_ARGC(4); + PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); const f64 x = py_tofloat(py_arg(0)); const f64 y = py_tofloat(py_arg(1)); const f64 w = py_tofloat(py_arg(2)); @@ -722,6 +763,7 @@ bool cre_pkpy_api_camera2d_get_boundary(int argc, py_StackRef argv) { bool cre_pkpy_api_camera2d_follow_node(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 entityId = py_toint(py_arg(0)); const SkaEntity entity = (SkaEntity)entityId; @@ -732,6 +774,7 @@ bool cre_pkpy_api_camera2d_follow_node(int argc, py_StackRef argv) { bool cre_pkpy_api_camera2d_unfollow_node(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 entityId = py_toint(py_arg(0)); const SkaEntity entity = (SkaEntity)entityId; @@ -749,6 +792,7 @@ static void mark_scene_nodes_time_dilation_flag_dirty(SceneTreeNode* node) { bool cre_pkpy_api_world_set_time_dilation(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_float); const f64 timeDilation = py_tofloat(py_arg(0)); cre_world_set_time_dilation((f32)timeDilation); @@ -775,6 +819,7 @@ bool cre_pkpy_api_world_get_variable_delta_time(int argc, py_StackRef argv) { bool cre_pkpy_api_audio_source_set_pitch(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_str); PY_CHECK_ARG_TYPE(1, tp_float); const char* path = py_tostr(py_arg(0)); const f64 pitch = py_tofloat(py_arg(1)); @@ -789,6 +834,7 @@ bool cre_pkpy_api_audio_source_set_pitch(int argc, py_StackRef argv) { bool cre_pkpy_api_audio_source_get_pitch(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_str); const char* path = py_tostr(py_arg(0)); f64 pitch; @@ -807,6 +853,7 @@ bool cre_pkpy_api_audio_source_get_pitch(int argc, py_StackRef argv) { bool cre_pkpy_api_audio_manager_play_sound(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_str); PY_CHECK_ARG_TYPE(1, tp_bool); const char* path = py_tostr(py_arg(0)); const bool loops = py_tobool(py_arg(1)); @@ -816,6 +863,7 @@ bool cre_pkpy_api_audio_manager_play_sound(int argc, py_StackRef argv) { bool cre_pkpy_api_audio_manager_stop_sound(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_str); const char* path = py_tostr(py_arg(0)); ska_audio_manager_stop_sound(path); @@ -826,6 +874,7 @@ bool cre_pkpy_api_audio_manager_stop_sound(int argc, py_StackRef argv) { bool cre_pkpy_api_game_config_save(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_str); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_str); const char* path = py_tostr(py_arg(0)); const char* jsonData = py_tostr(py_arg(1)); const char* encryptionKey = py_tostr(py_arg(2)); @@ -842,7 +891,8 @@ bool cre_pkpy_api_game_config_save(int argc, py_StackRef argv) { } bool cre_pkpy_api_game_config_load(int argc, py_StackRef argv) { - PY_CHECK_ARGC(3); + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_str); PY_CHECK_ARG_TYPE(1, tp_str); const char* path = py_tostr(py_arg(0)); const char* encryptionKey = py_tostr(py_arg(1)); @@ -862,6 +912,7 @@ bool cre_pkpy_api_game_config_load(int argc, py_StackRef argv) { bool cre_pkpy_api_packed_scene_create_instance(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 cacheId = py_toint(py_arg(0)); JsonSceneNode* sceneNode = cre_scene_template_cache_get_scene((CreSceneCacheId)cacheId); @@ -880,6 +931,7 @@ bool cre_pkpy_api_packed_scene_create_instance(int argc, py_StackRef argv) { bool cre_pkpy_api_packed_scene_load(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_str); const char* path = py_tostr(py_arg(0)); const CreSceneCacheId cacheId = cre_scene_template_cache_load_scene(path); @@ -891,6 +943,7 @@ bool cre_pkpy_api_packed_scene_load(int argc, py_StackRef argv) { bool cre_pkpy_api_collision_handler_process_collisions(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 entityId = py_toint(py_arg(0)); const SkaEntity entity = (SkaEntity)entityId; @@ -905,6 +958,7 @@ bool cre_pkpy_api_collision_handler_process_collisions(int argc, py_StackRef arg bool cre_pkpy_api_collision_handler_process_mouse_collisions(int argc, py_StackRef argv) { PY_CHECK_ARGC(4); + PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); const f64 offsetX = py_tofloat(py_arg(0)); const f64 offsetY = py_tofloat(py_arg(1)); const f64 sizeW = py_tofloat(py_arg(2)); @@ -934,6 +988,7 @@ bool cre_pkpy_api_network_is_server(int argc, py_StackRef argv) { bool cre_pkpy_api_server_start(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 port = py_toint(py_arg(0)); ska_udp_server_initialize((int32)port, ska_ecs_system_event_network_callback); @@ -947,6 +1002,7 @@ bool cre_pkpy_api_server_stop(int argc, py_StackRef argv) { bool cre_pkpy_api_server_send(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_str); const char* message = py_tostr(py_arg(0)); ska_udp_server_send_message(message); @@ -957,6 +1013,7 @@ bool cre_pkpy_api_server_send(int argc, py_StackRef argv) { bool cre_pkpy_api_client_start(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_str); PY_CHECK_ARG_TYPE(1, tp_int); const char* host = py_tostr(py_arg(0)); const py_i64 port = py_toint(py_arg(1)); @@ -971,6 +1028,7 @@ bool cre_pkpy_api_client_stop(int argc, py_StackRef argv) { bool cre_pkpy_api_client_send(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_str); const char* message = py_tostr(py_arg(0)); ska_udp_client_send_message(message); @@ -1018,6 +1076,7 @@ static void set_node_component_from_type(SkaEntity entity, const char* classPath bool cre_pkpy_api_node_new(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_str); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_int); const char* classPath = py_tostr(py_arg(0)); const char* className = py_tostr(py_arg(1)); const py_i64 nodeTypeFlag = py_toint(py_arg(2)); @@ -1033,6 +1092,7 @@ bool cre_pkpy_api_node_new(int argc, py_StackRef argv) { bool cre_pkpy_api_node_get_name(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 entityId = py_toint(py_arg(0)); const SkaEntity entity = (SkaEntity)entityId; @@ -1043,6 +1103,7 @@ bool cre_pkpy_api_node_get_name(int argc, py_StackRef argv) { bool cre_pkpy_api_node_add_child(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); const py_i64 childEntityId = py_toint(py_arg(0)); const py_i64 parentEntityId = py_toint(py_arg(1)); @@ -1052,6 +1113,7 @@ bool cre_pkpy_api_node_add_child(int argc, py_StackRef argv) { bool cre_pkpy_api_node_get_child(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); const py_i64 parentEntityId = py_toint(py_arg(0)); const char* childName = py_tostr(py_arg(1)); @@ -1067,6 +1129,7 @@ bool cre_pkpy_api_node_get_child(int argc, py_StackRef argv) { bool cre_pkpy_api_node_get_children(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 entityId = py_toint(py_arg(0)); const SkaEntity entity = (SkaEntity)entityId; @@ -1087,6 +1150,7 @@ bool cre_pkpy_api_node_get_children(int argc, py_StackRef argv) { bool cre_pkpy_api_node_get_parent(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 childEntityId = py_toint(py_arg(0)); const SkaEntity childEntity = (SkaEntity)childEntityId; @@ -1103,6 +1167,7 @@ bool cre_pkpy_api_node_get_parent(int argc, py_StackRef argv) { bool cre_pkpy_api_node_queue_deletion(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 entityId = py_toint(py_arg(0)); const SkaEntity entity = (SkaEntity)entityId; @@ -1122,6 +1187,7 @@ bool cre_pkpy_api_node_queue_deletion(int argc, py_StackRef argv) { bool cre_pkpy_api_node_is_queued_for_deletion(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 entityId = py_toint(py_arg(0)); const SkaEntity entity = (SkaEntity)entityId; @@ -1132,6 +1198,7 @@ bool cre_pkpy_api_node_is_queued_for_deletion(int argc, py_StackRef argv) { bool cre_pkpy_api_node_set_time_dilation(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); const py_i64 entityId = py_toint(py_arg(0)); const f64 timeDilation = py_tofloat(py_arg(1)); @@ -1144,6 +1211,7 @@ bool cre_pkpy_api_node_set_time_dilation(int argc, py_StackRef argv) { bool cre_pkpy_api_node_get_time_dilation(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 entityId = py_toint(py_arg(0)); const SkaEntity entity = (SkaEntity)entityId; @@ -1155,6 +1223,7 @@ bool cre_pkpy_api_node_get_time_dilation(int argc, py_StackRef argv) { bool cre_pkpy_api_node_get_total_time_dilation(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 entityId = py_toint(py_arg(0)); const SkaEntity entity = (SkaEntity)entityId; From ec6f3ac9ccd343f4c6a54b938ce1ae4f20bf6b4e Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Fri, 4 Oct 2024 23:45:29 -0400 Subject: [PATCH 28/54] Update. --- .../python/pocketpy/api/pkpy_api_impl.c | 249 ++++++++++++++++-- 1 file changed, 233 insertions(+), 16 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 8c614a90a..1c0db92c1 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -1234,22 +1234,239 @@ bool cre_pkpy_api_node_get_total_time_dilation(int argc, py_StackRef argv) { // Node2D -bool cre_pkpy_api_node2d_set_position(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_add_to_position(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_get_position(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_get_global_position(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_set_scale(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_add_to_scale(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_get_scale(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_set_rotation(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_add_to_rotation(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_get_rotation(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_set_z_index(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_get_z_index(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_set_z_index_relative_to_parent(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_get_z_index_relative_to_parent(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_set_ignore_camera(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_node2d_get_ignore_camera(int argc, py_StackRef argv) { return true; } +static void pkpy_update_entity_local_position(SkaEntity entity, SkaVector2* position) { + Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity,TRANSFORM2D_COMPONENT_INDEX); + const SkaVector2 prevPosition = transformComp->localTransform.position; + transformComp->localTransform.position.x = position->x; + transformComp->localTransform.position.y = position->y; + transformComp->isGlobalTransformDirty = true; + if (transformComp->localTransform.position.x != prevPosition.x || transformComp->localTransform.position.y != prevPosition.y) { + cre_scene_manager_notify_all_on_transform_events(entity, transformComp); + } +} + +static void pkpy_update_entity_local_scale(SkaEntity entity, SkaVector2 * scale) { + Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity,TRANSFORM2D_COMPONENT_INDEX); + const SkaVector2 prevScale = transformComp->localTransform.scale; + transformComp->localTransform.scale.x = scale->x; + transformComp->localTransform.scale.y = scale->y; + transformComp->isGlobalTransformDirty = true; + if (transformComp->localTransform.scale.x != prevScale.x || transformComp->localTransform.scale.y != prevScale.y) { + cre_scene_manager_notify_all_on_transform_events(entity, transformComp); + } +} + +static void pkpy_update_entity_local_rotation(SkaEntity entity, f32 rotation) { + Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + const f32 prevRotation = transformComp->localTransform.rotation; + transformComp->localTransform.rotation = rotation; + transformComp->isGlobalTransformDirty = true; + if (transformComp->localTransform.rotation != prevRotation) { + cre_scene_manager_notify_all_on_transform_events(entity, transformComp); + } +} + +bool cre_pkpy_api_node2d_set_position(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(0, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 posX = py_tofloat(py_arg(1)); + const f64 posY = py_tofloat(py_arg(2)); + + const SkaEntity entity = (SkaEntity)entityId; + pkpy_update_entity_local_position(entity, &(SkaVector2) { .x = (f32)posX, .y = (f32)posY }); + return true; +} + +bool cre_pkpy_api_node2d_add_to_position(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(0, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 posX = py_tofloat(py_arg(1)); + const f64 posY = py_tofloat(py_arg(2)); + + const SkaEntity entity = (SkaEntity)entityId; + const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + pkpy_update_entity_local_position(entity, &(SkaVector2) { + .x = transformComp->localTransform.position.x + (f32)posX, + .y = transformComp->localTransform.position.y + (f32)posY + }); + return true; +} + +bool cre_pkpy_api_node2d_get_position(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, (f64)transformComp->localTransform.position.x); + py_newfloat(pyY, (f64)transformComp->localTransform.position.y); + return true; +} + +bool cre_pkpy_api_node2d_get_global_position(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + SkaTransformModel2D* globalTransform = cre_scene_manager_get_scene_node_global_transform(entity, transformComp); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, (f64)globalTransform->position.x); + py_newfloat(pyY, (f64)globalTransform->position.y); + return true; +} + +bool cre_pkpy_api_node2d_set_scale(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(0, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 scaleX = py_tofloat(py_arg(1)); + const f64 scaleY = py_tofloat(py_arg(2)); + + const SkaEntity entity = (SkaEntity)entityId; + pkpy_update_entity_local_scale(entity, &(SkaVector2) { .x = (f32)scaleX, .y = (f32)scaleY }); + return true; +} + +bool cre_pkpy_api_node2d_add_to_scale(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(0, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 scaleX = py_tofloat(py_arg(1)); + const f64 scaleY = py_tofloat(py_arg(2)); + + const SkaEntity entity = (SkaEntity)entityId; + const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + pkpy_update_entity_local_scale(entity, &(SkaVector2) { .x = transformComp->localTransform.scale.x + (f32)scaleX, .y = transformComp->localTransform.scale.y + (f32)scaleY }); + return true; +} + +bool cre_pkpy_api_node2d_get_scale(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, (f64)transformComp->localTransform.scale.x); + py_newfloat(pyY, (f64)transformComp->localTransform.scale.y); + return true; +} + +bool cre_pkpy_api_node2d_set_rotation(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float);; + const py_i64 entityId = py_toint(py_arg(0)); + const f64 rotation = py_tofloat(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + pkpy_update_entity_local_rotation(entity, (f32)rotation); + return true; +} + +bool cre_pkpy_api_node2d_add_to_rotation(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float);; + const py_i64 entityId = py_toint(py_arg(0)); + const f64 rotation = py_tofloat(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + pkpy_update_entity_local_rotation(entity, transformComp->localTransform.rotation + (f32)rotation); + return true; +} + +bool cre_pkpy_api_node2d_get_rotation(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + py_newfloat(py_retval(), (f64)transformComp->localTransform.rotation); + return true; +} + +bool cre_pkpy_api_node2d_set_z_index(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_int);; + const py_i64 entityId = py_toint(py_arg(0)); + const py_i64 zIndex = py_toint(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + transformComp->zIndex = (int32)zIndex; + return true; +} + +bool cre_pkpy_api_node2d_get_z_index(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + py_newint(py_retval(), transformComp->zIndex); + return true; +} + +bool cre_pkpy_api_node2d_set_z_index_relative_to_parent(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_bool);; + const py_i64 entityId = py_toint(py_arg(0)); + const bool zIndexRelativeToParent = py_tobool(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + transformComp->isZIndexRelativeToParent = zIndexRelativeToParent; + return true; +} + +bool cre_pkpy_api_node2d_get_z_index_relative_to_parent(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + py_newbool(py_retval(), transformComp->isZIndexRelativeToParent); + return true; +} + +bool cre_pkpy_api_node2d_set_ignore_camera(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_bool);; + const py_i64 entityId = py_toint(py_arg(0)); + const bool ignoreCamera = py_tobool(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + transformComp->ignoreCamera = ignoreCamera; + return true; +} + +bool cre_pkpy_api_node2d_get_ignore_camera(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); + py_newbool(py_retval(), transformComp->ignoreCamera); + return true; +} // Sprite From f9374660ae432e2e97a9727e4c1a8752a5a5fa43 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sat, 5 Oct 2024 15:23:33 -0400 Subject: [PATCH 29/54] Updates. --- Dependencies.cmake | 2 +- crescent_py_api/pocketpy/crescent.py | 2 +- crescent_py_api/pocketpy/crescent_internal.py | 2 +- .../python/pocketpy/api/pkpy_api_impl.c | 242 ++++++++++++++++-- .../python/pocketpy/cre_pkpy_api_source.h | 2 +- 5 files changed, 223 insertions(+), 27 deletions(-) diff --git a/Dependencies.cmake b/Dependencies.cmake index 0f1aab707..4cf304d81 100644 --- a/Dependencies.cmake +++ b/Dependencies.cmake @@ -9,7 +9,7 @@ if (NOT TARGET seika) FetchContent_Declare( seika_content GIT_REPOSITORY https://github.com/Chukobyte/seika.git - GIT_TAG v0.1.5 + GIT_TAG v0.1.6 ) FetchContent_MakeAvailable(seika_content) endif () diff --git a/crescent_py_api/pocketpy/crescent.py b/crescent_py_api/pocketpy/crescent.py index 4cee6e847..9f15a70ac 100644 --- a/crescent_py_api/pocketpy/crescent.py +++ b/crescent_py_api/pocketpy/crescent.py @@ -1159,7 +1159,7 @@ def origin(self, value: Vector2) -> None: @property def shader_instance(self) -> Optional[ShaderInstance]: shader_instance_id = crescent_internal.sprite_get_shader_instance(self.entity_id) - if shader_instance_id >= 0: + if shader_instance_id: return ShaderInstance(shader_instance_id) return None diff --git a/crescent_py_api/pocketpy/crescent_internal.py b/crescent_py_api/pocketpy/crescent_internal.py index 0ae40f577..3e1f5876e 100644 --- a/crescent_py_api/pocketpy/crescent_internal.py +++ b/crescent_py_api/pocketpy/crescent_internal.py @@ -163,7 +163,7 @@ def sprite_set_origin(entity_id: int, x: float, y: float) -> None: pass -def sprite_get_shader_instance(entity_id: int) -> int: +def sprite_get_shader_instance(entity_id: int) -> Optional[int]: return 0 diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 1c0db92c1..be1c33cac 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -1268,7 +1269,7 @@ static void pkpy_update_entity_local_rotation(SkaEntity entity, f32 rotation) { bool cre_pkpy_api_node2d_set_position(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); - PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(0, tp_float); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); const py_i64 entityId = py_toint(py_arg(0)); const f64 posX = py_tofloat(py_arg(1)); const f64 posY = py_tofloat(py_arg(2)); @@ -1280,7 +1281,7 @@ bool cre_pkpy_api_node2d_set_position(int argc, py_StackRef argv) { bool cre_pkpy_api_node2d_add_to_position(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); - PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(0, tp_float); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); const py_i64 entityId = py_toint(py_arg(0)); const f64 posX = py_tofloat(py_arg(1)); const f64 posY = py_tofloat(py_arg(2)); @@ -1327,7 +1328,7 @@ bool cre_pkpy_api_node2d_get_global_position(int argc, py_StackRef argv) { bool cre_pkpy_api_node2d_set_scale(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); - PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(0, tp_float); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); const py_i64 entityId = py_toint(py_arg(0)); const f64 scaleX = py_tofloat(py_arg(1)); const f64 scaleY = py_tofloat(py_arg(2)); @@ -1339,7 +1340,7 @@ bool cre_pkpy_api_node2d_set_scale(int argc, py_StackRef argv) { bool cre_pkpy_api_node2d_add_to_scale(int argc, py_StackRef argv) { PY_CHECK_ARGC(3); - PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float); PY_CHECK_ARG_TYPE(0, tp_float); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); const py_i64 entityId = py_toint(py_arg(0)); const f64 scaleX = py_tofloat(py_arg(1)); const f64 scaleY = py_tofloat(py_arg(2)); @@ -1367,7 +1368,7 @@ bool cre_pkpy_api_node2d_get_scale(int argc, py_StackRef argv) { bool cre_pkpy_api_node2d_set_rotation(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); - PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float);; + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float);; const py_i64 entityId = py_toint(py_arg(0)); const f64 rotation = py_tofloat(py_arg(1)); @@ -1378,7 +1379,7 @@ bool cre_pkpy_api_node2d_set_rotation(int argc, py_StackRef argv) { bool cre_pkpy_api_node2d_add_to_rotation(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); - PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_float);; + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float);; const py_i64 entityId = py_toint(py_arg(0)); const f64 rotation = py_tofloat(py_arg(1)); @@ -1401,7 +1402,7 @@ bool cre_pkpy_api_node2d_get_rotation(int argc, py_StackRef argv) { bool cre_pkpy_api_node2d_set_z_index(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); - PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_int);; + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int);; const py_i64 entityId = py_toint(py_arg(0)); const py_i64 zIndex = py_toint(py_arg(1)); @@ -1424,7 +1425,7 @@ bool cre_pkpy_api_node2d_get_z_index(int argc, py_StackRef argv) { bool cre_pkpy_api_node2d_set_z_index_relative_to_parent(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); - PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_bool);; + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_bool);; const py_i64 entityId = py_toint(py_arg(0)); const bool zIndexRelativeToParent = py_tobool(py_arg(1)); @@ -1447,7 +1448,7 @@ bool cre_pkpy_api_node2d_get_z_index_relative_to_parent(int argc, py_StackRef ar bool cre_pkpy_api_node2d_set_ignore_camera(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); - PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_bool);; + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_bool);; const py_i64 entityId = py_toint(py_arg(0)); const bool ignoreCamera = py_tobool(py_arg(1)); @@ -1470,20 +1471,215 @@ bool cre_pkpy_api_node2d_get_ignore_camera(int argc, py_StackRef argv) { // Sprite -bool cre_pkpy_api_sprite_get_texture(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_set_texture(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_get_draw_source(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_set_draw_source(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_get_flip_h(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_set_flip_h(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_get_flip_v(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_set_flip_v(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_get_modulate(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_set_modulate(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_get_origin(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_set_origin(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_get_shader_instance(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_sprite_set_shader_instance(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_sprite_get_texture(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + const SkaTexture* texture = spriteComponent->texture; + py_newtuple(py_retval(), 4); + py_Ref pyFileName = py_tuple_getitem(py_retval(), 0); + py_Ref pyWrapS = py_tuple_getitem(py_retval(), 1); + py_Ref pyWrapT = py_tuple_getitem(py_retval(), 2); + py_Ref pyApplyNearestNeighbor = py_tuple_getitem(py_retval(), 3); + py_newstr(pyFileName, texture->fileName); + py_newstr(pyWrapS, ska_texture_get_wrap_s_string(texture)); + py_newstr(pyWrapT, ska_texture_get_wrap_t_string(texture)); + py_newbool(pyApplyNearestNeighbor, texture->applyNearestNeighbor); + return true; +} + +bool cre_pkpy_api_sprite_set_texture(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); + const py_i64 entityId = py_toint(py_arg(0)); + const char* texturePath = py_tostr(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + SkaTexture* texture = ska_asset_manager_get_texture(texturePath); + if (texture) { + SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + spriteComponent->texture = texture; + } else { + ska_logger_warn("Failed to find texture at path '%s'", texturePath); + } + return true; +} + +bool cre_pkpy_api_sprite_get_draw_source(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + const SkaRect2* drawSource = &spriteComponent->drawSource; + py_newtuple(py_retval(), 4); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_Ref pyW = py_tuple_getitem(py_retval(), 2); + py_Ref pyH = py_tuple_getitem(py_retval(), 3); + py_newfloat(pyX, drawSource->x); + py_newfloat(pyY, drawSource->y); + py_newfloat(pyW, drawSource->w); + py_newfloat(pyH, drawSource->h); + return true; +} + +bool cre_pkpy_api_sprite_set_draw_source(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); PY_CHECK_ARG_TYPE(4, tp_float); PY_CHECK_ARG_TYPE(5, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 x = py_tofloat(py_arg(1)); + const f64 y = py_tofloat(py_arg(2)); + const f64 w = py_tofloat(py_arg(3)); + const f64 h = py_tofloat(py_arg(4)); + + const SkaEntity entity = (SkaEntity)entityId; + SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + spriteComponent->drawSource = (SkaRect2){ (f32)x, (f32)y, (f32)w, (f32)h }; + return true; +} + +bool cre_pkpy_api_sprite_get_flip_h(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + py_newbool(py_retval(), spriteComponent->flipH); + return true; +} + +bool cre_pkpy_api_sprite_set_flip_h(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_bool); + const py_i64 entityId = py_toint(py_arg(0)); + const bool flipH = py_tobool(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + spriteComponent->flipH = flipH; + return true; +} + +bool cre_pkpy_api_sprite_get_flip_v(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + py_newbool(py_retval(), spriteComponent->flipV); + return true; +} + +bool cre_pkpy_api_sprite_set_flip_v(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_bool); + const py_i64 entityId = py_toint(py_arg(0)); + const bool flipV = py_tobool(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + spriteComponent->flipV = flipV; + return true; +} + +bool cre_pkpy_api_sprite_get_modulate(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + py_newtuple(py_retval(), 4); + py_Ref pyR = py_tuple_getitem(py_retval(), 0); + py_Ref pyG = py_tuple_getitem(py_retval(), 1); + py_Ref pyB = py_tuple_getitem(py_retval(), 2); + py_Ref pyA = py_tuple_getitem(py_retval(), 3); + py_newfloat(pyR, spriteComponent->modulate.r); + py_newfloat(pyG, spriteComponent->modulate.g); + py_newfloat(pyB, spriteComponent->modulate.b); + py_newfloat(pyA, spriteComponent->modulate.a); + return true; +} + +bool cre_pkpy_api_sprite_set_modulate(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); PY_CHECK_ARG_TYPE(2, tp_int); PY_CHECK_ARG_TYPE(3, tp_int); PY_CHECK_ARG_TYPE(4, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + const py_i64 r = py_toint(py_arg(1)); + const py_i64 g = py_toint(py_arg(2)); + const py_i64 b = py_toint(py_arg(3)); + const py_i64 a = py_toint(py_arg(4)); + + const SkaEntity entity = (SkaEntity)entityId; + SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + spriteComponent->modulate = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); + return true; +} + +bool cre_pkpy_api_sprite_get_origin(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, spriteComponent->origin.x); + py_newfloat(pyY, spriteComponent->origin.y); + return true; +} + +bool cre_pkpy_api_sprite_set_origin(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 x = py_tofloat(py_arg(1)); + const f64 y = py_tofloat(py_arg(2)); + + const SkaEntity entity = (SkaEntity)entityId; + SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + spriteComponent->origin = (SkaVector2){ (f32)x, (f32)y }; + return true; +} + +bool cre_pkpy_api_sprite_get_shader_instance(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + if (spriteComponent->shaderInstanceId != SKA_SHADER_INSTANCE_INVALID_ID) { + py_newint(py_retval(), (py_i64)spriteComponent->shaderInstanceId); + } else { + py_newnone(py_retval()); + } + return true; +} + +bool cre_pkpy_api_sprite_set_shader_instance(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + const py_i64 pyShaderInstanceId = py_toint(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + const SkaShaderInstanceId instanceId = (SkaShaderInstanceId)pyShaderInstanceId; + SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + spriteComponent->shaderInstanceId = instanceId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(instanceId); + ska_renderer_set_sprite_shader_default_params(shaderInstance->shader); + return true; +} // Animated Sprite diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h index 589f9f509..14b84a17c 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h @@ -1162,7 +1162,7 @@ " @property\n"\ " def shader_instance(self) -> Optional[ShaderInstance]:\n"\ " shader_instance_id = crescent_internal.sprite_get_shader_instance(self.entity_id)\n"\ -" if shader_instance_id >= 0:\n"\ +" if shader_instance_id:\n"\ " return ShaderInstance(shader_instance_id)\n"\ " return None\n"\ "\n"\ From 650b0113f630a7fb822e0ec9417c4d0b053923a2 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sat, 5 Oct 2024 15:27:24 -0400 Subject: [PATCH 30/54] Clean. --- engine/src/core/scripting/python/pocketpy/api/pkpy_api.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api.c index 757231cac..bbba7dc18 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api.c @@ -1,11 +1,9 @@ #include "pkpy_api.h" -#include - #include "pkpy_api_impl.h" -#include "../pkpy_util.h" -#include "../cre_pkpy.h" -#include "../cre_pkpy_api_source.h" +#include "core/scripting/python/pocketpy/pkpy_util.h" +#include "core/scripting/python/pocketpy/cre_pkpy.h" +#include "core/scripting/python/pocketpy/cre_pkpy_api_source.h" void cre_pkpy_api_load_internal_modules() { // Load crescent internal first From a87a8c1290e90cdc3eed65c96526bdbf64432c18 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 11:51:25 -0400 Subject: [PATCH 31/54] Updates. --- crescent_py_api/pocketpy/crescent.py | 2 +- crescent_py_api/pocketpy/crescent_internal.py | 2 +- .../python/pocketpy/api/pkpy_api_impl.c | 353 +++++++++++++++--- .../python/pocketpy/cre_pkpy_api_source.h | 2 +- 4 files changed, 312 insertions(+), 47 deletions(-) diff --git a/crescent_py_api/pocketpy/crescent.py b/crescent_py_api/pocketpy/crescent.py index 9f15a70ac..5944bc915 100644 --- a/crescent_py_api/pocketpy/crescent.py +++ b/crescent_py_api/pocketpy/crescent.py @@ -1230,7 +1230,7 @@ def stagger_animation_start_times(self, value: bool) -> None: @property def shader_instance(self) -> Optional[ShaderInstance]: shader_instance_id = crescent_internal.animated_sprite_get_shader_instance(self.entity_id) - if shader_instance_id >= 0: + if shader_instance_id: return ShaderInstance(shader_instance_id) return None diff --git a/crescent_py_api/pocketpy/crescent_internal.py b/crescent_py_api/pocketpy/crescent_internal.py index 3e1f5876e..fb450f609 100644 --- a/crescent_py_api/pocketpy/crescent_internal.py +++ b/crescent_py_api/pocketpy/crescent_internal.py @@ -229,7 +229,7 @@ def animated_sprite_set_origin(entity_id: int, x: float, y: float) -> None: pass -def animated_sprite_get_shader_instance(entity_id: int) -> int: +def animated_sprite_get_shader_instance(entity_id: int) -> Optional[int]: return 0 diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index be1c33cac..50e18cf39 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -1683,63 +1683,328 @@ bool cre_pkpy_api_sprite_set_shader_instance(int argc, py_StackRef argv) { // Animated Sprite -bool cre_pkpy_api_animated_sprite_play(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_stop(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_set_current_animation_frame(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_add_animation(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_get_stagger_animation_start_times(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_set_stagger_animation_start_times(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_get_flip_h(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_set_flip_h(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_get_flip_v(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_set_flip_v(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_get_modulate(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_set_modulate(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_get_origin(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_set_origin(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_get_shader_instance(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_animated_sprite_set_shader_instance(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_animated_sprite_play(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); + const py_i64 entityId = py_toint(py_arg(0)); + const char* animationName = py_tostr(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + const bool hasSuccessfullyPlayed = animated_sprite_component_play_animation(animatedSpriteComponent, animationName); + py_newbool(py_retval(), hasSuccessfullyPlayed); + return true; +} + +bool cre_pkpy_api_animated_sprite_stop(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + animatedSpriteComponent->isPlaying = false; + return true; +} + +bool cre_pkpy_api_animated_sprite_set_current_animation_frame(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + const py_i64 frame = py_toint(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + animatedSpriteComponent->currentAnimation->currentFrame = ska_math_clamp_int((int32)frame, 0, animatedSpriteComponent->currentAnimation->frameCount - 1); + return true; +} + +// TODO: Do later as it's too hard to figure out now :D +bool cre_pkpy_api_animated_sprite_add_animation(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_animated_sprite_get_stagger_animation_start_times(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + py_newbool(py_retval(), animatedSpriteComponent->staggerStartAnimationTimes); + return true; +} + +bool cre_pkpy_api_animated_sprite_set_stagger_animation_start_times(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_bool); + const py_i64 entityId = py_toint(py_arg(0)); + const bool staggerAnimationsStartTimes = py_tobool(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + animatedSpriteComponent->staggerStartAnimationTimes = staggerAnimationsStartTimes; + return true; +} + +bool cre_pkpy_api_animated_sprite_get_flip_h(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + py_newbool(py_retval(), animatedSpriteComponent->flipH); + return true; +} + +bool cre_pkpy_api_animated_sprite_set_flip_h(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_bool); + const py_i64 entityId = py_toint(py_arg(0)); + const bool flipH = py_tobool(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + animatedSpriteComponent->flipH = flipH; + return true; +} + +bool cre_pkpy_api_animated_sprite_get_flip_v(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + py_newbool(py_retval(), animatedSpriteComponent->flipV); + return true; +} + +bool cre_pkpy_api_animated_sprite_set_flip_v(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_bool); + const py_i64 entityId = py_toint(py_arg(0)); + const bool flipV = py_tobool(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + animatedSpriteComponent->flipV = flipV; + return true; +} + +bool cre_pkpy_api_animated_sprite_get_modulate(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + py_newtuple(py_retval(), 4); + py_Ref pyR = py_tuple_getitem(py_retval(), 0); + py_Ref pyG = py_tuple_getitem(py_retval(), 1); + py_Ref pyB = py_tuple_getitem(py_retval(), 2); + py_Ref pyA = py_tuple_getitem(py_retval(), 3); + py_newfloat(pyR, animatedSpriteComponent->modulate.r); + py_newfloat(pyG, animatedSpriteComponent->modulate.g); + py_newfloat(pyB, animatedSpriteComponent->modulate.b); + py_newfloat(pyA, animatedSpriteComponent->modulate.a); + return true; +} + +bool cre_pkpy_api_animated_sprite_set_modulate(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); PY_CHECK_ARG_TYPE(2, tp_int); PY_CHECK_ARG_TYPE(3, tp_int); PY_CHECK_ARG_TYPE(4, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + const py_i64 r = py_toint(py_arg(1)); + const py_i64 g = py_toint(py_arg(2)); + const py_i64 b = py_toint(py_arg(3)); + const py_i64 a = py_toint(py_arg(4)); + + const SkaEntity entity = (SkaEntity)entityId; + AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + animatedSpriteComponent->modulate = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); + return true; +} + +bool cre_pkpy_api_animated_sprite_get_origin(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, animatedSpriteComponent->origin.x); + py_newfloat(pyY, animatedSpriteComponent->origin.y); + return true; +} + +bool cre_pkpy_api_animated_sprite_set_origin(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 x = py_tofloat(py_arg(1)); + const f64 y = py_tofloat(py_arg(2)); + + const SkaEntity entity = (SkaEntity)entityId; + AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + animatedSpriteComponent->origin = (SkaVector2){ (f32)x, (f32)y }; + return true; +} + +bool cre_pkpy_api_animated_sprite_get_shader_instance(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + if (animatedSpriteComponent->shaderInstanceId != SKA_SHADER_INSTANCE_INVALID_ID) { + py_newint(py_retval(), (py_i64)animatedSpriteComponent->shaderInstanceId); + } else { + py_newnone(py_retval()); + } + return true; +} + +bool cre_pkpy_api_animated_sprite_set_shader_instance(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + const py_i64 pyShaderInstanceId = py_toint(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + const SkaShaderInstanceId instanceId = (SkaShaderInstanceId)pyShaderInstanceId; + AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + animatedSpriteComponent->shaderInstanceId = instanceId; + SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(instanceId); + ska_renderer_set_sprite_shader_default_params(shaderInstance->shader); + return true; +} // Text Label -bool cre_pkpy_api_text_label_get_text(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_text_label_set_text(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_text_label_get_color(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_text_label_set_color(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_text_label_set_font_uid(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_text_label_get_text(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_text_label_set_text(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_text_label_get_color(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_text_label_set_color(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_text_label_set_font_uid(int argc, py_StackRef argv) { + return true; +} // Collider2D -bool cre_pkpy_api_collider2d_get_extents(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_collider2d_set_extents(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_collider2d_get_color(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_collider2d_get_extents(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_collider2d_set_extents(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_collider2d_get_color(int argc, py_StackRef argv) { + return true; +} + bool cre_pkpy_api_collider2d_set_color(int argc, py_StackRef argv) { return true; } // Color Rect -bool cre_pkpy_api_color_rect_get_size(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_color_rect_set_size(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_color_rect_get_color(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_color_rect_set_color(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_color_rect_get_size(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_color_rect_set_size(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_color_rect_get_color(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_color_rect_set_color(int argc, py_StackRef argv) { + return true; +} // Parallax -bool cre_pkpy_api_parallax_get_scroll_speed(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_parallax_set_scroll_speed(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_parallax_get_scroll_speed(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_parallax_set_scroll_speed(int argc, py_StackRef argv) { + return true; +} // Particles2D -bool cre_pkpy_api_particles2d_get_amount(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_set_amount(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_get_life_time(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_set_life_time(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_get_damping(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_set_damping(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_get_explosiveness(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_set_explosiveness(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_get_color(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_set_color(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_get_initial_velocity(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_set_initial_velocity(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_get_spread(int argc, py_StackRef argv) { return true; } -bool cre_pkpy_api_particles2d_set_spread(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_particles2d_get_amount(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_set_amount(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_get_life_time(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_set_life_time(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_get_damping(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_set_damping(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_get_explosiveness(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_set_explosiveness(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_get_color(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_set_color(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_get_initial_velocity(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_set_initial_velocity(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_get_spread(int argc, py_StackRef argv) { + return true; +} + +bool cre_pkpy_api_particles2d_set_spread(int argc, py_StackRef argv) { + return true; +} diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h index 14b84a17c..8e5bf5ed7 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h @@ -1233,7 +1233,7 @@ " @property\n"\ " def shader_instance(self) -> Optional[ShaderInstance]:\n"\ " shader_instance_id = crescent_internal.animated_sprite_get_shader_instance(self.entity_id)\n"\ -" if shader_instance_id >= 0:\n"\ +" if shader_instance_id:\n"\ " return ShaderInstance(shader_instance_id)\n"\ " return None\n"\ "\n"\ From 6ea5a7d6a4513d62ec5623d02983dc05e7ae4559 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 12:14:42 -0400 Subject: [PATCH 32/54] Updates. --- .../python/pocketpy/api/pkpy_api_impl.c | 236 +++++++++++++++++- 1 file changed, 226 insertions(+), 10 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 50e18cf39..cda59f7fd 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -1471,6 +1471,22 @@ bool cre_pkpy_api_node2d_get_ignore_camera(int argc, py_StackRef argv) { // Sprite +typedef struct CreColor { + int32 r; + int32 g; + int32 b; + int32 a; +} CreColor; + +static CreColor convert_ska_color_to_cre(const SkaColor* color) { + return (CreColor){ + .r = (int32)(color->r * 255.0f), + .g = (int32)(color->g * 255.0f), + .b = (int32)(color->b * 255.0f), + .a = (int32)(color->a * 255.0f) + }; +} + bool cre_pkpy_api_sprite_get_texture(int argc, py_StackRef argv) { PY_CHECK_ARGC(1); PY_CHECK_ARG_TYPE(0, tp_int); @@ -1596,15 +1612,16 @@ bool cre_pkpy_api_sprite_get_modulate(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); + const CreColor color = convert_ska_color_to_cre(&spriteComponent->modulate); py_newtuple(py_retval(), 4); py_Ref pyR = py_tuple_getitem(py_retval(), 0); py_Ref pyG = py_tuple_getitem(py_retval(), 1); py_Ref pyB = py_tuple_getitem(py_retval(), 2); py_Ref pyA = py_tuple_getitem(py_retval(), 3); - py_newfloat(pyR, spriteComponent->modulate.r); - py_newfloat(pyG, spriteComponent->modulate.g); - py_newfloat(pyB, spriteComponent->modulate.b); - py_newfloat(pyA, spriteComponent->modulate.a); + py_newint(pyR, color.r); + py_newint(pyG, color.g); + py_newint(pyB, color.b); + py_newint(pyA, color.a); return true; } @@ -1639,7 +1656,7 @@ bool cre_pkpy_api_sprite_get_origin(int argc, py_StackRef argv) { } bool cre_pkpy_api_sprite_set_origin(int argc, py_StackRef argv) { - PY_CHECK_ARGC(5); + PY_CHECK_ARGC(3); PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); const py_i64 entityId = py_toint(py_arg(0)); const f64 x = py_tofloat(py_arg(1)); @@ -1800,15 +1817,16 @@ bool cre_pkpy_api_animated_sprite_get_modulate(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + const CreColor color = convert_ska_color_to_cre(&animatedSpriteComponent->modulate); py_newtuple(py_retval(), 4); py_Ref pyR = py_tuple_getitem(py_retval(), 0); py_Ref pyG = py_tuple_getitem(py_retval(), 1); py_Ref pyB = py_tuple_getitem(py_retval(), 2); py_Ref pyA = py_tuple_getitem(py_retval(), 3); - py_newfloat(pyR, animatedSpriteComponent->modulate.r); - py_newfloat(pyG, animatedSpriteComponent->modulate.g); - py_newfloat(pyB, animatedSpriteComponent->modulate.b); - py_newfloat(pyA, animatedSpriteComponent->modulate.a); + py_newint(pyR, color.r); + py_newint(pyG, color.g); + py_newint(pyB, color.b); + py_newint(pyA, color.a); return true; } @@ -1888,66 +1906,237 @@ bool cre_pkpy_api_animated_sprite_set_shader_instance(int argc, py_StackRef argv // Text Label bool cre_pkpy_api_text_label_get_text(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); + py_newstr(py_retval(), textLabelComponent->text); return true; } bool cre_pkpy_api_text_label_set_text(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); + const py_i64 entityId = py_toint(py_arg(0)); + const char* text = py_tostr(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); + ska_strcpy(textLabelComponent->text, text); return true; } bool cre_pkpy_api_text_label_get_color(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); + const CreColor color = convert_ska_color_to_cre(&textLabelComponent->color); + py_newtuple(py_retval(), 4); + py_Ref pyR = py_tuple_getitem(py_retval(), 0); + py_Ref pyG = py_tuple_getitem(py_retval(), 1); + py_Ref pyB = py_tuple_getitem(py_retval(), 2); + py_Ref pyA = py_tuple_getitem(py_retval(), 3); + py_newint(pyR, color.r); + py_newint(pyG, color.g); + py_newint(pyB, color.b); + py_newint(pyA, color.a); return true; } bool cre_pkpy_api_text_label_set_color(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); PY_CHECK_ARG_TYPE(2, tp_int); PY_CHECK_ARG_TYPE(3, tp_int); PY_CHECK_ARG_TYPE(4, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + const py_i64 r = py_toint(py_arg(1)); + const py_i64 g = py_toint(py_arg(2)); + const py_i64 b = py_toint(py_arg(3)); + const py_i64 a = py_toint(py_arg(4)); + + const SkaEntity entity = (SkaEntity)entityId; + TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); + textLabelComponent->color = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); return true; } bool cre_pkpy_api_text_label_set_font_uid(int argc, py_StackRef argv) { + PY_CHECK_ARGC(2); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); + const py_i64 entityId = py_toint(py_arg(0)); + const char* fontUID = py_tostr(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + SkaFont* font = ska_asset_manager_get_font(fontUID); + if (font) { + TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); + textLabelComponent->font = font; + } else { + ska_logger_warn("Failed to set font to '%s' as it doesn't exist in the asset manager!", fontUID); + } return true; } // Collider2D bool cre_pkpy_api_collider2d_get_extents(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Collider2DComponent* collider2DComponent = (Collider2DComponent*)ska_ecs_component_manager_get_component(entity, COLLIDER2D_COMPONENT_INDEX); + py_newtuple(py_retval(), 2); + py_Ref pyW = py_tuple_getitem(py_retval(), 0); + py_Ref pyH = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyW, collider2DComponent->extents.w); + py_newfloat(pyH, collider2DComponent->extents.h); return true; } bool cre_pkpy_api_collider2d_set_extents(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 w = py_tofloat(py_arg(1)); + const f64 h = py_tofloat(py_arg(2)); + + const SkaEntity entity = (SkaEntity)entityId; + Collider2DComponent* collider2DComponent = (Collider2DComponent*)ska_ecs_component_manager_get_component(entity, COLLIDER2D_COMPONENT_INDEX); + collider2DComponent->extents = (SkaSize2D){ (f32)w, (f32)h }; return true; } bool cre_pkpy_api_collider2d_get_color(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Collider2DComponent* collider2DComponent = (Collider2DComponent*)ska_ecs_component_manager_get_component(entity, COLLIDER2D_COMPONENT_INDEX); + const CreColor color = convert_ska_color_to_cre(&collider2DComponent->color); + py_newtuple(py_retval(), 4); + py_Ref pyR = py_tuple_getitem(py_retval(), 0); + py_Ref pyG = py_tuple_getitem(py_retval(), 1); + py_Ref pyB = py_tuple_getitem(py_retval(), 2); + py_Ref pyA = py_tuple_getitem(py_retval(), 3); + py_newint(pyR, color.r); + py_newint(pyG, color.g); + py_newint(pyB, color.b); + py_newint(pyA, color.a); return true; } -bool cre_pkpy_api_collider2d_set_color(int argc, py_StackRef argv) { return true; } +bool cre_pkpy_api_collider2d_set_color(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); PY_CHECK_ARG_TYPE(2, tp_int); PY_CHECK_ARG_TYPE(3, tp_int); PY_CHECK_ARG_TYPE(4, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + const py_i64 r = py_toint(py_arg(1)); + const py_i64 g = py_toint(py_arg(2)); + const py_i64 b = py_toint(py_arg(3)); + const py_i64 a = py_toint(py_arg(4)); + + const SkaEntity entity = (SkaEntity)entityId; + Collider2DComponent* collider2DComponent = (Collider2DComponent*)ska_ecs_component_manager_get_component(entity, COLLIDER2D_COMPONENT_INDEX); + collider2DComponent->color = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); + return true; +} // Color Rect bool cre_pkpy_api_color_rect_get_size(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const ColorRectComponent* colorRectComponent = (ColorRectComponent*)ska_ecs_component_manager_get_component(entity, COLOR_RECT_COMPONENT_INDEX); + py_newtuple(py_retval(), 2); + py_Ref pyW = py_tuple_getitem(py_retval(), 0); + py_Ref pyH = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyW, colorRectComponent->size.w); + py_newfloat(pyH, colorRectComponent->size.h); return true; } bool cre_pkpy_api_color_rect_set_size(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 w = py_tofloat(py_arg(1)); + const f64 h = py_tofloat(py_arg(2)); + + const SkaEntity entity = (SkaEntity)entityId; + ColorRectComponent* colorRectComponent = (ColorRectComponent*)ska_ecs_component_manager_get_component(entity, COLOR_RECT_COMPONENT_INDEX); + colorRectComponent->size = (SkaSize2D){ (f32)w, (f32)h }; return true; } bool cre_pkpy_api_color_rect_get_color(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const ColorRectComponent* colorRectComponent = (ColorRectComponent*)ska_ecs_component_manager_get_component(entity, COLOR_RECT_COMPONENT_INDEX); + const CreColor color = convert_ska_color_to_cre(&colorRectComponent->color); + py_newtuple(py_retval(), 4); + py_Ref pyR = py_tuple_getitem(py_retval(), 0); + py_Ref pyG = py_tuple_getitem(py_retval(), 1); + py_Ref pyB = py_tuple_getitem(py_retval(), 2); + py_Ref pyA = py_tuple_getitem(py_retval(), 3); + py_newint(pyR, color.r); + py_newint(pyG, color.g); + py_newint(pyB, color.b); + py_newint(pyA, color.a); return true; } bool cre_pkpy_api_color_rect_set_color(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); PY_CHECK_ARG_TYPE(2, tp_int); PY_CHECK_ARG_TYPE(3, tp_int); PY_CHECK_ARG_TYPE(4, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + const py_i64 r = py_toint(py_arg(1)); + const py_i64 g = py_toint(py_arg(2)); + const py_i64 b = py_toint(py_arg(3)); + const py_i64 a = py_toint(py_arg(4)); + + const SkaEntity entity = (SkaEntity)entityId; + ColorRectComponent* colorRectComponent = (ColorRectComponent*)ska_ecs_component_manager_get_component(entity, COLOR_RECT_COMPONENT_INDEX); + colorRectComponent->color = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); return true; } // Parallax bool cre_pkpy_api_parallax_get_scroll_speed(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const ParallaxComponent* parallaxComponent = (ParallaxComponent*)ska_ecs_component_manager_get_component(entity, PARALLAX_COMPONENT_INDEX); + py_newtuple(py_retval(), 2); + py_Ref pyX = py_tuple_getitem(py_retval(), 0); + py_Ref pyY = py_tuple_getitem(py_retval(), 1); + py_newfloat(pyX, parallaxComponent->scrollSpeed.x); + py_newfloat(pyY, parallaxComponent->scrollSpeed.y); return true; } bool cre_pkpy_api_parallax_set_scroll_speed(int argc, py_StackRef argv) { + PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 x = py_tofloat(py_arg(1)); + const f64 y = py_tofloat(py_arg(2)); + + const SkaEntity entity = (SkaEntity)entityId; + ParallaxComponent* parallaxComponent = (ParallaxComponent*)ska_ecs_component_manager_get_component(entity, PARALLAX_COMPONENT_INDEX); + parallaxComponent->scrollSpeed = (SkaVector2){ (f32)x, (f32)y }; return true; } @@ -1986,10 +2175,37 @@ bool cre_pkpy_api_particles2d_set_explosiveness(int argc, py_StackRef argv) { } bool cre_pkpy_api_particles2d_get_color(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + const CreColor color = convert_ska_color_to_cre(&particles2dComponent->color); + py_newtuple(py_retval(), 4); + py_Ref pyR = py_tuple_getitem(py_retval(), 0); + py_Ref pyG = py_tuple_getitem(py_retval(), 1); + py_Ref pyB = py_tuple_getitem(py_retval(), 2); + py_Ref pyA = py_tuple_getitem(py_retval(), 3); + py_newint(pyR, color.r); + py_newint(pyG, color.g); + py_newint(pyB, color.b); + py_newint(pyA, color.a); return true; } bool cre_pkpy_api_particles2d_set_color(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); PY_CHECK_ARG_TYPE(2, tp_int); PY_CHECK_ARG_TYPE(3, tp_int); PY_CHECK_ARG_TYPE(4, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + const py_i64 r = py_toint(py_arg(1)); + const py_i64 g = py_toint(py_arg(2)); + const py_i64 b = py_toint(py_arg(3)); + const py_i64 a = py_toint(py_arg(4)); + + const SkaEntity entity = (SkaEntity)entityId; + Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + particles2dComponent->color = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); return true; } From 43d764c916811a8eb38bd076e1838bcba1150ca1 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 12:25:07 -0400 Subject: [PATCH 33/54] Rough implementation of all api functions except for anim sprite add animation. --- .../python/pocketpy/api/pkpy_api_impl.c | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index cda59f7fd..e7c73b4a2 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -2143,34 +2143,94 @@ bool cre_pkpy_api_parallax_set_scroll_speed(int argc, py_StackRef argv) { // Particles2D bool cre_pkpy_api_particles2d_get_amount(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + py_newint(py_retval(), (py_i64)particles2dComponent->amount); return true; } bool cre_pkpy_api_particles2d_set_amount(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + const py_i64 amount = py_toint(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + particles2dComponent->amount = (int32)amount; return true; } bool cre_pkpy_api_particles2d_get_life_time(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + py_newfloat(py_retval(), (f64)particles2dComponent->lifeTime); return true; } bool cre_pkpy_api_particles2d_set_life_time(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 lifeTime = py_tofloat(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + particles2dComponent->lifeTime = (f32)lifeTime; return true; } bool cre_pkpy_api_particles2d_get_damping(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + py_newfloat(py_retval(), (f64)particles2dComponent->damping); return true; } bool cre_pkpy_api_particles2d_set_damping(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 damping = py_tofloat(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + particles2dComponent->damping = (f32)damping; return true; } bool cre_pkpy_api_particles2d_get_explosiveness(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + py_newfloat(py_retval(), (f64)particles2dComponent->explosiveness); return true; } bool cre_pkpy_api_particles2d_set_explosiveness(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 explosiveness = py_tofloat(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + particles2dComponent->lifeTime = (f32)explosiveness; return true; } @@ -2210,17 +2270,61 @@ bool cre_pkpy_api_particles2d_set_color(int argc, py_StackRef argv) { } bool cre_pkpy_api_particles2d_get_initial_velocity(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + py_newtuple(py_retval(), 4); + py_Ref pyMinX = py_tuple_getitem(py_retval(), 0); + py_Ref pyMinY = py_tuple_getitem(py_retval(), 1); + py_Ref pyMaxX = py_tuple_getitem(py_retval(), 2); + py_Ref pyMaxY = py_tuple_getitem(py_retval(), 3); + py_newfloat(pyMinX, (f64)particles2dComponent->initialVelocity.min.x); + py_newfloat(pyMinY, (f64)particles2dComponent->initialVelocity.min.y); + py_newfloat(pyMaxX, (f64)particles2dComponent->initialVelocity.max.x); + py_newfloat(pyMaxY, (f64)particles2dComponent->initialVelocity.max.y); return true; } bool cre_pkpy_api_particles2d_set_initial_velocity(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); PY_CHECK_ARG_TYPE(2, tp_float); PY_CHECK_ARG_TYPE(3, tp_float); PY_CHECK_ARG_TYPE(4, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 minX = py_tofloat(py_arg(1)); + const f64 minY = py_tofloat(py_arg(2)); + const f64 maxX = py_tofloat(py_arg(3)); + const f64 maxY = py_tofloat(py_arg(4)); + + const SkaEntity entity = (SkaEntity)entityId; + Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + particles2dComponent->initialVelocity = (SkaMinMaxVec2) { + .min = { .x = (f32)minX, .y = (f32)minY }, + .max = { .x = (f32)maxX, .y = (f32)maxY } + }; return true; } bool cre_pkpy_api_particles2d_get_spread(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); + const py_i64 entityId = py_toint(py_arg(0)); + + const SkaEntity entity = (SkaEntity)entityId; + const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + py_newfloat(py_retval(), (f64)particles2dComponent->spread); return true; } bool cre_pkpy_api_particles2d_set_spread(int argc, py_StackRef argv) { + PY_CHECK_ARGC(1); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_float); + const py_i64 entityId = py_toint(py_arg(0)); + const f64 spread = py_tofloat(py_arg(1)); + + const SkaEntity entity = (SkaEntity)entityId; + Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); + particles2dComponent->spread = (f32)spread; return true; } From a1ea39f0e06df8d7bf82efaad93bbffba2f496d5 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 12:28:40 -0400 Subject: [PATCH 34/54] Update. --- engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index e7c73b4a2..e5fd56c51 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -56,8 +56,6 @@ static inline SkaVector2 cre_pkpy_api_helper_mouse_get_global_position(const Ska return mouseWorldPos; } -// TODO: Check parameter types to ensure the correct parameters are passed into the api functions - // Shader Instance bool cre_pkpy_api_shader_instance_delete(int argc, py_StackRef argv) { From cc78ea0cbf60dbed6040afed1ed17cda4a1e3fe4 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 13:58:44 -0400 Subject: [PATCH 35/54] Deleted stuff. --- .../python/pocketpy/api/cre_pkpy_api.c | 1335 ----------------- .../python/pocketpy/api/cre_pkpy_api.h | 6 - .../python/pocketpy/api/cre_pkpy_api_node.c | 1270 ---------------- .../python/pocketpy/api/cre_pkpy_api_node.h | 110 -- .../pocketpy/cre_pkpy_entity_instance_cache.c | 106 -- .../pocketpy/cre_pkpy_entity_instance_cache.h | 16 - .../python/pocketpy/cre_pkpy_node_event.h | 9 - .../pocketpy/cre_pkpy_node_event_manager.c | 92 -- .../pocketpy/cre_pkpy_node_event_manager.h | 23 - .../python/pocketpy/cre_pkpy_script_context.c | 300 ---- .../python/pocketpy/cre_pkpy_script_context.h | 14 - .../scripting/python/pocketpy/cre_pkpy_util.c | 70 - .../scripting/python/pocketpy/cre_pkpy_util.h | 24 - 13 files changed, 3375 deletions(-) delete mode 100644 engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.c delete mode 100644 engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.h delete mode 100644 engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.c delete mode 100644 engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.h delete mode 100644 engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.c delete mode 100644 engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.h delete mode 100644 engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event.h delete mode 100644 engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.c delete mode 100644 engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.h delete mode 100644 engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.c delete mode 100644 engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.h delete mode 100644 engine/src/core/scripting/python/pocketpy/cre_pkpy_util.c delete mode 100644 engine/src/core/scripting/python/pocketpy/cre_pkpy_util.h diff --git a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.c b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.c deleted file mode 100644 index e319f2ea2..000000000 --- a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.c +++ /dev/null @@ -1,1335 +0,0 @@ -#include "cre_pkpy_api.h" -/* -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "cre_pkpy_api_node.h" -#include "../cre_pkpy.h" -#include "../cre_pkpy_util.h" -#include "../cre_pkpy_entity_instance_cache.h" -#include "../cre_pkpy_api_source.h" -#include "../cre_pkpy_script_context.h" -#include "../../../../engine_context.h" -#include "../../../../world.h" -#include "../../../../game_properties.h" -#include "../../../../scene/scene_utils.h" -#include "../../../../scene/scene_template_cache.h" -#include "../../../../ecs/ecs_manager.h" -#include "../../../../camera/camera.h" -#include "../../../../camera/camera_manager.h" -#include "../../../../physics/collision/collision.h" -#include "../../../../ecs/ecs_globals.h" -#include "../../../../ecs/components/script_component.h" - -// Shader Instance -static int cre_pkpy_api_shader_instance_delete(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_create_bool_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_set_bool_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_get_bool_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_create_int_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_set_int_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_get_int_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_create_float_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_set_float_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_get_float_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_create_float2_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_set_float2_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_get_float2_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_create_float3_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_set_float3_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_get_float3_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_create_float4_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_set_float4_param(pkpy_vm* vm); -static int cre_pkpy_api_shader_instance_get_float4_param(pkpy_vm* vm); - -// Shader Util -static int cre_pkpy_api_shader_util_compile_shader(pkpy_vm* vm); -static int cre_pkpy_api_shader_util_compile_shader_raw(pkpy_vm* vm); -static int cre_pkpy_api_shader_util_set_screen_shader(pkpy_vm* vm); -static int cre_pkpy_api_shader_util_get_current_screen_shader(pkpy_vm* vm); -static int cre_pkpy_api_shader_util_reset_screen_shader_to_default(pkpy_vm* vm); - -// Engine -static int cre_pkpy_api_engine_exit(pkpy_vm* vm); -static int cre_pkpy_api_engine_set_target_fps(pkpy_vm* vm); -static int cre_pkpy_api_engine_get_target_fps(pkpy_vm* vm); -static int cre_pkpy_api_engine_get_average_fps(pkpy_vm* vm); -static int cre_pkpy_api_engine_set_fps_display_enabled(pkpy_vm* vm); -static int cre_pkpy_api_engine_get_global_physics_delta_time(pkpy_vm* vm); - -// Input -static int cre_pkpy_api_input_is_key_pressed(pkpy_vm* vm); -static int cre_pkpy_api_input_is_key_just_pressed(pkpy_vm* vm); -static int cre_pkpy_api_input_is_key_just_released(pkpy_vm* vm); -static int cre_pkpy_api_input_add_action(pkpy_vm* vm); -static int cre_pkpy_api_input_is_action_pressed(pkpy_vm* vm); -static int cre_pkpy_api_input_is_action_just_pressed(pkpy_vm* vm); -static int cre_pkpy_api_input_is_action_just_released(pkpy_vm* vm); -static int cre_pkpy_api_input_start_gamepad_vibration(pkpy_vm* vm); -static int cre_pkpy_api_input_stop_gamepad_vibration(pkpy_vm* vm); -static int cre_pkpy_api_input_mouse_get_position(pkpy_vm* vm); -static int cre_pkpy_api_input_mouse_get_world_position(pkpy_vm* vm); - -// Scene Tree -static int cre_pkpy_api_scene_tree_change_scene(pkpy_vm* vm); -static int cre_pkpy_api_scene_tree_get_root(pkpy_vm* vm); - -// Scene Manager -static int cre_pkpy_api_scene_manager_process_queued_creation_entities(pkpy_vm* vm); -static int cre_pkpy_api_scene_manager_process_queued_scene_change(pkpy_vm* vm); - -// Game Properties -static int cre_pkpy_api_game_properties_get(pkpy_vm* vm); - -// Camera2D -static int cre_pkpy_api_camera2d_set_position(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_add_to_position(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_get_position(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_set_offset(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_add_to_offset(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_get_offset(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_set_zoom(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_add_to_zoom(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_get_zoom(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_set_boundary(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_get_boundary(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_follow_node(pkpy_vm* vm); -static int cre_pkpy_api_camera2d_unfollow_node(pkpy_vm* vm); - -// World -static int cre_pkpy_api_world_set_time_dilation(pkpy_vm* vm); -static int cre_pkpy_api_world_get_time_dilation(pkpy_vm* vm); -static int cre_pkpy_api_world_get_delta_time(pkpy_vm* vm); -static int cre_pkpy_api_world_get_variable_delta_time(pkpy_vm* vm); - -// Audio Source -static int cre_pkpy_api_audio_source_set_pitch(pkpy_vm* vm); -static int cre_pkpy_api_audio_source_get_pitch(pkpy_vm* vm); - -// Audio Manager -static int cre_pkpy_api_audio_manager_play_sound(pkpy_vm* vm); -static int cre_pkpy_api_audio_manager_stop_sound(pkpy_vm* vm); - -// Game Config -static int cre_pkpy_api_game_config_save(pkpy_vm* vm); -static int cre_pkpy_api_game_config_load(pkpy_vm* vm); - -// Packed Scene -static int cre_pkpy_api_packed_scene_create_instance(pkpy_vm* vm); -static int cre_pkpy_api_packed_scene_load(pkpy_vm* vm); - -// Collision Handler -static int cre_pkpy_api_collision_handler_process_collisions(pkpy_vm* vm); -static int cre_pkpy_api_collision_handler_process_mouse_collisions(pkpy_vm* vm); - -// Network -static int cre_pkpy_api_network_is_server(pkpy_vm* vm); -// Server -static int cre_pkpy_api_server_start(pkpy_vm* vm); -static int cre_pkpy_api_server_stop(pkpy_vm* vm); -static int cre_pkpy_api_server_send(pkpy_vm* vm); -// Client -static int cre_pkpy_api_client_start(pkpy_vm* vm); -static int cre_pkpy_api_client_stop(pkpy_vm* vm); -static int cre_pkpy_api_client_send(pkpy_vm* vm); - -void cre_pkpy_api_load_internal_modules(pkpy_vm* vm) { - // Load internal first - cre_pkpy_util_create_module(vm, &(CrePPModule) { - .name = "crescent_internal", - .functions = { - // Shader Instance - {.signature = "shader_instance_delete(shader_id: int) -> bool", .function = cre_pkpy_api_shader_instance_delete}, - {.signature = "shader_instance_create_bool_param(shader_id: int, initial_value: bool) -> None", .function = cre_pkpy_api_shader_instance_create_bool_param}, - {.signature = "shader_instance_set_bool_param(shader_id: int, name: str, value: bool) -> None", .function = cre_pkpy_api_shader_instance_set_bool_param}, - {.signature = "shader_instance_get_bool_param(shader_id: int, name: str) -> bool", .function = cre_pkpy_api_shader_instance_get_bool_param}, - {.signature = "shader_instance_create_int_param(shader_id: int, initial_value: int) -> None", .function = cre_pkpy_api_shader_instance_create_int_param}, - {.signature = "shader_instance_set_int_param(shader_id: int, name: str, value: int) -> None", .function = cre_pkpy_api_shader_instance_set_int_param}, - {.signature = "shader_instance_get_int_param(shader_id: int, name: str) -> int", .function = cre_pkpy_api_shader_instance_get_int_param}, - {.signature = "shader_instance_create_float_param(shader_id: int, initial_value: float) -> None", .function = cre_pkpy_api_shader_instance_create_float_param}, - {.signature = "shader_instance_set_float_param(shader_id: int, name: str, value: float) -> None", .function = cre_pkpy_api_shader_instance_set_float_param}, - {.signature = "shader_instance_get_float_param(shader_id: int, name: str) -> float", .function = cre_pkpy_api_shader_instance_get_float_param}, - {.signature = "shader_instance_create_float2_param(shader_id: int, initial_value_x: float, initial_value_y: float) -> None", .function = cre_pkpy_api_shader_instance_create_float2_param}, - {.signature = "shader_instance_set_float2_param(shader_id: int, name: str, value_x: float, value_y: float) -> None", .function = cre_pkpy_api_shader_instance_set_float2_param}, - {.signature = "shader_instance_get_float2_param(shader_id: int, name: str) -> Tuple[float, float]", .function = cre_pkpy_api_shader_instance_get_float2_param}, - {.signature = "shader_instance_create_float3_param(shader_id: int, initial_value_x: float, initial_value_y: float, initial_value_z: float) -> None", .function = cre_pkpy_api_shader_instance_create_float3_param}, - {.signature = "shader_instance_set_float3_param(shader_id: int, name: str, value_x: float, value_y: float, value_z: float) -> None", .function = cre_pkpy_api_shader_instance_set_float3_param}, - {.signature = "shader_instance_get_float3_param(shader_id: int, name: str) -> Tuple[float, float, float]", .function = cre_pkpy_api_shader_instance_get_float3_param}, - {.signature = "shader_instance_create_float4_param(shader_id: int, initial_value_x: float, initial_value_y: float, initial_value_z: float, initial_value_w: float) -> None", .function = cre_pkpy_api_shader_instance_create_float4_param}, - {.signature = "shader_instance_set_float4_param(shader_id: int, name: str, value_x: float, value_y: float, value_z: float, value_w: float) -> None", .function = cre_pkpy_api_shader_instance_set_float4_param}, - {.signature = "shader_instance_get_float4_param(shader_id: int, name: str) -> Tuple[float, float, float, float]", .function = cre_pkpy_api_shader_instance_get_float4_param}, - // Shader Util - {.signature = "shader_util_compile_shader(shader_path: str) -> int", .function = cre_pkpy_api_shader_util_compile_shader}, - {.signature = "shader_util_compile_shader_raw(vertex_path, fragment_path) -> int", .function = cre_pkpy_api_shader_util_compile_shader_raw}, - {.signature = "shader_util_set_screen_shader(shader_path: str) -> bool", .function = cre_pkpy_api_shader_util_set_screen_shader}, - {.signature = "shader_util_get_current_screen_shader() -> int", .function = cre_pkpy_api_shader_util_get_current_screen_shader}, - {.signature = "shader_util_reset_screen_shader_to_default() -> None", .function = cre_pkpy_api_shader_util_reset_screen_shader_to_default}, - // Engine - {.signature = "engine_exit(code: int) -> None", .function = cre_pkpy_api_engine_exit}, - {.signature = "engine_set_target_fps(fps: int) -> None", .function = cre_pkpy_api_engine_set_target_fps}, - {.signature = "engine_get_target_fps() -> int", .function = cre_pkpy_api_engine_get_target_fps}, - {.signature = "engine_get_average_fps() -> int", .function = cre_pkpy_api_engine_get_average_fps}, - {.signature = "engine_set_fps_display_enabled(enabled: bool, font_uid: str, position_x: float, position_y: float) -> None", .function = cre_pkpy_api_engine_set_fps_display_enabled}, - {.signature = "engine_get_global_physics_delta_time() -> float", .function = cre_pkpy_api_engine_get_global_physics_delta_time}, - // Input - {.signature = "input_is_key_pressed(key: int) -> bool", .function = cre_pkpy_api_input_is_key_pressed}, - {.signature = "input_is_key_just_pressed(key: int) -> bool", .function = cre_pkpy_api_input_is_key_just_pressed}, - {.signature = "input_is_key_just_released(key: int) -> bool", .function = cre_pkpy_api_input_is_key_just_released}, - {.signature = "input_add_action(name: str, value_key: int, device_id: int) -> None", .function = cre_pkpy_api_input_add_action}, - {.signature = "input_is_action_pressed(name: str) -> bool", .function = cre_pkpy_api_input_is_action_pressed}, - {.signature = "input_is_action_just_pressed(name: str) -> bool", .function = cre_pkpy_api_input_is_action_just_pressed}, - {.signature = "input_is_action_just_released(name: str) -> bool", .function = cre_pkpy_api_input_is_action_just_released}, - {.signature = "input_start_gamepad_vibration(device_id: int, weak_magnitude: float, strong_magnitude: float, duration: float) -> None", .function = cre_pkpy_api_input_start_gamepad_vibration}, - {.signature = "input_stop_gamepad_vibration(device_id: int) -> None", .function = cre_pkpy_api_input_stop_gamepad_vibration}, - {.signature = "input_mouse_get_position() -> Tuple[float, float]", .function = cre_pkpy_api_input_mouse_get_position}, - {.signature = "input_mouse_get_world_position() -> Tuple[float, float]", .function = cre_pkpy_api_input_mouse_get_world_position}, - // Node - {.signature = "node_new(class_path: str, class_name: str, node_type_flag: int) -> \"Node\"", .function = cre_pkpy_api_node_new}, - {.signature = "node_get_name(entity_id: int) -> str", .function = cre_pkpy_api_node_get_name}, - {.signature = "node_add_child(parent_entity_id: int, child_entity_id: int) -> None", .function = cre_pkpy_api_node_add_child}, - {.signature = "node_get_child(parent_entity_id: int, child_entity_name: str) -> Optional[\"Node\"]", .function = cre_pkpy_api_node_get_child}, - {.signature = "node_get_children(entity_id: int) -> Tuple[\"Node\", ...]", .function = cre_pkpy_api_node_get_children}, - {.signature = "node_get_parent(child_entity_id: int) -> Optional[\"Node\"]", .function = cre_pkpy_api_node_get_parent}, - {.signature = "node_queue_deletion(entity_id: int) -> None", .function = cre_pkpy_api_node_queue_deletion}, - {.signature = "node_is_queued_for_deletion(entity_id: int) -> bool", .function = cre_pkpy_api_node_is_queued_for_deletion}, - {.signature = "node_set_time_dilation(entity_id: int, dilation: float) -> None", .function = cre_pkpy_api_node_set_time_dilation}, - {.signature = "node_get_time_dilation(entity_id: int) -> float", .function = cre_pkpy_api_node_get_time_dilation}, - {.signature = "node_get_total_time_dilation(entity_id: int) -> float", .function = cre_pkpy_api_node_get_total_time_dilation}, - // Node2D - {.signature = "node2d_set_position(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_node2d_set_position}, - {.signature = "node2d_add_to_position(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_node2d_add_to_position}, - {.signature = "node2d_get_position(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_node2d_get_position}, - {.signature = "node2d_get_global_position(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_node2d_get_global_position}, - {.signature = "node2d_set_scale(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_node2d_set_scale}, - {.signature = "node2d_add_to_scale(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_node2d_add_to_scale}, - {.signature = "node2d_get_scale(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_node2d_get_scale}, - {.signature = "node2d_set_rotation(entity_id: int, rotation: float) -> None", .function = cre_pkpy_api_node2d_set_rotation}, - {.signature = "node2d_add_to_rotation(entity_id: int, rotation: float) -> None", .function = cre_pkpy_api_node2d_add_to_rotation}, - {.signature = "node2d_get_rotation(entity_id: int) -> float", .function = cre_pkpy_api_node2d_get_rotation}, - {.signature = "node2d_set_z_index(entity_id: int, z_index: int) -> None", .function = cre_pkpy_api_node2d_set_z_index}, - {.signature = "node2d_get_z_index(entity_id: int) -> int", .function = cre_pkpy_api_node2d_get_z_index}, - {.signature = "node2d_set_z_index_relative_to_parent(entity_id: int, is_relative: bool) -> None", .function = cre_pkpy_api_node2d_set_z_index_relative_to_parent}, - {.signature = "node2d_get_z_index_relative_to_parent(entity_id: int) -> bool", .function = cre_pkpy_api_node2d_get_z_index_relative_to_parent}, - {.signature = "node2d_set_ignore_camera(entity_id: int, ignore_camera: bool) -> None", .function = cre_pkpy_api_node2d_set_ignore_camera}, - {.signature = "node2d_get_ignore_camera(entity_id: int) -> bool", .function = cre_pkpy_api_node2d_get_ignore_camera}, - // Sprite - {.signature = "sprite_get_texture(entity_id: int) -> Tuple[str, str, str, bool]", .function = cre_pkpy_api_sprite_get_texture}, - {.signature = "sprite_set_texture(entity_id: int, file_path: str) -> None", .function = cre_pkpy_api_sprite_set_texture}, - {.signature = "sprite_get_draw_source(entity_id: int) -> Tuple[float, float, float, float]", .function = cre_pkpy_api_sprite_get_draw_source}, - {.signature = "sprite_set_draw_source(entity_id: int, x: float, y: float, w: float, h: float) -> None", .function = cre_pkpy_api_sprite_set_draw_source}, - {.signature = "sprite_get_flip_h(entity_id: int) -> bool", .function = cre_pkpy_api_sprite_get_flip_h}, - {.signature = "sprite_set_flip_h(entity_id: int, flip_h: bool) -> None", .function = cre_pkpy_api_sprite_set_flip_h}, - {.signature = "sprite_get_flip_v(entity_id: int) -> bool", .function = cre_pkpy_api_sprite_get_flip_v}, - {.signature = "sprite_set_flip_v(entity_id: int, flip_v: bool) -> None", .function = cre_pkpy_api_sprite_set_flip_v}, - {.signature = "sprite_get_modulate(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_sprite_get_modulate}, - {.signature = "sprite_set_modulate(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_sprite_set_modulate}, - {.signature = "sprite_get_origin(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_sprite_get_origin}, - {.signature = "sprite_set_origin(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_sprite_set_origin}, - {.signature = "sprite_get_shader_instance(entity_id: int) -> int", .function = cre_pkpy_api_sprite_get_shader_instance}, - {.signature = "sprite_set_shader_instance(entity_id: int, shader_instance_id: int) -> None", .function = cre_pkpy_api_sprite_set_shader_instance}, - // Animated Sprite - {.signature = "animated_sprite_play(entity_id: int, animation_name: str) -> bool", .function = cre_pkpy_api_animated_sprite_play}, - {.signature = "animated_sprite_stop(entity_id: int) -> None", .function = cre_pkpy_api_animated_sprite_stop}, - {.signature = "animated_sprite_set_current_animation_frame(entity_id: int, frame: int) -> None", .function = cre_pkpy_api_animated_sprite_set_current_animation_frame}, - {.signature = "animated_sprite_add_animation(entity_id: int, name: str, speed: float, does_loop: bool, frame_count: int, *args) -> None", .function = cre_pkpy_api_animated_sprite_add_animation}, - {.signature = "animated_sprite_get_stagger_animation_start_times(entity_id: int) -> bool", .function = cre_pkpy_api_animated_sprite_get_stagger_animation_start_times}, - {.signature = "animated_sprite_set_stagger_animation_start_times(entity_id: int, stagger: bool) -> None", .function = cre_pkpy_api_animated_sprite_set_stagger_animation_start_times}, - {.signature = "animated_sprite_get_flip_h(entity_id: int) -> bool", .function = cre_pkpy_api_animated_sprite_get_flip_h}, - {.signature = "animated_sprite_set_flip_h(entity_id: int, flip_h: bool) -> None", .function = cre_pkpy_api_animated_sprite_set_flip_h}, - {.signature = "animated_sprite_get_flip_v(entity_id: int) -> bool", .function = cre_pkpy_api_animated_sprite_get_flip_v}, - {.signature = "animated_sprite_set_flip_v(entity_id: int, flip_v: bool) -> None", .function = cre_pkpy_api_animated_sprite_set_flip_v}, - {.signature = "animated_sprite_get_modulate(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_animated_sprite_get_modulate}, - {.signature = "animated_sprite_set_modulate(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_animated_sprite_set_modulate}, - {.signature = "animated_sprite_get_origin(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_animated_sprite_get_origin}, - {.signature = "animated_sprite_set_origin(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_animated_sprite_set_origin}, - {.signature = "animated_sprite_get_shader_instance(entity_id: int) -> int", .function = cre_pkpy_api_animated_sprite_get_shader_instance}, - {.signature = "animated_sprite_set_shader_instance(entity_id: int, shader_instance_id: int) -> None", .function = cre_pkpy_api_animated_sprite_set_shader_instance}, - // Text Label - {.signature = "text_label_get_text(entity_id: int) -> str", .function = cre_pkpy_api_text_label_get_text}, - {.signature = "text_label_set_text(entity_id: int, text: str) -> None", .function = cre_pkpy_api_text_label_set_text}, - {.signature = "text_label_get_color(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_text_label_get_color}, - {.signature = "text_label_set_color(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_text_label_set_color}, - {.signature = "text_label_set_font_uid(entity_id: int, uid: str) -> None", .function = cre_pkpy_api_text_label_set_font_uid}, - // Collider2D - {.signature = "collider2d_get_extents(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_collider2d_get_extents}, - {.signature = "collider2d_set_extents(entity_id: int, w: float, h: float)", .function = cre_pkpy_api_collider2d_set_extents}, - {.signature = "collider2d_get_color(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_collider2d_get_color}, - {.signature = "collider2d_set_color(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_collider2d_set_color}, - // Color Rect - {.signature = "color_rect_get_size(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_color_rect_get_size}, - {.signature = "color_rect_set_size(entity_id: int, w: float, h: float) -> None", .function = cre_pkpy_api_color_rect_set_size}, - {.signature = "color_rect_get_color(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_color_rect_get_color}, - {.signature = "color_rect_set_color(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_color_rect_set_color}, - // Parallax - {.signature = "parallax_get_scroll_speed(entity_id: int) -> Tuple[float, float]", .function = cre_pkpy_api_parallax_get_scroll_speed}, - {.signature = "parallax_set_scroll_speed(entity_id: int, x: float, y: float) -> None", .function = cre_pkpy_api_parallax_set_scroll_speed}, - // Particles2D - {.signature = "particles2d_get_amount(entity_id: int) -> int", .function = cre_pkpy_api_particles2d_get_amount}, - {.signature = "particles2d_set_amount(entity_id: int, amount: int) -> None", .function = cre_pkpy_api_particles2d_set_amount}, - {.signature = "particles2d_get_life_time(entity_id: int) -> float", .function = cre_pkpy_api_particles2d_get_life_time}, - {.signature = "particles2d_set_life_time(entity_id: int, life_time: float) -> None", .function = cre_pkpy_api_particles2d_set_life_time}, - {.signature = "particles2d_set_damping(entity_id: int, damping: float) -> None", .function = cre_pkpy_api_particles2d_set_damping}, - {.signature = "particles2d_get_damping(entity_id: int) -> float", .function = cre_pkpy_api_particles2d_get_damping}, - {.signature = "particles2d_set_explosiveness(entity_id: int, explosiveness: float) -> None", .function = cre_pkpy_api_particles2d_set_explosiveness}, - {.signature = "particles2d_get_explosiveness(entity_id: int) -> float", .function = cre_pkpy_api_particles2d_get_explosiveness}, - {.signature = "particles2d_get_color(entity_id: int) -> Tuple[int, int, int, int]", .function = cre_pkpy_api_particles2d_get_color}, - {.signature = "particles2d_set_color(entity_id: int, r: int, g: int, b: int, a: int) -> None", .function = cre_pkpy_api_particles2d_set_color}, - {.signature = "particles2d_get_initial_velocity(entity_id: int) -> Tuple[float, float, float, float]", .function = cre_pkpy_api_particles2d_get_initial_velocity}, - {.signature = "particles2d_set_initial_velocity(entity_id: int, min_x: float, min_y: float, max_x: float, max_y: float) -> None", .function = cre_pkpy_api_particles2d_set_initial_velocity}, - {.signature = "particles2d_get_spread(entity_id: int) -> float", .function = cre_pkpy_api_particles2d_get_spread}, - {.signature = "particles2d_set_spread(entity_id: int, spread: float) -> None", .function = cre_pkpy_api_particles2d_set_spread}, - // Scene Tree - {.signature = "scene_tree_change_scene(path: str) -> None", .function = cre_pkpy_api_scene_tree_change_scene}, - {.signature = "scene_tree_get_root()", .function = cre_pkpy_api_scene_tree_get_root}, - // Scene Manager - {.signature = "_scene_manager_process_queued_creation_entities() -> None", .function = cre_pkpy_api_scene_manager_process_queued_creation_entities}, - {.signature = "_scene_manager_process_queued_scene_change() -> None", .function = cre_pkpy_api_scene_manager_process_queued_scene_change}, - // Game Properties - {.signature = "game_properties_get() -> Tuple[str, int, int, int, int, int, str, bool]", .function = cre_pkpy_api_game_properties_get}, - // Camera2D - {.signature = "camera2d_set_position(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_set_position}, - {.signature = "camera2d_add_to_position(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_add_to_position}, - {.signature = "camera2d_get_position() -> Tuple[float, float]", .function = cre_pkpy_api_camera2d_get_position}, - {.signature = "camera2d_set_offset(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_set_offset}, - {.signature = "camera2d_add_to_offset(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_add_to_offset}, - {.signature = "camera2d_get_offset() -> Tuple[float, float]", .function = cre_pkpy_api_camera2d_get_offset}, - {.signature = "camera2d_set_zoom(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_set_zoom}, - {.signature = "camera2d_add_to_zoom(x: float, y: float) -> None", .function = cre_pkpy_api_camera2d_add_to_zoom}, - {.signature = "camera2d_get_zoom() -> Tuple[float, float]", .function = cre_pkpy_api_camera2d_get_zoom}, - {.signature = "camera2d_set_boundary(x: float, y: float, w: float, h: float) -> None", .function = cre_pkpy_api_camera2d_set_boundary}, - {.signature = "camera2d_get_boundary() -> Tuple[float, float, float, float]", .function = cre_pkpy_api_camera2d_get_boundary}, - {.signature = "camera2d_follow_node(entity_id: int) -> None", .function = cre_pkpy_api_camera2d_follow_node}, - {.signature = "camera2d_unfollow_node(entity_id: int) -> None", .function = cre_pkpy_api_camera2d_unfollow_node}, - // World - {.signature = "world_set_time_dilation(time_dilation: float) -> None", .function = cre_pkpy_api_world_set_time_dilation}, - {.signature = "world_get_time_dilation() -> float", .function = cre_pkpy_api_world_get_time_dilation}, - {.signature = "world_get_delta_time() -> float", .function = cre_pkpy_api_world_get_delta_time}, - {.signature = "world_get_variable_delta_time() -> float", .function = cre_pkpy_api_world_get_variable_delta_time}, - // Audio Source - {.signature = "audio_source_set_pitch(path: str, pitch: float) -> None", .function = cre_pkpy_api_audio_source_set_pitch}, - {.signature = "audio_source_get_pitch(path: str) -> float", .function = cre_pkpy_api_audio_source_get_pitch}, - // Audio Manager - {.signature = "audio_manager_play_sound(path: str, loops: bool) -> None", .function = cre_pkpy_api_audio_manager_play_sound}, - {.signature = "audio_manager_stop_sound(path: str) -> None", .function = cre_pkpy_api_audio_manager_stop_sound}, - // Game Config - {.signature = "game_config_save(path: str, json_text: str, encryption_key: str) -> bool", .function = cre_pkpy_api_game_config_save}, - {.signature = "game_config_load(path, encryption_key) -> str", .function = cre_pkpy_api_game_config_load}, - // Packed Scene - {.signature = "packed_scene_create_instance(scene_cache_id: int) -> \"Node\"", .function = cre_pkpy_api_packed_scene_create_instance}, - {.signature = "packed_scene_load(path: str) -> int", .function = cre_pkpy_api_packed_scene_load}, - // Collision Handler - {.signature = "collision_handler_process_collisions(entity_id: float) -> Tuple[\"Node\", ...]", .function = cre_pkpy_api_collision_handler_process_collisions}, - {.signature = "collision_handler_process_mouse_collisions(pos_offset_x: float, pos_offset_y: float, collision_size_w: float, collision_size_h: float) -> Tuple[\"Node\", ...]", .function = cre_pkpy_api_collision_handler_process_mouse_collisions}, - // Network - {.signature = "network_is_server() -> bool", .function = cre_pkpy_api_network_is_server}, - // Server - {.signature = "server_start(port: int) -> None", .function = cre_pkpy_api_server_start}, - {.signature = "server_stop() -> None", .function = cre_pkpy_api_server_stop}, - {.signature = "server_send(message: str) -> None", .function = cre_pkpy_api_server_send}, - // Client - {.signature = "client_start(host: str, port: int) -> None", .function = cre_pkpy_api_client_start}, - {.signature = "client_stop() -> None", .function = cre_pkpy_api_client_stop}, - {.signature = "client_send(message: str) -> None", .function = cre_pkpy_api_client_send}, - - { NULL, NULL }, - } - }); - - // Now load front facing api - cre_pkpy_util_create_from_string(vm, CRE_PKPY_MODULE_NAME_CRESCENT, CRE_PKPY_CRESCENT_SOURCE); -} - -// Helper functions -static inline SkaVector2 cre_pkpy_api_helper_mouse_get_global_position(const SkaVector2* offset) { - SkaMouse* globalMouse = ska_input_get_mouse(); - const CRECamera2D* camera = cre_camera_manager_get_current_camera(); - CREGameProperties* gameProps = cre_game_props_get(); - SkaRenderContext* renderContext = ska_render_context_get(); - const SkaVector2 mouse_pixel_coord = { - ska_math_map_to_range(globalMouse->position.x, 0.0f, (float) renderContext->windowWidth, 0.0f, (float) gameProps->resolutionWidth), - ska_math_map_to_range(globalMouse->position.y, 0.0f, (float) renderContext->windowHeight, 0.0f, (float) gameProps->resolutionHeight) - }; - const SkaVector2 mouseWorldPos = { - (camera->viewport.x + camera->offset.x + mouse_pixel_coord.x + offset->x) * camera->zoom.x, - (camera->viewport.y + camera->offset.y + mouse_pixel_coord.y + offset->y) * camera->zoom.y - }; - return mouseWorldPos; -} - -//--- SHADER INSTANCE ---// - -int cre_pkpy_api_shader_instance_delete(pkpy_vm* vm) { - int pyShaderId; - pkpy_to_int(vm, 0, &pyShaderId); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - bool hasDeletedInstance = false; - if (shaderInstance != NULL) { - ska_shader_cache_remove_instance(shaderId); - ska_shader_instance_destroy(shaderInstance); - hasDeletedInstance = true; - } - pkpy_push_bool(vm, hasDeletedInstance); - return 1; -} - -int cre_pkpy_api_shader_instance_create_bool_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - bool value; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_bool(vm, 2, &value); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_create_bool(shaderInstance, paramName, value); - return 0; -} - -int cre_pkpy_api_shader_instance_set_bool_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - bool value; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_bool(vm, 2, &value); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_update_bool(shaderInstance, paramName, value); - return 0; -} - -int cre_pkpy_api_shader_instance_get_bool_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - const bool paramValue = ska_shader_instance_param_get_bool(shaderInstance, paramName); - pkpy_push_bool(vm, paramValue); - return 1; -} - -int cre_pkpy_api_shader_instance_create_int_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - int value; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_int(vm, 2, &value); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_create_int(shaderInstance, paramName, value); - return 0; -} - -int cre_pkpy_api_shader_instance_set_int_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - int value; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_int(vm, 2, &value); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_update_int(shaderInstance, paramName, value); - return 0; -} - -int cre_pkpy_api_shader_instance_get_int_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - const int paramValue = ska_shader_instance_param_get_int(shaderInstance, paramName); - pkpy_push_int(vm, paramValue); - return 1; -} - -int cre_pkpy_api_shader_instance_create_float_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - f64 value; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_float(vm, 2, &value); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_create_float(shaderInstance, paramName, (float)value); - return 0; -} - -int cre_pkpy_api_shader_instance_set_float_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - f64 value; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_float(vm, 2, &value); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_update_float(shaderInstance, paramName, (float)value); - return 0; -} - -int cre_pkpy_api_shader_instance_get_float_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - const float paramValue = ska_shader_instance_param_get_float(shaderInstance, paramName); - pkpy_push_float(vm, paramValue); - return 1; -} - -int cre_pkpy_api_shader_instance_create_float2_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - f64 valueX; - f64 valueY; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_float(vm, 2, &valueX); - pkpy_to_float(vm, 3, &valueY); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_create_float2(shaderInstance, paramName, (SkaVector2){ (float)valueX, (float)valueY }); - return 0; -} - -int cre_pkpy_api_shader_instance_set_float2_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - f64 valueX; - f64 valueY; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_float(vm, 2, &valueX); - pkpy_to_float(vm, 3, &valueY); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_update_float2(shaderInstance, paramName, (SkaVector2){ (float)valueX, (float)valueY }); - return 0; -} - -int cre_pkpy_api_shader_instance_get_float2_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - const SkaVector2 paramValue = ska_shader_instance_param_get_float2(shaderInstance, paramName); - pkpy_push_float(vm, paramValue.x); - pkpy_push_float(vm, paramValue.y); - return 2; -} - -int cre_pkpy_api_shader_instance_create_float3_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - f64 valueX; - f64 valueY; - f64 valueZ; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_float(vm, 2, &valueX); - pkpy_to_float(vm, 3, &valueY); - pkpy_to_float(vm, 4, &valueZ); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_create_float3(shaderInstance, paramName, (SkaVector3){ (float)valueX, (float)valueY, (float)valueZ }); - return 0; -} - -int cre_pkpy_api_shader_instance_set_float3_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - f64 valueX; - f64 valueY; - f64 valueZ; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_float(vm, 2, &valueX); - pkpy_to_float(vm, 3, &valueY); - pkpy_to_float(vm, 4, &valueZ); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_update_float3(shaderInstance, paramName, (SkaVector3){ (float)valueX, (float)valueY, (float)valueZ }); - return 0; -} - -int cre_pkpy_api_shader_instance_get_float3_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - const SkaVector3 paramValue = ska_shader_instance_param_get_float3(shaderInstance, paramName); - pkpy_push_float(vm, paramValue.x); - pkpy_push_float(vm, paramValue.y); - pkpy_push_float(vm, paramValue.z); - return 3; -} - -int cre_pkpy_api_shader_instance_create_float4_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - f64 valueX; - f64 valueY; - f64 valueZ; - f64 valueW; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_float(vm, 2, &valueX); - pkpy_to_float(vm, 3, &valueY); - pkpy_to_float(vm, 4, &valueZ); - pkpy_to_float(vm, 5, &valueW); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_create_float4(shaderInstance, paramName, (SkaVector4){ (float)valueX, (float)valueY, (float)valueZ, (float)valueW }); - return 0; -} - -int cre_pkpy_api_shader_instance_set_float4_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - f64 valueX; - f64 valueY; - f64 valueZ; - f64 valueW; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - pkpy_to_float(vm, 2, &valueX); - pkpy_to_float(vm, 3, &valueY); - pkpy_to_float(vm, 4, &valueZ); - pkpy_to_float(vm, 5, &valueW); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - ska_shader_instance_param_update_float4(shaderInstance, paramName, (SkaVector4){ (float)valueX, (float)valueY, (float)valueZ, (float)valueW }); - return 0; -} - -int cre_pkpy_api_shader_instance_get_float4_param(pkpy_vm* vm) { - int pyShaderId; - pkpy_CString pyParamName; - pkpy_to_int(vm, 0, &pyShaderId); - pkpy_to_string(vm, 1, &pyParamName); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - const char* paramName = pyParamName; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - const SkaVector4 paramValue = ska_shader_instance_param_get_float4(shaderInstance, paramName); - pkpy_push_float(vm, paramValue.x); - pkpy_push_float(vm, paramValue.y); - pkpy_push_float(vm, paramValue.z); - pkpy_push_float(vm, paramValue.w); - return 4; -} - -//--- SHADER UTIL ---// - -int cre_pkpy_api_shader_util_compile_shader(pkpy_vm* vm) { - pkpy_CString pyShaderPath; - pkpy_to_string(vm, 0, &pyShaderPath); - - const char* shaderPath = pyShaderPath; - const SkaShaderInstanceId newId = ska_shader_cache_create_instance_and_add(shaderPath); - SKA_ASSERT_FMT(newId != SKA_SHADER_INSTANCE_INVALID_ID, "Invalid shader id reading from path '%s'", shaderPath); - pkpy_push_int(vm, (int)newId); - return 1; -} - -int cre_pkpy_api_shader_util_compile_shader_raw(pkpy_vm* vm) { - pkpy_CString pyVertexPath; - pkpy_CString pyFragmentPath; - pkpy_to_string(vm, 0, &pyVertexPath); - pkpy_to_string(vm, 1, &pyFragmentPath); - - const char* vertexPath = pyVertexPath; - const char* fragmentPath = pyFragmentPath; - const SkaShaderInstanceId newId = ska_shader_cache_create_instance_and_add_from_raw(vertexPath, fragmentPath); - SKA_ASSERT_FMT(newId != SKA_SHADER_INSTANCE_INVALID_ID, "Invalid shader id reading from paths: vertex = '%s', fragment = '%s'", vertexPath, fragmentPath); - pkpy_push_int(vm, (int)newId); - return 1; -} - -int cre_pkpy_api_shader_util_set_screen_shader(pkpy_vm* vm) { - int pyShaderId; - pkpy_to_int(vm, 0, &pyShaderId); - - const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); - bool hasSetShaderInstance = false; - if (shaderInstance) { - ska_frame_buffer_set_screen_shader(shaderInstance); - hasSetShaderInstance = true; - } - pkpy_push_bool(vm, hasSetShaderInstance); - return 1; -} - -int cre_pkpy_api_shader_util_get_current_screen_shader(pkpy_vm* vm) { - const SkaShaderInstanceId currentScreenShaderId = 0; // TODO: Keep track of current screen shader somewhere - pkpy_push_int(vm, (int)currentScreenShaderId); - return 1; -} - -int cre_pkpy_api_shader_util_reset_screen_shader_to_default(pkpy_vm* vm) { - ska_frame_buffer_reset_to_default_screen_shader(); - return 0; -} - -//--- ENGINE ---// - -int cre_pkpy_api_engine_exit(pkpy_vm* vm) { - int pyExitCode; - pkpy_to_int(vm, 0, &pyExitCode); - - CREEngineContext* engineContext = cre_engine_context_get(); - engineContext->isRunning = false; - engineContext->exitCode = pyExitCode; - return 0; -} - -int cre_pkpy_api_engine_set_target_fps(pkpy_vm* vm) { - int pyTargetFPS; - pkpy_to_int(vm, 0, &pyTargetFPS); - - CREEngineContext* engineContext = cre_engine_context_get(); - engineContext->targetFPS = pyTargetFPS; - return 0; -} - -int cre_pkpy_api_engine_get_target_fps(pkpy_vm* vm) { - const CREEngineContext* engineContext = cre_engine_context_get(); - pkpy_push_int(vm, engineContext->targetFPS); - return 1; -} - -int cre_pkpy_api_engine_get_average_fps(pkpy_vm* vm) { - const CREEngineContext* engineContext = cre_engine_context_get(); - pkpy_push_int(vm, (int)engineContext->stats.averageFPS); - return 1; -} - -int cre_pkpy_api_engine_set_fps_display_enabled(pkpy_vm* vm) { - bool pyIsEnabled; - pkpy_CString pyFontUID; - f64 pyPositionX, pyPositionY; - pkpy_to_bool(vm, 0, &pyIsEnabled); - pkpy_to_string(vm, 1, &pyFontUID); - pkpy_to_float(vm, 2, &pyPositionX); - pkpy_to_float(vm, 3, &pyPositionY); - - const char* potentialFontUID = pyFontUID; - cre_ecs_manager_enable_fps_display_entity( - pyIsEnabled, - strcmp(potentialFontUID, "") != 0 ? potentialFontUID : NULL, - (float)pyPositionX, - (float)pyPositionY - ); - return 0; -} - -int cre_pkpy_api_engine_get_global_physics_delta_time(pkpy_vm* vm) { - pkpy_push_float(vm, (f64)CRE_GLOBAL_PHYSICS_DELTA_TIME); - return 1; -} - -//--- INPUT ---// - -int cre_pkpy_api_input_is_key_pressed(pkpy_vm* vm) { - int pyKey; - pkpy_to_int(vm, 0, &pyKey); - - const bool isPressed = ska_input_is_key_pressed((SkaInputKey)pyKey, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX); - pkpy_push_bool(vm, isPressed); - return 1; -} - -int cre_pkpy_api_input_is_key_just_pressed(pkpy_vm* vm) { - int pyKey; - pkpy_to_int(vm, 0, &pyKey); - - const bool isJustPressed = ska_input_is_key_just_pressed((SkaInputKey)pyKey, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX); - pkpy_push_bool(vm, isJustPressed); - return 1; -} - -int cre_pkpy_api_input_is_key_just_released(pkpy_vm* vm) { - int pyKey; - pkpy_to_int(vm, 0, &pyKey); - - const bool isJustReleased = ska_input_is_key_just_released((SkaInputKey)pyKey, SKA_INPUT_FIRST_PLAYER_DEVICE_INDEX); - pkpy_push_bool(vm, isJustReleased); - return 1; -} - -int cre_pkpy_api_input_add_action(pkpy_vm* vm) { - pkpy_CString pyActionName; - int pyValueKey; - int pyDeviceId; - pkpy_to_string(vm, 0, &pyActionName); - pkpy_to_int(vm, 1, &pyValueKey); - pkpy_to_int(vm, 2, &pyDeviceId); - - const char* actionName = pyActionName; - const SkaInputKey actionValueKey = (SkaInputKey)pyValueKey; - SKA_ASSERT(actionValueKey); - ska_input_add_input_action( - actionName, - (SkaInputActionValue[]){ { .key = actionValueKey, .strengthThreshold = 0.5f }, { SkaInputKey_INVALID } }, - (SkaInputDeviceIndex)pyDeviceId - ); - return 0; -} - -int cre_pkpy_api_input_is_action_pressed(pkpy_vm* vm) { - pkpy_CString pyActionName; - pkpy_to_string(vm, 0, &pyActionName); - - const char* actionName = pyActionName; - const SkaInputActionHandle handle = ska_input_find_input_action_handle(actionName, 0); - const bool isPressed = handle != SKA_INPUT_INVALID_INPUT_ACTION_HANDLE ? ska_input_is_input_action_pressed(handle, 0) : false; - pkpy_push_bool(vm, isPressed); - return 1; -} - -int cre_pkpy_api_input_is_action_just_pressed(pkpy_vm* vm) { - pkpy_CString pyActionName; - pkpy_to_string(vm, 0, &pyActionName); - - const char* actionName = pyActionName; - const SkaInputActionHandle handle = ska_input_find_input_action_handle(actionName, 0); - const bool isJustPressed = handle != SKA_INPUT_INVALID_INPUT_ACTION_HANDLE ? ska_input_is_input_action_just_pressed(handle, 0) : false; - pkpy_push_bool(vm, isJustPressed); - return 1; -} - -int cre_pkpy_api_input_is_action_just_released(pkpy_vm* vm) { - pkpy_CString pyActionName; - pkpy_to_string(vm, 0, &pyActionName); - - const char* actionName = pyActionName; - const SkaInputActionHandle handle = ska_input_find_input_action_handle(actionName, 0); - const bool isJustReleased = handle != SKA_INPUT_INVALID_INPUT_ACTION_HANDLE ? ska_input_is_input_action_just_released(handle, 0) : false; - pkpy_push_bool(vm, isJustReleased); - return 1; -} - -int cre_pkpy_api_input_start_gamepad_vibration(pkpy_vm* vm) { - int pyDeviceId; - f64 pyWeakMagnitude; - f64 pyStrongMagnitude; - f64 pyDurationSeconds; - pkpy_to_int(vm, 0, &pyDeviceId); - pkpy_to_float(vm, 1, &pyWeakMagnitude); - pkpy_to_float(vm, 2, &pyStrongMagnitude); - pkpy_to_float(vm, 3, &pyDurationSeconds); - - // TODO: Input - fix up -// se_input_gamepad_start_vibration(pyDeviceId, (float)pyWeakMagnitude, (float)pyStrongMagnitude, (float)pyDurationSeconds); - return 0; -} - -int cre_pkpy_api_input_stop_gamepad_vibration(pkpy_vm* vm) { - int pyDeviceId; - pkpy_to_int(vm, 0, &pyDeviceId); - - // TODO: Input - fix up -// se_input_gamepad_stop_vibration(pyDeviceId); - return 0; -} - -int cre_pkpy_api_input_mouse_get_position(pkpy_vm* vm) { - const SkaMouse* globalMouse = ska_input_get_mouse(); - pkpy_push_float(vm, (f64)globalMouse->position.x); - pkpy_push_float(vm, (f64)globalMouse->position.y); - return 2; -} - -int cre_pkpy_api_input_mouse_get_world_position(pkpy_vm* vm) { - const SkaVector2 mouseWorldPosition = cre_pkpy_api_helper_mouse_get_global_position(&SKA_VECTOR2_ZERO); - pkpy_push_float(vm, (f64)mouseWorldPosition.x); - pkpy_push_float(vm, (f64)mouseWorldPosition.y); - return 2; -} - -//--- SCENE TREE ---// - -int cre_pkpy_api_scene_tree_change_scene(pkpy_vm* vm) { - pkpy_CString pyScenePath; - pkpy_to_string(vm, 0, &pyScenePath); - - const char* scenePath = pyScenePath; - cre_scene_manager_queue_scene_change(scenePath); - return 0; -} - -int cre_pkpy_api_scene_tree_get_root(pkpy_vm* vm) { - SceneTreeNode* rootNode = cre_scene_manager_get_active_scene_root(); - SKA_ASSERT(rootNode != NULL); - cre_pkpy_script_context_create_instance_if_nonexistent_and_push_entity_instance(rootNode->entity); - return 1; -} - -//--- SCENE MANAGER ---// - -int cre_pkpy_api_scene_manager_process_queued_creation_entities(pkpy_vm* vm) { - cre_scene_manager_process_queued_creation_entities(); - return 0; -} - -int cre_pkpy_api_scene_manager_process_queued_scene_change(pkpy_vm* vm) { - cre_scene_manager_process_queued_scene_change(); - return 0; -} - -//--- GAME PROPERTIES ---// - -int cre_pkpy_api_game_properties_get(pkpy_vm* vm) { - const CREGameProperties* gameProps = cre_game_props_get(); - SKA_ASSERT(gameProps->gameTitle); - SKA_ASSERT(gameProps->initialScenePath); - pkpy_push_string(vm, pkpy_string(gameProps->gameTitle)); - pkpy_push_int(vm, gameProps->resolutionWidth); - pkpy_push_int(vm, gameProps->resolutionHeight); - pkpy_push_int(vm, gameProps->windowWidth); - pkpy_push_int(vm, gameProps->windowHeight); - pkpy_push_int(vm, gameProps->targetFPS); - pkpy_push_string(vm, pkpy_string(gameProps->initialScenePath)); - pkpy_push_bool(vm, gameProps->areCollidersVisible); - return 8; -} - -//--- CAMERA2D ---// - -int cre_pkpy_api_camera2d_set_position(pkpy_vm* vm) { - f64 pyPositionX, pyPositionY; - pkpy_to_float(vm, 0, &pyPositionX); - pkpy_to_float(vm, 1, &pyPositionY); - - CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - camera2D->viewport = (SkaVector2){ (float)pyPositionX, (float)pyPositionY }; - cre_camera2d_clamp_viewport_to_boundary(camera2D); - return 0; -} - -int cre_pkpy_api_camera2d_add_to_position(pkpy_vm* vm) { - f64 pyPositionX, pyPositionY; - pkpy_to_float(vm, 0, &pyPositionX); - pkpy_to_float(vm, 1, &pyPositionY); - - CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - camera2D->viewport = (SkaVector2){ camera2D->viewport.x + (float)pyPositionX, camera2D->viewport.y + (float)pyPositionY }; - cre_camera2d_clamp_viewport_to_boundary(camera2D); - return 0; -} - -int cre_pkpy_api_camera2d_get_position(pkpy_vm* vm) { - const CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - pkpy_push_float(vm, (f64)camera2D->viewport.x); - pkpy_push_float(vm, (f64)camera2D->viewport.y); - return 2; -} - -int cre_pkpy_api_camera2d_set_offset(pkpy_vm* vm) { - f64 pyOffsetX, pyOffsetY; - pkpy_to_float(vm, 0, &pyOffsetX); - pkpy_to_float(vm, 1, &pyOffsetY); - - CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - camera2D->offset = (SkaVector2){(float)pyOffsetX, (float)pyOffsetY }; - return 0; -} - -int cre_pkpy_api_camera2d_add_to_offset(pkpy_vm* vm) { - f64 pyOffsetX, pyOffsetY; - pkpy_to_float(vm, 0, &pyOffsetX); - pkpy_to_float(vm, 1, &pyOffsetY); - - CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - camera2D->offset = (SkaVector2){camera2D->offset.x + (float)pyOffsetX, camera2D->offset.y + (float)pyOffsetY }; - return 0; -} - -int cre_pkpy_api_camera2d_get_offset(pkpy_vm* vm) { - const CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - pkpy_push_float(vm, (f64)camera2D->offset.x); - pkpy_push_float(vm, (f64)camera2D->offset.y); - return 2; -} - -int cre_pkpy_api_camera2d_set_zoom(pkpy_vm* vm) { - f64 pyZoomX, pyZoomY; - pkpy_to_float(vm, 0, &pyZoomX); - pkpy_to_float(vm, 1, &pyZoomY); - - CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - camera2D->zoom = (SkaVector2){(float)pyZoomX, (float)pyZoomY }; - return 0; -} - -int cre_pkpy_api_camera2d_add_to_zoom(pkpy_vm* vm) { - f64 pyZoomX, pyZoomY; - pkpy_to_float(vm, 0, &pyZoomX); - pkpy_to_float(vm, 1, &pyZoomY); - - CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - camera2D->zoom = (SkaVector2){camera2D->zoom.x + (float)pyZoomX, camera2D->zoom.y + (float)pyZoomY }; - return 0; -} - -int cre_pkpy_api_camera2d_get_zoom(pkpy_vm* vm) { - const CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - pkpy_push_float(vm, (f64)camera2D->zoom.x); - pkpy_push_float(vm, (f64)camera2D->zoom.y); - return 2; -} - -int cre_pkpy_api_camera2d_set_boundary(pkpy_vm* vm) { - f64 pyBoundaryX, pyBoundaryY, pyBoundaryW, pyBoundaryH; - pkpy_to_float(vm, 0, &pyBoundaryX); - pkpy_to_float(vm, 1, &pyBoundaryY); - pkpy_to_float(vm, 2, &pyBoundaryW); - pkpy_to_float(vm, 3, &pyBoundaryH); - - CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - camera2D->boundary = (SkaRect2){ (float)pyBoundaryX, (float)pyBoundaryY, (float)pyBoundaryW, (float)pyBoundaryH }; - cre_camera2d_clamp_viewport_to_boundary(camera2D); - return 0; -} - -int cre_pkpy_api_camera2d_get_boundary(pkpy_vm* vm) { - const CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - pkpy_push_float(vm, (f64)camera2D->boundary.x); - pkpy_push_float(vm, (f64)camera2D->boundary.y); - pkpy_push_float(vm, (f64)camera2D->boundary.w); - pkpy_push_float(vm, (f64)camera2D->boundary.h); - return 4; -} - -int cre_pkpy_api_camera2d_follow_node(pkpy_vm* vm) { - int pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - cre_camera2d_follow_entity(camera2D, entity); - return 0; -} - -int cre_pkpy_api_camera2d_unfollow_node(pkpy_vm* vm) { - int pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); - cre_camera2d_unfollow_entity(camera2D, entity); - return 0; -} - -//--- WORLD ---// -void cre_pkpy_mark_scene_nodes_time_dilation_flag_dirty(SceneTreeNode* node) { - NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(node->entity, NODE_COMPONENT_INDEX); - nodeComponent->timeDilation.cacheInvalid = true; -} - -int cre_pkpy_api_world_set_time_dilation(pkpy_vm* vm) { - f64 pyTimeDilation; - pkpy_to_float(vm, 0, &pyTimeDilation); - - cre_world_set_time_dilation((float)pyTimeDilation); - cre_scene_manager_execute_on_root_and_child_nodes(cre_pkpy_mark_scene_nodes_time_dilation_flag_dirty); - return 0; -} - -int cre_pkpy_api_world_get_time_dilation(pkpy_vm* vm) { - pkpy_push_float(vm, (f64)cre_world_get_time_dilation()); - return 1; -} - -int cre_pkpy_api_world_get_delta_time(pkpy_vm* vm) { - pkpy_push_float(vm, (f64)(cre_world_get_time_dilation() * CRE_GLOBAL_PHYSICS_DELTA_TIME)); - return 1; -} - -int cre_pkpy_api_world_get_variable_delta_time(pkpy_vm* vm) { - pkpy_push_float(vm, (f64)cre_world_get_frame_delta_time()); - return 1; -} - -//--- AUDIO SOURCE ---// - -int cre_pkpy_api_audio_source_set_pitch(pkpy_vm* vm) { - pkpy_CString pyPath; - f64 pyPitch; - pkpy_to_string(vm, 0, &pyPath); - pkpy_to_float(vm, 1, &pyPitch); - - const char* path = pyPath; - if (ska_asset_manager_has_audio_source(path)) { - SkaAudioSource* audioSource = ska_asset_manager_get_audio_source(path); - audioSource->pitch = pyPitch; - } else { - ska_logger_warn("Tried to set non-existent audio source's pitch at '%s'", path); - } - return 0; -} -int cre_pkpy_api_audio_source_get_pitch(pkpy_vm* vm) { - pkpy_CString pyPath; - pkpy_to_string(vm, 0, &pyPath); - - const char* path = pyPath; - if (ska_asset_manager_has_audio_source(path)) { - SkaAudioSource* audioSource = ska_asset_manager_get_audio_source(path); - pkpy_push_float(vm, audioSource->pitch); - } else { - ska_logger_warn("Tried to get non-existent audio source's pitch at '%s'", path); - pkpy_push_float(vm, 1.0); - } - return 1; -} - -//--- AUDIO MANAGER ---// - -int cre_pkpy_api_audio_manager_play_sound(pkpy_vm* vm) { - pkpy_CString pyPath; - bool pyLoops; - pkpy_to_string(vm, 0, &pyPath); - pkpy_to_bool(vm, 1, &pyLoops); - - const char* path = pyPath; - ska_audio_manager_play_sound(path, pyLoops); - return 0; -} - -int cre_pkpy_api_audio_manager_stop_sound(pkpy_vm* vm) { - pkpy_CString pyPath; - pkpy_to_string(vm, 0, &pyPath); - - const char* path = pyPath; - ska_audio_manager_stop_sound(path); - return 0; -} - -//--- GAME CONFIG ---// - -int cre_pkpy_api_game_config_save(pkpy_vm* vm) { - pkpy_CString pyPath; - pkpy_CString pyDataJson; - pkpy_CString pyEncryptionKey; - pkpy_to_string(vm, 0, &pyPath); - pkpy_to_string(vm, 1, &pyDataJson); - pkpy_to_string(vm, 2, &pyEncryptionKey); - - const char* path = pyPath; - const char* dataJson = pyDataJson; - const char* encryptionKey = pyEncryptionKey; // TODO: Use - const CREGameProperties* gameProps = cre_game_props_get(); - char* validGameTitle = ska_strdup(gameProps->gameTitle); - ska_str_to_lower_and_underscore_whitespace(validGameTitle); - char* fullSavePath = ska_fs_get_user_save_path("crescent", validGameTitle, path); - const bool success = ska_fs_write_to_file(fullSavePath, dataJson); - SKA_MEM_FREE(fullSavePath); - SKA_MEM_FREE(validGameTitle); - pkpy_push_bool(vm, success); - return 1; -} - -int cre_pkpy_api_game_config_load(pkpy_vm* vm) { - pkpy_CString pyPath; - pkpy_CString pyEncryptionKey; - pkpy_to_string(vm, 0, &pyPath); - pkpy_to_string(vm, 1, &pyEncryptionKey); - - const char* path = pyPath; - const char* encryptionKey = pyEncryptionKey; // TODO: Use - const CREGameProperties* gameProps = cre_game_props_get(); - char* validGameTitle = ska_strdup(gameProps->gameTitle); - ska_str_to_lower_and_underscore_whitespace(validGameTitle); - char* fullSavePath = ska_fs_get_user_save_path("crescent", validGameTitle, path); - char* fileContents = ska_asset_file_loader_read_file_contents_as_string(fullSavePath, NULL); - pkpy_push_string(vm, pkpy_string(fileContents)); - SKA_MEM_FREE(validGameTitle); - SKA_MEM_FREE(fullSavePath); - SKA_MEM_FREE(fileContents); - return 1; -} - -//--- Packed Scene ---// - -int cre_pkpy_api_packed_scene_create_instance(pkpy_vm* vm) { - int pyCacheId; - pkpy_to_int(vm, 0, &pyCacheId); - - const CreSceneCacheId cacheId = (CreSceneCacheId)pyCacheId; - JsonSceneNode* sceneNode = cre_scene_template_cache_get_scene(cacheId); - SceneTreeNode* rootNode = cre_scene_manager_stage_scene_nodes_from_json(sceneNode); - - // Get class path and name from root node if it has a script component - if (sceneNode->components[SCRIPT_COMPONENT_INDEX] != NULL) { - ScriptComponent* scriptComp = (ScriptComponent*)sceneNode->components[SCRIPT_COMPONENT_INDEX]; - cre_pkpy_script_context_create_instance(rootNode->entity, scriptComp->classPath, scriptComp->className); - } else { - cre_pkpy_script_context_create_instance(rootNode->entity, CRE_PKPY_MODULE_NAME_CRESCENT, node_get_base_type_string(sceneNode->type)); - } - cre_pkpy_entity_instance_cache_push_entity_instance(vm, rootNode->entity); - return 1; -} - -int cre_pkpy_api_packed_scene_load(pkpy_vm* vm) { - pkpy_CString pyScenePath; - pkpy_to_string(vm, 0, &pyScenePath); - - const char* scenePath = pyScenePath; - const CreSceneCacheId cacheId = cre_scene_template_cache_load_scene(scenePath); - pkpy_push_int(vm, (int)cacheId); - return 1; -} - -//--- Collision Handler ---// - -int cre_pkpy_api_collision_handler_process_collisions(pkpy_vm* vm) { - int pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const CollisionResult collisionResult = cre_collision_process_entity_collisions(entity); - for (size_t i = 0; i < collisionResult.collidedEntityCount; i++) { - const SkaEntity collidedEntity = collisionResult.collidedEntities[i]; - cre_pkpy_script_context_create_instance_if_nonexistent_and_push_entity_instance(collidedEntity); - } - return (int)collisionResult.collidedEntityCount; -} - -int cre_pkpy_api_collision_handler_process_mouse_collisions(pkpy_vm* vm) { - f64 pyPosOffsetX, pyPosOffsetY; - f64 pyCollisionSizeW, pyCollisionSizeH; - pkpy_to_float(vm, 0, &pyPosOffsetX); - pkpy_to_float(vm, 1, &pyPosOffsetY); - pkpy_to_float(vm, 2, &pyCollisionSizeW); - pkpy_to_float(vm, 3, &pyCollisionSizeH); - - const SkaVector2 positionOffset = { (float)pyPosOffsetX, (float)pyPosOffsetY }; - const SkaVector2 mouseWorldPos = cre_pkpy_api_helper_mouse_get_global_position(&positionOffset); - const SkaRect2 collisionRect = { mouseWorldPos.x, mouseWorldPos.y, (float)pyCollisionSizeW, (float)pyCollisionSizeH }; - const CollisionResult collisionResult = cre_collision_process_mouse_collisions(&collisionRect); - for (size_t i = 0; i < collisionResult.collidedEntityCount; i++) { - const SkaEntity collidedEntity = collisionResult.collidedEntities[i]; - cre_pkpy_script_context_create_instance_if_nonexistent_and_push_entity_instance(collidedEntity); - } - return (int)collisionResult.collidedEntityCount; -} - -//--- Network ---// -int cre_pkpy_api_network_is_server(pkpy_vm* vm) { - pkpy_push_bool(vm, ska_network_is_server()); - return 1; -} - -//--- Server ---// -int cre_pkpy_api_server_start(pkpy_vm* vm) { - int pyPort; - pkpy_to_int(vm, 0, &pyPort); - - ska_udp_server_initialize(pyPort, ska_ecs_system_event_network_callback); - return 0; -} - -int cre_pkpy_api_server_stop(pkpy_vm* vm) { - ska_udp_server_finalize(); - return 0; -} - -int cre_pkpy_api_server_send(pkpy_vm* vm) { - pkpy_CString pyMessage; - pkpy_to_string(vm, 0, &pyMessage); - - const char* message = pyMessage; - ska_udp_server_send_message(message); - return 0; -} - -//--- Client ---// -int cre_pkpy_api_client_start(pkpy_vm* vm) { - pkpy_CString pyHost; - int pyPort; - pkpy_to_string(vm, 0, &pyHost); - pkpy_to_int(vm, 1, &pyPort); - - const char* host = pyHost; - ska_udp_client_initialize(host, pyPort, ska_ecs_system_event_network_callback); - return 0; -} -int cre_pkpy_api_client_stop(pkpy_vm* vm) { - ska_udp_client_finalize(); - return 0; -} -int cre_pkpy_api_client_send(pkpy_vm* vm) { - pkpy_CString pyMessage; - pkpy_to_string(vm, 0, &pyMessage); - - const char* message = pyMessage; - ska_udp_client_send_message(message); - return 0; -} -*/ diff --git a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.h b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.h deleted file mode 100644 index 24743046d..000000000 --- a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once -/* -struct pkpy_vm_handle; - -void cre_pkpy_api_load_internal_modules(struct pkpy_vm_handle* vm); -*/ \ No newline at end of file diff --git a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.c b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.c deleted file mode 100644 index d90d5f4ef..000000000 --- a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.c +++ /dev/null @@ -1,1270 +0,0 @@ -#include "cre_pkpy_api_node.h" -/* -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../cre_pkpy_entity_instance_cache.h" -#include "../cre_pkpy_script_context.h" -#include "../../../../scene/scene_manager.h" -#include "../../../../ecs/ecs_globals.h" -#include "../../../../ecs/components/sprite_component.h" -#include "../../../../ecs/components/animated_sprite_component.h" -#include "../../../../ecs/components/text_label_component.h" -#include "../../../../ecs/components/collider2d_component.h" -#include "../../../../ecs/components/color_rect_component.h" -#include "../../../../ecs/components/parallax_component.h" -#include "../../../../ecs/components/script_component.h" -#include "../../../../ecs/components/particles2d_component.h" -#include "../../../../ecs/components/tilemap_component.h" - -typedef struct CreColor { - int32 r; - int32 g; - int32 b; - int32 a; -} CreColor; - -static CreColor convert_ska_color_to_cre(const SkaColor* color) { - return (CreColor){ - .r = (int32)(color->r * 255.0f), - .g = (int32)(color->g * 255.0f), - .b = (int32)(color->b * 255.0f), - .a = (int32)(color->a * 255.0f) - }; -} - -static void set_node_component_from_type(SkaEntity entity, const char* classPath, const char* className, NodeBaseType baseType) { - - // Set components that should be set for a base node (that has invoked .new() from scripting) - ska_ecs_component_manager_set_component(entity, NODE_COMPONENT_INDEX, node_component_create_ex(className, baseType)); - ska_ecs_component_manager_set_component(entity, SCRIPT_COMPONENT_INDEX, script_component_create_ex(classPath, className, ScriptContextType_PYTHON)); - - const NodeBaseInheritanceType inheritanceType = node_get_type_inheritance(baseType); - - if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_NODE2D)) { - ska_ecs_component_manager_set_component(entity, TRANSFORM2D_COMPONENT_INDEX, transform2d_component_create()); - } - if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_SPRITE)) { - ska_ecs_component_manager_set_component(entity, SPRITE_COMPONENT_INDEX, sprite_component_create()); - } - if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_ANIMATED_SPRITE)) { - ska_ecs_component_manager_set_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX, animated_sprite_component_create()); - } - if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_TEXT_LABEL)) { - ska_ecs_component_manager_set_component(entity, TEXT_LABEL_COMPONENT_INDEX, text_label_component_create()); - } - if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_COLLIDER2D)) { - ska_ecs_component_manager_set_component(entity, COLLIDER2D_COMPONENT_INDEX, collider2d_component_create()); - } - if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_COLOR_RECT)) { - ska_ecs_component_manager_set_component(entity, COLOR_RECT_COMPONENT_INDEX, color_rect_component_create()); - } - if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_PARALLAX)) { - ska_ecs_component_manager_set_component(entity, PARALLAX_COMPONENT_INDEX, parallax_component_create()); - } - if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_PARTICLES2D)) { - ska_ecs_component_manager_set_component(entity, PARTICLES2D_COMPONENT_INDEX, particles2d_component_create()); - } - if (SKA_FLAG_CONTAINS(inheritanceType, NodeBaseInheritanceType_TILEMAP)) { - ska_ecs_component_manager_set_component(entity, TILEMAP_COMPONENT_INDEX, tilemap_component_create()); - } -} - -//--- NODE ---// - -int32 cre_pkpy_api_node_new(pkpy_vm* vm) { - pkpy_CString pyClassPath; - pkpy_CString pyClassName; - int32 pyNodeTypeFlag; - pkpy_to_string(vm, 0, &pyClassPath); - pkpy_to_string(vm, 1, &pyClassName); - pkpy_to_int(vm, 2, &pyNodeTypeFlag); - - char classPath[64]; - char className[32]; - ska_strcpy(classPath, pyClassPath); - ska_strcpy(className, pyClassName); - const SkaEntity entity = cre_pkpy_entity_instance_cache_create_new_entity(vm, classPath, className, ska_ecs_entity_create()); - SceneTreeNode* newNode = cre_scene_tree_create_tree_node(entity, NULL); - cre_scene_manager_stage_child_node_to_be_added_later(newNode); - - set_node_component_from_type(entity, classPath, className, (NodeBaseType)pyNodeTypeFlag); - - cre_pkpy_script_context_create_instance(entity, classPath, className); - cre_pkpy_entity_instance_cache_push_entity_instance(vm, entity); - return 1; -} - -int32 cre_pkpy_api_node_get_name(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); - pkpy_push_string(vm, pkpy_string(nodeComponent->name)); - return 1; -} - -int32 cre_pkpy_api_node_add_child(pkpy_vm* vm) { - int32 childEntityId; - int32 parentEntityId; - pkpy_to_int(vm, 0, &parentEntityId); - pkpy_to_int(vm, 1, &childEntityId); - - SkaEntity childEntity = (SkaEntity)childEntityId; - SkaEntity parentEntity = (SkaEntity)parentEntityId; - cre_scene_manager_add_node_as_child(childEntity, parentEntity); - return 0; -} - -int32 cre_pkpy_api_node_get_child(pkpy_vm* vm) { - int32 parentEntityId; - pkpy_CString childEntityName; - pkpy_to_int(vm, 0, &parentEntityId); - pkpy_to_string(vm, 1, &childEntityName); - - const char* childName = childEntityName; - const SkaEntity childEntity = cre_scene_manager_get_entity_child_by_name((SkaEntity)parentEntityId, childName); - if (childEntity != SKA_NULL_ENTITY) { - cre_pkpy_script_context_create_instance_if_nonexistent_and_push_entity_instance(childEntity); - return 1; - } - return 0; -} - -int32 cre_pkpy_api_node_get_children(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - if (!cre_scene_manager_has_entity_tree_node(entity)) { - return 0; - } - - const SceneTreeNode* parentTreeNode = cre_scene_manager_get_entity_tree_node(entity); - for (size_t i = 0; i < parentTreeNode->childCount; i++) { - const SceneTreeNode* childTreeNode = parentTreeNode->children[i]; - cre_pkpy_script_context_create_instance_if_nonexistent_and_push_entity_instance(childTreeNode->entity); - } - - return (int32)parentTreeNode->childCount; -} - -int32 cre_pkpy_api_node_get_parent(pkpy_vm* vm) { - int32 childEntityId; - pkpy_to_int(vm, 0, &childEntityId); - - const SkaEntity childEntity = (SkaEntity)childEntityId; - if (cre_scene_manager_has_entity_tree_node(childEntity)) { - const SceneTreeNode* treeNode = cre_scene_manager_get_entity_tree_node(childEntity); - if (treeNode->parent) { - cre_pkpy_script_context_create_instance_if_nonexistent_and_push_entity_instance(treeNode->parent->entity); - } else { - pkpy_push_none(vm); - } - return 1; - } - return 0; -} - -int32 cre_pkpy_api_node_queue_deletion(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); - if (!nodeComponent->queuedForDeletion) { - if (cre_scene_manager_has_entity_tree_node(entity)) { - SceneTreeNode* node = cre_scene_manager_get_entity_tree_node(entity); - cre_queue_destroy_tree_node_entity_all(node); - } else { - ska_logger_warn("Entity not found in tree, skipping queue deletion."); - } - } else { - ska_logger_warn("Node '%s' already queued for deletion!", nodeComponent->name); - } - return 0; -} - -int32 cre_pkpy_api_node_is_queued_for_deletion(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); - pkpy_push_bool(vm, nodeComponent->queuedForDeletion); - return 1; -} - -int32 cre_pkpy_api_node_set_time_dilation(pkpy_vm* vm) { - int32 pyEntityId; - f64 timeDilation; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &timeDilation); - - const SkaEntity entity = (SkaEntity)pyEntityId; - NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); - nodeComponent->timeDilation.value = (f32)timeDilation; - cre_scene_manager_invalidate_time_dilation_nodes_with_children(entity); - - return 0; -} - -int32 cre_pkpy_api_node_get_time_dilation(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); - const f64 timeDilation = (f64)nodeComponent->timeDilation.value; - pkpy_push_float(vm, timeDilation); - return 1; -} - -int32 cre_pkpy_api_node_get_total_time_dilation(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const f64 totalTimeDilation = (f64)cre_scene_manager_get_node_full_time_dilation(entity); - pkpy_push_float(vm, totalTimeDilation); - return 1; -} - -//--- NODE2D ---// - -void cre_pkpy_update_entity_local_position(SkaEntity entity, SkaVector2* position) { - Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity,TRANSFORM2D_COMPONENT_INDEX); - const SkaVector2 prevPosition = transformComp->localTransform.position; - transformComp->localTransform.position.x = position->x; - transformComp->localTransform.position.y = position->y; - transformComp->isGlobalTransformDirty = true; - if (transformComp->localTransform.position.x != prevPosition.x || transformComp->localTransform.position.y != prevPosition.y) { - cre_scene_manager_notify_all_on_transform_events(entity, transformComp); - } -} - -void cre_pkpy_update_entity_local_scale(SkaEntity entity, SkaVector2 * scale) { - Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity,TRANSFORM2D_COMPONENT_INDEX); - const SkaVector2 prevScale = transformComp->localTransform.scale; - transformComp->localTransform.scale.x = scale->x; - transformComp->localTransform.scale.y = scale->y; - transformComp->isGlobalTransformDirty = true; - if (transformComp->localTransform.scale.x != prevScale.x || transformComp->localTransform.scale.y != prevScale.y) { - cre_scene_manager_notify_all_on_transform_events(entity, transformComp); - } -} - -void cre_pkpy_update_entity_local_rotation(SkaEntity entity, f32 rotation) { - Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - const f32 prevRotation = transformComp->localTransform.rotation; - transformComp->localTransform.rotation = rotation; - transformComp->isGlobalTransformDirty = true; - if (transformComp->localTransform.rotation != prevRotation) { - cre_scene_manager_notify_all_on_transform_events(entity, transformComp); - } -} - -int32 cre_pkpy_api_node2d_set_position(pkpy_vm* vm) { - int32 pyEntityId; - f64 positionX; - f64 positionY; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &positionX); - pkpy_to_float(vm, 2, &positionY); - - const SkaEntity entity = (SkaEntity)pyEntityId; - cre_pkpy_update_entity_local_position(entity, &(SkaVector2){ (f32)positionX, (f32)positionY }); - return 0; -} - -int32 cre_pkpy_api_node2d_add_to_position(pkpy_vm* vm) { - int32 pyEntityId; - f64 positionX; - f64 positionY; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &positionX); - pkpy_to_float(vm, 2, &positionY); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - cre_pkpy_update_entity_local_position(entity, &(SkaVector2){ - transformComp->localTransform.position.x + (f32)positionX, - transformComp->localTransform.position.y + (f32)positionY - }); - return 0; -} - -int32 cre_pkpy_api_node2d_get_position(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)transformComp->localTransform.position.x); - pkpy_push_float(vm, (f64)transformComp->localTransform.position.y); - return 2; -} - -int32 cre_pkpy_api_node2d_get_global_position(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - SkaTransformModel2D* globalTransform = cre_scene_manager_get_scene_node_global_transform(entity, transformComp); - pkpy_push_float(vm, (f64)globalTransform->position.x); - pkpy_push_float(vm, (f64)globalTransform->position.y); - return 2; -} - -int32 cre_pkpy_api_node2d_set_scale(pkpy_vm* vm) { - int32 pyEntityId; - f64 scaleX; - f64 scaleY; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &scaleX); - pkpy_to_float(vm, 2, &scaleY); - - const SkaEntity entity = (SkaEntity)pyEntityId; - cre_pkpy_update_entity_local_scale(entity, &(SkaVector2){(f32)scaleX, (f32)scaleY }); - return 0; -} - -int32 cre_pkpy_api_node2d_add_to_scale(pkpy_vm* vm) { - int32 pyEntityId; - f64 scaleX; - f64 scaleY; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &scaleX); - pkpy_to_float(vm, 2, &scaleY); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - cre_pkpy_update_entity_local_scale(entity, &(SkaVector2){ - transformComp->localTransform.scale.x + (f32)scaleX, - transformComp->localTransform.scale.y + (f32)scaleY - }); - return 0; -} - -int32 cre_pkpy_api_node2d_get_scale(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)transformComp->localTransform.scale.x); - pkpy_push_float(vm, (f64)transformComp->localTransform.scale.y); - return 2; -} - -int32 cre_pkpy_api_node2d_set_rotation(pkpy_vm* vm) { - int32 pyEntityId; - f64 rotation; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &rotation); - - const SkaEntity entity = (SkaEntity)pyEntityId; - cre_pkpy_update_entity_local_rotation(entity, (f32)rotation); - return 0; -} - -int32 cre_pkpy_api_node2d_add_to_rotation(pkpy_vm* vm) { - int32 pyEntityId; - f64 rotation; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &rotation); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - cre_pkpy_update_entity_local_rotation(entity, transformComp->localTransform.rotation + (f32)rotation); - return 0; -} - -int32 cre_pkpy_api_node2d_get_rotation(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)transformComp->localTransform.rotation); - return 1; -} - -int32 cre_pkpy_api_node2d_set_z_index(pkpy_vm* vm) { - int32 pyEntityId; - int32 zIndex; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_int(vm, 1, &zIndex); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - transformComp->zIndex = zIndex; - return 0; -} - -int32 cre_pkpy_api_node2d_get_z_index(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - pkpy_push_int(vm, transformComp->zIndex); - return 1; -} - -int32 cre_pkpy_api_node2d_set_z_index_relative_to_parent(pkpy_vm* vm) { - int32 pyEntityId; - bool zIndexRelativeToParent; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_bool(vm, 1, &zIndexRelativeToParent); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - transformComp->isZIndexRelativeToParent = zIndexRelativeToParent; - return 0; -} - -int32 cre_pkpy_api_node2d_get_z_index_relative_to_parent(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - pkpy_push_bool(vm, transformComp->isZIndexRelativeToParent); - return 1; -} - -int32 cre_pkpy_api_node2d_set_ignore_camera(pkpy_vm* vm) { - int32 pyEntityId; - bool ignoreCamera; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_bool(vm, 1, &ignoreCamera); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - transformComp->ignoreCamera = ignoreCamera; - return 0; -} - -int32 cre_pkpy_api_node2d_get_ignore_camera(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); - pkpy_push_bool(vm, transformComp->ignoreCamera); - return 1; -} - -//--- SPRITE ---// - -int32 cre_pkpy_api_sprite_get_texture(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - SkaTexture* texture = spriteComponent->texture; // TODO: Make const once applied to se_texture_get_wrap function params - pkpy_push_string(vm, pkpy_string(texture->fileName)); - pkpy_push_string(vm, pkpy_string(ska_texture_get_wrap_s_string(texture))); - pkpy_push_string(vm, pkpy_string(ska_texture_get_wrap_t_string(texture))); - pkpy_push_bool(vm, texture->applyNearestNeighbor); - return 4; -} - -int32 cre_pkpy_api_sprite_set_texture(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_CString pyTexturePath; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_string(vm, 1, &pyTexturePath); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const char* texturePath = pyTexturePath; - SKA_ASSERT_FMT(ska_asset_manager_has_texture(texturePath), "Doesn't have texture with file path '%s'", texturePath); - SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - spriteComponent->texture = ska_asset_manager_get_texture(texturePath); - return 0; -} - -int32 cre_pkpy_api_sprite_get_draw_source(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)spriteComponent->drawSource.x); - pkpy_push_float(vm, (f64)spriteComponent->drawSource.y); - pkpy_push_float(vm, (f64)spriteComponent->drawSource.w); - pkpy_push_float(vm, (f64)spriteComponent->drawSource.h); - return 4; -} - -int32 cre_pkpy_api_sprite_set_draw_source(pkpy_vm* vm) { - int32 pyEntityId; - f64 pyDrawSourceX; - f64 pyDrawSourceY; - f64 pyDrawSourceW; - f64 pyDrawSourceH; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &pyDrawSourceX); - pkpy_to_float(vm, 2, &pyDrawSourceY); - pkpy_to_float(vm, 3, &pyDrawSourceW); - pkpy_to_float(vm, 4, &pyDrawSourceH); - - const SkaEntity entity = (SkaEntity)pyEntityId; - SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - spriteComponent->drawSource = (SkaRect2){ (f32)pyDrawSourceX, (f32)pyDrawSourceY, (f32)pyDrawSourceW, (f32)pyDrawSourceH }; - return 0; -} - -int32 cre_pkpy_api_sprite_get_flip_h(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - pkpy_push_bool(vm, spriteComponent->flipH); - return 1; -} - -int32 cre_pkpy_api_sprite_set_flip_h(pkpy_vm* vm) { - int32 pyEntityId; - bool pySetFlipH; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_bool(vm, 1, &pySetFlipH); - - const SkaEntity entity = (SkaEntity)pyEntityId; - SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - spriteComponent->flipH = pySetFlipH; - return 0; -} - -int32 cre_pkpy_api_sprite_get_flip_v(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - pkpy_push_bool(vm, spriteComponent->flipV); - return 1; -} - -int32 cre_pkpy_api_sprite_set_flip_v(pkpy_vm* vm) { - int32 pyEntityId; - bool pySetFlipV; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_bool(vm, 1, &pySetFlipV); - - const SkaEntity entity = (SkaEntity)pyEntityId; - SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - spriteComponent->flipV = pySetFlipV; - return 0; -} - -int32 cre_pkpy_api_sprite_get_modulate(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - const CreColor color = convert_ska_color_to_cre(&spriteComponent->modulate); - pkpy_push_int(vm, color.r); - pkpy_push_int(vm, color.g); - pkpy_push_int(vm, color.b); - pkpy_push_int(vm, color.a); - return 4; -} - -int32 cre_pkpy_api_sprite_set_modulate(pkpy_vm* vm) { - int32 pyEntityId; - int32 pyModulateR, pyModulateG, pyModulateB, pyModulateA; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_int(vm, 1, &pyModulateR); - pkpy_to_int(vm, 2, &pyModulateG); - pkpy_to_int(vm, 3, &pyModulateB); - pkpy_to_int(vm, 4, &pyModulateA); - - const SkaEntity entity = (SkaEntity)pyEntityId; - SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - spriteComponent->modulate = ska_color_get_normalized_color(pyModulateR, pyModulateB, pyModulateG, pyModulateA); - return 0; -} - -int32 cre_pkpy_api_sprite_get_origin(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)spriteComponent->origin.x); - pkpy_push_float(vm, (f64)spriteComponent->origin.y); - return 2; -} - -int32 cre_pkpy_api_sprite_set_origin(pkpy_vm* vm) { - int32 pyEntityId; - f64 pyOriginX; - f64 pyOriginY; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &pyOriginX); - pkpy_to_float(vm, 2, &pyOriginY); - - const SkaEntity entity = (SkaEntity)pyEntityId; - SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - spriteComponent->origin = (SkaVector2){ (f32)pyOriginX, (f32)pyOriginY }; - return 0; -} - -int32 cre_pkpy_api_sprite_get_shader_instance(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - const int32 pyShaderInstanceId = spriteComponent->shaderInstanceId != SKA_SHADER_INSTANCE_INVALID_ID ? (int32)spriteComponent->shaderInstanceId : -1; - pkpy_push_int(vm, pyShaderInstanceId); - return 1; -} - -int32 cre_pkpy_api_sprite_set_shader_instance(pkpy_vm* vm) { - int32 pyEntityId; - int32 pyShaderInstanceId; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_int(vm, 1, &pyShaderInstanceId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); - spriteComponent->shaderInstanceId = (SkaShaderInstanceId)pyShaderInstanceId; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(spriteComponent->shaderInstanceId); - ska_renderer_set_sprite_shader_default_params(shaderInstance->shader); - return 0; -} - -//--- ANIMATED SPRITE ---// - -int32 cre_pkpy_api_animated_sprite_play(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_CString pyAnimationName; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_string(vm, 1, &pyAnimationName); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const char* animationName = pyAnimationName; - AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - const bool hasSuccessfullyPlayedAnimation = animated_sprite_component_play_animation(animatedSpriteComponent, animationName); - pkpy_push_bool(vm, hasSuccessfullyPlayedAnimation); - return 1; -} - -int32 cre_pkpy_api_animated_sprite_stop(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - animatedSpriteComponent->isPlaying = false; - return 0; -} - -int32 cre_pkpy_api_animated_sprite_set_current_animation_frame(pkpy_vm* vm) { - int32 pyEntityId; - int32 pyFrame; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_int(vm, 1, &pyFrame); - - const SkaEntity entity = (SkaEntity)pyEntityId; - AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - animatedSpriteComponent->currentAnimation->currentFrame = ska_math_clamp_int(pyFrame, 0, animatedSpriteComponent->currentAnimation->frameCount - 1); - return 0; -} - -int32 cre_pkpy_api_animated_sprite_add_animation(pkpy_vm* vm) { -#define CRE_PKPY_API_ANIM_FRAME_STRIDE 6 - int32 pyEntityId; - pkpy_CString pyAnimationName; - int32 pyAnimationSpeed; - bool pyAnimationDoesLoop; - int32 pyFrameCount; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_string(vm, 1, &pyAnimationName); - pkpy_to_int(vm, 2, &pyAnimationSpeed); - pkpy_to_bool(vm, 3, &pyAnimationDoesLoop); - pkpy_to_int(vm, 4, &pyFrameCount); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const char* animationName = pyAnimationName; - AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - CreAnimation newAnim = { .frameCount = 0, .currentFrame = 0, .speed = pyAnimationSpeed, .name = {'\0'}, .doesLoop = pyAnimationDoesLoop, .isValid = true }; - ska_strcpy(newAnim.name, animationName); - // Unpack *args filled with frame data - pkpy_unpack_sequence(vm, pyFrameCount * CRE_PKPY_API_ANIM_FRAME_STRIDE); - for (int32 i = 0; i < pyFrameCount; i++) { - int32 pyFrame; - pkpy_CString pyTexturePath; - f64 pyDrawSourceX; - f64 pyDrawSourceY; - f64 pyDrawSourceW; - f64 pyDrawSourceH; - const int32 dataIndex = i * CRE_PKPY_API_ANIM_FRAME_STRIDE; - pkpy_to_int(vm, dataIndex, &pyFrame); - pkpy_to_string(vm, dataIndex + 1, &pyTexturePath); - pkpy_to_float(vm, dataIndex + 2, &pyDrawSourceX); - pkpy_to_float(vm, dataIndex + 3, &pyDrawSourceY); - pkpy_to_float(vm, dataIndex + 4, &pyDrawSourceW); - pkpy_to_float(vm, dataIndex + 5, &pyDrawSourceH); - - const char* texturePath = pyTexturePath; - CreAnimationFrame animationFrame = { - .frame = pyFrame, - .texture = ska_asset_manager_get_texture(texturePath), - .drawSource = { (f32)pyDrawSourceX, (f32)pyDrawSourceY, (f32)pyDrawSourceW, (f32)pyDrawSourceH } - }; - newAnim.animationFrames[newAnim.frameCount++] = animationFrame; - } - // Finally add the animation - animated_sprite_component_add_animation(animatedSpriteComponent, newAnim);\ - // If the only animation then set the current to that - if (animatedSpriteComponent->animationCount == 1) { - animated_sprite_component_set_animation(animatedSpriteComponent, newAnim.name); - } - return 0; -#undef CRE_PKPY_API_ANIM_FRAME_STRIDE -} - -int32 cre_pkpy_api_animated_sprite_get_stagger_animation_start_times(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - pkpy_push_bool(vm, animatedSpriteComponent->staggerStartAnimationTimes); - return 1; -} - -int32 cre_pkpy_api_animated_sprite_set_stagger_animation_start_times(pkpy_vm* vm) { - int32 pyEntityId; - bool pyStaggerStartAnimationTimes; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_bool(vm, 1, &pyStaggerStartAnimationTimes); - - const SkaEntity entity = (SkaEntity)pyEntityId; - AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - animatedSpriteComponent->staggerStartAnimationTimes = pyStaggerStartAnimationTimes; - return 0; -} - -int32 cre_pkpy_api_animated_sprite_get_flip_h(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - pkpy_push_bool(vm, animatedSpriteComponent->flipH); - return 1; -} - -int32 cre_pkpy_api_animated_sprite_set_flip_h(pkpy_vm* vm) { - int32 pyEntityId; - bool pySetFlipH; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_bool(vm, 1, &pySetFlipH); - - const SkaEntity entity = (SkaEntity)pyEntityId; - AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - animatedSpriteComponent->flipH = pySetFlipH; - return 0; -} - -int32 cre_pkpy_api_animated_sprite_get_flip_v(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - pkpy_push_bool(vm, animatedSpriteComponent->flipV); - return 1; -} - -int32 cre_pkpy_api_animated_sprite_set_flip_v(pkpy_vm* vm) { - int32 pyEntityId; - bool pySetFlipV; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_bool(vm, 1, &pySetFlipV); - - const SkaEntity entity = (SkaEntity)pyEntityId; - AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - animatedSpriteComponent->flipV = pySetFlipV; - return 0; -} - -int32 cre_pkpy_api_animated_sprite_get_modulate(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - const CreColor color = convert_ska_color_to_cre(&animatedSpriteComponent->modulate); - pkpy_push_int(vm, color.r); - pkpy_push_int(vm, color.g); - pkpy_push_int(vm, color.b); - pkpy_push_int(vm, color.a); - return 4; -} - -int32 cre_pkpy_api_animated_sprite_set_modulate(pkpy_vm* vm) { - int32 pyEntityId; - int32 pyModulateR, pyModulateG, pyModulateB, pyModulateA; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_int(vm, 1, &pyModulateR); - pkpy_to_int(vm, 2, &pyModulateG); - pkpy_to_int(vm, 3, &pyModulateB); - pkpy_to_int(vm, 4, &pyModulateA); - - const SkaEntity entity = (SkaEntity)pyEntityId; - AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - animatedSpriteComponent->modulate = ska_color_get_normalized_color(pyModulateR, pyModulateB, pyModulateG, pyModulateA); - return 0; -} - -int32 cre_pkpy_api_animated_sprite_get_origin(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)animatedSpriteComponent->origin.x); - pkpy_push_float(vm, (f64)animatedSpriteComponent->origin.y); - return 2; -} - -int32 cre_pkpy_api_animated_sprite_set_origin(pkpy_vm* vm) { - int32 pyEntityId; - f64 pyOriginX; - f64 pyOriginY; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &pyOriginX); - pkpy_to_float(vm, 2, &pyOriginY); - - const SkaEntity entity = (SkaEntity)pyEntityId; - AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - animatedSpriteComponent->origin = (SkaVector2){ (f32)pyOriginX, (f32)pyOriginY }; - return 0; -} - -int32 cre_pkpy_api_animated_sprite_get_shader_instance(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - const int32 pyShaderInstanceId = animatedSpriteComponent->shaderInstanceId != SKA_SHADER_INSTANCE_INVALID_ID ? (int32)animatedSpriteComponent->shaderInstanceId : -1; - pkpy_push_int(vm, pyShaderInstanceId); - return 1; -} - -int32 cre_pkpy_api_animated_sprite_set_shader_instance(pkpy_vm* vm) { - int32 pyEntityId; - int32 pyShaderInstanceId; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_int(vm, 1, &pyShaderInstanceId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - animatedSpriteComponent->shaderInstanceId = (SkaShaderInstanceId)pyShaderInstanceId; - SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(animatedSpriteComponent->shaderInstanceId); - ska_renderer_set_sprite_shader_default_params(shaderInstance->shader); - return 0; -} - -//--- TEXT LABEL ---// - -int32 cre_pkpy_api_text_label_get_text(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); - pkpy_push_string(vm, pkpy_string(textLabelComponent->text)); - return 1; -} - -int32 cre_pkpy_api_text_label_set_text(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_CString pyText; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_string(vm, 1, &pyText); - - static char textBuffer[2048]; - ska_strcpy(textBuffer, pyText); - const SkaEntity entity = (SkaEntity)pyEntityId; - TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); - ska_strcpy(textLabelComponent->text, textBuffer); - return 0; -} - -int32 cre_pkpy_api_text_label_get_color(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); - const CreColor color = convert_ska_color_to_cre(&textLabelComponent->color); - pkpy_push_int(vm, color.r); - pkpy_push_int(vm, color.g); - pkpy_push_int(vm, color.b); - pkpy_push_int(vm, color.a); - return 4; -} - -int32 cre_pkpy_api_text_label_set_color(pkpy_vm* vm) { - int32 pyEntityId; - int32 pyRed, pyGreen, pyBlue, pyAlpha; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_int(vm, 1, &pyRed); - pkpy_to_int(vm, 2, &pyGreen); - pkpy_to_int(vm, 3, &pyBlue); - pkpy_to_int(vm, 4, &pyAlpha); - - const SkaEntity entity = (SkaEntity)pyEntityId; - TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); - textLabelComponent->color = ska_color_get_normalized_color(pyRed, pyGreen, pyBlue, pyAlpha); - return 0; -} - -int32 cre_pkpy_api_text_label_set_font_uid(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_CString pyFontUID; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_string(vm, 1, &pyFontUID); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const char* fontUID = pyFontUID; - if (ska_asset_manager_has_font(fontUID)) { - TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); - textLabelComponent->font = ska_asset_manager_get_font(fontUID); - } else { - ska_logger_warn("Failed to set font to '%s' as it doesn't exist in the asset manager!", fontUID); - } - return 0; -} - -// --- COLLIDER2D ---// - -int32 cre_pkpy_api_collider2d_get_extents(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Collider2DComponent* collider2DComponent = (Collider2DComponent*)ska_ecs_component_manager_get_component(entity, COLLIDER2D_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)collider2DComponent->extents.w); - pkpy_push_float(vm, (f64)collider2DComponent->extents.h); - return 2; -} - -int32 cre_pkpy_api_collider2d_set_extents(pkpy_vm* vm) { - int32 pyEntityId; - f64 pyExtentsX, pyExtentsY; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &pyExtentsX); - pkpy_to_float(vm, 2, &pyExtentsY); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Collider2DComponent* collider2DComponent = (Collider2DComponent*)ska_ecs_component_manager_get_component(entity, COLLIDER2D_COMPONENT_INDEX); - collider2DComponent->extents = (SkaSize2D){ (f32)pyExtentsX, (f32)pyExtentsY }; - return 0; -} - -int32 cre_pkpy_api_collider2d_get_color(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Collider2DComponent* collider2DComponent = (Collider2DComponent*)ska_ecs_component_manager_get_component(entity, COLLIDER2D_COMPONENT_INDEX); - const CreColor color = convert_ska_color_to_cre(&collider2DComponent->color); - pkpy_push_int(vm, color.r); - pkpy_push_int(vm, color.g); - pkpy_push_int(vm, color.b); - pkpy_push_int(vm, color.a); - return 4; -} - -int32 cre_pkpy_api_collider2d_set_color(pkpy_vm* vm) { - int32 pyEntityId; - int32 pyRed, pyGreen, pyBlue, pyAlpha; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_int(vm, 1, &pyRed); - pkpy_to_int(vm, 2, &pyGreen); - pkpy_to_int(vm, 3, &pyBlue); - pkpy_to_int(vm, 4, &pyAlpha); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Collider2DComponent* collider2DComponent = (Collider2DComponent*)ska_ecs_component_manager_get_component(entity, COLLIDER2D_COMPONENT_INDEX); - collider2DComponent->color = ska_color_get_normalized_color(pyRed, pyGreen, pyBlue, pyAlpha); - return 0; -} - -//--- COLOR RECT ---// - -int32 cre_pkpy_api_color_rect_get_size(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const ColorRectComponent* colorRectComponent = (ColorRectComponent*)ska_ecs_component_manager_get_component(entity, COLOR_RECT_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)colorRectComponent->size.w); - pkpy_push_float(vm, (f64)colorRectComponent->size.h); - return 2; -} - -int32 cre_pkpy_api_color_rect_set_size(pkpy_vm* vm) { - int32 pyEntityId; - f64 pySizeW, pySizeH; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &pySizeW); - pkpy_to_float(vm, 2, &pySizeH); - - const SkaEntity entity = (SkaEntity)pyEntityId; - ColorRectComponent* colorRectComponent = (ColorRectComponent*)ska_ecs_component_manager_get_component(entity, COLOR_RECT_COMPONENT_INDEX); - colorRectComponent->size = (SkaSize2D){ (f32)pySizeW, (f32)pySizeH }; - return 0; -} - -int32 cre_pkpy_api_color_rect_get_color(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const ColorRectComponent* colorRectComponent = (ColorRectComponent*)ska_ecs_component_manager_get_component(entity, COLOR_RECT_COMPONENT_INDEX); - const CreColor color = convert_ska_color_to_cre(&colorRectComponent->color); - pkpy_push_int(vm, color.r); - pkpy_push_int(vm, color.g); - pkpy_push_int(vm, color.b); - pkpy_push_int(vm, color.a); - return 4; -} - -int32 cre_pkpy_api_color_rect_set_color(pkpy_vm* vm) { - int32 pyEntityId; - int32 pyRed, pyGreen, pyBlue, pyAlpha; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_int(vm, 1, &pyRed); - pkpy_to_int(vm, 2, &pyGreen); - pkpy_to_int(vm, 3, &pyBlue); - pkpy_to_int(vm, 4, &pyAlpha); - - const SkaEntity entity = (SkaEntity)pyEntityId; - ColorRectComponent* colorRectComponent = (ColorRectComponent*)ska_ecs_component_manager_get_component(entity, COLOR_RECT_COMPONENT_INDEX); - colorRectComponent->color = ska_color_get_normalized_color(pyRed, pyGreen, pyBlue, pyAlpha); - return 0; -} - -//--- PARALLAX ---// - -int32 cre_pkpy_api_parallax_get_scroll_speed(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const ParallaxComponent* parallaxComponent = (ParallaxComponent*)ska_ecs_component_manager_get_component(entity, PARALLAX_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)parallaxComponent->scrollSpeed.x); - pkpy_push_float(vm, (f64)parallaxComponent->scrollSpeed.y); - return 2; -} - -int32 cre_pkpy_api_parallax_set_scroll_speed(pkpy_vm* vm) { - int32 pyEntityId; - f64 pySpeedX, pySpeedY; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &pySpeedX); - pkpy_to_float(vm, 2, &pySpeedY); - - const SkaEntity entity = (SkaEntity)pyEntityId; - ParallaxComponent* parallaxComponent = (ParallaxComponent*)ska_ecs_component_manager_get_component(entity, PARALLAX_COMPONENT_INDEX); - parallaxComponent->scrollSpeed = (SkaVector2){ (f32)pySpeedX, (f32)pySpeedY }; - return 0; -} - -//--- PARTICLES2D ---// - -int32 cre_pkpy_api_particles2d_get_amount(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - pkpy_push_int(vm, particles2dComponent->amount); - return 1; -} - -int32 cre_pkpy_api_particles2d_set_amount(pkpy_vm* vm) { - int32 pyEntityId; - int32 pyAmount; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_int(vm, 1, &pyAmount); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - particles2dComponent->amount = pyAmount; - // TODO: Update particle stuff... - return 0; -} - -int32 cre_pkpy_api_particles2d_get_life_time(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)particles2dComponent->lifeTime); - return 1; -} - -int32 cre_pkpy_api_particles2d_set_life_time(pkpy_vm* vm) { - int32 pyEntityId; - f64 pyLifeTime; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &pyLifeTime); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - particles2dComponent->lifeTime = (f32)pyLifeTime; - return 0; -} - -int32 cre_pkpy_api_particles2d_get_damping(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)particles2dComponent->damping); - return 1; -} - -int32 cre_pkpy_api_particles2d_set_damping(pkpy_vm* vm) { - int32 pyEntityId; - f64 pyDamping; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &pyDamping); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - particles2dComponent->damping = (f32)pyDamping; - return 0; -} - -int32 cre_pkpy_api_particles2d_get_explosiveness(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)particles2dComponent->explosiveness); - return 1; -} - -int32 cre_pkpy_api_particles2d_set_explosiveness(pkpy_vm* vm) { - int32 pyEntityId; - f64 pyExplosiveness; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &pyExplosiveness); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - particles2dComponent->explosiveness = ska_math_clamp_float((f32)pyExplosiveness, 0.0f, 1.0f); - return 0; -} - -int32 cre_pkpy_api_particles2d_get_color(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - const CreColor color = convert_ska_color_to_cre(&particles2dComponent->color); - pkpy_push_int(vm, color.r); - pkpy_push_int(vm, color.g); - pkpy_push_int(vm, color.b); - pkpy_push_int(vm, color.a); - return 4; -} - -int32 cre_pkpy_api_particles2d_set_color(pkpy_vm* vm) { - int32 pyEntityId; - int32 pyRed, pyGreen, pyBlue, pyAlpha; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_int(vm, 1, &pyRed); - pkpy_to_int(vm, 2, &pyGreen); - pkpy_to_int(vm, 3, &pyBlue); - pkpy_to_int(vm, 4, &pyAlpha); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - particles2dComponent->color = ska_color_get_normalized_color(pyRed, pyGreen, pyBlue, pyAlpha); - return 0; -} - -int32 cre_pkpy_api_particles2d_get_initial_velocity(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)particles2dComponent->initialVelocity.min.x); - pkpy_push_float(vm, (f64)particles2dComponent->initialVelocity.min.y); - pkpy_push_float(vm, (f64)particles2dComponent->initialVelocity.max.x); - pkpy_push_float(vm, (f64)particles2dComponent->initialVelocity.max.y); - return 4; -} - -int32 cre_pkpy_api_particles2d_set_initial_velocity(pkpy_vm* vm) { - int32 pyEntityId; - f64 pyMinX, pyMinY; - f64 pyMaxX, pyMaxY; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &pyMinX); - pkpy_to_float(vm, 2, &pyMinY); - pkpy_to_float(vm, 3, &pyMaxX); - pkpy_to_float(vm, 4, &pyMaxY); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - particles2dComponent->initialVelocity = (SkaMinMaxVec2){ - .min = { .x = (f32)pyMinX, .y = (f32)pyMinY }, - .max = { .x = (f32)pyMaxX, .y = (f32)pyMaxY } - }; - return 0; -} - -int32 cre_pkpy_api_particles2d_get_spread(pkpy_vm* vm) { - int32 pyEntityId; - pkpy_to_int(vm, 0, &pyEntityId); - - const SkaEntity entity = (SkaEntity)pyEntityId; - const Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - pkpy_push_float(vm, (f64)particles2dComponent->spread); - return 1; -} - -int32 cre_pkpy_api_particles2d_set_spread(pkpy_vm* vm) { - int32 pyEntityId; - f64 pySpread; - pkpy_to_int(vm, 0, &pyEntityId); - pkpy_to_float(vm, 1, &pySpread); - - const SkaEntity entity = (SkaEntity)pyEntityId; - Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); - particles2dComponent->spread = (f32)pySpread; - return 0; -} -*/ diff --git a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.h b/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.h deleted file mode 100644 index 2e7b59596..000000000 --- a/engine/src/core/scripting/python/pocketpy/api/cre_pkpy_api_node.h +++ /dev/null @@ -1,110 +0,0 @@ -#pragma once -/* -#include - -struct pkpy_vm_handle; - -// Node -int32 cre_pkpy_api_node_new(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node_get_name(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node_add_child(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node_get_child(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node_get_children(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node_get_parent(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node_queue_deletion(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node_is_queued_for_deletion(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node_set_time_dilation(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node_get_time_dilation(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node_get_total_time_dilation(struct pkpy_vm_handle* vm); - -// Node2D -int32 cre_pkpy_api_node2d_set_position(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_add_to_position(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_get_position(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_get_global_position(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_set_scale(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_add_to_scale(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_get_scale(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_set_rotation(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_add_to_rotation(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_get_rotation(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_set_z_index(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_get_z_index(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_set_z_index_relative_to_parent(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_get_z_index_relative_to_parent(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_set_ignore_camera(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_node2d_get_ignore_camera(struct pkpy_vm_handle* vm); - -// Sprite -int32 cre_pkpy_api_sprite_get_texture(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_set_texture(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_get_draw_source(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_set_draw_source(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_get_flip_h(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_set_flip_h(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_get_flip_v(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_set_flip_v(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_get_modulate(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_set_modulate(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_get_origin(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_set_origin(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_get_shader_instance(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_sprite_set_shader_instance(struct pkpy_vm_handle* vm); - -// Animated Sprite -int32 cre_pkpy_api_animated_sprite_play(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_stop(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_set_current_animation_frame(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_add_animation(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_get_stagger_animation_start_times(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_set_stagger_animation_start_times(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_get_flip_h(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_set_flip_h(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_get_flip_v(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_set_flip_v(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_get_modulate(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_set_modulate(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_get_origin(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_set_origin(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_get_shader_instance(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_animated_sprite_set_shader_instance(struct pkpy_vm_handle* vm); - -// Text Label -int32 cre_pkpy_api_text_label_get_text(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_text_label_set_text(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_text_label_get_color(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_text_label_set_color(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_text_label_set_font_uid(struct pkpy_vm_handle* vm); - -// Collider2D -int32 cre_pkpy_api_collider2d_get_extents(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_collider2d_set_extents(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_collider2d_get_color(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_collider2d_set_color(struct pkpy_vm_handle* vm); - -// Color Rect -int32 cre_pkpy_api_color_rect_get_size(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_color_rect_set_size(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_color_rect_get_color(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_color_rect_set_color(struct pkpy_vm_handle* vm); - -// Parallax -int32 cre_pkpy_api_parallax_get_scroll_speed(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_parallax_set_scroll_speed(struct pkpy_vm_handle* vm); - -// Particles2D -int32 cre_pkpy_api_particles2d_get_amount(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_set_amount(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_get_life_time(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_set_life_time(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_get_damping(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_set_damping(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_get_explosiveness(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_set_explosiveness(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_get_color(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_set_color(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_get_initial_velocity(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_set_initial_velocity(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_get_spread(struct pkpy_vm_handle* vm); -int32 cre_pkpy_api_particles2d_set_spread(struct pkpy_vm_handle* vm); -*/ \ No newline at end of file diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.c b/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.c deleted file mode 100644 index 044302009..000000000 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.c +++ /dev/null @@ -1,106 +0,0 @@ -#include "cre_pkpy_entity_instance_cache.h" - -#include - -#include - -#include "cre_pkpy_util.h" -#include "cre_pkpy_api_source.h" -#include "../../../ecs/ecs_globals.h" -#include "../../../ecs/components/node_component.h" -#include "../../../ecs/components/script_component.h" -/* -static bool entity_instance_cache_is_initialized = false; - -void cre_pkpy_entity_instance_cache_initialize(pkpy_vm* vm) { - if (!entity_instance_cache_is_initialized) { - pkpy_push_module(vm, "crescent_internal_py"); - pkpy_exec_2(vm, CRE_PKPY_CRESCENT_INTERNAL_PY_SOURCE, "crescent_internal_py.py", 0, "crescent_internal_py"); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_exec(vm, "import crescent_internal_py"); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_pop_top(vm); - entity_instance_cache_is_initialized = true; - } -} - -void cre_pkpy_entity_instance_cache_finalize(pkpy_vm* vm) { - if (entity_instance_cache_is_initialized) { - // TODO: Similar to 'pkpy_sc_on_script_context_destroy' commenting out since we're leaking the vm instance for now - // pkpy_exec(vm, "crescent_internal_py.CRE_ENTITY_TO_NODE_MAP.clear()"); - // SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - entity_instance_cache_is_initialized = false; - } -} - -SkaEntity cre_pkpy_entity_instance_cache_create_new_entity(pkpy_vm* vm, const char* classPath, const char* className, SkaEntity entity) { - SKA_ASSERT(entity != SKA_NULL_ENTITY); - // import module first - char importCmdBuffer[96]; - sprintf(importCmdBuffer, "from %s import %s", classPath, className); - pkpy_exec(vm, importCmdBuffer); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - - pkpy_getglobal(vm, pkpy_name(className)); - pkpy_push_null(vm); - pkpy_push_int(vm, (int32)entity); - pkpy_vectorcall(vm, 1); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - - pkpy_eval(vm, "crescent_internal_py"); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_getattr(vm, pkpy_name("set_entity")); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_rot_two(vm); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_push_null(vm); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_rot_two(vm); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_vectorcall(vm, 1); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_pop_top(vm); - - // Add script component if missing - if (!ska_ecs_component_manager_has_component(entity, SCRIPT_COMPONENT_INDEX)) { - ska_ecs_component_manager_set_component(entity, SCRIPT_COMPONENT_INDEX, script_component_create_ex(classPath, className, ScriptContextType_PYTHON)); - } - - return entity; -} - -bool cre_pkpy_entity_instance_cache_create_new_entity_if_nonexistent(pkpy_vm* vm, const char* classPath, const char* className, SkaEntity entity) { - if (!cre_pkpy_entity_instance_cache_has_entity(vm, entity)) { - cre_pkpy_entity_instance_cache_create_new_entity(vm, classPath, className, entity); - return true; - } - return false; -} - -void cre_pkpy_entity_instance_cache_remove_entity(pkpy_vm* vm, SkaEntity entity) { - char entityStringBuffer[56]; - sprintf(entityStringBuffer, "del crescent_internal_py.CRE_ENTITY_TO_NODE_MAP[%d]", entity); - pkpy_exec(vm, entityStringBuffer); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); -} - -bool cre_pkpy_entity_instance_cache_has_entity(pkpy_vm *vm, SkaEntity entity) { - const int stackSizeBeforeEval = pkpy_stack_size(vm); - char entityStringBuffer[56]; - sprintf(entityStringBuffer, "%d in crescent_internal_py.CRE_ENTITY_TO_NODE_MAP", entity); - pkpy_eval(vm, entityStringBuffer); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - bool doesKeyExist = false; - pkpy_to_bool(vm, stackSizeBeforeEval, &doesKeyExist); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_pop_top(vm); - return doesKeyExist; -} - -void cre_pkpy_entity_instance_cache_push_entity_instance(pkpy_vm* vm, SkaEntity entity) { - char entityStringBuffer[56]; - sprintf(entityStringBuffer, "crescent_internal_py.CRE_ENTITY_TO_NODE_MAP[%d]", entity); - pkpy_eval(vm, entityStringBuffer); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); -} -*/ \ No newline at end of file diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.h deleted file mode 100644 index ceb784d36..000000000 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -/* -#include - -#include - -// Assumes new vm instance -void cre_pkpy_entity_instance_cache_initialize(pkpy_vm* vm); -void cre_pkpy_entity_instance_cache_finalize(pkpy_vm* vm); -SkaEntity cre_pkpy_entity_instance_cache_create_new_entity(pkpy_vm* vm, const char* classPath, const char* className, SkaEntity entity); -// Returns true if an entity is created -bool cre_pkpy_entity_instance_cache_create_new_entity_if_nonexistent(pkpy_vm* vm, const char* classPath, const char* className, SkaEntity entity); -void cre_pkpy_entity_instance_cache_remove_entity(pkpy_vm* vm, SkaEntity entity); -bool cre_pkpy_entity_instance_cache_has_entity(pkpy_vm* vm, SkaEntity entity); -void cre_pkpy_entity_instance_cache_push_entity_instance(pkpy_vm* vm, SkaEntity entity); -*/ \ No newline at end of file diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event.h deleted file mode 100644 index 8e334641f..000000000 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#define CRE_PKPY_NODE_EVENT_NAME_SCENE_ENTERED "scene_entered" -#define CRE_PKPY_NODE_EVENT_NAME_SCENE_EXITED "scene_exited" -#define CRE_PKPY_NODE_EVENT_NAME_FRAME_CHANGED "frame_changed" -#define CRE_PKPY_NODE_EVENT_NAME_ANIMATION_FINISHED "animation_finished" -#define CRE_PKPY_NODE_EVENT_NAME_MESSAGE_RECEIVED "message_received" - -#define CRE_PKPY_NODE_EVENT_OWNER_ID_NETWORK (-100) diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.c b/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.c deleted file mode 100644 index 3b228822a..000000000 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.c +++ /dev/null @@ -1,92 +0,0 @@ -#include "cre_pkpy_node_event_manager.h" -/* -#include - -#include "cre_pkpy_util.h" - - -static inline void cre_pkpy_node_event_manager_push_broadcast_and_initial_args(pkpy_vm* vm, int32 ownerId, const char* eventName) { - cre_pkpy_node_event_manager_push_broadcast_event_func(vm); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_push_null(vm); - pkpy_push_int(vm, ownerId); - pkpy_push_string(vm, pkpy_string(eventName)); -} - -static inline bool cre_pkpy_node_event_manager_push_create_event_func(pkpy_vm* vm) { - return pkpy_eval(vm, "crescent._node_event_manager.create_event"); -} - -static inline bool cre_pkpy_node_event_manager_push_remove_entity_and_connections_func(pkpy_vm* vm) { - return pkpy_eval(vm, "crescent._node_event_manager.remove_entity_and_connections"); -} - -void cre_pkpy_node_event_manager_initialize(pkpy_vm* vm) { - pkpy_exec(vm, "import crescent"); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); -} - -void cre_pkpy_node_event_manager_finalize() {} - -void cre_pkpy_node_event_manager_create_event(pkpy_vm* vm, int32 ownerId, const char* eventName) { - cre_pkpy_node_event_manager_push_create_event_func(vm); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_push_null(vm); - pkpy_push_int(vm, ownerId); - pkpy_push_string(vm, pkpy_string(eventName)); - pkpy_vectorcall(vm, 2); - pkpy_pop_top(vm); -} - -void cre_pkpy_node_event_manager_remove_entity_and_connections(pkpy_vm* vm, int32 ownerId) { - cre_pkpy_node_event_manager_push_remove_entity_and_connections_func(vm); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_push_null(vm); - pkpy_push_int(vm, ownerId); - pkpy_vectorcall(vm, 1); - pkpy_pop_top(vm); -} - -bool cre_pkpy_node_event_manager_push_broadcast_event_func(pkpy_vm* vm) { - return pkpy_eval(vm, "crescent._node_event_manager.broadcast_event"); -} - -void cre_pkpy_node_event_manager_broadcast_event(pkpy_vm* vm, int32 ownerId, const char* eventName) { - cre_pkpy_node_event_manager_push_broadcast_and_initial_args(vm, ownerId, eventName); - pkpy_vectorcall(vm, 2); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_pop_top(vm); -} - -void cre_pkpy_node_event_manager_broadcast_event_bool(pkpy_vm* vm, int32 ownerId, const char* eventName, bool value) { - cre_pkpy_node_event_manager_push_broadcast_and_initial_args(vm, ownerId, eventName); - pkpy_push_bool(vm, value); - pkpy_vectorcall(vm, 3); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_pop_top(vm); -} - -void cre_pkpy_node_event_manager_broadcast_event_int(pkpy_vm* vm, int32 ownerId, const char* eventName, int32 value) { - cre_pkpy_node_event_manager_push_broadcast_and_initial_args(vm, ownerId, eventName); - pkpy_push_int(vm, value); - pkpy_vectorcall(vm, 3); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_pop_top(vm); -} - -void cre_pkpy_node_event_manager_broadcast_event_float(pkpy_vm* vm, int32 ownerId, const char* eventName, f32 value) { - cre_pkpy_node_event_manager_push_broadcast_and_initial_args(vm, ownerId, eventName); - pkpy_push_float(vm, (f64)value); - pkpy_vectorcall(vm, 3); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_pop_top(vm); -} - -void cre_pkpy_node_event_manager_broadcast_event_string(pkpy_vm* vm, int32 ownerId, const char* eventName, const char* value) { - cre_pkpy_node_event_manager_push_broadcast_and_initial_args(vm, ownerId, eventName); - pkpy_push_string(vm, pkpy_string(value)); - pkpy_vectorcall(vm, 3); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_pop_top(vm); -} -*/ diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.h deleted file mode 100644 index 55e9d30da..000000000 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -/* -#include - -#include - -struct pkpy_vm_handle; - -void cre_pkpy_node_event_manager_initialize(struct pkpy_vm_handle* vm); -void cre_pkpy_node_event_manager_finalize(); - -void cre_pkpy_node_event_manager_create_event(struct pkpy_vm_handle* vm, int32 ownerId, const char* eventName); -void cre_pkpy_node_event_manager_remove_entity_and_connections(struct pkpy_vm_handle* vm, int32 ownerId); - -// Broadcast event functions - -bool cre_pkpy_node_event_manager_push_broadcast_event_func(struct pkpy_vm_handle *vm); -void cre_pkpy_node_event_manager_broadcast_event(struct pkpy_vm_handle* vm, int32 ownerId, const char* eventName); -void cre_pkpy_node_event_manager_broadcast_event_bool(struct pkpy_vm_handle* vm, int32 ownerId, const char* eventName, bool value); -void cre_pkpy_node_event_manager_broadcast_event_int(struct pkpy_vm_handle* vm, int32 ownerId, const char* eventName, int32 value); -void cre_pkpy_node_event_manager_broadcast_event_float(struct pkpy_vm_handle* vm, int32 ownerId, const char* eventName, f32 value); -void cre_pkpy_node_event_manager_broadcast_event_string(struct pkpy_vm_handle* vm, int32 ownerId, const char* eventName, const char* value); -*/ diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.c deleted file mode 100644 index 55de2ecb3..000000000 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.c +++ /dev/null @@ -1,300 +0,0 @@ -#include "cre_pkpy_script_context.h" -/* -#include - -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "cre_pkpy.h" -#include "cre_pkpy_entity_instance_cache.h" -#include "cre_pkpy_node_event.h" -#include "cre_pkpy_node_event_manager.h" -#include "api/cre_pkpy_api.h" -#include "../../script_context.h" -#include "../../../ecs/components/animated_sprite_component.h" -#include "../../../ecs/component.h" -#include "../../../ecs/components/node_component.h" -#include "../../../ecs/components/script_component.h" -#include "../../../engine_context.h" -#include "../../../ecs/ecs_globals.h" - -//--- Script Context Interface ---// -void pkpy_sc_on_delete_instance(SkaEntity entity); -void pkpy_sc_on_start(SkaEntity entity); -void pkpy_sc_on_update_instance(SkaEntity entity, f32 deltaTime); -void pkpy_sc_on_fixed_update_instance(SkaEntity entity, f32 deltaTime); -void pkpy_sc_on_end(SkaEntity entity); -void pkpy_sc_on_network_callback(const char* message); -void pkpy_sc_on_script_context_destroy(); - -// Custom Import Handler -unsigned char* cre_pkpy_import_handler(const char* path, int32 pathSize, int32* outSize); - -//--- Node Event Callbacks ---// -void pkpy_node_event_node_scene_entered(SkaSubjectNotifyPayload* payload); -void pkpy_node_event_node_scene_exited(SkaSubjectNotifyPayload* payload); - -void pkpy_node_event_animated_sprite_frame_changed(SkaSubjectNotifyPayload* payload); -void pkpy_node_event_animated_sprite_animation_finished(SkaSubjectNotifyPayload* payload); - -// Observers -SkaObserver node_scene_entered_observer = { .on_notify = pkpy_node_event_node_scene_entered }; -SkaObserver node_scene_exited_observer = { .on_notify = pkpy_node_event_node_scene_exited }; - -SkaObserver animated_sprite_frame_changed_observer = { .on_notify = pkpy_node_event_animated_sprite_frame_changed }; -SkaObserver animated_sprite_animation_finished_observer = { .on_notify = pkpy_node_event_animated_sprite_animation_finished }; - -CREScriptContext* pkpy_script_context = NULL; -pkpy_vm* cre_pkpy_vm = NULL; - -pkpy_CName pyStartFunctionName; -pkpy_CName pyProcessFunctionName; -pkpy_CName pyFixedProcessFunctionName; -pkpy_CName pyEndFunctionName; - -SKA_STATIC_ARRAY_CREATE(SkaEntity, SKA_MAX_ENTITIES, entityInitializedList); - -CREScriptContext* cre_pkpy_script_context_create() { - SKA_ASSERT_FMT(pkpy_script_context == NULL, "Script context already created!"); - CREScriptContext* scriptContext = cre_script_context_create(); - scriptContext->on_create_instance = cre_pkpy_script_context_create_instance_if_nonexistent; // Using nonexistent version in case added to entity cache from elsewhere - scriptContext->on_delete_instance = pkpy_sc_on_delete_instance; - scriptContext->on_start = pkpy_sc_on_start; - scriptContext->on_update_instance = pkpy_sc_on_update_instance; - scriptContext->on_fixed_update_instance = pkpy_sc_on_fixed_update_instance; - scriptContext->on_end = pkpy_sc_on_end; - scriptContext->on_network_callback = pkpy_sc_on_network_callback; - scriptContext->on_script_context_destroy = pkpy_sc_on_script_context_destroy; - - pkpy_script_context = scriptContext; - - if (!cre_pkpy_vm) { - cre_pkpy_vm = pkpy_new_vm(false); - pyStartFunctionName = pkpy_name("_start"); - pyProcessFunctionName = pkpy_name("_process"); - pyFixedProcessFunctionName = pkpy_name("_fixed_process"); - pyEndFunctionName = pkpy_name("_end"); - - pkpy_set_import_handler(cre_pkpy_vm, cre_pkpy_import_handler); - // TODO: create and set pkpy output handler - } - - cre_pkpy_api_load_internal_modules(cre_pkpy_vm); - cre_pkpy_node_event_manager_initialize(cre_pkpy_vm); - cre_pkpy_entity_instance_cache_initialize(cre_pkpy_vm); - - return scriptContext; -} - -CREScriptContext* cre_pkpy_script_context_get() { - return pkpy_script_context; -} - -void cre_pkpy_script_context_setup_node_event(SkaEntity entity) { - if (!entityInitializedList[entity]) { - entityInitializedList[entity] = true; - // Node - NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); - SKA_ASSERT_FMT(nodeComponent, "Node component should never be null! Entity id: '%u'", entity); - ska_event_register_observer(&nodeComponent->onSceneTreeEnter, &node_scene_entered_observer); - ska_event_register_observer(&nodeComponent->onSceneTreeExit, &node_scene_exited_observer); - // Animated Sprite - AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*) ska_ecs_component_manager_get_component_unchecked(entity, ANIMATED_SPRITE_COMPONENT_INDEX); - if (animatedSpriteComponent) { - ska_event_register_observer(&animatedSpriteComponent->onFrameChanged, &animated_sprite_frame_changed_observer); - ska_event_register_observer(&animatedSpriteComponent->onAnimationFinished, &animated_sprite_animation_finished_observer); - } - } -} - -void cre_pkpy_script_context_create_instance(SkaEntity entity, const char* classPath, const char* className) { - SKA_ASSERT(cre_pkpy_vm); - cre_pkpy_entity_instance_cache_create_new_entity(cre_pkpy_vm, classPath, className, entity); - cre_pkpy_script_context_setup_node_event(entity); -} - -void cre_pkpy_script_context_create_instance_if_nonexistent(SkaEntity entity, const char* classPath, const char* className) { - SKA_ASSERT(cre_pkpy_vm); - // If exists, just make sure node events are setup - if (cre_pkpy_entity_instance_cache_has_entity(cre_pkpy_vm, entity)) { - cre_pkpy_script_context_setup_node_event(entity); - } else { - cre_pkpy_script_context_create_instance(entity, classPath, className); - } -} - -void cre_pkpy_script_context_create_instance_if_nonexistent_and_push_entity_instance(SkaEntity entity) { - SKA_ASSERT(cre_pkpy_vm); - const NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); - ScriptComponent* scriptComponent = (ScriptComponent*)ska_ecs_component_manager_get_component_unchecked(entity, SCRIPT_COMPONENT_INDEX); - if (!scriptComponent) { - // If an entity doesn't have a script component at this point, it should just be a plain base class from the crescent module. - // Since that is the case, just create a script component so that we can clean up within the script context when the entity - // needs to leave the instance cache. - const char* baseClassName = node_get_base_type_string(nodeComponent->type); - scriptComponent = script_component_create_ex(CRE_PKPY_MODULE_NAME_CRESCENT, baseClassName, ScriptContextType_PYTHON); - ska_ecs_component_manager_set_component(entity, SCRIPT_COMPONENT_INDEX, scriptComponent); - } - cre_pkpy_script_context_create_instance_if_nonexistent(entity, scriptComponent->classPath, scriptComponent->className); - cre_pkpy_entity_instance_cache_push_entity_instance(cre_pkpy_vm, entity); -} - -void pkpy_sc_on_delete_instance(SkaEntity entity) { - SKA_ASSERT(cre_pkpy_vm); - cre_pkpy_entity_instance_cache_remove_entity(cre_pkpy_vm, entity); - - ska_array_utils_remove_item_uint32( - pkpy_script_context->updateEntities, - &pkpy_script_context->updateEntityCount, - entity, - SKA_NULL_ENTITY - ); - - ska_array_utils_remove_item_uint32( - pkpy_script_context->fixedUpdateEntities, - &pkpy_script_context->fixedUpdateEntityCount, - entity, - SKA_NULL_ENTITY - ); - - entityInitializedList[entity] = false; -} - -void pkpy_sc_on_start(SkaEntity entity) { - SKA_ASSERT(cre_pkpy_vm); - // Check for process funcs - cre_pkpy_entity_instance_cache_push_entity_instance(cre_pkpy_vm, entity); - if (pkpy_getattr(cre_pkpy_vm, pyStartFunctionName)) { - pkpy_push_null(cre_pkpy_vm); - pkpy_vectorcall(cre_pkpy_vm, 0); - } - pkpy_pop_top(cre_pkpy_vm); - cre_pkpy_entity_instance_cache_push_entity_instance(cre_pkpy_vm, entity); - if (pkpy_getattr(cre_pkpy_vm, pyProcessFunctionName)) { - pkpy_script_context->updateEntities[pkpy_script_context->updateEntityCount++] = entity; - } - pkpy_pop_top(cre_pkpy_vm); - cre_pkpy_entity_instance_cache_push_entity_instance(cre_pkpy_vm, entity); - if (pkpy_getattr(cre_pkpy_vm, pyFixedProcessFunctionName)) { - pkpy_script_context->fixedUpdateEntities[pkpy_script_context->fixedUpdateEntityCount++] = entity; - } - pkpy_pop_top(cre_pkpy_vm); -} - -void pkpy_sc_on_update_instance(SkaEntity entity, float deltaTime) { - SKA_ASSERT(cre_pkpy_vm); - cre_pkpy_entity_instance_cache_push_entity_instance(cre_pkpy_vm, entity); - const bool hasFunc = pkpy_getattr(cre_pkpy_vm, pyProcessFunctionName); - SKA_ASSERT(hasFunc); - pkpy_push_null(cre_pkpy_vm); - pkpy_push_float(cre_pkpy_vm, (f64)deltaTime); - pkpy_vectorcall(cre_pkpy_vm, 1); - pkpy_pop_top(cre_pkpy_vm); -} - -void pkpy_sc_on_fixed_update_instance(SkaEntity entity, float deltaTime) { - SKA_ASSERT(cre_pkpy_vm); - cre_pkpy_entity_instance_cache_push_entity_instance(cre_pkpy_vm, entity); - const bool hasFunc = pkpy_getattr(cre_pkpy_vm, pyFixedProcessFunctionName); - SKA_ASSERT(hasFunc); - pkpy_push_null(cre_pkpy_vm); - pkpy_push_float(cre_pkpy_vm, (f64)deltaTime); - pkpy_vectorcall(cre_pkpy_vm, 1); - pkpy_pop_top(cre_pkpy_vm); -} - -void pkpy_sc_on_end(SkaEntity entity) { - SKA_ASSERT(cre_pkpy_vm); - cre_pkpy_entity_instance_cache_push_entity_instance(cre_pkpy_vm, entity); - if (pkpy_getattr(cre_pkpy_vm, pyEndFunctionName)) { - pkpy_push_null(cre_pkpy_vm); - pkpy_vectorcall(cre_pkpy_vm, 0); - } - pkpy_pop_top(cre_pkpy_vm); -} - -void pkpy_sc_on_network_callback(const char* message) { - SKA_ASSERT(cre_pkpy_vm); - cre_pkpy_node_event_manager_broadcast_event_string(cre_pkpy_vm, CRE_PKPY_NODE_EVENT_OWNER_ID_NETWORK, CRE_PKPY_NODE_EVENT_NAME_MESSAGE_RECEIVED, message); -} - -void pkpy_sc_on_script_context_destroy() { - cre_pkpy_node_event_manager_finalize(); - cre_pkpy_entity_instance_cache_finalize(cre_pkpy_vm); - - SKA_ASSERT(cre_pkpy_vm); - // TODO: Known memory leak here due to issue with deleting pkpy vm multiple times during testing, not an issue in real world since we won't re-init the vm - // pkpy_delete_vm(cre_pkpy_vm); - cre_pkpy_vm = NULL; - - SKA_MEM_FREE(pkpy_script_context); - pkpy_script_context = NULL; - - memset(entityInitializedList, 0, sizeof(entityInitializedList)); -} - -pkpy_vm* cre_pkpy_script_context_get_active_pkpy_vm() { - return cre_pkpy_vm; -} - -void cre_pkpy_script_context_on_network_udp_server_client_connected() {} - -//--- Custom Import Handler ---// -unsigned char* cre_pkpy_import_handler(const char* path, int pathSize, int* outSize) { -#define CRE_PKPY_IMPORT_HANDLER_PATH_BUFFER_SIZE 512 - SKA_ASSERT_FMT( - pathSize <= CRE_PKPY_IMPORT_HANDLER_PATH_BUFFER_SIZE, - "Passed in pkpy path size is '%d' while 'CRE_PKPY_IMPORT_HANDLER_PATH_BUFFER_SIZE' is '%d', consider increasing CRE_PKPY_IMPORT_HANDLER_PATH_BUFFER_SIZE!", - pathSize, CRE_PKPY_IMPORT_HANDLER_PATH_BUFFER_SIZE - ); - // Construct path, adds full path if loading from disk - char pathBuffer[CRE_PKPY_IMPORT_HANDLER_PATH_BUFFER_SIZE]; - ska_str_trim_by_size(path, pathBuffer, pathSize); - ska_logger_debug("Importing pkpy module from path '%s'", pathBuffer); - // Now attempt to load - char* moduleString = ska_asset_file_loader_read_file_contents_as_string(pathBuffer, NULL); - if (!moduleString) { - ska_logger_error("Failed to load pkpy module at path'%s'", pathBuffer); - *outSize = 0; - return NULL; - } - size_t moduleDataSize; - unsigned char* cachedImportData = ska_str_convert_string_to_unsigned_char(moduleString, &moduleDataSize); - SKA_MEM_FREE(moduleString); - *outSize = (int)moduleDataSize; - return cachedImportData; -#undef CRE_PKPY_IMPORT_HANDLER_PATH_BUFFER_SIZE -} - -//--- Node event callbacks ---// - -// NODE -void pkpy_node_event_node_scene_entered(SkaSubjectNotifyPayload* payload) { - const SkaEntity entity = *(SkaEntity*)payload->data; - cre_pkpy_node_event_manager_broadcast_event(cre_pkpy_vm, (int)entity, CRE_PKPY_NODE_EVENT_NAME_SCENE_ENTERED); -} - -void pkpy_node_event_node_scene_exited(SkaSubjectNotifyPayload* payload) { - const SkaEntity entity = *(SkaEntity*)payload->data; - cre_pkpy_node_event_manager_broadcast_event(cre_pkpy_vm, (int)entity, CRE_PKPY_NODE_EVENT_NAME_SCENE_EXITED); - cre_pkpy_node_event_manager_remove_entity_and_connections(cre_pkpy_vm, (int)entity); -} - -// ANIMATED SPRITE -void pkpy_node_event_animated_sprite_frame_changed(SkaSubjectNotifyPayload* payload) { - const AnimatedSpriteFrameChangedPayload* eventPayload = (AnimatedSpriteFrameChangedPayload*)payload->data; - cre_pkpy_node_event_manager_broadcast_event_int(cre_pkpy_vm, (int)eventPayload->entity, CRE_PKPY_NODE_EVENT_NAME_FRAME_CHANGED, eventPayload->newFrame); -} - -void pkpy_node_event_animated_sprite_animation_finished(SkaSubjectNotifyPayload* payload) { - const AnimatedSpriteAnimationFinishedPayload* eventPayload = (AnimatedSpriteAnimationFinishedPayload*)payload->data; - cre_pkpy_node_event_manager_broadcast_event_string(cre_pkpy_vm, (int)eventPayload->entity, CRE_PKPY_NODE_EVENT_NAME_ANIMATION_FINISHED, eventPayload->animation->name); -} -*/ diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.h deleted file mode 100644 index 3107ba675..000000000 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_script_context.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -/* -#include - -struct CREScriptContext* cre_pkpy_script_context_create(); -struct CREScriptContext* cre_pkpy_script_context_get(); - -struct pkpy_vm_handle* cre_pkpy_script_context_get_active_pkpy_vm(); -void cre_pkpy_script_context_on_network_udp_server_client_connected(); -// All instances of script instance creation should be routed through one of these functions to setup things properly -void cre_pkpy_script_context_create_instance(SkaEntity entity, const char* classPath, const char* className); -void cre_pkpy_script_context_create_instance_if_nonexistent(SkaEntity entity, const char* classPath, const char* className); -void cre_pkpy_script_context_create_instance_if_nonexistent_and_push_entity_instance(SkaEntity entity); -*/ diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.c b/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.c deleted file mode 100644 index ce58a57d0..000000000 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.c +++ /dev/null @@ -1,70 +0,0 @@ -#include "cre_pkpy_util.h" -/* -#include - -#include -#include -#include - -void cre_copy_name_from_signature(char* target, const char* signature) { - const char* open_parenthesis = strchr(signature, '('); - if (open_parenthesis) { - const size_t nameLength = open_parenthesis - signature; - ska_strncpy(target, sizeof(char) * (nameLength + 1), signature, nameLength); - } -} - -char* cre_pkpy_util_get_error_message(pkpy_vm* vm) { - char* errorMessage = NULL; - pkpy_clear_error(vm, &errorMessage); - return errorMessage; -} - -bool cre_pkpy_util_print_error_message(pkpy_vm* vm) { - char* errorMessage = cre_pkpy_util_get_error_message(vm); - if (errorMessage) { - ska_logger_error("python error:\n%s", errorMessage); - pkpy_free(errorMessage); - return true; - } - return false; -} - -void cre_pkpy_util_create_module(pkpy_vm* vm, CrePPModule* module) { - pkpy_push_module(vm, module->name); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_pop_top(vm); - char nameBuffer[64]; - ska_strncpy(nameBuffer, sizeof(char) * 8, "import ", 8); - ska_strcat(nameBuffer, module->name); - pkpy_exec(vm, nameBuffer); // import { module->name } - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - for (size_t i = 0; i < CRE_PKPY_MODULE_FUNCTION_LIMIT; i++) { - const CrePPFunction* func = &module->functions[i]; - if (!func->signature || !func->function) { - break; - } - pkpy_push_function(vm, func->signature, func->function); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_eval(vm, module->name); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - - cre_copy_name_from_signature(nameBuffer, func->signature); - pkpy_setattr(vm, pkpy_name(nameBuffer)); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - } -} - -void cre_pkpy_util_create_from_string(pkpy_vm* vm, const char* moduleName, const char* moduleSource) { - const int32 stackBeforePush = pkpy_stack_size(vm); - char moduleFileName[64]; - ska_strcpy(moduleFileName, moduleName); - ska_strcat(moduleFileName, ".py"); - - pkpy_push_module(vm, moduleName); - pkpy_exec_2(vm, moduleSource, moduleFileName, 0, moduleName); - SKA_ASSERT(!cre_pkpy_util_print_error_message(vm)); - pkpy_pop_top(vm); - SKA_ASSERT(stackBeforePush == pkpy_stack_size(vm)); -} -*/ diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.h deleted file mode 100644 index 9f70e9e9a..000000000 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_util.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -/* -#include - -#include -#include - -#define CRE_PKPY_MODULE_FUNCTION_LIMIT 256 - -typedef struct CrePPFunction { - const char* signature; - pkpy_CFunction function; -} CrePPFunction; - -typedef struct CrePPModule { - const char* name; - CrePPFunction functions[CRE_PKPY_MODULE_FUNCTION_LIMIT]; -} CrePPModule; - -char* cre_pkpy_util_get_error_message(pkpy_vm* vm); -bool cre_pkpy_util_print_error_message(pkpy_vm* vm); -void cre_pkpy_util_create_module(pkpy_vm* vm, CrePPModule* module); -void cre_pkpy_util_create_from_string(pkpy_vm* vm, const char* moduleName, const char* moduleSource); -*/ From 74a371247a4a654833f8e2db5141b69fc579c8d3 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 14:19:58 -0400 Subject: [PATCH 36/54] Updates. --- .../python/pocketpy/api/pkpy_api_impl.c | 82 +++++++++++++++++++ .../python/pocketpy/pkpy_script_context.c | 27 +++++- 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index e5fd56c51..c34fc7722 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -84,6 +84,7 @@ bool cre_pkpy_api_shader_instance_create_bool_param(int argc, py_StackRef argv) const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); ska_shader_instance_param_create_bool(shaderInstance, paramName, value); + py_newnone(py_retval()); return true; } @@ -97,6 +98,7 @@ bool cre_pkpy_api_shader_instance_set_bool_param(int argc, py_StackRef argv) { const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); ska_shader_instance_param_update_bool(shaderInstance, paramName, value); + py_newnone(py_retval()); return true; } @@ -123,6 +125,7 @@ bool cre_pkpy_api_shader_instance_create_int_param(int argc, py_StackRef argv) { const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); ska_shader_instance_param_create_int(shaderInstance, paramName, (int32)value); + py_newnone(py_retval()); return true; } @@ -136,6 +139,7 @@ bool cre_pkpy_api_shader_instance_set_int_param(int argc, py_StackRef argv) { const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); ska_shader_instance_param_update_int(shaderInstance, paramName, (int32)value); + py_newnone(py_retval()); return true; } @@ -162,6 +166,7 @@ bool cre_pkpy_api_shader_instance_create_float_param(int argc, py_StackRef argv) const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); ska_shader_instance_param_create_float(shaderInstance, paramName, (f32)value); + py_newnone(py_retval()); return true; } @@ -175,6 +180,7 @@ bool cre_pkpy_api_shader_instance_set_float_param(int argc, py_StackRef argv) { const SkaShaderInstanceId shaderId = (SkaShaderInstanceId)pyShaderId; SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); ska_shader_instance_param_update_float(shaderInstance, paramName, (f32)value); + py_newnone(py_retval()); return true; } @@ -203,6 +209,7 @@ bool cre_pkpy_api_shader_instance_create_float2_param(int argc, py_StackRef argv SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); const SkaVector2 vecValue = (SkaVector2){ .x = (f32)valueX, .y = (f32)valueY }; ska_shader_instance_param_create_float2(shaderInstance, paramName, vecValue); + py_newnone(py_retval()); return true; } @@ -218,6 +225,7 @@ bool cre_pkpy_api_shader_instance_set_float2_param(int argc, py_StackRef argv) { SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); const SkaVector2 vecValue = (SkaVector2){ .x = (f32)valueX, .y = (f32)valueY }; ska_shader_instance_param_update_float2(shaderInstance, paramName, vecValue); + py_newnone(py_retval()); return true; } @@ -251,6 +259,7 @@ bool cre_pkpy_api_shader_instance_create_float3_param(int argc, py_StackRef argv SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); const SkaVector3 vecValue = (SkaVector3){ .x = (f32)valueX, .y = (f32)valueY, .z = (f32)valueZ }; ska_shader_instance_param_create_float3(shaderInstance, paramName, vecValue); + py_newnone(py_retval()); return true; } @@ -267,6 +276,7 @@ bool cre_pkpy_api_shader_instance_set_float3_param(int argc, py_StackRef argv) { SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); const SkaVector3 vecValue = (SkaVector3){ .x = (f32)valueX, .y = (f32)valueY, .z = (f32)valueZ }; ska_shader_instance_param_update_float3(shaderInstance, paramName, vecValue); + py_newnone(py_retval()); return true; } @@ -303,6 +313,7 @@ bool cre_pkpy_api_shader_instance_create_float4_param(int argc, py_StackRef argv SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); const SkaVector4 vecValue = (SkaVector4){ .x = (f32)valueX, .y = (f32)valueY, .z = (f32)valueZ, .w = (f32)valueW }; ska_shader_instance_param_create_float4(shaderInstance, paramName, vecValue); + py_newnone(py_retval()); return true; } @@ -320,6 +331,7 @@ bool cre_pkpy_api_shader_instance_set_float4_param(int argc, py_StackRef argv) { SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(shaderId); const SkaVector4 vecValue = (SkaVector4){ .x = (f32)valueX, .y = (f32)valueY, .z = (f32)valueZ, .w = (f32)valueW }; ska_shader_instance_param_update_float4(shaderInstance, paramName, vecValue); + py_newnone(py_retval()); return true; } @@ -393,6 +405,7 @@ bool cre_pkpy_api_shader_util_get_current_screen_shader(int argc, py_StackRef ar bool cre_pkpy_api_shader_util_reset_screen_shader_to_default(int argc, py_StackRef argv) { ska_frame_buffer_reset_to_default_screen_shader(); + py_newnone(py_retval()); return true; } @@ -406,6 +419,7 @@ bool cre_pkpy_api_engine_exit(int argc, py_StackRef argv) { CREEngineContext* engineContext = cre_engine_context_get(); engineContext->isRunning = false; engineContext->exitCode = (int32)pyExitCode; + py_newnone(py_retval()); return true; } @@ -415,6 +429,7 @@ bool cre_pkpy_api_engine_set_target_fps(int argc, py_StackRef argv) { CREEngineContext* engineContext = cre_engine_context_get(); engineContext->targetFPS = (int32)pyTargetFPS; + py_newnone(py_retval()); return true; } @@ -439,6 +454,7 @@ bool cre_pkpy_api_engine_set_fps_display_enabled(int argc, py_StackRef argv) { const py_f64 pyPosY = py_tofloat(py_arg(3)); cre_ecs_manager_enable_fps_display_entity(isEnabled, fontUID, (f32)pyPosX, (f32)pyPosY); + py_newnone(py_retval()); return true; } @@ -491,6 +507,7 @@ bool cre_pkpy_api_input_add_action(int argc, py_StackRef argv) { (SkaInputActionValue[]){ { .key = (SkaInputKey)pyValueKey, .strengthThreshold = 0.5f }, { SkaInputKey_INVALID } }, (SkaInputDeviceIndex)pyDeviceId ); + py_newnone(py_retval()); return true; } @@ -541,6 +558,7 @@ bool cre_pkpy_api_input_start_gamepad_vibration(int argc, py_StackRef argv) { const py_f64 pyDurationSeconds = py_tofloat(py_arg(3)); // ska_input_gamepad_start_vibration((int32)pyDeviceId, (f32)pyWeakMagnitude, (f32)pyStrongMagnitude, (f32)pyDurationSeconds); + py_newnone(py_retval()); return true; } @@ -551,6 +569,7 @@ bool cre_pkpy_api_input_stop_gamepad_vibration(int argc, py_StackRef argv) { const py_i64 pyDeviceId = py_toint(py_arg(0)); // ska_input_gamepad_stop_vibration((SkaInputDeviceIndex)pyDeviceId); + py_newnone(py_retval()); return true; } @@ -582,6 +601,7 @@ bool cre_pkpy_api_scene_tree_change_scene(int argc, py_StackRef argv) { const char* scenePath = py_tostr(py_arg(0)); cre_scene_manager_queue_scene_change(scenePath); + py_newnone(py_retval()); return true; } @@ -597,11 +617,13 @@ bool cre_pkpy_api_scene_tree_get_root(int argc, py_StackRef argv) { bool cre_pkpy_api_scene_manager_process_queued_creation_entities(int argc, py_StackRef argv) { cre_scene_manager_process_queued_creation_entities(); + py_newnone(py_retval()); return true; } bool cre_pkpy_api_scene_manager_process_queued_scene_change(int argc, py_StackRef argv) { cre_scene_manager_process_queued_scene_change(); + py_newnone(py_retval()); return true; } @@ -643,6 +665,7 @@ bool cre_pkpy_api_camera2d_set_position(int argc, py_StackRef argv) { CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); camera2D->viewport = (SkaVector2){ .x = (f32)posX, .y = (f32)posY }; cre_camera2d_clamp_viewport_to_boundary(camera2D); + py_newnone(py_retval()); return true; } @@ -655,6 +678,7 @@ bool cre_pkpy_api_camera2d_add_to_position(int argc, py_StackRef argv) { CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); camera2D->viewport = (SkaVector2){ .x = camera2D->viewport.x + (f32)posX, .y = camera2D->viewport.y + (f32)posY }; cre_camera2d_clamp_viewport_to_boundary(camera2D); + py_newnone(py_retval()); return true; } @@ -676,6 +700,7 @@ bool cre_pkpy_api_camera2d_set_offset(int argc, py_StackRef argv) { CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); camera2D->offset = (SkaVector2){ .x = (f32)offsetX, .y = (f32)offsetY }; + py_newnone(py_retval()); return true; } @@ -687,6 +712,7 @@ bool cre_pkpy_api_camera2d_add_to_offset(int argc, py_StackRef argv) { CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); camera2D->offset = (SkaVector2){ .x = camera2D->offset.x + (f32)offsetX, .y = camera2D->offset.y + (f32)offsetY }; + py_newnone(py_retval()); return true; } @@ -708,6 +734,7 @@ bool cre_pkpy_api_camera2d_set_zoom(int argc, py_StackRef argv) { CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); camera2D->zoom = (SkaVector2){ .x = (f32)zoomX, .y = (f32)zoomY }; + py_newnone(py_retval()); return true; } @@ -719,6 +746,7 @@ bool cre_pkpy_api_camera2d_add_to_zoom(int argc, py_StackRef argv) { CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); camera2D->zoom = (SkaVector2){ .x = camera2D->zoom.x + (f32)zoomX, .y = camera2D->zoom.y + (f32)zoomY }; + py_newnone(py_retval()); return true; } @@ -743,6 +771,7 @@ bool cre_pkpy_api_camera2d_set_boundary(int argc, py_StackRef argv) { CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); camera2D->boundary = (SkaRect2){ .x = (f32)x, .y = (f32)y, .w = (f32)w, .h = (f32)h }; cre_camera2d_clamp_viewport_to_boundary(camera2D); + py_newnone(py_retval()); return true; } @@ -768,6 +797,7 @@ bool cre_pkpy_api_camera2d_follow_node(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); cre_camera2d_follow_entity(camera2D, entity); + py_newnone(py_retval()); return true; } @@ -779,6 +809,7 @@ bool cre_pkpy_api_camera2d_unfollow_node(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; CRECamera2D* camera2D = cre_camera_manager_get_current_camera(); cre_camera2d_unfollow_entity(camera2D, entity); + py_newnone(py_retval()); return true; } @@ -796,6 +827,7 @@ bool cre_pkpy_api_world_set_time_dilation(int argc, py_StackRef argv) { cre_world_set_time_dilation((f32)timeDilation); cre_scene_manager_execute_on_root_and_child_nodes(mark_scene_nodes_time_dilation_flag_dirty); + py_newnone(py_retval()); return true; } @@ -828,6 +860,7 @@ bool cre_pkpy_api_audio_source_set_pitch(int argc, py_StackRef argv) { } else { ska_logger_warn("Tried to set non-existent audio source's pitch at '%s'", path); } + py_newnone(py_retval()); return true; } @@ -857,6 +890,7 @@ bool cre_pkpy_api_audio_manager_play_sound(int argc, py_StackRef argv) { const bool loops = py_tobool(py_arg(1)); ska_audio_manager_play_sound(path, loops); + py_newnone(py_retval()); return true; } @@ -866,6 +900,7 @@ bool cre_pkpy_api_audio_manager_stop_sound(int argc, py_StackRef argv) { const char* path = py_tostr(py_arg(0)); ska_audio_manager_stop_sound(path); + py_newnone(py_retval()); return true; } @@ -996,6 +1031,7 @@ bool cre_pkpy_api_server_start(int argc, py_StackRef argv) { bool cre_pkpy_api_server_stop(int argc, py_StackRef argv) { ska_udp_server_finalize(); + py_newnone(py_retval()); return true; } @@ -1005,6 +1041,7 @@ bool cre_pkpy_api_server_send(int argc, py_StackRef argv) { const char* message = py_tostr(py_arg(0)); ska_udp_server_send_message(message); + py_newnone(py_retval()); return true; } @@ -1017,11 +1054,13 @@ bool cre_pkpy_api_client_start(int argc, py_StackRef argv) { const py_i64 port = py_toint(py_arg(1)); ska_udp_client_initialize(host, (int32)port, ska_ecs_system_event_network_callback); + py_newnone(py_retval()); return true; } bool cre_pkpy_api_client_stop(int argc, py_StackRef argv) { ska_udp_client_finalize(); + py_newnone(py_retval()); return true; } @@ -1031,6 +1070,7 @@ bool cre_pkpy_api_client_send(int argc, py_StackRef argv) { const char* message = py_tostr(py_arg(0)); ska_udp_client_send_message(message); + py_newnone(py_retval()); return true; } @@ -1107,6 +1147,7 @@ bool cre_pkpy_api_node_add_child(int argc, py_StackRef argv) { const py_i64 parentEntityId = py_toint(py_arg(1)); cre_scene_manager_add_node_as_child((SkaEntity)childEntityId, (SkaEntity)parentEntityId); + py_newnone(py_retval()); return true; } @@ -1181,6 +1222,7 @@ bool cre_pkpy_api_node_queue_deletion(int argc, py_StackRef argv) { } else { ska_logger_warn("Node '%s' already queued for deletion!", nodeComponent->name); } + py_newnone(py_retval()); return true; } @@ -1205,6 +1247,7 @@ bool cre_pkpy_api_node_set_time_dilation(int argc, py_StackRef argv) { NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); nodeComponent->timeDilation.value = (f32)timeDilation; cre_scene_manager_invalidate_time_dilation_nodes_with_children(entity); + py_newnone(py_retval()); return true; } @@ -1274,6 +1317,7 @@ bool cre_pkpy_api_node2d_set_position(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; pkpy_update_entity_local_position(entity, &(SkaVector2) { .x = (f32)posX, .y = (f32)posY }); + py_newnone(py_retval()); return true; } @@ -1333,6 +1377,7 @@ bool cre_pkpy_api_node2d_set_scale(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; pkpy_update_entity_local_scale(entity, &(SkaVector2) { .x = (f32)scaleX, .y = (f32)scaleY }); + py_newnone(py_retval()); return true; } @@ -1346,6 +1391,7 @@ bool cre_pkpy_api_node2d_add_to_scale(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); pkpy_update_entity_local_scale(entity, &(SkaVector2) { .x = transformComp->localTransform.scale.x + (f32)scaleX, .y = transformComp->localTransform.scale.y + (f32)scaleY }); + py_newnone(py_retval()); return true; } @@ -1372,6 +1418,7 @@ bool cre_pkpy_api_node2d_set_rotation(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; pkpy_update_entity_local_rotation(entity, (f32)rotation); + py_newnone(py_retval()); return true; } @@ -1384,6 +1431,7 @@ bool cre_pkpy_api_node2d_add_to_rotation(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; const Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); pkpy_update_entity_local_rotation(entity, transformComp->localTransform.rotation + (f32)rotation); + py_newnone(py_retval()); return true; } @@ -1407,6 +1455,7 @@ bool cre_pkpy_api_node2d_set_z_index(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); transformComp->zIndex = (int32)zIndex; + py_newnone(py_retval()); return true; } @@ -1430,6 +1479,7 @@ bool cre_pkpy_api_node2d_set_z_index_relative_to_parent(int argc, py_StackRef ar const SkaEntity entity = (SkaEntity)entityId; Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); transformComp->isZIndexRelativeToParent = zIndexRelativeToParent; + py_newnone(py_retval()); return true; } @@ -1453,6 +1503,7 @@ bool cre_pkpy_api_node2d_set_ignore_camera(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; Transform2DComponent* transformComp = (Transform2DComponent*)ska_ecs_component_manager_get_component(entity, TRANSFORM2D_COMPONENT_INDEX); transformComp->ignoreCamera = ignoreCamera; + py_newnone(py_retval()); return true; } @@ -1519,6 +1570,7 @@ bool cre_pkpy_api_sprite_set_texture(int argc, py_StackRef argv) { } else { ska_logger_warn("Failed to find texture at path '%s'", texturePath); } + py_newnone(py_retval()); return true; } @@ -1554,6 +1606,7 @@ bool cre_pkpy_api_sprite_set_draw_source(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); spriteComponent->drawSource = (SkaRect2){ (f32)x, (f32)y, (f32)w, (f32)h }; + py_newnone(py_retval()); return true; } @@ -1577,6 +1630,7 @@ bool cre_pkpy_api_sprite_set_flip_h(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); spriteComponent->flipH = flipH; + py_newnone(py_retval()); return true; } @@ -1600,6 +1654,7 @@ bool cre_pkpy_api_sprite_set_flip_v(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); spriteComponent->flipV = flipV; + py_newnone(py_retval()); return true; } @@ -1635,6 +1690,7 @@ bool cre_pkpy_api_sprite_set_modulate(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); spriteComponent->modulate = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); + py_newnone(py_retval()); return true; } @@ -1663,6 +1719,7 @@ bool cre_pkpy_api_sprite_set_origin(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; SpriteComponent* spriteComponent = (SpriteComponent*)ska_ecs_component_manager_get_component(entity, SPRITE_COMPONENT_INDEX); spriteComponent->origin = (SkaVector2){ (f32)x, (f32)y }; + py_newnone(py_retval()); return true; } @@ -1693,6 +1750,7 @@ bool cre_pkpy_api_sprite_set_shader_instance(int argc, py_StackRef argv) { spriteComponent->shaderInstanceId = instanceId; SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(instanceId); ska_renderer_set_sprite_shader_default_params(shaderInstance->shader); + py_newnone(py_retval()); return true; } @@ -1719,6 +1777,7 @@ bool cre_pkpy_api_animated_sprite_stop(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); animatedSpriteComponent->isPlaying = false; + py_newnone(py_retval()); return true; } @@ -1731,11 +1790,13 @@ bool cre_pkpy_api_animated_sprite_set_current_animation_frame(int argc, py_Stack const SkaEntity entity = (SkaEntity)entityId; AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); animatedSpriteComponent->currentAnimation->currentFrame = ska_math_clamp_int((int32)frame, 0, animatedSpriteComponent->currentAnimation->frameCount - 1); + py_newnone(py_retval()); return true; } // TODO: Do later as it's too hard to figure out now :D bool cre_pkpy_api_animated_sprite_add_animation(int argc, py_StackRef argv) { + py_newnone(py_retval()); return true; } @@ -1759,6 +1820,7 @@ bool cre_pkpy_api_animated_sprite_set_stagger_animation_start_times(int argc, py const SkaEntity entity = (SkaEntity)entityId; AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); animatedSpriteComponent->staggerStartAnimationTimes = staggerAnimationsStartTimes; + py_newnone(py_retval()); return true; } @@ -1782,6 +1844,7 @@ bool cre_pkpy_api_animated_sprite_set_flip_h(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); animatedSpriteComponent->flipH = flipH; + py_newnone(py_retval()); return true; } @@ -1805,6 +1868,7 @@ bool cre_pkpy_api_animated_sprite_set_flip_v(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); animatedSpriteComponent->flipV = flipV; + py_newnone(py_retval()); return true; } @@ -1840,6 +1904,7 @@ bool cre_pkpy_api_animated_sprite_set_modulate(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); animatedSpriteComponent->modulate = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); + py_newnone(py_retval()); return true; } @@ -1868,6 +1933,7 @@ bool cre_pkpy_api_animated_sprite_set_origin(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); animatedSpriteComponent->origin = (SkaVector2){ (f32)x, (f32)y }; + py_newnone(py_retval()); return true; } @@ -1898,6 +1964,7 @@ bool cre_pkpy_api_animated_sprite_set_shader_instance(int argc, py_StackRef argv animatedSpriteComponent->shaderInstanceId = instanceId; SkaShaderInstance* shaderInstance = ska_shader_cache_get_instance(instanceId); ska_renderer_set_sprite_shader_default_params(shaderInstance->shader); + py_newnone(py_retval()); return true; } @@ -1923,6 +1990,7 @@ bool cre_pkpy_api_text_label_set_text(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); ska_strcpy(textLabelComponent->text, text); + py_newnone(py_retval()); return true; } @@ -1958,6 +2026,7 @@ bool cre_pkpy_api_text_label_set_color(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; TextLabelComponent* textLabelComponent = (TextLabelComponent*)ska_ecs_component_manager_get_component(entity, TEXT_LABEL_COMPONENT_INDEX); textLabelComponent->color = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); + py_newnone(py_retval()); return true; } @@ -1975,6 +2044,7 @@ bool cre_pkpy_api_text_label_set_font_uid(int argc, py_StackRef argv) { } else { ska_logger_warn("Failed to set font to '%s' as it doesn't exist in the asset manager!", fontUID); } + py_newnone(py_retval()); return true; } @@ -2005,6 +2075,7 @@ bool cre_pkpy_api_collider2d_set_extents(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; Collider2DComponent* collider2DComponent = (Collider2DComponent*)ska_ecs_component_manager_get_component(entity, COLLIDER2D_COMPONENT_INDEX); collider2DComponent->extents = (SkaSize2D){ (f32)w, (f32)h }; + py_newnone(py_retval()); return true; } @@ -2040,6 +2111,7 @@ bool cre_pkpy_api_collider2d_set_color(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; Collider2DComponent* collider2DComponent = (Collider2DComponent*)ska_ecs_component_manager_get_component(entity, COLLIDER2D_COMPONENT_INDEX); collider2DComponent->color = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); + py_newnone(py_retval()); return true; } @@ -2070,6 +2142,7 @@ bool cre_pkpy_api_color_rect_set_size(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; ColorRectComponent* colorRectComponent = (ColorRectComponent*)ska_ecs_component_manager_get_component(entity, COLOR_RECT_COMPONENT_INDEX); colorRectComponent->size = (SkaSize2D){ (f32)w, (f32)h }; + py_newnone(py_retval()); return true; } @@ -2105,6 +2178,7 @@ bool cre_pkpy_api_color_rect_set_color(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; ColorRectComponent* colorRectComponent = (ColorRectComponent*)ska_ecs_component_manager_get_component(entity, COLOR_RECT_COMPONENT_INDEX); colorRectComponent->color = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); + py_newnone(py_retval()); return true; } @@ -2135,6 +2209,7 @@ bool cre_pkpy_api_parallax_set_scroll_speed(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; ParallaxComponent* parallaxComponent = (ParallaxComponent*)ska_ecs_component_manager_get_component(entity, PARALLAX_COMPONENT_INDEX); parallaxComponent->scrollSpeed = (SkaVector2){ (f32)x, (f32)y }; + py_newnone(py_retval()); return true; } @@ -2160,6 +2235,7 @@ bool cre_pkpy_api_particles2d_set_amount(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); particles2dComponent->amount = (int32)amount; + py_newnone(py_retval()); return true; } @@ -2183,6 +2259,7 @@ bool cre_pkpy_api_particles2d_set_life_time(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); particles2dComponent->lifeTime = (f32)lifeTime; + py_newnone(py_retval()); return true; } @@ -2206,6 +2283,7 @@ bool cre_pkpy_api_particles2d_set_damping(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); particles2dComponent->damping = (f32)damping; + py_newnone(py_retval()); return true; } @@ -2229,6 +2307,7 @@ bool cre_pkpy_api_particles2d_set_explosiveness(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); particles2dComponent->lifeTime = (f32)explosiveness; + py_newnone(py_retval()); return true; } @@ -2264,6 +2343,7 @@ bool cre_pkpy_api_particles2d_set_color(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); particles2dComponent->color = ska_color_get_normalized_color((uint32)r, (uint32)g, (uint32)b, (uint32)a); + py_newnone(py_retval()); return true; } @@ -2301,6 +2381,7 @@ bool cre_pkpy_api_particles2d_set_initial_velocity(int argc, py_StackRef argv) { .min = { .x = (f32)minX, .y = (f32)minY }, .max = { .x = (f32)maxX, .y = (f32)maxY } }; + py_newnone(py_retval()); return true; } @@ -2324,5 +2405,6 @@ bool cre_pkpy_api_particles2d_set_spread(int argc, py_StackRef argv) { const SkaEntity entity = (SkaEntity)entityId; Particles2DComponent* particles2dComponent = (Particles2DComponent*)ska_ecs_component_manager_get_component(entity, PARTICLES2D_COMPONENT_INDEX); particles2dComponent->spread = (f32)spread; + py_newnone(py_retval()); return true; } diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index b47594ecd..407acfccc 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -7,6 +7,7 @@ #include "pkpy_util.h" #include "pkpy_instance_cache.h" +#include "../../../../../../cmake-build-release-msvc/_deps/seika_content-src/seika/data_structures/array_utils.h" #include "api/pkpy_api.h" static CREScriptContext* scriptContext = NULL; @@ -72,11 +73,33 @@ void pkpy_finalize(CREScriptContext* context) { } void pkpy_create_instance(SkaEntity entity, const char* classPath, const char* className) { - cre_pkpy_instance_cache_add(entity, classPath, className); + py_Ref newInstance = cre_pkpy_instance_cache_add(entity, classPath, className); + if (py_getattr(newInstance, processFunctionName)) { + scriptContext->updateEntities[scriptContext->updateEntityCount++] = entity; + } else { + py_clearexc(NULL); + } + if (py_getattr(newInstance, fixedProcessFunctionName)) { + scriptContext->fixedUpdateEntities[scriptContext->fixedUpdateEntityCount++] = entity; + } else { + py_clearexc(NULL); + } } void pkpy_delete_instance(SkaEntity entity) { cre_pkpy_instance_cache_remove(entity); + ska_array_utils_remove_item_uint32( + scriptContext->updateEntities, + &scriptContext->updateEntityCount, + entity, + SKA_NULL_ENTITY + ); + ska_array_utils_remove_item_uint32( + scriptContext->fixedUpdateEntities, + &scriptContext->fixedUpdateEntityCount, + entity, + SKA_NULL_ENTITY + ); } void pkpy_on_start(SkaEntity entity) { @@ -106,7 +129,7 @@ void pkpy_on_end(SkaEntity entity) { void pkpy_on_update(SkaEntity entity, f32 deltaTime) { py_Ref self = cre_pkpy_instance_cache_get_checked(entity); if (py_getattr(self, processFunctionName)) { - py_Ref pyDeltaTime = NULL; + py_Ref pyDeltaTime = py_pushtmp(); py_newfloat(pyDeltaTime, deltaTime); py_push(py_retval()); From 23e1be0cbdc8b2525c542c287c1d600b6e5824cd Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 14:20:15 -0400 Subject: [PATCH 37/54] Updates. --- engine/src/core/scripting/python/pocketpy/pkpy_script_context.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index 407acfccc..ef9b50b7e 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -145,7 +145,7 @@ void pkpy_on_update(SkaEntity entity, f32 deltaTime) { void pkpy_on_fixed_update(SkaEntity entity, f32 deltaTime) { py_Ref self = cre_pkpy_instance_cache_get_checked(entity); if (py_getattr(self, fixedProcessFunctionName)) { - py_Ref pyDeltaTime = NULL; + py_Ref pyDeltaTime = py_pushtmp(); py_newfloat(pyDeltaTime, deltaTime); py_push(py_retval()); From 63e69620da1094cd2c956d7905c277e40af1d2ea Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 19:55:23 -0400 Subject: [PATCH 38/54] Updates. --- engine/src/core/scripting/python/pocketpy/pkpy_script_context.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index ef9b50b7e..04bf1b8e2 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -4,10 +4,10 @@ #include #include +#include #include "pkpy_util.h" #include "pkpy_instance_cache.h" -#include "../../../../../../cmake-build-release-msvc/_deps/seika_content-src/seika/data_structures/array_utils.h" #include "api/pkpy_api.h" static CREScriptContext* scriptContext = NULL; From 319f82cec894a71cfbf2f1df0b2ce3120f38b01e Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 20:29:40 -0400 Subject: [PATCH 39/54] Updates. --- crescent_py_api/pocketpy/crescent.py | 2 +- crescent_py_api/pocketpy/crescent_internal.py | 2 +- .../scripting/python/pocketpy/api/pkpy_api.c | 2 +- .../python/pocketpy/api/pkpy_api_impl.c | 46 +++++++++++++++++++ .../python/pocketpy/cre_pkpy_api_source.h | 2 +- .../python/pocketpy/pkpy_script_context.c | 1 + 6 files changed, 51 insertions(+), 4 deletions(-) diff --git a/crescent_py_api/pocketpy/crescent.py b/crescent_py_api/pocketpy/crescent.py index 5944bc915..471e3a2bc 100644 --- a/crescent_py_api/pocketpy/crescent.py +++ b/crescent_py_api/pocketpy/crescent.py @@ -1183,7 +1183,7 @@ def set_current_animation_frame(self, frame: int) -> None: crescent_internal.animated_sprite_set_current_animation_frame(self.entity_id, frame) def add_animation(self, animation: Animation) -> None: - crescent_internal.animated_sprite_add_animation(self.entity_id, animation.name, animation.speed, animation.loops, len(animation.frames), *animation.frames) + crescent_internal.animated_sprite_add_animation(self.entity_id, animation.name, animation.speed, animation.loops, animation.frames) @property def flip_h(self) -> bool: diff --git a/crescent_py_api/pocketpy/crescent_internal.py b/crescent_py_api/pocketpy/crescent_internal.py index fb450f609..3d022850d 100644 --- a/crescent_py_api/pocketpy/crescent_internal.py +++ b/crescent_py_api/pocketpy/crescent_internal.py @@ -185,7 +185,7 @@ def animated_sprite_set_current_animation_frame(entity_id: int, frame: int) -> N pass -def animated_sprite_add_animation(entity_id: int, name: str, speed: float, does_loop: bool, frame_count: int, *args) -> None: +def animated_sprite_add_animation(entity_id: int, name: str, speed: int, does_loop: bool, frames: List["AnimationFrame"]) -> None: pass diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api.c index bbba7dc18..70b6f97f1 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api.c @@ -103,7 +103,7 @@ void cre_pkpy_api_load_internal_modules() { {.signature = "animated_sprite_play(entity_id: int, animation_name: str) -> bool", .function = cre_pkpy_api_animated_sprite_play}, {.signature = "animated_sprite_stop(entity_id: int) -> None", .function = cre_pkpy_api_animated_sprite_stop}, {.signature = "animated_sprite_set_current_animation_frame(entity_id: int, frame: int) -> None", .function = cre_pkpy_api_animated_sprite_set_current_animation_frame}, - {.signature = "animated_sprite_add_animation(entity_id: int, name: str, speed: float, does_loop: bool, frame_count: int, *args) -> None", .function = cre_pkpy_api_animated_sprite_add_animation}, + {.signature = "animated_sprite_add_animation(entity_id: int, name: str, speed: float, does_loop: bool, frames: List[\"AnimationFrame\"]) -> None", .function = cre_pkpy_api_animated_sprite_add_animation}, {.signature = "animated_sprite_get_stagger_animation_start_times(entity_id: int) -> bool", .function = cre_pkpy_api_animated_sprite_get_stagger_animation_start_times}, {.signature = "animated_sprite_set_stagger_animation_start_times(entity_id: int, stagger: bool) -> None", .function = cre_pkpy_api_animated_sprite_set_stagger_animation_start_times}, {.signature = "animated_sprite_get_flip_h(entity_id: int) -> bool", .function = cre_pkpy_api_animated_sprite_get_flip_h}, diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index c34fc7722..9190960c9 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -1796,6 +1796,52 @@ bool cre_pkpy_api_animated_sprite_set_current_animation_frame(int argc, py_Stack // TODO: Do later as it's too hard to figure out now :D bool cre_pkpy_api_animated_sprite_add_animation(int argc, py_StackRef argv) { + PY_CHECK_ARGC(5); + PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(2, tp_int); PY_CHECK_ARG_TYPE(3, tp_bool); PY_CHECK_ARG_TYPE(4, tp_list); + const py_i64 entityId = py_toint(py_arg(0)); + const char* animationName = py_tostr(py_arg(1)); + const py_i64 animationSpeed = py_toint(py_arg(2)); + const bool doesLoop = py_tobool(py_arg(3)); + const py_Ref pyFramesList = py_arg(4); + + const SkaEntity entity = (SkaEntity)entityId; + AnimatedSpriteComponent* animatedSpriteComponent = (AnimatedSpriteComponent*)ska_ecs_component_manager_get_component(entity, ANIMATED_SPRITE_COMPONENT_INDEX); + CreAnimation newAnim = { .frameCount = 0, .currentFrame = 0, .speed = (int32)animationSpeed, .name = {'\0'}, .doesLoop = doesLoop, .isValid = true }; + ska_strcpy(newAnim.name, animationName); + // Add frames to animation + const int listLength = py_list_len(pyFramesList); + for (int i = 0; i < listLength; i++) { + CreAnimationFrame animFrame; + py_Ref pyAnimFrame = py_list_getitem(pyFramesList, i); + bool hasAttribute = py_getattr(pyAnimFrame, py_name("frame")); + SKA_ASSERT(hasAttribute); + animFrame.frame = (int32)py_toint(py_retval()); + hasAttribute = py_getattr(pyAnimFrame, py_name("texture_path")); + SKA_ASSERT(hasAttribute); + animFrame.texture = ska_asset_manager_get_texture(py_tostr(py_retval())); + hasAttribute = py_getattr(pyAnimFrame, py_name("draw_source")); + SKA_ASSERT(hasAttribute); + py_Ref pyDrawSource = py_retval(); + hasAttribute = py_getattr(pyDrawSource, py_name("x")); + SKA_ASSERT(hasAttribute); + animFrame.drawSource.x = (f32)py_tofloat(py_retval()); + hasAttribute = py_getattr(pyDrawSource, py_name("y")); + SKA_ASSERT(hasAttribute); + animFrame.drawSource.y = (f32)py_tofloat(py_retval()); + hasAttribute = py_getattr(pyDrawSource, py_name("2")); + SKA_ASSERT(hasAttribute); + animFrame.drawSource.w = (f32)py_tofloat(py_retval()); + hasAttribute = py_getattr(pyDrawSource, py_name("h")); + SKA_ASSERT(hasAttribute); + animFrame.drawSource.h = (f32)py_tofloat(py_retval()); + newAnim.animationFrames[newAnim.frameCount++] = animFrame; + } + // Add animation + animated_sprite_component_add_animation(animatedSpriteComponent, newAnim); + const bool isFirstAnim = animatedSpriteComponent->animationCount == 1; + if (isFirstAnim) { + animated_sprite_component_set_animation(animatedSpriteComponent, newAnim.name); + } py_newnone(py_retval()); return true; } diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h index 8e5bf5ed7..20c56021c 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h @@ -1186,7 +1186,7 @@ " crescent_internal.animated_sprite_set_current_animation_frame(self.entity_id, frame)\n"\ "\n"\ " def add_animation(self, animation: Animation) -> None:\n"\ -" crescent_internal.animated_sprite_add_animation(self.entity_id, animation.name, animation.speed, animation.loops, len(animation.frames), *animation.frames)\n"\ +" crescent_internal.animated_sprite_add_animation(self.entity_id, animation.name, animation.speed, animation.loops, animation.frames)\n"\ "\n"\ " @property\n"\ " def flip_h(self) -> bool:\n"\ diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index 04bf1b8e2..35dd6c913 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -73,6 +73,7 @@ void pkpy_finalize(CREScriptContext* context) { } void pkpy_create_instance(SkaEntity entity, const char* classPath, const char* className) { + if (cre_pkpy_instance_cache_has(entity)) { return; } py_Ref newInstance = cre_pkpy_instance_cache_add(entity, classPath, className); if (py_getattr(newInstance, processFunctionName)) { scriptContext->updateEntities[scriptContext->updateEntityCount++] = entity; From cfe61ff9eb81d4d6b72d88a2ec99f45a2ac45533 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 20:31:03 -0400 Subject: [PATCH 40/54] Updates. --- Dependencies.cmake | 1 - 1 file changed, 1 deletion(-) diff --git a/Dependencies.cmake b/Dependencies.cmake index 4cf304d81..3a91afea5 100644 --- a/Dependencies.cmake +++ b/Dependencies.cmake @@ -20,7 +20,6 @@ if (NOT TARGET pocketpy) FetchContent_Declare( pocketpy_content GIT_REPOSITORY https://github.com/blueloveTH/pocketpy.git -# GIT_TAG v1.4.5 GIT_TAG v2.0.0 ) FetchContent_MakeAvailable(pocketpy_content) From c910fe79d95f02b8a8fd8d93c26aa50ad5059cec Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 20:57:31 -0400 Subject: [PATCH 41/54] Updates. --- engine/src/core/scripting/python/pocketpy/pkpy_script_context.h | 2 +- engine/src/main.c | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.h b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.h index e269517b5..61368b894 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.h +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.h @@ -1,5 +1,5 @@ #pragma once -#include "../../script_context.h" +#include "core/scripting/script_context.h" CREScriptContextTemplate cre_pkpy_get_script_context_template(); diff --git a/engine/src/main.c b/engine/src/main.c index 08528bce3..c497de57b 100644 --- a/engine/src/main.c +++ b/engine/src/main.c @@ -4,7 +4,6 @@ #include "core/core.h" - int main(int argv, char** args) { if (!cre_initialize(argv, args)) { return EXIT_FAILURE; From 7bb51a54f81108b7c785ef315a2cd029631bcccd Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 21:04:38 -0400 Subject: [PATCH 42/54] Updates. --- engine/test/main.c | 113 ++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 62 deletions(-) diff --git a/engine/test/main.c b/engine/test/main.c index e26ce5cd7..49a1ac3c1 100644 --- a/engine/test/main.c +++ b/engine/test/main.c @@ -19,14 +19,11 @@ #include "../src/core/ecs/ecs_manager.h" #include "../src/core/json/json_file_loader.h" #include "../src/core/scripting/python/pocketpy/cre_pkpy.h" -#include "../src/core/scripting/python/pocketpy/cre_pkpy_util.h" -#include "../src/core/scripting/python/pocketpy/cre_pkpy_entity_instance_cache.h" -#include "../src/core/scripting/python/pocketpy/cre_pkpy_script_context.h" -#include "../src/core/scripting/python/pocketpy/cre_pkpy_node_event_manager.h" #include "../src/core/game_properties.h" #include "../src/core/engine_context.h" #include "../src/core/scene/scene_manager.h" #include "../src/core/tilemap/tilemap.h" +#include "core/scripting/python/pocketpy/pkpy_util.h" inline static SkaTexture* create_mock_texture() { SkaTexture* texture = SKA_MEM_ALLOCATE(SkaTexture); @@ -212,49 +209,49 @@ void cre_json_file_loader_scene_test(void) { //--- Pocketpy Test ---// void cre_pocketpy_test(void) { - pkpy_vm* vm = cre_pkpy_script_context_get_active_pkpy_vm(); - - TEST_MESSAGE("Misc pocketpy tests"); -#define CRE_TEST_POCKETPY_SOURCE ""\ -"class Test:\n"\ -" @staticmethod\n"\ -" def test_static(value: int) -> None:\n" \ -" pass\n"\ -"\n" - - pkpy_exec(vm, CRE_TEST_POCKETPY_SOURCE); - TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); - pkpy_exec(vm, "Test.test_static(12)"); - TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); - -#undef CRE_TEST_POCKETPY_SOURCE - - TEST_MESSAGE("Testing loading included internal modules"); - pkpy_exec(vm, "from crescent import Node"); - TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); - pkpy_eval(vm, "Node(10).entity_id"); - int nodeEntity = 0; - pkpy_to_int(vm, 0, &nodeEntity); - TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); - TEST_ASSERT_EQUAL_INT(10, nodeEntity); - pkpy_pop_top(vm); - TEST_ASSERT_EQUAL_INT(0, pkpy_stack_size(vm)); +// pkpy_vm* vm = cre_pkpy_script_context_get_active_pkpy_vm(); +// +// TEST_MESSAGE("Misc pocketpy tests"); +// #define CRE_TEST_POCKETPY_SOURCE ""\ +// "class Test:\n"\ +// " @staticmethod\n"\ +// " def test_static(value: int) -> None:\n" \ +// " pass\n"\ +// "\n" +// +// pkpy_exec(vm, CRE_TEST_POCKETPY_SOURCE); +// TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); +// pkpy_exec(vm, "Test.test_static(12)"); +// TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); +// +// #undef CRE_TEST_POCKETPY_SOURCE + + // TEST_MESSAGE("Testing loading included internal modules"); + // pkpy_exec(vm, "from crescent import Node"); + // TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); + // pkpy_eval(vm, "Node(10).entity_id"); + // int nodeEntity = 0; + // pkpy_to_int(vm, 0, &nodeEntity); + // TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); + // TEST_ASSERT_EQUAL_INT(10, nodeEntity); + // pkpy_pop_top(vm); + // TEST_ASSERT_EQUAL_INT(0, pkpy_stack_size(vm)); TEST_MESSAGE("Testing entity instance cache"); - const SkaEntity entity = cre_pkpy_entity_instance_cache_create_new_entity(vm, CRE_PKPY_MODULE_NAME_CRESCENT, "Node", ska_ecs_entity_create()); - cre_pkpy_entity_instance_cache_push_entity_instance(vm, entity); - TEST_ASSERT_EQUAL_INT(1, pkpy_stack_size(vm)); - pkpy_getattr(vm, pkpy_name("entity_id")); - TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); - pkpy_to_int(vm, 0, &nodeEntity); - TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); - TEST_ASSERT_EQUAL_INT((int)entity, nodeEntity); - // Test removing entity - TEST_ASSERT_TRUE(cre_pkpy_entity_instance_cache_has_entity(vm, nodeEntity)); - cre_pkpy_entity_instance_cache_remove_entity(vm, entity); - TEST_ASSERT_FALSE(cre_pkpy_entity_instance_cache_has_entity(vm, nodeEntity)); - pkpy_pop_top(vm); - TEST_ASSERT_EQUAL_INT(0, pkpy_stack_size(vm)); + // const SkaEntity entity = cre_pkpy_entity_instance_cache_create_new_entity(vm, CRE_PKPY_MODULE_NAME_CRESCENT, "Node", ska_ecs_entity_create()); + // cre_pkpy_entity_instance_cache_push_entity_instance(vm, entity); + // TEST_ASSERT_EQUAL_INT(1, pkpy_stack_size(vm)); + // pkpy_getattr(vm, pkpy_name("entity_id")); + // TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); + // pkpy_to_int(vm, 0, &nodeEntity); + // TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); + // TEST_ASSERT_EQUAL_INT((int)entity, nodeEntity); + // // Test removing entity + // TEST_ASSERT_TRUE(cre_pkpy_entity_instance_cache_has_entity(vm, nodeEntity)); + // cre_pkpy_entity_instance_cache_remove_entity(vm, entity); + // TEST_ASSERT_FALSE(cre_pkpy_entity_instance_cache_has_entity(vm, nodeEntity)); + // pkpy_pop_top(vm); + // TEST_ASSERT_EQUAL_INT(0, pkpy_stack_size(vm)); ska_asset_manager_initialize(); cre_scene_manager_initialize(); @@ -271,23 +268,15 @@ void cre_pocketpy_test(void) { TEST_MESSAGE("Testing python api"); // Load test node - char* testCustomNodesSource = ska_fs_read_file_contents("engine/test/resources/test_custom_nodes.py", NULL); - cre_pkpy_util_create_from_string(vm, "test_custom_nodes", testCustomNodesSource); - // Load test file - char* pythonText = ska_fs_read_file_contents("engine/test/resources/crescent_api_test.py", NULL); - TEST_ASSERT_NOT_NULL(pythonText); - pkpy_exec_2(vm, pythonText, "crescent_api_test.py", 0, NULL); - SKA_MEM_FREE(testCustomNodesSource); - SKA_MEM_FREE(pythonText); - TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); - - // Testing pushing broadcast event func for node manager - cre_pkpy_node_event_manager_broadcast_event(vm, 1, "talk"); - cre_pkpy_node_event_manager_broadcast_event_string(vm, 1, "talk_string", "Testing"); - cre_pkpy_node_event_manager_broadcast_event_int(vm, 1, "talk_int", 42); - cre_pkpy_node_event_manager_broadcast_event_float(vm, 1, "talk_float", 10.0f); - cre_pkpy_node_event_manager_broadcast_event_bool(vm, 1, "talk_bool", true); - TEST_ASSERT_EQUAL_INT(0, pkpy_stack_size(vm)); + // char* testCustomNodesSource = ska_fs_read_file_contents("engine/test/resources/test_custom_nodes.py", NULL); + // cre_pkpy_util_create_from_string(vm, "test_custom_nodes", testCustomNodesSource); + // // Load test file + // char* pythonText = ska_fs_read_file_contents("engine/test/resources/crescent_api_test.py", NULL); + // TEST_ASSERT_NOT_NULL(pythonText); + // pkpy_exec_2(vm, pythonText, "crescent_api_test.py", 0, NULL); + // SKA_MEM_FREE(testCustomNodesSource); + // SKA_MEM_FREE(pythonText); + // TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); cre_scene_manager_finalize(); ska_asset_manager_finalize(); From 192dec077db723afe4c9dbde591f9464b4446f21 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 21:12:35 -0400 Subject: [PATCH 43/54] Clean up. --- engine/src/core/ecs/systems/script_ec_system.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/engine/src/core/ecs/systems/script_ec_system.c b/engine/src/core/ecs/systems/script_ec_system.c index c4056ec5b..f5501a6ca 100644 --- a/engine/src/core/ecs/systems/script_ec_system.c +++ b/engine/src/core/ecs/systems/script_ec_system.c @@ -1,18 +1,16 @@ #include "script_ec_system.h" +#include +#include #include #include -#include -#include "../ecs_globals.h" -#include "../components/script_component.h" -#include "../../scene/scene_manager.h" -#include "../../scripting/script_context.h" -// #include "../../scripting/python/pocketpy/cre_pkpy_script_context.h" -#include "../../../../../cmake-build-release-mingw/_deps/seika_content-src/seika/logger.h" -#include "../../scripting/python/pocketpy/pkpy_script_context.h" -#include "../../scripting/native/native_script_context.h" -#include "../../scripting/native/internal_classes/fps_display_class.h" +#include "core/ecs/ecs_globals.h" +#include "core/ecs/components/script_component.h" +#include "core/scene/scene_manager.h" +#include "core/scripting/script_context.h" +#include "core/scripting/python/pocketpy/pkpy_script_context.h" +#include "core/scripting/native/native_script_context.h" static void on_ec_system_registered(SkaECSSystem* system); static void on_ec_system_destroyed(SkaECSSystem* system); From 367a8443f1df0cb02493dfbb81158285f9bc6414 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Sun, 6 Oct 2024 21:21:45 -0400 Subject: [PATCH 44/54] Updates. --- engine/src/core/ecs/components/script_component.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/engine/src/core/ecs/components/script_component.h b/engine/src/core/ecs/components/script_component.h index d31819f97..e00e234b7 100644 --- a/engine/src/core/ecs/components/script_component.h +++ b/engine/src/core/ecs/components/script_component.h @@ -1,11 +1,11 @@ #pragma once +#include "core/scripting/script_context.h" + #ifdef __cplusplus extern "C" { #endif -#include "src/core/scripting/script_context.h" - // TODO: Give a way for components to clean up themselves (e.g. call '[component_name]_component_delete' when deleted) // TODO: (Part2) This way we can have less memory overhead by having flexibility for objects to clean themselves up typedef struct ScriptComponent { From 90d85a858434fa82e16005fd94b94467f289a43c Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Mon, 7 Oct 2024 18:34:57 -0400 Subject: [PATCH 45/54] Updates. --- editor/CMakeLists.txt | 1 + .../scripting/native/native_script_context.h | 2 +- engine/src/core/scripting/script_context.c | 24 +++++++++---------- engine/src/core/scripting/script_context.h | 10 +++++++- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/editor/CMakeLists.txt b/editor/CMakeLists.txt index b214ba750..7a83d01d9 100644 --- a/editor/CMakeLists.txt +++ b/editor/CMakeLists.txt @@ -19,6 +19,7 @@ endif () file(GLOB_RECURSE CRESCENT_EDITOR_SRC src/core/*.cpp src/core/*.h) add_library(${PROJECT_NAME} STATIC ${CRESCENT_EDITOR_SRC}) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../engine/src) if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") target_link_libraries(${PROJECT_NAME} PUBLIC imgui implot seika font_awesome nlohmann_json zip) diff --git a/engine/src/core/scripting/native/native_script_context.h b/engine/src/core/scripting/native/native_script_context.h index 12b4c0583..297e97bb5 100644 --- a/engine/src/core/scripting/native/native_script_context.h +++ b/engine/src/core/scripting/native/native_script_context.h @@ -1,6 +1,6 @@ #pragma once -#include "../script_context.h" +#include "core/scripting/script_context.h" struct CRENativeScriptClass; diff --git a/engine/src/core/scripting/script_context.c b/engine/src/core/scripting/script_context.c index a67c33158..db0ac5c53 100644 --- a/engine/src/core/scripting/script_context.c +++ b/engine/src/core/scripting/script_context.c @@ -6,19 +6,19 @@ CREScriptContext* cre_script_context_create() { return SKA_MEM_ALLOCATE(CREScriptContext); } -CREScriptContext* cre_script_context_create_from_template(const CREScriptContextTemplate* template) { +CREScriptContext* cre_script_context_create_from_template(const CREScriptContextTemplate* temp) { CREScriptContext* scriptContext = cre_script_context_create(); - scriptContext->on_script_context_init = template->on_script_context_init; - scriptContext->on_script_context_finalize = template->on_script_context_finalize; - scriptContext->on_create_instance = template->on_create_instance; - scriptContext->on_delete_instance = template->on_delete_instance; - scriptContext->on_start = template->on_start; - scriptContext->on_pre_update_all = template->on_pre_update_all; - scriptContext->on_post_update_all = template->on_post_update_all; - scriptContext->on_update_instance = template->on_update_instance; - scriptContext->on_fixed_update_instance = template->on_fixed_update_instance; - scriptContext->on_end = template->on_end; - scriptContext->on_network_callback = template->on_network_callback; + scriptContext->on_script_context_init = temp->on_script_context_init; + scriptContext->on_script_context_finalize = temp->on_script_context_finalize; + scriptContext->on_create_instance = temp->on_create_instance; + scriptContext->on_delete_instance = temp->on_delete_instance; + scriptContext->on_start = temp->on_start; + scriptContext->on_pre_update_all = temp->on_pre_update_all; + scriptContext->on_post_update_all = temp->on_post_update_all; + scriptContext->on_update_instance = temp->on_update_instance; + scriptContext->on_fixed_update_instance = temp->on_fixed_update_instance; + scriptContext->on_end = temp->on_end; + scriptContext->on_network_callback = temp->on_network_callback; return scriptContext; } diff --git a/engine/src/core/scripting/script_context.h b/engine/src/core/scripting/script_context.h index ffa274761..e0214f162 100644 --- a/engine/src/core/scripting/script_context.h +++ b/engine/src/core/scripting/script_context.h @@ -4,6 +4,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + typedef void (*OnCreateInstance) (SkaEntity, const char*, const char*); typedef void (*OnDeleteInstance) (SkaEntity); typedef void (*OnStart) (SkaEntity); @@ -83,5 +87,9 @@ typedef struct CREScriptContextTemplate { } CREScriptContextTemplate; CREScriptContext* cre_script_context_create(); -CREScriptContext* cre_script_context_create_from_template(const CREScriptContextTemplate* template); +CREScriptContext* cre_script_context_create_from_template(const CREScriptContextTemplate* temp); void cre_script_context_destroy(CREScriptContext* scriptContext); + +#ifdef __cplusplus +} +#endif From ebf5c5e91ceb19d2609eebe13f03dff263fa0849 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Mon, 7 Oct 2024 21:20:17 -0400 Subject: [PATCH 46/54] Update. --- engine/src/core/scripting/script_context.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/engine/src/core/scripting/script_context.h b/engine/src/core/scripting/script_context.h index e0214f162..5faa01f1d 100644 --- a/engine/src/core/scripting/script_context.h +++ b/engine/src/core/scripting/script_context.h @@ -20,9 +20,6 @@ typedef void (*OnFixedUpdateInstance) (SkaEntity, f32); typedef void (*OnNetworkCallback) (const char*); -typedef void (*OnScriptContextInit) (struct CREScriptContext*); -typedef void (*OnScriptContextFinalize) (struct CREScriptContext*); - // TODO: Make network callbacks not specific to python struct _object; // PyObject typedef void (*OnEntitySubscribeToNetworkCallback) (SkaEntity, struct _object*, const char*); @@ -40,9 +37,9 @@ typedef enum CreScriptContextType { // Also check out other script contexts for examples. typedef struct CREScriptContext { // Called when the script context is created - OnScriptContextInit on_script_context_init; + void (*on_script_context_init)(struct CREScriptContext*); // Called when the script context is destroyed - OnScriptContextFinalize on_script_context_finalize; + void (*on_script_context_finalize)(struct CREScriptContext*); // Called when a new entity instance is requested to be created once a node has entered the scene. // If using the same function to dynamically create instance from the script side, be sure to check if the instance // Isn't already created @@ -71,6 +68,9 @@ typedef struct CREScriptContext { SkaEntity fixedUpdateEntities[SKA_MAX_ENTITIES]; } CREScriptContext; +typedef void (*OnScriptContextInit) (struct CREScriptContext*); +typedef void (*OnScriptContextFinalize) (struct CREScriptContext*); + typedef struct CREScriptContextTemplate { CreScriptContextType contextType; OnScriptContextInit on_script_context_init; From b49d11667477685394733193aa9ab143bcdc1634 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Mon, 7 Oct 2024 21:30:19 -0400 Subject: [PATCH 47/54] Update. --- engine/src/core/scripting/script_context.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/engine/src/core/scripting/script_context.h b/engine/src/core/scripting/script_context.h index 5faa01f1d..280dc2102 100644 --- a/engine/src/core/scripting/script_context.h +++ b/engine/src/core/scripting/script_context.h @@ -20,10 +20,6 @@ typedef void (*OnFixedUpdateInstance) (SkaEntity, f32); typedef void (*OnNetworkCallback) (const char*); -// TODO: Make network callbacks not specific to python -struct _object; // PyObject -typedef void (*OnEntitySubscribeToNetworkCallback) (SkaEntity, struct _object*, const char*); - typedef enum CreScriptContextType { CreScriptContextType_NONE = -1, // INVALID CreScriptContextType_PYTHON = 0, @@ -58,7 +54,6 @@ typedef struct CREScriptContext { OnEnd on_end; // The main network callback for forwarding network data to the script context OnNetworkCallback on_network_callback; -// OnEntitySubscribeToNetworkCallback on_entity_subscribe_to_network_callback; // We could have a validation step on the script contexts to check if the update, fixed_update, etc... funcs exists // in the class within the scripting language. For now, the script context is responsible for entity and entity count // even though it's not used in the script ec system From c10866156896680ca03300aca1e4b5477d1f1201 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Mon, 7 Oct 2024 21:38:46 -0400 Subject: [PATCH 48/54] Updates. --- .../python/pocketpy/pkpy_script_context.c | 3 +- engine/test/main.c | 65 ++++--------------- 2 files changed, 13 insertions(+), 55 deletions(-) diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index 35dd6c913..3eef9d774 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -69,7 +69,8 @@ void pkpy_init(CREScriptContext* context) { void pkpy_finalize(CREScriptContext* context) { scriptContext = NULL; cre_pkpy_instance_cache_finalize(); - py_finalize(); + // TODO: Doesn't work with test, so we won't free until we figure this out... + // py_finalize(); } void pkpy_create_instance(SkaEntity entity, const char* classPath, const char* className) { diff --git a/engine/test/main.c b/engine/test/main.c index 49a1ac3c1..737fbc7bb 100644 --- a/engine/test/main.c +++ b/engine/test/main.c @@ -48,14 +48,14 @@ void tearDown() { void cre_node_event_test(void); void cre_json_file_loader_scene_test(void); -void cre_pocketpy_test(void); +void cre_pocketpy_api_test(void); void cre_tilemap_test(void); int32 main(int argv, char** args) { UNITY_BEGIN(); RUN_TEST(cre_node_event_test); RUN_TEST(cre_json_file_loader_scene_test); - RUN_TEST(cre_pocketpy_test); + RUN_TEST(cre_pocketpy_api_test); RUN_TEST(cre_tilemap_test); return UNITY_END(); } @@ -207,52 +207,8 @@ void cre_json_file_loader_scene_test(void) { cre_json_delete_json_scene_node(rootNode); } -//--- Pocketpy Test ---// -void cre_pocketpy_test(void) { -// pkpy_vm* vm = cre_pkpy_script_context_get_active_pkpy_vm(); -// -// TEST_MESSAGE("Misc pocketpy tests"); -// #define CRE_TEST_POCKETPY_SOURCE ""\ -// "class Test:\n"\ -// " @staticmethod\n"\ -// " def test_static(value: int) -> None:\n" \ -// " pass\n"\ -// "\n" -// -// pkpy_exec(vm, CRE_TEST_POCKETPY_SOURCE); -// TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); -// pkpy_exec(vm, "Test.test_static(12)"); -// TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); -// -// #undef CRE_TEST_POCKETPY_SOURCE - - // TEST_MESSAGE("Testing loading included internal modules"); - // pkpy_exec(vm, "from crescent import Node"); - // TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); - // pkpy_eval(vm, "Node(10).entity_id"); - // int nodeEntity = 0; - // pkpy_to_int(vm, 0, &nodeEntity); - // TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); - // TEST_ASSERT_EQUAL_INT(10, nodeEntity); - // pkpy_pop_top(vm); - // TEST_ASSERT_EQUAL_INT(0, pkpy_stack_size(vm)); - - TEST_MESSAGE("Testing entity instance cache"); - // const SkaEntity entity = cre_pkpy_entity_instance_cache_create_new_entity(vm, CRE_PKPY_MODULE_NAME_CRESCENT, "Node", ska_ecs_entity_create()); - // cre_pkpy_entity_instance_cache_push_entity_instance(vm, entity); - // TEST_ASSERT_EQUAL_INT(1, pkpy_stack_size(vm)); - // pkpy_getattr(vm, pkpy_name("entity_id")); - // TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); - // pkpy_to_int(vm, 0, &nodeEntity); - // TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); - // TEST_ASSERT_EQUAL_INT((int)entity, nodeEntity); - // // Test removing entity - // TEST_ASSERT_TRUE(cre_pkpy_entity_instance_cache_has_entity(vm, nodeEntity)); - // cre_pkpy_entity_instance_cache_remove_entity(vm, entity); - // TEST_ASSERT_FALSE(cre_pkpy_entity_instance_cache_has_entity(vm, nodeEntity)); - // pkpy_pop_top(vm); - // TEST_ASSERT_EQUAL_INT(0, pkpy_stack_size(vm)); - +//--- Pocketpy API Test ---// +void cre_pocketpy_api_test(void) { ska_asset_manager_initialize(); cre_scene_manager_initialize(); cre_scene_manager_queue_scene_change("engine/test/resources/test_scene1.cscn"); @@ -268,14 +224,15 @@ void cre_pocketpy_test(void) { TEST_MESSAGE("Testing python api"); // Load test node - // char* testCustomNodesSource = ska_fs_read_file_contents("engine/test/resources/test_custom_nodes.py", NULL); - // cre_pkpy_util_create_from_string(vm, "test_custom_nodes", testCustomNodesSource); + char* testCustomNodesSource = ska_fs_read_file_contents("engine/test/resources/test_custom_nodes.py", NULL); + cre_pkpy_util_create_from_string("test_custom_nodes", testCustomNodesSource); // // Load test file - // char* pythonText = ska_fs_read_file_contents("engine/test/resources/crescent_api_test.py", NULL); - // TEST_ASSERT_NOT_NULL(pythonText); + char* pythonText = ska_fs_read_file_contents("engine/test/resources/crescent_api_test.py", NULL); + TEST_ASSERT_NOT_NULL(pythonText); + py_exec(pythonText, "crescent_api_test.py", EXEC_MODE, NULL); // pkpy_exec_2(vm, pythonText, "crescent_api_test.py", 0, NULL); - // SKA_MEM_FREE(testCustomNodesSource); - // SKA_MEM_FREE(pythonText); + SKA_MEM_FREE(testCustomNodesSource); + SKA_MEM_FREE(pythonText); // TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); cre_scene_manager_finalize(); From f9d2239f8849e985511ad4321c3f7ad7e8ff06b1 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Mon, 7 Oct 2024 22:11:02 -0400 Subject: [PATCH 49/54] Clean. --- crescent_py_api/pocketpy/crescent.py | 20 +++++++++---------- engine/src/core/scene/scene_manager.c | 8 ++++++-- engine/src/core/scene/scene_manager.h | 2 +- .../python/pocketpy/api/pkpy_api_impl.c | 7 ++++--- .../python/pocketpy/cre_pkpy_api_source.h | 20 +++++++++---------- engine/test/main.c | 6 +++--- 6 files changed, 34 insertions(+), 29 deletions(-) diff --git a/crescent_py_api/pocketpy/crescent.py b/crescent_py_api/pocketpy/crescent.py index 471e3a2bc..94a52aadb 100644 --- a/crescent_py_api/pocketpy/crescent.py +++ b/crescent_py_api/pocketpy/crescent.py @@ -936,7 +936,7 @@ def __init__(self, entity_id: int) -> None: @classmethod def new(cls) -> "Node": - return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Node) + return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Node) def get_name(self) -> str: return crescent_internal.node_get_name(self.entity_id) @@ -1008,7 +1008,7 @@ def __repr__(self) -> str: class Node2D(Node): @classmethod def new(cls) -> "Node2D": - return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Node2D) + return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Node2D) def set_position(self, value: Vector2) -> None: crescent_internal.node2d_set_position(self.entity_id, value.x, value.y) @@ -1102,7 +1102,7 @@ def ignore_camera(self, value: bool) -> None: class Sprite(Node2D): @classmethod def new(cls) -> "Sprite": - return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Sprite) + return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Sprite) @property def texture(self) -> Texture: @@ -1171,7 +1171,7 @@ def shader_instance(self, value: ShaderInstance) -> None: class AnimatedSprite(Node2D): @classmethod def new(cls) -> "AnimatedSprite": - return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.AnimatedSprite) + return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.AnimatedSprite) def play(self, name: str) -> bool: return crescent_internal.animated_sprite_play(self.entity_id, name) @@ -1242,7 +1242,7 @@ def shader_instance(self, value: ShaderInstance) -> None: class TextLabel(Node2D): @classmethod def new(cls) -> "TextLabel": - return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.TextLabel) + return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.TextLabel) @property def text(self) -> str: @@ -1281,7 +1281,7 @@ def set_font(self, uid: str) -> None: class Collider2D(Node2D): @classmethod def new(cls) -> "Collider2D": - return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Collider2D) + return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Collider2D) def get_extents(self) -> Size2D: w, h = crescent_internal.collider2d_get_extents(self.entity_id) @@ -1319,7 +1319,7 @@ def color(self, value: Color) -> None: class ColorRect(Node2D): @classmethod def new(cls) -> "ColorRect": - return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.ColorRect) + return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.ColorRect) def get_size(self) -> Size2D: w, h = crescent_internal.color_rect_get_size(self.entity_id) @@ -1357,7 +1357,7 @@ def color(self, value: Color) -> None: class Parallax(Node2D): @classmethod def new(cls) -> "Parallax": - return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Parallax) + return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Parallax) @property def scroll_speed(self) -> Vector2: @@ -1372,7 +1372,7 @@ def scroll_speed(self, value: Vector2) -> None: class Particles2D(Node2D): @classmethod def new(cls) -> "Particles2D": - return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Particles2D) + return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Particles2D) @property def amount(self) -> int: @@ -1439,7 +1439,7 @@ def spread(self, value: float) -> None: class Tilemap(Node2D): @classmethod def new(cls) -> "Tilemap": - return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Tilemap) + return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Tilemap) class SceneTree: diff --git a/engine/src/core/scene/scene_manager.c b/engine/src/core/scene/scene_manager.c index 8e77f2360..bd2a13361 100644 --- a/engine/src/core/scene/scene_manager.c +++ b/engine/src/core/scene/scene_manager.c @@ -336,7 +336,11 @@ f32 cre_scene_manager_get_node_full_time_dilation(SkaEntity entity) { } SceneTreeNode* cre_scene_manager_get_entity_tree_node(SkaEntity entity) { - SKA_ASSERT_FMT(ska_hash_map_has(entityToTreeNodeMap, &entity), "Doesn't have entity '%d' in scene tree!", entity); + if (!ska_hash_map_has(entityToTreeNodeMap, &entity)) { + const NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); + SKA_ASSERT_FMT(ska_hash_map_has(entityToTreeNodeMap, &entity), "Doesn't have entity '%d' in scene tree!", entity); + } + // SKA_ASSERT_FMT(ska_hash_map_has(entityToTreeNodeMap, &entity), "Doesn't have entity '%d' in scene tree!", entity); SceneTreeNode* treeNode = (SceneTreeNode*) *(SceneTreeNode**) ska_hash_map_get(entityToTreeNodeMap, &entity); SKA_ASSERT_FMT(treeNode != NULL, "Failed to get tree node for entity '%d'", entity); return treeNode; @@ -369,7 +373,7 @@ bool cre_scene_manager_has_entity_tree_node(SkaEntity entity) { return ska_hash_map_has(entityToTreeNodeMap, &entity); } -void cre_scene_manager_add_node_as_child(SkaEntity childEntity, SkaEntity parentEntity) { +void cre_scene_manager_add_node_as_child(SkaEntity parentEntity, SkaEntity childEntity) { SceneTreeNode* parentNode = cre_scene_manager_get_entity_tree_node(parentEntity); SceneTreeNode* node = cre_scene_manager_pop_staged_entity_tree_node(childEntity); node->parent = parentNode; diff --git a/engine/src/core/scene/scene_manager.h b/engine/src/core/scene/scene_manager.h index a7a5c4eff..b578d43e9 100644 --- a/engine/src/core/scene/scene_manager.h +++ b/engine/src/core/scene/scene_manager.h @@ -59,7 +59,7 @@ f32 cre_scene_manager_get_node_full_time_dilation(SkaEntity entity); SkaEntity cre_scene_manager_get_entity_child_by_name(SkaEntity parent, const char* childName); SceneTreeNode* cre_scene_manager_get_entity_tree_node(SkaEntity entity); bool cre_scene_manager_has_entity_tree_node(SkaEntity entity); -void cre_scene_manager_add_node_as_child(SkaEntity childEntity, SkaEntity parentEntity); +void cre_scene_manager_add_node_as_child(SkaEntity parentEntity, SkaEntity childEntity); EntityArray cre_scene_manager_get_self_and_parent_nodes(SkaEntity entity); void cre_scene_manager_invalidate_time_dilation_nodes_with_children(SkaEntity entity); diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 9190960c9..8506cd424 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -1143,10 +1143,10 @@ bool cre_pkpy_api_node_get_name(int argc, py_StackRef argv) { bool cre_pkpy_api_node_add_child(int argc, py_StackRef argv) { PY_CHECK_ARGC(2); PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(1, tp_int); - const py_i64 childEntityId = py_toint(py_arg(0)); - const py_i64 parentEntityId = py_toint(py_arg(1)); + const py_i64 parentEntityId = py_toint(py_arg(0)); + const py_i64 childEntityId = py_toint(py_arg(1)); - cre_scene_manager_add_node_as_child((SkaEntity)childEntityId, (SkaEntity)parentEntityId); + cre_scene_manager_add_node_as_child((SkaEntity)parentEntityId, (SkaEntity)childEntityId); py_newnone(py_retval()); return true; } @@ -1172,6 +1172,7 @@ bool cre_pkpy_api_node_get_children(int argc, py_StackRef argv) { PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 entityId = py_toint(py_arg(0)); + const SkaEntity entity = (SkaEntity)entityId; if (cre_scene_manager_has_entity_tree_node(entity)) { const SceneTreeNode* parentTreeNode = cre_scene_manager_get_entity_tree_node(entity); diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h index 20c56021c..465ea2019 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h @@ -939,7 +939,7 @@ "\n"\ " @classmethod\n"\ " def new(cls) -> \"Node\":\n"\ -" return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Node)\n"\ +" return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Node)\n"\ "\n"\ " def get_name(self) -> str:\n"\ " return crescent_internal.node_get_name(self.entity_id)\n"\ @@ -1011,7 +1011,7 @@ "class Node2D(Node):\n"\ " @classmethod\n"\ " def new(cls) -> \"Node2D\":\n"\ -" return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Node2D)\n"\ +" return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Node2D)\n"\ "\n"\ " def set_position(self, value: Vector2) -> None:\n"\ " crescent_internal.node2d_set_position(self.entity_id, value.x, value.y)\n"\ @@ -1105,7 +1105,7 @@ "class Sprite(Node2D):\n"\ " @classmethod\n"\ " def new(cls) -> \"Sprite\":\n"\ -" return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Sprite)\n"\ +" return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Sprite)\n"\ "\n"\ " @property\n"\ " def texture(self) -> Texture:\n"\ @@ -1174,7 +1174,7 @@ "class AnimatedSprite(Node2D):\n"\ " @classmethod\n"\ " def new(cls) -> \"AnimatedSprite\":\n"\ -" return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.AnimatedSprite)\n"\ +" return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.AnimatedSprite)\n"\ "\n"\ " def play(self, name: str) -> bool:\n"\ " return crescent_internal.animated_sprite_play(self.entity_id, name)\n"\ @@ -1245,7 +1245,7 @@ "class TextLabel(Node2D):\n"\ " @classmethod\n"\ " def new(cls) -> \"TextLabel\":\n"\ -" return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.TextLabel)\n"\ +" return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.TextLabel)\n"\ "\n"\ " @property\n"\ " def text(self) -> str:\n"\ @@ -1284,7 +1284,7 @@ "class Collider2D(Node2D):\n"\ " @classmethod\n"\ " def new(cls) -> \"Collider2D\":\n"\ -" return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Collider2D)\n"\ +" return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Collider2D)\n"\ "\n"\ " def get_extents(self) -> Size2D:\n"\ " w, h = crescent_internal.collider2d_get_extents(self.entity_id)\n"\ @@ -1322,7 +1322,7 @@ "class ColorRect(Node2D):\n"\ " @classmethod\n"\ " def new(cls) -> \"ColorRect\":\n"\ -" return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.ColorRect)\n"\ +" return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.ColorRect)\n"\ "\n"\ " def get_size(self) -> Size2D:\n"\ " w, h = crescent_internal.color_rect_get_size(self.entity_id)\n"\ @@ -1360,7 +1360,7 @@ "class Parallax(Node2D):\n"\ " @classmethod\n"\ " def new(cls) -> \"Parallax\":\n"\ -" return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Parallax)\n"\ +" return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Parallax)\n"\ "\n"\ " @property\n"\ " def scroll_speed(self) -> Vector2:\n"\ @@ -1375,7 +1375,7 @@ "class Particles2D(Node2D):\n"\ " @classmethod\n"\ " def new(cls) -> \"Particles2D\":\n"\ -" return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Particles2D)\n"\ +" return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Particles2D)\n"\ "\n"\ " @property\n"\ " def amount(self) -> int:\n"\ @@ -1442,7 +1442,7 @@ "class Tilemap(Node2D):\n"\ " @classmethod\n"\ " def new(cls) -> \"Tilemap\":\n"\ -" return crescent_internal.node_new(str(cls.__module__.__name__), cls.__name__, NodeType.Tilemap)\n"\ +" return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Tilemap)\n"\ "\n"\ "\n"\ "class SceneTree:\n"\ diff --git a/engine/test/main.c b/engine/test/main.c index 737fbc7bb..cecc92c21 100644 --- a/engine/test/main.c +++ b/engine/test/main.c @@ -226,14 +226,14 @@ void cre_pocketpy_api_test(void) { // Load test node char* testCustomNodesSource = ska_fs_read_file_contents("engine/test/resources/test_custom_nodes.py", NULL); cre_pkpy_util_create_from_string("test_custom_nodes", testCustomNodesSource); - // // Load test file + // Load test file char* pythonText = ska_fs_read_file_contents("engine/test/resources/crescent_api_test.py", NULL); TEST_ASSERT_NOT_NULL(pythonText); py_exec(pythonText, "crescent_api_test.py", EXEC_MODE, NULL); - // pkpy_exec_2(vm, pythonText, "crescent_api_test.py", 0, NULL); SKA_MEM_FREE(testCustomNodesSource); SKA_MEM_FREE(pythonText); - // TEST_ASSERT_FALSE(cre_pkpy_util_print_error_message(vm)); + if (py_checkexc(false)) { printf("PKPY Error:\n%s", py_formatexc()); } + TEST_ASSERT_FALSE(py_checkexc(false)); cre_scene_manager_finalize(); ska_asset_manager_finalize(); From de96e2f6e067acad22e94dcab16840a89365170f Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Tue, 8 Oct 2024 22:00:23 -0400 Subject: [PATCH 50/54] More updates. --- crescent_py_api/pocketpy/crescent.py | 12 +++--------- crescent_py_api/pocketpy/crescent_internal.py | 4 ++-- .../scripting/python/pocketpy/api/pkpy_api_impl.c | 2 +- .../scripting/python/pocketpy/cre_pkpy_api_source.h | 12 +++--------- 4 files changed, 9 insertions(+), 21 deletions(-) diff --git a/crescent_py_api/pocketpy/crescent.py b/crescent_py_api/pocketpy/crescent.py index 94a52aadb..2a1d8c5fa 100644 --- a/crescent_py_api/pocketpy/crescent.py +++ b/crescent_py_api/pocketpy/crescent.py @@ -58,8 +58,8 @@ def lerp(source: float, destination: float, amount: float) -> float: class Vector2: def __init__(self, x=0.0, y=0.0): - self.x = x - self.y = y + self.x = float(x) + self.y = float(y) def dot_product(self, other) -> float: return (self.x * other.x) + (self.y * other.y) @@ -952,13 +952,7 @@ def get_child(self, child_name: str) -> Optional["Node"]: return crescent_internal.node_get_child(self.entity_id, child_name) def get_children(self) -> List["Node"]: - children = crescent_internal.node_get_children(self.entity_id) - if children: - if isinstance(children, tuple): - return list(children) - elif isinstance(children, Node): - return [children] - return [] + return crescent_internal.node_get_children(self.entity_id) def get_parent(self) -> Optional["Node"]: return crescent_internal.node_get_parent(self.entity_id) diff --git a/crescent_py_api/pocketpy/crescent_internal.py b/crescent_py_api/pocketpy/crescent_internal.py index 3d022850d..781dfcc36 100644 --- a/crescent_py_api/pocketpy/crescent_internal.py +++ b/crescent_py_api/pocketpy/crescent_internal.py @@ -20,8 +20,8 @@ def node_get_child(parent_entity_id: int, child_entity_name: str) -> Optional["N return None -def node_get_children(entity_id: int) -> Tuple["Node", ...]: - return () +def node_get_children(entity_id: int) -> List["Node"]: + return [] def node_get_parent(entity_id: int) -> Optional["Node"]: diff --git a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c index 8506cd424..e1ed821e4 100644 --- a/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c +++ b/engine/src/core/scripting/python/pocketpy/api/pkpy_api_impl.c @@ -1172,7 +1172,6 @@ bool cre_pkpy_api_node_get_children(int argc, py_StackRef argv) { PY_CHECK_ARG_TYPE(0, tp_int); const py_i64 entityId = py_toint(py_arg(0)); - const SkaEntity entity = (SkaEntity)entityId; if (cre_scene_manager_has_entity_tree_node(entity)) { const SceneTreeNode* parentTreeNode = cre_scene_manager_get_entity_tree_node(entity); @@ -1335,6 +1334,7 @@ bool cre_pkpy_api_node2d_add_to_position(int argc, py_StackRef argv) { .x = transformComp->localTransform.position.x + (f32)posX, .y = transformComp->localTransform.position.y + (f32)posY }); + py_newnone(py_retval()); return true; } diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h index 465ea2019..2077c826d 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h @@ -61,8 +61,8 @@ "\n"\ "class Vector2:\n"\ " def __init__(self, x=0.0, y=0.0):\n"\ -" self.x = x\n"\ -" self.y = y\n"\ +" self.x = float(x)\n"\ +" self.y = float(y)\n"\ "\n"\ " def dot_product(self, other) -> float:\n"\ " return (self.x * other.x) + (self.y * other.y)\n"\ @@ -955,13 +955,7 @@ " return crescent_internal.node_get_child(self.entity_id, child_name)\n"\ "\n"\ " def get_children(self) -> List[\"Node\"]:\n"\ -" children = crescent_internal.node_get_children(self.entity_id)\n"\ -" if children:\n"\ -" if isinstance(children, tuple):\n"\ -" return list(children)\n"\ -" elif isinstance(children, Node):\n"\ -" return [children]\n"\ -" return []\n"\ +" return crescent_internal.node_get_children(self.entity_id)\n"\ "\n"\ " def get_parent(self) -> Optional[\"Node\"]:\n"\ " return crescent_internal.node_get_parent(self.entity_id)\n"\ From e2d12579396342a4d541d26d132fc0c57a13a18e Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Wed, 9 Oct 2024 21:46:32 -0400 Subject: [PATCH 51/54] More tests. --- crescent_py_api/pocketpy/crescent.py | 58 +++++++++++++- .../pocketpy/crescent_internal_py.py | 15 ---- .../pocketpy/utils/pkpy_header_generator.py | 4 - .../python/pocketpy/cre_pkpy_api_source.h | 76 +++++++++++++------ .../python/pocketpy/pkpy_script_context.c | 9 +++ engine/test/resources/crescent_api_test.py | 4 +- 6 files changed, 119 insertions(+), 47 deletions(-) delete mode 100644 crescent_py_api/pocketpy/crescent_internal_py.py diff --git a/crescent_py_api/pocketpy/crescent.py b/crescent_py_api/pocketpy/crescent.py index 2a1d8c5fa..1ae8d092e 100644 --- a/crescent_py_api/pocketpy/crescent.py +++ b/crescent_py_api/pocketpy/crescent.py @@ -294,10 +294,10 @@ def to_vec2(self) -> Vector2: class Rect2: def __init__(self, x=0.0, y=0.0, w=0.0, h=0.0): - self.x = x - self.y = y - self.w = w - self.h = h + self.x = float(x) + self.y = float(y) + self.w = float(w) + self.h = float(h) def __eq__(self, other) -> bool: if self.x == other.x and self.y == other.y and self.w == other.w and self.h == other.h: @@ -930,9 +930,53 @@ class NodeType: Tilemap = 512 +class NodeEventSubscriber: + def __init__(self, entity_id: int, callback: Callable[..., None]): + self.entity_id = entity_id + self.callback = callback + + def __eq__(self, other: "NodeEventSubscriber") -> bool: + return self.entity_id == other.entity_id + + +_node_event_subscriber_links = {} + + +class NodeEvent: + def __init__(self, owner: "Node") -> None: + self.owner_id = owner.entity_id + self.subscribers = [] + + def subscribe(self, subscriber: "Node", callback: Callable[..., None]) -> None: + if subscriber.entity_id not in _node_event_subscriber_links: + _node_event_subscriber_links[subscriber.entity_id] = [] + if self not in _node_event_subscriber_links[subscriber.entity_id]: + _node_event_subscriber_links[subscriber.entity_id].append(self) + self.subscribers.append(NodeEventSubscriber(subscriber.entity_id, callback)) + + def unsubscribe(self, subscriber: "Node") -> None: + for sub in self.subscribers[:]: + if sub == subscriber: + self.subscribers.remove(sub) + + def broadcast(self, *args) -> None: + for sub in self.subscribers: + sub.callback(args) + + @staticmethod + def unsubscribe_from_all(subscriber: "Node") -> None: + if subscriber.entity_id in _node_event_subscriber_links: + for event in _node_event_subscriber_links[subscriber.entity_id]: + event.unsubscribe(subscriber) + del _node_event_subscriber_links[subscriber.entity_id] + + class Node: + def __init__(self, entity_id: int) -> None: self.entity_id = entity_id + self.scene_entered = NodeEvent(self) + self.scene_exited = NodeEvent(self) @classmethod def new(cls) -> "Node": @@ -1163,6 +1207,12 @@ def shader_instance(self, value: ShaderInstance) -> None: class AnimatedSprite(Node2D): + + def __init__(self, entity_id: int) -> None: + super().__init__(entity_id) + self.frame_changed = NodeEvent(self) + self.animation_finished = NodeEvent(self) + @classmethod def new(cls) -> "AnimatedSprite": return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.AnimatedSprite) diff --git a/crescent_py_api/pocketpy/crescent_internal_py.py b/crescent_py_api/pocketpy/crescent_internal_py.py deleted file mode 100644 index e5f92d4a9..000000000 --- a/crescent_py_api/pocketpy/crescent_internal_py.py +++ /dev/null @@ -1,15 +0,0 @@ -from typing import Dict, List, Optional, Callable, Tuple - -from crescent import Node - -# Engine's python api used for internal functionality - -CRE_ENTITY_TO_NODE_MAP: Dict[int, Node] = {} - - -def get_entity(entity_id: int) -> Node: - return CRE_ENTITY_TO_NODE_MAP[entity_id] - - -def set_entity(node: Node) -> None: - CRE_ENTITY_TO_NODE_MAP[node.entity_id] = node diff --git a/crescent_py_api/pocketpy/utils/pkpy_header_generator.py b/crescent_py_api/pocketpy/utils/pkpy_header_generator.py index 78a6e2b22..a89e0e8de 100644 --- a/crescent_py_api/pocketpy/utils/pkpy_header_generator.py +++ b/crescent_py_api/pocketpy/utils/pkpy_header_generator.py @@ -28,9 +28,5 @@ def generate_source_header_file( output_path="../../../engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h", source_inputs=[ SourceInput(file_path="../crescent.py", macro_name="CRE_PKPY_CRESCENT_SOURCE"), - SourceInput( - file_path="../crescent_internal_py.py", - macro_name="CRE_PKPY_CRESCENT_INTERNAL_PY_SOURCE", - ), ], ) diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h index 2077c826d..46e20d157 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h @@ -297,10 +297,10 @@ "\n"\ "class Rect2:\n"\ " def __init__(self, x=0.0, y=0.0, w=0.0, h=0.0):\n"\ -" self.x = x\n"\ -" self.y = y\n"\ -" self.w = w\n"\ -" self.h = h\n"\ +" self.x = float(x)\n"\ +" self.y = float(y)\n"\ +" self.w = float(w)\n"\ +" self.h = float(h)\n"\ "\n"\ " def __eq__(self, other) -> bool:\n"\ " if self.x == other.x and self.y == other.y and self.w == other.w and self.h == other.h:\n"\ @@ -933,9 +933,53 @@ " Tilemap = 512\n"\ "\n"\ "\n"\ +"class NodeEventSubscriber:\n"\ +" def __init__(self, entity_id: int, callback: Callable[..., None]):\n"\ +" self.entity_id = entity_id\n"\ +" self.callback = callback\n"\ +"\n"\ +" def __eq__(self, other: \"NodeEventSubscriber\") -> bool:\n"\ +" return self.entity_id == other.entity_id\n"\ +"\n"\ +"\n"\ +"_node_event_subscriber_links = {}\n"\ +"\n"\ +"\n"\ +"class NodeEvent:\n"\ +" def __init__(self, owner: \"Node\") -> None:\n"\ +" self.owner_id = owner.entity_id\n"\ +" self.subscribers = []\n"\ +"\n"\ +" def subscribe(self, subscriber: \"Node\", callback: Callable[..., None]) -> None:\n"\ +" if subscriber.entity_id not in _node_event_subscriber_links:\n"\ +" _node_event_subscriber_links[subscriber.entity_id] = []\n"\ +" if self not in _node_event_subscriber_links[subscriber.entity_id]:\n"\ +" _node_event_subscriber_links[subscriber.entity_id].append(self)\n"\ +" self.subscribers.append(NodeEventSubscriber(subscriber.entity_id, callback))\n"\ +"\n"\ +" def unsubscribe(self, subscriber: \"Node\") -> None:\n"\ +" for sub in self.subscribers[:]:\n"\ +" if sub == subscriber:\n"\ +" self.subscribers.remove(sub)\n"\ +"\n"\ +" def broadcast(self, *args) -> None:\n"\ +" for sub in self.subscribers:\n"\ +" sub.callback(args)\n"\ +"\n"\ +" @staticmethod\n"\ +" def unsubscribe_from_all(subscriber: \"Node\") -> None:\n"\ +" if subscriber.entity_id in _node_event_subscriber_links:\n"\ +" for event in _node_event_subscriber_links[subscriber.entity_id]:\n"\ +" event.unsubscribe(subscriber)\n"\ +" del _node_event_subscriber_links[subscriber.entity_id]\n"\ +"\n"\ +"\n"\ "class Node:\n"\ +"\n"\ " def __init__(self, entity_id: int) -> None:\n"\ " self.entity_id = entity_id\n"\ +" self.scene_entered = NodeEvent(self)\n"\ +" self.scene_exited = NodeEvent(self)\n"\ "\n"\ " @classmethod\n"\ " def new(cls) -> \"Node\":\n"\ @@ -1166,6 +1210,12 @@ "\n"\ "\n"\ "class AnimatedSprite(Node2D):\n"\ +"\n"\ +" def __init__(self, entity_id: int) -> None:\n"\ +" super().__init__(entity_id)\n"\ +" self.frame_changed = NodeEvent(self)\n"\ +" self.animation_finished = NodeEvent(self)\n"\ +"\n"\ " @classmethod\n"\ " def new(cls) -> \"AnimatedSprite\":\n"\ " return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.AnimatedSprite)\n"\ @@ -1676,21 +1726,3 @@ " _node_event_manager.subscribe_to_event(-100, event_name, listener_node.entity_id, listener_func)\n"\ "\n" -#define CRE_PKPY_CRESCENT_INTERNAL_PY_SOURCE ""\ -"from typing import Dict, List, Optional, Callable, Tuple\n"\ -"\n"\ -"from crescent import Node\n"\ -"\n"\ -"# Engine's python api used for internal functionality\n"\ -"\n"\ -"CRE_ENTITY_TO_NODE_MAP: Dict[int, Node] = {}\n"\ -"\n"\ -"\n"\ -"def get_entity(entity_id: int) -> Node:\n"\ -" return CRE_ENTITY_TO_NODE_MAP[entity_id]\n"\ -"\n"\ -"\n"\ -"def set_entity(node: Node) -> None:\n"\ -" CRE_ENTITY_TO_NODE_MAP[node.entity_id] = node\n"\ -"\n" - diff --git a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c index 3eef9d774..6d0f812da 100644 --- a/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c +++ b/engine/src/core/scripting/python/pocketpy/pkpy_script_context.c @@ -104,6 +104,13 @@ void pkpy_delete_instance(SkaEntity entity) { ); } +static void broadcast_internal_event(SkaEntity entity, py_Ref self, const char* eventName) { + static char broadcastStringBuffer[64]; + snprintf(broadcastStringBuffer, sizeof(broadcastStringBuffer), "_e_%u.%s.broadcast()", entity, eventName); + py_exec(broadcastStringBuffer, "
", EXEC_MODE, NULL); + PY_ASSERT_NO_EXC(); +} + void pkpy_on_start(SkaEntity entity) { py_Ref self = cre_pkpy_instance_cache_get_checked(entity); if (py_getattr(self, startFunctionName)) { @@ -114,6 +121,7 @@ void pkpy_on_start(SkaEntity entity) { } else { py_clearexc(NULL); } + broadcast_internal_event(entity, self, "scene_entered"); } void pkpy_on_end(SkaEntity entity) { @@ -126,6 +134,7 @@ void pkpy_on_end(SkaEntity entity) { } else { py_clearexc(NULL); } + broadcast_internal_event(entity, self, "scene_exited"); } void pkpy_on_update(SkaEntity entity, f32 deltaTime) { diff --git a/engine/test/resources/crescent_api_test.py b/engine/test/resources/crescent_api_test.py index 5eb649e37..881a571b4 100644 --- a/engine/test/resources/crescent_api_test.py +++ b/engine/test/resources/crescent_api_test.py @@ -157,8 +157,8 @@ def on_has_exited_scene(*args): global has_exited_scene has_exited_scene = True - test_node.subscribe_to_event("scene_entered", root_node, on_has_entered_scene) - test_node.subscribe_to_event("scene_exited", root_node, on_has_exited_scene) + test_node.scene_entered.subscribe(root_node, on_has_entered_scene) + test_node.scene_exited.subscribe(root_node, on_has_exited_scene) root_node.add_child(test_node) crescent_internal._scene_manager_process_queued_creation_entities() From 5b6af7e765ed1a25300b218976bcf10b3f54eb3c Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Thu, 10 Oct 2024 17:47:43 -0400 Subject: [PATCH 52/54] Updates. --- crescent_py_api/pocketpy/crescent.py | 144 +++--------------- .../python/pocketpy/cre_pkpy_api_source.h | 144 +++--------------- engine/test/resources/crescent_api_test.py | 21 ++- 3 files changed, 51 insertions(+), 258 deletions(-) diff --git a/crescent_py_api/pocketpy/crescent.py b/crescent_py_api/pocketpy/crescent.py index 1ae8d092e..a6789a0f2 100644 --- a/crescent_py_api/pocketpy/crescent.py +++ b/crescent_py_api/pocketpy/crescent.py @@ -58,8 +58,8 @@ def lerp(source: float, destination: float, amount: float) -> float: class Vector2: def __init__(self, x=0.0, y=0.0): - self.x = float(x) - self.y = float(y) + self.x = x + self.y = y def dot_product(self, other) -> float: return (self.x * other.x) + (self.y * other.y) @@ -294,10 +294,10 @@ def to_vec2(self) -> Vector2: class Rect2: def __init__(self, x=0.0, y=0.0, w=0.0, h=0.0): - self.x = float(x) - self.y = float(y) - self.w = float(w) - self.h = float(h) + self.x = x + self.y = y + self.w = w + self.h = h def __eq__(self, other) -> bool: if self.x == other.x and self.y == other.y and self.w == other.w and self.h == other.h: @@ -820,103 +820,6 @@ class Gamepad: AXIS_RIGHT_ANALOG_DOWN = 28 -class _NodeEventSubscriber: - def __init__(self, entity_id: int, call_back: Callable[[Tuple], None], event_owner_entity_id: int, event_name: str) -> None: - self.entity_id = entity_id - self.call_back = call_back - self.event_owner_entity_id = event_owner_entity_id - self.event_name = event_name - - -class _NodeEvent: - def __init__(self, entity_id: int, name: str, subscribers: List[_NodeEventSubscriber] = None) -> None: - self.entity_id = entity_id - self.name = name - if not subscribers: - subscribers = [] - self.subscribers = subscribers - - def add_or_update_subscriber(self, entity_id: int, call_back: Callable[[Tuple], None]) -> _NodeEventSubscriber: - for sub in self.subscribers: - if entity_id == sub.entity_id: - sub.call_back = call_back - return sub - subscriber = _NodeEventSubscriber(entity_id, call_back, self.entity_id, self.name) - self.subscribers.append(subscriber) - return subscriber - - def remove_subscriber(self, subscribe_entity_id: int) -> None: - for sub in self.subscribers: - if subscribe_entity_id == sub.entity_id: - self.subscribers.remove(sub) - break - - -class _NodeEventManager: - """ - Used to manage events between nodes - """ - def __init__(self, events: Dict[int, Dict[str, _NodeEvent]] = None, entity_subscribers: Dict[int, List[_NodeEventSubscriber]] = None) -> None: - if not events: - events = {} - if not entity_subscribers: - entity_subscribers = {} - self.events = events - self.entity_subscribers = entity_subscribers - - def create_event(self, entity_id: int, event_name: str) -> _NodeEvent: - if entity_id not in self.events: - self.events[entity_id] = {} - if event_name not in self.events[entity_id]: - self.events[entity_id][event_name] = _NodeEvent(entity_id, event_name) - return self.events[entity_id][event_name] - - def remove_event(self, entity_id: int, event_name: str) -> None: - if self.has_event(entity_id, event_name): - del self.events[entity_id][event_name] - - def remove_entity_and_connections(self, entity_id: int) -> None: - if entity_id in self.events: - del self.events[entity_id] - if entity_id in self.entity_subscribers: - for sub in self.entity_subscribers[entity_id]: - event = self.get_event(sub.event_owner_entity_id, sub.event_name) - if event: - event.remove_subscriber(entity_id) - del self.entity_subscribers[entity_id] - - def get_event(self, entity_id: int, event_name: str) -> Optional[_NodeEvent]: - if self.has_event(entity_id, event_name): - return self.events[entity_id][event_name] - return None - - def has_event(self, entity_id: int, event_name: str) -> bool: - return entity_id in self.events and event_name in self.events[entity_id] - - def broadcast_event(self, entity_id: int, event_name: str, *args) -> None: - event = self.create_event(entity_id, event_name) - for sub in event.subscribers: - sub.call_back(*args) - - def subscribe_to_event(self, owner_entity: int, event_name: str, subscriber_entity: int, subscriber_call_back: Callable[[...], None]) -> None: - event = self.create_event(owner_entity, event_name) - subscriber = event.add_or_update_subscriber( - subscriber_entity, subscriber_call_back - ) - if subscriber_entity not in self.entity_subscribers: - self.entity_subscribers[subscriber_entity] = [] - sub_list = self.entity_subscribers.get(subscriber_entity, []) - if subscriber not in sub_list: - sub_list.append(subscriber) - - def clear_all_data(self) -> None: - self.events.clear() - self.entity_subscribers.clear() - - -_node_event_manager = _NodeEventManager() - - class NodeType: Node = 1 Node2D = 2 @@ -1024,15 +927,6 @@ def time_dilation(self, value: float) -> None: def get_total_time_dilation(self) -> float: return crescent_internal.node_get_total_time_dilation(self.entity_id) - def create_event(self, event_name: str) -> None: - _node_event_manager.create_event(self.entity_id, event_name) - - def subscribe_to_event(self, event_name: str, subscriber: "Node", callback_func: Callable[[...], None]) -> None: - _node_event_manager.subscribe_to_event(self.entity_id, event_name, subscriber.entity_id, callback_func) - - def broadcast_event(self, event_name: str, *args) -> None: - _node_event_manager.broadcast_event(self.entity_id, event_name, *args) - def __eq__(self, other: "Node") -> bool: return self.entity_id == other.entity_id @@ -1049,10 +943,10 @@ def new(cls) -> "Node2D": return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Node2D) def set_position(self, value: Vector2) -> None: - crescent_internal.node2d_set_position(self.entity_id, value.x, value.y) + crescent_internal.node2d_set_position(self.entity_id, float(value.x), float(value.y)) def add_to_position(self, value: Vector2) -> None: - crescent_internal.node2d_add_to_position(self.entity_id, value.x, value.y) + crescent_internal.node2d_add_to_position(self.entity_id, float(value.x), float(value.y)) def get_position(self) -> Vector2: px, py = crescent_internal.node2d_get_position(self.entity_id) @@ -1065,7 +959,7 @@ def position(self) -> Vector2: @position.setter def position(self, value: Vector2) -> None: - crescent_internal.node2d_set_position(self.entity_id, value.x, value.y) + crescent_internal.node2d_set_position(self.entity_id, float(value.x), float(value.y)) def get_global_position(self) -> Vector2: px, py = crescent_internal.node2d_get_global_position(self.entity_id) @@ -1077,10 +971,10 @@ def global_position(self) -> Vector2: return Vector2(px, py) def set_scale(self, value: Vector2) -> None: - crescent_internal.node2d_set_scale(self.entity_id, value.x, value.y) + crescent_internal.node2d_set_scale(self.entity_id, float(value.x), float(value.y)) def add_to_scale(self, value: Vector2) -> None: - crescent_internal.node2d_add_to_scale(self.entity_id, value.x, value.y) + crescent_internal.node2d_add_to_scale(self.entity_id, float(value.x), float(value.y)) def get_scale(self) -> Vector2: sx, sy = crescent_internal.node2d_get_scale(self.entity_id) @@ -1093,7 +987,7 @@ def scale(self) -> Vector2: @scale.setter def scale(self, value: Vector2) -> None: - crescent_internal.node2d_set_scale(self.entity_id, value.x, value.y) + crescent_internal.node2d_set_scale(self.entity_id, float(value.x), float(value.y)) def set_rotation(self, value: float) -> None: crescent_internal.node2d_set_rotation(self.entity_id, value) @@ -1586,11 +1480,11 @@ def are_colliders_visible(self) -> bool: class Camera2D: @staticmethod def set_position(position: Vector2) -> None: - crescent_internal.camera2d_set_position(position.x, position.y) + crescent_internal.camera2d_set_position(float(position.x), float(position.y)) @staticmethod def add_to_position(position: Vector2) -> None: - crescent_internal.camera2d_add_to_position(position.x, position.y) + crescent_internal.camera2d_add_to_position(float(position.x), float(position.y)) @staticmethod def get_position() -> Vector2: @@ -1599,11 +1493,11 @@ def get_position() -> Vector2: @staticmethod def set_offset(offset: Vector2) -> None: - crescent_internal.camera2d_set_offset(offset.x, offset.y) + crescent_internal.camera2d_set_offset(float(offset.x), float(offset.y)) @staticmethod def add_to_offset(offset: Vector2) -> None: - crescent_internal.camera2d_add_to_offset(offset.x, offset.y) + crescent_internal.camera2d_add_to_offset(float(offset.x), float(offset.y)) @staticmethod def get_offset() -> Vector2: @@ -1612,11 +1506,11 @@ def get_offset() -> Vector2: @staticmethod def set_zoom(zoom: Vector2) -> None: - crescent_internal.camera2d_set_zoom(zoom.x, zoom.y) + crescent_internal.camera2d_set_zoom(float(zoom.x), float(zoom.y)) @staticmethod def add_to_zoom(zoom: Vector2) -> None: - crescent_internal.camera2d_add_to_zoom(zoom.x, zoom.y) + crescent_internal.camera2d_add_to_zoom(float(zoom.x), float(zoom.y)) @staticmethod def get_zoom() -> Vector2: @@ -1626,7 +1520,7 @@ def get_zoom() -> Vector2: @staticmethod def set_boundary(boundary: Rect2) -> None: crescent_internal.camera2d_set_boundary( - boundary.x, boundary.y, boundary.w, boundary.h + float(boundary.x), float(boundary.y), float(boundary.w), float(boundary.h) ) @staticmethod diff --git a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h index 46e20d157..f98474e2e 100644 --- a/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h +++ b/engine/src/core/scripting/python/pocketpy/cre_pkpy_api_source.h @@ -61,8 +61,8 @@ "\n"\ "class Vector2:\n"\ " def __init__(self, x=0.0, y=0.0):\n"\ -" self.x = float(x)\n"\ -" self.y = float(y)\n"\ +" self.x = x\n"\ +" self.y = y\n"\ "\n"\ " def dot_product(self, other) -> float:\n"\ " return (self.x * other.x) + (self.y * other.y)\n"\ @@ -297,10 +297,10 @@ "\n"\ "class Rect2:\n"\ " def __init__(self, x=0.0, y=0.0, w=0.0, h=0.0):\n"\ -" self.x = float(x)\n"\ -" self.y = float(y)\n"\ -" self.w = float(w)\n"\ -" self.h = float(h)\n"\ +" self.x = x\n"\ +" self.y = y\n"\ +" self.w = w\n"\ +" self.h = h\n"\ "\n"\ " def __eq__(self, other) -> bool:\n"\ " if self.x == other.x and self.y == other.y and self.w == other.w and self.h == other.h:\n"\ @@ -823,103 +823,6 @@ " AXIS_RIGHT_ANALOG_DOWN = 28\n"\ "\n"\ "\n"\ -"class _NodeEventSubscriber:\n"\ -" def __init__(self, entity_id: int, call_back: Callable[[Tuple], None], event_owner_entity_id: int, event_name: str) -> None:\n"\ -" self.entity_id = entity_id\n"\ -" self.call_back = call_back\n"\ -" self.event_owner_entity_id = event_owner_entity_id\n"\ -" self.event_name = event_name\n"\ -"\n"\ -"\n"\ -"class _NodeEvent:\n"\ -" def __init__(self, entity_id: int, name: str, subscribers: List[_NodeEventSubscriber] = None) -> None:\n"\ -" self.entity_id = entity_id\n"\ -" self.name = name\n"\ -" if not subscribers:\n"\ -" subscribers = []\n"\ -" self.subscribers = subscribers\n"\ -"\n"\ -" def add_or_update_subscriber(self, entity_id: int, call_back: Callable[[Tuple], None]) -> _NodeEventSubscriber:\n"\ -" for sub in self.subscribers:\n"\ -" if entity_id == sub.entity_id:\n"\ -" sub.call_back = call_back\n"\ -" return sub\n"\ -" subscriber = _NodeEventSubscriber(entity_id, call_back, self.entity_id, self.name)\n"\ -" self.subscribers.append(subscriber)\n"\ -" return subscriber\n"\ -"\n"\ -" def remove_subscriber(self, subscribe_entity_id: int) -> None:\n"\ -" for sub in self.subscribers:\n"\ -" if subscribe_entity_id == sub.entity_id:\n"\ -" self.subscribers.remove(sub)\n"\ -" break\n"\ -"\n"\ -"\n"\ -"class _NodeEventManager:\n"\ -" \"\"\"\n"\ -" Used to manage events between nodes\n"\ -" \"\"\"\n"\ -" def __init__(self, events: Dict[int, Dict[str, _NodeEvent]] = None, entity_subscribers: Dict[int, List[_NodeEventSubscriber]] = None) -> None:\n"\ -" if not events:\n"\ -" events = {}\n"\ -" if not entity_subscribers:\n"\ -" entity_subscribers = {}\n"\ -" self.events = events\n"\ -" self.entity_subscribers = entity_subscribers\n"\ -"\n"\ -" def create_event(self, entity_id: int, event_name: str) -> _NodeEvent:\n"\ -" if entity_id not in self.events:\n"\ -" self.events[entity_id] = {}\n"\ -" if event_name not in self.events[entity_id]:\n"\ -" self.events[entity_id][event_name] = _NodeEvent(entity_id, event_name)\n"\ -" return self.events[entity_id][event_name]\n"\ -"\n"\ -" def remove_event(self, entity_id: int, event_name: str) -> None:\n"\ -" if self.has_event(entity_id, event_name):\n"\ -" del self.events[entity_id][event_name]\n"\ -"\n"\ -" def remove_entity_and_connections(self, entity_id: int) -> None:\n"\ -" if entity_id in self.events:\n"\ -" del self.events[entity_id]\n"\ -" if entity_id in self.entity_subscribers:\n"\ -" for sub in self.entity_subscribers[entity_id]:\n"\ -" event = self.get_event(sub.event_owner_entity_id, sub.event_name)\n"\ -" if event:\n"\ -" event.remove_subscriber(entity_id)\n"\ -" del self.entity_subscribers[entity_id]\n"\ -"\n"\ -" def get_event(self, entity_id: int, event_name: str) -> Optional[_NodeEvent]:\n"\ -" if self.has_event(entity_id, event_name):\n"\ -" return self.events[entity_id][event_name]\n"\ -" return None\n"\ -"\n"\ -" def has_event(self, entity_id: int, event_name: str) -> bool:\n"\ -" return entity_id in self.events and event_name in self.events[entity_id]\n"\ -"\n"\ -" def broadcast_event(self, entity_id: int, event_name: str, *args) -> None:\n"\ -" event = self.create_event(entity_id, event_name)\n"\ -" for sub in event.subscribers:\n"\ -" sub.call_back(*args)\n"\ -"\n"\ -" def subscribe_to_event(self, owner_entity: int, event_name: str, subscriber_entity: int, subscriber_call_back: Callable[[...], None]) -> None:\n"\ -" event = self.create_event(owner_entity, event_name)\n"\ -" subscriber = event.add_or_update_subscriber(\n"\ -" subscriber_entity, subscriber_call_back\n"\ -" )\n"\ -" if subscriber_entity not in self.entity_subscribers:\n"\ -" self.entity_subscribers[subscriber_entity] = []\n"\ -" sub_list = self.entity_subscribers.get(subscriber_entity, [])\n"\ -" if subscriber not in sub_list:\n"\ -" sub_list.append(subscriber)\n"\ -"\n"\ -" def clear_all_data(self) -> None:\n"\ -" self.events.clear()\n"\ -" self.entity_subscribers.clear()\n"\ -"\n"\ -"\n"\ -"_node_event_manager = _NodeEventManager()\n"\ -"\n"\ -"\n"\ "class NodeType:\n"\ " Node = 1\n"\ " Node2D = 2\n"\ @@ -1027,15 +930,6 @@ " def get_total_time_dilation(self) -> float:\n"\ " return crescent_internal.node_get_total_time_dilation(self.entity_id)\n"\ "\n"\ -" def create_event(self, event_name: str) -> None:\n"\ -" _node_event_manager.create_event(self.entity_id, event_name)\n"\ -"\n"\ -" def subscribe_to_event(self, event_name: str, subscriber: \"Node\", callback_func: Callable[[...], None]) -> None:\n"\ -" _node_event_manager.subscribe_to_event(self.entity_id, event_name, subscriber.entity_id, callback_func)\n"\ -"\n"\ -" def broadcast_event(self, event_name: str, *args) -> None:\n"\ -" _node_event_manager.broadcast_event(self.entity_id, event_name, *args)\n"\ -"\n"\ " def __eq__(self, other: \"Node\") -> bool:\n"\ " return self.entity_id == other.entity_id\n"\ "\n"\ @@ -1052,10 +946,10 @@ " return crescent_internal.node_new(cls.__module__, cls.__name__, NodeType.Node2D)\n"\ "\n"\ " def set_position(self, value: Vector2) -> None:\n"\ -" crescent_internal.node2d_set_position(self.entity_id, value.x, value.y)\n"\ +" crescent_internal.node2d_set_position(self.entity_id, float(value.x), float(value.y))\n"\ "\n"\ " def add_to_position(self, value: Vector2) -> None:\n"\ -" crescent_internal.node2d_add_to_position(self.entity_id, value.x, value.y)\n"\ +" crescent_internal.node2d_add_to_position(self.entity_id, float(value.x), float(value.y))\n"\ "\n"\ " def get_position(self) -> Vector2:\n"\ " px, py = crescent_internal.node2d_get_position(self.entity_id)\n"\ @@ -1068,7 +962,7 @@ "\n"\ " @position.setter\n"\ " def position(self, value: Vector2) -> None:\n"\ -" crescent_internal.node2d_set_position(self.entity_id, value.x, value.y)\n"\ +" crescent_internal.node2d_set_position(self.entity_id, float(value.x), float(value.y))\n"\ "\n"\ " def get_global_position(self) -> Vector2:\n"\ " px, py = crescent_internal.node2d_get_global_position(self.entity_id)\n"\ @@ -1080,10 +974,10 @@ " return Vector2(px, py)\n"\ "\n"\ " def set_scale(self, value: Vector2) -> None:\n"\ -" crescent_internal.node2d_set_scale(self.entity_id, value.x, value.y)\n"\ +" crescent_internal.node2d_set_scale(self.entity_id, float(value.x), float(value.y))\n"\ "\n"\ " def add_to_scale(self, value: Vector2) -> None:\n"\ -" crescent_internal.node2d_add_to_scale(self.entity_id, value.x, value.y)\n"\ +" crescent_internal.node2d_add_to_scale(self.entity_id, float(value.x), float(value.y))\n"\ "\n"\ " def get_scale(self) -> Vector2:\n"\ " sx, sy = crescent_internal.node2d_get_scale(self.entity_id)\n"\ @@ -1096,7 +990,7 @@ "\n"\ " @scale.setter\n"\ " def scale(self, value: Vector2) -> None:\n"\ -" crescent_internal.node2d_set_scale(self.entity_id, value.x, value.y)\n"\ +" crescent_internal.node2d_set_scale(self.entity_id, float(value.x), float(value.y))\n"\ "\n"\ " def set_rotation(self, value: float) -> None:\n"\ " crescent_internal.node2d_set_rotation(self.entity_id, value)\n"\ @@ -1589,11 +1483,11 @@ "class Camera2D:\n"\ " @staticmethod\n"\ " def set_position(position: Vector2) -> None:\n"\ -" crescent_internal.camera2d_set_position(position.x, position.y)\n"\ +" crescent_internal.camera2d_set_position(float(position.x), float(position.y))\n"\ "\n"\ " @staticmethod\n"\ " def add_to_position(position: Vector2) -> None:\n"\ -" crescent_internal.camera2d_add_to_position(position.x, position.y)\n"\ +" crescent_internal.camera2d_add_to_position(float(position.x), float(position.y))\n"\ "\n"\ " @staticmethod\n"\ " def get_position() -> Vector2:\n"\ @@ -1602,11 +1496,11 @@ "\n"\ " @staticmethod\n"\ " def set_offset(offset: Vector2) -> None:\n"\ -" crescent_internal.camera2d_set_offset(offset.x, offset.y)\n"\ +" crescent_internal.camera2d_set_offset(float(offset.x), float(offset.y))\n"\ "\n"\ " @staticmethod\n"\ " def add_to_offset(offset: Vector2) -> None:\n"\ -" crescent_internal.camera2d_add_to_offset(offset.x, offset.y)\n"\ +" crescent_internal.camera2d_add_to_offset(float(offset.x), float(offset.y))\n"\ "\n"\ " @staticmethod\n"\ " def get_offset() -> Vector2:\n"\ @@ -1615,11 +1509,11 @@ "\n"\ " @staticmethod\n"\ " def set_zoom(zoom: Vector2) -> None:\n"\ -" crescent_internal.camera2d_set_zoom(zoom.x, zoom.y)\n"\ +" crescent_internal.camera2d_set_zoom(float(zoom.x), float(zoom.y))\n"\ "\n"\ " @staticmethod\n"\ " def add_to_zoom(zoom: Vector2) -> None:\n"\ -" crescent_internal.camera2d_add_to_zoom(zoom.x, zoom.y)\n"\ +" crescent_internal.camera2d_add_to_zoom(float(zoom.x), float(zoom.y))\n"\ "\n"\ " @staticmethod\n"\ " def get_zoom() -> Vector2:\n"\ @@ -1629,7 +1523,7 @@ " @staticmethod\n"\ " def set_boundary(boundary: Rect2) -> None:\n"\ " crescent_internal.camera2d_set_boundary(\n"\ -" boundary.x, boundary.y, boundary.w, boundary.h\n"\ +" float(boundary.x), float(boundary.y), float(boundary.w), float(boundary.h)\n"\ " )\n"\ "\n"\ " @staticmethod\n"\ diff --git a/engine/test/resources/crescent_api_test.py b/engine/test/resources/crescent_api_test.py index 881a571b4..44ded0494 100644 --- a/engine/test/resources/crescent_api_test.py +++ b/engine/test/resources/crescent_api_test.py @@ -1,7 +1,7 @@ from typing import Optional import crescent_internal -from crescent import Node, SceneTree, Node2D, Vector2, GameProperties, Size2D, Camera2D, Rect2, World +from crescent import Node, SceneTree, Node2D, Vector2, GameProperties, Size2D, Camera2D, Rect2, World, NodeEvent import test_custom_nodes @@ -138,11 +138,17 @@ def test_node_event_manager_callback_one_param_bool(*args) -> None: subscriber_node = Node2D.new() root_node = SceneTree.get_root() root_node.add_child(subscriber_node) - root_node.subscribe_to_event("talk", subscriber_node, test_node_event_manager_callback) - root_node.subscribe_to_event("talk_string", subscriber_node, test_node_event_manager_callback_one_param_string) - root_node.subscribe_to_event("talk_int", subscriber_node, test_node_event_manager_callback_one_param_int) - root_node.subscribe_to_event("talk_float", subscriber_node, test_node_event_manager_callback_one_param_float) - root_node.subscribe_to_event("talk_bool", subscriber_node, test_node_event_manager_callback_one_param_bool) + + root_node.talk = NodeEvent(root_node) + root_node.talk_string = NodeEvent(root_node) + root_node.talk_int = NodeEvent(root_node) + root_node.talk_float = NodeEvent(root_node) + root_node.talk_bool = NodeEvent(root_node) + root_node.talk.subscribe(subscriber_node, test_node_event_manager_callback) + root_node.talk_string.subscribe(subscriber_node, test_node_event_manager_callback_one_param_string) + root_node.talk_int.subscribe(subscriber_node, test_node_event_manager_callback_one_param_int) + root_node.talk_float.subscribe(subscriber_node, test_node_event_manager_callback_one_param_float) + root_node.talk_bool.subscribe(subscriber_node, test_node_event_manager_callback_one_param_bool) # Testing node events test_node = Node2D.new() @@ -170,8 +176,7 @@ def on_has_exited_scene(*args): with TestCase("Scene Tree Tests") as test_case: SceneTree.change_scene("test_scene1.cscn") - # TODO: Fix once scene manager updates are in - # assert SceneTree.get_root() + assert SceneTree.get_root() with TestCase("Game Properties Tests") as test_case: game_props = GameProperties() From 8697da37fcfa94d7177bccb106c7fa3c099f4b87 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Thu, 10 Oct 2024 18:24:25 -0400 Subject: [PATCH 53/54] Updates. --- engine/src/core/scene/scene_manager.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/engine/src/core/scene/scene_manager.c b/engine/src/core/scene/scene_manager.c index bd2a13361..05675d544 100644 --- a/engine/src/core/scene/scene_manager.c +++ b/engine/src/core/scene/scene_manager.c @@ -336,11 +336,7 @@ f32 cre_scene_manager_get_node_full_time_dilation(SkaEntity entity) { } SceneTreeNode* cre_scene_manager_get_entity_tree_node(SkaEntity entity) { - if (!ska_hash_map_has(entityToTreeNodeMap, &entity)) { - const NodeComponent* nodeComponent = (NodeComponent*)ska_ecs_component_manager_get_component(entity, NODE_COMPONENT_INDEX); - SKA_ASSERT_FMT(ska_hash_map_has(entityToTreeNodeMap, &entity), "Doesn't have entity '%d' in scene tree!", entity); - } - // SKA_ASSERT_FMT(ska_hash_map_has(entityToTreeNodeMap, &entity), "Doesn't have entity '%d' in scene tree!", entity); + SKA_ASSERT_FMT(ska_hash_map_has(entityToTreeNodeMap, &entity), "Doesn't have entity '%d' in scene tree!", entity); SceneTreeNode* treeNode = (SceneTreeNode*) *(SceneTreeNode**) ska_hash_map_get(entityToTreeNodeMap, &entity); SKA_ASSERT_FMT(treeNode != NULL, "Failed to get tree node for entity '%d'", entity); return treeNode; From 1f09f6ea70855a833b17b9eb4573bf31973748f6 Mon Sep 17 00:00:00 2001 From: Chukobyte Date: Thu, 10 Oct 2024 18:26:29 -0400 Subject: [PATCH 54/54] Updates. --- engine/test/main.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/engine/test/main.c b/engine/test/main.c index cecc92c21..9f40a558c 100644 --- a/engine/test/main.c +++ b/engine/test/main.c @@ -10,19 +10,17 @@ #include #include -#include "../src/core/node_event.h" -#include "../src/core/ecs/ecs_globals.h" -#include "../src/core/ecs/components/collider2d_component.h" -#include "../src/core/ecs/components/text_label_component.h" -#include "../src/core/ecs/components/tilemap_component.h" -#include "../src/core/ecs/components/transform2d_component.h" -#include "../src/core/ecs/ecs_manager.h" -#include "../src/core/json/json_file_loader.h" -#include "../src/core/scripting/python/pocketpy/cre_pkpy.h" -#include "../src/core/game_properties.h" -#include "../src/core/engine_context.h" -#include "../src/core/scene/scene_manager.h" -#include "../src/core/tilemap/tilemap.h" +#include "core/node_event.h" +#include "core/ecs/ecs_globals.h" +#include "core/ecs/components/collider2d_component.h" +#include "core/ecs/components/text_label_component.h" +#include "core/ecs/components/transform2d_component.h" +#include "core/ecs/ecs_manager.h" +#include "core/json/json_file_loader.h" +#include "core/game_properties.h" +#include "core/engine_context.h" +#include "core/scene/scene_manager.h" +#include "core/tilemap/tilemap.h" #include "core/scripting/python/pocketpy/pkpy_util.h" inline static SkaTexture* create_mock_texture() {