aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author ClarkeCode <33846391+ClarkeCode@users.noreply.github.com>2023-02-13 17:04:22 +0100
committer GitHub <noreply@github.com>2023-02-13 17:04:22 +0100
commit6a0158e625eaffbae0f214620a1d728728934033 (patch)
treec3af6ac753b8b3c177d1e9a41e94017c1c2afbd0
parentc0d8a4ac3723021c95da9674c3bc0eea511ee3a0 (diff)
parenta4f213bf923b79674b8dcef4c35f0f79329ffc80 (diff)
Merge pull request #8 from OpenVic2Project/dev-trailblazing-cpp
Exploration of uniting Godot UI with Stateful C++ Objects via GDExtension
-rw-r--r--extension/src/Simulation.hpp52
-rw-r--r--extension/src/TestSingleton.hpp2
-rw-r--r--extension/src/register_types.cpp51
-rw-r--r--game/src/MainMenu.gd1
-rw-r--r--game/src/SampleGame.gd40
-rw-r--r--game/src/SampleGame.tscn64
6 files changed, 188 insertions, 22 deletions
diff --git a/extension/src/Simulation.hpp b/extension/src/Simulation.hpp
new file mode 100644
index 0000000..e16f34b
--- /dev/null
+++ b/extension/src/Simulation.hpp
@@ -0,0 +1,52 @@
+#pragma once
+
+#include <godot_cpp/classes/object.hpp>
+#include <godot_cpp/core/class_db.hpp>
+#include <godot_cpp/variant/utility_functions.hpp>
+#include <vector>
+
+namespace OpenVic2 {
+ class Simulation : public godot::Object {
+ GDCLASS(Simulation, godot::Object)
+ std::vector<size_t> exampleProvinces;
+
+ //BEGIN BOILERPLATE
+ 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/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;
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 <godot_cpp/classes/engine.hpp>
#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<TestSingleton>();
+ ClassDB::register_class<TestSingleton>();
+ _test_singleton = memnew(TestSingleton);
+ Engine::get_singleton()->register_singleton("TestSingleton", TestSingleton::get_singleton());
+
+ ClassDB::register_class<Simulation>();
+ _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
diff --git a/game/src/MainMenu.gd b/game/src/MainMenu.gd
index a5c6198..de49ec4 100644
--- a/game/src/MainMenu.gd
+++ b/game/src/MainMenu.gd
@@ -10,6 +10,7 @@ func _ready():
func _on_new_game_button_pressed():
print("Start a new game!")
+ get_tree().change_scene_to_file("res://src/SampleGame.tscn")
func _on_continue_button_pressed():
diff --git a/game/src/SampleGame.gd b/game/src/SampleGame.gd
new file mode 100644
index 0000000..0e3a61d
--- /dev/null
+++ b/game/src/SampleGame.gd
@@ -0,0 +1,40 @@
+extends Control
+
+var selectedId = 0
+
+# Called when the node enters the scene tree for the first time.
+func _ready():
+ updateVisibleInfo()
+ pass # Replace with function body.
+
+
+# Called every frame. 'delta' is the elapsed time since the previous frame.
+#func _process(delta):
+# pass
+
+
+func updateVisibleInfo():
+ $CenterContainer/VBoxContainer2/GridContainer/ProvinceNumDisplay.text = str(selectedId)
+ $CenterContainer/VBoxContainer2/GridContainer/ProvinceSizeDisplay.text = str(Simulation.queryProvinceSize(selectedId))
+
+
+func _on_pass_time_button_pressed():
+ Simulation.conductSimulationStep()
+ updateVisibleInfo()
+
+
+func _on_next_prov_button_pressed():
+ selectedId = (selectedId + 1) % 10
+ updateVisibleInfo()
+
+
+func _on_prev_prov_button_pressed():
+ if selectedId == 0:
+ selectedId = 9
+ else:
+ selectedId -= 1
+ updateVisibleInfo()
+
+
+func _on_to_main_menu_pressed():
+ get_tree().change_scene_to_file("res://src/MainMenu.tscn")
diff --git a/game/src/SampleGame.tscn b/game/src/SampleGame.tscn
new file mode 100644
index 0000000..8221857
--- /dev/null
+++ b/game/src/SampleGame.tscn
@@ -0,0 +1,64 @@
+[gd_scene load_steps=2 format=3 uid="uid://bgnupcshe1m7r"]
+
+[ext_resource type="Script" path="res://src/SampleGame.gd" id="1_eklvp"]
+
+[node name="SampleGame" type="Control"]
+layout_mode = 3
+anchors_preset = 15
+anchor_right = 1.0
+anchor_bottom = 1.0
+grow_horizontal = 2
+grow_vertical = 2
+script = ExtResource("1_eklvp")
+
+[node name="CenterContainer" type="CenterContainer" parent="."]
+layout_mode = 0
+offset_right = 1152.0
+offset_bottom = 648.0
+
+[node name="VBoxContainer2" type="VBoxContainer" parent="CenterContainer"]
+layout_mode = 2
+
+[node name="GridContainer" type="GridContainer" parent="CenterContainer/VBoxContainer2"]
+layout_mode = 2
+columns = 2
+
+[node name="ProvenceLabel" type="Label" parent="CenterContainer/VBoxContainer2/GridContainer"]
+layout_mode = 2
+text = "Viewing Province #:"
+horizontal_alignment = 2
+
+[node name="ProvinceNumDisplay" type="Label" parent="CenterContainer/VBoxContainer2/GridContainer"]
+layout_mode = 2
+
+[node name="ProvinceSizeLabel" type="Label" parent="CenterContainer/VBoxContainer2/GridContainer"]
+layout_mode = 2
+text = "Province Size:"
+horizontal_alignment = 2
+
+[node name="ProvinceSizeDisplay" type="Label" parent="CenterContainer/VBoxContainer2/GridContainer"]
+layout_mode = 2
+
+[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer/VBoxContainer2"]
+layout_mode = 2
+
+[node name="PassTimeButton" type="Button" parent="CenterContainer/VBoxContainer2/VBoxContainer"]
+layout_mode = 2
+text = "Pass Time"
+
+[node name="NextProvButton" type="Button" parent="CenterContainer/VBoxContainer2/VBoxContainer"]
+layout_mode = 2
+text = "View Next Province"
+
+[node name="PrevProvButton" type="Button" parent="CenterContainer/VBoxContainer2/VBoxContainer"]
+layout_mode = 2
+text = "View Previous Province"
+
+[node name="ToMainMenu" type="Button" parent="CenterContainer/VBoxContainer2/VBoxContainer"]
+layout_mode = 2
+text = "Exit to Main Menu"
+
+[connection signal="pressed" from="CenterContainer/VBoxContainer2/VBoxContainer/PassTimeButton" to="." method="_on_pass_time_button_pressed"]
+[connection signal="pressed" from="CenterContainer/VBoxContainer2/VBoxContainer/NextProvButton" to="." method="_on_next_prov_button_pressed"]
+[connection signal="pressed" from="CenterContainer/VBoxContainer2/VBoxContainer/PrevProvButton" to="." method="_on_prev_prov_button_pressed"]
+[connection signal="pressed" from="CenterContainer/VBoxContainer2/VBoxContainer/ToMainMenu" to="." method="_on_to_main_menu_pressed"]