Freeciv21
Develop your civilization from humble roots to a global empire
layer_terrain.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 2021-2023 Freeciv21 contributors.
3 \_ \ / __/ This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 #include "fc_types.h"
16 #include "layer.h"
17 #include "tilespec.h"
18 
19 #include <QPixmap>
20 
21 #include <array>
22 #include <map>
23 #include <memory>
24 
25 namespace freeciv {
26 
27 class layer_terrain : public layer {
28 public:
30  enum sprite_type {
34  };
35 
36 private:
37  struct matching_group {
38  int number;
39  QString name;
40  };
41 
42  enum match_style {
44  MATCH_SAME, // "boolean" match
47  };
48 
49  struct terrain_info {
51  QString sprite_name;
52  int offset_x = 0, offset_y = 0;
53 
55  matching_group *group = nullptr;
56  std::vector<matching_group *> matches_with;
57 
58  std::vector<QPixmap *> sprites = {};
59 
60  bool blend = false;
61  std::array<QPixmap *, 4> blend_sprites = {nullptr};
62  };
63 
64 public:
65  constexpr static auto MAX_NUM_MATCH_WITH = 8;
66 
67  explicit layer_terrain(struct tileset *ts, int number);
68  virtual ~layer_terrain() = default;
69 
70  bool create_matching_group(const QString &name);
71 
72  bool add_tag(const QString &tag, const QString &sprite_name);
73  bool set_tag_sprite_type(const QString &tag, sprite_type type);
74  bool set_tag_offsets(const QString &tag, int offset_x, int offset_y);
75  bool set_tag_matching_group(const QString &tag, const QString &group_name);
76  bool set_tag_matches_with(const QString &tag, const QString &group_name);
77  void enable_blending(const QString &tag);
78 
79  void initialize_terrain(const terrain *terrain) override;
80 
81  std::vector<drawn_sprite>
82  fill_sprite_array(const tile *ptile, const tile_edge *pedge,
83  const tile_corner *pcorner,
84  const unit *punit) const override;
85 
86 private:
87  matching_group *group(const QString &name);
88 
90  terrain_info &info);
92  terrain_info &info);
94  terrain_info &info);
96  terrain_info &info);
98  terrain_info &info);
100  terrain_info &info);
102  terrain_info &info);
103  void initialize_blending(const terrain *terrain, terrain_info &info);
104 
105  int terrain_group(const terrain *pterrain) const;
106  void fill_terrain_sprite_array(std::vector<drawn_sprite> &sprs,
107  const tile *ptile, const terrain *pterrain,
108  terrain **tterrain_near) const;
109  void fill_blending_sprite_array(std::vector<drawn_sprite> &sprs,
110  const tile *ptile, const terrain *pterrain,
111  terrain **tterrain_near) const;
112 
113  int m_number = 0;
114 
119  std::vector<std::unique_ptr<QPixmap>> m_allocated;
120 
121  std::map<QChar, matching_group> m_matching_groups;
122 
127  std::map<QString, terrain_info> m_terrain_tag_info;
128 
130  std::map<int, terrain_info> m_terrain_info;
131 };
132 
133 } // namespace freeciv
Draws terrain sprites on the map.
Definition: layer_terrain.h:27
bool add_tag(const QString &tag, const QString &sprite_name)
Makes a terrain tag available for use by this layer.
matching_group * group(const QString &name)
Retrieves the group structure of the provided name.
int terrain_group(const terrain *pterrain) const
Retrieves the group number for a given terrain.
void fill_blending_sprite_array(std::vector< drawn_sprite > &sprs, const tile *ptile, const terrain *pterrain, terrain **tterrain_near) const
Helper function for fill_sprite_array.
void initialize_cell_corner_match_none(const terrain *terrain, terrain_info &info)
Sets up terrain information for CELL_CORNER and MATCH_NONE.
void initialize_cell_corner_match_full(const terrain *terrain, terrain_info &info)
Sets up terrain information for CELL_CORNER and MATCH_FULL.
bool set_tag_matches_with(const QString &tag, const QString &group_name)
Sets the specified tag to be matched against a group.
std::map< int, terrain_info > m_terrain_info
Every terrain drawn in this layer appears here.
bool set_tag_offsets(const QString &tag, int offset_x, int offset_y)
Sets the offsets used to draw the specified tag.
layer_terrain(struct tileset *ts, int number)
sprite_type
Indicates how many sprites are used to draw a tile.
Definition: layer_terrain.h:30
@ CELL_WHOLE
One sprite for the entire tile.
Definition: layer_terrain.h:31
@ CELL_HEX_CORNER
One sprite for each hexagonal corner of the tile.
Definition: layer_terrain.h:33
@ CELL_CORNER
One sprite for each corner of the tile.
Definition: layer_terrain.h:32
bool set_tag_sprite_type(const QString &tag, sprite_type type)
Sets the type of sprite used to draw the specified tag.
void fill_terrain_sprite_array(std::vector< drawn_sprite > &sprs, const tile *ptile, const terrain *pterrain, terrain **tterrain_near) const
Helper function for fill_sprite_array.
void initialize_cell_whole_match_none(const terrain *terrain, terrain_info &info)
Sets up terrain information for CELL_WHOLE and MATCH_SAME.
void initialize_cell_corner_match_pair(const terrain *terrain, terrain_info &info)
Sets up terrain information for CELL_CORNER and MATCH_PAIR.
void enable_blending(const QString &tag)
Enable blending on this layer for the given terrain tag.
void initialize_cell_corner_match_same(const terrain *terrain, terrain_info &info)
Sets up terrain information for CELL_CORNER and MATCH_SAME.
std::map< QString, terrain_info > m_terrain_tag_info
Before terrains are loaded, this contains the list of available terrain tags.
virtual ~layer_terrain()=default
std::vector< drawn_sprite > fill_sprite_array(const tile *ptile, const tile_edge *pedge, const tile_corner *pcorner, const unit *punit) const override
Returns the list of sprites drawn by this layer somewhere on the map.
void initialize_terrain(const terrain *terrain) override
Sets up the structure to draw the specified terrain.
void initialize_cell_whole_match_same(const terrain *terrain, terrain_info &info)
Sets up terrain information for CELL_WHOLE and MATCH_SAME.
std::vector< std::unique_ptr< QPixmap > > m_allocated
List of those sprites in 'cells' that are allocated by some other means than load_sprite() and thus a...
void initialize_blending(const terrain *terrain, terrain_info &info)
Initializes blending sprites.
bool set_tag_matching_group(const QString &tag, const QString &group_name)
Sets the matching group for the specified tag.
std::map< QChar, matching_group > m_matching_groups
constexpr static auto MAX_NUM_MATCH_WITH
Definition: layer_terrain.h:65
void initialize_cell_hex_corner(const terrain *terrain, terrain_info &info)
Sets up terrain information for CELL_HEX_CORNER.
bool create_matching_group(const QString &name)
Creates a matching group with the given name.
A layer when drawing the map.
Definition: layer.h:153
mapview_layer type() const
Definition: layer.h:238
const char * name
Definition: inputfile.cpp:118
Definition: path.cpp:10
std::vector< matching_group * > matches_with
Definition: layer_terrain.h:56
std::array< QPixmap *, 4 > blend_sprites
Definition: layer_terrain.h:61
std::vector< QPixmap * > sprites
Definition: layer_terrain.h:58
Definition: tile.h:42
Definition: unit.h:134