/
viewport.cpp
3599 lines (3073 loc) · 121 KB
/
viewport.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file viewport.cpp Handling of all viewports.
*
* \verbatim
* The in-game coordinate system looks like this *
* *
* ^ Z *
* | *
* | *
* | *
* | *
* / \ *
* / \ *
* / \ *
* / \ *
* X < > Y *
* \endverbatim
*/
/**
* @defgroup vp_column_row Rows and columns in the viewport
*
* Columns are vertical sections of the viewport that are half a tile wide.
* The origin, i.e. column 0, is through the northern and southern most tile.
* This means that the column of e.g. Tile(0, 0) and Tile(100, 100) are in
* column number 0. The negative columns are towards the left of the screen,
* or towards the west, whereas the positive ones are towards respectively
* the right and east.
* With half a tile wide is meant that the next column of tiles directly west
* or east of the centre line are respectively column -1 and 1. Their tile
* centers are only half a tile from the center of their adjoining tile when
* looking only at the X-coordinate.
*
* \verbatim
* ╳ *
* ╱ ╲ *
* ╳ 0 ╳ *
* ╱ ╲ ╱ ╲ *
* ╳-1 ╳ 1 ╳ *
* ╱ ╲ ╱ ╲ ╱ ╲ *
* ╳-2 ╳ 0 ╳ 2 ╳ *
* ╲ ╱ ╲ ╱ ╲ ╱ *
* ╳-1 ╳ 1 ╳ *
* ╲ ╱ ╲ ╱ *
* ╳ 0 ╳ *
* ╲ ╱ *
* ╳ *
* \endverbatim
*
*
* Rows are horizontal sections of the viewport, also half a tile wide.
* This time the northern most tile on the map defines 0 and
* everything south of that has a positive number.
*/
#include "stdafx.h"
#include "landscape.h"
#include "viewport_func.h"
#include "station_base.h"
#include "waypoint_base.h"
#include "town.h"
#include "signs_base.h"
#include "signs_func.h"
#include "vehicle_base.h"
#include "vehicle_gui.h"
#include "blitter/factory.hpp"
#include "strings_func.h"
#include "zoom_func.h"
#include "vehicle_func.h"
#include "company_func.h"
#include "waypoint_func.h"
#include "window_func.h"
#include "tilehighlight_func.h"
#include "window_gui.h"
#include "linkgraph/linkgraph_gui.h"
#include "viewport_kdtree.h"
#include "town_kdtree.h"
#include "viewport_sprite_sorter.h"
#include "bridge_map.h"
#include "tunnel_map.h"
#include "tunnelbridge.h"
#include "tunnelbridge_map.h"
#include "company_base.h"
#include "command_func.h"
#include "network/network_func.h"
#include "framerate_type.h"
#include <forward_list>
#include <map>
#include <stack>
#include <unordered_set>
#include "table/strings.h"
#include "table/string_colours.h"
#include "safeguards.h"
Point _tile_fract_coords;
ViewportSignKdtree _viewport_sign_kdtree(&Kdtree_ViewportSignXYFunc);
static int _viewport_sign_maxwidth = 0;
static const int MAX_TILE_EXTENT_LEFT = ZOOM_LVL_BASE * TILE_PIXELS; ///< Maximum left extent of tile relative to north corner.
static const int MAX_TILE_EXTENT_RIGHT = ZOOM_LVL_BASE * TILE_PIXELS; ///< Maximum right extent of tile relative to north corner.
static const int MAX_TILE_EXTENT_TOP = ZOOM_LVL_BASE * MAX_BUILDING_PIXELS; ///< Maximum top extent of tile relative to north corner (not considering bridges).
static const int MAX_TILE_EXTENT_BOTTOM = ZOOM_LVL_BASE * (TILE_PIXELS + 2 * TILE_HEIGHT); ///< Maximum bottom extent of tile relative to north corner (worst case: #SLOPE_STEEP_N).
struct StringSpriteToDraw {
StringID string;
Colours colour;
int32 x;
int32 y;
uint64 params[2];
uint16 width;
};
struct TileSpriteToDraw {
SpriteID image;
PaletteID pal;
const SubSprite *sub; ///< only draw a rectangular part of the sprite
int32 x; ///< screen X coordinate of sprite
int32 y; ///< screen Y coordinate of sprite
};
struct ChildScreenSpriteToDraw {
SpriteID image;
PaletteID pal;
const SubSprite *sub; ///< only draw a rectangular part of the sprite
int32 x;
int32 y;
int next; ///< next child to draw (-1 at the end)
};
/** Enumeration of multi-part foundations */
enum FoundationPart {
FOUNDATION_PART_NONE = 0xFF, ///< Neither foundation nor groundsprite drawn yet.
FOUNDATION_PART_NORMAL = 0, ///< First part (normal foundation or no foundation)
FOUNDATION_PART_HALFTILE = 1, ///< Second part (halftile foundation)
FOUNDATION_PART_END
};
/**
* Mode of "sprite combining"
* @see StartSpriteCombine
*/
enum SpriteCombineMode {
SPRITE_COMBINE_NONE, ///< Every #AddSortableSpriteToDraw start its own bounding box
SPRITE_COMBINE_PENDING, ///< %Sprite combining will start with the next unclipped sprite.
SPRITE_COMBINE_ACTIVE, ///< %Sprite combining is active. #AddSortableSpriteToDraw outputs child sprites.
};
typedef std::vector<TileSpriteToDraw> TileSpriteToDrawVector;
typedef std::vector<StringSpriteToDraw> StringSpriteToDrawVector;
typedef std::vector<ParentSpriteToDraw> ParentSpriteToDrawVector;
typedef std::vector<ChildScreenSpriteToDraw> ChildScreenSpriteToDrawVector;
/** Data structure storing rendering information */
struct ViewportDrawer {
DrawPixelInfo dpi;
StringSpriteToDrawVector string_sprites_to_draw;
TileSpriteToDrawVector tile_sprites_to_draw;
ParentSpriteToDrawVector parent_sprites_to_draw;
ParentSpriteToSortVector parent_sprites_to_sort; ///< Parent sprite pointer array used for sorting
ChildScreenSpriteToDrawVector child_screen_sprites_to_draw;
int *last_child;
SpriteCombineMode combine_sprites; ///< Current mode of "sprite combining". @see StartSpriteCombine
int foundation[FOUNDATION_PART_END]; ///< Foundation sprites (index into parent_sprites_to_draw).
FoundationPart foundation_part; ///< Currently active foundation for ground sprite drawing.
int *last_foundation_child[FOUNDATION_PART_END]; ///< Tail of ChildSprite list of the foundations. (index into child_screen_sprites_to_draw)
Point foundation_offset[FOUNDATION_PART_END]; ///< Pixel offset for ground sprites on the foundations.
};
static bool MarkViewportDirty(const Viewport *vp, int left, int top, int right, int bottom);
static ViewportDrawer _vd;
TileHighlightData _thd;
static TileInfo *_cur_ti;
bool _draw_bounding_boxes = false;
bool _draw_dirty_blocks = false;
uint _dirty_block_colour = 0;
static VpSpriteSorter _vp_sprite_sorter = nullptr;
static Point MapXYZToViewport(const Viewport *vp, int x, int y, int z)
{
Point p = RemapCoords(x, y, z);
p.x -= vp->virtual_width / 2;
p.y -= vp->virtual_height / 2;
return p;
}
void DeleteWindowViewport(Window *w)
{
if (w->viewport == nullptr) return;
delete w->viewport->overlay;
free(w->viewport);
w->viewport = nullptr;
}
/**
* Initialize viewport of the window for use.
* @param w Window to use/display the viewport in
* @param x Offset of left edge of viewport with respect to left edge window \a w
* @param y Offset of top edge of viewport with respect to top edge window \a w
* @param width Width of the viewport
* @param height Height of the viewport
* @param follow_flags Flags controlling the viewport.
* - If bit 31 is set, the lower 20 bits are the vehicle that the viewport should follow.
* - If bit 31 is clear, it is a #TileIndex.
* @param zoom Zoomlevel to display
*/
void InitializeWindowViewport(Window *w, int x, int y,
int width, int height, uint32 follow_flags, ZoomLevel zoom)
{
assert(w->viewport == nullptr);
ViewportData *vp = CallocT<ViewportData>(1);
vp->left = x + w->left;
vp->top = y + w->top;
vp->width = width;
vp->height = height;
vp->zoom = static_cast<ZoomLevel>(Clamp(zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max));
vp->virtual_width = ScaleByZoom(width, zoom);
vp->virtual_height = ScaleByZoom(height, zoom);
Point pt;
if (follow_flags & 0x80000000) {
const Vehicle *veh;
vp->follow_vehicle = (VehicleID)(follow_flags & 0xFFFFF);
veh = Vehicle::Get(vp->follow_vehicle);
pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
} else {
x = TileX(follow_flags) * TILE_SIZE;
y = TileY(follow_flags) * TILE_SIZE;
vp->follow_vehicle = INVALID_VEHICLE;
pt = MapXYZToViewport(vp, x, y, GetSlopePixelZ(x, y));
}
vp->scrollpos_x = pt.x;
vp->scrollpos_y = pt.y;
vp->dest_scrollpos_x = pt.x;
vp->dest_scrollpos_y = pt.y;
vp->overlay = nullptr;
w->viewport = vp;
vp->virtual_left = 0; // pt.x;
vp->virtual_top = 0; // pt.y;
}
static Point _vp_move_offs;
static void DoSetViewportPosition(Window::IteratorToFront it, int left, int top, int width, int height)
{
for (; !it.IsEnd(); ++it) {
const Window *w = *it;
if (left + width > w->left &&
w->left + w->width > left &&
top + height > w->top &&
w->top + w->height > top) {
if (left < w->left) {
DoSetViewportPosition(it, left, top, w->left - left, height);
DoSetViewportPosition(it, left + (w->left - left), top, width - (w->left - left), height);
return;
}
if (left + width > w->left + w->width) {
DoSetViewportPosition(it, left, top, (w->left + w->width - left), height);
DoSetViewportPosition(it, left + (w->left + w->width - left), top, width - (w->left + w->width - left), height);
return;
}
if (top < w->top) {
DoSetViewportPosition(it, left, top, width, (w->top - top));
DoSetViewportPosition(it, left, top + (w->top - top), width, height - (w->top - top));
return;
}
if (top + height > w->top + w->height) {
DoSetViewportPosition(it, left, top, width, (w->top + w->height - top));
DoSetViewportPosition(it, left, top + (w->top + w->height - top), width, height - (w->top + w->height - top));
return;
}
return;
}
}
{
int xo = _vp_move_offs.x;
int yo = _vp_move_offs.y;
if (abs(xo) >= width || abs(yo) >= height) {
/* fully_outside */
RedrawScreenRect(left, top, left + width, top + height);
return;
}
GfxScroll(left, top, width, height, xo, yo);
if (xo > 0) {
RedrawScreenRect(left, top, xo + left, top + height);
left += xo;
width -= xo;
} else if (xo < 0) {
RedrawScreenRect(left + width + xo, top, left + width, top + height);
width += xo;
}
if (yo > 0) {
RedrawScreenRect(left, top, width + left, top + yo);
} else if (yo < 0) {
RedrawScreenRect(left, top + height + yo, width + left, top + height);
}
}
}
static void SetViewportPosition(Window *w, int x, int y)
{
Viewport *vp = w->viewport;
int old_left = vp->virtual_left;
int old_top = vp->virtual_top;
int i;
int left, top, width, height;
vp->virtual_left = x;
vp->virtual_top = y;
/* Viewport is bound to its left top corner, so it must be rounded down (UnScaleByZoomLower)
* else glitch described in FS#1412 will happen (offset by 1 pixel with zoom level > NORMAL)
*/
old_left = UnScaleByZoomLower(old_left, vp->zoom);
old_top = UnScaleByZoomLower(old_top, vp->zoom);
x = UnScaleByZoomLower(x, vp->zoom);
y = UnScaleByZoomLower(y, vp->zoom);
old_left -= x;
old_top -= y;
if (old_top == 0 && old_left == 0) return;
_vp_move_offs.x = old_left;
_vp_move_offs.y = old_top;
left = vp->left;
top = vp->top;
width = vp->width;
height = vp->height;
if (left < 0) {
width += left;
left = 0;
}
i = left + width - _screen.width;
if (i >= 0) width -= i;
if (width > 0) {
if (top < 0) {
height += top;
top = 0;
}
i = top + height - _screen.height;
if (i >= 0) height -= i;
if (height > 0) {
Window::IteratorToFront it(w);
++it;
DoSetViewportPosition(it, left, top, width, height);
}
}
}
/**
* Is a xy position inside the viewport of the window?
* @param w Window to examine its viewport
* @param x X coordinate of the xy position
* @param y Y coordinate of the xy position
* @return Pointer to the viewport if the xy position is in the viewport of the window,
* otherwise \c nullptr is returned.
*/
Viewport *IsPtInWindowViewport(const Window *w, int x, int y)
{
Viewport *vp = w->viewport;
if (vp != nullptr &&
IsInsideMM(x, vp->left, vp->left + vp->width) &&
IsInsideMM(y, vp->top, vp->top + vp->height))
return vp;
return nullptr;
}
/**
* Translate screen coordinate in a viewport to underlying tile coordinate.
*
* Returns exact point of the map that is visible in the given place
* of the viewport (3D perspective), height of tiles and foundations matter.
*
* @param vp Viewport that contains the (\a x, \a y) screen coordinate
* @param x Screen x coordinate, distance in pixels from the left edge of viewport frame
* @param y Screen y coordinate, distance in pixels from the top edge of viewport frame
* @param clamp_to_map Clamp the coordinate outside of the map to the closest, non-void tile within the map
* @return Tile coordinate or (-1, -1) if given x or y is not within viewport frame
*/
Point TranslateXYToTileCoord(const Viewport *vp, int x, int y, bool clamp_to_map)
{
if (!IsInsideBS(x, vp->left, vp->width) || !IsInsideBS(y, vp->top, vp->height)) {
Point pt = { -1, -1 };
return pt;
}
return InverseRemapCoords2(
ScaleByZoom(x - vp->left, vp->zoom) + vp->virtual_left,
ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top, clamp_to_map);
}
/* When used for zooming, check area below current coordinates (x,y)
* and return the tile of the zoomed out/in position (zoom_x, zoom_y)
* when you just want the tile, make x = zoom_x and y = zoom_y */
static Point GetTileFromScreenXY(int x, int y, int zoom_x, int zoom_y)
{
Window *w;
Viewport *vp;
Point pt;
if ( (w = FindWindowFromPt(x, y)) != nullptr &&
(vp = IsPtInWindowViewport(w, x, y)) != nullptr)
return TranslateXYToTileCoord(vp, zoom_x, zoom_y);
pt.y = pt.x = -1;
return pt;
}
Point GetTileBelowCursor()
{
return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y);
}
Point GetTileZoomCenterWindow(bool in, Window * w)
{
int x, y;
Viewport *vp = w->viewport;
if (in) {
x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2);
y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2);
} else {
x = vp->width - (_cursor.pos.x - vp->left);
y = vp->height - (_cursor.pos.y - vp->top);
}
/* Get the tile below the cursor and center on the zoomed-out center */
return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top);
}
/**
* Update the status of the zoom-buttons according to the zoom-level
* of the viewport. This will update their status and invalidate accordingly
* @param w Window pointer to the window that has the zoom buttons
* @param vp pointer to the viewport whose zoom-level the buttons represent
* @param widget_zoom_in widget index for window with zoom-in button
* @param widget_zoom_out widget index for window with zoom-out button
*/
void HandleZoomMessage(Window *w, const Viewport *vp, byte widget_zoom_in, byte widget_zoom_out)
{
w->SetWidgetDisabledState(widget_zoom_in, vp->zoom <= _settings_client.gui.zoom_min);
w->SetWidgetDirty(widget_zoom_in);
w->SetWidgetDisabledState(widget_zoom_out, vp->zoom >= _settings_client.gui.zoom_max);
w->SetWidgetDirty(widget_zoom_out);
}
/**
* Schedules a tile sprite for drawing.
*
* @param image the image to draw.
* @param pal the provided palette.
* @param x position x (world coordinates) of the sprite.
* @param y position y (world coordinates) of the sprite.
* @param z position z (world coordinates) of the sprite.
* @param sub Only draw a part of the sprite.
* @param extra_offs_x Pixel X offset for the sprite position.
* @param extra_offs_y Pixel Y offset for the sprite position.
*/
static void AddTileSpriteToDraw(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub = nullptr, int extra_offs_x = 0, int extra_offs_y = 0)
{
assert((image & SPRITE_MASK) < MAX_SPRITES);
TileSpriteToDraw &ts = _vd.tile_sprites_to_draw.emplace_back();
ts.image = image;
ts.pal = pal;
ts.sub = sub;
Point pt = RemapCoords(x, y, z);
ts.x = pt.x + extra_offs_x;
ts.y = pt.y + extra_offs_y;
}
/**
* Adds a child sprite to the active foundation.
*
* The pixel offset of the sprite relative to the ParentSprite is the sum of the offset passed to OffsetGroundSprite() and extra_offs_?.
*
* @param image the image to draw.
* @param pal the provided palette.
* @param sub Only draw a part of the sprite.
* @param foundation_part Foundation part.
* @param extra_offs_x Pixel X offset for the sprite position.
* @param extra_offs_y Pixel Y offset for the sprite position.
*/
static void AddChildSpriteToFoundation(SpriteID image, PaletteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y)
{
assert(IsInsideMM(foundation_part, 0, FOUNDATION_PART_END));
assert(_vd.foundation[foundation_part] != -1);
Point offs = _vd.foundation_offset[foundation_part];
/* Change the active ChildSprite list to the one of the foundation */
int *old_child = _vd.last_child;
_vd.last_child = _vd.last_foundation_child[foundation_part];
AddChildSpriteScreen(image, pal, offs.x + extra_offs_x, offs.y + extra_offs_y, false, sub, false);
/* Switch back to last ChildSprite list */
_vd.last_child = old_child;
}
/**
* Draws a ground sprite at a specific world-coordinate relative to the current tile.
* If the current tile is drawn on top of a foundation the sprite is added as child sprite to the "foundation"-ParentSprite.
*
* @param image the image to draw.
* @param pal the provided palette.
* @param x position x (world coordinates) of the sprite relative to current tile.
* @param y position y (world coordinates) of the sprite relative to current tile.
* @param z position z (world coordinates) of the sprite relative to current tile.
* @param sub Only draw a part of the sprite.
* @param extra_offs_x Pixel X offset for the sprite position.
* @param extra_offs_y Pixel Y offset for the sprite position.
*/
void DrawGroundSpriteAt(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
{
/* Switch to first foundation part, if no foundation was drawn */
if (_vd.foundation_part == FOUNDATION_PART_NONE) _vd.foundation_part = FOUNDATION_PART_NORMAL;
if (_vd.foundation[_vd.foundation_part] != -1) {
Point pt = RemapCoords(x, y, z);
AddChildSpriteToFoundation(image, pal, sub, _vd.foundation_part, pt.x + extra_offs_x * ZOOM_LVL_BASE, pt.y + extra_offs_y * ZOOM_LVL_BASE);
} else {
AddTileSpriteToDraw(image, pal, _cur_ti->x + x, _cur_ti->y + y, _cur_ti->z + z, sub, extra_offs_x * ZOOM_LVL_BASE, extra_offs_y * ZOOM_LVL_BASE);
}
}
/**
* Draws a ground sprite for the current tile.
* If the current tile is drawn on top of a foundation the sprite is added as child sprite to the "foundation"-ParentSprite.
*
* @param image the image to draw.
* @param pal the provided palette.
* @param sub Only draw a part of the sprite.
* @param extra_offs_x Pixel X offset for the sprite position.
* @param extra_offs_y Pixel Y offset for the sprite position.
*/
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
{
DrawGroundSpriteAt(image, pal, 0, 0, 0, sub, extra_offs_x, extra_offs_y);
}
/**
* Called when a foundation has been drawn for the current tile.
* Successive ground sprites for the current tile will be drawn as child sprites of the "foundation"-ParentSprite, not as TileSprites.
*
* @param x sprite x-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite.
* @param y sprite y-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite.
*/
void OffsetGroundSprite(int x, int y)
{
/* Switch to next foundation part */
switch (_vd.foundation_part) {
case FOUNDATION_PART_NONE:
_vd.foundation_part = FOUNDATION_PART_NORMAL;
break;
case FOUNDATION_PART_NORMAL:
_vd.foundation_part = FOUNDATION_PART_HALFTILE;
break;
default: NOT_REACHED();
}
/* _vd.last_child == nullptr if foundation sprite was clipped by the viewport bounds */
if (_vd.last_child != nullptr) _vd.foundation[_vd.foundation_part] = (uint)_vd.parent_sprites_to_draw.size() - 1;
_vd.foundation_offset[_vd.foundation_part].x = x * ZOOM_LVL_BASE;
_vd.foundation_offset[_vd.foundation_part].y = y * ZOOM_LVL_BASE;
_vd.last_foundation_child[_vd.foundation_part] = _vd.last_child;
}
/**
* Adds a child sprite to a parent sprite.
* In contrast to "AddChildSpriteScreen()" the sprite position is in world coordinates
*
* @param image the image to draw.
* @param pal the provided palette.
* @param x position x of the sprite.
* @param y position y of the sprite.
* @param z position z of the sprite.
* @param sub Only draw a part of the sprite.
*/
static void AddCombinedSprite(SpriteID image, PaletteID pal, int x, int y, int z, const SubSprite *sub)
{
Point pt = RemapCoords(x, y, z);
const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
if (pt.x + spr->x_offs >= _vd.dpi.left + _vd.dpi.width ||
pt.x + spr->x_offs + spr->width <= _vd.dpi.left ||
pt.y + spr->y_offs >= _vd.dpi.top + _vd.dpi.height ||
pt.y + spr->y_offs + spr->height <= _vd.dpi.top)
return;
const ParentSpriteToDraw &pstd = _vd.parent_sprites_to_draw.back();
AddChildSpriteScreen(image, pal, pt.x - pstd.left, pt.y - pstd.top, false, sub, false);
}
/**
* Draw a (transparent) sprite at given coordinates with a given bounding box.
* The bounding box extends from (x + bb_offset_x, y + bb_offset_y, z + bb_offset_z) to (x + w - 1, y + h - 1, z + dz - 1), both corners included.
* Bounding boxes with bb_offset_x == w or bb_offset_y == h or bb_offset_z == dz are allowed and produce thin slices.
*
* @note Bounding boxes are normally specified with bb_offset_x = bb_offset_y = bb_offset_z = 0. The extent of the bounding box in negative direction is
* defined by the sprite offset in the grf file.
* However if modifying the sprite offsets is not suitable (e.g. when using existing graphics), the bounding box can be tuned by bb_offset.
*
* @pre w >= bb_offset_x, h >= bb_offset_y, dz >= bb_offset_z. Else w, h or dz are ignored.
*
* @param image the image to combine and draw,
* @param pal the provided palette,
* @param x position X (world) of the sprite,
* @param y position Y (world) of the sprite,
* @param w bounding box extent towards positive X (world),
* @param h bounding box extent towards positive Y (world),
* @param dz bounding box extent towards positive Z (world),
* @param z position Z (world) of the sprite,
* @param transparent if true, switch the palette between the provided palette and the transparent palette,
* @param bb_offset_x bounding box extent towards negative X (world),
* @param bb_offset_y bounding box extent towards negative Y (world),
* @param bb_offset_z bounding box extent towards negative Z (world)
* @param sub Only draw a part of the sprite.
*/
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
{
int32 left, right, top, bottom;
assert((image & SPRITE_MASK) < MAX_SPRITES);
/* make the sprites transparent with the right palette */
if (transparent) {
SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
pal = PALETTE_TO_TRANSPARENT;
}
if (_vd.combine_sprites == SPRITE_COMBINE_ACTIVE) {
AddCombinedSprite(image, pal, x, y, z, sub);
return;
}
_vd.last_child = nullptr;
Point pt = RemapCoords(x, y, z);
int tmp_left, tmp_top, tmp_x = pt.x, tmp_y = pt.y;
/* Compute screen extents of sprite */
if (image == SPR_EMPTY_BOUNDING_BOX) {
left = tmp_left = RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x;
right = RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1;
top = tmp_top = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y;
bottom = RemapCoords(x + w , y + h , z + bb_offset_z).y + 1;
} else {
const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
left = tmp_left = (pt.x += spr->x_offs);
right = (pt.x + spr->width );
top = tmp_top = (pt.y += spr->y_offs);
bottom = (pt.y + spr->height);
}
if (_draw_bounding_boxes && (image != SPR_EMPTY_BOUNDING_BOX)) {
/* Compute maximal extents of sprite and its bounding box */
left = std::min(left , RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x);
right = std::max(right , RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1);
top = std::min(top , RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y);
bottom = std::max(bottom, RemapCoords(x + w , y + h , z + bb_offset_z).y + 1);
}
/* Do not add the sprite to the viewport, if it is outside */
if (left >= _vd.dpi.left + _vd.dpi.width ||
right <= _vd.dpi.left ||
top >= _vd.dpi.top + _vd.dpi.height ||
bottom <= _vd.dpi.top) {
return;
}
ParentSpriteToDraw &ps = _vd.parent_sprites_to_draw.emplace_back();
ps.x = tmp_x;
ps.y = tmp_y;
ps.left = tmp_left;
ps.top = tmp_top;
ps.image = image;
ps.pal = pal;
ps.sub = sub;
ps.xmin = x + bb_offset_x;
ps.xmax = x + std::max(bb_offset_x, w) - 1;
ps.ymin = y + bb_offset_y;
ps.ymax = y + std::max(bb_offset_y, h) - 1;
ps.zmin = z + bb_offset_z;
ps.zmax = z + std::max(bb_offset_z, dz) - 1;
ps.first_child = -1;
_vd.last_child = &ps.first_child;
if (_vd.combine_sprites == SPRITE_COMBINE_PENDING) _vd.combine_sprites = SPRITE_COMBINE_ACTIVE;
}
/**
* Starts a block of sprites, which are "combined" into a single bounding box.
*
* Subsequent calls to #AddSortableSpriteToDraw will be drawn into the same bounding box.
* That is: The first sprite that is not clipped by the viewport defines the bounding box, and
* the following sprites will be child sprites to that one.
*
* That implies:
* - The drawing order is definite. No other sprites will be sorted between those of the block.
* - You have to provide a valid bounding box for all sprites,
* as you won't know which one is the first non-clipped one.
* Preferable you use the same bounding box for all.
* - You cannot use #AddChildSpriteScreen inside the block, as its result will be indefinite.
*
* The block is terminated by #EndSpriteCombine.
*
* You cannot nest "combined" blocks.
*/
void StartSpriteCombine()
{
assert(_vd.combine_sprites == SPRITE_COMBINE_NONE);
_vd.combine_sprites = SPRITE_COMBINE_PENDING;
}
/**
* Terminates a block of sprites started by #StartSpriteCombine.
* Take a look there for details.
*/
void EndSpriteCombine()
{
assert(_vd.combine_sprites != SPRITE_COMBINE_NONE);
_vd.combine_sprites = SPRITE_COMBINE_NONE;
}
/**
* Check if the parameter "check" is inside the interval between
* begin and end, including both begin and end.
* @note Whether \c begin or \c end is the biggest does not matter.
* This method will account for that.
* @param begin The begin of the interval.
* @param end The end of the interval.
* @param check The value to check.
*/
static bool IsInRangeInclusive(int begin, int end, int check)
{
if (begin > end) Swap(begin, end);
return begin <= check && check <= end;
}
/**
* Checks whether a point is inside the selected a diagonal rectangle given by _thd.size and _thd.pos
* @param x The x coordinate of the point to be checked.
* @param y The y coordinate of the point to be checked.
* @return True if the point is inside the rectangle, else false.
*/
bool IsInsideRotatedRectangle(int x, int y)
{
int dist_a = (_thd.size.x + _thd.size.y); // Rotated coordinate system for selected rectangle.
int dist_b = (_thd.size.x - _thd.size.y); // We don't have to divide by 2. It's all relative!
int a = ((x - _thd.pos.x) + (y - _thd.pos.y)); // Rotated coordinate system for the point under scrutiny.
int b = ((x - _thd.pos.x) - (y - _thd.pos.y));
/* Check if a and b are between 0 and dist_a or dist_b respectively. */
return IsInRangeInclusive(dist_a, 0, a) && IsInRangeInclusive(dist_b, 0, b);
}
/**
* Add a child sprite to a parent sprite.
*
* @param image the image to draw.
* @param pal the provided palette.
* @param x sprite x-offset (screen coordinates) relative to parent sprite.
* @param y sprite y-offset (screen coordinates) relative to parent sprite.
* @param transparent if true, switch the palette between the provided palette and the transparent palette,
* @param sub Only draw a part of the sprite.
*/
void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub, bool scale)
{
assert((image & SPRITE_MASK) < MAX_SPRITES);
/* If the ParentSprite was clipped by the viewport bounds, do not draw the ChildSprites either */
if (_vd.last_child == nullptr) return;
/* make the sprites transparent with the right palette */
if (transparent) {
SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
pal = PALETTE_TO_TRANSPARENT;
}
*_vd.last_child = (uint)_vd.child_screen_sprites_to_draw.size();
ChildScreenSpriteToDraw &cs = _vd.child_screen_sprites_to_draw.emplace_back();
cs.image = image;
cs.pal = pal;
cs.sub = sub;
cs.x = scale ? x * ZOOM_LVL_BASE : x;
cs.y = scale ? y * ZOOM_LVL_BASE : y;
cs.next = -1;
/* Append the sprite to the active ChildSprite list.
* If the active ParentSprite is a foundation, update last_foundation_child as well.
* Note: ChildSprites of foundations are NOT sequential in the vector, as selection sprites are added at last. */
if (_vd.last_foundation_child[0] == _vd.last_child) _vd.last_foundation_child[0] = &cs.next;
if (_vd.last_foundation_child[1] == _vd.last_child) _vd.last_foundation_child[1] = &cs.next;
_vd.last_child = &cs.next;
}
static void AddStringToDraw(int x, int y, StringID string, uint64 params_1, uint64 params_2, Colours colour, uint16 width)
{
assert(width != 0);
StringSpriteToDraw &ss = _vd.string_sprites_to_draw.emplace_back();
ss.string = string;
ss.x = x;
ss.y = y;
ss.params[0] = params_1;
ss.params[1] = params_2;
ss.width = width;
ss.colour = colour;
}
/**
* Draws sprites between ground sprite and everything above.
*
* The sprite is either drawn as TileSprite or as ChildSprite of the active foundation.
*
* @param image the image to draw.
* @param pal the provided palette.
* @param ti TileInfo Tile that is being drawn
* @param z_offset Z offset relative to the groundsprite. Only used for the sprite position, not for sprite sorting.
* @param foundation_part Foundation part the sprite belongs to.
*/
static void DrawSelectionSprite(SpriteID image, PaletteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part)
{
/* FIXME: This is not totally valid for some autorail highlights that extend over the edges of the tile. */
if (_vd.foundation[foundation_part] == -1) {
/* draw on real ground */
AddTileSpriteToDraw(image, pal, ti->x, ti->y, ti->z + z_offset);
} else {
/* draw on top of foundation */
AddChildSpriteToFoundation(image, pal, nullptr, foundation_part, 0, -z_offset * ZOOM_LVL_BASE);
}
}
/**
* Draws a selection rectangle on a tile.
*
* @param ti TileInfo Tile that is being drawn
* @param pal Palette to apply.
*/
static void DrawTileSelectionRect(const TileInfo *ti, PaletteID pal)
{
if (!IsValidTile(ti->tile)) return;
SpriteID sel;
if (IsHalftileSlope(ti->tileh)) {
Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
SpriteID sel2 = SPR_HALFTILE_SELECTION_FLAT + halftile_corner;
DrawSelectionSprite(sel2, pal, ti, 7 + TILE_HEIGHT, FOUNDATION_PART_HALFTILE);
Corner opposite_corner = OppositeCorner(halftile_corner);
if (IsSteepSlope(ti->tileh)) {
sel = SPR_HALFTILE_SELECTION_DOWN;
} else {
sel = ((ti->tileh & SlopeWithOneCornerRaised(opposite_corner)) != 0 ? SPR_HALFTILE_SELECTION_UP : SPR_HALFTILE_SELECTION_FLAT);
}
sel += opposite_corner;
} else {
sel = SPR_SELECT_TILE + SlopeToSpriteOffset(ti->tileh);
}
DrawSelectionSprite(sel, pal, ti, 7, FOUNDATION_PART_NORMAL);
}
static bool IsPartOfAutoLine(int px, int py)
{
px -= _thd.selstart.x;
py -= _thd.selstart.y;
if ((_thd.drawstyle & HT_DRAG_MASK) != HT_LINE) return false;
switch (_thd.drawstyle & HT_DIR_MASK) {
case HT_DIR_X: return py == 0; // x direction
case HT_DIR_Y: return px == 0; // y direction
case HT_DIR_HU: return px == -py || px == -py - 16; // horizontal upper
case HT_DIR_HL: return px == -py || px == -py + 16; // horizontal lower
case HT_DIR_VL: return px == py || px == py + 16; // vertical left
case HT_DIR_VR: return px == py || px == py - 16; // vertical right
default:
NOT_REACHED();
}
}
/* [direction][side] */
static const HighLightStyle _autorail_type[6][2] = {
{ HT_DIR_X, HT_DIR_X },
{ HT_DIR_Y, HT_DIR_Y },
{ HT_DIR_HU, HT_DIR_HL },
{ HT_DIR_HL, HT_DIR_HU },
{ HT_DIR_VL, HT_DIR_VR },
{ HT_DIR_VR, HT_DIR_VL }
};
#include "table/autorail.h"
/**
* Draws autorail highlights.
*
* @param *ti TileInfo Tile that is being drawn
* @param autorail_type Offset into _AutorailTilehSprite[][]
*/
static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type, bool multi_tile)
{
std::unordered_set<TileIndex> tiles_to_draw_set;
std::unordered_set<TileIndex> tiles_to_highlight_set;
const TileIndex selection_start = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
const TileIndex selection_end = TileVirtXY(_thd.selend.x, _thd.selend.y);
const DiagDirection selection_dir = DiagdirBetweenTiles(selection_start, selection_end);
const bool selection_dir_is_diagonal = (selection_dir != DiagDirection::INVALID_DIAGDIR);
/* Determine which parts of tunnels and bridges should be drawn */
if (multi_tile && selection_dir_is_diagonal) {
int steps = DistanceManhattan(selection_start, selection_end);
if (selection_dir != DiagDirection::INVALID_DIAGDIR) {
TileIndex tile = selection_start;
for (int step = 0; step <= steps; step++) {
if (IsBridgeTile(tile) || IsTunnelTile(tile)) {
if (selection_dir == GetTunnelBridgeDirection(tile)) {
const TileIndex tile_other_end = GetOtherTunnelBridgeEnd(tile);
tiles_to_highlight_set.insert(tile);
tiles_to_highlight_set.insert(tile_other_end);
step += 2 + GetTunnelBridgeLength(tile, tile_other_end);
tile = TileAddByDiagDir(tile_other_end, selection_dir);
}
}
tiles_to_draw_set.insert(tile);
tile = TileAddByDiagDir(tile, selection_dir);
}
}
}
SpriteID image;
PaletteID pal;
int offset;
FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
Slope autorail_tileh = RemoveHalftileSlope(ti->tileh);
if (IsHalftileSlope(ti->tileh)) {
static const uint _lower_rail[4] = { 5U, 2U, 4U, 3U };
Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
if (autorail_type != _lower_rail[halftile_corner]) {
foundation_part = FOUNDATION_PART_HALFTILE;
/* Here we draw the highlights of the "three-corners-raised"-slope. That looks ok to me. */
autorail_tileh = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));