-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowMapIntersect.cc
1294 lines (1002 loc) · 44.5 KB
/
WindowMapIntersect.cc
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
#ifdef _MSC_VER
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <SDL_image.h>
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#endif
#include <stdexcept>
#include <cassert>
// Types
//---------------------------------------------------------------------------------------------------
typedef struct IntVec2
{
int X; // X coordinate or column number
int Y; // Y coordinate or row number
} IntVec2_t;
enum class WindowIntersectType_t
{
// window is completely out of the map
TotallyOut = 0,
// window is completely inside the map
TotallyIn,
// window contains the northwest corner of the map
NorthWest,
// window contains the north wall of the map
North,
// window contains the northeast corner of the map
NorthEast,
// window contains the east wall of the map
East,
// window contains the southeast corner of the map
SouthEast,
// window contains the south wall of the map
South,
// window contains the southwest corner of the map
SouthWest,
// window contains the west wall of the map
West
};
typedef struct InitSDLValues_s
{
SDL_Window* Window;
SDL_Renderer* Renderer;
} InitSDLValues_t;
struct TestTextures_t
{
SDL_Texture* NorthWest;
SDL_Texture* North;
SDL_Texture* NorthEast;
SDL_Texture* East;
SDL_Texture* SouthEast;
SDL_Texture* South;
SDL_Texture* SouthWest;
SDL_Texture* West;
SDL_Texture* AllIn;
SDL_Texture* AllOut;
SDL_Texture* Moveable;
};
struct Color_t
{
int R, G, B;
};
// Constants
//---------------------------------------------------------------------------------------------------
constexpr Color_t cMagenta = {255, 0, 255};
// it's assumed that tiles are all cGridSize_px x cGridSize_px in dimensions
constexpr int cGridSize_px = 16;
// change this to change the size of the window
const IntVec2_t cScreenResolution = {1024, 768};
// this is more FYI than anything
const int cFPS = 60;
// 1/60 is 0.016666666666666666
const int cFrameDuration_ms = 16;
// imaginary, simulated window looking at our map
const IntVec2_t cWindowSize_px = {32, 32};
const IntVec2_t cWindowSize_Tiles = {cWindowSize_px.X / cGridSize_px, cWindowSize_px.Y / cGridSize_px};
// account for one more tile in the map render size; if a tile is partially out of view it still has to be rendered.
// in this demo's case, a 2 x 2 viewable area will need a 3 x 3 tile map render area.
const IntVec2_t cMapRenderTextureSize_Tiles = {cWindowSize_Tiles.X + 1, cWindowSize_Tiles.Y + 1};
const IntVec2_t cMapRenderTextureSize_px = {cMapRenderTextureSize_Tiles.X * cGridSize_px, cMapRenderTextureSize_Tiles.Y * cGridSize_px};
const IntVec2_t cMapOrigin = {432, 322};
constexpr IntVec2_t cTileSetSize_Tiles = {8, 8};
// DEMO: The map size would definitely NOT be the same as the tileset size in a real game
constexpr IntVec2_t cMapSize_Tiles = cTileSetSize_Tiles;
// Globals
//---------------------------------------------------------------------------------------------------
// various SDL resources required for rendering.
InitSDLValues_t SDLGlobals;
SDL_Texture* MapTestTexture = nullptr;
IntVec2_t MapTextureSize;
TestTextures_t ScreenRenderTextures;
TestTextures_t MapRenderTextures;
IntVec2_t MousePosition;
//--------------------------------------------------------------------------------------
// Misc. Utility Functions
//--------------------------------------------------------------------------------------
IntVec2_t InquireTextureSize(SDL_Texture* texture)
{
int width, height;
SDL_QueryTexture(texture, NULL, NULL, &width, &height);
IntVec2_t size = {width, height};
return size;
}
SDL_Texture* LoadImage(SDL_Renderer *renderer, const char* path)
{
SDL_Texture *texture = NULL;
SDL_Surface* image = IMG_Load(path);
if (image != NULL)
{
texture = SDL_CreateTextureFromSurface(renderer, image);
SDL_FreeSurface(image);
image = NULL;
}
else
{
printf("Image '%s' could not be loaded. SDL Error: %s\n", path, SDL_GetError());
}
return texture;
}
bool PointInRect(const IntVec2_t& point, const IntVec2_t& rectTopLeft, const IntVec2_t& rectSize)
{
// you might be wondering what the point of IntVect2 is if SDL_Point exists,
// well, there isn't one, I just didn't know SDL already had something.
SDL_Rect rect;
rect.x = rectTopLeft.X;
rect.y = rectTopLeft.Y;
rect.w = rectSize.X;
rect.h = rectSize.Y;
SDL_Point sdlPoint;
sdlPoint.x = point.X;
sdlPoint.y = point.Y;
SDL_bool inRect = SDL_PointInRect(&sdlPoint, &rect);
bool bInRect = (inRect == SDL_TRUE);
//printf("Point (%d, %d) in rect [(%d, %d), (%d, %d)]: %d (b) %d\n", point.X, point.Y, rect.x, rect.y, rect.w, rect.h, inRect, bInRect);
return bInRect;
}
IntVec2_t FindGridCoordinateForPoint_RoundUp(IntVec2_t point, int gridSize)
{
int columnIndex = point.X / gridSize;
int rowIndex = point.Y / gridSize;
if((point.X % gridSize) != 0)
{
columnIndex++;
}
if((point.Y % gridSize) != 0)
{
rowIndex++;
}
return {columnIndex, rowIndex};
}
IntVec2_t FindGridCoordinateForPoint(IntVec2_t point, int gridSize)
{
int columnIndex = point.X / gridSize;
int rowIndex = point.Y / gridSize;
return {columnIndex, rowIndex};
}
//--------------------------------------------------------------------------------------
// Demo / placeholder only functions
//--------------------------------------------------------------------------------------
static inline int InRange(int min, int value, int max);
// this is only for the sake of the demo, in a real game you would look up the image
// you need to draw from the map
void DEMO_DrawTile(SDL_Texture* mapRenderTexture, SDL_Texture* tileSetTexture, const IntVec2_t& sourceTileCoordinate_tiles, const IntVec2_t& textureDestCoordinate_tiles)
{
assert(InRange(0, sourceTileCoordinate_tiles.X, cTileSetSize_Tiles.X));
assert(InRange(0, sourceTileCoordinate_tiles.Y, cTileSetSize_Tiles.Y));
assert(InRange(0, textureDestCoordinate_tiles.X, cMapRenderTextureSize_Tiles.X));
assert(InRange(0, textureDestCoordinate_tiles.Y, cMapRenderTextureSize_Tiles.Y));
// in this demo's case, the tileset is the same as the map size, this will certainly NOT be the case in a real game
const IntVec2_t sourceTilesetCoord_px = {sourceTileCoordinate_tiles.X * cGridSize_px, sourceTileCoordinate_tiles.Y * cGridSize_px};
// For this partiuclar demo we could reduce the amount of calls to SDL_RenderCopy by copying the entire contiguous area at once,
// but this might not be a useful optimization in a real game unless it just so happened that the tileset exactly contained
// what was in the player's fov (unlikely unless you rendered the map itself into the tileset, which is not what I have in mind)
SDL_Rect srcRect = {0};
srcRect.x = sourceTilesetCoord_px.X;
srcRect.y = sourceTilesetCoord_px.Y;
srcRect.w = cGridSize_px;
srcRect.h = cGridSize_px;
SDL_Rect destRect = {0};
destRect.x = textureDestCoordinate_tiles.X * cGridSize_px;
destRect.y = textureDestCoordinate_tiles.Y * cGridSize_px;
destRect.w = srcRect.w;
destRect.h = srcRect.h;
SDL_SetRenderTarget(SDLGlobals.Renderer, mapRenderTexture);
SDL_RenderCopy(SDLGlobals.Renderer, tileSetTexture, &srcRect, &destRect);
SDL_SetRenderTarget(SDLGlobals.Renderer, nullptr);
}
//--------------------------------------------------------------------------------------
// Tile rendering functions
//--------------------------------------------------------------------------------------
static inline void CheckArea(const SDL_Rect& offset, const IntVec2_t& windowSize)
{
assert(offset.x >= 0);
assert(offset.y >= 0);
assert(offset.w >= 0);
assert(offset.h >= 0);
assert(offset.x < windowSize.X);
assert(offset.y < windowSize.Y);
assert(offset.w <= windowSize.X);
assert(offset.h <= windowSize.Y);
}
static inline int max(int a, int b)
{
if(a > b)
{
return a;
}
else
{
return b;
}
}
static inline int min(int a, int b)
{
if(a < b)
{
return a;
}
else
{
return b;
}
}
static inline int InRange(int min, int value, int max)
{
if(value < min)
{
return false;
}
if(value > max)
{
return false;
}
return true;
}
// You really need to see this drawn out on a sheet of paper to best understand this to be honest
// see scanned png hand written pages in this folder.
//
// Returns a value that represents the window's positioning; This value tells you if the window hangs off the corner of the map, the sides, or neither.
WindowIntersectType_t GetWindowIntersectType(const IntVec2_t& mapSize_px, const IntVec2_t& windowNorthWestCorner, const IntVec2_t& windowSize_px)
{
const IntVec2_t windowNorthEastCorner = {windowNorthWestCorner.X + windowSize_px.X, windowNorthWestCorner.Y + 0};
const IntVec2_t windowSouthWestCorner = {windowNorthWestCorner.X + 0, windowNorthWestCorner.Y + windowSize_px.Y};
const IntVec2_t windowSouthEastCorner = {windowNorthWestCorner.X + windowSize_px.X, windowNorthWestCorner.Y + windowSize_px.Y};
const IntVec2_t mapTopLeft = {0, 0};
const bool northWestCornerIn = PointInRect(windowNorthWestCorner, mapTopLeft, mapSize_px);
const bool northEastCornerIn = PointInRect(windowNorthEastCorner, mapTopLeft, mapSize_px);
const bool southWestCornerIn = PointInRect(windowSouthWestCorner, mapTopLeft, mapSize_px);
const bool southEastCornerIn = PointInRect(windowSouthEastCorner, mapTopLeft, mapSize_px);
if(northWestCornerIn && northEastCornerIn && southWestCornerIn && southEastCornerIn)
{
return WindowIntersectType_t::TotallyIn;
}
if(!northWestCornerIn && !northEastCornerIn && !southWestCornerIn && !southEastCornerIn)
{
return WindowIntersectType_t::TotallyOut;
}
if(southWestCornerIn && southEastCornerIn)
{
return WindowIntersectType_t::North;
}
else if(northWestCornerIn && southWestCornerIn)
{
return WindowIntersectType_t::East;
}
else if(northWestCornerIn && northEastCornerIn)
{
return WindowIntersectType_t::South;
}
else if(northEastCornerIn && southEastCornerIn)
{
return WindowIntersectType_t::West;
}
else if(southEastCornerIn)
{
return WindowIntersectType_t::NorthWest;
}
else if(southWestCornerIn)
{
return WindowIntersectType_t::NorthEast;
}
else if(northWestCornerIn)
{
return WindowIntersectType_t::SouthEast;
}
else if(northEastCornerIn)
{
return WindowIntersectType_t::SouthWest;
}
else
{
assert(0);
throw std::logic_error("Impossible intersect type detected.");
}
}
// See scanned png hand written pages in this folder.
// Returns a rectangle (top left corner in map coordinates) containing the dimensions of the map to copy into the window.
SDL_Rect GetMapRenderRectangle(const IntVec2_t& mapSize_px, const IntVec2_t& windowNorthWestCorner_px, const IntVec2_t& windowSize_px)
{
WindowIntersectType_t intersectType = GetWindowIntersectType(mapSize_px, windowNorthWestCorner_px, windowSize_px);
if(intersectType == WindowIntersectType_t::TotallyOut)
{
// nothing to draw from the map
return {0,0,0,0};
}
if(intersectType == WindowIntersectType_t::TotallyIn)
{
SDL_Rect mapRenderRect{0};
mapRenderRect.x = windowNorthWestCorner_px.X;
mapRenderRect.y = windowNorthWestCorner_px.Y;
mapRenderRect.w = windowSize_px.X;
mapRenderRect.h = windowSize_px.Y;
return mapRenderRect;
}
// abbreviations
const IntVec2_t& winP = windowNorthWestCorner_px;
const IntVec2_t& winSiz = windowSize_px;
const IntVec2_t& mSiz = mapSize_px;
const int northHeight = winP.Y + winSiz.Y;
const int southHeight = mSiz.Y - winP.Y;
const int westWidth = winP.X + winSiz.X;
const int eastWidth = mSiz.X - winP.X;
switch(intersectType)
{
case WindowIntersectType_t::NorthWest:
{
SDL_Rect mapRenderRect{0};
mapRenderRect.x = 0;
mapRenderRect.y = 0;
mapRenderRect.w = westWidth;
mapRenderRect.h = northHeight;
return mapRenderRect;
}
case WindowIntersectType_t::North:
{
SDL_Rect mapRenderRect{0};
mapRenderRect.x = winP.X;
mapRenderRect.y = 0;
mapRenderRect.w = winSiz.X;
mapRenderRect.h = northHeight;
return mapRenderRect;
}
case WindowIntersectType_t::NorthEast:
{
SDL_Rect mapRenderRect{0};
mapRenderRect.x = winP.X;
mapRenderRect.y = 0;
mapRenderRect.w = eastWidth;
mapRenderRect.h = northHeight;
return mapRenderRect;
}
case WindowIntersectType_t::East:
{
SDL_Rect mapRenderRect{0};
mapRenderRect.x = winP.X;
mapRenderRect.y = winP.Y;
mapRenderRect.w = eastWidth;
mapRenderRect.h = winSiz.Y;
return mapRenderRect;
}
case WindowIntersectType_t::SouthEast:
{
SDL_Rect mapRenderRect{0};
mapRenderRect.x = winP.X;
mapRenderRect.y = winP.Y;
mapRenderRect.w = eastWidth;
mapRenderRect.h = southHeight;
return mapRenderRect;
}
case WindowIntersectType_t::South:
{
SDL_Rect mapRenderRect{0};
mapRenderRect.x = winP.X;
mapRenderRect.y = winP.Y;
mapRenderRect.w = winSiz.X;
mapRenderRect.h = southHeight;
return mapRenderRect;
}
case WindowIntersectType_t::SouthWest:
{
SDL_Rect mapRenderRect{0};
mapRenderRect.x = 0;
mapRenderRect.y = winP.Y;
mapRenderRect.w = westWidth;
mapRenderRect.h = southHeight;
return mapRenderRect;
}
case WindowIntersectType_t::West:
{
SDL_Rect mapRenderRect{0};
mapRenderRect.x = 0;
mapRenderRect.y = winP.Y;
mapRenderRect.w = westWidth;
mapRenderRect.h = winSiz.Y;
return mapRenderRect;
}
default:
{
assert(0);
throw std::logic_error("Impossible intersect type dected.");
}
}
}
// try and draw a window, return the rectangle that it drew
// draw the tileset underneath it in red, draw the area it rendered in white maybe
SDL_Rect DrawTiles(SDL_Texture* mapRenderTexture, SDL_Texture* tileSetTexture, const IntVec2_t& topLeftTile, const IntVec2_t& topLeftOfTileToWindow_px, const IntVec2_t& windowSize_Tiles, const IntVec2_t& mapSize_Tiles)
{
// if the window is shifted right or down in the tile it's in, you'll have to render one extra tile to the east / south
IntVec2_t renderNextOffset = {0,0};
if(topLeftOfTileToWindow_px.X > 0)
{
renderNextOffset.X = 1;
}
if(topLeftOfTileToWindow_px.Y > 0)
{
renderNextOffset.Y = 1;
}
int maxEast = min(mapSize_Tiles.X - 1, topLeftTile.X + windowSize_Tiles.X + renderNextOffset.X);
int maxSouth = min(mapSize_Tiles.Y - 1, topLeftTile.Y + windowSize_Tiles.Y + renderNextOffset.Y);
int minWest = max(0, topLeftTile.X);
int minNorth = max(0, topLeftTile.Y);
int validRows = 0;
int validColumns = 0;
for(int rowIndex = topLeftTile.Y; rowIndex <= (topLeftTile.Y + windowSize_Tiles.Y); rowIndex++)
{
if(!InRange(minNorth, rowIndex, maxSouth))
{
continue;
}
int oldValidColumns = validColumns;
if(oldValidColumns != 0)
{
// should not change shape from one row to the next
assert(oldValidColumns == validColumns);
}
validColumns = 0;
// look 1 past the size, otherwise if you're in the middle of tile 1,
// 1 + 2, < 3, stop at tile 2
for(int columnIndex = topLeftTile.X; columnIndex <= (topLeftTile.X + windowSize_Tiles.X); columnIndex++)
{
if(!InRange(minWest, columnIndex, maxEast))
{
continue;
}
IntVec2_t mapCoordinate_Tiles = {columnIndex, rowIndex};
IntVec2_t textureCoordinate_Tiles = {validColumns, validRows};
DEMO_DrawTile(mapRenderTexture, tileSetTexture, mapCoordinate_Tiles, textureCoordinate_Tiles);
validColumns++;
}
// don't count the row if it contained zero valid columns
if(validColumns != 0)
{
validRows++;
}
}
SDL_Rect resultRect = {0};
resultRect.w = validColumns * cGridSize_px;
resultRect.h = validRows * cGridSize_px;
resultRect.x = minWest * cGridSize_px;
resultRect.y = minNorth * cGridSize_px;
return resultRect;
}
// Returns a 2D point giving the top left corner of a rectangle that serves as the destination of where the map pixels will be copied to the screen.
// This is needed because we don't copy the map pixels to the bottom left of the map render texture
// (and it wouldn't help anyway because the map render texture is 1 tile bigger than the screen in width and height)
//
// This might be a bit hard to visualize, but if you stood on the northwest corner of the map, you would see the map in the bottom right corner,
// and the sky would fill the rest of the top left
IntVec2_t GetDrawRenderOffset(const SDL_Rect& srcFromRenderRect, const IntVec2_t& windowSize, const WindowIntersectType_t& intersectType)
{
switch(intersectType)
{
case WindowIntersectType_t::TotallyOut:
case WindowIntersectType_t::TotallyIn:
{
return {0, 0};
}
case WindowIntersectType_t::NorthWest:
{
// put it in the bottom right corner
IntVec2_t result = {windowSize.X - srcFromRenderRect.w, windowSize.Y - srcFromRenderRect.h};
return result;
}
case WindowIntersectType_t::North:
{
// put it on the bottom
IntVec2_t result = {0, windowSize.Y - srcFromRenderRect.h};
return result;
}
case WindowIntersectType_t::NorthEast:
{
// put it on the bottom
IntVec2_t result = {0, windowSize.Y - srcFromRenderRect.h};
return result;
}
case WindowIntersectType_t::East:
{
// leave as is
IntVec2_t result = {0,0};
return result;
}
case WindowIntersectType_t::SouthEast:
{
// leave as is
IntVec2_t result = {0,0};
return result;
}
case WindowIntersectType_t::South:
{
// leave as is
IntVec2_t result = {0,0};
return result;
}
case WindowIntersectType_t::SouthWest:
{
// offset to right
IntVec2_t result = {windowSize.X - srcFromRenderRect.w,0};
return result;
}
case WindowIntersectType_t::West:
{
//offset to right
IntVec2_t result = {windowSize.X - srcFromRenderRect.w,0};
return result;
}
default:
{
assert(0);
throw std::logic_error("Impossible intersection type");
}
}
}
// Returns a rectangle showing the area to copy from, to get all of the useful pixels rendered from the map that would be in the player's view
SDL_Rect GetTextureReadArea(const IntVec2_t& windowTopLeft_RelToTextureTopLeft, const IntVec2_t& windowSize, const WindowIntersectType_t& intersectType, const SDL_Rect& renderedRectangle)
{
// yeah, we do need the offset to be not clamped at 0, because otherwise hit doesn't truncate the northeast case
const IntVec2_t textureOffset = {max(0, windowTopLeft_RelToTextureTopLeft.X), max(0, windowTopLeft_RelToTextureTopLeft.Y)};
switch(intersectType)
{
case WindowIntersectType_t::TotallyOut:
{
SDL_Rect rect = {0,0,0,0};
// there's really no reason for CheckArea to be in this case, but it's a handy breakpoint location
CheckArea(rect, windowSize);
return rect;
}
case WindowIntersectType_t::TotallyIn:
{
SDL_Rect rect = {0};
rect.x = windowTopLeft_RelToTextureTopLeft.X;
rect.y = windowTopLeft_RelToTextureTopLeft.Y;
rect.w = windowSize.X;
rect.h = windowSize.Y;
CheckArea(rect, windowSize);
return rect;
}
// maybe I can reduce some of this by doing some kind of absolute value for some of the width and height calculations
// I'm not sure it's worth it, I thought I had a more elegant method about 3 times now.
// very special case, can't be merged with anything
case WindowIntersectType_t::NorthWest:
{
SDL_Rect rect = {0};
rect.x = 0; // is NOT windowTopLeft_RelToTextureTopLeft.X, which is NOT zero
rect.y = 0; // is NOT windowTopLeft_RelToTextureTopLeft.Y, which is NOT zero
rect.w = windowSize.X + windowTopLeft_RelToTextureTopLeft.X; // really is plus! windowTopLeft is negative!
rect.h = windowSize.Y + windowTopLeft_RelToTextureTopLeft.Y; // really is plus! windowTopLeft is negative!
CheckArea(rect, windowSize);
return rect;
}
// possible merge with N/NE
case WindowIntersectType_t::North:
{
SDL_Rect rect = {0};
rect.x = windowTopLeft_RelToTextureTopLeft.X;
rect.y = 0; // is NOT windowTopLeft_RelToTextureTopLeft.Y, which is NOT zero
rect.w = windowSize.X;
rect.h = windowSize.Y + windowTopLeft_RelToTextureTopLeft.Y; // really is plus! windowTopLeft is negative!
CheckArea(rect, windowSize);
return rect;
}
case WindowIntersectType_t::East:
{
SDL_Rect rect = {0};
rect.x = windowTopLeft_RelToTextureTopLeft.X;
rect.y = windowTopLeft_RelToTextureTopLeft.Y;
rect.w = renderedRectangle.w - windowTopLeft_RelToTextureTopLeft.X;
rect.h = windowSize.Y;
CheckArea(rect, windowSize);
return rect;
}
case WindowIntersectType_t::NorthEast:
{
SDL_Rect rect = {0};
rect.x = windowTopLeft_RelToTextureTopLeft.X;
rect.y = 0; // is NOT windowTopLeft_RelToTextureTopLeft.Y, which is NOT zero
rect.w = renderedRectangle.w - windowTopLeft_RelToTextureTopLeft.X;
rect.h = windowSize.Y + windowTopLeft_RelToTextureTopLeft.Y; // for east, windowTopLeft_RelToTexturetopLeft will be 0; really is plus, Y is negative for NE
CheckArea(rect, windowSize);
return rect;
}
case WindowIntersectType_t::SouthEast:
{
SDL_Rect rect = {0};
rect.x = windowTopLeft_RelToTextureTopLeft.X;
rect.y = windowTopLeft_RelToTextureTopLeft.Y;
rect.w = renderedRectangle.w - windowTopLeft_RelToTextureTopLeft.X;
rect.h = renderedRectangle.h - windowTopLeft_RelToTextureTopLeft.Y;
CheckArea(rect, windowSize);
return rect;
}
case WindowIntersectType_t::South:
{
SDL_Rect rect = {0};
rect.x = windowTopLeft_RelToTextureTopLeft.X;
rect.y = windowTopLeft_RelToTextureTopLeft.Y;
rect.w = windowSize.X;
rect.h = renderedRectangle.h - windowTopLeft_RelToTextureTopLeft.Y;
CheckArea(rect, windowSize);
return rect;
}
case WindowIntersectType_t::SouthWest:
{
SDL_Rect rect = {0};
rect.x = 0; // is NOT windowTopLeft_RelToTextureTopLeft, which is NOT zero
rect.y = windowTopLeft_RelToTextureTopLeft.Y;
rect.w = windowSize.X + windowTopLeft_RelToTextureTopLeft.X; // is NOT minus, X is negative, we want to truncate off the part to the left of the texture's bounds
rect.h = renderedRectangle.h - windowTopLeft_RelToTextureTopLeft.Y;
CheckArea(rect, windowSize);
return rect;
}
case WindowIntersectType_t::West:
{
SDL_Rect rect = {0};
rect.x = 0; // is NOT windowTopLeft_RelToTextureTopLeft, which is NOT zero
rect.y = windowTopLeft_RelToTextureTopLeft.Y;
rect.w = windowSize.X + windowTopLeft_RelToTextureTopLeft.X; // is NOT minus, X is negative, we want to truncate off the part to the left of the texture's bounds
rect.h = windowSize.Y;
CheckArea(rect, windowSize);
return rect;
}
default:
{
assert(0);
throw std::logic_error("Impossible intersection type");
}
}
}
SDL_Rect RenderMapRegion(SDL_Texture* mapRenderTexture, SDL_Texture* tileSetTexture, const IntVec2_t& northWestTile, const IntVec2_t& topLeftOfTileToWindow_px, const IntVec2_t& windowSize_Tiles, const IntVec2_t& mapSize_tiles)
{
SDL_SetRenderTarget(SDLGlobals.Renderer, mapRenderTexture);
// you should never see this cyan color in this example, because the map has no transparent pixels.
// in a real game you may want transparent pixels in the middle of the map to show some background.
//
// If you do want that, use
// SDL_SetTextureBlendMode(mapRenderTexture, SDL_BLENDMODE_BLEND);
// SDL_SetRenderDrawColor(SDLGlobals.Renderer, 0, 0, 0, 0);
// instead.
SDL_SetRenderDrawColor(SDLGlobals.Renderer, 0, 255, 255, 255);
SDL_RenderClear(SDLGlobals.Renderer);
// The non-demo code would draw tiles spanning between the northWestTile and southEastTile to your mapRenderTexture
// I'm not going to do that in this demo, I'm just going to use a pre-rendered map texture. In this demo the map is already "rendered" in full.
// I just want to focus on the geometry of what's visible, so this example does not show the code for tiles and their tile pictures.
SDL_Rect renderedArea = DrawTiles(mapRenderTexture, tileSetTexture, northWestTile, topLeftOfTileToWindow_px, windowSize_Tiles, mapSize_tiles);
return renderedArea;
}
SDL_Rect RenderMapToTexture(SDL_Texture* mapRenderTexture, SDL_Texture* tileSetTexture, const IntVec2_t& windowSize_Tiles, const IntVec2_t& relToMap_WindowTopLeft)
{
const IntVec2_t gridCoordOfWindow_TopLeft = FindGridCoordinateForPoint(relToMap_WindowTopLeft, cGridSize_px);
const IntVec2_t coordOfTopLeftOfEnclosingGrid_px = {gridCoordOfWindow_TopLeft.X * cGridSize_px, gridCoordOfWindow_TopLeft.Y * cGridSize_px};
const IntVec2_t topLeftOfTileToWindow_px = {relToMap_WindowTopLeft.X - coordOfTopLeftOfEnclosingGrid_px.X, relToMap_WindowTopLeft.Y - coordOfTopLeftOfEnclosingGrid_px.Y};
// render the part of the map the player can see to a texture
SDL_Rect renderedRectangle = RenderMapRegion(mapRenderTexture, tileSetTexture, gridCoordOfWindow_TopLeft, topLeftOfTileToWindow_px, windowSize_Tiles, cMapSize_Tiles);
return renderedRectangle;
}
void CopyRenderedMapToScreen(SDL_Texture* screenRenderTexture, SDL_Texture* mapRenderTexture, const IntVec2_t& relToMap_WindowTopLeft, const IntVec2_t& windowSize, const SDL_Rect& renderedRectangle)
{
const IntVec2_t gridCoordOfWindow_TopLeft = FindGridCoordinateForPoint(relToMap_WindowTopLeft, cGridSize_px);
// This is the northwest most tile coordinate that our region touches.
const IntVec2_t topLeftValidTile = {max(0, gridCoordOfWindow_TopLeft.X), max(0, gridCoordOfWindow_TopLeft.Y)};
// DON'T use relToRenderTexture for the intersect type! It needs to be relative to the map!
const WindowIntersectType_t intersectType = GetWindowIntersectType(MapTextureSize, relToMap_WindowTopLeft, windowSize);
SDL_SetRenderTarget(SDLGlobals.Renderer, screenRenderTexture);
// I'm using this orangish color to simulate a sky texture or background color.
// in a real game you will probably want this to be set to transparent instead,
// If you do want that, use
// SDL_SetTextureBlendMode(screenRenderTexture, SDL_BLENDMODE_BLEND);
// SDL_SetRenderDrawColor(SDLGlobals.Renderer, 0, 0, 0, 0);
// instead.
SDL_SetRenderDrawColor(SDLGlobals.Renderer, 255, 180, 0, 255);
SDL_RenderClear(SDLGlobals.Renderer);
const IntVec2_t topLeftValidTileTopLeft_px = {topLeftValidTile.X * cGridSize_px, topLeftValidTile.Y * cGridSize_px};
const IntVec2_t validTopLeftTileToRegionTopLeft = {relToMap_WindowTopLeft.X - topLeftValidTileTopLeft_px.X, relToMap_WindowTopLeft.Y - topLeftValidTileTopLeft_px.Y};
const SDL_Rect srcRect = GetTextureReadArea(validTopLeftTileToRegionTopLeft , windowSize, intersectType, renderedRectangle);
const IntVec2_t screenDestOrigin = GetDrawRenderOffset(srcRect, windowSize, intersectType);
SDL_Rect destRect = {0};
destRect.x = screenDestOrigin.X;
destRect.y = screenDestOrigin.Y;
destRect.w = srcRect.w;
destRect.h = srcRect.h;
SDL_RenderCopy(SDLGlobals.Renderer, mapRenderTexture, &srcRect, &destRect);
}
//---------------------------------------------------------------------------------------------------------------------------
// Demo main functions
//---------------------------------------------------------------------------------------------------------------------------
const InitSDLValues_t InitSDL(IntVec2_t windowSize_px)
{
InitSDLValues_t sdlInitResult = {NULL, NULL};
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
return sdlInitResult;
}
// Init the window
SDL_Window* window = SDL_CreateWindow("A wild map intersect test program appears!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowSize_px.X, windowSize_px.Y, SDL_WINDOW_SHOWN);
if (!window)
{
printf("An error occured while trying to create window : %s\n", SDL_GetError());
return sdlInitResult;
}
// Init the renderer
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer)
{
printf("An error occured while trying to create renderer : %s\n", SDL_GetError());
return sdlInitResult;
}
sdlInitResult.Window = window;
sdlInitResult.Renderer = renderer;
return sdlInitResult;
}
// just checks for a quit signal. You can put more keypresses and mouse button handlers here if you want.
// returns 1 on quit, returns 0 otherwise
int HandleInput()
{
SDL_Event event = {0};
while (SDL_PollEvent(&event))
{
// E.g., from hitting the close window button
if (event.type == SDL_QUIT)
{
return 1;
}
else if(event.type == SDL_MOUSEMOTION)
{
MousePosition.X = event.motion.x;
MousePosition.Y = event.motion.y;
}
}
return 0;
}
void DrawTexture(SDL_Texture *texture, const IntVec2_t& textureSize, const IntVec2_t& screenCoord)
{
SDL_Rect textureRectangle;
textureRectangle.x = 0;
textureRectangle.y = 0;
textureRectangle.w = textureSize.X;
textureRectangle.h = textureSize.Y;
SDL_Rect screenRectangle;
screenRectangle.x = screenCoord.X;
screenRectangle.y = screenCoord.Y;
screenRectangle.w = textureSize.X;
screenRectangle.h = textureSize.Y;
SDL_RenderCopy(SDLGlobals.Renderer, texture, &textureRectangle, &screenRectangle);
}
IntVec2_t DEMO_TextureWindowRegion_RelToTexture(const IntVec2_t& relToMap_WindowTopLeft)
{
const IntVec2_t gridCoordOfWindow_TopLeft = FindGridCoordinateForPoint(relToMap_WindowTopLeft, cGridSize_px);
// This is the northwest most tile coordinate that our region touches.
const IntVec2_t topLeftValidTile = {max(0, gridCoordOfWindow_TopLeft.X), max(0, gridCoordOfWindow_TopLeft.Y)};
const IntVec2_t topLeftOfNorthWestTile_RelToMap_px = {topLeftValidTile.X * cGridSize_px, topLeftValidTile.Y * cGridSize_px};
const IntVec2_t topLeftOfTextureToRegionTopLeft = {relToMap_WindowTopLeft.X - topLeftOfNorthWestTile_RelToMap_px.X, relToMap_WindowTopLeft.Y - topLeftOfNorthWestTile_RelToMap_px.Y};
return topLeftOfTextureToRegionTopLeft;
}
// draws a magenta outline around the area that we're using as a window over the map
void DEMO_DrawWindowRegion(const IntVec2_t& testWindowSize, const IntVec2_t& windowTopLeft, Color_t color = cMagenta)