Freeciv21
Develop your civilization from humble roots to a global empire
layer.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 "tileset/drawn_sprite.h"
16 
17 // Forward declarations
18 class QPixmap;
19 
20 struct city;
21 struct citystyle;
22 struct extra_type;
23 struct player;
24 struct terrain;
25 struct tileset;
26 struct unit;
27 struct unit_type;
28 
29 /* An edge is the border between two tiles. This structure represents one
30  * edge. The tiles are given in the same order as the enumeration name. */
31 enum edge_type {
32  EDGE_NS, // North and south
33  EDGE_WE, // West and east
34  EDGE_UD, /* Up and down (nw/se), for hex_width tilesets */
35  EDGE_LR, /* Left and right (ne/sw), for hex_height tilesets */
37 };
38 
39 struct tile_edge {
41 #define NUM_EDGE_TILES 2
42  const struct tile *tile[NUM_EDGE_TILES];
43 };
44 
45 /* A corner is the endpoint of several edges. At each corner 4 tiles will
46  * meet (3 in hex view). Tiles are in clockwise order NESW. */
47 struct tile_corner {
48 #define NUM_CORNER_TILES 4
49  const struct tile *tile[NUM_CORNER_TILES];
50 };
51 
52 #define SPECENUM_NAME extrastyle_id
53 #define SPECENUM_VALUE0 ESTYLE_ROAD_ALL_SEPARATE
54 #define SPECENUM_VALUE0NAME "RoadAllSeparate"
55 #define SPECENUM_VALUE1 ESTYLE_ROAD_PARITY_COMBINED
56 #define SPECENUM_VALUE1NAME "RoadParityCombined"
57 #define SPECENUM_VALUE2 ESTYLE_ROAD_ALL_COMBINED
58 #define SPECENUM_VALUE2NAME "RoadAllCombined"
59 #define SPECENUM_VALUE3 ESTYLE_RIVER
60 #define SPECENUM_VALUE3NAME "River"
61 #define SPECENUM_VALUE4 ESTYLE_SINGLE1
62 #define SPECENUM_VALUE4NAME "Single1"
63 #define SPECENUM_VALUE5 ESTYLE_SINGLE2
64 #define SPECENUM_VALUE5NAME "Single2"
65 #define SPECENUM_VALUE6 ESTYLE_3LAYER
66 #define SPECENUM_VALUE6NAME "3Layer"
67 #define SPECENUM_VALUE7 ESTYLE_CARDINALS
68 #define SPECENUM_VALUE7NAME "Cardinals"
69 #define SPECENUM_COUNT ESTYLE_COUNT
70 #include "specenum_gen.h"
71 
72 // This the way directional indices are now encoded:
73 #define MAX_INDEX_CARDINAL 64
74 #define MAX_INDEX_HALF 16
75 #define MAX_INDEX_VALID 256
76 
77 // Numbers on the map are shown in base 10
78 #define NUM_TILES_DIGITS 10
79 
80 /* Items on the mapview are drawn in layers. Each entry below represents
81  * one layer. The names are basically arbitrary and just correspond to
82  * groups of elements in fill_sprite_array(). Callers of fill_sprite_array
83  * must call it once for each layer. */
84 #define SPECENUM_NAME mapview_layer
85 #define SPECENUM_VALUE0 LAYER_BACKGROUND
86 #define SPECENUM_VALUE0NAME "Background"
87 // Adjust also TERRAIN_LAYER_COUNT if changing these
88 #define SPECENUM_VALUE1 LAYER_TERRAIN1
89 #define SPECENUM_VALUE1NAME "Terrain1"
90 #define SPECENUM_VALUE2 LAYER_DARKNESS
91 #define SPECENUM_VALUE2NAME "Darkness"
92 #define SPECENUM_VALUE3 LAYER_TERRAIN2
93 #define SPECENUM_VALUE3NAME "Terrain2"
94 #define SPECENUM_VALUE4 LAYER_TERRAIN3
95 #define SPECENUM_VALUE4NAME "Terrain3"
96 #define SPECENUM_VALUE5 LAYER_WATER
97 #define SPECENUM_VALUE5NAME "Water"
98 #define SPECENUM_VALUE6 LAYER_ROADS
99 #define SPECENUM_VALUE6NAME "Roads"
100 #define SPECENUM_VALUE7 LAYER_SPECIAL1
101 #define SPECENUM_VALUE7NAME "Special1"
102 #define SPECENUM_VALUE8 LAYER_GRID1
103 #define SPECENUM_VALUE8NAME "Grid1"
104 #define SPECENUM_VALUE9 LAYER_CITY1
105 #define SPECENUM_VALUE9NAME "City1"
106 #define SPECENUM_VALUE10 LAYER_SPECIAL2
107 #define SPECENUM_VALUE10NAME "Special2"
108 #define SPECENUM_VALUE11 LAYER_FOG
109 #define SPECENUM_VALUE11NAME "Fog"
110 #define SPECENUM_VALUE12 LAYER_UNIT
111 #define SPECENUM_VALUE12NAME "Unit"
112 #define SPECENUM_VALUE13 LAYER_SPECIAL3
113 #define SPECENUM_VALUE13NAME "Special3"
114 #define SPECENUM_VALUE14 LAYER_BASE_FLAGS
115 #define SPECENUM_VALUE14NAME "BaseFlags"
116 #define SPECENUM_VALUE15 LAYER_CITY2
117 #define SPECENUM_VALUE15NAME "City2"
118 #define SPECENUM_VALUE16 LAYER_GRID2
119 #define SPECENUM_VALUE16NAME "Grid2"
120 #define SPECENUM_VALUE17 LAYER_OVERLAYS
121 #define SPECENUM_VALUE17NAME "Overlays"
122 #define SPECENUM_VALUE18 LAYER_TILELABEL
123 #define SPECENUM_VALUE18NAME "TileLabel"
124 #define SPECENUM_VALUE19 LAYER_CITYBAR
125 #define SPECENUM_VALUE19NAME "CityBar"
126 #define SPECENUM_VALUE20 LAYER_FOCUS_UNIT
127 #define SPECENUM_VALUE20NAME "FocusUnit"
128 #define SPECENUM_VALUE21 LAYER_GOTO
129 #define SPECENUM_VALUE21NAME "Goto"
130 #define SPECENUM_VALUE22 LAYER_WORKERTASK
131 #define SPECENUM_VALUE22NAME "WorkerTask"
132 #define SPECENUM_VALUE23 LAYER_EDITOR
133 #define SPECENUM_VALUE23NAME "Editor"
134 #define SPECENUM_VALUE24 LAYER_INFRAWORK
135 #define SPECENUM_VALUE24NAME "InfraWork"
136 #define SPECENUM_COUNT LAYER_COUNT
137 #include "specenum_gen.h"
138 
139 #define TERRAIN_LAYER_COUNT 3
140 
141 // Layer categories can be used to only render part of a tile.
143  LAYER_CATEGORY_CITY, // Render cities
144  LAYER_CATEGORY_TILE, // Render terrain only
145  LAYER_CATEGORY_UNIT // Render units only
146 };
147 
148 namespace freeciv {
149 
153 class layer {
154 public:
158  layer(struct tileset *ts, mapview_layer layer) : m_ts(ts), m_layer(layer)
159  {
160  }
161 
165  virtual ~layer() = default;
166 
171  virtual std::vector<drawn_sprite>
172  fill_sprite_array(const tile *ptile, const tile_edge *pedge,
173  const tile_corner *pcorner, const unit *punit) const
174  {
175  Q_UNUSED(ptile);
176  Q_UNUSED(pedge);
177  Q_UNUSED(pcorner);
178  Q_UNUSED(punit);
179  return {};
180  }
181 
185  virtual void load_sprites() {}
186 
193  virtual void initialize_player(const player *player) { Q_UNUSED(player); }
194 
202  virtual void free_player(int player_id) { Q_UNUSED(player_id); }
203 
207  virtual void initialize_city_style(const citystyle &style, int index)
208  {
209  Q_UNUSED(style);
210  Q_UNUSED(index);
211  }
212 
217  virtual void initialize_extra(const extra_type *extra, const QString &tag,
218  extrastyle_id style)
219  {
220  Q_UNUSED(extra);
221  Q_UNUSED(tag);
222  Q_UNUSED(style);
223  }
224 
228  virtual void initialize_terrain(const terrain *terrain)
229  {
230  Q_UNUSED(terrain);
231  }
232 
236  virtual void reset_ruleset() {}
237 
238  mapview_layer type() const { return m_layer; }
239 
240 protected:
241  struct tileset *tileset() const { return m_ts; }
242 
243  bool do_draw_unit(const tile *ptile, const unit *punit) const;
244  bool solid_background(const tile *ptile, const unit *punit,
245  const city *pcity) const;
246 
247  QPixmap *load_sprite(const QStringList &possible_names,
248  bool required = false, bool verbose = true) const;
249 
250 private:
251  struct tileset *m_ts;
252  mapview_layer m_layer;
253 };
254 
255 } // namespace freeciv
A layer when drawing the map.
Definition: layer.h:153
virtual ~layer()=default
Destructor.
virtual void reset_ruleset()
Resets cached data that depends on the ruleset.
Definition: layer.h:236
mapview_layer type() const
Definition: layer.h:238
virtual void free_player(int player_id)
Frees data initialized by initialize_player.
Definition: layer.h:202
virtual void initialize_city_style(const citystyle &style, int index)
Initializes data for a city style.
Definition: layer.h:207
virtual std::vector< drawn_sprite > fill_sprite_array(const tile *ptile, const tile_edge *pedge, const tile_corner *pcorner, const unit *punit) const
Returns the list of sprites drawn by this layer somewhere on the map.
Definition: layer.h:172
virtual void initialize_player(const player *player)
Initializes data specific to one player.
Definition: layer.h:193
QPixmap * load_sprite(const QStringList &possible_names, bool required=false, bool verbose=true) const
Shortcut to load a sprite from the tileset.
Definition: layer.cpp:79
bool do_draw_unit(const tile *ptile, const unit *punit) const
Whether a unit should be drawn.
Definition: layer.cpp:27
virtual void initialize_extra(const extra_type *extra, const QString &tag, extrastyle_id style)
Initializes extra-specific data.
Definition: layer.h:217
bool solid_background(const tile *ptile, const unit *punit, const city *pcity) const
Whether a solid background should be drawn on a tile instead of its terrain.
Definition: layer.cpp:65
struct tileset * tileset() const
Definition: layer.h:241
mapview_layer m_layer
Definition: layer.h:252
virtual void initialize_terrain(const terrain *terrain)
Initializes terrain-specific data.
Definition: layer.h:228
struct tileset * m_ts
Definition: layer.h:251
layer(struct tileset *ts, mapview_layer layer)
Constructor.
Definition: layer.h:158
virtual void load_sprites()
Loads all sprites that do not depend on the ruleset.
Definition: layer.h:185
#define NUM_EDGE_TILES
Definition: layer.h:41
#define NUM_CORNER_TILES
Definition: layer.h:48
layer_category
Definition: layer.h:142
@ LAYER_CATEGORY_TILE
Definition: layer.h:144
@ LAYER_CATEGORY_CITY
Definition: layer.h:143
@ LAYER_CATEGORY_UNIT
Definition: layer.h:145
edge_type
Definition: layer.h:31
@ EDGE_WE
Definition: layer.h:33
@ EDGE_UD
Definition: layer.h:34
@ EDGE_LR
Definition: layer.h:35
@ EDGE_NS
Definition: layer.h:32
@ EDGE_COUNT
Definition: layer.h:36
Definition: path.cpp:10
Definition: city.h:291
Definition: player.h:231
edge_type type
Definition: layer.h:40
Definition: tile.h:42
Definition: unit.h:134