amd64 fixes
[btb/d2x.git] / 2d / clip.h
1 /* $Id: clip.h,v 1.4 2004-08-28 23:17:45 schaffner Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14 /*
15  *
16  * Macros used for clipping
17  *
18  */
19
20
21 /*#define MIN(a,b)    (((a) < (b)) ? (a) : (b))
22 #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
23 */
24 #define IABS(x)         (((x) < 0) ?  -(x) : (x))
25
26 #define EXCHG(a,b) do {                                                 \
27         int __temp__ = (a);                                                 \
28         (a) = (b);                                                          \
29         (b) = __temp__;                                                     \
30 } while(0)
31
32 // sort two values
33 #define SORT2(a,b) do {                                                 \
34         if((a) > (b)) EXCHG(a,b);                                           \
35 } while(0)
36
37 # define SCALE(var,arg,num,den)                                         \
38         ((var) = ((arg) * (num)) / (den))
39
40 # define USCALE(var,arg,num,den)                                        \
41         ((var) = ((unsigned)(arg) * (unsigned)(num)) / (unsigned)(den))
42
43 # define FSCALE(var,arg,num,den) ((var) = fixmuldiv((arg),(num),(den)))
44
45 #define CLIPDOT(xx,yy,WHEN_OUTSIDE,WHEN_CLIPPED) {                      \
46         if((xx > XMAX) || (xx < XMIN)) { WHEN_OUTSIDE; }                    \
47         if((yy > YMAX) || (yy < YMIN)) { WHEN_OUTSIDE; }                    \
48 }
49
50 #define CLIPHLINE(x1,x2,yy,WHEN_OUTSIDE,WHEN_CLIPPED) {                 \
51         if(x1 > x2) EXCHG(x1,x2);                                           \
52         if((x1 > XMAX) || (x2 < XMIN)) { WHEN_OUTSIDE; }                    \
53         if((yy > YMAX) || (yy < YMIN)) { WHEN_OUTSIDE; }                    \
54         if(x1 < XMIN) { x1 = XMIN; WHEN_CLIPPED; }                          \
55         if(x2 > XMAX) { x2 = XMAX; WHEN_CLIPPED; }                          \
56 }
57
58 #define CLIPVLINE(xx,y1,y2,WHEN_OUTSIDE,WHEN_CLIPPED) {                 \
59         if(y1 > y2) EXCHG(y1,y2);                                           \
60         if((xx > XMAX) || (xx < XMIN)) { WHEN_OUTSIDE; }                    \
61         if((y1 > YMAX) || (y2 < YMIN)) { WHEN_OUTSIDE; }                    \
62         if(y1 < YMIN) { y1 = YMIN; WHEN_CLIPPED; }                          \
63         if(y2 > YMAX) { y2 = YMAX; WHEN_CLIPPED; }                          \
64 }
65
66 #define CLIPBOX(x1,y1,x2,y2,WHEN_OUTSIDE,WHEN_CLIPPED) {                \
67         if(x1 > x2) EXCHG(x1,x2);                                           \
68         if(y1 > y2) EXCHG(y1,y2);                                           \
69         if((x1 > XMAX) || (x2 < 0)) { WHEN_OUTSIDE; }                       \
70         if((y1 > YMAX) || (y2 < 0)) { WHEN_OUTSIDE; }                       \
71         if(x1 < 0) { x1 = 0; WHEN_CLIPPED; }                                \
72         if(y1 < 0) { y1 = 0; WHEN_CLIPPED; }                                \
73         if(x2 > XMAX) { x2 = XMAX; WHEN_CLIPPED; }                          \
74         if(y2 > YMAX) { y2 = YMAX; WHEN_CLIPPED; }                          \
75 }
76
77 #define CLIPLINE(x1,y1,x2,y2,XMIN,YMIN,XMAX,YMAX,WHEN_OUTSIDE,WHEN_CLIPPED,MY_SCALE) do {       \
78         register int temp;                                                  \
79         if(y1 > y2)                                                         \
80                 { EXCHG(y1,y2); EXCHG(x1,x2); }                                 \
81         if((y2 < YMIN) || (y1 > YMAX))                                      \
82                 { WHEN_OUTSIDE; }                                               \
83         if(x1 < x2) {                                                       \
84                 if((x2 < XMIN) || (x1 > XMAX)) {                                \
85                         WHEN_OUTSIDE;                                               \
86                 }                                                               \
87                 if(x1 < XMIN) {                                                 \
88                         MY_SCALE(temp,(y2 - y1),(XMIN - x1),(x2 - x1)); \
89                         if((y1 += temp) > YMAX) { WHEN_OUTSIDE; }                   \
90                         x1 = XMIN;                                                  \
91                         WHEN_CLIPPED;                                               \
92                 }                                                               \
93                 if(x2 > XMAX) {                                                 \
94                         MY_SCALE(temp,(y2 - y1),(x2 - XMAX),(x2 - x1)); \
95                         if((y2 -= temp) < YMIN) { WHEN_OUTSIDE; }                   \
96                         x2 = XMAX;                                                  \
97                         WHEN_CLIPPED;                                               \
98                 }                                                               \
99                 if(y1 < YMIN) {                                                 \
100                         MY_SCALE(temp,(x2 - x1),(YMIN - y1),(y2 - y1)); \
101                         x1 += temp;                                                 \
102                         y1 = YMIN;                                                  \
103                         WHEN_CLIPPED;                                               \
104                 }                                                               \
105                 if(y2 > YMAX) {                                                 \
106                         MY_SCALE(temp,(x2 - x1),(y2 - YMAX),(y2 - y1)); \
107                         x2 -= temp;                                                 \
108                         y2 = YMAX;                                                  \
109                         WHEN_CLIPPED;                                               \
110                 }                                                               \
111         }                                                                   \
112         else {                                                              \
113                 if((x1 < XMIN) || (x2 > XMAX)) {                                \
114                         WHEN_OUTSIDE;                                               \
115                 }                                                               \
116                 if(x1 > XMAX) {                                                 \
117                         MY_SCALE(temp,(y2 - y1),(x1 - XMAX),(x1 - x2)); \
118                         if((y1 += temp) > YMAX) { WHEN_OUTSIDE; }                   \
119                         x1 = XMAX;                                                  \
120                         WHEN_CLIPPED;                                               \
121                 }                                                               \
122                 if(x2 < XMIN) {                                                 \
123                         MY_SCALE(temp,(y2 - y1),(XMIN - x2),(x1 - x2)); \
124                         if((y2 -= temp) < YMIN) { WHEN_OUTSIDE; }                   \
125                         x2 = XMIN;                                                  \
126                         WHEN_CLIPPED;                                               \
127                 }                                                               \
128                 if(y1 < YMIN) {                                                 \
129                         MY_SCALE(temp,(x1 - x2),(YMIN - y1),(y2 - y1)); \
130                         x1 -= temp;                                                 \
131                         y1 = YMIN;                                                  \
132                         WHEN_CLIPPED;                                               \
133                 }                                                               \
134                 if(y2 > YMAX) {                                                 \
135                         MY_SCALE(temp,(x1 - x2),(y2 - YMAX),(y2 - y1)); \
136                         x2 += temp;                                                 \
137                         y2 = YMAX;                                                  \
138                         WHEN_CLIPPED;                                               \
139                 }                                                               \
140         }                                                                   \
141 } while(0)