-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGCP_Draw.cpp
931 lines (817 loc) · 29.7 KB
/
GCP_Draw.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
#include "GCP_Math.h"
#include "GCP_Draw.h"
#include "SDL_ttf.h"
#include "SDL2_gfxPrimitives.h"
using namespace gcp;
gcpUint8 GCP_Draw_SDL2::_currentAlpha = 255;
gcpUint8 GCP_Draw_SDL2::_originalAlpha = 255;
void GCP_Draw_SDL2::SetAlpha(gcpUint8 alpha)
{
_currentAlpha = alpha;
}
gcpUint8 GCP_Draw_SDL2::GetAlpha()
{
return _currentAlpha;
}
void GCP_Draw_SDL2::ResetAlpha()
{
_currentAlpha = _originalAlpha;
}
void GCP_Draw_SDL2::Draw_Line(const GCP_Point<int> &A, const GCP_Point<int> &B, const GCP_Color &color, short bold) const
{
Draw_Line(A.X, A.Y, B.X, B.Y, color, bold);
}
void GCP_Draw_SDL2::Draw_Line(int x, int y, int x2, int y2, const GCP_Color &color, short bold) const
{
Uint8 r,g,b,a;
SDL_GetRenderDrawColor(_screen,&r,&g,&b,&a);
SDL_SetRenderDrawColor(_screen,color.r,color.g,color.b,color.a);
switch(bold)
{
case 2:
SDL_RenderDrawLine(_screen,x,y,x2,y2);
SDL_RenderDrawLine(_screen,x+1,y,x2+1,y2);
SDL_RenderDrawLine(_screen,x-1,y,x2-1,y2);
SDL_RenderDrawLine(_screen,x,y+1,x2,y2+1);
SDL_RenderDrawLine(_screen,x,y-1,x2,y2-1);
break;
default:
SDL_RenderDrawLine(_screen,x,y,x2,y2);
}
SDL_SetRenderDrawColor(_screen,r,g,b,a);
}
void GCP_Draw_SDL2::Draw_Round(int x, int y, int width, int height, short scale, const GCP_Color &color) const
{
//!!! scale íå ó÷èòûâàåñÿ!!!
Uint8 r,g,b,a;
SDL_GetRenderDrawColor(_screen,&r,&g,&b,&a);
SDL_SetRenderDrawColor(_screen,color.r,color.g,color.b,_currentAlpha);
SDL_Rect recta = {x,y,width,height};
SDL_RenderDrawRect(_screen,&recta);
SDL_SetRenderDrawColor(_screen,r,g,b,a);
}
void GCP_Draw_SDL2::Draw_Round(const GCP_Rect<int>& rect, short scale, const GCP_Color &color) const
{
Draw_Round(rect.x(), rect.y(), rect.width(), rect.height(), scale, color);
}
void GCP_Draw_SDL2::Draw_FillRound(const GCP_Rect<int>& rect, int scale, const GCP_Color &color) const
{
Draw_FillRound(rect.x(), rect.y(), rect.width(), rect.height(), scale, color);
}
void GCP_Draw_SDL2::Draw_FillRound(int x, int y, int width, int height, int scale, const GCP_Color &color) const
{
//!!! scale íå ó÷èòûâàåñÿ!!!
Uint8 r,g,b,a;
SDL_GetRenderDrawColor(_screen,&r,&g,&b,&a);
SDL_SetRenderDrawColor(_screen,color.r,color.g,color.b, _currentAlpha);
SDL_Rect recta = {x,y,width,height};
SDL_RenderFillRect(_screen,&recta);
SDL_SetRenderDrawColor(_screen,r,g,b,a);
}
void GCP_Draw_SDL2::Draw_Rect(const GCP_Rect<int> &rect, const GCP_Color &color) const
{
Draw_Rect(rect.x(), rect.y(), rect.width(), rect.height(), color);
}
void GCP_Draw_SDL2::Draw_Rect(int x, int y, int width, int height, const GCP_Color &color) const
{
Uint8 r,g,b,a;
SDL_GetRenderDrawColor(_screen,&r,&g,&b,&a);
SDL_SetRenderDrawColor(_screen,color.r,color.g,color.b, _currentAlpha);
SDL_Rect recta = {x,y,width,height};
SDL_RenderDrawRect(_screen,&recta);
SDL_SetRenderDrawColor(_screen,r,g,b,a);
}
void GCP_Draw_SDL2::Draw_FillRect(const GCP_Rect<int> &rect, const GCP_Color &color) const
{
Draw_FillRect(rect.x(), rect.y(), rect.width(), rect.height(), color);
}
void GCP_Draw_SDL2::Draw_FillRect(int x, int y, int width, int height, const GCP_Color &color) const
{
Uint8 r,g,b,a;
SDL_GetRenderDrawColor(_screen,&r,&g,&b,&a);
SDL_SetRenderDrawColor(_screen,color.r,color.g,color.b,_currentAlpha);
SDL_Rect recta = {x,y,width,height};
SDL_RenderFillRect(_screen,&recta);
SDL_SetRenderDrawColor(_screen,r,g,b,a);
}
void GCP_Draw_SDL2::Draw_Polygon(const GCP_Vector<GCP_PointInt> &polygon, const GCP_Color &color) const
{
Sint16 *xv = new Sint16[polygon.size()];
Sint16 *yv = new Sint16[polygon.size()];
for (int i = 0; i < polygon.size(); i++)
{
xv[i] = polygon.at(i).X;
yv[i] = polygon.at(i).Y;
}
polygonRGBA(_screen, xv, yv, polygon.size(), color.r,color.g,color.b, _currentAlpha);
delete xv;
delete yv;
}
void GCP_Draw_SDL2::Draw_FillPolygon(const GCP_Vector<GCP_PointInt> &polygon, const GCP_Color &color) const
{
Sint16 *xv = new Sint16[polygon.size()];
Sint16 *yv = new Sint16[polygon.size()];
for (int i = 0; i < polygon.size(); i++)
{
xv[i] = polygon.at(i).X;
yv[i] = polygon.at(i).Y;
}
filledPolygonRGBA(_screen, xv, yv, polygon.size(), color.r,color.g,color.b, _currentAlpha);
delete xv;
delete yv;
}
void GCP_Draw_SDL2::Draw_FillEllipse(int x0, int y0, int Xradius, int Yradius, const GCP_Color &color) const
{
//Ðèñóåì åëëèïñ ïî òî÷êàì
//Îðèãèíàëüíûé êîä âçÿò èç SDL_draw-1.2.13
Uint8 r,g,b,a;
SDL_GetRenderDrawColor(_screen,&r,&g,&b,&a);
SDL_SetRenderDrawColor(_screen,color.r,color.g,color.b,_currentAlpha);
Sint32 x, y;
Sint32 Xchange, Ychange;
Sint32 EllipseError;
Sint32 TwoASquare, TwoBSquare;
Sint32 StoppingX, StoppingY;
TwoASquare = 2*Xradius*Xradius;
TwoBSquare = 2*Yradius*Yradius;
/*1st set of points*/
x = Xradius-1; /*radius zero == draw nothing*/
y = 0;
Xchange = Yradius*Yradius*(1-2*Xradius);
Ychange = Xradius*Xradius;
EllipseError = 0;
StoppingX = TwoBSquare*Xradius;
StoppingY = 0;
/*Plot four ellipse points by iteration*/
while (StoppingX > StoppingY) {
SDL_RenderDrawLine(_screen,x0+x,y0+y,x0+x,y0-y);
SDL_RenderDrawLine(_screen,x0-x,y0+y,x0-x,y0-y);
++y;
StoppingY += TwoASquare;
EllipseError += Ychange;
Ychange += TwoASquare;
if (( 2*EllipseError + Xchange) > 0) {
--x;
StoppingX -= TwoBSquare;
EllipseError += Xchange;
Xchange += TwoBSquare;
}
}/*while*/
/*2nd set of points*/
x = 0;
y = Yradius-1; /*radius zero == draw nothing*/
Xchange = Yradius*Yradius;
Ychange = Xradius*Xradius*(1-2*Yradius);
EllipseError = 0;
StoppingX = 0;
StoppingY = TwoASquare*Yradius;
/*Plot four ellipse points by iteration*/
while (StoppingX < StoppingY) {
SDL_RenderDrawLine(_screen,x0+x,y0+y,x0+x,y0-y);
SDL_RenderDrawLine(_screen,x0-x,y0+y,x0-x,y0-y);
++x;
StoppingX += TwoBSquare;
EllipseError += Xchange;
Xchange += TwoBSquare;
if ((2*EllipseError + Ychange) > 0) {
--y;
StoppingY -= TwoASquare;
EllipseError += Ychange;
Ychange += TwoASquare;
}
}
SDL_SetRenderDrawColor(_screen,r,g,b,a);
/*Draw_Ellipse*/
}
void GCP_Draw_SDL2::Draw_Ellipse(int x0, int y0, int Xradius, int Yradius, const GCP_Color &color) const
{
//Ðèñóåì åëëèïñ ïî òî÷êàì
//Îðèãèíàëüíûé êîä âçÿò èç SDL_draw-1.2.13
Uint8 r,g,b,a;
SDL_GetRenderDrawColor(_screen,&r,&g,&b,&a);
SDL_SetRenderDrawColor(_screen,color.r,color.g,color.b,_currentAlpha);
Sint32 x, y;
Sint32 Xchange, Ychange;
Sint32 EllipseError;
Sint32 TwoASquare, TwoBSquare;
Sint32 StoppingX, StoppingY;
TwoASquare = 2*Xradius*Xradius;
TwoBSquare = 2*Yradius*Yradius;
/*1st set of points*/
x = Xradius-1; /*radius zero == draw nothing*/
y = 0;
Xchange = Yradius*Yradius*(1-2*Xradius);
Ychange = Xradius*Xradius;
EllipseError = 0;
StoppingX = TwoBSquare*Xradius;
StoppingY = 0;
/*Plot four ellipse points by iteration*/
while (StoppingX > StoppingY) {
SDL_RenderDrawPoint(_screen,x0+x,y0+y);
SDL_RenderDrawPoint(_screen,x0+x,y0-y);
SDL_RenderDrawPoint(_screen,x0-x,y0+y);
SDL_RenderDrawPoint(_screen,x0-x,y0-y);
++y;
StoppingY += TwoASquare;
EllipseError += Ychange;
Ychange += TwoASquare;
if (( 2*EllipseError + Xchange) > 0) {
--x;
StoppingX -= TwoBSquare;
EllipseError += Xchange;
Xchange += TwoBSquare;
}
}/*while*/
/*2nd set of points*/
x = 0;
y = Yradius-1; /*radius zero == draw nothing*/
Xchange = Yradius*Yradius;
Ychange = Xradius*Xradius*(1-2*Yradius);
EllipseError = 0;
StoppingX = 0;
StoppingY = TwoASquare*Yradius;
/*Plot four ellipse points by iteration*/
while (StoppingX < StoppingY) {
SDL_RenderDrawPoint(_screen,x0+x,y0+y);
SDL_RenderDrawPoint(_screen,x0+x,y0-y);
SDL_RenderDrawPoint(_screen,x0-x,y0+y);
SDL_RenderDrawPoint(_screen,x0-x,y0-y);
++x;
StoppingX += TwoBSquare;
EllipseError += Xchange;
Xchange += TwoBSquare;
if ((2*EllipseError + Ychange) > 0) {
--y;
StoppingY -= TwoASquare;
EllipseError += Ychange;
Ychange += TwoASquare;
}
}
SDL_SetRenderDrawColor(_screen,r,g,b,a);
/*Draw_Ellipse*/
}
/////////////////////////////////////////////////////////////////////////
///////////////////////DRAW Graphic
/////////////////////////////////////////////////////////////////////////
void GCP_Draw_SDL2::Draw_Graphic(int* data, int data_size, int x, int y, int width, int height) const
{
//int data_size = sizeof(&data)/sizeof(int);
//Ðèñóåì !!!êîðÿâûé!!! ãðàôèê.
//d - ìàññèâ çíà÷åíèé.
//x , y - ëåâûé âåðõíèé óãîë
x+=1; y+=1; width -= 2; height -=2; //Îòñóòï äëÿ ðàìî÷êè
GCP_Color color = c_red;
int it;
double i , _x, _y, _x2, _y2,percent;
double distance = 4; //Ìèíèìàëüíîå ðàññòîÿíèå ìåæäó òî÷êàìè
int drawing_points = (int)(width/distance); //êîëè÷åñòâî òî÷åê êîòîðûå ìû íàðèñóåì
int iteration = 1; //èòåðàöèÿ ñ êîòîðîé ìû ïåðåáèðàåì ìàññèâ
int tmp_data_size = data_size; //Íàðàñ÷èâàåì èòåðàöèþ åñëè ìàññèâ íå ïîìåùàåòñÿ
while(tmp_data_size>drawing_points) { //Óõóäøàåì èíôîðìàòèâíîñòü ãðàôèêà
tmp_data_size/=2;
iteration *=2;
}
tmp_data_size -=1; //Ñäâèã äëÿ ðàñ÷åòà ÿ÷ååê
distance = (double)width/(tmp_data_size); //Ðàñ÷èòûâàåì íîâîå ìèíèìàëüíîå ðàññòîÿíèå
double insert = (distance- floor(distance)); //Êîðåëÿöèÿ
distance = floor(distance);
int max = abs(data[0]); //åêñòðåìàëüíîå çíà÷åíèå ãðàôèêà
for(it = 0; it<data_size; it+=iteration) {
if (abs(data[it]) > max)
max = abs(data[it]);
}
if(max==0) max = 1;
int draw_point;
double insert_sum=0;
for(it = iteration, i = x+distance, draw_point=0; draw_point < tmp_data_size;
i+=distance, it+=iteration, insert_sum +=insert, draw_point++)
{
_x = i-distance; //èêñ îòðèñîâêè
_y = data[(int)(it-iteration)]; //èãðåê çíà÷åíèå èç ãðàôèêà
percent = (double)(_y*100);
percent /= max; //ðàñ÷èòûâàåì åãî ïðîöåíò îòíîñèòåëüíî åêñòðåìàëüíîãî çíà÷åíèÿ (ñî çíàêîì)
_y = y + height/2 - percent*(height-20)/200; //èãðåê îòðèñîâêè. íà÷èíàåì ðèñîâàòü îò ñåðåäèíû ïðÿìîóãîëüíèêà
//è ïðèáàâëÿåì ïðîöåíò îò âûñîòû âñåãî ïðÿìîóãîëüíèêà (âûñîòà ïîïîëàì)
_x2 = i;
_y2 = data[it];
percent = (double)(_y2*100);
percent /= max;
_y2 = y + height/2 - percent*(height-20)/200;
drawLine(GCP_Point<int>(_x,_y),GCP_Point<int>(_x2,_y2),c_red,0);
if(insert_sum > 1) {
insert_sum = insert_sum-1;
i+=1;
}
}
/*std::string stringstd;
MyMath::StringToStd(Convert::ToString(data_size),stringstd);
SDL_Main::RenderTextToSurface(stringstd,x+width-50,y+height-20,screen,c_black,c_white);
MyMath::StringToStd(Convert::ToString(max),stringstd);
SDL_Main::RenderTextToSurface(stringstd,x+5,y+5,screen,c_black,c_white);
SDL_Main::DrawRect(screen,PointD(x,y),PointD(x+width,y+height),c_black,1);
SDL_Main::DrawLine(screen,PointD(x,y+height/2),PointD(x+width,y+height/2),c_black,0);*/
}
/////////////////////////////////////////////////////////////////////////
///////////////////////DRAW RECTANGLE
/////////////////////////////////////////////////////////////////////////
void GCP_Draw_SDL2::drawRect(const GCP_Point<int> &pointTopLeft, const GCP_Point<int> &pointBottomRight, const GCP_Color &color, int bold) const
{
drawLine( pointTopLeft, GCP_Point<int>(pointBottomRight.X,pointTopLeft.Y),color,bold);
drawLine(pointTopLeft, GCP_Point<int>(pointTopLeft.X, pointBottomRight.Y), color, bold);
drawLine(pointBottomRight, GCP_Point<int>(pointBottomRight.X, pointTopLeft.Y), color, bold);
drawLine(pointBottomRight, GCP_Point<int>(pointTopLeft.X, pointBottomRight.Y), color, bold);
}
/////////////////////////////////////////////////////////////////////////
////////////////////////DRAW LINE
/////////////////////////////////////////////////////////////////////////
void GCP_Draw_SDL2::drawLine(const GCP_Point<int> &pointA, const GCP_Point<int> &pointB, const GCP_Color &color, int bold) const
{
/////////////////////////////////////ÇÀÊÎÌÌÅÍÒÈÐÎÂÀÍÀ ÏÐÎÂÅÐÊÀ ÂÛÕÎÄÀ ËÈÍÈÈ ÇÀ ÁÓÔÅÐ
/////////////////////////////////////(ÍÅ ÒÐÅÁÓÅÒÑß Â SDL2.0)
////Îòñòóï îò ãðàíèöû ýêðàíà
//int bs = 4;
//GCP_Point pointA_norm,pointB_norm;
//pointA_norm = pointA;// = PointD(639,470);//pointA; //557.178 565.636
//pointB_norm = pointB;// = PointD(595.648,564.464);//pointB; //559.089 -437.361
//
//bool isPointAinRect = GCP_Math::isInRect(pointA.X,pointA.Y,bs,bs,w,h);
//bool isPointBinRect = GCP_Math::isInRect(pointB.X,pointB.Y,bs,bs,w,h);
//
//GCP_Vector<GCP_Point> R;
////Åñëè õîòü îäíà òî÷êà âûõîäèò çà ãðàíèöó ïîâåðõíîñòè
//if ( !isPointAinRect || !isPointBinRect) {
// //Íàéòè âñå ïåðåñå÷åíèÿ ëèíèè èç òî÷êè À â òî÷êó Á ñ ãðàíèöàìè ïîâåðõíîñòè (*èìåííî ëèíèè, à íå ëó÷à)
//
// GCP_Math::lineRectIntersection(GCP_Point (bs,bs),GCP_Point (w-bs,bs),GCP_Point (bs,h-bs), GCP_Point (w-bs,h-bs),
// GCP_Line(pointA,pointB),&R);
// switch(R.size())
// {
// case 0:
// //Âèäèìî îáå òî÷êè ðàñïîëîæåíû âíå ïîâåðõíîñòè. Ðèñîâàòü íå÷åãî
// return;
// break;
// case 1:
// if(isPointAinRect) {
// pointA_norm = pointA;
// pointB_norm = R[0];
// }
// else {
// pointA_norm = R[0];
// pointB_norm = pointB;
// }
// break;
// case 2:
// case 3: // óãëàõ ìîæåò íàéòè äâå òî÷êè ïåðåñå÷åíèÿ
// pointA_norm = R[0];
// pointB_norm = R[1];
// break;
// }
//}
////Íà âñå îñòàâøèåñÿ èñêëþ÷åíèÿ
//pointA_norm = GCP_Math::normalizePointInRect(pointA_norm,GCP_Point(bs,bs),GCP_Point(w-bs,h-bs));
//pointB_norm = GCP_Math::normalizePointInRect(pointB_norm,GCP_Point(bs,bs),GCP_Point(w-bs,h-bs));
//SLD2 àâòîìàòè÷åñêè îáðàáàòûâàåò èñêëþ÷åíèå åñëè ìû ðèñóåì ëèíèþ çà ïðåäåëàìè áóôåðà
GCP_Point<int> pointA_norm,pointB_norm;
pointA_norm = pointA;
pointB_norm = pointB;
switch(bold)
{
case 0:
Draw_Line((int)pointA_norm.X,(int)pointA_norm.Y,(int)pointB_norm.X,(int)pointB_norm.Y,color); break;
case 1:
Draw_Line((int)pointA_norm.X,(int)pointA_norm.Y,(int)pointB_norm.X,(int)pointB_norm.Y,color);
Draw_Line((int)pointA_norm.X+1,(int)pointA_norm.Y,(int)pointB_norm.X+1,(int)pointB_norm.Y,color);
Draw_Line((int)pointA_norm.X-1,(int)pointA_norm.Y,(int)pointB_norm.X-1,(int)pointB_norm.Y,color);
Draw_Line((int)pointA_norm.X,(int)pointA_norm.Y+1,(int)pointB_norm.X,(int)pointB_norm.Y+1,color);
Draw_Line((int)pointA_norm.X,(int)pointA_norm.Y-1,(int)pointB_norm.X,(int)pointB_norm.Y-1,color);
break;
}
}
/////////////////////////////////////////////////////////////////////////
////////////////////////RENDER TEXT TO SURFACE
/////////////////////////////////////////////////////////////////////////
void GCP_Draw_SDL2::applySurface(int x, int y, SDL_Texture *tex, SDL_Renderer* render) const
{
//First we must create an SDL_Rect for the position of the image, as SDL
//won't accept raw coordinates as the image's position
SDL_Rect pos;
pos.x = x;
pos.y = y;
//We also need to query the texture to get its width and height to use
SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h);
SDL_RenderCopy(render, tex, NULL, &pos);
}
SDL_Texture* GCP_Draw_SDL2::renderText(GCP_DrawData* drawdata, const std::string &message, const gcp_spStyle &style) const
{
//!!!!!!!!!!!!!!!
//!!!!!!!!!!!!!!!
//ÍÓÆÍÎ ÐÀÇÎÁÐÀÒÜÑß Ñ ÂÛÂÎÄÎÌ ÒÅÊÑÒÀ
//ÑÄË ÏËÎÕÎ ÐÈÑÓÅÒ ÒÅÊÑÒ
//ÂÎÇÌÎÆÍÎ ÑÒÎÈÒ ÏÎÈÑÊÀÒÜ ÄÐÓÃÈÅ ÐÅØÅÍÈß ÍÀ OPENGL íàïðèìåð â GLEW/GLUT
//!!!!!!!!!!!!!!!
//!!!!!!!!!!!!!!!
SDL_Texture* texture = NULL;
TTF_Font *pFont = TTF_OpenFont(GCP_Draw::getFontPath(style->font.type).c_str(),style->font.size);
if (pFont == NULL)
return texture;
//throw std::runtime_error("Failed to load font: " + fontFile + TTF_GetError());
/*const std::string input = message; // null-call to get the size
size_t needed = ::mbstowcs(NULL,&input[0],input.length()); // allocate
std::wstring output;
output.resize(needed); // real call
::mbstowcs(&output[0],&input[0],input.length()); // You asked for a pointer
const wchar_t *pout = output.c_str();
//TTF_RenderUNICODE_Solid
//TTF_RenderUTF8_Blended
SDL_Surface *surf = TTF_RenderUNICODE_Solid(font,(Uint16*)pout, color);
*/
SDL_Color clr;
clr.r = style->textColor.r;
clr.g = style->textColor.g;
clr.b = style->textColor.b;
clr.a = _currentAlpha;
SDL_Surface *surf = TTF_RenderUTF8_Blended(pFont, message.c_str(), clr);
/*texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
surf->w, surf->h);*/
//SDL_UpdateTexture(texture, NULL, screen->pixels, screen->pitch);
////Ñîçäàäèì ïðîòîòèï ýêðàíà
////Çàïîëíèì åãî áåëûì öâåòîì !! íàäî ñäåëàòü ïðîçðà÷í!
//SDL_Surface* surf2 = SDL_CreateRGBSurface(0,surf->w, surf->h,32,0,0,0,0);
//SDL_FillRect(surf2, &surf->clip_rect, SDL_MapRGBA(surf->format,255,255,255,0));
//////Ïðîçðà÷íîñòü ÍÅÐÀÁÎÒÀÅÒ"!
////Uint32 white = SDL_MapRGBA(drawdata->tscreen->format, 255, 255, 255,0);
////SDL_SetColorKey(drawdata->tscreen, true, white);
//////Íàðèñóåì òåêñò íà ïðîòîòèï ýêðàíà
//SDL_BlitSurface(surf,NULL,surf2,NULL);
////Îáíîâèì òåêñòóðó äëÿ ðåíäåðåðà
//SDL_UpdateTexture(texture, NULL, surf->pixels, surf->pitch);
//SDL_UpdateTexture(texture, &surf->clip_rect, surf->pixels, surf->pitch);
//texture = SDL_CreateTextureFromSurface(renderer, surf); //LOW PERFOMANCE USE UPDATE TEXTURE
//Clean up unneeded stuff
//SDL_FreeSurface(screen1);
TTF_CloseFont(pFont);
texture = SDL_CreateTextureFromSurface(_screen, surf); //LOW PERFOMANCE USE UPDATE TEXTURE
SDL_FreeSurface(surf);
return texture;
}
void GCP_Draw_SDL2::SetBlendMode(GCPE_BlendMode mode)
{
switch (mode)
{
case E_BLEND_NONE:
SDL_SetRenderDrawBlendMode(_screen, SDL_BLENDMODE_NONE);
break;
case E_BLEND_ADD:
SDL_SetRenderDrawBlendMode(_screen, SDL_BLENDMODE_BLEND);
break;
default:
break;
}
}
void GCP_Draw_SDL2::Draw_Image(const string &path, int x, int y) const
{
SDL_Surface *bmp = SDL_LoadBMP(path.c_str());
if(bmp != NULL){
//SDL_Rect recta = {(int)xPos+2,(int)yPos+2,width,height};
//SDL_BlitSurface( hello, NULL, screen, &recta );
//ãðóçèì êàðòèíêó â òåêñòóðó è âûâîäèì åå íà áóôåð
//SDL_SetColorKey(bmp, true, SDL_MapRGB(bmp->format, 255, 255, 255));
SDL_Texture *tex = SDL_CreateTextureFromSurface(_screen, bmp);
//SDL_SetTextureAlphaMod(tex, 233);
applySurface(x, y, tex, _screen);
SDL_FreeSurface( bmp );
SDL_DestroyTexture(tex); //Create texture should be once !!!!!!!!! not dynamic!!!!!
//!!!!!!! ñîçäàíèå òåêñòóðû è çàãðóçêà êàðòèíêè êàæäûé ðàç ýòî çàòðàòíî
//!!!!!!! íàäî äåëàòü ýòî îäèí ðàç, à ïîòîì ïðîñòî âûâîäèòü òåêñòóðó íà áóôôåð
}
}
int GCP_Draw_SDL2::GetTextSize(const std::string &text, int &width, int &height, const gcp_spStyle &style) const
{
width = 0;
height = 20;
//Âû÷èñëåíèå øèðèíû è âûñîòû òåêñòà
GCP_Vector<string>* strings = GCP_Math::strToLines(text);
bool hasIndex = false;
size_t maxSize = 0, maxSizeIndex = 0;
for (size_t i = 0; i < strings->size(); i++)
{
if (strings->at(i).length() > maxSize)
{
maxSize = strings->at(i).length();
maxSizeIndex = i;
hasIndex = true;
}
}
if (!hasIndex)
return -1;
TTF_Font* font = TTF_OpenFont(GCP_Draw::getFontPath(style->font.type).c_str(), style->font.size);
if (font == 0)
throw;
TTF_SizeUTF8(font, strings->at(maxSizeIndex).c_str(), &width, &height);
TTF_CloseFont(font);
size_t strSize = strings->size();
height *= strSize;
delete strings;
return strSize;
}
///
void GCP_Draw_SDL2::Draw_Text(const std::string &text, const GCP_Rect<int> &pos, const gcp_spStyle &pStyle, GCP_DrawData* drawdata) const
{
int iX, iY;
switch (pStyle->font.align)
{
case E_LEFT:
iX = pos.x();// + 3;
iY = pos.y();// + 3;
break;
case E_CENTER:
int width, height;
GetTextSize(text, width, height, pStyle);
iX = pos.x() + (pos.width() - width) / 2;
iY = pos.y() + (pos.height() - height) / 2;
break;
case E_RIGHT:
break;
default:
break;
}
Draw_Text(text, iX, iY, pStyle, drawdata);
}
///make this realisation private
void GCP_Draw_SDL2::Draw_Text(const std::string &text, int x, int y, const gcp_spStyle &pStyle, GCP_DrawData* drawdata) const
{
GCP_Color text_color = pStyle->textColor;
//stringRGBA (_screen, x, y, text.c_str(), text_color.r,text_color.g,text_color.b,text_color.a);
//return;
//SDL_Color color = {text_color.r,text_color.g,text_color.b,_currentAlpha};
SDL_Texture *image = NULL;
GCP_Vector<std::string> *strings = GCP_Math::strToLines(text);
//Åñëè òåêñòà áûëî äî ýòîãî âûâåäåíî áîëüøå. Óìåíüøèì ðàçìåð êåøà.
if(drawdata != NULL) {
while(drawdata->text.size()>strings->size())
{
drawdata->text.pop_back();
drawdata->theight.pop_back();
drawdata->twidth.pop_back();
drawdata->color.pop_back();
SDL_DestroyTexture(drawdata->texture.pop_back());
}
}
for(unsigned int j=0; j<strings->size(); j++)
{
if(drawdata == NULL)
{
//Åñëè ðèñóåì áåç êåøèðîâàíèÿ òî ïðîñòî ðåíäåðèì òåêñò íà åêðàí
image = renderText(drawdata, strings->at(j), pStyle);
applySurface(x, y + 20 * j, image, _screen);
SDL_DestroyTexture(image);
}else
{
//Èíà÷å åñëè èñïîëüçóåì êåøèðîâàíèå
if(drawdata->text.size()>j) //Åñëè ó íàñ åñòü ÿ÷åéêà â êåøå
{
if(drawdata->text[j] != strings->at(j) || drawdata->color[j] != text_color) //Åñëè ñòðîêè â íåé ðàçíÿòñÿ
{
//Ðèñóåì íîâûé òåêñò è âûâîäèì åãî íà åêðàí
image = renderText(drawdata, strings->at(j), pStyle);
applySurface(x, y+20*j, image, _screen);
//Çàïîìèíàåì êàêîé òåïåðü òåêñò íàðèñîâàí
drawdata->text[j]=strings->at(j);
//Çàïîìèíàåì åãî õàðàêòåðèñòèêè
drawdata->twidth[j]=x;
drawdata->theight[j]=y+20*j;
drawdata->color[j]=text_color;
//Î÷èùàåì ïðîøëóþ ïàìÿòü è çàìåíÿåì åå íîâîé
SDL_DestroyTexture(drawdata->texture[j]);
drawdata->texture[j]=image;
}
else
{
//Ñòðîêè íå ðàçíÿòñÿ ðèñóåì ïðîøëóþ ïàìÿòü
//Íî ñíà÷àëà ïðîâåðèì íå èçìåíèëèñü ëè êîîðäèíàòû òåêñòà
if(drawdata->twidth[j] != x)
drawdata->twidth[j] = x;
if(drawdata->theight[j] != y+20*j)
drawdata->theight[j] = y+20*j;
applySurface(drawdata->twidth[j], drawdata->theight[j], drawdata->texture[j], _screen);
}
}else
{
//ß÷åéêè íåò. íàäî åå ñîçäàòü
image = renderText(drawdata, strings->at(j), pStyle);//20 should be defined by font size
applySurface(x, y+20*j, image, _screen);
drawdata->text.push_back( strings->at(j));
drawdata->texture.push_back( image);
drawdata->twidth.push_back(x);
drawdata->theight.push_back(y+20*j);
drawdata->color.push_back(text_color);
}
}
}
delete strings;
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////COLORS
/////////////////////////////////////////////////////////////////////////
void GCP_Draw::initConstants()
{
//Êîíñòàíòû íå îáúÿâëÿþòñÿ â íåêîòîðûõ êîìïèëÿòîðàõ åñëè íàõîäÿòñÿ âíå ôóíêöèé. ïðèøëîñü ñäåëàòü èõ ïðååìåííûìè
c_white = GCP_Color(255, 255, 255);
c_black = GCP_Color( 0, 0, 0);
c_aquadark = GCP_Color( 121, 188, 255);
c_yellow = GCP_Color( 255, 255, 128);
c_red = GCP_Color( 255, 23, 23);
c_blue = GCP_Color( 29, 23, 255);
c_lime = GCP_Color( 60, 240, 50);
c_green = GCP_Color( 10, 135, 3);
c_aqua = GCP_Color( 64, 244, 231);
c_orange = GCP_Color( 255, 130, 5);
c_grey = GCP_Color( 125, 125, 125);
c_dkgrey = GCP_Color(75, 75, 75);
GCP_DefaultStyle = gcp_spStyle(new GCP_Style(E_DEFAULT));
GCP_DefaultMessageBoxStyle = gcp_spStyle(new GCP_Style(E_MESSAGEBOX));
GCP_ButtonBlackStyle = gcp_spStyle(new GCP_Style(E_BUTTON));
GCP_ButtonWhiteStyle = gcp_spStyle(new GCP_Style(E_BUTTONWHITE));
GCP_PanelBlackStyle = gcp_spStyle(new GCP_Style(E_PANELBLACK));
}
//does not work when entry point defined
namespace gcp
{
GCP_Color c_white = GCP_Color(255, 255, 255);
GCP_Color c_black = GCP_Color(0, 0, 0);
GCP_Color c_aquadark = GCP_Color(121, 188, 255);
GCP_Color c_yellow = GCP_Color(255, 255, 128);
GCP_Color c_red = GCP_Color(255, 23, 23);
GCP_Color c_dkred = GCP_Color(225, 23, 23);
GCP_Color c_blue = GCP_Color(29, 23, 255);
GCP_Color c_lime = GCP_Color(60, 240, 50);
GCP_Color c_green = GCP_Color(10, 135, 3);
GCP_Color c_aqua = GCP_Color(64, 244, 231);
GCP_Color c_orange = GCP_Color(255, 130, 5);
GCP_Color c_grey = GCP_Color(125, 125, 125);
GCP_Color c_dkgrey = GCP_Color(75, 75, 75);
GCP_SPointer<GCP_Style> GCP_DefaultStyle;
GCP_SPointer<GCP_Style> GCP_DefaultMessageBoxStyle;
GCP_SPointer<GCP_Style> GCP_ButtonBlackStyle;
GCP_SPointer<GCP_Style> GCP_ButtonWhiteStyle;
GCP_SPointer<GCP_Style> GCP_PanelBlackStyle;
GCP_Draw_Base* GCP_Draw::_drawbase = NULL;
}
////////////////////////////////////////////////////////////////
////////////////////////TO SDL COLOR
////////////////////////////////////////////////////////////////
SDL_Color GCP_Draw_SDL2::toSDLColor(Uint32 int_color)
{
//Change from an "int color" to an SDL_Color
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
SDL_Color color={(int_color & 0x00ff0000)/0x10000,(int_color &0x0000ff00)/0x100,(int_color & 0x000000ff),0};
#else
SDL_Color color={(int_color & 0x000000ff),(int_color &0x0000ff00)/0x100,(int_color & 0x00ff0000)/0x10000,0};
#endif
return color;
}
////////////////////////////////////////////////////////////////
////////////////////////PRINT STRINGS
////////////////////////////////////////////////////////////////
//SDL_Surface* GCP_Draw::printStrings(SDL_Surface *dest, TTF_Font *font,
// std::string str, SDL_Rect &rc, SDL_Color clrFg,
// SDL_Color clrBg, int flags )
//{
//Ñòàðûé êîä èç SDL-Draw
///* This function prints "str" with font "font" and color "clrFg"
// * onto a rectangle of color "clrBg".
// * It does not pad the text.
// * If CREATE_SURFACE is NOT passed, the function returns NULL,
// *otherwise, it returns an SDL_Renderer * pointer.
// */
// // If there's nothing to draw, return NULL:
//if( str.length() == 0 || font == NULL ) {
//return( NULL );
// }
// // This is the surface that everything is printed to.
// SDL_Renderer *sText = NULL;
// int lineSkip = TTF_FontLineSkip( font );
// int width = 10, height = 10;
// std::vector<std::string> vLines;
//
// // Break the String into its lines:
// int n = 0;
// while( n != -1 ) {
//// Get until either "\n" or "\0":
//std::string strSub;
//n = str.find('\\',0); //replace \n text occurancies
//if (n != -1 && str[n+1] == 'n')
//{
// strSub = str.substr( 0,n );
// n = n+1;
//}
//else
//{
// n = str.find( '\n', 0 );
// strSub = str.substr( 0,n );
//}
//if( n != -1 ) {
// str = str.substr( n+1, -1 );
//}
//vLines.push_back(strSub);
//
//int w = 0;
//// Get the size of the rendered text.
//TTF_SizeText( font, strSub.c_str(), &w,&height );
//if( w > width ) { width = w; }
// }
//
// // vLines.size() == Number of Lines.
// // we assume that height is the same for all lines.
// height = (vLines.size()-1) * lineSkip + height;
//
// // dest CAN'T be NULL if you're creating the surface!
// if( dest != NULL && (flags&PS_CREATE_SURFACE) ) {
//printf("dest != NULL with PS_CREATE_SURFACE!\n");
//return(NULL);
// }
// if( dest == NULL ) {
//if( flags&PS_CREATE_SURFACE ) {
// // Create a "dest" to which to print:
// dest = SDL_CreateRGBSurface( SDL_SWSURFACE, width,height, 1, 0,0,0,0 );
//} else {
// printf("There was no surface.\n(Exiting function...)\n");
// return(NULL);
//}
// }
//
// sText = SDL_CreateRGBSurface( SDL_SWSURFACE, width,height,
//dest->format->BitsPerPixel, dest->format->Rmask,dest->format->Gmask,
//dest->format->Bmask,dest->format->Amask );
// // Color in the rectangle on the destination:
// if( flags &PS_TRANSPARENT_BG ) {
//// If the fg & bg colors are the same, we need to fix it:
///*if( clrFg == clrBg ) {
// if( clrFg == BLACK ) {
// clrBg = WHITE;
// } else {
// clrBg = BLACK;
// }
//}*/
//
// /*
//SDL_FillRect( sText,NULL,
// SDL_MapRGB(sText->format,clrBg.r,clrBg.g,clrBg.b) );
// SDL_SetColorKey( sText, SDL_SRCCOLORKEY|SDL_RLEACCEL,
// SDL_MapRGB(sText->format,clrBg.r,clrBg.g,clrBg.b) );*/
// SDL_FillRect( sText,NULL, SDL_MapRGB(sText->format,clrBg.r,clrBg.g,clrBg.b) );
// } else {
//SDL_FillRect( sText,NULL, SDL_MapRGB(sText->format,clrBg.r,clrBg.g,clrBg.b) );
// }
//
//
// // Actually render the text:
// SDL_Renderer *sTemp = NULL;
// for( unsigned int i = 0; i < vLines.size(); i++ ) {
//// The rendered text:
//if( flags & PS_BLENDED ) {
// sTemp = TTF_RenderText_Blended( font, vLines[i].c_str(), clrFg );
//} else {
// sTemp = TTF_RenderText_Solid ( font, vLines[i].c_str(), clrFg );
//}
//
//
//// Put it on the surface:
//if( (flags&PS_ALIGN_LEFT) ||
// !(flags&PS_ALIGN_CENTER ||
// flags&PS_ALIGN_RIGHT) ) {
// // If it's specifically LEFT or none of the others:
// SDL_Rect rect = {0,i*lineSkip,0,0};
// SDL_BlitSurface( sTemp,NULL, sText,
// &(SDL_Rect) rect );
//}
//if( flags & PS_ALIGN_CENTER ) {
// int w = 0, h = 0; TTF_SizeText( font, vLines[i].c_str(), &w,&h );
// SDL_Rect rect = {width/2-w/2,i*lineSkip,0,0};
// SDL_BlitSurface( sTemp,NULL, sText,
// &(SDL_Rect) rect );
//}
//if( flags & PS_ALIGN_RIGHT ) {
// printf("ERROR: PrintStrings()::PS_ALIGN_RIGHT:"
// "Oops, this hasn't been implemented yet\n");
//}
//// Clean up:
//SDL_FreeSurface( sTemp );
// }
//
// if( flags & PS_CREATE_SURFACE ) {
//SDL_FreeSurface( dest );
//return( sText );
// }
//
// // Draw the text on top of that:
////Transparent
// //SDL_SetAlpha( sText, SDL_SRCALPHA, SDL_ALPHA_OPAQUE );
////apply_surface( 0, 0, back, screen );
////apply_surface( 0, 0, front, screen );*/
// SDL_BlitSurface( sText,NULL, dest,&rc );
// SDL_FreeSurface( sText );
// return(NULL);
//}