diff --git a/project/addons/discord-rpc-gd/bin/windows/discord_game_sdk_binding_debug.dll b/project/addons/discord-rpc-gd/bin/windows/discord_game_sdk_binding_debug.dll index 613a520..fa186f1 100644 Binary files a/project/addons/discord-rpc-gd/bin/windows/discord_game_sdk_binding_debug.dll and b/project/addons/discord-rpc-gd/bin/windows/discord_game_sdk_binding_debug.dll differ diff --git a/project/main.gd b/project/main.gd new file mode 100644 index 0000000..0302425 --- /dev/null +++ b/project/main.gd @@ -0,0 +1,6 @@ +extends Node + + +# Called when the node enters the scene tree for the first time. +func _ready(): + DiscordSDK.debug() diff --git a/project/main.tscn b/project/main.tscn index 3b5d76e..331bc51 100644 --- a/project/main.tscn +++ b/project/main.tscn @@ -1,10 +1,6 @@ -[gd_scene format=3 uid="uid://dmx2xuigcpvt4"] +[gd_scene load_steps=2 format=3 uid="uid://dmx2xuigcpvt4"] + +[ext_resource type="Script" path="res://main.gd" id="1_kl8ri"] [node name="Node" type="Node"] - -[node name="Button" type="Button" parent="."] -offset_right = 79.0 -offset_bottom = 29.0 -text = "Click me!" - -[node name="DiscordRPC" type="DiscordRPC" parent="."] +script = ExtResource("1_kl8ri") diff --git a/src/main.cpp b/src/main.cpp index d23d2bb..d9df27b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,27 +1,36 @@ #include "main.h" +#include "./discord-game-sdk-cpp/discord.h" #include #include -#include "discord-game-sdk-cpp/discord.h" using namespace godot; +DiscordSDK *DiscordSDK::singleton = nullptr; discord::Core *core{}; -void DiscordRPC::_bind_methods() +void DiscordSDK::_bind_methods() { + ClassDB::bind_method(D_METHOD("debug"), &DiscordSDK::debug); } -DiscordRPC::DiscordRPC() +DiscordSDK *DiscordSDK::get_singleton() { - // initialize any variables here + return singleton; } -DiscordRPC::~DiscordRPC() +DiscordSDK::DiscordSDK() { - // add your cleanup here + ERR_FAIL_COND(singleton != nullptr); + singleton = this; } -void DiscordRPC::_ready() +DiscordSDK::~DiscordSDK() +{ + ERR_FAIL_COND(singleton != this); + singleton = nullptr; +} + +void DiscordSDK::debug() { auto result = discord::Core::Create(1080224638845591692, DiscordCreateFlags_Default, &core); discord::Activity activity{}; @@ -31,9 +40,5 @@ void DiscordRPC::_ready() assets.SetLargeImage("test1"); assets.SetSmallImage("godot"); core->ActivityManager().UpdateActivity(activity, [](discord::Result result) {}); -} - -void DiscordRPC::_process(float delta) -{ ::core->RunCallbacks(); } \ No newline at end of file diff --git a/src/main.h b/src/main.h index fb83d37..e3d0781 100644 --- a/src/main.h +++ b/src/main.h @@ -1,27 +1,27 @@ -#ifndef DISCORDRPC_H -#define DISCORDRPC_H +#ifndef MAIN_H +#define MAIN_H -#include +#include +#include -namespace godot +using namespace godot; + +class DiscordSDK : public Object { - class DiscordRPC : public Node - { - GDCLASS(DiscordRPC, Node) + GDCLASS(DiscordSDK, Object); - private: - float time_passed; + static DiscordSDK *singleton; - protected: - static void _bind_methods(); +protected: + static void _bind_methods(); - public: - DiscordRPC(); - ~DiscordRPC(); +public: + static DiscordSDK *get_singleton(); - void _ready(); - void _process(float delta); - }; -} + DiscordSDK(); + ~DiscordSDK(); + + void debug(); +}; #endif \ No newline at end of file diff --git a/src/register_types.cpp b/src/register_types.cpp index 1116cd8..38b9ac6 100644 --- a/src/register_types.cpp +++ b/src/register_types.cpp @@ -1,37 +1,46 @@ #include "register_types.h" -#include "main.h" - #include -#include #include +#include +#include #include +#include "main.h" using namespace godot; -void initialize_discordrpc_module(ModuleInitializationLevel p_level) { - if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { - return; - } +static DiscordSDK *discordsdk; - ClassDB::register_class(); -} +void gdextension_initialize(ModuleInitializationLevel p_level) +{ + if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) + { + ClassDB::register_class(); -void uninitialize_discordrpc_module(ModuleInitializationLevel p_level) { - if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { - return; + discordsdk = memnew(DiscordSDK); + Engine::get_singleton()->register_singleton("DiscordSDK", DiscordSDK::get_singleton()); } } -extern "C" { -// Initialization. -GDExtensionBool GDE_EXPORT discordrpcgd_library_init(const GDExtensionInterface *p_interface, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) { - godot::GDExtensionBinding::InitObject init_obj(p_interface, p_library, r_initialization); - - init_obj.register_initializer(initialize_discordrpc_module); - init_obj.register_terminator(uninitialize_discordrpc_module); - init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE); - - return init_obj.init(); +void gdextension_terminate(ModuleInitializationLevel p_level) +{ + if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) + { + Engine::get_singleton()->unregister_singleton("DiscordSDK"); + memdelete(discordsdk); + } +} + +extern "C" +{ + GDExtensionBool GDE_EXPORT discordrpcgd_library_init(const GDExtensionInterface *p_interface, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) + { + godot::GDExtensionBinding::InitObject init_obj(p_interface, p_library, r_initialization); + + init_obj.register_initializer(gdextension_initialize); + init_obj.register_terminator(gdextension_terminate); + init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE); + + return init_obj.init(); + } } -} \ No newline at end of file diff --git a/src/register_types.h b/src/register_types.h index e7398d5..61012a7 100644 --- a/src/register_types.h +++ b/src/register_types.h @@ -1,7 +1,7 @@ -#ifndef DISCORDRPC_REGISTER_TYPES_H -#define DISCORDRPC_REGISTER_TYPES_H +#ifndef REGISTER_TYPES_H +#define REGISTER_TYPES_H void initialize_discordrpc_module(); void uninitialize_discordrpc_module(); -#endif // DISCORDRPC_REGISTER_TYPES_H \ No newline at end of file +#endif // REGISTER_TYPES_H \ No newline at end of file