Skip to content

Commit

Permalink
Start implementation
Browse files Browse the repository at this point in the history
Start implementing flecs library, included as a static lib via FetchContent
  • Loading branch information
LouChiSoft committed Dec 13, 2024
1 parent 9aebc92 commit 00ebfc6
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 108 deletions.
19 changes: 16 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ cmake_minimum_required(VERSION 3.25)
include(cmake/apple_silicon.cmake) # Handling Apple Silicon specific behaviours
include(cmake/platform.cmake) # Handling outputs based on platform
include(cmake/godot-cpp.cmake) # Handle getting and linking Godot-CPP
include(cmake/gdextension.cmake) # Handling the creation of the .gdextension file
include(cmake/flecs.cmake) # Handle getting and linking flecs
include(cmake/gdextension.cmake) # Handling the creation of the .gdextension file

# Apple Silicon devices need flags set before the project is created.
set_apple_silicon_flags()

# TODO: Update project name
project(gdextension-template LANGUAGES CXX)
project(gdflecs LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

Expand Down Expand Up @@ -47,6 +47,19 @@ project(gdextension-template LANGUAGES CXX)
${EXTENSION_API_FILE} # Provided via the command line
)

#[[
get_and_link_godot_cpp uses FetchContent to download godot-cpp
from github, include it's CMakeList and link it to a target
Variables:
--- TARGET: target name
--- TAG: Tag or Branch to checkout when linking godot-cpp
--- PRECISION: Floating point precision. Valid arguments: single, double
]]
get_and_link_flecs(
${PROJECT_NAME}
v4.0.3
)

#[[
get_and_link_godot_cpp uses FetchContent to download godot-cpp
from github, include it's CMakeList and link it to a target
Expand Down
22 changes: 22 additions & 0 deletions cmake/flecs.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function(get_and_link_flecs TARGET TAG)

include(FetchContent)
FetchContent_Declare(
flecs
GIT_REPOSITORY https://github.com/SanderMertens/flecs
GIT_TAG ${TAG}
GIT_SUBMODULES_RECURSE 1
)
FetchContent_MakeAvailable(flecs)

set_target_properties(flecs PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON
)
# Turn on LTO and export compile commands for VSCode to use
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)

target_link_libraries(${TARGET} PRIVATE flecs::flecs_static)

endfunction()

1 change: 1 addition & 0 deletions cmake/godot-cpp.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function(get_and_link_godot_cpp TARGET TAG PRECISION EXTENSION_FILE)

set(GODOT_ENABLE_HOT_RELOAD ON CACHE INTERNAL "Enable hot reloading support")
set(GODOT_CPP_SYSTEM_HEADERS ON CACHE INTERNAL "Generate system headers")
set(GODOT_CPP_WARNING_AS_ERROR OFF CACHE INTERNAL "Treat warnings as errors")
set(GENERATE_TEMPLATE_GET_NODE ON CACHE INTERNAL "Get template node")
Expand Down
13 changes: 0 additions & 13 deletions demo/node.tscn

This file was deleted.

10 changes: 10 additions & 0 deletions demo/scenes/test.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=3 uid="uid://cmsfs2w7fgkbs"]

[ext_resource type="Script" path="res://scripts/node.gd" id="1_dng8d"]
[ext_resource type="Script" path="res://scripts/world.gd" id="2_n3jn6"]

[node name="Node" type="Node"]
script = ExtResource("1_dng8d")

[node name="FlecsWorld" type="FlecsWorld" parent="."]
script = ExtResource("2_n3jn6")
2 changes: 1 addition & 1 deletion demo/node.gd → demo/scripts/node.gd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extends Node
@tool class_name ExtensionChecker extends Node


# Called when the node enters the scene tree for the first time.
Expand Down
10 changes: 10 additions & 0 deletions demo/scripts/world.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class_name test_script extends FlecsWorld

var field_1: int
var field_2: float

#func _ready():
#for prop in get_property_list():
#if prop["usage"] & PROPERTY_USAGE_SCRIPT_VARIABLE != 0:
#print(prop)
#pass
7 changes: 7 additions & 0 deletions src/component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "component.hpp"

using namespace godot;

auto FlecsComponent::_bind_methods() -> void {

}
21 changes: 21 additions & 0 deletions src/component.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <map>

#include <godot_cpp/classes/object.hpp>
#include <flecs.h>

class World;

class FlecsComponent : public godot::Object {
GDCLASS(FlecsComponent, godot::Object)
friend World;
public:
FlecsComponent() = default;
~FlecsComponent() override = default;

protected:


static auto _bind_methods() -> void;
};
51 changes: 0 additions & 51 deletions src/gdexample.cpp

This file was deleted.

27 changes: 0 additions & 27 deletions src/gdexample.hpp

This file was deleted.

18 changes: 8 additions & 10 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
#include "register_types.hpp"

#include "gdexample.hpp"
#include "world.hpp"
#include "component.hpp"

#include <gdextension_interface.h>
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/godot.hpp>

using namespace godot;

// TODO: Rename 'example' to extension name
void initialize_example_module(ModuleInitializationLevel p_level) {
void initialize_gdflecs_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}


GDREGISTER_CLASS(GDExample);
GDREGISTER_CLASS(FlecsWorld);
GDREGISTER_CLASS(FlecsComponent);
}

// TODO: Rename 'example' to extension name
void terminate_example_module(ModuleInitializationLevel p_level) {
void terminate_gdflecs_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
Expand All @@ -30,9 +29,8 @@ extern "C" {
GDExtensionBool GDE_EXPORT GDExtensionInit(GDExtensionInterfaceGetProcAddress p_get_proc_address, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) {
GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization);

// TODO: Replace function names with updated function names
init_obj.register_initializer(initialize_example_module);
init_obj.register_terminator(terminate_example_module);
init_obj.register_initializer(initialize_gdflecs_module);
init_obj.register_terminator(terminate_gdflecs_module);

init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE);

Expand Down
6 changes: 3 additions & 3 deletions src/register_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

using namespace godot;

// TODO: Rename 'example' to extension name
void initialize_example_module(ModuleInitializationLevel p_level);
void terminate_example_module(ModuleInitializationLevel p_level);

void initialize_gdflecs_module(ModuleInitializationLevel p_level);
void terminate_gdflecs_module(ModuleInitializationLevel p_level);
44 changes: 44 additions & 0 deletions src/world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "world.hpp"

#include <godot_cpp/classes/script.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

using namespace godot;

void FlecsWorld::_ready() {

Ref<Script> script = get_script();
if(script.is_valid() && script->is_class("GDScript")) {


UtilityFunctions::prints("Script name: ", script->get_global_name());
TypedArray<Dictionary> properties = get_property_list();
for(int i = 0; i < properties.size(); i++)
{
Dictionary property = properties[i];
int usage = property["usage"];
if((usage & PROPERTY_USAGE_SCRIPT_VARIABLE) == PROPERTY_USAGE_SCRIPT_VARIABLE)
{
UtilityFunctions::prints("-- Name:", property["name"],"- Type:", property["type"], "- Usage:", property["usage"]);
}
}
}

// For every GD entity
// For every GD component
// Make Flecs entity
// Make Flecs component
}

auto FlecsWorld::component_registered(const String& commponent_name) const -> bool {
return _registered_components.count(commponent_name);
}

auto FlecsWorld::register_new_component() -> void {

}


void FlecsWorld::_bind_methods() {

}
25 changes: 25 additions & 0 deletions src/world.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <map>

#include <godot_cpp/classes/node.hpp>
#include <flecs.h>

class FlecsWorld : public godot::Node {
GDCLASS(FlecsWorld, godot::Node)
public:
FlecsWorld() = default;
~FlecsWorld() override = default;

void _ready() override;

protected:
auto component_registered(const godot::String& commponent_name) const -> bool;
auto register_new_component() -> void;

static auto _bind_methods() -> void;

private:
flecs::world _world;
std::map<godot::String, flecs::entity> _registered_components;
};

0 comments on commit 00ebfc6

Please sign in to comment.