From e7ee0ddbefa8f6696aa7b5a001b3a04eb046ba0d Mon Sep 17 00:00:00 2001 From: Robert Clarke Date: Sun, 12 Feb 2023 03:42:25 -0500 Subject: Just changed bracketing style --- extension/src/TestSingleton.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'extension') diff --git a/extension/src/TestSingleton.cpp b/extension/src/TestSingleton.cpp index 0855a30..2da8000 100644 --- a/extension/src/TestSingleton.cpp +++ b/extension/src/TestSingleton.cpp @@ -6,31 +6,26 @@ using namespace godot; using namespace OpenVic2; -TestSingleton *TestSingleton::singleton = nullptr; +TestSingleton* TestSingleton::singleton = nullptr; -void TestSingleton::_bind_methods() -{ +void TestSingleton::_bind_methods() { ClassDB::bind_method(D_METHOD("hello_singleton"), &TestSingleton::hello_singleton); } -TestSingleton *TestSingleton::get_singleton() -{ +TestSingleton *TestSingleton::get_singleton() { return singleton; } -TestSingleton::TestSingleton() -{ +TestSingleton::TestSingleton() { ERR_FAIL_COND(singleton != nullptr); singleton = this; } -TestSingleton::~TestSingleton() -{ +TestSingleton::~TestSingleton() { ERR_FAIL_COND(singleton != this); singleton = nullptr; } -void TestSingleton::hello_singleton() -{ +void TestSingleton::hello_singleton() { UtilityFunctions::print("Hello GDExtension Singleton!"); } \ No newline at end of file -- cgit v1.2.3-56-ga3b1 From d45323cadee9044d8536f7eb9827a43b36e4bdff Mon Sep 17 00:00:00 2001 From: Robert Clarke Date: Sun, 12 Feb 2023 03:44:39 -0500 Subject: Everything necessary to register a C++ class with internal state for access within Godot --- extension/src/Simulation.hpp | 52 ++++++++++++++++++++++++++++++++++++++++ extension/src/register_types.cpp | 51 +++++++++++++++++++++++---------------- 2 files changed, 82 insertions(+), 21 deletions(-) create mode 100644 extension/src/Simulation.hpp (limited to 'extension') diff --git a/extension/src/Simulation.hpp b/extension/src/Simulation.hpp new file mode 100644 index 0000000..6ade846 --- /dev/null +++ b/extension/src/Simulation.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include +#include +#include +#include + +namespace OpenVic2 { + class Simulation : public godot::Object { + std::vector exampleProvinces; + + //BEGIN BOILERPLATE + GDCLASS(Simulation, godot::Object); + static Simulation* _simulation; + + protected: + static void _bind_methods() { + godot::ClassDB::bind_method(godot::D_METHOD("conductSimulationStep"), &Simulation::conductSimulationStep); + godot::ClassDB::bind_method(godot::D_METHOD("queryProvinceSize"), &Simulation::queryProvinceSize); + } + + public: + inline static Simulation* get_singleton() { return _simulation; } + + inline Simulation() { + ERR_FAIL_COND(_simulation != nullptr); + _simulation = this; + + exampleProvinces.resize(10, 1); + } + inline ~Simulation() { + ERR_FAIL_COND(_simulation != this); + _simulation = nullptr; + } + //END BOILERPLATE + + inline void conductSimulationStep() { + for (size_t x = 0; x < exampleProvinces.size(); x++) { + exampleProvinces[x] += (x + 1); + } + } + + inline size_t queryProvinceSize(size_t provinceID) { + if (provinceID >= exampleProvinces.size()) { + return 0; + } + return exampleProvinces[provinceID]; + } + }; + + Simulation* Simulation::_simulation = nullptr; +} \ No newline at end of file diff --git a/extension/src/register_types.cpp b/extension/src/register_types.cpp index 775e6c3..4b049ce 100644 --- a/extension/src/register_types.cpp +++ b/extension/src/register_types.cpp @@ -6,46 +6,55 @@ #include #include "TestSingleton.hpp" +#include "Simulation.hpp" using namespace godot; using namespace OpenVic2; -static TestSingleton *_test_singleton; +static TestSingleton* _test_singleton; +static Simulation* _simulation; void initialize_openvic2_types(ModuleInitializationLevel p_level) { - if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { - return; - } + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } - ClassDB::register_class(); + ClassDB::register_class(); + _test_singleton = memnew(TestSingleton); + Engine::get_singleton()->register_singleton("TestSingleton", TestSingleton::get_singleton()); + + ClassDB::register_class(); + _simulation = memnew(Simulation); + Engine::get_singleton()->register_singleton("Simulation", Simulation::get_singleton()); - _test_singleton = memnew(TestSingleton); - Engine::get_singleton()->register_singleton("TestSingleton", TestSingleton::get_singleton()); } void uninitialize_openvic2_types(ModuleInitializationLevel p_level) { - if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { - return; - } + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } + + Engine::get_singleton()->unregister_singleton("TestSingleton"); + memdelete(_test_singleton); - Engine::get_singleton()->unregister_singleton("TestSingleton"); - memdelete(_test_singleton); + Engine::get_singleton()->unregister_singleton("Simulation"); + memdelete(_test_singleton); } extern "C" { - // Initialization. + // Initialization. - GDExtensionBool GDE_EXPORT openvic2_library_init(const GDExtensionInterface *p_interface, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) - { - GDExtensionBinding::InitObject init_obj(p_interface, p_library, r_initialization); + GDExtensionBool GDE_EXPORT openvic2_library_init(const GDExtensionInterface *p_interface, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) + { + GDExtensionBinding::InitObject init_obj(p_interface, p_library, r_initialization); - init_obj.register_initializer(initialize_openvic2_types); - init_obj.register_terminator(uninitialize_openvic2_types); - init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE); + init_obj.register_initializer(initialize_openvic2_types); + init_obj.register_terminator(uninitialize_openvic2_types); + init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE); - return init_obj.init(); - } + return init_obj.init(); + } } \ No newline at end of file -- cgit v1.2.3-56-ga3b1 From ea33d7e2d96bcdbf5135f4ab82333bc8c15cdfba Mon Sep 17 00:00:00 2001 From: Robert Clarke Date: Sun, 12 Feb 2023 18:55:18 -0500 Subject: Reversion of commit e7ee0dd --- extension/src/TestSingleton.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'extension') diff --git a/extension/src/TestSingleton.cpp b/extension/src/TestSingleton.cpp index 2da8000..0855a30 100644 --- a/extension/src/TestSingleton.cpp +++ b/extension/src/TestSingleton.cpp @@ -6,26 +6,31 @@ using namespace godot; using namespace OpenVic2; -TestSingleton* TestSingleton::singleton = nullptr; +TestSingleton *TestSingleton::singleton = nullptr; -void TestSingleton::_bind_methods() { +void TestSingleton::_bind_methods() +{ ClassDB::bind_method(D_METHOD("hello_singleton"), &TestSingleton::hello_singleton); } -TestSingleton *TestSingleton::get_singleton() { +TestSingleton *TestSingleton::get_singleton() +{ return singleton; } -TestSingleton::TestSingleton() { +TestSingleton::TestSingleton() +{ ERR_FAIL_COND(singleton != nullptr); singleton = this; } -TestSingleton::~TestSingleton() { +TestSingleton::~TestSingleton() +{ ERR_FAIL_COND(singleton != this); singleton = nullptr; } -void TestSingleton::hello_singleton() { +void TestSingleton::hello_singleton() +{ UtilityFunctions::print("Hello GDExtension Singleton!"); } \ No newline at end of file -- cgit v1.2.3-56-ga3b1 From 9f7ad25bdb8a821b24042bfd62e5a694451789f6 Mon Sep 17 00:00:00 2001 From: Robert Clarke Date: Sun, 12 Feb 2023 21:49:58 -0500 Subject: Moved positioning of GDCLASS macro --- extension/src/Simulation.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extension') diff --git a/extension/src/Simulation.hpp b/extension/src/Simulation.hpp index 6ade846..554f257 100644 --- a/extension/src/Simulation.hpp +++ b/extension/src/Simulation.hpp @@ -7,10 +7,10 @@ namespace OpenVic2 { class Simulation : public godot::Object { + GDCLASS(Simulation, godot::Object); std::vector exampleProvinces; //BEGIN BOILERPLATE - GDCLASS(Simulation, godot::Object); static Simulation* _simulation; protected: -- cgit v1.2.3-56-ga3b1 From a4f213bf923b79674b8dcef4c35f0f79329ffc80 Mon Sep 17 00:00:00 2001 From: Robert Clarke Date: Sun, 12 Feb 2023 22:05:08 -0500 Subject: Removing semicolons that would cause issues on MSVC --- extension/src/Simulation.hpp | 2 +- extension/src/TestSingleton.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'extension') diff --git a/extension/src/Simulation.hpp b/extension/src/Simulation.hpp index 554f257..e16f34b 100644 --- a/extension/src/Simulation.hpp +++ b/extension/src/Simulation.hpp @@ -7,7 +7,7 @@ namespace OpenVic2 { class Simulation : public godot::Object { - GDCLASS(Simulation, godot::Object); + GDCLASS(Simulation, godot::Object) std::vector exampleProvinces; //BEGIN BOILERPLATE diff --git a/extension/src/TestSingleton.hpp b/extension/src/TestSingleton.hpp index 0a591ac..de27589 100644 --- a/extension/src/TestSingleton.hpp +++ b/extension/src/TestSingleton.hpp @@ -6,7 +6,7 @@ namespace OpenVic2 { class TestSingleton : public godot::Object { - GDCLASS(TestSingleton, godot::Object); + GDCLASS(TestSingleton, godot::Object) static TestSingleton *singleton; -- cgit v1.2.3-56-ga3b1