Debian Codegraph @

CorsixTH/Src/th_map_overlays.cpp

raw
1 /*
2 Copyright (c) 2010 Peter "Corsix" Cawley
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #include "th_map_overlays.h"
24
25 #include <array>
26 #include <list>
27 #include <sstream>
28 #include <string>
29 #include <string_view>
30
31 #include "th_gfx_font.h"
32 #include "th_gfx_sdl.h"
33 #include "th_map.h"
34
35 map_overlay_pair::~map_overlay_pair() {
36 set_first(nullptr, false);
37 set_second(nullptr, false);
38 }
39
40 void map_overlay_pair::set_first(map_overlay* pOverlay, bool bTakeOwnership) {
41 if (first && owns_first) {
42 delete first;
43 }
44 first = pOverlay;
45 owns_first = bTakeOwnership;
46 }
47
48 void map_overlay_pair::set_second(map_overlay* pOverlay, bool bTakeOwnership) {
49 if (second && owns_second) {
50 delete second;
51 }
52 second = pOverlay;
53 owns_second = bTakeOwnership;
54 }
55
56 void map_overlay_pair::draw_cell(render_target* pCanvas, int iCanvasX,
57 int iCanvasY, const level_map* pMap,
58 int iNodeX, int iNodeY) {
59 if (first) {
60 first->draw_cell(pCanvas, iCanvasX, iCanvasY, pMap, iNodeX, iNodeY);
61 }
62 if (second) {
63 second->draw_cell(pCanvas, iCanvasX, iCanvasY, pMap, iNodeX, iNodeY);
64 }
65 }
66
67 void map_text_overlay::set_background_sprite(size_t iSprite) {
68 background_sprite = iSprite;
69 }
70
71 void map_text_overlay::draw_cell(render_target* pCanvas, int iCanvasX,
72 int iCanvasY, const level_map* pMap,
73 int iNodeX, int iNodeY) {
74 if (sprites && background_sprite) {
75 sprites->draw_sprite(pCanvas, background_sprite, iCanvasX, iCanvasY, 0);
76 }
77 if (font) {
78 draw_text(pCanvas, iCanvasX, iCanvasY, get_text(pMap, iNodeX, iNodeY));
79 }
80 }
81
82 const std::string map_positions_overlay::get_text(const level_map* pMap,
83 int iNodeX, int iNodeY) {
84 std::ostringstream str;
85 str << iNodeX + 1 << ',' << iNodeY + 1;
86 return str.str();
87 }
88
89 map_typical_overlay::~map_typical_overlay() {
90 set_sprites(nullptr, false);
91 set_font(nullptr, false);
92 }
93
94 void map_flags_overlay::draw_cell(render_target* pCanvas, int iCanvasX,
95 int iCanvasY, const level_map* pMap,
96 int iNodeX, int iNodeY) {
97 const map_tile* pNode = pMap->get_tile(iNodeX, iNodeY);
98 if (!pNode) {
99 return;
100 }
101 if (sprites) {
102 if (pNode->flags.passable) {
103 sprites->draw_sprite(pCanvas, 3, iCanvasX, iCanvasY, 0);
104 }
105 if (pNode->flags.hospital) {
106 sprites->draw_sprite(pCanvas, 8, iCanvasX, iCanvasY, 0);
107 }
108 if (pNode->flags.buildable) {
109 sprites->draw_sprite(pCanvas, 9, iCanvasX, iCanvasY, 0);
110 }
111 if (pNode->flags.can_travel_n &&
112 pMap->get_tile(iNodeX, iNodeY - 1)->flags.passable) {
113 sprites->draw_sprite(pCanvas, 4, iCanvasX, iCanvasY, 0);
114 }
115 if (pNode->flags.can_travel_e &&
116 pMap->get_tile(iNodeX + 1, iNodeY)->flags.passable) {
117 sprites->draw_sprite(pCanvas, 5, iCanvasX, iCanvasY, 0);
118 }
119 if (pNode->flags.can_travel_s &&
120 pMap->get_tile(iNodeX, iNodeY + 1)->flags.passable) {
121 sprites->draw_sprite(pCanvas, 6, iCanvasX, iCanvasY, 0);
122 }
123 if (pNode->flags.can_travel_w &&
124 pMap->get_tile(iNodeX - 1, iNodeY)->flags.passable) {
125 sprites->draw_sprite(pCanvas, 7, iCanvasX, iCanvasY, 0);
126 }
127 }
128 if (font) {
129 if (!pNode->objects.empty()) {
130 std::ostringstream str;
131 str << 'T' << static_cast<int>(pNode->objects.front());
132 draw_text(pCanvas, iCanvasX, iCanvasY - 8, str.str());
133 }
134 if (pNode->iRoomId) {
135 std::ostringstream str;
136 str << 'R' << static_cast<int>(pNode->iRoomId);
137 draw_text(pCanvas, iCanvasX, iCanvasY + 8, str.str());
138 }
139 }
140 }
141
142 namespace {
143
144 // The sprite to include if the tile in the direction indicated by dx,dy is
145 // not from the same parcel.
146 struct parcel_edge_sprite {
147 int dx;
148 int dy;
149 size_t sprite;
150 };
151
152 // Parcel edge sprites for all four directions
153 constexpr std::array<parcel_edge_sprite, 4> parcel_edges{
154 {{0, -1, 18}, {1, 0, 19}, {0, 1, 20}, {-1, 0, 21}}};
155
156 } // namespace
157
158 void map_parcels_overlay::draw_cell(render_target* pCanvas, int iCanvasX,
159 int iCanvasY, const level_map* pMap,
160 int iNodeX, int iNodeY) {
161 const map_tile* pNode = pMap->get_tile(iNodeX, iNodeY);
162 if (!pNode) {
163 return;
164 }
165 if (font) {
166 draw_text(pCanvas, iCanvasX, iCanvasY, std::to_string(pNode->iParcelId));
167 }
168 if (sprites) {
169 for (const parcel_edge_sprite& dir_sprite : parcel_edges) {
170 const map_tile* dir_node =
171 pMap->get_tile(iNodeX + dir_sprite.dx, iNodeY + dir_sprite.dy);
172 if (!dir_node || dir_node->iParcelId == pNode->iParcelId) {
173 sprites->draw_sprite(pCanvas, dir_sprite.sprite, iCanvasX, iCanvasY, 0);
174 }
175 }
176 }
177 }
178
179 void map_typical_overlay::draw_text(render_target* pCanvas, int iX, int iY,
180 const std::string_view str) {
181 text_layout oArea = font->get_text_dimensions(str.data(), str.length());
182 font->draw_text(pCanvas, str.data(), str.length(),
183 iX + (64 - oArea.end_x) / 2, iY + (32 - oArea.end_y) / 2);
184 }
185
186 void map_typical_overlay::set_sprites(sprite_sheet* pSheet,
187 bool bTakeOwnership) {
188 if (sprites && owns_sprites) {
189 delete sprites;
190 }
191 sprites = pSheet;
192 owns_sprites = bTakeOwnership;
193 }
194
195 void map_typical_overlay::set_font(::font* font, bool take_ownership) {
196 if (this->font && owns_font) {
197 delete this->font;
198 }
199 this->font = font;
200 owns_font = take_ownership;
201 }