Skip to content

Commit

Permalink
Updating to pocketpy 2.0 (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chukobyte authored Oct 10, 2024
1 parent b5fc5b8 commit c563ccc
Show file tree
Hide file tree
Showing 44 changed files with 3,579 additions and 3,895 deletions.
5 changes: 3 additions & 2 deletions Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand All @@ -20,9 +20,10 @@ 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)
target_include_directories(pocketpy PUBLIC ${pocketpy_content_SOURCE_DIR} ${pocketpy_content_SOURCE_DIR}/include)
endif ()

# https://github.com/DaveGamble/cJSON
Expand Down
230 changes: 78 additions & 152 deletions crescent_py_api/pocketpy/crescent.py

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions crescent_py_api/pocketpy/crescent_internal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple, Optional
from typing import Tuple, Optional, List


# Mock class for engine's internal c python module
Expand All @@ -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"]:
Expand Down Expand Up @@ -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


Expand All @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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 ()


Expand Down
15 changes: 0 additions & 15 deletions crescent_py_api/pocketpy/crescent_internal_py.py

This file was deleted.

4 changes: 0 additions & 4 deletions crescent_py_api/pocketpy/utils/pkpy_header_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
],
)
1 change: 1 addition & 0 deletions editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions engine/src/core/ecs/components/script_component.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 4 additions & 10 deletions engine/src/core/ecs/components/script_component.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
#pragma once

#include "core/scripting/script_context.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef enum ScriptContextType {
ScriptContextType_NONE = -1, // INVALID
ScriptContextType_PYTHON = 0,
ScriptContextType_NATIVE = 1, // C/C++

ScriptContextType_TOTAL_TYPES = 2
} ScriptContextType;

// 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);

Expand Down
6 changes: 3 additions & 3 deletions engine/src/core/ecs/ecs_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
53 changes: 30 additions & 23 deletions engine/src/core/ecs/systems/script_ec_system.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "script_ec_system.h"

#include <seika/assert.h>
#include <seika/logger.h>
#include <seika/data_structures/hash_map.h>
#include <seika/ecs/ecs.h>
#include <seika/assert.h>

#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 "../../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);
Expand All @@ -24,7 +24,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() {
Expand All @@ -44,29 +44,36 @@ 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;
}

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);
Expand All @@ -80,7 +87,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);
}

Expand Down Expand Up @@ -127,5 +134,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);
}
2 changes: 1 addition & 1 deletion engine/src/core/json/json_file_loader_scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
2 changes: 1 addition & 1 deletion engine/src/core/scene/scene_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,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;
Expand Down
2 changes: 1 addition & 1 deletion engine/src/core/scene/scene_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading

0 comments on commit c563ccc

Please sign in to comment.