-
Notifications
You must be signed in to change notification settings - Fork 4
/
CSurface.cpp
1491 lines (1365 loc) · 58.7 KB
/
CSurface.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
// Copyright (C) 2009 - 2021 Marc Vester (XaserLE)
// Copyright (C) 2009 - 2021 Settlers Freaks <sf-team at siedler25.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "CSurface.h"
#include "CGame.h"
#include "CMap.h"
#include "Rect.h"
#include "SGE/sge_blib.h"
#include "SGE/sge_rotation.h"
#include "globals.h"
#include "gameData/EdgeDesc.h"
#include "gameData/TerrainDesc.h"
#include <algorithm>
#include <cassert>
#include <cmath>
namespace {
SDL_Rect rect2SDL_Rect(const Rect& rect)
{
Point<Sint16> origin(rect.getOrigin());
Point<Uint16> size(rect.getSize());
SDL_Rect result;
result.x = origin.x;
result.y = origin.y;
result.w = size.x;
result.h = size.y;
return result;
}
void DrawPreCalcFadedTexturedTrigon(SDL_Surface* dest, const Point16& p1, const Point16& p2, const Point16& p3,
SDL_Surface* source, const SDL_Rect& rect, Uint16 I1, Uint16 I2,
Uint8 PreCalcPalettes[][256])
{
Sint16 right = rect.x + rect.w - 1;
Sint16 middle = rect.x + rect.w / Sint16(2);
Sint16 bottom = rect.y + rect.h - 1;
Uint32 colorKey;
const int keycount = (SDL_GetColorKey(source, &colorKey) < 0) ? 0 : 1;
sge_PreCalcFadedTexturedTrigonColorKeys(dest, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, source, rect.x, rect.y, right,
rect.y, middle, bottom, I1, I2, I2, PreCalcPalettes, &colorKey, keycount);
}
void DrawFadedTexturedTrigon(SDL_Surface* dest, const Point16& p1, const Point16& p2, const Point16& p3,
SDL_Surface* source, const SDL_Rect& rect, Sint32 I1, Sint32 I2)
{
Sint16 right = rect.x + rect.w - 1;
Sint16 middle = rect.x + rect.w / Sint16(2);
Sint16 bottom = rect.y + rect.h - 1;
Uint32 colorKey;
const int keycount = (SDL_GetColorKey(source, &colorKey) < 0) ? 0 : 1;
sge_FadedTexturedTrigonColorKeys(dest, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, source, rect.x, rect.y, right, rect.y,
middle, bottom, I1, I2, I2, &colorKey, keycount);
}
} // namespace
bool CSurface::drawTextures = false;
bool CSurface::Draw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y)
{
if(!Surf_Dest || !Surf_Src)
return false;
SDL_Rect DestR;
DestR.x = X;
DestR.y = Y;
SDL_BlitSurface(Surf_Src, nullptr, Surf_Dest, &DestR);
return true;
}
bool CSurface::Draw(SDL_Surface* Surf_Dest, SdlSurface& Surf_Src, int X, int Y)
{
return Draw(Surf_Dest, Surf_Src.get(), X, Y);
}
bool CSurface::Draw(SdlSurface& Surf_Dest, SdlSurface& Surf_Src, Position pos)
{
return Draw(Surf_Dest.get(), Surf_Src.get(), pos.x, pos.y);
}
bool CSurface::Draw(SdlSurface& Surf_Dest, SDL_Surface* Surf_Src, int X, int Y)
{
return Draw(Surf_Dest.get(), Surf_Src, X, Y);
}
bool CSurface::Draw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, int angle)
{
if(!Surf_Dest || !Surf_Src)
return false;
Uint16 px, py;
switch(angle)
{
case 90:
px = 0;
py = Surf_Src->h - 1;
break;
case 180:
px = Surf_Src->w - 1;
py = Surf_Src->h - 1;
break;
case 270:
px = Surf_Src->w - 1;
py = 0;
break;
default: return false;
}
sge_transform(Surf_Src, Surf_Dest, (float)angle, 1.0, 1.0, px, py, X, Y, SGE_TSAFE);
return true;
}
bool CSurface::Draw(SdlSurface& Surf_Dest, SdlSurface& Surf_Src, int X, int Y, int angle)
{
return Draw(Surf_Dest.get(), Surf_Src.get(), X, Y, angle);
}
bool CSurface::Draw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, int X2, int Y2, int W, int H)
{
if(!Surf_Dest || !Surf_Src)
return false;
SDL_Rect DestR;
DestR.x = X;
DestR.y = Y;
SDL_Rect SrcR;
SrcR.x = X2;
SrcR.y = Y2;
SrcR.w = W;
SrcR.h = H;
SDL_BlitSurface(Surf_Src, &SrcR, Surf_Dest, &DestR);
return true;
}
bool CSurface::Draw(SDL_Surface* Surf_Dest, SdlSurface& Surf_Src, int X, int Y, int X2, int Y2, int W, int H)
{
return Draw(Surf_Dest, Surf_Src.get(), X, Y, X2, Y2, W, H);
}
// this is the example function from the SDL-documentation to draw pixels
void CSurface::DrawPixel_Color(SDL_Surface* screen, int x, int y, Uint32 color)
{
int bpp = screen->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8* p = (Uint8*)screen->pixels + y * screen->pitch + x * bpp;
if(SDL_MUSTLOCK(screen))
SDL_LockSurface(screen);
switch(bpp)
{
case 1: *p = color; break;
case 2: *(Uint16*)p = color; break;
case 3:
if((SDL_BYTEORDER) == SDL_LIL_ENDIAN)
{
p[0] = color;
p[1] = color >> 8;
p[2] = color >> 16;
} else
{
p[2] = color;
p[1] = color >> 8;
p[0] = color >> 16;
}
break;
case 4: *(Uint32*)p = color; break;
}
if(SDL_MUSTLOCK(screen))
SDL_UnlockSurface(screen);
}
// this is the example function from the sdl-documentation to draw pixels
void CSurface::DrawPixel_RGB(SDL_Surface* screen, int x, int y, Uint8 R, Uint8 G, Uint8 B)
{
DrawPixel_Color(screen, x, y, SDL_MapRGB(screen->format, R, G, B));
}
void CSurface::DrawPixel_RGBA(SDL_Surface* screen, int x, int y, Uint8 R, Uint8 G, Uint8 B, Uint8 A)
{
DrawPixel_Color(screen, x, y, SDL_MapRGBA(screen->format, R, G, B, A));
}
Uint32 CSurface::GetPixel(SDL_Surface* surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8* p = (Uint8*)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp)
{
case 1: return *p;
case 2: return *(Uint16*)p;
case 3:
if((SDL_BYTEORDER) == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4: return *(Uint32*)p; //-V206
default: return 0; /* shouldn't happen, but avoids warnings */
}
}
void CSurface::DrawTriangleField(SDL_Surface* display, const DisplayRectangle& displayRect, const bobMAP& myMap)
{
Uint16 width = myMap.width;
Uint16 height = myMap.height;
auto type = myMap.type;
MapNode tempP1, tempP2, tempP3;
// min size to avoid underflows
if(width < 8 || height < 8)
return;
assert(displayRect.left < myMap.width_pixel);
assert(displayRect.right > 0);
assert(displayRect.top < myMap.height_pixel);
assert(displayRect.bottom > 0);
Uint8 maxH = 0;
for(int y = 0; y < height; ++y)
{
for(int x = 0; x < width; ++x)
{
maxH = std::max(myMap.getVertex(x, y).h, maxH);
}
}
const int additionalRows = TRIANGLE_INCREASE * std::max(0, maxH - 0x0A) / TRIANGLE_HEIGHT;
// draw triangle field
// NOTE: WE DO THIS TWICE, AT FIRST ONLY TRIANGLE-TEXTURES, AT SECOND THE TEXTURE-BORDERS AND OBJECTS
for(int i = 0; i < 2; i++)
{
drawTextures = (i == 0);
for(int k = 0; k < 4; k++)
{
// beware calling DrawTriangle for each triangle
// IMPORTANT: integer values like +8 or -1 are for tolerance to beware of high triangles are not shown
// at first call DrawTriangle for all triangles inside the map edges
int row_start = std::max(displayRect.top, 2 * TRIANGLE_HEIGHT) / TRIANGLE_HEIGHT - 2;
int row_end = (displayRect.bottom) / TRIANGLE_HEIGHT + additionalRows;
int col_start = std::max<int>(displayRect.left, TRIANGLE_WIDTH) / TRIANGLE_WIDTH - 1;
int col_end = (displayRect.right) / TRIANGLE_WIDTH + 1;
bool view_outside_edges;
if(k > 0)
{
// now call DrawTriangle for all triangles outside the map edges
view_outside_edges = false;
if(k == 1 || k == 3)
{
// at first call DrawTriangle for all triangles up or down outside
if(displayRect.top < 0)
{
row_start = std::max(0, height - 1 - (-displayRect.top / TRIANGLE_HEIGHT) - 1);
row_end = height - 1;
view_outside_edges = true;
} else if(displayRect.bottom > myMap.height_pixel)
{
row_start = 0;
row_end = (displayRect.bottom - myMap.height_pixel) / TRIANGLE_HEIGHT + 8;
view_outside_edges = true;
} else if(displayRect.top <= 2 * TRIANGLE_HEIGHT)
{
// this is for draw triangles that are reduced under the lower map edge (have bigger y-coords as
// myMap.height_pixel)
row_start = height - 3;
row_end = height - 1;
view_outside_edges = true;
} else if(displayRect.bottom >= (myMap.height_pixel - 8 * TRIANGLE_HEIGHT))
{
// this is for draw triangles that are raised over the upper map edge (have negative y-coords)
row_start = 0;
row_end = 8;
view_outside_edges = true;
}
}
if(k == 2 || k == 3)
{
// now call DrawTriangle for all triangles left or right outside
if(displayRect.left <= 0)
{
col_start = std::max(0, width - 1 - (-displayRect.left / TRIANGLE_WIDTH) - 1);
col_end = width - 1;
view_outside_edges = true;
} else if(displayRect.left < TRIANGLE_WIDTH)
{
col_start = width - 2;
col_end = width - 1;
view_outside_edges = true;
} else if(displayRect.right > myMap.width_pixel)
{
col_start = 0;
col_end = (displayRect.right - myMap.width_pixel) / TRIANGLE_WIDTH + 1;
view_outside_edges = true;
}
}
// if displayRect is not outside the map edges, there is nothing to do
if(!view_outside_edges)
continue;
}
assert(col_start >= 0);
assert(row_start >= 0);
assert(col_start <= col_end);
assert(row_start <= row_end);
for(unsigned y = row_start; y < height - 1u && y <= static_cast<unsigned>(row_end); y++)
{
if(y % 2 == 0)
{
// first RightSideUp
tempP2 = myMap.getVertex(width - 1, y + 1);
tempP2.x = 0;
DrawTriangle(display, displayRect, myMap, type, myMap.getVertex(0, y), tempP2,
myMap.getVertex(0, y + 1));
for(unsigned x = std::max(col_start, 1); x < width && x <= static_cast<unsigned>(col_end); x++)
{
// RightSideUp
DrawTriangle(display, displayRect, myMap, type, myMap.getVertex(x, y),
myMap.getVertex(x - 1, y + 1), myMap.getVertex(x, y + 1));
// UpSideDown
DrawTriangle(display, displayRect, myMap, type, myMap.getVertex(x - 1, y + 1),
myMap.getVertex(x - 1, y), myMap.getVertex(x, y));
}
// last UpSideDown
tempP3 = myMap.getVertex(0, y);
tempP3.x = myMap.getVertex(width - 1, y).x + TRIANGLE_WIDTH;
DrawTriangle(display, displayRect, myMap, type, myMap.getVertex(width - 1, y + 1),
myMap.getVertex(width - 1, y), tempP3);
} else
{
for(unsigned x = col_start; x < width - 1u && x <= static_cast<unsigned>(col_end); x++)
{
// RightSideUp
DrawTriangle(display, displayRect, myMap, type, myMap.getVertex(x, y),
myMap.getVertex(x, y + 1), myMap.getVertex(x + 1, y + 1));
// UpSideDown
DrawTriangle(display, displayRect, myMap, type, myMap.getVertex(x + 1, y + 1),
myMap.getVertex(x, y), myMap.getVertex(x + 1, y));
}
// last RightSideUp
tempP3 = myMap.getVertex(0, y + 1);
tempP3.x = myMap.getVertex(width - 1, y + 1).x + TRIANGLE_WIDTH;
DrawTriangle(display, displayRect, myMap, type, myMap.getVertex(width - 1, y),
myMap.getVertex(width - 1, y + 1), tempP3);
// last UpSideDown
tempP1 = myMap.getVertex(0, y + 1);
tempP1.x = myMap.getVertex(width - 1, y + 1).x + TRIANGLE_WIDTH;
tempP3 = myMap.getVertex(0, y);
tempP3.x = myMap.getVertex(width - 1, y).x + TRIANGLE_WIDTH;
DrawTriangle(display, displayRect, myMap, type, tempP1, myMap.getVertex(width - 1, y), tempP3);
}
}
// draw last line
for(unsigned x = col_start; x < width - 1u && x <= static_cast<unsigned>(col_end); x++)
{
// RightSideUp
tempP2 = myMap.getVertex(x, 0);
tempP2.y = height * TRIANGLE_HEIGHT + myMap.getVertex(x, 0).y;
tempP3 = myMap.getVertex(x + 1, 0);
tempP3.y = height * TRIANGLE_HEIGHT + myMap.getVertex(x + 1, 0).y;
DrawTriangle(display, displayRect, myMap, type, myMap.getVertex(x, height - 1), tempP2, tempP3);
// UpSideDown
tempP1 = myMap.getVertex(x + 1, 0);
tempP1.y = height * TRIANGLE_HEIGHT + myMap.getVertex(x + 1, 0).y;
DrawTriangle(display, displayRect, myMap, type, tempP1, myMap.getVertex(x, height - 1),
myMap.getVertex(x + 1, height - 1));
}
}
// last RightSideUp
tempP2 = myMap.getVertex(width - 1, 0);
tempP2.y += height * TRIANGLE_HEIGHT;
tempP3 = myMap.getVertex(0, 0);
tempP3.x = myMap.getVertex(width - 1, 0).x + TRIANGLE_WIDTH;
tempP3.y += height * TRIANGLE_HEIGHT;
DrawTriangle(display, displayRect, myMap, type, myMap.getVertex(width - 1, height - 1), tempP2, tempP3);
// last UpSideDown
tempP1 = myMap.getVertex(0, 0);
tempP1.x = myMap.getVertex(width - 1, 0).x + TRIANGLE_WIDTH;
tempP1.y += height * TRIANGLE_HEIGHT;
tempP3 = myMap.getVertex(0, height - 1);
tempP3.x = myMap.getVertex(width - 1, height - 1).x + TRIANGLE_WIDTH;
DrawTriangle(display, displayRect, myMap, type, tempP1, myMap.getVertex(width - 1, height - 1), tempP3);
}
}
namespace {
enum class BorderPreference
{
None,
LeftTop,
RightBottom
};
BorderPreference CalcBorders(const bobMAP& map, Uint8 s2Id1, Uint8 s2Id2, SDL_Rect& borderRect)
{
// we have to decide which border to blit, "left or right" or "top or bottom"
s2Id1 &= ~(0x40 | 0x80);
s2Id2 &= ~(0x40 | 0x80);
assert(s2Id1 < map.s2IdToTerrain.size());
assert(s2Id2 < map.s2IdToTerrain.size());
DescIdx<TerrainDesc> idxTop = map.s2IdToTerrain[s2Id1];
DescIdx<TerrainDesc> idxBottom = map.s2IdToTerrain[s2Id2];
if(idxTop == idxBottom)
return BorderPreference::None;
const TerrainDesc& t1 = global::worldDesc.get(idxTop);
const TerrainDesc& t2 = global::worldDesc.get(idxBottom);
if(t1.edgePriority > t2.edgePriority)
{
if(!t1.edgeType)
return BorderPreference::None;
borderRect = rect2SDL_Rect(global::worldDesc.get(t1.edgeType).posInTexture);
return BorderPreference::LeftTop;
} else if(t1.edgePriority < t2.edgePriority)
{
if(!t2.edgeType)
return BorderPreference::None;
borderRect = rect2SDL_Rect(global::worldDesc.get(t2.edgeType).posInTexture);
return BorderPreference::RightBottom;
}
return BorderPreference::None;
}
template<typename T>
constexpr bool isInRange(T val, T min, T max)
{
return val >= min && val <= max;
}
/// Return true if triangle is drawn
bool GetAdjustedPoints(const DisplayRectangle& displayRect, const bobMAP& myMap, Point32& p1, Point32& p2, Point32& p3)
{
if((!isInRange(p1.x, displayRect.left, displayRect.right) && !isInRange(p2.x, displayRect.left, displayRect.right)
&& !isInRange(p3.x, displayRect.left, displayRect.right))
|| (!isInRange(p1.y, displayRect.top, displayRect.bottom)
&& !isInRange(p2.y, displayRect.top, displayRect.bottom)
&& !isInRange(p3.y, displayRect.top, displayRect.bottom)))
{
bool triangle_shown = false;
if(displayRect.left <= 0)
{
int outside_left = displayRect.left;
int outside_right = 0;
if(isInRange(p1.x - myMap.width_pixel, outside_left, outside_right)
|| isInRange(p2.x - myMap.width_pixel, outside_left, outside_right)
|| isInRange(p3.x - myMap.width_pixel, outside_left, outside_right))
{
p1.x -= myMap.width_pixel;
p2.x -= myMap.width_pixel;
p3.x -= myMap.width_pixel;
triangle_shown = true;
}
} else if(displayRect.left < TRIANGLE_WIDTH)
{
int outside_left = displayRect.left;
int outside_right = displayRect.left + TRIANGLE_WIDTH;
if(isInRange(p1.x - myMap.width_pixel, outside_left, outside_right)
|| isInRange(p2.x - myMap.width_pixel, outside_left, outside_right)
|| isInRange(p3.x - myMap.width_pixel, outside_left, outside_right))
{
p1.x -= myMap.width_pixel;
p2.x -= myMap.width_pixel;
p3.x -= myMap.width_pixel;
triangle_shown = true;
}
} else if(displayRect.right > myMap.width_pixel)
{
int outside_left = myMap.width_pixel;
int outside_right = displayRect.right;
if(isInRange(p1.x + myMap.width_pixel, outside_left, outside_right)
|| isInRange(p2.x + myMap.width_pixel, outside_left, outside_right)
|| isInRange(p3.x + myMap.width_pixel, outside_left, outside_right))
{
p1.x += myMap.width_pixel;
p2.x += myMap.width_pixel;
p3.x += myMap.width_pixel;
triangle_shown = true;
}
}
if(displayRect.top < 0)
{
int outside_top = displayRect.top;
int outside_bottom = 0;
if(isInRange(p1.y - myMap.height_pixel, outside_top, outside_bottom)
|| isInRange(p2.y - myMap.height_pixel, outside_top, outside_bottom)
|| isInRange(p3.y - myMap.height_pixel, outside_top, outside_bottom))
{
p1.y -= myMap.height_pixel;
p2.y -= myMap.height_pixel;
p3.y -= myMap.height_pixel;
triangle_shown = true;
}
} else if(displayRect.bottom > myMap.height_pixel)
{
int outside_top = myMap.height_pixel;
int outside_bottom = displayRect.bottom;
if(isInRange(p1.y + myMap.height_pixel, outside_top, outside_bottom)
|| isInRange(p2.y + myMap.height_pixel, outside_top, outside_bottom)
|| isInRange(p3.y + myMap.height_pixel, outside_top, outside_bottom))
{
p1.y += myMap.height_pixel;
p2.y += myMap.height_pixel;
p3.y += myMap.height_pixel;
triangle_shown = true;
}
}
// now test if triangle has negative y-coords cause it's raised over the upper map edge
if(p1.y < 0 || p2.y < 0 || p3.y < 0)
{
if(isInRange(p1.y + myMap.height_pixel, displayRect.top, displayRect.bottom)
|| isInRange(p2.y + myMap.height_pixel, displayRect.top, displayRect.bottom)
|| isInRange(p3.y + myMap.height_pixel, displayRect.top, displayRect.bottom))
{
p1.y += myMap.height_pixel;
p2.y += myMap.height_pixel;
p3.y += myMap.height_pixel;
triangle_shown = true;
}
}
// now test if triangle has bigger y-coords as myMap.height_pixel cause it's reduced under the lower map edge
if(p1.y > myMap.height_pixel || p2.y > myMap.height_pixel || p3.y > myMap.height_pixel)
{
if(isInRange(p1.y - myMap.height_pixel, displayRect.top, displayRect.bottom)
|| isInRange(p2.y - myMap.height_pixel, displayRect.top, displayRect.bottom)
|| isInRange(p3.y - myMap.height_pixel, displayRect.top, displayRect.bottom))
{
p1.y -= myMap.height_pixel;
p2.y -= myMap.height_pixel;
p3.y -= myMap.height_pixel;
triangle_shown = true;
}
}
if(!triangle_shown)
return false;
}
p1 -= displayRect.getOrigin();
p2 -= displayRect.getOrigin();
p3 -= displayRect.getOrigin();
return true;
}
} // namespace
void CSurface::GetTerrainTextureCoords(MapType mapType, TriangleTerrainType texture, bool isRSU, int texture_move,
Point16& upper, Point16& left, Point16& right, Point16& upper2, Point16& left2,
Point16& right2)
{
const auto animOffset = Point16(-texture_move, texture_move);
switch(texture)
{
// in case of USD-Triangle "upper.x" and "upper.y" means "lowerX" and "lowerY"
case TRIANGLE_TEXTURE_STEPPE_MEADOW1:
upper = Point16(17, 96);
left = Point16(0, 126);
right = Point16(35, 126);
break;
case TRIANGLE_TEXTURE_MINING1:
upper = Point16(17, 48);
left = Point16(0, 78);
right = Point16(35, 78);
break;
case TRIANGLE_TEXTURE_SNOW:
if(isRSU)
{
upper = Point16(17, 0);
left = Point16(0, 30);
right = Point16(35, 30);
} else
{
upper = Point16(17, 28);
left = Point16(0, 0);
right = Point16(37, 0);
}
if(mapType == MAP_WINTERLAND)
{
if(isRSU)
{
upper2 = Point16(231, 61) + animOffset;
left2 = Point16(207, 62) + animOffset;
right2 = Point16(223, 78) + animOffset;
} else
{
upper2 = Point16(224, 79) + animOffset;
left2 = Point16(232, 62) + animOffset;
right2 = Point16(245, 76) + animOffset;
}
}
break;
case TRIANGLE_TEXTURE_SWAMP:
upper = Point16(113, 0);
left = Point16(96, 30);
right = Point16(131, 30);
if(mapType == MAP_WINTERLAND)
{
if(isRSU)
{
upper2 = Point16(231, 61) + animOffset;
left2 = Point16(207, 62) + animOffset;
right2 = Point16(223, 78) + animOffset;
} else
{
upper2 = Point16(224, 79) + animOffset;
left2 = Point16(232, 62) + animOffset;
right2 = Point16(245, 76) + animOffset;
}
}
break;
case TRIANGLE_TEXTURE_STEPPE:
case TRIANGLE_TEXTURE_STEPPE_:
case TRIANGLE_TEXTURE_STEPPE__:
case TRIANGLE_TEXTURE_STEPPE___:
upper = Point16(65, 0);
left = Point16(48, 30);
right = Point16(83, 30);
break;
case TRIANGLE_TEXTURE_WATER:
case TRIANGLE_TEXTURE_WATER_:
case TRIANGLE_TEXTURE_WATER__:
if(isRSU)
{
upper = Point16(231, 61) + animOffset;
left = Point16(207, 62) + animOffset;
right = Point16(223, 78) + animOffset;
} else
{
upper = Point16(224, 79) + animOffset;
left = Point16(232, 62) + animOffset;
right = Point16(245, 76) + animOffset;
}
break;
case TRIANGLE_TEXTURE_MEADOW1:
upper = Point16(65, 96);
left = Point16(48, 126);
right = Point16(83, 126);
break;
case TRIANGLE_TEXTURE_MEADOW2:
upper = Point16(113, 96);
left = Point16(96, 126);
right = Point16(131, 126);
break;
case TRIANGLE_TEXTURE_MEADOW3:
upper = Point16(161, 96);
left = Point16(144, 126);
right = Point16(179, 126);
break;
case TRIANGLE_TEXTURE_MINING2:
upper = Point16(65, 48);
left = Point16(48, 78);
right = Point16(83, 78);
break;
case TRIANGLE_TEXTURE_MINING3:
upper = Point16(113, 48);
left = Point16(96, 78);
right = Point16(131, 78);
break;
case TRIANGLE_TEXTURE_MINING4:
upper = Point16(161, 48);
left = Point16(144, 78);
right = Point16(179, 78);
break;
case TRIANGLE_TEXTURE_STEPPE_MEADOW2:
upper = Point16(17, 144);
left = Point16(0, 174);
right = Point16(35, 174);
break;
case TRIANGLE_TEXTURE_FLOWER:
upper = Point16(161, 0);
left = Point16(144, 30);
right = Point16(179, 30);
break;
case TRIANGLE_TEXTURE_LAVA:
if(isRSU)
{
upper = Point16(231, 117) + animOffset;
left = Point16(207, 118) + animOffset;
right = Point16(223, 134) + animOffset;
} else
{
upper = Point16(224, 135) + animOffset;
left = Point16(232, 118) + animOffset;
right = Point16(245, 132) + animOffset;
}
break;
case TRIANGLE_TEXTURE_MINING_MEADOW:
upper = Point16(65, 144);
left = Point16(48, 174);
right = Point16(83, 174);
break;
default: // TRIANGLE_TEXTURE_FLOWER
upper = Point16(161, 0);
left = Point16(144, 30);
right = Point16(179, 30);
break;
}
}
void CSurface::DrawTriangle(SDL_Surface* display, const DisplayRectangle& displayRect, const bobMAP& myMap,
MapType type, const MapNode& P1, const MapNode& P2, const MapNode& P3)
{
Point32 p1(P1.x, P1.y);
Point32 p2(P2.x, P2.y);
Point32 p3(P3.x, P3.y);
// prevent drawing triangles that are not shown
if(!GetAdjustedPoints(displayRect, myMap, p1, p2, p3))
return;
// for moving water, lava, objects and so on
// This is very tricky: there are ice floes in the winterland and the water under this floes is moving.
// I don't know how this works in original settlers 2 but i solved it this way:
// i texture the triangle with normal water and then draw the floe over it. To Extract the floe
// from it's surrounded water, i use this color keys below. These are the color values for the water texture.
// I wrote a special SGE-Function that uses these color keys and ignores them in the Surf_Tileset.
static std::array<Uint32, 5> colorkeys = {14191, 14195, 13167, 13159, 11119};
static int texture_move = 0;
static int roundCount = 0;
static Uint32 roundTimeObjects = SDL_GetTicks();
static Uint32 roundTimeTextures = SDL_GetTicks();
if(SDL_GetTicks() - roundTimeObjects > 30)
{
roundTimeObjects = SDL_GetTicks();
if(roundCount >= 7)
roundCount = 0;
else
roundCount++;
}
if(SDL_GetTicks() - roundTimeTextures > 170)
{
roundTimeTextures = SDL_GetTicks();
texture_move++;
if(texture_move > 14)
texture_move = 0;
}
SDL_Surface* Surf_Tileset;
switch(type)
{
case MAP_GREENLAND:
default:
Surf_Tileset = global::bmpArray[global::s2->getMapObj()->getBitsPerPixel() == 8 ? TILESET_GREENLAND_8BPP :
TILESET_GREENLAND_32BPP]
.surface.get();
break;
case MAP_WASTELAND:
Surf_Tileset = global::bmpArray[global::s2->getMapObj()->getBitsPerPixel() == 8 ? TILESET_WASTELAND_8BPP :
TILESET_WASTELAND_32BPP]
.surface.get();
break;
case MAP_WINTERLAND:
Surf_Tileset = global::bmpArray[global::s2->getMapObj()->getBitsPerPixel() == 8 ? TILESET_WINTERLAND_8BPP :
TILESET_WINTERLAND_32BPP]
.surface.get();
break;
}
bool const isRSU = p1.y < p2.y;
if(drawTextures)
{
// upper2, ..... are for special use in winterland.
Point16 upper, left, right, upper2, left2, right2;
auto const texture =
TriangleTerrainType((isRSU ? P1.rsuTexture : P2.usdTexture) & ~0x40); // Mask out harbor bit
GetTerrainTextureCoords(type, texture, isRSU, texture_move, upper, left, right, upper2, left2, right2);
// draw the triangle
// do not shade water and lava
if(texture == TRIANGLE_TEXTURE_WATER || texture == TRIANGLE_TEXTURE_LAVA)
sge_TexturedTrigon(display, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, Surf_Tileset, upper.x, upper.y, left.x,
left.y, right.x, right.y);
else
{
// draw special winterland textures with moving water (ice floe textures)
if(type == MAP_WINTERLAND && (texture == TRIANGLE_TEXTURE_SNOW || texture == TRIANGLE_TEXTURE_SWAMP))
{
sge_TexturedTrigon(display, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, Surf_Tileset, upper2.x, upper2.y,
left2.x, left2.y, right2.x, right2.y);
if(global::s2->getMapObj()->getBitsPerPixel() == 8)
sge_PreCalcFadedTexturedTrigonColorKeys(display, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, Surf_Tileset,
upper.x, upper.y, left.x, left.y, right.x, right.y,
P1.shading << 8, P2.shading << 8, P3.shading << 8,
gouData[type], colorkeys.data(), colorkeys.size());
else
sge_FadedTexturedTrigonColorKeys(display, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, Surf_Tileset, upper.x,
upper.y, left.x, left.y, right.x, right.y, P1.i, P2.i, P3.i,
colorkeys.data(), colorkeys.size());
} else
{
if(global::s2->getMapObj()->getBitsPerPixel() == 8)
sge_PreCalcFadedTexturedTrigon(display, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, Surf_Tileset, upper.x,
upper.y, left.x, left.y, right.x, right.y, P1.shading << 8,
P2.shading << 8, P3.shading << 8, gouData[type]);
else
sge_FadedTexturedTrigon(display, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, Surf_Tileset, upper.x, upper.y,
left.x, left.y, right.x, right.y, P1.i, P2.i, P3.i);
}
}
return;
}
// blit borders
/// PRIORITY FROM HIGH TO LOW: SNOW, MINING_MEADOW, STEPPE, STEPPE_MEADOW2, MINING, MEADOW, FLOWER, STEPPE_MEADOW1,
/// SWAMP, WATER, LAVA
if(global::s2->getMapObj()->getRenderBorders())
{
// RSU-Triangle
if(isRSU)
{
// left upper / right lower edge - therefore get the usd-texture from left to compare
Uint16 col = (P1.VertexX - 1 < 0 ? myMap.width - 1 : P1.VertexX - 1);
MapNode tempP = myMap.getVertex(col, P1.VertexY);
SDL_Rect BorderRect;
auto borderSide = CalcBorders(myMap, tempP.usdTexture, P1.rsuTexture, BorderRect);
if(borderSide != BorderPreference::None)
{
Point16 tmpP1{p1}, tmpP2{p2};
Point32 thirdPt;
if(borderSide == BorderPreference::LeftTop)
thirdPt = p3;
else
{
tmpP1 += Point16(1, 0);
tmpP2 += Point16(1, 0);
thirdPt = Point32(tempP.x, tempP.y) - displayRect.getOrigin();
// Shift it close to p1
auto diff = thirdPt - p1;
if(diff.x < -myMap.width_pixel / 2)
thirdPt.x += myMap.width_pixel;
else if(diff.x > myMap.width_pixel / 2)
thirdPt.x -= myMap.width_pixel;
if(diff.y < -myMap.height_pixel / 2)
thirdPt.y += myMap.height_pixel;
else if(diff.y > myMap.height_pixel / 2)
thirdPt.y -= myMap.height_pixel;
}
Point16 tipPt{(p1 + p2 + thirdPt) / 3};
if(global::s2->getMapObj()->getBitsPerPixel() == 8)
DrawPreCalcFadedTexturedTrigon(display, tmpP1, tmpP2, tipPt, Surf_Tileset, BorderRect,
P1.shading << 8, P2.shading << 8, gouData[type]);
else
DrawFadedTexturedTrigon(display, tmpP1, tmpP2, tipPt, Surf_Tileset, BorderRect, P1.i, P2.i);
}
}
// USD-Triangle
else
{
SDL_Rect BorderRect;
// left lower / right upper
auto borderSide = CalcBorders(myMap, P2.rsuTexture, P2.usdTexture, BorderRect);
if(borderSide != BorderPreference::None)
{
Uint16 col = (P1.VertexX - 1 < 0 ? myMap.width - 1 : P1.VertexX - 1);
MapNode tempP = myMap.getVertex(col, P1.VertexY);
Point16 tmpP1{p1}, tmpP2{p2};
Point32 thirdPt;
if(borderSide == BorderPreference::LeftTop)
{
thirdPt = p3;
tmpP1 -= Point16(1, 0);
tmpP2 -= Point16(1, 0);
} else
{
thirdPt = Point32(tempP.x, tempP.y) - displayRect.getOrigin();
// Shift it close to p1
auto diff = thirdPt - p1;
if(diff.x < -myMap.width_pixel / 2)
thirdPt.x += myMap.width_pixel;
else if(diff.x > myMap.width_pixel / 2)
thirdPt.x -= myMap.width_pixel;
if(diff.y < -myMap.height_pixel / 2)
thirdPt.y += myMap.height_pixel;
else if(diff.y > myMap.height_pixel / 2)
thirdPt.y -= myMap.height_pixel;
}
Point16 tipPt{(p1 + p2 + thirdPt) / 3};
if(global::s2->getMapObj()->getBitsPerPixel() == 8)
DrawPreCalcFadedTexturedTrigon(display, tmpP1, tmpP2, tipPt, Surf_Tileset, BorderRect,
P1.shading << 8, P2.shading << 8, gouData[type]);
else
DrawFadedTexturedTrigon(display, tmpP1, tmpP2, tipPt, Surf_Tileset, BorderRect, P1.i, P2.i);
}
// top / bottom - therefore get the rsu-texture one line above to compare
Uint16 row = (P2.VertexY - 1 < 0 ? myMap.height - 1 : P2.VertexY - 1);
Uint16 col = (P2.VertexY % 2 == 0 ? P2.VertexX : (P2.VertexX + 1 > myMap.width - 1 ? 0 : P2.VertexX + 1));
MapNode tempP = myMap.getVertex(col, row);
borderSide = CalcBorders(myMap, tempP.rsuTexture, P2.usdTexture, BorderRect);
if(borderSide != BorderPreference::None)
{
Point32 thirdPt;
if(borderSide == BorderPreference::LeftTop)
thirdPt = p1;
else
{
thirdPt = Point32(tempP.x, tempP.y) - displayRect.getOrigin();
// Shift it close to p2
auto diff = thirdPt - p2;
if(diff.x < -myMap.width_pixel / 2)
thirdPt.x += myMap.width_pixel;
else if(diff.x > myMap.width_pixel / 2)
thirdPt.x -= myMap.width_pixel;
if(diff.y < -myMap.height_pixel / 2)
thirdPt.y += myMap.height_pixel;
else if(diff.y > myMap.height_pixel / 2)
thirdPt.y -= myMap.height_pixel;
}
Point16 tipPt{(p2 + p3 + thirdPt) / 3};
if(global::s2->getMapObj()->getBitsPerPixel() == 8)
DrawPreCalcFadedTexturedTrigon(display, Point16(p2), Point16(p3), tipPt, Surf_Tileset, BorderRect,
P2.shading << 8, P3.shading << 8, gouData[type]);
else
DrawFadedTexturedTrigon(display, Point16(p2), Point16(p3), tipPt, Surf_Tileset, BorderRect, P2.i,
P3.i);
}
}
}
// blit picture to vertex (trees, animals, buildings and so on) --> BUT ONLY AT node1 ON RIGHTSIDEUP-TRIANGLES
// blit objects
if(!isRSU)
{
int objIdx = 0;
switch(P2.objectInfo)
{
// tree
case 0xC4:
if(P2.objectType >= 0x30 && P2.objectType <= 0x37)
{
if(P2.objectType + roundCount > 0x37)
objIdx = MAPPIC_TREE_PINE + (P2.objectType - 0x30) + (roundCount - 7);
else
objIdx = MAPPIC_TREE_PINE + (P2.objectType - 0x30) + roundCount;
} else if(P2.objectType >= 0x70 && P2.objectType <= 0x77)
{
if(P2.objectType + roundCount > 0x77)
objIdx = MAPPIC_TREE_BIRCH + (P2.objectType - 0x70) + (roundCount - 7);
else
objIdx = MAPPIC_TREE_BIRCH + (P2.objectType - 0x70) + roundCount;
} else if(P2.objectType >= 0xB0 && P2.objectType <= 0xB7)
{
if(P2.objectType + roundCount > 0xB7)
objIdx = MAPPIC_TREE_OAK + (P2.objectType - 0xB0) + (roundCount - 7);
else
objIdx = MAPPIC_TREE_OAK + (P2.objectType - 0xB0) + roundCount;
} else if(P2.objectType >= 0xF0 && P2.objectType <= 0xF7)
{
if(P2.objectType + roundCount > 0xF7)
objIdx = MAPPIC_TREE_PALM1 + (P2.objectType - 0xF0) + (roundCount - 7);
else
objIdx = MAPPIC_TREE_PALM1 + (P2.objectType - 0xF0) + roundCount;
}
break;
// tree
case 0xC5:
if(P2.objectType >= 0x30 && P2.objectType <= 0x37)
{
if(P2.objectType + roundCount > 0x37)
objIdx = MAPPIC_TREE_PALM2 + (P2.objectType - 0x30) + (roundCount - 7);
else