Skip to content

Commit

Permalink
add GDExt example
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Apr 23, 2024
1 parent e2b2305 commit 02900b5
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 2 deletions.
File renamed without changes.
30 changes: 30 additions & 0 deletions doc/examples/GDExt/SConstruct
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions doc/examples/GDExt/project/addons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"addons": {
"imgui-godot": {
"url": "../../../../../addons/imgui-godot",
"source": "symlink"
}
}
}
13 changes: 13 additions & 0 deletions doc/examples/GDExt/project/example.gdextension
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions doc/examples/GDExt/project/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions doc/examples/GDExt/project/icon.svg.import
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions doc/examples/GDExt/project/main.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[gd_scene format=3 uid="uid://c08lt0b17jqog"]

[node name="Node2D" type="Node2D"]

[node name="Example" type="Example" parent="."]
24 changes: 24 additions & 0 deletions doc/examples/GDExt/project/project.godot
Original file line number Diff line number Diff line change
@@ -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")
37 changes: 37 additions & 0 deletions doc/examples/GDExt/src/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "example.h"
#include <imgui-godot.h>

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();
}
19 changes: 19 additions & 0 deletions doc/examples/GDExt/src/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <godot_cpp/classes/node.hpp>

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;
};
42 changes: 42 additions & 0 deletions doc/examples/GDExt/src/register_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "register_types.h"
#include "example.h"

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

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();
}
}
7 changes: 7 additions & 0 deletions doc/examples/GDExt/src/register_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#include <godot_cpp/core/class_db.hpp>

using namespace godot;

void initialize_example_module(ModuleInitializationLevel p_level);
void uninitialize_example_module(ModuleInitializationLevel p_level);
2 changes: 1 addition & 1 deletion gdext/include/imconfig-godot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <godot_cpp/variant/utility_functions.hpp>
#define IM_ASSERT(_EXPR) \
Expand Down
2 changes: 1 addition & 1 deletion gdext/include/imgui-godot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 02900b5

Please sign in to comment.