-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.h
More file actions
3216 lines (2940 loc) · 193 KB
/
Copy pathprinter.h
File metadata and controls
3216 lines (2940 loc) · 193 KB
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
//
// 25.10.2022 by xterminal86
//
#ifndef PRINTER_H
#define PRINTER_H
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <cstdio>
#include <algorithm>
#ifndef USE_SDL
#include <ncurses.h>
#else
#include "SDL2/SDL.h"
#endif
///
/// For "Terminal Graphics"
///
namespace TG
{
///
/// Auxillary structs
///
#ifdef USE_SDL
struct TileColor
{
int R = 0;
int G = 0;
int B = 0;
};
// ===========================================================================
struct TileInfo
{
int X = 0;
int Y = 0;
std::string Data;
};
#else
struct FBPixel
{
size_t ColorPairHash;
int Character;
};
#endif
// ===========================================================================
struct NColor
{
short ColorIndex;
short R;
short G;
short B;
};
// ===========================================================================
struct ColorPair
{
short PairIndex;
NColor FgColor;
NColor BgColor;
};
// ===========================================================================
namespace Colors
{
namespace ShadesOfGrey
{
const uint32_t One = 0x111111;
const uint32_t Two = 0x222222;
const uint32_t Three = 0x333333;
const uint32_t Four = 0x444444;
const uint32_t Five = 0x555555;
const uint32_t Six = 0x666666;
const uint32_t Seven = 0x777777;
const uint32_t Eight = 0x888888;
const uint32_t Nine = 0x999999;
const uint32_t Ten = 0xAAAAAA;
const uint32_t Eleven = 0xBBBBBB;
const uint32_t Twelve = 0xCCCCCC;
const uint32_t Thirteen = 0xDDDDDD;
const uint32_t Fourteen = 0xEEEEEE;
}
//
// Special value to determine
// whether we should draw an object or not.
//
const uint32_t None = 0xFFFFFFFF;
const uint32_t Black = 0x000000;
const uint32_t White = 0xFFFFFF;
const uint32_t Red = 0xFF0000;
const uint32_t Green = 0x00FF00;
const uint32_t Blue = 0x0000FF;
const uint32_t Cyan = 0x00FFFF;
const uint32_t Magenta = 0xFF00FF;
const uint32_t Yellow = 0xFFFF00;
}
// ===========================================================================
struct Position
{
Position() : X(0), Y(0) {}
Position(int x, int y) : X(x), Y(y) {}
Position(const Position& from) : X(from.X), Y(from.Y) {}
void Set(int x, int y)
{
X = x;
Y = y;
}
void Set(const Position& from)
{
X = from.X;
Y = from.Y;
}
// For use inside std::map as a key
bool operator< (const Position& rhs) const
{
return (X < rhs.X || Y < rhs.Y);
}
bool operator== (const Position& rhs) const
{
return (X == rhs.X && Y == rhs.Y);
}
bool operator!= (const Position& rhs) const
{
return (X != rhs.X || Y != rhs.Y);
}
// For debug logs
void Print()
{
printf("%s: [%i ; %i]\n", __PRETTY_FUNCTION__, X, Y);
}
int X;
int Y;
};
// ===========================================================================
///
/// Ye olde CP437 glyphs charmap
///
enum class NameCP437
{
FIRST = 0,
FACE_1,
FACE_2,
HEART,
DIAMOND,
CLUB,
SPADE,
DOT_1,
DOT_2,
SQUARE_1,
SQUARE_2,
SIGN_M,
SIGN_F,
NOTE_1,
NOTE_2,
LAMP,
RARROW_2,
LARROW_2,
UDARROW_1,
EXCLAIM_DBL,
PI,
DOLLAR_1,
DASH_1,
UDARROW_2,
UARROW_1,
DARROW_1,
RARROW_1,
LARROW_1,
CORNER_SMALL,
LRARROW,
UARROW_2,
DARROW_2,
ULCORNER_1 = 218,
DLCORNER_1 = 192,
URCORNER_1 = 191,
DRCORNER_1 = 217,
HBAR_1 = 196,
VBAR_1 = 179,
ULCORNER_2 = 201,
DLCORNER_2 = 200,
URCORNER_2 = 187,
DRCORNER_2 = 188,
HBAR_2 = 205,
VBAR_2 = 186,
ULCORNER_3 = 222,
DLCORNER_3 = 222,
URCORNER_3 = 221,
DRCORNER_3 = 221,
HBAR_3U = 223,
HBAR_3D = 220,
VBAR_3L = 222,
VBAR_3R = 221,
BLOCK = 219,
WAVES = 247,
};
// ===========================================================================
const std::map<NameCP437, int> CP437IndexByType =
{
{ NameCP437::FACE_1, (int)NameCP437::FACE_1 },
{ NameCP437::FACE_2, (int)NameCP437::FACE_2 },
{ NameCP437::HEART, (int)NameCP437::HEART },
{ NameCP437::DIAMOND, (int)NameCP437::DIAMOND },
{ NameCP437::CLUB, (int)NameCP437::CLUB },
{ NameCP437::SPADE, (int)NameCP437::SPADE },
{ NameCP437::DOT_1, (int)NameCP437::DOT_1 },
{ NameCP437::DOT_2, (int)NameCP437::DOT_2 },
{ NameCP437::SQUARE_1, (int)NameCP437::SQUARE_1 },
{ NameCP437::SQUARE_2, (int)NameCP437::SQUARE_2 },
{ NameCP437::SIGN_M, (int)NameCP437::SIGN_M },
{ NameCP437::SIGN_F, (int)NameCP437::SIGN_F },
{ NameCP437::NOTE_1, (int)NameCP437::NOTE_1 },
{ NameCP437::NOTE_2, (int)NameCP437::NOTE_2 },
{ NameCP437::LAMP, (int)NameCP437::LAMP },
{ NameCP437::RARROW_2, (int)NameCP437::RARROW_2 },
{ NameCP437::LARROW_2, (int)NameCP437::LARROW_2 },
{ NameCP437::UDARROW_1, (int)NameCP437::UDARROW_1 },
{ NameCP437::EXCLAIM_DBL, (int)NameCP437::EXCLAIM_DBL },
{ NameCP437::PI, (int)NameCP437::PI },
{ NameCP437::DOLLAR_1, (int)NameCP437::DOLLAR_1 },
{ NameCP437::DASH_1, (int)NameCP437::DASH_1 },
{ NameCP437::UDARROW_2, (int)NameCP437::UDARROW_2 },
{ NameCP437::UARROW_1, (int)NameCP437::UARROW_1 },
{ NameCP437::DARROW_1, (int)NameCP437::DARROW_1 },
{ NameCP437::RARROW_1, (int)NameCP437::RARROW_1 },
{ NameCP437::LARROW_1, (int)NameCP437::LARROW_1 },
{ NameCP437::CORNER_SMALL, (int)NameCP437::CORNER_SMALL },
{ NameCP437::LRARROW, (int)NameCP437::LRARROW },
{ NameCP437::UARROW_2, (int)NameCP437::UARROW_2 },
{ NameCP437::DARROW_2, (int)NameCP437::DARROW_2 },
{ NameCP437::URCORNER_1, (int)NameCP437::URCORNER_1 },
{ NameCP437::ULCORNER_1, (int)NameCP437::ULCORNER_1 },
{ NameCP437::DRCORNER_1, (int)NameCP437::DRCORNER_1 },
{ NameCP437::DLCORNER_1, (int)NameCP437::DLCORNER_1 },
{ NameCP437::HBAR_1, (int)NameCP437::HBAR_1 },
{ NameCP437::VBAR_1, (int)NameCP437::VBAR_1 },
{ NameCP437::URCORNER_2, (int)NameCP437::URCORNER_2 },
{ NameCP437::ULCORNER_2, (int)NameCP437::ULCORNER_2 },
{ NameCP437::DRCORNER_2, (int)NameCP437::DRCORNER_2 },
{ NameCP437::DLCORNER_2, (int)NameCP437::DLCORNER_2 },
{ NameCP437::HBAR_2, (int)NameCP437::HBAR_2 },
{ NameCP437::VBAR_2, (int)NameCP437::VBAR_2 },
{ NameCP437::URCORNER_3, (int)NameCP437::URCORNER_3 },
{ NameCP437::ULCORNER_3, (int)NameCP437::ULCORNER_3 },
{ NameCP437::DRCORNER_3, (int)NameCP437::DRCORNER_3 },
{ NameCP437::DLCORNER_3, (int)NameCP437::DLCORNER_3 },
{ NameCP437::HBAR_3U, (int)NameCP437::HBAR_3U },
{ NameCP437::HBAR_3D, (int)NameCP437::HBAR_3D },
{ NameCP437::VBAR_3L, (int)NameCP437::VBAR_3L },
{ NameCP437::VBAR_3R, (int)NameCP437::VBAR_3R },
{ NameCP437::BLOCK, (int)NameCP437::BLOCK },
{ NameCP437::WAVES, (int)NameCP437::WAVES }
};
// ===========================================================================
class Printer
{
public:
static const int kAlignLeft = 0;
static const int kAlignCenter = 1;
static const int kAlignRight = 2;
~Printer()
{
#ifdef USE_SDL
for (auto& i : _images)
{
SDL_DestroyTexture(i);
}
#endif
}
#ifdef USE_SDL
/// @param[in] renderer Reference to SDL_Renderer.
/// @param[in] fontImageFilename Font image filename.
/// @param[in] charImageWidth Font tileset char width in pixels.
/// @param[in] charImageHeight Font tileset char height in pixels.
/// @param[in] windowWidth Window created width for framebuffer dimensions calculation.
/// @param[in] windowHeight Window created height for framebuffer dimensions calculation.
/// @param[in] globalScale Optional parameter to scale the drawn font.
bool Init(SDL_Renderer* renderer,
const std::string& fontImageFilename,
int charImageWidth,
int charImageHeight,
int windowWidth,
int windowHeight,
float globalScale = 1.0f)
{
if (_initialized)
{
printf("%s - already initialized!\n", __PRETTY_FUNCTION__);
return _initialized;
}
_rendererRef = renderer;
_tilesetFilename = fontImageFilename;
_tileWidth = charImageWidth;
_tileHeight = charImageHeight;
_globalScale = globalScale;
_windowWidth = windowWidth;
_windowHeight = windowHeight;
if (InitForSDL(false))
{
_initialized = true;
}
return _initialized;
}
// =======================================================================
bool Init(SDL_Renderer* renderer,
int windowWidth,
int windowHeight,
float globalScale = 1.0f)
{
if (_initialized)
{
printf("%s - already initialized!\n", __PRETTY_FUNCTION__);
return _initialized;
}
_rendererRef = renderer;
_tileWidth = 8;
_tileHeight = 16;
_globalScale = globalScale;
_windowWidth = windowWidth;
_windowHeight = windowHeight;
if (InitForSDL(true))
{
_initialized = true;
}
return _initialized;
}
#else
void Init()
{
InitForCurses();
}
#endif
// =======================================================================
/// Clears framebuffer (ncurses) or renderer (SDL)
/// Use this before all PrintFB calls
void Clear()
{
#ifndef USE_SDL
for (int x = 0; x < _terminalWidth; x++)
{
for (int y = 0; y < _terminalHeight; y++)
{
PrintFB(x, y, ' ', Colors::Black, Colors::Black);
}
}
#else
SDL_SetRenderTarget(_rendererRef, _frameBuffer);
SDL_RenderClear(_rendererRef);
#endif
}
// =======================================================================
/// Prints framebuffer contents to the screen
/// Call this after all PrintFB calls
void Render()
{
#ifndef USE_SDL
for (int x = 0; x < _terminalWidth; x++)
{
for (int y = 0; y < _terminalHeight; y++)
{
attron(COLOR_PAIR(_colorMap[_frameBuffer[x][y].ColorPairHash].PairIndex));
mvaddch(y, x, _frameBuffer[x][y].Character);
attroff(COLOR_PAIR(_colorMap[_frameBuffer[x][y].ColorPairHash].PairIndex));
}
}
refresh();
#else
SDL_SetRenderTarget(_rendererRef, nullptr);
SDL_RenderClear(_rendererRef);
SDL_RenderCopy(_rendererRef, _frameBuffer, nullptr, nullptr);
SDL_RenderPresent(_rendererRef);
#endif
}
// =======================================================================
int TerminalWidth() { return _terminalWidth; }
int TerminalHeight() { return _terminalHeight; }
// =======================================================================
#ifndef USE_SDL
/// Print to "framebuffer"
void PrintFB(const int& x,
const int& y,
const int& ch,
const uint32_t& htmlColorFg,
const uint32_t& htmlColorBg = Colors::Black)
{
if (x < 0 || x > _terminalWidth - 1
|| y < 0 || y > _terminalHeight - 1)
{
return;
}
// Black & White mode for Windows
// due to PDCurses not handling colors correctly
#if !(defined(__unix__) || defined(__linux__))
uint32_t tmpFg;
uint32_t tmpBg;
if (htmlColorFg == Colors::Black
and htmlColorBg == Colors::Black)
{
tmpFg = Colors::Black;
tmpBg = Colors::Black;
}
else if (htmlColorBg != Colors::Black)
{
tmpFg = Colors::Black;
tmpBg = Colors::White;
}
else
{
tmpFg = Colors::White;
tmpBg = Colors::Black;
}
size_t hash = GetOrSetColor(tmpFg, tmpBg);
#else
size_t hash = GetOrSetColor(htmlColorFg, htmlColorBg);
#endif
_frameBuffer[x][y].Character = ch;
_frameBuffer[x][y].ColorPairHash = hash;
}
// =======================================================================
void PrintFB(const int& x,
const int& y,
const std::string& text,
int align,
const uint32_t& htmlColorFg,
const uint32_t& htmlColorBg = Colors::Black)
{
auto textPos = AlignText(x, y, align, text);
int xOffset = 0;
for (auto& c : text)
{
//
// Coordinates are swapped because
// in framebuffer we don't work in ncurses
// coordinate system.
//
PrintFB(textPos.second + xOffset,
textPos.first,
c,
htmlColorFg,
htmlColorBg);
xOffset++;
}
}
// =======================================================================
void DrawWindow(const Position& leftCorner,
const Position& size,
const std::string& header = std::string{},
const uint32_t& headerFgColor = Colors::White,
const uint32_t& headerBgColor = Colors::Black,
const uint32_t& borderColor = Colors::White,
const uint32_t& borderBgColor = Colors::Black,
const uint32_t& bgColor = Colors::Black)
{
int x = leftCorner.X;
int y = leftCorner.Y;
// Fill background
for (int i = x + 1; i < x + size.X; i++)
{
for (int j = y + 1; j < y + size.Y; j++)
{
PrintFB(i, j, ' ', Colors::Black, bgColor);
}
}
// Corners
PrintFB(x, y, '+', borderColor, borderBgColor);
PrintFB(x + size.X, y, '+', borderColor, borderBgColor);
PrintFB(x, y + size.Y, '+', borderColor, borderBgColor);
PrintFB(x + size.X, y + size.Y, '+', borderColor, borderBgColor);
// Horizontal bars
for (int i = x + 1; i < x + size.X; i++)
{
PrintFB(i, y, '-', borderColor, borderBgColor);
PrintFB(i, y + size.Y, '-', borderColor, borderBgColor);
}
// Vertical bars
for (int i = y + 1; i < y + size.Y; i++)
{
PrintFB(x, i, '|', borderColor, borderBgColor);
PrintFB(x + size.X, i, '|', borderColor, borderBgColor);
}
if (header.length() != 0)
{
std::string lHeader = header;
lHeader.insert(0, " ");
lHeader.append(" ");
int headerPosX = (x + (size.X / 2)) - lHeader.length() / 2;
int headerPosY = y;
for (auto& c : lHeader)
{
PrintFB(headerPosX, headerPosY, c, headerFgColor, headerBgColor);
headerPosX++;
}
}
}
#else
void PrintFB(const int& x,
const int& y,
int image,
const uint32_t& htmlColorFg,
const uint32_t& htmlColorBg = Colors::Black)
{
int posX = x * _tileWidthScaled;
int posY = y * _tileHeightScaled;
if (htmlColorBg != Colors::None)
{
ConvertHtmlToRGB(htmlColorBg);
SDL_SetTextureColorMod(_tileset,
_convertedHtml.R,
_convertedHtml.G,
_convertedHtml.B);
DrawTile(posX, posY, 219);
}
ConvertHtmlToRGB(htmlColorFg);
SDL_SetTextureColorMod(_tileset,
_convertedHtml.R,
_convertedHtml.G,
_convertedHtml.B);
DrawTile(posX, posY, image);
}
// =======================================================================
void PrintFB(const int& x,
const int& y,
const std::string& text,
int align,
const uint32_t& htmlColorFg,
const uint32_t& htmlColorBg = Colors::Black)
{
int px = x * _tileWidthScaled;
int py = y * _tileHeightScaled;
switch (align)
{
case kAlignCenter:
{
int pixelWidth = text.length() * _tileWidthScaled;
px -= pixelWidth / 2;
}
break;
case kAlignRight:
{
int pixelWidth = text.length() * _tileWidthScaled;
px -= pixelWidth;
}
break;
}
for (auto& c : text)
{
if (htmlColorBg != Colors::None)
{
ConvertHtmlToRGB(htmlColorBg);
SDL_SetTextureColorMod(_tileset,
_convertedHtml.R,
_convertedHtml.G,
_convertedHtml.B);
DrawTile(px, py, 219);
}
ConvertHtmlToRGB(htmlColorFg);
SDL_SetTextureColorMod(_tileset,
_convertedHtml.R,
_convertedHtml.G,
_convertedHtml.B);
DrawTile(px, py, c);
px += _tileWidthScaled;
}
}
// =======================================================================
void DrawImage(const int& x,
const int& y,
SDL_Texture* tex)
{
int tw, th;
SDL_QueryTexture(tex, nullptr, nullptr, &tw, &th);
SDL_Rect src;
src.x = 0;
src.y = 0;
src.w = tw;
src.h = th;
SDL_Rect dst;
dst.x = x;
dst.y = y;
dst.w = tw;
dst.h = th;
if (SDL_GetRenderTarget(_rendererRef) == nullptr)
{
SDL_SetRenderTarget(_rendererRef, _frameBuffer);
}
SDL_RenderCopy(_rendererRef, tex, &src, &dst);
}
// =======================================================================
int LoadImage(const std::string& fname)
{
int ret = -1;
SDL_Surface* surf = SDL_LoadBMP(fname.data());
if (surf == nullptr)
{
printf("Couldn't load image %s: %s\n", fname.data(), SDL_GetError());
return ret;
}
SDL_Texture* tex = SDL_CreateTextureFromSurface(_rendererRef, surf);
if (tex == nullptr)
{
printf("Couldn't create texture from surface: %s\n", SDL_GetError());
return ret;
}
_images.push_back(tex);
ret = _images.size() - 1;
SDL_FreeSurface(surf);
return ret;
}
// =======================================================================
int LoadImage(const std::string& fname, uint32_t colorKey)
{
int ret = -1;
SDL_Surface* surf = SDL_LoadBMP(fname.data());
if (surf == nullptr)
{
printf("Couldn't load image %s: %s\n", fname.data(), SDL_GetError());
return ret;
}
uint32_t r = ((colorKey & _maskR) >> 16);
uint32_t g = ((colorKey & _maskG) >> 8);
uint32_t b = (colorKey & _maskB);
int ok = SDL_SetColorKey(surf, SDL_TRUE, SDL_MapRGB(surf->format, r, g, b));
if (ok == -1)
{
printf("Couldn't set color key: %s\n", SDL_GetError());
return ret;
}
SDL_Texture* tex = SDL_CreateTextureFromSurface(_rendererRef, surf);
if (tex == nullptr)
{
printf("Couldn't create texture from surface: %s\n", SDL_GetError());
return ret;
}
_images.push_back(tex);
ret = _images.size() - 1;
SDL_FreeSurface(surf);
return ret;
}
// =======================================================================
void DrawImage(int imageIndex,
const SDL_Rect& r,
int angle,
SDL_RendererFlip flip = SDL_RendererFlip::SDL_FLIP_NONE)
{
if (imageIndex < 0 or imageIndex > (int)_images.size() - 1)
{
return;
}
SDL_Texture* t = _images[imageIndex];
int tw, th;
int ok = SDL_QueryTexture(t, nullptr, nullptr, &tw, &th);
if (ok == -1)
{
printf("Couldn't query texture: %s\n", SDL_GetError());
return;
}
SDL_Rect src;
src.x = 0;
src.y = 0;
src.w = tw;
src.h = th;
SDL_Rect dst;
dst.x = r.x;
dst.y = r.y;
dst.w = r.w;
dst.h = r.h;
if (SDL_GetRenderTarget(_rendererRef) == nullptr)
{
SDL_SetRenderTarget(_rendererRef, _frameBuffer);
}
SDL_RenderCopyEx(_rendererRef, t, &src, &dst, angle, nullptr, flip);
}
// =======================================================================
void DrawImage(int imageIndex,
const SDL_Rect& r)
{
if (imageIndex < 0 or imageIndex > (int)_images.size() - 1)
{
return;
}
SDL_Texture* t = _images[imageIndex];
int tw, th;
int ok = SDL_QueryTexture(t, nullptr, nullptr, &tw, &th);
if (ok == -1)
{
printf("Couldn't query texture: %s\n", SDL_GetError());
return;
}
SDL_Rect src;
src.x = 0;
src.y = 0;
src.w = tw;
src.h = th;
SDL_Rect dst;
dst.x = r.x;
dst.y = r.y;
dst.w = r.w;
dst.h = r.h;
if (SDL_GetRenderTarget(_rendererRef) == nullptr)
{
SDL_SetRenderTarget(_rendererRef, _frameBuffer);
}
SDL_RenderCopy(_rendererRef, t, &src, &dst);
}
// =======================================================================
void DrawImage(const int& x,
const int& y,
const int& w,
const int& h,
SDL_Texture* tex)
{
int tw, th;
SDL_QueryTexture(tex, nullptr, nullptr, &tw, &th);
SDL_Rect src;
src.x = 0;
src.y = 0;
src.w = tw;
src.h = th;
SDL_Rect dst;
dst.x = x;
dst.y = y;
dst.w = w;
dst.h = h;
if (SDL_GetRenderTarget(_rendererRef) == nullptr)
{
SDL_SetRenderTarget(_rendererRef, _frameBuffer);
}
SDL_RenderCopy(_rendererRef, tex, &src, &dst);
}
// =======================================================================
void DrawWindow(const Position& leftCorner,
const Position& size,
const std::string& header = std::string{},
const uint32_t& headerFgColor = Colors::White,
const uint32_t& headerBgColor = Colors::Black,
const uint32_t& borderColor = Colors::White,
const uint32_t& borderBgColor = Colors::Black,
const uint32_t& bgColor = Colors::Black,
int variant = 0)
{
auto res = GetPerimeter(leftCorner.X,
leftCorner.Y,
size.X,
size.Y,
true);
int x = leftCorner.X;
int y = leftCorner.Y;
int ulCorner;
int urCorner;
int dlCorner;
int drCorner;
int hBarU;
int hBarD;
int vBarL;
int vBarR;
if (variant == 0)
{
ulCorner = CP437IndexByType.at(NameCP437::ULCORNER_2);
urCorner = CP437IndexByType.at(NameCP437::URCORNER_2);
dlCorner = CP437IndexByType.at(NameCP437::DLCORNER_2);
drCorner = CP437IndexByType.at(NameCP437::DRCORNER_2);
hBarU = CP437IndexByType.at(NameCP437::HBAR_2);
hBarD = CP437IndexByType.at(NameCP437::HBAR_2);
vBarL = CP437IndexByType.at(NameCP437::VBAR_2);
vBarR = CP437IndexByType.at(NameCP437::VBAR_2);
}
else
{
ulCorner = CP437IndexByType.at(NameCP437::ULCORNER_3);
urCorner = CP437IndexByType.at(NameCP437::URCORNER_3);
dlCorner = CP437IndexByType.at(NameCP437::DLCORNER_3);
drCorner = CP437IndexByType.at(NameCP437::DRCORNER_3);
hBarU = CP437IndexByType.at(NameCP437::HBAR_3U);
hBarD = CP437IndexByType.at(NameCP437::HBAR_3D);
vBarL = CP437IndexByType.at(NameCP437::VBAR_3L);
vBarR = CP437IndexByType.at(NameCP437::VBAR_3R);
}
// Fill background
for (int i = x + 1; i < x + size.X; i++)
{
for (int j = y + 1; j < y + size.Y; j++)
{
PrintFB(i, j, ' ', Colors::Black, bgColor);
}
}
// Corners
PrintFB(x, y, ulCorner, borderColor, borderBgColor);
PrintFB(x + size.X, y, urCorner, borderColor, borderBgColor);
PrintFB(x, y + size.Y, dlCorner, borderColor, borderBgColor);
PrintFB(x + size.X, y + size.Y, drCorner, borderColor, borderBgColor);
// Horizontal bars
for (int i = x + 1; i < x + size.X; i++)
{
PrintFB(i, y, hBarU, borderColor, borderBgColor);
PrintFB(i, y + size.Y, hBarD, borderColor, borderBgColor);
}
// Vertical bars
for (int i = y + 1; i < y + size.Y; i++)
{
PrintFB(x, i, vBarL, borderColor, borderBgColor);
PrintFB(x + size.X, i, vBarR, borderColor, borderBgColor);
}
if (header.length() != 0)
{
std::string lHeader = header;
lHeader.insert(0, " ");
lHeader.append(" ");
int stringPixelWidth = (lHeader.length() * _tileWidthScaled);
int headerPosX = x * _tileWidthScaled;
int headerPosY = y * _tileHeightScaled;
// size.X actually gives + 1 (see corners section of PrintFBs above),
// so if size.X = 4 it means span from x to x + 4 end point.
// Thus, we either must align to string.length() / 2, or
// adjust header's position by adding additional _tileWidthScaled / 2,
// depending on size.X
//
// It's too long to explain thoroughly,
// just open some graphics editor and see for yourself.
if (size.X % 2 != 0)
{
int toAdd = ((size.X + 1) / 2) * _tileWidthScaled;
headerPosX += toAdd;
headerPosX -= stringPixelWidth / 2;
}
else
{
int toAdd = (size.X / 2) * _tileWidthScaled;
headerPosX += toAdd;
headerPosX -= stringPixelWidth / 2;
headerPosX += _tileWidthScaled / 2;
}
for (auto& c : lHeader)
{
ConvertHtmlToRGB(headerBgColor);
SDL_SetTextureColorMod(_tileset,
_convertedHtml.R,
_convertedHtml.G,
_convertedHtml.B);
DrawTile(headerPosX, headerPosY, 219);
ConvertHtmlToRGB(headerFgColor);
SDL_SetTextureColorMod(_tileset,
_convertedHtml.R,
_convertedHtml.G,
_convertedHtml.B);
DrawTile(headerPosX, headerPosY, c);
headerPosX += _tileWidthScaled;
}
}
}
#endif
private:
// Width and height of the window in characters
int _terminalWidth;
int _terminalHeight;
const uint32_t _maskR = 0x00FF0000;