aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map/Mapmode.hpp
blob: d514f7a8dca8d3790a1624c7d854db0b3ef9d48d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once

#include <functional>

#include "openvic-simulation/types/Colour.hpp"
#include "openvic-simulation/types/HasIdentifier.hpp"
#include "openvic-simulation/types/IdentifierRegistry.hpp"

namespace OpenVic {
   struct MapmodeManager;
   struct MapInstance;
   struct ProvinceInstance;

   struct Mapmode : HasIdentifier {
      friend struct MapmodeManager;

      /* Bottom 32 bits are the base colour, top 32 are the stripe colour, both in ARGB format with the alpha channels
       * controlling interpolation with the terrain colour (0 = all terrain, 255 = all corresponding RGB) */
      struct base_stripe_t {
         colour_argb_t base_colour;
         colour_argb_t stripe_colour;
         constexpr base_stripe_t(colour_argb_t base, colour_argb_t stripe)
            : base_colour { base }, stripe_colour { stripe } {}
         constexpr base_stripe_t(colour_argb_t both) : base_stripe_t { both, both } {}
      };
      using colour_func_t = std::function<base_stripe_t(MapInstance const&, ProvinceInstance const&)>;
      using index_t = size_t;

   private:
      const index_t PROPERTY(index);
      const colour_func_t colour_func;

      Mapmode(std::string_view new_identifier, index_t new_index, colour_func_t new_colour_func);

   public:
      static const Mapmode ERROR_MAPMODE;

      Mapmode(Mapmode&&) = default;

      base_stripe_t get_base_stripe_colours(MapInstance const& map_instance, ProvinceInstance const& province) const;
   };

   struct MapmodeManager {
   private:
      IdentifierRegistry<Mapmode> IDENTIFIER_REGISTRY(mapmode);

   public:
      MapmodeManager() = default;

      bool add_mapmode(std::string_view identifier, Mapmode::colour_func_t colour_func);

      /* The mapmode colour image contains of a list of base colours and stripe colours. Each colour is four bytes
       * in RGBA format, with the alpha value being used to interpolate with the terrain colour, so A = 0 is fully terrain
       * and A = 255 is fully the RGB colour packaged with A. The base and stripe colours for each province are packed
       * together adjacently, so each province's entry is 8 bytes long. The list contains ProvinceDefinition::MAX_INDEX + 1
       * entries, that is the maximum allowed number of provinces plus one for the index-zero "null province". */
      bool generate_mapmode_colours(MapInstance const& map_instance, Mapmode::index_t index, uint8_t* target) const;

      bool setup_mapmodes();
   };
}