diff options
author | Hop311 <Hop3114@gmail.com> | 2023-04-29 00:48:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-29 00:48:13 +0200 |
commit | 5195a460330af234391dfdc892847d74d0d29393 (patch) | |
tree | 588f505bd94991b3a6c7a95b18b02e712df36cc7 /extension/src/GameSingleton.cpp | |
parent | 1b35c3a4434873b98f8e3aa7770f0edd37ec053c (diff) | |
parent | 05937359e8c53ee76dc3ce537dd70083a016f766 (diff) |
Merge branch 'main' into goods
Diffstat (limited to 'extension/src/GameSingleton.cpp')
-rw-r--r-- | extension/src/GameSingleton.cpp | 93 |
1 files changed, 58 insertions, 35 deletions
diff --git a/extension/src/GameSingleton.cpp b/extension/src/GameSingleton.cpp index 41c1b91..64f6fc1 100644 --- a/extension/src/GameSingleton.cpp +++ b/extension/src/GameSingleton.cpp @@ -25,8 +25,10 @@ void GameSingleton::_bind_methods() { ClassDB::bind_method(D_METHOD("get_province_info_from_index", "index"), &GameSingleton::get_province_info_from_index); ClassDB::bind_method(D_METHOD("get_width"), &GameSingleton::get_width); ClassDB::bind_method(D_METHOD("get_height"), &GameSingleton::get_height); - ClassDB::bind_method(D_METHOD("get_province_index_images"), &GameSingleton::get_province_index_images); - ClassDB::bind_method(D_METHOD("get_province_colour_image"), &GameSingleton::get_province_colour_image); + ClassDB::bind_method(D_METHOD("get_aspect_ratio"), &GameSingleton::get_aspect_ratio); + ClassDB::bind_method(D_METHOD("get_province_shape_image_subdivisions"), &GameSingleton::get_province_shape_image_subdivisions); + ClassDB::bind_method(D_METHOD("get_province_shape_texture"), &GameSingleton::get_province_shape_texture); + ClassDB::bind_method(D_METHOD("get_province_colour_texture"), &GameSingleton::get_province_colour_texture); ClassDB::bind_method(D_METHOD("update_colour_image"), &GameSingleton::update_colour_image); ClassDB::bind_method(D_METHOD("get_mapmode_count"), &GameSingleton::get_mapmode_count); @@ -234,7 +236,7 @@ Error GameSingleton::load_region_file(String const& file_path) { } Error GameSingleton::load_province_shape_file(String const& file_path) { - if (province_index_image[0].is_valid()) { + if (province_shape_texture.is_valid()) { UtilityFunctions::push_error("Province shape file has already been loaded, cannot load: ", file_path); return FAILED; } @@ -245,16 +247,15 @@ Error GameSingleton::load_province_shape_file(String const& file_path) { UtilityFunctions::push_error("Failed to load province shape file: ", file_path); return err; } - const int32_t width = province_shape_image->get_width(); - const int32_t height = province_shape_image->get_height(); - if (width < 1 || height < 1) { - UtilityFunctions::push_error("Invalid dimensions (", width, "x", height, ") for province shape file: ", file_path); - err = FAILED; - } - if (width % image_width_divide != 0) { - UtilityFunctions::push_error("Invalid width ", width, " (must be divisible by ", image_width_divide, ") for province shape file: ", file_path); + const Vector2i image_dims = province_shape_image->get_size(); + if (image_dims.x < 1 || image_dims.y < 1) { + UtilityFunctions::push_error("Invalid dimensions (", image_dims.x, "x", image_dims.y, ") for province shape file: ", file_path); err = FAILED; } + static constexpr int32_t GPU_DIM_LIMIT = 0x3FFF; + // For each dimension of the image, this finds the small number of equal subdivisions required get the individual texture dims under GPU_DIM_LIMIT + for (int i = 0; i < 2; ++i) for (image_subdivisions[i] = 1; + image_dims[i] / image_subdivisions[i] > GPU_DIM_LIMIT || image_dims[i] % image_subdivisions[i] != 0; ++image_subdivisions[i]); static constexpr Image::Format expected_format = Image::FORMAT_RGB8; const Image::Format format = province_shape_image->get_format(); if (format != expected_format) { @@ -262,24 +263,37 @@ Error GameSingleton::load_province_shape_file(String const& file_path) { err = FAILED; } if (err != OK) return err; - err = ERR(game_manager.map.generate_province_index_image(width, height, province_shape_image->get_data().ptr())); - - std::vector<index_t> const& province_index_data = game_manager.map.get_province_index_image(); - const int32_t divided_width = width / image_width_divide; - for (int32_t i = 0; i < image_width_divide; ++i) { - PackedByteArray index_data_array; - index_data_array.resize(divided_width * height * sizeof(index_t)); - for (int32_t y = 0; y < height; ++y) - memcpy(index_data_array.ptrw() + y * divided_width * sizeof(index_t), - province_index_data.data() + y * width + i * divided_width, - divided_width * sizeof(index_t)); - province_index_image[i] = Image::create_from_data(divided_width, height, false, Image::FORMAT_RG8, index_data_array); - if (province_index_image[i].is_null()) { - UtilityFunctions::push_error("Failed to create province ID image #", i); - err = FAILED; + err = ERR(game_manager.map.generate_province_shape_image(image_dims.x, image_dims.y, province_shape_image->get_data().ptr())); + + std::vector<Map::shape_pixel_t> const& province_shape_data = game_manager.map.get_province_shape_image(); + const Vector2i divided_dims = image_dims / image_subdivisions; + Array province_shape_images; + province_shape_images.resize(image_subdivisions.x * image_subdivisions.y); + for (int32_t v = 0; v < image_subdivisions.y; ++v) { + for (int32_t u = 0; u < image_subdivisions.x; ++u) { + PackedByteArray index_data_array; + index_data_array.resize(divided_dims.x * divided_dims.y * sizeof(Map::shape_pixel_t)); + + for (int32_t y = 0; y < divided_dims.y; ++y) + memcpy(index_data_array.ptrw() + y * divided_dims.x * sizeof(Map::shape_pixel_t), + province_shape_data.data() + (v * divided_dims.y + y) * image_dims.x + u * divided_dims.x, + divided_dims.x * sizeof(Map::shape_pixel_t)); + + const Ref<Image> province_index_subimage = Image::create_from_data(divided_dims.x, divided_dims.y, false, Image::FORMAT_RGB8, index_data_array); + if (province_index_subimage.is_null()) { + UtilityFunctions::push_error("Failed to create province index image (", u, ", ", v, ")"); + err = FAILED; + } + province_shape_images[u + v * image_subdivisions.x] = province_index_subimage; } } + province_shape_texture.instantiate(); + if (province_shape_texture->create_from_images(province_shape_images) != OK) { + UtilityFunctions::push_error(""); + err = FAILED; + } + if (update_colour_image() != OK) err = FAILED; return err; @@ -370,20 +384,25 @@ int32_t GameSingleton::get_height() const { return game_manager.map.get_height(); } -Array GameSingleton::get_province_index_images() const { - Array ret; - for (int i = 0; i < image_width_divide; ++i) - ret.append(province_index_image[i]); - return ret; +float GameSingleton::get_aspect_ratio() const { + return static_cast<float>(get_width()) / static_cast<float>(get_height()); +} + +Vector2i GameSingleton::get_province_shape_image_subdivisions() const { + return image_subdivisions; +} + +Ref<Texture> GameSingleton::get_province_shape_texture() const { + return province_shape_texture; } -Ref<Image> GameSingleton::get_province_colour_image() const { - return province_colour_image; +Ref<Texture> GameSingleton::get_province_colour_texture() const { + return province_colour_texture; } Error GameSingleton::update_colour_image() { static PackedByteArray colour_data_array; - static constexpr int64_t colour_data_array_size = (MAX_INDEX + 1) * 4; + static constexpr int64_t colour_data_array_size = (MAX_INDEX + 1) * Map::MAPMODE_COLOUR_SIZE; colour_data_array.resize(colour_data_array_size); Error err = OK; @@ -394,11 +413,15 @@ Error GameSingleton::update_colour_image() { if (province_colour_image.is_null()) province_colour_image.instantiate(); province_colour_image->set_data(PROVINCE_INDEX_SQRT, PROVINCE_INDEX_SQRT, - false, Image::FORMAT_RGBA8, colour_data_array); + false, Image::FORMAT_RGB8, colour_data_array); if (province_colour_image.is_null()) { UtilityFunctions::push_error("Failed to update province colour image"); return FAILED; } + if (province_colour_texture.is_null()) + province_colour_texture = ImageTexture::create_from_image(province_colour_image); + else + province_colour_texture->update(province_colour_image); return err; } |