diff options
author | Hop311 <hop3114@gmail.com> | 2023-04-29 21:00:44 +0200 |
---|---|---|
committer | Hop311 <hop3114@gmail.com> | 2023-04-29 21:00:44 +0200 |
commit | 7e2700514652212e50a006ad525e7c7ca8e7840c (patch) | |
tree | 681431640bca4f25f5e383cc7960e5ff228b47cc /extension/src/openvic2 | |
parent | 1f8545facd248bbf4ea0cd551fea5f7d9b0efdce (diff) |
Terrain textures + province colour alpha channel
Diffstat (limited to 'extension/src/openvic2')
-rw-r--r-- | extension/src/openvic2/Date.cpp | 4 | ||||
-rw-r--r-- | extension/src/openvic2/GameAdvancementHook.cpp | 2 | ||||
-rw-r--r-- | extension/src/openvic2/GameManager.cpp | 4 | ||||
-rw-r--r-- | extension/src/openvic2/GameManager.hpp | 5 | ||||
-rw-r--r-- | extension/src/openvic2/Good.hpp | 1 | ||||
-rw-r--r-- | extension/src/openvic2/Logger.cpp | 2 | ||||
-rw-r--r-- | extension/src/openvic2/Types.cpp | 7 | ||||
-rw-r--r-- | extension/src/openvic2/Types.hpp | 19 | ||||
-rw-r--r-- | extension/src/openvic2/map/Building.cpp | 6 | ||||
-rw-r--r-- | extension/src/openvic2/map/Building.hpp | 4 | ||||
-rw-r--r-- | extension/src/openvic2/map/Map.cpp | 49 | ||||
-rw-r--r-- | extension/src/openvic2/map/Map.hpp | 13 | ||||
-rw-r--r-- | extension/src/openvic2/map/Province.cpp | 2 | ||||
-rw-r--r-- | extension/src/openvic2/map/Province.hpp | 3 | ||||
-rw-r--r-- | extension/src/openvic2/map/Region.cpp | 3 | ||||
-rw-r--r-- | extension/src/openvic2/map/Region.hpp | 3 |
16 files changed, 75 insertions, 52 deletions
diff --git a/extension/src/openvic2/Date.cpp b/extension/src/openvic2/Date.cpp index bb891fd..ed800d5 100644 --- a/extension/src/openvic2/Date.cpp +++ b/extension/src/openvic2/Date.cpp @@ -1,9 +1,9 @@ -#include "openvic2/Date.hpp" +#include "Date.hpp" #include <cctype> #include <algorithm> -#include "openvic2/Logger.hpp" +#include "Logger.hpp" using namespace OpenVic2; diff --git a/extension/src/openvic2/GameAdvancementHook.cpp b/extension/src/openvic2/GameAdvancementHook.cpp index 4b9bc25..d90b7b1 100644 --- a/extension/src/openvic2/GameAdvancementHook.cpp +++ b/extension/src/openvic2/GameAdvancementHook.cpp @@ -1,4 +1,4 @@ -#include "openvic2/GameAdvancementHook.hpp" +#include "GameAdvancementHook.hpp" using namespace OpenVic2; diff --git a/extension/src/openvic2/GameManager.cpp b/extension/src/openvic2/GameManager.cpp index ca6e8ab..58e40bb 100644 --- a/extension/src/openvic2/GameManager.cpp +++ b/extension/src/openvic2/GameManager.cpp @@ -1,6 +1,6 @@ -#include "openvic2/GameManager.hpp" +#include "GameManager.hpp" -#include "openvic2/Logger.hpp" +#include "Logger.hpp" using namespace OpenVic2; diff --git a/extension/src/openvic2/GameManager.hpp b/extension/src/openvic2/GameManager.hpp index 7b15dc8..70e98bd 100644 --- a/extension/src/openvic2/GameManager.hpp +++ b/extension/src/openvic2/GameManager.hpp @@ -1,8 +1,7 @@ #pragma once -#include "openvic2/GameAdvancementHook.hpp" -#include "openvic2/map/Map.hpp" -#include "openvic2/Types.hpp" +#include "GameAdvancementHook.hpp" +#include "map/Map.hpp" namespace OpenVic2 { struct GameManager { diff --git a/extension/src/openvic2/Good.hpp b/extension/src/openvic2/Good.hpp index 54078dd..378bbc4 100644 --- a/extension/src/openvic2/Good.hpp +++ b/extension/src/openvic2/Good.hpp @@ -1,6 +1,5 @@ #pragma once -#include <string> #include "Types.hpp" namespace OpenVic2 { diff --git a/extension/src/openvic2/Logger.cpp b/extension/src/openvic2/Logger.cpp index 56d74ab..4d7378e 100644 --- a/extension/src/openvic2/Logger.cpp +++ b/extension/src/openvic2/Logger.cpp @@ -1,4 +1,4 @@ -#include "openvic2/Logger.hpp" +#include "Logger.hpp" #include <iostream> diff --git a/extension/src/openvic2/Types.cpp b/extension/src/openvic2/Types.cpp index 42aa5a4..f51ad7c 100644 --- a/extension/src/openvic2/Types.cpp +++ b/extension/src/openvic2/Types.cpp @@ -1,9 +1,8 @@ -#include "openvic2/Types.hpp" +#include "Types.hpp" #include <cassert> #include <sstream> #include <iomanip> -#include "Types.hpp" using namespace OpenVic2; @@ -16,7 +15,7 @@ std::string const& HasIdentifier::get_identifier() const { } HasColour::HasColour(colour_t const new_colour) : colour(new_colour) { - assert(colour <= MAX_COLOUR && colour != NULL_COLOUR); + assert(colour != NULL_COLOUR && colour <= MAX_COLOUR_RGB); } colour_t HasColour::get_colour() const { return colour; } @@ -29,4 +28,4 @@ std::string OpenVic2::HasColour::colour_to_hex_string(colour_t const colour) { std::string HasColour::colour_to_hex_string() const { return colour_to_hex_string(colour); -}
\ No newline at end of file +} diff --git a/extension/src/openvic2/Types.hpp b/extension/src/openvic2/Types.hpp index f1ba639..1359fbe 100644 --- a/extension/src/openvic2/Types.hpp +++ b/extension/src/openvic2/Types.hpp @@ -3,18 +3,27 @@ #include <string> #include <vector> #include <cstdint> +#include <algorithm> -#include "openvic2/Logger.hpp" +#include "Logger.hpp" namespace OpenVic2 { - //Represents a 24-bit RGB integer + // Represents a 24-bit RGB integer OR a 32-bit ARGB integer using colour_t = uint32_t; - using index_t = uint16_t; + /* When colour_t is used as an identifier, NULL_COLOUR is disallowed + * and should be reserved as an error value. + * When colour_t is used in a purely graphical context, NULL_COLOUR + * should be allowed. + */ + static constexpr colour_t NULL_COLOUR = 0, MAX_COLOUR_RGB = 0xFFFFFF; + constexpr colour_t to_alpha_value(float a) { + return static_cast<colour_t>(std::clamp(a, 0.0f, 1.0f) * 255.0f) << 24; + } - static constexpr colour_t NULL_COLOUR = 0, MAX_COLOUR = 0xFFFFFF; + using index_t = uint16_t; static constexpr index_t NULL_INDEX = 0, MAX_INDEX = 0xFFFF; - //TODO: price_t must be changed to a fixed-point numeric type before multiplayer + // TODO: price_t must be changed to a fixed-point numeric type before multiplayer using price_t = double; using return_t = bool; diff --git a/extension/src/openvic2/map/Building.cpp b/extension/src/openvic2/map/Building.cpp index 00e121b..1e26873 100644 --- a/extension/src/openvic2/map/Building.cpp +++ b/extension/src/openvic2/map/Building.cpp @@ -1,9 +1,9 @@ -#include "openvic2/map/Building.hpp" +#include "Building.hpp" #include <cassert> -#include "openvic2/Logger.hpp" -#include "openvic2/map/Province.hpp" +#include "../Logger.hpp" +#include "Province.hpp" using namespace OpenVic2; diff --git a/extension/src/openvic2/map/Building.hpp b/extension/src/openvic2/map/Building.hpp index 1305014..78d08ae 100644 --- a/extension/src/openvic2/map/Building.hpp +++ b/extension/src/openvic2/map/Building.hpp @@ -2,8 +2,8 @@ #include <vector> -#include "openvic2/Types.hpp" -#include "openvic2/Date.hpp" +#include "../Types.hpp" +#include "../Date.hpp" namespace OpenVic2 { struct Province; diff --git a/extension/src/openvic2/map/Map.cpp b/extension/src/openvic2/map/Map.cpp index 4e7c0bb..ab035ad 100644 --- a/extension/src/openvic2/map/Map.cpp +++ b/extension/src/openvic2/map/Map.cpp @@ -1,9 +1,9 @@ -#include "openvic2/map/Map.hpp" +#include "Map.hpp" #include <cassert> #include <unordered_set> -#include "openvic2/Logger.hpp" +#include "../Logger.hpp" using namespace OpenVic2; @@ -31,7 +31,7 @@ return_t Map::add_province(std::string const& identifier, colour_t colour) { Logger::error("Invalid province identifier - empty!"); return FAILURE; } - if (colour == NULL_COLOUR || colour > MAX_COLOUR) { + if (colour == NULL_COLOUR || colour > MAX_COLOUR_RGB) { Logger::error("Invalid province colour: ", Province::colour_to_hex_string(colour)); return FAILURE; } @@ -175,7 +175,8 @@ static colour_t colour_at(uint8_t const* colour_data, int32_t idx) { return (colour_data[idx * 3] << 16) | (colour_data[idx * 3 + 1] << 8) | colour_data[idx * 3 + 2]; } -return_t Map::generate_province_shape_image(size_t new_width, size_t new_height, uint8_t const* colour_data) { +return_t Map::generate_province_shape_image(size_t new_width, size_t new_height, uint8_t const* colour_data, + uint8_t const* terrain_data, terrain_variant_map_t const& terrain_variant_map) { if (!province_shape_image.empty()) { Logger::error("Province index image has already been generated!"); return FAILURE; @@ -192,47 +193,62 @@ return_t Map::generate_province_shape_image(size_t new_width, size_t new_height, Logger::error("Province colour data pointer is null!"); return FAILURE; } + if (terrain_data == nullptr) { + Logger::error("Province terrain data pointer is null!"); + return FAILURE; + } width = new_width; height = new_height; province_shape_image.resize(width * height); std::vector<bool> province_checklist(provinces.get_item_count()); return_t ret = SUCCESS; - std::unordered_set<colour_t> unrecognised_colours; + std::unordered_set<colour_t> unrecognised_province_colours, unrecognised_terrain_colours; for (int32_t y = 0; y < height; ++y) { for (int32_t x = 0; x < width; ++x) { const int32_t idx = x + y * width; - const colour_t colour = colour_at(colour_data, idx); + + const colour_t terrain_colour = colour_at(terrain_data, idx); + const terrain_variant_map_t::const_iterator it = terrain_variant_map.find(terrain_colour); + if (it != terrain_variant_map.end()) province_shape_image[idx].terrain = it->second; + else { + if (unrecognised_terrain_colours.find(terrain_colour) == unrecognised_terrain_colours.end()) { + unrecognised_terrain_colours.insert(terrain_colour); + Logger::error("Unrecognised terrain colour ", Province::colour_to_hex_string(terrain_colour), " at (", x, ", ", y, ")"); + ret = FAILURE; + } + province_shape_image[idx].terrain = 0; + } + + const colour_t province_colour = colour_at(colour_data, idx); if (x > 0) { const int32_t jdx = idx - 1; - if (colour_at(colour_data, jdx) == colour) { - province_shape_image[idx] = province_shape_image[jdx]; + if (colour_at(colour_data, jdx) == province_colour) { + province_shape_image[idx].index = province_shape_image[jdx].index; continue; } } if (y > 0) { const int32_t jdx = idx - width; - if (colour_at(colour_data, jdx) == colour) { - province_shape_image[idx] = province_shape_image[jdx]; + if (colour_at(colour_data, jdx) == province_colour) { + province_shape_image[idx].index = province_shape_image[jdx].index; continue; } } - Province const* province = get_province_by_colour(colour); + Province const* province = get_province_by_colour(province_colour); if (province != nullptr) { const index_t index = province->get_index(); province_checklist[index - 1] = true; province_shape_image[idx].index = index; - province_shape_image[idx].terrain = !province->is_water(); continue; } - if (unrecognised_colours.find(colour) == unrecognised_colours.end()) { - unrecognised_colours.insert(colour); - Logger::error("Unrecognised province colour ", Province::colour_to_hex_string(colour), " at (", x, ", ", y, ")"); + if (unrecognised_province_colours.find(province_colour) == unrecognised_province_colours.end()) { + unrecognised_province_colours.insert(province_colour); + Logger::error("Unrecognised province colour ", Province::colour_to_hex_string(province_colour), " at (", x, ", ", y, ")"); ret = FAILURE; } province_shape_image[idx].index = NULL_INDEX; - province_shape_image[idx].terrain = 0; } } @@ -303,6 +319,7 @@ return_t Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) *target++ = (colour >> 16) & 0xFF; *target++ = (colour >> 8) & 0xFF; *target++ = colour & 0xFF; + *target++ = (colour >> 24) & 0xFF; } return SUCCESS; } diff --git a/extension/src/openvic2/map/Map.hpp b/extension/src/openvic2/map/Map.hpp index e5be9c7..a075505 100644 --- a/extension/src/openvic2/map/Map.hpp +++ b/extension/src/openvic2/map/Map.hpp @@ -1,9 +1,9 @@ #pragma once #include <functional> +#include <unordered_map> -#include "openvic2/map/Region.hpp" -#include "openvic2/Types.hpp" +#include "Region.hpp" namespace OpenVic2 { @@ -26,10 +26,12 @@ namespace OpenVic2 { * MAP-4 */ struct Map { + using terrain_t = uint8_t; + using terrain_variant_map_t = std::unordered_map<colour_t, terrain_t>; #pragma pack(push, 1) struct shape_pixel_t { index_t index; - uint8_t terrain; + terrain_t terrain; }; #pragma pack(pop) private: @@ -63,7 +65,8 @@ namespace OpenVic2 { Region* get_region_by_identifier(std::string const& identifier); Region const* get_region_by_identifier(std::string const& identifier) const; - return_t generate_province_shape_image(size_t new_width, size_t new_height, uint8_t const* colour_data); + return_t generate_province_shape_image(size_t new_width, size_t new_height, uint8_t const* colour_data, + uint8_t const* terrain_data, terrain_variant_map_t const& terrain_variant_map); size_t get_width() const; size_t get_height() const; std::vector<shape_pixel_t> const& get_province_shape_image() const; @@ -73,7 +76,7 @@ namespace OpenVic2 { size_t get_mapmode_count() const; Mapmode const* get_mapmode_by_index(Mapmode::index_t index) const; Mapmode const* get_mapmode_by_identifier(std::string const& identifier) const; - static constexpr size_t MAPMODE_COLOUR_SIZE = 3; + static constexpr size_t MAPMODE_COLOUR_SIZE = 4; return_t generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const; return_t generate_province_buildings(BuildingManager const& manager); diff --git a/extension/src/openvic2/map/Province.cpp b/extension/src/openvic2/map/Province.cpp index 472cccb..b3d455b 100644 --- a/extension/src/openvic2/map/Province.cpp +++ b/extension/src/openvic2/map/Province.cpp @@ -1,4 +1,4 @@ -#include "openvic2/map/Province.hpp" +#include "Province.hpp" #include <cassert> #include <sstream> diff --git a/extension/src/openvic2/map/Province.hpp b/extension/src/openvic2/map/Province.hpp index 65eaa09..9b07fc1 100644 --- a/extension/src/openvic2/map/Province.hpp +++ b/extension/src/openvic2/map/Province.hpp @@ -1,7 +1,6 @@ #pragma once -#include "openvic2/map/Building.hpp" -#include "openvic2/Types.hpp" +#include "Building.hpp" namespace OpenVic2 { struct Map; diff --git a/extension/src/openvic2/map/Region.cpp b/extension/src/openvic2/map/Region.cpp index 6839a10..da0dfdd 100644 --- a/extension/src/openvic2/map/Region.cpp +++ b/extension/src/openvic2/map/Region.cpp @@ -1,7 +1,6 @@ -#include "openvic2/map/Region.hpp" +#include "Region.hpp" #include <cassert> -#include <algorithm> using namespace OpenVic2; diff --git a/extension/src/openvic2/map/Region.hpp b/extension/src/openvic2/map/Region.hpp index 58fad31..3920dfc 100644 --- a/extension/src/openvic2/map/Region.hpp +++ b/extension/src/openvic2/map/Region.hpp @@ -2,8 +2,7 @@ #include <set> -#include "openvic2/map/Province.hpp" -#include "openvic2/Types.hpp" +#include "Province.hpp" namespace OpenVic2 { |