-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquickcg.cpp
2005 lines (1788 loc) · 71.1 KB
/
quickcg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
QuickCG SDL2 20190709
Copyright (c) 2004-2007, Lode Vandevenne
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
QuickCG is an SDL codebase that wraps some of the SDL functionality.
It's used by Lode's Computer Graphics Tutorial to work with simple C++ calls
to demonstrate graphical programs. This SDL2.0 port was made by Chrissy573.
QuickCG can handle some things that standard C++ doesn't but that are commonly useful, such as:
-drawing graphics
-a bitmap font
-simplified saving and loading of files
-reading keyboard and mouse input
-playing sound
-color models
-loading images
*/
#include <SDL.h>
#include "quickcg.h"
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <iostream>
#include <fstream>
namespace QuickCG
{
////////////////////////////////////////////////////////////////////////////////
//VARIABLES/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int w; //width of the screen
int h; //height of the screen
std::map<int, bool> keypressed; //for the "keyPressed" function to detect a keypress only once
SDL_Window* scr; //the single SDL window used
SDL_Surface* srf;
SDL_Renderer* render;
SDL_Texture* tex;
SDL_PixelFormat *fmt;
const Uint8* inkeys;
SDL_Event event = { 0 };
////////////////////////////////////////////////////////////////////////////////
//KEYBOARD FUNCTIONS////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
bool keyDown(int key) //this checks if the key is held down, returns true all the time until the key is up
{
return (inkeys[key] != 0);
}
bool keyPressed(int key) //this checks if the key is *just* pressed, returns true only once until the key is up again
{
if (keypressed.find(key) == keypressed.end()) keypressed[key] = false;
if (inkeys[key])
{
if (keypressed[key] == false)
{
keypressed[key] = true;
return true;
}
}
else keypressed[key] = false;
return false;
}
////////////////////////////////////////////////////////////////////////////////
//BASIC SCREEN FUNCTIONS////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//The screen function: sets up the window for 32-bit color graphics.
//Creates a graphical screen of width*height pixels in 32-bit color.
//Set fullscreen to 0 for a window, or to 1 for fullscreen output
//text is the caption or title of the window
//also inits SDL
void screen(int width, int height, bool fullscreen, const std::string& text)
{
w = width;
h = height;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
SDL_Quit();
std::exit(1);
}
std::atexit(SDL_Quit);
if (fullscreen)
{
scr = SDL_CreateWindow(text.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL);
lock();
}
else
{
scr = SDL_CreateWindow(text.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
}
if (scr == NULL)
{
printf("Unable to set video: %s\n", SDL_GetError());
SDL_Quit();
std::exit(1);
}
render = SDL_CreateRenderer(scr, -1, 0);
if (render == NULL)
{
printf("Unable to set renderer: %s\n", SDL_GetError());
SDL_Quit();
std::exit(1);
}
srf = SDL_CreateRGBSurface(0, w, h, 32,
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF);
if (srf == NULL)
{
printf("Unable to set surface: %s\n", SDL_GetError());
SDL_Quit();
std::exit(1);
}
tex = SDL_CreateTexture(render, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
if (tex == NULL)
{
printf("Unable to set texture: %s\n", SDL_GetError());
SDL_Quit();
std::exit(1);
}
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
fmt = srf->format;
if (fmt == NULL)
{
printf("Unable to set pixel format: %s\n", SDL_GetError());
SDL_Quit();
std::exit(1);
}
}
//Locks the screen
void lock()
{
if (SDL_MUSTLOCK(srf))
if (SDL_LockSurface(srf) < 0)
return;
}
//Unlocks the screen
void unlock()
{
if (SDL_MUSTLOCK(srf))
SDL_UnlockSurface(srf);
}
//Updates the screen. Has to be called to view new pixels, but use only after
//drawing the whole screen because it's slow.
void redraw()
{
SDL_UpdateTexture(tex, NULL, srf->pixels, srf->pitch);
SDL_RenderClear(render);
SDL_RenderCopy(render, tex, NULL, NULL);
SDL_RenderPresent(render);
}
//Clears the screen to black
void cls(const ColorRGBA& color)
{
SDL_SetRenderDrawColor(render, color.r, color.g, color.b, color.a);
SDL_RenderClear(render);
SDL_RenderPresent(render);
}
//Puts an RGBA color pixel at position x,y
void pset(int x, int y, const ColorRGBA& color)
{
if (x < 0 || y < 0 || x >= w || y >= h) return;
Uint32 colorSDL = SDL_MapRGBA(fmt, color.r, color.g, color.b, color.a);
Uint32* bufp;
bufp = (Uint32*)srf->pixels + y * srf->pitch / 4 + x;
*bufp = colorSDL;
}
//Gets RGB color of pixel at position x,y
ColorRGBA pget(int x, int y)
{
if (x < 0 || y < 0 || x >= w || y >= h) return RGB_Black;
Uint32* bufp;
bufp = (Uint32*)srf->pixels + y * srf->pitch / 4 + x;
Uint32 colorSDL = *bufp;
ColorRGBA8bit colorRGBA;
SDL_GetRGBA(colorSDL, fmt, &colorRGBA.r, &colorRGBA.g, &colorRGBA.b, &colorRGBA.a);
return ColorRGBA(colorRGBA);
}
//Draws a buffer of pixels to the screen
void drawBuffer(Uint32* buffer)
{
Uint32* bufp;
bufp = (Uint32*)srf->pixels;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
*bufp = buffer[y * w + x];
bufp++;
}
bufp += srf->pitch / 4;
bufp -= w;
}
}
void getScreenBuffer(std::vector<Uint32>& buffer)
{
Uint32* bufp;
bufp = (Uint32*)srf->pixels;
buffer.resize(w * h);
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
buffer[y * w + x] = *bufp;
bufp++;
}
bufp += srf->pitch / 4;
bufp -= w;
}
}
bool onScreen(int x, int y)
{
return (x >= 0 && y >= 0 && x < w && y < h);
}
////////////////////////////////////////////////////////////////////////////////
//NON GRAPHICAL FUNCTIONS///////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//Waits until you press a key. First the key has to be loose, this means, if you put two sleep functions in a row, the second will only work after you first released the key.
void sleep()
{
int done = 0;
SDL_PollEvent(&event);
while (done == 0)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT) end();
if (event.type == SDL_KEYDOWN) done = 1;
}
SDL_Delay(5); //so it consumes less processing power
}
}
void sleep(double seconds)
{
SDL_Delay((Uint32)seconds * 1000);
}
void waitFrame(double oldTime, double frameDuration) //in seconds
{
double time = getTime();
while (time - oldTime < frameDuration)
{
time = getTime();
SDL_PollEvent(&event);
if (event.type == SDL_QUIT) end();
inkeys = SDL_GetKeyboardState(NULL);
if (inkeys[SDLK_ESCAPE]) end();
SDL_Delay(5); //so it consumes less processing power
}
}
//Returns 1 if you close the window or press the escape key. Also handles everything that's needed per frame.
//Never put key input code right before done() or SDL may see the key as SDL_QUIT
bool done(bool quit_if_esc, bool delay) //delay makes CPU have some free time, use once per frame to avoid 100% usage of a CPU core
{
if (delay) SDL_Delay(5); //so it consumes less processing power
bool done = false;
if (!SDL_PollEvent(&event)) return 0;
readKeys();
if (quit_if_esc && inkeys[SDLK_ESCAPE]) done = true;
if (event.type == SDL_QUIT) done = true;
return done;
}
//Ends the program
void end()
{
SDL_Quit();
std::exit(1);
}
//Gives value of pressed key to inkeys.
//the variable inkeys can then be used anywhere to check for input
//Normally you have to use readkeys every time you want to use inkeys, but the done() function also uses inkeys so it's not needed to use readkeys if you use done().
void readKeys()
{
SDL_PollEvent(&event);
inkeys = SDL_GetKeyboardState(NULL);
}
void getMouseState(int& mouseX, int& mouseY)
{
SDL_GetMouseState(&mouseX, &mouseY);
}
void getMouseState(int& mouseX, int& mouseY, bool& LMB, bool& RMB)
{
Uint8 mouseState = SDL_GetMouseState(&mouseX, &mouseY);
if (mouseState & 1) LMB = true;
else LMB = false;
if (mouseState & 4) RMB = true;
else RMB = false;
}
//Returns the time in milliseconds since the program started
unsigned long getTicks()
{
return SDL_GetTicks();
}
////////////////////////////////////////////////////////////////////////////////
//2D SHAPES/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//Fast horizontal line from (x1,y) to (x2,y), with rgb color
bool horLine(int y, int x1, int x2, const ColorRGBA& color)
{
if (x2 < x1) { x1 += x2; x2 = x1 - x2; x1 -= x2; } //swap x1 and x2, x1 must be the leftmost endpoint
if (x2 < 0 || x1 >= w || y < 0 || y >= h) return 0; //no single point of the line is on screen
if (x1 < 0) x1 = 0; //clip
if (x2 >= w) x2 = w - 1; //clip
Uint32 colorSDL = SDL_MapRGBA(fmt, color.r, color.g, color.b, color.a);
Uint32* bufp;
bufp = (Uint32*)srf->pixels + y * srf->pitch / 4 + x1;
for (int x = x1; x <= x2; x++)
{
*bufp = colorSDL;
bufp++;
}
return 1;
}
//Fast vertical line from (x,y1) to (x,y2), with rgb color
bool verLine(int x, int y1, int y2, const ColorRGBA& color)
{
if (y2 < y1) { y1 += y2; y2 = y1 - y2; y1 -= y2; } //swap y1 and y2
if (y2 < 0 || y1 >= h || x < 0 || x >= w) return 0; //no single point of the line is on screen
if (y1 < 0) y1 = 0; //clip
if (y2 >= w) y2 = h - 1; //clip
Uint32 colorSDL = SDL_MapRGBA(fmt, color.r, color.g, color.b, color.a);
Uint32* bufp;
bufp = (Uint32*)srf->pixels + y1 * srf->pitch / 4 + x;
for (int y = y1; y <= y2; y++)
{
*bufp = colorSDL;
bufp += srf->pitch / 4;
}
return 1;
}
//Bresenham line from (x1,y1) to (x2,y2) with rgb color
bool drawLine(int x1, int y1, int x2, int y2, const ColorRGBA& color)
{
if (x1 < 0 || x1 > w - 1 || x2 < 0 || x2 > w - 1 || y1 < 0 || y1 > h - 1 || y2 < 0 || y2 > h - 1) return 0;
int deltax = std::abs(x2 - x1); //The difference between the x's
int deltay = std::abs(y2 - y1); //The difference between the y's
int x = x1; //Start x off at the first pixel
int y = y1; //Start y off at the first pixel
int xinc1, xinc2, yinc1, yinc2, den, num, numadd, numpixels, curpixel;
if (x2 >= x1) //The x-values are increasing
{
xinc1 = 1;
xinc2 = 1;
}
else //The x-values are decreasing
{
xinc1 = -1;
xinc2 = -1;
}
if (y2 >= y1) //The y-values are increasing
{
yinc1 = 1;
yinc2 = 1;
}
else //The y-values are decreasing
{
yinc1 = -1;
yinc2 = -1;
}
if (deltax >= deltay) //There is at least one x-value for every y-value
{
xinc1 = 0; //Don't change the x when numerator >= denominator
yinc2 = 0; //Don't change the y for every iteration
den = deltax;
num = deltax / 2;
numadd = deltay;
numpixels = deltax; //There are more x-values than y-values
}
else //There is at least one y-value for every x-value
{
xinc2 = 0; //Don't change the x for every iteration
yinc1 = 0; //Don't change the y when numerator >= denominator
den = deltay;
num = deltay / 2;
numadd = deltax;
numpixels = deltay; //There are more y-values than x-values
}
for (curpixel = 0; curpixel <= numpixels; curpixel++)
{
pset(x % w, y % h, color); //Draw the current pixel
num += numadd; //Increase the numerator by the top of the fraction
if (num >= den) //Check if numerator >= denominator
{
num -= den; //Calculate the new numerator value
x += xinc1; //Change the x as appropriate
y += yinc1; //Change the y as appropriate
}
x += xinc2; //Change the x as appropriate
y += yinc2; //Change the y as appropriate
}
return 1;
}
//Bresenham circle with center at (xc,yc) with radius and red green blue color
bool drawCircle(int xc, int yc, int radius, const ColorRGBA& color)
{
if (xc - radius < 0 || xc + radius >= w || yc - radius < 0 || yc + radius >= h) return 0;
int x = 0;
int y = radius;
int p = 3 - (radius << 1);
int a, b, c, d, e, f, g, h;
while (x <= y)
{
a = xc + x; //8 pixels can be calculated at once thanks to the symmetry
b = yc + y;
c = xc - x;
d = yc - y;
e = xc + y;
f = yc + x;
g = xc - y;
h = yc - x;
pset(a, b, color);
pset(c, d, color);
pset(e, f, color);
pset(g, f, color);
if (x > 0) //avoid drawing pixels at same position as the other ones
{
pset(a, d, color);
pset(c, b, color);
pset(e, h, color);
pset(g, h, color);
}
if (p < 0) p += (x++ << 2) + 6;
else p += ((x++ - y--) << 2) + 10;
}
return 1;
}
//Filled bresenham circle with center at (xc,yc) with radius and red green blue color
bool drawDisk(int xc, int yc, int radius, const ColorRGBA& color)
{
if (xc + radius < 0 || xc - radius >= w || yc + radius < 0 || yc - radius >= h) return 0; //every single pixel outside screen, so don't waste time on it
int x = 0;
int y = radius;
int p = 3 - (radius << 1);
int a, b, c, d, e, f, g, h;
int pb = yc + radius + 1, pd = yc + radius + 1; //previous values: to avoid drawing horizontal lines multiple times (ensure initial value is outside the range)
while (x <= y)
{
// write data
a = xc + x;
b = yc + y;
c = xc - x;
d = yc - y;
e = xc + y;
f = yc + x;
g = xc - y;
h = yc - x;
if (b != pb) horLine(b, a, c, color);
if (d != pd) horLine(d, a, c, color);
if (f != b) horLine(f, e, g, color);
if (h != d && h != f) horLine(h, e, g, color);
pb = b;
pd = d;
if (p < 0) p += (x++ << 2) + 6;
else p += ((x++ - y--) << 2) + 10;
}
return 1;
}
//Rectangle with corners (x1,y1) and (x2,y2) and rgb color
bool drawRect(int x1, int y1, int x2, int y2, const ColorRGBA& color)
{
if (x1 < 0 || x1 > w - 1 || x2 < 0 || x2 > w - 1 || y1 < 0 || y1 > h - 1 || y2 < 0 || y2 > h - 1) return 0;
SDL_Rect rec;
rec.x = x1;
rec.y = y1;
rec.w = x2 - x1 + 1;
rec.h = y2 - y1 + 1;
Uint32 colorSDL = SDL_MapRGBA(fmt, color.r, color.g, color.b, color.a);
SDL_FillRect(srf, &rec, colorSDL); //SDL's ability to draw a hardware rectangle is used for now
return 1;
}
//Functions for clipping a 2D line to the screen, which is the rectangle (0,0)-(w,h)
//This is the Cohen-Sutherland Clipping Algorithm
//Each of 9 regions gets an outcode, based on if it's at the top, bottom, left or right of the screen
// 1001 1000 1010 9 8 10
// 0001 0000 0010 1 0 2
// 0101 0100 0110 5 4 6
//int findregion returns which of the 9 regions a point is in, void clipline does the actual clipping
int findRegion(int x, int y)
{
int code = 0;
if (y >= h)
code |= 1; //top
else if (y < 0)
code |= 2; //bottom
if (x >= w)
code |= 4; //right
else if (x < 0)
code |= 8; //left
return(code);
}
bool clipLine(int x1, int y1, int x2, int y2, int & x3, int & y3, int & x4, int & y4)
{
int code1, code2, codeout;
bool accept = 0, done = 0;
code1 = findRegion(x1, y1); //the region outcodes for the endpoints
code2 = findRegion(x2, y2);
do //In theory, this can never end up in an infinite loop, it'll always come in one of the trivial cases eventually
{
if (!(code1 | code2)) accept = done = 1; //accept because both endpoints are in screen or on the border, trivial accept
else if (code1 & code2) done = 1; //the line isn't visible on screen, trivial reject
else //if no trivial reject or accept, continue the loop
{
int x, y;
codeout = code1 ? code1 : code2;
if (codeout & 1) //top
{
x = x1 + (x2 - x1) * (h - y1) / (y2 - y1);
y = h - 1;
}
else if (codeout & 2) //bottom
{
x = x1 + (x2 - x1) * -y1 / (y2 - y1);
y = 0;
}
else if (codeout & 4) //right
{
y = y1 + (y2 - y1) * (w - x1) / (x2 - x1);
x = w - 1;
}
else //left
{
y = y1 + (y2 - y1) * -x1 / (x2 - x1);
x = 0;
}
if (codeout == code1) //first endpoint was clipped
{
x1 = x; y1 = y;
code1 = findRegion(x1, y1);
}
else //second endpoint was clipped
{
x2 = x; y2 = y;
code2 = findRegion(x2, y2);
}
}
} while (done == 0);
if (accept)
{
x3 = x1;
x4 = x2;
y3 = y1;
y4 = y2;
return 1;
}
else
{
x3 = x4 = y3 = y4 = 0;
return 0;
}
}
////////////////////////////////////////////////////////////////////////////////
//COLOR STRUCTS/////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
ColorRGBA::ColorRGBA(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
this->r = r;
this->g = g;
this->b = b;
this->a = a;
}
ColorRGBA::ColorRGBA(const ColorRGBA8bit& color)
{
this->r = color.r;
this->g = color.g;
this->b = color.b;
this->a = color.a;
}
ColorRGBA::ColorRGBA()
{
this->r = 0;
this->g = 0;
this->b = 0;
this->a = 0;
}
ColorRGBA8bit::ColorRGBA8bit(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
this->r = r;
this->g = g;
this->b = b;
this->a = a;
}
ColorRGBA8bit::ColorRGBA8bit(const ColorRGBA& color)
{
this->r = color.r;
this->g = color.g;
this->b = color.b;
this->a = color.a;
}
ColorRGBA8bit::ColorRGBA8bit()
{
this->r = 0;
this->g = 0;
this->b = 0;
this->a = 0;
}
//Add two colors
ColorRGBA operator+(const ColorRGBA& color, const ColorRGBA& color2)
{
ColorRGBA c;
c.r = color.r + color2.r;
c.g = color.g + color2.g;
c.b = color.b + color2.b;
c.a = color.a + color2.a;
return c;
}
//Subtract two colors
ColorRGBA operator-(const ColorRGBA& color, const ColorRGBA& color2)
{
ColorRGBA c;
c.r = color.r - color2.r;
c.g = color.g - color2.g;
c.b = color.b - color2.b;
c.a = color.a - color2.a;
return c;
}
//Multiplies a color with an integer
ColorRGBA operator*(const ColorRGBA& color, int a)
{
ColorRGBA c;
c.r = color.r * a;
c.g = color.g * a;
c.b = color.b * a;
c.a = color.a * a;
return c;
}
//Multiplies a color with an integer
ColorRGBA operator*(int a, const ColorRGBA& color)
{
ColorRGBA c;
c.r = color.r * a;
c.g = color.g * a;
c.b = color.b * a;
c.a = color.a * a;
return c;
}
//Divides a color through an integer
ColorRGBA operator/(const ColorRGBA& color, int a)
{
if (a == 0) return color;
ColorRGBA c;
c.r = color.r / a;
c.g = color.g / a;
c.b = color.b / a;
c.a = color.a / a;
return c;
}
ColorRGBA overlay(const ColorRGBA& color, const ColorRGBA& color2)
{
ColorRGBA c;
c.r = (color.r * color.a / 255) + (color2.r * color2.a * (255 - color.a) / (255 * 255));
c.g = (color.g * color.a / 255) + (color2.g * color2.a * (255 - color.a) / (255 * 255));
c.b = (color.b * color.a / 255) + (color2.b * color2.a * (255 - color.a) / (255 * 255));
c.a = color.a + (color2.a * (255 - color.a) / 255);
return c;
}
//Are both colors equal?
bool operator==(const ColorRGBA& color, const ColorRGBA& color2)
{
return(color.r == color2.r && color.g == color2.g && color.b == color2.b && color.a == color2.a);
}
//Are both colors not equal?
bool operator!=(const ColorRGBA& color, const ColorRGBA& color2)
{
return(!(color.r == color2.r && color.g == color2.g && color.b == color2.b && color.a == color2.a));
}
ColorHSL::ColorHSL(Uint8 h, Uint8 s, Uint8 l, Uint8 a)
{
this->h = h;
this->s = s;
this->l = l;
this->a = a;
}
ColorHSL::ColorHSL()
{
this->h = 0;
this->s = 0;
this->l = 0;
this->a = 0;
}
ColorHSV::ColorHSV(Uint8 h, Uint8 s, Uint8 v, Uint8 a)
{
this->h = h;
this->s = s;
this->v = v;
this->a = a;
}
ColorHSV::ColorHSV()
{
this->h = 0;
this->s = 0;
this->v = 0;
this->a = 0;
}
////////////////////////////////////////////////////////////////////////////////
//COLOR CONVERSIONS/////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
Convert colors from one type to another
r=red g=green b=blue h=hue s=saturation l=lightness v=value
Color components from the color structs are Uint8's between 0 and 255
color components used in the calculations are normalized between 0.0-1.0
*/
//Converts an RGB color to HSL color
ColorHSL RGBtoHSL(const ColorRGBA& ColorRGBA)
{
double r, g, b, a, h = 0, s = 0, l; //this function works with floats between 0 and 1
r = ColorRGBA.r / 256.0;
g = ColorRGBA.g / 256.0;
b = ColorRGBA.b / 256.0;
a = ColorRGBA.a / 256.0;
const double maxColor = std::max(r, std::max(g, b));
const double minColor = std::min(r, std::min(g, b));
if (minColor == maxColor) //R = G = B, so it's a shade of grey
{
h = 0; //it doesn't matter what value it has
s = 0;
l = r; //doesn't matter if you pick r, g, or b
}
else
{
l = (minColor + maxColor) / 2;
if (l < 0.5) s = (maxColor - minColor) / (maxColor + minColor);
if (l >= 0.5) s = (maxColor - minColor) / (2.0 - maxColor - minColor);
if (r == maxColor) h = (g - b) / (maxColor - minColor);
if (g == maxColor) h = 2.0 + (b - r) / (maxColor - minColor);
if (b == maxColor) h = 4.0 + (r - g) / (maxColor - minColor);
h /= 6; //to bring it to a number between 0 and 1
if (h < 0) h += 1;
}
ColorHSL colorHSL;
colorHSL.h = int(h * 255.0);
colorHSL.s = int(s * 255.0);
colorHSL.l = int(l * 255.0);
colorHSL.a = int(a * 255.0);
return colorHSL;
}
//Converts an HSL color to RGB color
ColorRGBA HSLtoRGB(const ColorHSL& colorHSL)
{
double r, g, b, a, h, s, l; //this function works with floats between 0 and 1
double temp1, temp2, tempr, tempg, tempb;
h = colorHSL.h / 256.0;
s = colorHSL.s / 256.0;
l = colorHSL.l / 256.0;
a = colorHSL.a / 256.0;
//If saturation is 0, the color is a shade of grey
if (s == 0) r = g = b = l;
//If saturation > 0, more complex calculations are needed
else
{
//set the temporary values
if (l < 0.5) temp2 = l * (1 + s);
else temp2 = (l + s) - (l * s);
temp1 = 2 * l - temp2;
tempr = h + 1.0 / 3.0;
if (tempr > 1.0) tempr--;
tempg = h;
tempb = h - 1.0 / 3.0;
if (tempb < 0.0) tempb++;
//red
if (tempr < 1.0 / 6.0) r = temp1 + (temp2 - temp1) * 6.0 * tempr;
else if (tempr < 0.5) r = temp2;
else if (tempr < 2.0 / 3.0) r = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempr) * 6.0;
else r = temp1;
//green
if (tempg < 1.0 / 6.0) g = temp1 + (temp2 - temp1) * 6.0 * tempg;
else if (tempg < 0.5) g = temp2;
else if (tempg < 2.0 / 3.0) g = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempg) * 6.0;
else g = temp1;
//blue
if (tempb < 1.0 / 6.0) b = temp1 + (temp2 - temp1) * 6.0 * tempb;
else if (tempb < 0.5) b = temp2;
else if (tempb < 2.0 / 3.0) b = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempb) * 6.0;
else b = temp1;
}
ColorRGBA ColorRGBA;
ColorRGBA.r = int(r * 255.0);
ColorRGBA.g = int(g * 255.0);
ColorRGBA.b = int(b * 255.0);
ColorRGBA.a = int(a * 255.0);
return ColorRGBA;
}
//Converts an RGB color to HSV color
ColorHSV RGBtoHSV(const ColorRGBA& ColorRGBA)
{
double r, g, b, a, h = 0.0, s = 0.0, v; //this function works with floats between 0 and 1
r = ColorRGBA.r / 256.0;
g = ColorRGBA.g / 256.0;
b = ColorRGBA.b / 256.0;
a = ColorRGBA.a / 256.0;
const double maxColor = std::max(r, std::max(g, b));
const double minColor = std::min(r, std::min(g, b));
v = maxColor;
if (maxColor != 0.0) //avoid division by zero when the color is black
{
s = (maxColor - minColor) / maxColor;
}
if (s == 0.0)
{
h = 0.0; //it doesn't matter what value it has
}
else
{
if (r == maxColor) h = (g - b) / (maxColor - minColor);
if (g == maxColor) h = 2.0 + (b - r) / (maxColor - minColor);
if (b == maxColor) h = 4.0 + (r - g) / (maxColor - minColor);
h /= 6.0; //to bring it to a number between 0 and 1
if (h < 0.0) h++;
}
ColorHSV colorHSV;
colorHSV.h = int(h * 255.0);
colorHSV.s = int(s * 255.0);
colorHSV.v = int(v * 255.0);
colorHSV.a = int(a * 255.0);
return colorHSV;
}
//Converts an HSV color to RGB color
ColorRGBA HSVtoRGB(const ColorHSV& colorHSV)
{
double r, g, b, a, h, s, v; //this function works with floats between 0 and 1
h = colorHSV.h / 256.0;
s = colorHSV.s / 256.0;
v = colorHSV.v / 256.0;
a = colorHSV.a / 256.0;
//if saturation is 0, the color is a shade of grey
if (s == 0.0) r = g = b = v;
//if saturation > 0, more complex calculations are needed
else
{
double f, p, q, t;
int i;
h *= 6.0; //to bring hue to a number between 0 and 6, better for the calculations
i = int(floor(h)); //e.g. 2.7 becomes 2 and 3.01 becomes 3 or 4.9999 becomes 4
f = h - i;//the fractional part of h
p = v * (1.0 - s);
q = v * (1.0 - (s * f));
t = v * (1.0 - (s * (1.0 - f)));
switch (i)
{
case 0: r = v; g = t; b = p; break;
case 1: r = q; g = v; b = p; break;
case 2: r = p; g = v; b = t; break;
case 3: r = p; g = q; b = v; break;
case 4: r = t; g = p; b = v; break;
case 5: r = v; g = p; b = q; break;
default: r = g = b = 0; break;
}
}
ColorRGBA ColorRGBA;
ColorRGBA.r = int(r * 255.0);
ColorRGBA.g = int(g * 255.0);
ColorRGBA.b = int(b * 255.0);
ColorRGBA.a = int(a * 255.0);
return ColorRGBA;
}
Uint32 RGBtoINT(const ColorRGBA& ColorRGBA)
{
return (ColorRGBA.a | (ColorRGBA.b << 8) | (ColorRGBA.g << 16) | (ColorRGBA.r << 24));
}
ColorRGBA INTtoRGB(Uint32 colorINT)
{
ColorRGBA ColorRGBA;
ColorRGBA.r = (colorINT & 0xFF000000) >> 24;
ColorRGBA.g = (colorINT & 0x00FF0000) >> 16;
ColorRGBA.b = (colorINT & 0x0000FF00) >> 8;
ColorRGBA.a = (colorINT & 0x000000FF);
return ColorRGBA;
}