aboutsummaryrefslogtreecommitdiff
path: root/extension
diff options
context:
space:
mode:
author Hop311 <hop3114@gmail.com>2023-04-07 19:37:39 +0200
committer Hop311 <hop3114@gmail.com>2023-04-07 19:37:39 +0200
commit679657138d7e3422fbf9727617153e4a3f2f393b (patch)
tree40348af0bdeeaea8353a38f216448022c00ac46d /extension
parent2364fbc03f0a44bcef0881b38a5cf74fa819fb7f (diff)
Includes cleanup + const map getters
Diffstat (limited to 'extension')
-rw-r--r--extension/src/Checksum.hpp8
-rw-r--r--extension/src/LoadLocalisation.hpp1
-rw-r--r--extension/src/MapSingleton.cpp12
-rw-r--r--extension/src/MapSingleton.hpp3
-rw-r--r--extension/src/Simulation.hpp6
-rw-r--r--extension/src/TestSingleton.hpp1
-rw-r--r--extension/src/openvic2/Map.cpp28
-rw-r--r--extension/src/openvic2/Map.hpp5
-rw-r--r--extension/src/register_types.cpp7
-rw-r--r--extension/src/register_types.h2
10 files changed, 42 insertions, 31 deletions
diff --git a/extension/src/Checksum.hpp b/extension/src/Checksum.hpp
index ebdbd43..717910e 100644
--- a/extension/src/Checksum.hpp
+++ b/extension/src/Checksum.hpp
@@ -1,15 +1,13 @@
#pragma once
-#include <godot_cpp/classes/object.hpp>
#include <godot_cpp/core/class_db.hpp>
-#include <godot_cpp/variant/utility_functions.hpp>
namespace OpenVic2 {
class Checksum : public godot::Object {
GDCLASS(Checksum, godot::Object)
//BEGIN BOILERPLATE
- static Checksum* _checksum;
+ inline static Checksum* _checksum = nullptr;
protected:
static void _bind_methods() {
@@ -33,6 +31,4 @@ namespace OpenVic2 {
return godot::String("1234abcd");
}
};
-
- Checksum* Checksum::_checksum = nullptr;
-} \ No newline at end of file
+}
diff --git a/extension/src/LoadLocalisation.hpp b/extension/src/LoadLocalisation.hpp
index f54a025..49c0313 100644
--- a/extension/src/LoadLocalisation.hpp
+++ b/extension/src/LoadLocalisation.hpp
@@ -1,6 +1,5 @@
#pragma once
-#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/classes/translation.hpp>
namespace OpenVic2 {
diff --git a/extension/src/MapSingleton.cpp b/extension/src/MapSingleton.cpp
index 0f5fe7c..be98e85 100644
--- a/extension/src/MapSingleton.cpp
+++ b/extension/src/MapSingleton.cpp
@@ -178,7 +178,7 @@ Error MapSingleton::load_province_shape_file(String const& file_path) {
continue;
}
}
- const Province* province = map.get_province_by_colour(colour);
+ Province const* province = map.get_province_by_colour(colour);
if (province) {
Province::index_t index = province->get_index();
index_data[idx] = index;
@@ -193,7 +193,7 @@ Error MapSingleton::load_province_shape_file(String const& file_path) {
for (size_t idx = 0; idx < province_checklist.size(); ++idx) {
if (!province_checklist[idx]) {
- Province* province = map.get_province_by_index(idx + 1);
+ Province const* province = map.get_province_by_index(idx + 1);
if (province) UtilityFunctions::push_error("Province missing from shape image: ", province->to_string().c_str());
else UtilityFunctions::push_error("Province missing for index: ", static_cast<int32_t>(idx + 1));
err = FAILED;
@@ -209,7 +209,7 @@ Error MapSingleton::load_province_shape_file(String const& file_path) {
PackedByteArray colour_data_array;
colour_data_array.resize((Province::MAX_INDEX + 1) * 3);
for (size_t idx = 1; idx <= map.get_province_count(); ++idx) {
- const Province* province = map.get_province_by_index(idx);
+ Province const* province = map.get_province_by_index(idx);
if (province) {
const Province::colour_t colour = province->get_colour();
colour_data_array[3 * idx + 0] = (colour >> 16) & 0xFF;
@@ -227,13 +227,13 @@ Error MapSingleton::load_province_shape_file(String const& file_path) {
return err;
}
-String MapSingleton::get_province_identifier_from_pixel_coords(Vector2i const& coords) {
+String MapSingleton::get_province_identifier_from_pixel_coords(Vector2i const& coords) const {
if (province_index_image.is_valid()) {
const PackedByteArray index_data_array = province_index_image->get_data();
- const Province::index_t* index_data = reinterpret_cast<const Province::index_t*>(index_data_array.ptr());
+ Province::index_t const* index_data = reinterpret_cast<Province::index_t const*>(index_data_array.ptr());
const int32_t x_mod_w = UtilityFunctions::posmod(coords.x, width);
const int32_t y_mod_h = UtilityFunctions::posmod(coords.y, height);
- const Province* province = map.get_province_by_index(index_data[x_mod_w + y_mod_h * width]);
+ Province const* province = map.get_province_by_index(index_data[x_mod_w + y_mod_h * width]);
if (province) return province->get_identifier().c_str();
}
return String{};
diff --git a/extension/src/MapSingleton.hpp b/extension/src/MapSingleton.hpp
index 71761cd..bb34201 100644
--- a/extension/src/MapSingleton.hpp
+++ b/extension/src/MapSingleton.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <godot_cpp/classes/image.hpp>
+
#include "openvic2/Map.hpp"
namespace OpenVic2 {
@@ -25,7 +26,7 @@ namespace OpenVic2 {
godot::Error load_province_identifier_file(godot::String const& file_path);
godot::Error load_province_shape_file(godot::String const& file_path);
- godot::String get_province_identifier_from_pixel_coords(godot::Vector2i const& coords);
+ godot::String get_province_identifier_from_pixel_coords(godot::Vector2i const& coords) const;
int32_t get_width() const;
int32_t get_height() const;
godot::Ref<godot::Image> get_province_index_image() const;
diff --git a/extension/src/Simulation.hpp b/extension/src/Simulation.hpp
index 8959310..b84016b 100644
--- a/extension/src/Simulation.hpp
+++ b/extension/src/Simulation.hpp
@@ -1,8 +1,6 @@
#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 {
@@ -11,7 +9,7 @@ namespace OpenVic2 {
std::vector<uint64_t> exampleProvinces;
//BEGIN BOILERPLATE
- static Simulation* _simulation;
+ inline static Simulation* _simulation = nullptr;
protected:
static void _bind_methods() {
@@ -47,6 +45,4 @@ namespace OpenVic2 {
return exampleProvinces[provinceID];
}
};
-
- Simulation* Simulation::_simulation = nullptr;
}
diff --git a/extension/src/TestSingleton.hpp b/extension/src/TestSingleton.hpp
index d140516..1261573 100644
--- a/extension/src/TestSingleton.hpp
+++ b/extension/src/TestSingleton.hpp
@@ -1,7 +1,6 @@
#pragma once
#include <godot_cpp/classes/object.hpp>
-#include <godot_cpp/core/class_db.hpp>
namespace OpenVic2 {
class TestSingleton : public godot::Object
diff --git a/extension/src/openvic2/Map.cpp b/extension/src/openvic2/Map.cpp
index d980b88..6c6275a 100644
--- a/extension/src/openvic2/Map.cpp
+++ b/extension/src/openvic2/Map.cpp
@@ -76,14 +76,34 @@ Province* Map::get_province_by_index(Province::index_t index) {
return index != Province::NULL_INDEX && index <= provinces.size() ? &provinces[index - 1] : nullptr;
}
+Province const* Map::get_province_by_index(Province::index_t index) const {
+ return index != Province::NULL_INDEX && index <= provinces.size() ? &provinces[index - 1] : nullptr;
+}
+
Province* Map::get_province_by_identifier(std::string const& identifier) {
- for (Province& province : provinces)
- if (province.identifier == identifier) return &province;
+ if (!identifier.empty())
+ for (Province& province : provinces)
+ if (province.identifier == identifier) return &province;
+ return nullptr;
+}
+
+Province const* Map::get_province_by_identifier(std::string const& identifier) const {
+ if (!identifier.empty())
+ for (Province const& province : provinces)
+ if (province.identifier == identifier) return &province;
return nullptr;
}
Province* Map::get_province_by_colour(Province::colour_t colour) {
- for (Province& province : provinces)
- if (province.colour == colour) return &province;
+ if (colour != Province::NULL_COLOUR)
+ for (Province& province : provinces)
+ if (province.colour == colour) return &province;
+ return nullptr;
+}
+
+Province const* Map::get_province_by_colour(Province::colour_t colour) const {
+ if (colour != Province::NULL_COLOUR)
+ for (Province const& province : provinces)
+ if (province.colour == colour) return &province;
return nullptr;
}
diff --git a/extension/src/openvic2/Map.hpp b/extension/src/openvic2/Map.hpp
index 365d78b..423b427 100644
--- a/extension/src/openvic2/Map.hpp
+++ b/extension/src/openvic2/Map.hpp
@@ -17,7 +17,7 @@ namespace OpenVic2 {
std::string identifier;
colour_t colour;
- Province(index_t index, std::string const& identifier, colour_t colour);
+ Province(index_t new_index, std::string const& new_identifier, colour_t new_colour);
public:
static std::string colour_to_hex_string(colour_t colour);
@@ -38,8 +38,11 @@ namespace OpenVic2 {
size_t get_province_count() const;
Province* get_province_by_index(Province::index_t index);
+ Province const* get_province_by_index(Province::index_t index) const;
Province* get_province_by_identifier(std::string const& identifier);
+ Province const* get_province_by_identifier(std::string const& identifier) const;
Province* get_province_by_colour(Province::colour_t colour);
+ Province const* get_province_by_colour(Province::colour_t colour) const;
};
}
diff --git a/extension/src/register_types.cpp b/extension/src/register_types.cpp
index 16e59b2..9fd934e 100644
--- a/extension/src/register_types.cpp
+++ b/extension/src/register_types.cpp
@@ -1,8 +1,5 @@
#include "register_types.h"
-#include <gdextension_interface.h>
-#include <godot_cpp/core/class_db.hpp>
-#include <godot_cpp/core/defs.hpp>
-#include <godot_cpp/godot.hpp>
+
#include <godot_cpp/classes/engine.hpp>
#include "TestSingleton.hpp"
@@ -74,7 +71,7 @@ extern "C" {
// Initialization.
- GDExtensionBool GDE_EXPORT openvic2_library_init(const GDExtensionInterface* p_interface, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization* r_initialization) {
+ GDExtensionBool GDE_EXPORT openvic2_library_init(GDExtensionInterface const* 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);
diff --git a/extension/src/register_types.h b/extension/src/register_types.h
index 860359f..1bc96f8 100644
--- a/extension/src/register_types.h
+++ b/extension/src/register_types.h
@@ -1,6 +1,6 @@
#pragma once
-#include <godot_cpp/core/class_db.hpp>
+#include <godot_cpp/godot.hpp>
void initialize_openvic2_types(godot::ModuleInitializationLevel);
void uninitialize_openvic2_types(godot::ModuleInitializationLevel); \ No newline at end of file