Debian Codegraph @

engine/map/map_draw.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name map_draw.cpp - The map drawing. */
11 //
12 // (c) Copyright 1999-2008 by Lutz Sammer and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include "stratagus.h"
36 #include "unit.h"
37 #include "video.h"
38 #include "map.h"
39 #include "ui.h"
40 #include "missile.h"
41 #include "unittype.h"
42 #include "particle.h"
43 #include "patch_type.h"
44 #include "patch.h"
45 #include "patch_manager.h"
46 #include "editor.h"
47
48 /*----------------------------------------------------------------------------
49 -- Declarations
50 ----------------------------------------------------------------------------*/
51
52 /*----------------------------------------------------------------------------
53 -- Functions
54 ----------------------------------------------------------------------------*/
55
56 /*----------------------------------------------------------------------------
57 -- Global functions
58 ----------------------------------------------------------------------------*/
59
60 /**
61 ** Check if any part of an area is visible in a viewport.
62 **
63 ** @param sx X map tile position of area in map to be checked.
64 ** @param sy Y map tile position of area in map to be checked.
65 ** @param ex X map tile position of area in map to be checked.
66 ** @param ey Y map tile position of area in map to be checked.
67 **
68 ** @return True if any part of area is visible, false otherwise
69 */
70 bool CViewport::AnyMapAreaVisibleInViewport(int sx, int sy, int ex, int ey) const
71 {
72 if (ex < this->MapX || ey < this->MapY ||
73 sx >= this->MapX + this->MapWidth || sy >= this->MapY + this->MapHeight) {
74 return false;
75 }
76 return true;
77 }
78
79 bool CViewport::IsInsideMapArea(int x, int y) const
80 {
81 int tilex;
82 int tiley;
83
84 tilex = x - this->X + this->MapX * TileSizeX + this->OffsetX;
85 if (tilex < 0) {
86 tilex = (tilex - TileSizeX + 1) / TileSizeX;
87 } else {
88 tilex /= TileSizeX;
89 }
90
91 tiley = y - this->Y + this->MapY * TileSizeY + this->OffsetY;
92 if (tiley < 0) {
93 tiley = (tiley - TileSizeY + 1) / TileSizeY;
94 } else {
95 tiley /= TileSizeY;
96 }
97
98 return (tilex >= 0 && tiley >= 0 && tilex < Map.Info.MapWidth && tiley < Map.Info.MapHeight);
99 }
100
101 /**
102 ** Convert viewport x coordinate to map tile x coordinate.
103 **
104 ** @param x X coordinate into this viewport (in pixels, relative
105 ** to origin of Stratagus's window - not the viewport
106 ** itself!).
107 **
108 ** @return X map tile coordinate.
109 */
110 int CViewport::Viewport2MapX(int x) const
111 {
112 int r = (x - this->X + this->MapX * TileSizeX + this->OffsetX) / TileSizeX;
113 return std::min(r, Map.Info.MapWidth - 1);
114 }
115
116 /**
117 ** Convert viewport y coordinate to map tile y coordinate.
118 **
119 ** @param y Y coordinate into this viewport (in pixels, relative
120 ** to origin of Stratagus's window - not the viewport
121 ** itself!).
122 **
123 ** @return Y map tile coordinate.
124 */
125 int CViewport::Viewport2MapY(int y) const
126 {
127 int r = (y - this->Y + this->MapY * TileSizeY + this->OffsetY) / TileSizeY;
128 return std::min(r, Map.Info.MapHeight - 1);
129 }
130
131 /**
132 ** Convert a map tile X coordinate into a viewport x pixel coordinate.
133 **
134 ** @param x The map tile's X coordinate.
135 **
136 ** @return X screen coordinate in pixels (relative
137 ** to origin of Stratagus's window).
138 */
139 int CViewport::Map2ViewportX(int x) const
140 {
141 return this->X + (x - this->MapX) * TileSizeX - this->OffsetX;
142 }
143
144 /**
145 ** Convert a map tile Y coordinate into a viewport y pixel coordinate.
146 **
147 ** @param y The map tile's Y coordinate.
148 **
149 ** @return Y screen coordinate in pixels (relative
150 ** to origin of Stratagus's window).
151 */
152 int CViewport::Map2ViewportY(int y) const
153 {
154 return this->Y + (y - this->MapY) * TileSizeY - this->OffsetY;
155 }
156
157 /**
158 ** Convert map pixel coordinates into viewport coordinates.
159 */
160 void CViewport::MapPixel2Viewport(int &x, int &y) const
161 {
162 x = x + this->X - (this->MapX * TileSizeX + this->OffsetX);
163 y = y + this->Y - (this->MapY * TileSizeY + this->OffsetY);
164 }
165
166 /**
167 ** Change viewpoint of map viewport v to x,y.
168 **
169 ** @param x X map tile position.
170 ** @param y Y map tile position.
171 ** @param offsetx X offset in tile.
172 ** @param offsety Y offset in tile.
173 */
174 void CViewport::Set(int x, int y, int offsetx, int offsety)
175 {
176 x = x * TileSizeX + offsetx;
177 y = y * TileSizeY + offsety;
178
179 if (x < -UI.MapArea.ScrollPaddingLeft) {
180 x = -UI.MapArea.ScrollPaddingLeft;
181 }
182 if (y < -UI.MapArea.ScrollPaddingTop) {
183 y = -UI.MapArea.ScrollPaddingTop;
184 }
185 if (x > Map.Info.MapWidth * TileSizeX - (this->EndX - this->X) - 1 + UI.MapArea.ScrollPaddingRight) {
186 x = Map.Info.MapWidth * TileSizeX - (this->EndX - this->X) - 1 + UI.MapArea.ScrollPaddingRight;
187 }
188 if (y > Map.Info.MapHeight * TileSizeY - (this->EndY - this->Y) - 1 + UI.MapArea.ScrollPaddingBottom) {
189 y = Map.Info.MapHeight * TileSizeY - (this->EndY - this->Y) - 1 + UI.MapArea.ScrollPaddingBottom;
190 }
191
192 this->MapX = x / TileSizeX;
193 if (x < 0 && x % TileSizeX) {
194 this->MapX--;
195 }
196 this->MapY = y / TileSizeY;
197 if (y < 0 && y % TileSizeY) {
198 this->MapY--;
199 }
200 this->OffsetX = x % TileSizeX;
201 if (this->OffsetX < 0) {
202 this->OffsetX += TileSizeX;
203 }
204 this->OffsetY = y % TileSizeY;
205 if (this->OffsetY < 0) {
206 this->OffsetY += TileSizeY;
207 }
208 this->MapWidth = ((this->EndX - this->X) + this->OffsetX - 1) / TileSizeX + 1;
209 this->MapHeight = ((this->EndY - this->Y) + this->OffsetY - 1) / TileSizeY + 1;
210 }
211
212 /**
213 ** Center map viewport v on map tile (x,y).
214 **
215 ** @param x X map tile position.
216 ** @param y Y map tile position.
217 ** @param offsetx X offset in tile.
218 ** @param offsety Y offset in tile.
219 */
220 void CViewport::Center(int x, int y, int offsetx, int offsety)
221 {
222 x = x * TileSizeX + offsetx - (this->EndX - this->X) / 2;
223 y = y * TileSizeY + offsety - (this->EndY - this->Y) / 2;
224 this->Set(x / TileSizeX, y / TileSizeY, x % TileSizeX, y % TileSizeY);
225 }
226
227 /**
228 ** Draw the map backgrounds.
229 **
230 ** StephanR: variables explained below for screen:<PRE>
231 ** *---------------------------------------*
232 ** | |
233 ** | *-----------------------* |<-TheUi.MapY,dy (in pixels)
234 ** | | | | | | | | | |
235 ** | | | | | | | | | |
236 ** | |---+---+---+---+---+---| | |
237 ** | | | | | | | | | |MapHeight (in tiles)
238 ** | | | | | | | | | |
239 ** | |---+---+---+---+---+---| | |
240 ** | | | | | | | | | |
241 ** | | | | | | | | | |
242 ** | *-----------------------* |<-ey,UI.MapEndY (in pixels)
243 ** | |
244 ** | |
245 ** *---------------------------------------*
246 ** ^ ^
247 ** dx|-----------------------|ex,UI.MapEndX (in pixels)
248 ** UI.MapX MapWidth (in tiles)
249 ** (in pixels)
250 ** </PRE>
251 */
252 void CViewport::DrawMapBackgroundInViewport() const
253 {
254 const std::list<CPatch *> &patches = Map.PatchManager.getPatches();
255 std::list<CPatch *>::const_iterator i;
256
257 for (i = patches.begin(); i != patches.end(); ++i) {
258 const CPatch *patch = *i;
259
260 const CGraphic *g = patch->getType()->getGraphic();
261 int x = this->X - ((this->MapX - patch->getX()) * TileSizeX + this->OffsetX);
262 int y = this->Y - ((this->MapY - patch->getY()) * TileSizeY + this->OffsetY);
263 g->DrawClip(x, y);
264
265 if (Editor.Running && Editor.ShowPatchOutlines) {
266 Video.DrawRectangleClip(Editor.PatchOutlineColor, x, y,
267 patch->getType()->getTileWidth() * TileSizeX,
268 patch->getType()->getTileHeight() * TileSizeY);
269 }
270 }
271 }
272
273 /**
274 ** Draw a map viewport.
275 */
276 void CViewport::Draw() const
277 {
278 CUnit *table[UnitMax];
279 Missile *missiletable[MAX_MISSILES];
280 int nunits;
281 int nmissiles;
282 int i;
283 int j;
284
285 PushClipping();
286 SetClipping(this->X, this->Y, this->EndX, this->EndY);
287 this->DrawMapBackgroundInViewport();
288
289 //
290 // We find and sort units after draw level.
291 //
292 nunits = FindAndSortUnits(this, table, UnitMax);
293 nmissiles = FindAndSortMissiles(this, missiletable, MAX_MISSILES);
294
295 i = 0;
296 j = 0;
297 CurrentViewport = this;
298 while (i < nunits && j < nmissiles) {
299 if (table[i]->Type->DrawLevel <= missiletable[j]->Type->DrawLevel) {
300 table[i]->Draw();
301 ++i;
302 } else {
303 missiletable[j]->DrawMissile();
304 ++j;
305 }
306 }
307 for (; i < nunits; ++i) {
308 table[i]->Draw();
309 }
310 for (; j < nmissiles; ++j) {
311 missiletable[j]->DrawMissile();
312 }
313
314 ParticleManager.draw(this);
315
316 this->DrawMapFogOfWar();
317
318 //
319 // Draw orders of selected units.
320 // Drawn here so that they are shown even when the unit is out of the screen.
321 //
322 if (Preference.ShowOrders < 0 ||
323 (ShowOrdersCount >= GameCycle) || (KeyModifiers & ModifierShift)) {
324 for (i = 0; i < NumSelected; ++i) {
325 ShowOrder(Selected[i]);
326 }
327 }
328
329 DrawBorder();
330
331 PopClipping();
332 }
333
334 /**
335 ** Draw border around the viewport
336 */
337 void CViewport::DrawBorder() const
338 {
339 // if we a single viewport, no need to denote the "selected" one
340 if (UI.NumViewports == 1) {
341 return;
342 }
343
344 Uint32 color = ColorBlack;
345 if (this == UI.SelectedViewport) {
346 color = ColorOrange;
347 }
348
349 Video.DrawRectangle(color, this->X, this->Y, this->EndX - this->X + 1,
350 this->EndY - this->Y + 1);
351 }
352
353 //@}