From 02900b5f719bd0ed1a69e186c139ac775713e019 Mon Sep 17 00:00:00 2001 From: Patrick Dawson Date: Tue, 23 Apr 2024 03:22:38 +0200 Subject: [PATCH] add GDExt example --- gdext/.clang-format => .clang-format | 0 doc/examples/GDExt/SConstruct | 30 +++++++++++++ doc/examples/GDExt/project/addons.json | 8 ++++ .../GDExt/project/example.gdextension | 13 ++++++ doc/examples/GDExt/project/icon.svg | 1 + doc/examples/GDExt/project/icon.svg.import | 37 ++++++++++++++++ doc/examples/GDExt/project/main.tscn | 5 +++ doc/examples/GDExt/project/project.godot | 24 +++++++++++ doc/examples/GDExt/src/example.cpp | 37 ++++++++++++++++ doc/examples/GDExt/src/example.h | 19 +++++++++ doc/examples/GDExt/src/register_types.cpp | 42 +++++++++++++++++++ doc/examples/GDExt/src/register_types.h | 7 ++++ gdext/include/imconfig-godot.h | 2 +- gdext/include/imgui-godot.h | 2 +- 14 files changed, 225 insertions(+), 2 deletions(-) rename gdext/.clang-format => .clang-format (100%) create mode 100644 doc/examples/GDExt/SConstruct create mode 100644 doc/examples/GDExt/project/addons.json create mode 100644 doc/examples/GDExt/project/example.gdextension create mode 100644 doc/examples/GDExt/project/icon.svg create mode 100644 doc/examples/GDExt/project/icon.svg.import create mode 100644 doc/examples/GDExt/project/main.tscn create mode 100644 doc/examples/GDExt/project/project.godot create mode 100644 doc/examples/GDExt/src/example.cpp create mode 100644 doc/examples/GDExt/src/example.h create mode 100644 doc/examples/GDExt/src/register_types.cpp create mode 100644 doc/examples/GDExt/src/register_types.h diff --git a/gdext/.clang-format b/.clang-format similarity index 100% rename from gdext/.clang-format rename to .clang-format diff --git a/doc/examples/GDExt/SConstruct b/doc/examples/GDExt/SConstruct new file mode 100644 index 00000000..e309b4fc --- /dev/null +++ b/doc/examples/GDExt/SConstruct @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +IMGUI_PATH = "../../../gdext/imgui" +IMGUI_GODOT_INCLUDE = "../../../addons/imgui-godot/include" + +env = SConscript("../../../gdext/godot-cpp/SConstruct") +env = env.Clone() + +env.Append(CPPPATH=["src/"]) +sources = Glob("src/*.cpp") + +# Dear ImGui +sources += Glob(f"{IMGUI_PATH}/*.cpp") +env.Append(CPPDEFINES=['IMGUI_USER_CONFIG="\\"imconfig-godot.h\\""']) +env.Append(CPPPATH=[IMGUI_PATH, IMGUI_GODOT_INCLUDE]) + +if env["platform"] == "macos": + library = env.SharedLibrary( + "project/bin/libgdexample.{}.{}.framework/libgdexample.{}.{}".format( + env["platform"], env["target"], env["platform"], env["target"] + ), + source=sources, + ) +else: + library = env.SharedLibrary( + "project/bin/libgdexample{}{}".format(env["suffix"], env["SHLIBSUFFIX"]), + source=sources, + ) + +Default(library) diff --git a/doc/examples/GDExt/project/addons.json b/doc/examples/GDExt/project/addons.json new file mode 100644 index 00000000..3506dba4 --- /dev/null +++ b/doc/examples/GDExt/project/addons.json @@ -0,0 +1,8 @@ +{ + "addons": { + "imgui-godot": { + "url": "../../../../../addons/imgui-godot", + "source": "symlink" + } + } +} diff --git a/doc/examples/GDExt/project/example.gdextension b/doc/examples/GDExt/project/example.gdextension new file mode 100644 index 00000000..5fb6ed9d --- /dev/null +++ b/doc/examples/GDExt/project/example.gdextension @@ -0,0 +1,13 @@ +[configuration] + +entry_symbol = "example_library_init" +compatibility_minimum = "4.2" + +[libraries] + +macos.debug = "res://bin/libgdexample.macos.template_debug.framework" +macos.release = "res://bin/libgdexample.macos.template_release.framework" +windows.debug.x86_64 = "res://bin/libgdexample.windows.template_debug.x86_64.dll" +windows.release.x86_64 = "res://bin/libgdexample.windows.template_release.x86_64.dll" +linux.debug.x86_64 = "res://bin/libgdexample.linux.template_debug.x86_64.so" +linux.release.x86_64 = "res://bin/libgdexample.linux.template_release.x86_64.so" diff --git a/doc/examples/GDExt/project/icon.svg b/doc/examples/GDExt/project/icon.svg new file mode 100644 index 00000000..3fe4f4ae --- /dev/null +++ b/doc/examples/GDExt/project/icon.svg @@ -0,0 +1 @@ + diff --git a/doc/examples/GDExt/project/icon.svg.import b/doc/examples/GDExt/project/icon.svg.import new file mode 100644 index 00000000..04c9c91d --- /dev/null +++ b/doc/examples/GDExt/project/icon.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://cr045l23wjmqc" +path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.svg" +dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/doc/examples/GDExt/project/main.tscn b/doc/examples/GDExt/project/main.tscn new file mode 100644 index 00000000..f72df5f1 --- /dev/null +++ b/doc/examples/GDExt/project/main.tscn @@ -0,0 +1,5 @@ +[gd_scene format=3 uid="uid://c08lt0b17jqog"] + +[node name="Node2D" type="Node2D"] + +[node name="Example" type="Example" parent="."] diff --git a/doc/examples/GDExt/project/project.godot b/doc/examples/GDExt/project/project.godot new file mode 100644 index 00000000..d8f2b52a --- /dev/null +++ b/doc/examples/GDExt/project/project.godot @@ -0,0 +1,24 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=5 + +[application] + +config/name="GDExt" +run/main_scene="res://main.tscn" +config/features=PackedStringArray("4.2", "Forward Plus") +config/icon="res://icon.svg" + +[autoload] + +ImGuiRoot="*res://addons/imgui-godot/data/ImGuiRoot.tscn" + +[editor_plugins] + +enabled=PackedStringArray("res://addons/imgui-godot/plugin.cfg") diff --git a/doc/examples/GDExt/src/example.cpp b/doc/examples/GDExt/src/example.cpp new file mode 100644 index 00000000..05cf2e94 --- /dev/null +++ b/doc/examples/GDExt/src/example.cpp @@ -0,0 +1,37 @@ +#include "example.h" +#include + +using namespace godot; + +void Example::_bind_methods() +{ +} + +Example::Example() +{ +} + +Example::~Example() +{ +} + +void Example::_ready() +{ + ImGui::Godot::SyncImGuiPtrs(); +} + +void Example::_process(double delta) +{ +#ifdef DEBUG_ENABLED + if (Engine::get_singleton()->is_editor_hint()) + return; +#endif + + static int x = 0; + + ImGui::SetNextWindowSize({200, 200}, ImGuiCond_Once); + ImGui::Begin("GDExtension Example"); + ImGui::DragInt("x", &x); + ImGui::Text("x = %d", x); + ImGui::End(); +} diff --git a/doc/examples/GDExt/src/example.h b/doc/examples/GDExt/src/example.h new file mode 100644 index 00000000..560e70fd --- /dev/null +++ b/doc/examples/GDExt/src/example.h @@ -0,0 +1,19 @@ +#pragma once +#include + +using namespace godot; + +class Example : public Node +{ + GDCLASS(Example, Node); + +protected: + static void _bind_methods(); + +public: + Example(); + ~Example(); + + void _ready() override; + void _process(double delta) override; +}; diff --git a/doc/examples/GDExt/src/register_types.cpp b/doc/examples/GDExt/src/register_types.cpp new file mode 100644 index 00000000..24f9ad8b --- /dev/null +++ b/doc/examples/GDExt/src/register_types.cpp @@ -0,0 +1,42 @@ +#include "register_types.h" +#include "example.h" + +#include +#include +#include +#include + +using namespace godot; + +void initialize_example_module(ModuleInitializationLevel p_level) +{ + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) + { + return; + } + + GDREGISTER_CLASS(Example); +} + +void uninitialize_example_module(ModuleInitializationLevel p_level) +{ + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) + { + return; + } +} + +extern "C" { +GDExtensionBool GDE_EXPORT example_library_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, + GDExtensionClassLibraryPtr p_library, + GDExtensionInitialization* r_initialization) +{ + godot::GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization); + + init_obj.register_initializer(initialize_example_module); + init_obj.register_terminator(uninitialize_example_module); + init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE); + + return init_obj.init(); +} +} diff --git a/doc/examples/GDExt/src/register_types.h b/doc/examples/GDExt/src/register_types.h new file mode 100644 index 00000000..720dcf9b --- /dev/null +++ b/doc/examples/GDExt/src/register_types.h @@ -0,0 +1,7 @@ +#pragma once +#include + +using namespace godot; + +void initialize_example_module(ModuleInitializationLevel p_level); +void uninitialize_example_module(ModuleInitializationLevel p_level); diff --git a/gdext/include/imconfig-godot.h b/gdext/include/imconfig-godot.h index 8c184d04..442129fb 100644 --- a/gdext/include/imconfig-godot.h +++ b/gdext/include/imconfig-godot.h @@ -13,7 +13,7 @@ using godot::Vector2; using godot::Vector2i; using godot::Vector4; -#ifdef DEBUG_ENABLED +#if defined(DEBUG_ENABLED) && defined(IGN_EXPORT) #ifndef IM_ASSERT #include #define IM_ASSERT(_EXPR) \ diff --git a/gdext/include/imgui-godot.h b/gdext/include/imgui-godot.h index 77202292..7329cd83 100644 --- a/gdext/include/imgui-godot.h +++ b/gdext/include/imgui-godot.h @@ -115,7 +115,7 @@ inline void SetVisible(bool vis) inline bool ToolInit() { - ERR_FAIL_COND(!detail::GET_IMGUIGD()); + ERR_FAIL_COND_V(!detail::GET_IMGUIGD(), false); static const StringName sn("ToolInit"); return detail::ImGuiGD->call(sn); }