-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTQSG.cpp
More file actions
1986 lines (1752 loc) · 54.4 KB
/
TQSG.cpp
File metadata and controls
1986 lines (1752 loc) · 54.4 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
// Lic:
// TQSG.cpp
// TQSG Code
// version: 21.10.26
// Copyright (C) 2020, 2021 Jeroen P. Broks
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
// EndLic
#define TQSG_CopyTileDebug
#include "TQSG.hpp"
// TODO: Bundle support over JCR
// C++
#include <iostream>
#include <string>
#include <algorithm>
#include <memory>
// Myself
#include "TQSG.hpp"
// Tricky's Units
#include <QuickStream.hpp>
#include <QuickString.hpp>
#include <TrickySTOI.hpp>
#include <TrickyMath.hpp>
// JCR6
#include <jcr6_core.hpp>
#undef TQSG_Debug
using namespace std;
namespace TrickyUnits {
// This code is obtained from Stack Overflow. It's only meant for internal use
typedef struct {
double r; // a fraction between 0 and 1
double g; // a fraction between 0 and 1
double b; // a fraction between 0 and 1
} rgb;
typedef struct {
double h; // angle in degrees
double s; // a fraction between 0 and 1
double v; // a fraction between 0 and 1
} hsv;
static hsv rgb2hsv(rgb in);
static rgb hsv2rgb(hsv in);
hsv rgb2hsv(rgb in) {
hsv out;
double min, max, delta;
min = in.r < in.g ? in.r : in.g;
min = min < in.b ? min : in.b;
max = in.r > in.g ? in.r : in.g;
max = max > in.b ? max : in.b;
out.v = max; // v
delta = max - min;
if (delta < 0.00001) {
out.s = 0;
out.h = 0; // undefined, maybe nan?
return out;
}
if (max > 0.0) { // NOTE: if Max is == 0, this divide would cause a crash
out.s = (delta / max); // s
} else {
// if max is 0, then r = g = b = 0
// s = 0, h is undefined
out.s = 0.0;
out.h = NAN; // its now undefined
return out;
}
if (in.r >= max) // > is bogus, just keeps compilor happy
out.h = (in.g - in.b) / delta; // between yellow & magenta
else
if (in.g >= max)
out.h = 2.0 + (in.b - in.r) / delta; // between cyan & yellow
else
out.h = 4.0 + (in.r - in.g) / delta; // between magenta & cyan
out.h *= 60.0; // degrees
if (out.h < 0.0)
out.h += 360.0;
return out;
}
rgb hsv2rgb(hsv in) {
double hh, p, q, t, ff;
long i;
rgb out;
if (in.s <= 0.0) { // < is bogus, just shuts up warnings
out.r = in.v;
out.g = in.v;
out.b = in.v;
return out;
}
hh = in.h;
if (hh >= 360.0) hh = 0.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = in.v * (1.0 - in.s);
q = in.v * (1.0 - (in.s * ff));
t = in.v * (1.0 - (in.s * (1.0 - ff)));
switch (i) {
case 0:
out.r = in.v;
out.g = t;
out.b = p;
break;
case 1:
out.r = q;
out.g = in.v;
out.b = p;
break;
case 2:
out.r = p;
out.g = in.v;
out.b = t;
break;
case 3:
out.r = p;
out.g = q;
out.b = in.v;
break;
case 4:
out.r = t;
out.g = p;
out.b = in.v;
break;
case 5:
default:
out.r = in.v;
out.g = p;
out.b = q;
break;
}
return out;
}
static void Ouwehoeren(std::string whatever) {
#ifdef TQSG_Debug
printf("\x1b[32mTQSG DEBUG>\x1b[0m %s\n", whatever.c_str());
#endif
}
static SDL_Rect _VP{ 0,0,0,0 };
string TError{ "No Error" };
TQSG_PanicType TQSG_Panic = NULL;
static string LastError = "";
//The window we'll be rendering to
static SDL_Window* gWindow = NULL;
//The surface contained by the window
static SDL_Surface* gScreenSurface = NULL;
static SDL_Renderer* gRenderer = NULL;
static double scalex = 1, scaley = 1;
static Uint8 tcr = 255, tcg = 255, tcb = 255, tcalpha=255;
static double rotatedegrees = 0;
static SDL_RendererFlip imgflip = SDL_FLIP_NONE;
static SDL_BlendMode BlendMode = SDL_BLENDMODE_BLEND;
static int TQSG_OriginX{ 0 };
static int TQSG_OriginY{ 0 };
static void _Panic(std::string errormessage) {
LastError = errormessage;
if (TQSG_Panic) TQSG_Panic(errormessage);
}
string TQSG_GetError() { return LastError; }
void TQSG_Image::LoadRWops(SDL_RWops* data, int frame, int autofree) {
LastError = "";
if (frame >= Textures.size()) {
char FE[400];
sprintf_s(FE, 395, "Texture assignment out of bouds! (%d/%d)", frame, (int)Textures.size());
LastError = FE;
return;
}
auto buf = IMG_LoadTexture_RW(gRenderer, data, autofree);
if (buf == NULL) { LastError = "Getting texture from SLD_RWops failed!"; return; }
if (frame < 0) {
Textures.push_back(buf);
}
else {
Textures[frame] = buf;
}
}
int TQSG_Image::Frames() {
if (altframing)
return AltFrames.size();
else
return Textures.size();
}
void TQSG_Image::Create(SDL_RWops* data, int autofree) {
Textures.clear();
LastError = "";
LoadRWops(data, -1, autofree);
}
void TQSG_Image::Create(SDL_Texture* data) {
Textures.clear();
LastError = "";
Textures.push_back(data);
}
void TQSG_Image::Create(std::string file) {
LastError = "";
Textures.clear();
if (!FileExists(file)) {
LastError = "File \"" + file + "\" not found";
return;
}
if (jcr6::Recognize(file) != "") {
LastError = "No support for bundled jcr yet!\nRome wasn't built in one day either, you know!";
}
SDL_Surface* loadedSurface = IMG_Load(file.c_str());
if (loadedSurface == NULL) {
char FE[300];
sprintf_s(FE, 295, "Unable to load image %s!\nSDL_image Error: %s", file.c_str(), IMG_GetError());
LastError = FE;
return;
}
//Create texture from surface pixels
auto newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
if (newTexture == NULL) {
char FE[300];
sprintf_s(FE, 295, "Unable to create texture from %s!\nSDL Error: %s", file.c_str(), SDL_GetError());
LastError = FE;
return;
}
Textures.push_back(newTexture);
}
static SDL_Texture* Tex_From_JCR(jcr6::JT_Dir& JD, std::string entry) {
SDL_RWops* RWBuf = NULL;
jcr6::JT_Entry E = JD.Entry(entry);
jcr6::JT_EntryReader buf;
JD.B(entry, buf);
RWBuf = SDL_RWFromMem(buf.pointme(), buf.getsize());
SDL_Texture* ret = IMG_LoadTexture_RW(gRenderer, RWBuf, 1);
return ret;
}
void TQSG_Image::TrueLoadJCR(jcr6::JT_Dir& JCR, std::string entry) {
LastError = "";
Textures.clear();
if (JCR.EntryExists(entry)) {
// Load Entry
Textures.push_back(Tex_From_JCR(JCR, entry));
if (JCR.EntryExists(StripExt(entry) + ".frames")) {
auto frameinst = JCR.String(StripExt(entry) + ".frames");
auto params = Split(frameinst, ',');
AltFrame(stoi(params[0]), stoi(params[1]), stoi(params[3]));
if (stoi(params[2]) != 0) printf("\x7Warning! Parameter 3 contained valued %d. The TQSG engine ignores this value!", stoi(params[2]));
}
}
else if (JCR.DirectoryExists(entry)) {
// Bundle
auto ue = TReplace(Upper(entry), '\\', '/');
if (!suffixed(ue, "/")) ue += "/";
for (auto LT : JCR.Entries()) {
if (prefixed(LT.first, ue)) Textures.push_back(Tex_From_JCR(JCR, LT.first));
}
}
else {
char FE[300];
sprintf_s(FE, 295, "I could not find any JCR data for image \"%s\"!", entry.c_str());
//LastError = FE;
_Panic(FE);
}
}
void TQSG_Image::Create(jcr6::JT_Dir& JCR, std::string entry) {
TrueLoadJCR(JCR, entry);
}
void TQSG_Image::Create(std::string mainfile, std::string entry) {
auto J = jcr6::Dir(mainfile);
TrueLoadJCR(J, entry);
}
void TQSG_Image::Create(unsigned int w, unsigned int h) {
Uint32 format = SDL_GetWindowPixelFormat(gWindow);
for (auto T : Textures) delete T;
Textures.clear();
Textures.push_back(SDL_CreateTexture(gRenderer, format, SDL_TEXTUREACCESS_STREAMING, w,h));
}
void TQSG_Image::Copy(TQSG_Image* Original) {
Uint32 format = SDL_GetWindowPixelFormat(gWindow);
//TQSG_Image ret;
auto ret{ this }; // better?
ret->hotx = Original->hotx;
ret->hoty = Original->hoty;
ret->altframing = Original->altframing;
for (auto af : Original->AltFrames) ret->AltFrames.push_back(af);
for (auto tex : Original->Textures) {
auto tcopy = SDL_CreateTexture(gRenderer, format, SDL_TEXTUREACCESS_STREAMING, Original->Width(), Original->Height());
void* cpixels;
void* opixels;
int cpitch,opitch;
//pixels[x + (y * SCREEN_WIDTH)] = SDL_MapRGB(fmt, 255, 0, 0);
SDL_LockTexture(tcopy, NULL, &cpixels, &cpitch);
if (SDL_LockTexture(tex, NULL, &opixels, &opitch) <0) {
cout << "TQSG_Image::Copy(" << (int)Original << "): (got) " << SDL_GetError() << endl;
};
unsigned long long m = (unsigned long long)(((unsigned long long)Original->Width()) * Original->Height());
for (unsigned long long i = 0; i < m; i++) {
auto v{ ((Uint32*)opixels)[i] };
((Uint32*)cpixels)[i] = v;
}
SDL_UnlockTexture(tcopy);
SDL_UnlockTexture(tex);
}
}
void TQSG_Image::Negative() {
for (auto Tex : Textures) {
Uint32 format = SDL_GetWindowPixelFormat(gWindow);
auto pixformat = SDL_AllocFormat(format);
void* pixels;
int pitch;
SDL_LockTexture(Tex, NULL, &pixels, &pitch);
for (unsigned long long i = 0; i < Width() * Height(); ++i) {
Uint8
r,
g,
b;
SDL_GetRGB(((Uint32*)pixels)[i], pixformat, &r, &g, &b);
((Uint32*)pixels)[i]= SDL_MapRGB(pixformat, 255-r, 255-g, 255-b);
}
}
}
void TQSG_Image::Negative(TQSG_Image* _Copy) {
Copy(_Copy);
Negative();
}
void TQSG_Image::AltFrame(int w, int h, int num) {
LastError = "";
if (altframing) {
//LastError = "Image already altframed";
_Panic("Image already altframed");
return;
}
if (Textures.size() != 1) {
//LastError = "Altframing only possible when there is 1 single texture in an image. No more, no less!";
_Panic("Altframing only possible when there is 1 single texture in an image. No more, no less!");
return;
}
int ow = Width();
int oh = Height();
int collumns = floor(ow / w);
int rows = floor(oh / h);
if (collumns * rows < num) {
char FE[255];
sprintf_s(FE, 250, "Only %d frames possible, but %d were requested!", collumns * rows, num);
LastError = FE;
}
AltFrames.clear();
for (int frame = 0; frame < num; ++frame) {
SDL_Rect framerect;
framerect.x = (frame % collumns) * w;
framerect.y = floor(frame / collumns) * h;
framerect.w = w;
framerect.h = h;
AltFrames.push_back(framerect);
}
altframing = true;
}
void TQSG_Image::Draw(int x, int y, int frame) {
LastError = "";
if (altframing) {
if (frame < 0 || frame >= AltFrames.size()) {
char FE[400];
sprintf_s(FE, 395, "DRAW:Texture frame assignment out of bouds! (%d/%d/A)", frame, (int)AltFrames.size());
_Panic(FE);
return;
}
} else if (frame < 0 || frame >= Textures.size()) {
char FE[400];
sprintf_s(FE, 395, "DRAW:Texture frame assignment out of bouds! (%d/%d/R)", frame, (int)Textures.size());
//LastError = FE;
_Panic(FE);
return;
}
//printf("DEBUG! B.Color(%3d, %3d, %3d) \n", tcr, tcg, tcb);
//printf("DEBUG! A.Color(%3d, %3d, %3d) \n", tcr, tcg, tcb);
SDL_Rect Target;
Target.x = (x - (int)ceil(hotx*scalex))+TQSG_OriginX;
Target.y = (y - (int)ceil(hoty*scaley))+TQSG_OriginY;
Target.w = (int)ceil(Width() * scalex);
Target.h = (int)ceil(Height() * scaley);
if (altframing) {
SDL_SetTextureBlendMode(Textures[0], BlendMode);
SDL_SetTextureAlphaMod(Textures[0], tcalpha);
SDL_SetTextureColorMod(Textures[0], tcr, tcg, tcb);
SDL_RenderCopy(gRenderer, Textures[0], &AltFrames[frame], &Target);
} else {
SDL_SetTextureBlendMode(Textures[frame], BlendMode);
SDL_SetTextureAlphaMod(Textures[frame], tcalpha);
SDL_SetTextureColorMod(Textures[frame], tcr, tcg, tcb);
SDL_RenderCopy(gRenderer, Textures[frame], NULL, &Target);
}
}
void TQSG_Image::CopyTiled(TQSG_Image* Copy, int ix, int iy) {
int
sx{ 0 },
cx{ ix },
cy{ iy };
SDL_SetRenderTarget(gRenderer, Copy->Textures[0]);
SDL_RenderClear(gRenderer);
SDL_SetTextureBlendMode(Textures[0], BlendMode);
SDL_SetTextureAlphaMod(Textures[0], 255);
SDL_SetTextureColorMod(Textures[0], 255,255,255);
while (cx > 0) cx -= Width(); sx = cx;
while (cy > 0) cy -= Height();
while (cy < Copy->Height()) {
while (cx < Copy->Width()) {
#ifdef TQSG_CopyTileDebug
std::cout << "\tTiling. Insert(" << ix << "," << iy << ") Position(" << cx << "," << cy << ") Area: " << Copy->Width() << "x" << Copy->Height() << endl;
#endif
SDL_Rect Target{ cx,cy,Width(),Height() };
SDL_RenderCopy(gRenderer, Textures[0], NULL, &Target);
cx += Width();
}
cx = sx;
cy += Height();
}
SDL_SetRenderTarget(gRenderer, NULL);
}
void TQSG_Image::XDraw(int x, int y, int frame) {
int limgflip{ SDL_FLIP_NONE };
auto _scalex{ scalex };
auto _scaley{ scaley };
LastError = "";
if (frame < 0 || frame >= Textures.size()) {
char FE[400];
sprintf_s(FE, 395, "XDRAW:Texture frame assignment out of bouds! (%d/%d)", frame, (int)Textures.size());
//LastError = FE;
_Panic(FE);
return;
}
if (_scalex < 0) {
_scalex = abs(_scalex);
limgflip |= SDL_FLIP_HORIZONTAL;
}
if (_scaley < 0) {
_scaley = abs(_scaley);
limgflip |= SDL_FLIP_VERTICAL;
}
//printf("DEBUG! B.Color(%3d, %3d, %3d) \n", tcr, tcg, tcb);
//printf("DEBUG! A.Color(%3d, %3d, %3d) \n", tcr, tcg, tcb);
/*
SDL_Rect Target;
Target.x = (x -(int)ceil(hotx * _scalex))+TQSG_OriginX;
Target.y = (y -(int)ceil(hoty * _scaley))+TQSG_OriginY;
Target.w = (int)ceil(Width() * _scalex);
Target.h = (int)ceil(Height() * _scaley);
//*/
//*
SDL_Rect Target {
(x-(hotx*_scalex))+TQSG_OriginX,
(y-(hoty*_scaley))+TQSG_OriginY,
(int)(Width()*_scalex),
(int)(Height()*_scaley)
};
//*/
/*
SDL_Point cpoint;
cpoint.x = hotx;
cpoint.y = hoty;
*/
SDL_Point cpoint{ (int)(hotx * _scalex),(int)(hoty * _scaley) };
//SDL_RenderCopy(gRenderer, Textures[frame], NULL, &Target);
if (altframing) {
SDL_SetTextureBlendMode(Textures[0], BlendMode);
SDL_SetTextureAlphaMod(Textures[0], tcalpha);
SDL_SetTextureColorMod(Textures[0], tcr, tcg, tcb);
SDL_RenderCopyEx(gRenderer, Textures[0], &AltFrames[frame], &Target, rotatedegrees, &cpoint, (SDL_RendererFlip)limgflip);
} else {
SDL_SetTextureBlendMode(Textures[frame], BlendMode);
SDL_SetTextureAlphaMod(Textures[frame], tcalpha);
SDL_SetTextureColorMod(Textures[frame], tcr, tcg, tcb);
SDL_RenderCopyEx(gRenderer, Textures[frame], NULL, &Target, rotatedegrees, &cpoint, (SDL_RendererFlip)limgflip);
}
}
void TQSG_Image::StretchDraw(int x, int y, int w, int h, int frame) {
LastError = "";
if (frame < 0 || frame >= Textures.size()) {
char FE[400];
sprintf_s(FE, 395, "Texture assignment out of bouds! (%d/%d)", frame, (int)Textures.size());
LastError = FE;
return;
}
SDL_Rect Target;
Target.x = x+TQSG_OriginX;
Target.y = y+TQSG_OriginY;
Target.w = w;
Target.h = h;
if (altframing) {
SDL_SetTextureBlendMode(Textures[0], BlendMode);
SDL_SetTextureAlphaMod(Textures[0], tcalpha);
SDL_SetTextureColorMod(Textures[0], tcr, tcg, tcb);
SDL_RenderCopy(gRenderer, Textures[frame], &AltFrames[frame], &Target);
} else {
SDL_SetTextureBlendMode(Textures[frame], BlendMode);
SDL_SetTextureAlphaMod(Textures[frame], tcalpha);
SDL_SetTextureColorMod(Textures[frame], tcr, tcg, tcb);
SDL_RenderCopy(gRenderer, Textures[frame], NULL, &Target);
}
}
void TQSG_Image::Tile(int ax, int ay, int w, int h, int frame,int aix, int aiy) {
auto
x = ax + TQSG_OriginX,
y = ay + TQSG_OriginY,
ix = aix,
iy = aiy;
LastError = "";
if (altframing) {
LastError = "Altframing NOT supported for tiling (and is not likely to be supported in the future, either!)";
return;
}
// todo: Fix issues with negative ix
/*???
if (iy>0)
iy = (y + (Height() - iy)) % Height();
if (ix > 0)
//ix = (x+ (Width() - ix)) % Width();
//ix = (x - (Width() + ix)) % Width();
ix = -(ix % Width());
//*/
if (ix < 0) {
//cout << "neg x:" << ix << " to ";
ix = (x - (Width() + ix)) % Width();
//cout << ix << "\n";
}
if (iy < 0) {
//cout << "neg x:" << ix << " to ";
iy = (y - (Height() + iy)) % Height();
//cout << ix << "\n";
}
int ox, oy, ow, oh;
TQSG_GetViewPort(&ox, &oy, &ow, &oh);
int tsx, tsy, tex, tey, tw, th;
int imgh = Height();
int imgw = Width();
/*
tsx = max(ox, x);
tsy = max(oy, y);
tex = min(ow + ox, x + w); tw = tex - tsx;
tey = min(oh + oy, y + h); th = tey - tsy;
*/
tsx = x;
tsy = y;
tex = w + x;
tey = h + y;
tw = w;
th = h;
if (tw <= 0 || th <= 0) return; // Nothing to do but getting bugged!
//cout << "TILE: Rect("<<x<<","<<y<<") "<<w<<"x"<<h<<" "<<"\n";
//cout << "\tViewPort(" << tsx << "," << tsy << "," << tw << "[" << tex << "]" << "," << th << "[" << tey << "])\n";
SDL_Rect Target, Source;
//TQSG_ViewPort(tsx, tsy, tw, th);
//TQSG_Rect(tsx, tsy, tw, th);
//cout << "for (int dy = tsy("<<tsy<<") - iy("<<iy<<")(" << (tsy - iy) << "); dy < tey(" << tey << "); dy += imgh(" << imgh << ")) \n";
SDL_SetTextureColorMod(Textures[frame],tcr, tcg, tcb);
SDL_SetTextureBlendMode(Textures[frame], BlendMode);
SDL_SetTextureAlphaMod(Textures[frame], tcalpha);
for (int dy = tsy - iy; dy < tey; dy += imgh) {
//cout << "(" << x << "," << y << ")\tdy:" << dy << "; tsy:" << tsy << " imgh:" << imgh << " th:" << th << "\n";
for (int dx = tsx - ix; dx < tex; dx += imgw) {
//cout << "\t\tDrawTile(" << dx << "," << dy << "," << imgw << "," << imgh << ")\n";
Target.x = dx;
Target.y = dy;
Target.w = imgw;
Target.h = imgh;
Source.x = 0;
Source.y = 0;
Source.w = imgw;
Source.h = imgh;
//cout << "tgt (" << Target.x << "," << Target.y << ") " << Target.w << "x" << Target.h<<"\n";
//cout << "src (" << Source.x << "," << Source.y << ") " << Source.w << "x" << Source.h<<"; Frame:"<<frame<<"\n\n";
//cout << "("<<x<<","<<y<<")\tdx:" << dx << "; tsx:" << tsx << " imgw:" << imgw << " tw:" << tw<<"\n";
if (dx >= tsx && (dx + imgw) > tex) {
Source.w = imgw - ((dx + imgw) - tex);
Target.w = Source.w; //(dx + imgw) - tex;
//cout << "aw " << Source.w << "\n";
} else if (dx <= tsx) {
Source.x = tsx - dx;
Source.w = imgw - Source.x;
Target.x = tsx;
Target.w = Source.w;
}
if (dy <= tsy && dy + imgh > tey) {
Source.y = tsy - dy;
Source.h = th;
Target.y = tsy;
Target.h = th;
} else if (dy >= tsy && (dy + imgh) > tey) {
Source.h = imgh - ((dy + imgh) - tey);
Target.h = Source.h;//(dy + imgh) - tey;
//cout << "ah " << Source.h << "\t" << dy << "\tImgHeight:>" << imgh << "; img-maxy::>" << (dy + imgh) << "; rect-maxy::>" << tey << "=="<<(h+y)<<"\n";
} else if (dy <= tsy) {
Source.y = tsy - dy;
Source.h = imgh - Source.y;
Target.y = tsy;
Target.h = Source.h;
}
// TQSG_Rect(dx, dy, imgw, imgh);//debug
if (frame < 0 || frame >= Textures.size()) {
_Panic("<IMAGE>.Tile(" + to_string(x) + "," + to_string(y) + "," + to_string(w) + "," + to_string(h) + "," + to_string(frame) + "): Out of frame boundaries (framecount: " + to_string(Textures.size()) + ")"); return;
}
SDL_RenderCopy(gRenderer, Textures[frame], &Source, &Target);
}
}
//TQSG_ViewPort(ox, oy, ow, oh);
//TQSG_Color(180, 0, 255);
//TQSG_Rect(tsx, tsy, tw, th,true);
}
void TQSG_Image::Blit(int ax, int ay, int isx, int isy, int iex, int iey, int frame) {
auto
x = ax + TQSG_OriginX,
y = ay + TQSG_OriginY,
imgh = Height(),
imgw = Width();
SDL_Rect
Target,
Source;
if (altframing) {
LastError = "Altframing NOT supported for Blitting (and is not likely to be supported in the future, either!)";
return;
}
Source.x = max(isx, 0);
Source.y = max(isy, 0);
Source.w = min(iex, imgw - isx);
Source.h = min(iey, imgh - isy);
if (Source.w < 1 || Source.h < 1) { LastError = "Blit format error"; return; }
Target.x = x;
Target.y = y;
Target.w = Source.w * scalex;
Target.h = Source.h * scaley;
SDL_SetTextureColorMod(Textures[frame], tcr, tcg, tcb);
SDL_SetTextureBlendMode(Textures[frame], BlendMode);
SDL_SetTextureAlphaMod(Textures[frame], tcalpha);
SDL_RenderCopy(gRenderer, Textures[frame], &Source, &Target);
}
int TQSG_Image::Width() {
LastError = "";
if (Textures.size() == 0) { LastError = "No textures!"; return 0; }
if (altframing) {
if (AltFrames.size() == 0) {
LastError = "No alt frame rectangle config!";
return 0;
}
return AltFrames[0].w;
}
int w, h;
SDL_QueryTexture(Textures[0], NULL, NULL, &w, &h);
return w;
}
int TQSG_Image::Height() {
LastError = "";
if (Textures.size() == 0) { LastError = "No textures!"; return 0; }
if (altframing) {
if (AltFrames.size() == 0) {
LastError = "No alt frame rectangle config!";
return 0;
}
return AltFrames[0].w;
}
int w, h;
SDL_QueryTexture(Textures[0], NULL, NULL, &w, &h);
return h;
}
void TQSG_Image::KillAll() {
for (int i = 0; i < Textures.size(); ++i) SDL_DestroyTexture(Textures[i]);
}
void TQSG_Image::Hot(int x, int y) {
hotx = x;
hoty = y;
}
void TQSG_Image::HotCenter() {
hotx = floor(Width() / 2);
hoty = floor(Height() / 2);
}
void TQSG_Image::HotBottomCenter() {
hotx = floor(Width() / 2);
hoty = Height();
}
void TQSG_Image::HotGet(int& x, int& y) {
x = hotx;
y = hoty;
}
TQSG_Image::~TQSG_Image() {
if (AutoClean) KillAll();
}
std::vector<SDL_Texture*> TQSG_Image::GetTextures() {
return Textures;
}
bool TQSG_Image::AllowAltFraming() { return altframing; }
void SetScale(double x, double y) {
scalex = x;
scaley = y;
}
void GetScale(double& x, double& y) {
x = scalex;
y = scaley;
}
static int CLSR = 0, CLSG = 0, CLSB = 0;
void TQSG_ClsColor(int r, int g, int b) {
CLSR = r;
CLSG = g;
CLSB = b;
}
void TQSG_Cls() {
//SDL_FillRect(gScreenSurface, NULL, SDL_MapRGB(gScreenSurface->format, CLSR, CLSG, CLSB));
Uint8 r, g, b,a;
SDL_GetRenderDrawColor(gRenderer, &r, &g, &b, &a);
SDL_SetRenderDrawColor(gRenderer, CLSR, CLSG, CLSB, 255);
SDL_RenderClear(gRenderer);
SDL_SetRenderDrawColor(gRenderer, r,g,b,a);
}
void TQSG_SetBlend(SDL_BlendMode BM) { BlendMode = BM; }
void TQSG_SetBlend(TQSG_Blend BM) { BlendMode = (SDL_BlendMode)BM; }
TQSG_Blend TQSG_GetBlend() { return (TQSG_Blend)BlendMode; }
void TQSG_SetBlitzBlend(int i) {
/*
* Const MASKBLEND=1
Const SOLIDBLEND=2
Const ALPHABLEND=3
Const LIGHTBLEND=4
Const SHADEBLEND=5
*/
switch (i) {
case 0:
case 3:
TQSG_SetBlend(TQSG_Blend::ALPHA);
break;
case 4:
TQSG_SetBlend(TQSG_Blend::ADDITIVE);
break;
default:
cout << "ERROR! Unknown blitz blend (" << i << ")\n";
break;
}
}
void TQSG_Plot(int x, int y) {
SDL_SetRenderDrawColor(gRenderer, tcr, tcg, tcb,tcalpha);
SDL_RenderDrawPoint(gRenderer,x, y);
}
int TQSG_ScreenWidth() {
int w, h;
SDL_GetRendererOutputSize(gRenderer, &w, &h);
return w;
}
int TQSG_ScreenHeight() {
int w, h;
SDL_GetRendererOutputSize(gRenderer, &w, &h);
return h;
}
int TQSG_DesktopWidth() {
SDL_DisplayMode mode;
SDL_GetDesktopDisplayMode(0, &mode);
return mode.w;
}
int TQSG_DesktopHeight() {
SDL_DisplayMode mode;
SDL_GetDesktopDisplayMode(0, &mode);
return mode.h;
}
void TQSG_DesktopSize(int &w,int &h){
SDL_DisplayMode mode;
SDL_GetDesktopDisplayMode(0, &mode);
w = mode.w;
h = mode.h;
}
SDL_Window* TQSG_Window() {
return gWindow;
}
void TQSG_SetOrigin(int x, int y) {
TQSG_OriginX = x;
TQSG_OriginY = y;
}
void TQSG_GetOrigin(int& x, int& y) {
x = TQSG_OriginX;
y = TQSG_OriginY;
}
void TQSG_SetMouse(int x, int y) {
SDL_WarpMouseInWindow(gWindow, x, y);
}
void TQSG_ScreenSize(int* w, int* h) {
SDL_GetRendererOutputSize(gRenderer, w, h);
}
void TQSG_ViewPort(int x, int y,int w,int h) {
SDL_Rect R;
R.x = x;
R.y = y;
R.w = w;
R.h = h;
if (SDL_RenderSetViewport(gRenderer, &R) != 0) {
cout << "\x7\x1b[31mERROR!\x1b[0m\t TQSQ_ViewPort(" << x << "," << y << "," << w << "," << h << "): " << SDL_GetError() << endl;
}
//SDL_RenderSetClipRect(gRenderer, &R);
}
void TQSG_ViewPort() {
int w, h;
//SDL_GetRendererOutputSize(gRenderer, &w, &h);
if (SDL_RenderSetViewport(gRenderer, NULL) != 0) {
cout << "\x7\x1b[31mERROR!\x1b[0m\t TQSQ_ViewPort(): " << SDL_GetError() << endl;
}
//TQSG_ViewPort(0, 0, w, h);
}
void TQSG_GetViewPort(int* x, int* y, int* w, int* h) {
SDL_Rect R;
SDL_RenderGetViewport(gRenderer, &R);
//SDL_RenderGetClipRect(gRenderer, &R);
*x = R.x;
*y = R.y;
*w = R.w;
*h = R.h;
}
void TQSG_Rotate(double degrees) { rotatedegrees = degrees; }
void TQSG_RotateRAD(double radians) { rotatedegrees = radians * (180 / M_PI); }
void TQSG_RotateGRAD(double gradians) { rotatedegrees = gradians * 180 / 200; }
double TQSG_Rotate() { return rotatedegrees; }
double TQSG_RotateRAD() { return rotatedegrees * M_PI / 180; }
double TQSG_RotateGRAD() { return rotatedegrees * (200 / 180); }
void TQSG_VP(int x, int y, int w, int h) {
_VP.x = x;
_VP.y = y;
_VP.w = w;
_VP.h = h;
}
void TQSG_VP() {
_VP.x = 0;
_VP.y = 0;
_VP.w = TQSG_ScreenWidth();
_VP.h = TQSG_ScreenHeight();
}
void TQSG_GetVP(int* x, int* y, int* w, int* h) {
*x = _VP.x;
*y = _VP.y;
*w = _VP.h;
*h = _VP.h;
}
void TQSG_Flip(int minticks) {
//SDL_UpdateWindowSurface(gWindow);
static auto oud{ SDL_GetTicks() };
static auto mt{ 0 };
if (minticks >= 0) mt = minticks;
while (minticks && (SDL_GetTicks() - oud < mt)) SDL_Delay(1);
oud = SDL_GetTicks();
SDL_RenderPresent(gRenderer);
}
void TQSG_Color(Uint8 r, Uint8 g, Uint8 b) {
tcr = r;
tcg = g;
tcb = b;
}
void TQSG_GetColor(Uint8* r, Uint8* g, Uint8* b) {
*r = tcr;
*g = tcg;
*b = tcb;
}
void TQSG_ColorHSV(double Hue, double Saturation, double Value) {
hsv _hsv{ Hue,Saturation,Value };
auto _rgb = hsv2rgb(_hsv);
Uint8 R = floor(_rgb.r * 255.0);
Uint8 G = floor(_rgb.g * 255.0);
Uint8 B = floor(_rgb.b * 255.0);
TQSG_Color(R, G, B);
}
void TQSG_SetAlpha(Uint8 a) {
tcalpha = a;
}
Uint8 TQSG_GetAlpha() {
return tcalpha;
}
void TQSG_Rect(int x, int y, int w, int h,bool open) {
//SDL_FillRect(gScreenSurface, NULL, SDL_MapRGB(gScreenSurface->format, 0xFF, 0x00, 0xFF));
//printf("WARNING! Rect not fully implemented yet");
SDL_Rect r;
r.x = x;
r.y = y;
r.w = w;
r.h = h;
SDL_SetRenderDrawBlendMode(gRenderer,BlendMode);
SDL_SetRenderDrawColor(gRenderer, tcr, tcg, tcb, tcalpha);
if (open)