aboutsummaryrefslogtreecommitdiff
path: root/CLIP.C
blob: 93f89af17bc7ca8481489117946892dd60353778 (plain)
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
/*--------------------------------------------------------------*/
/*								*/
/*			  CLIP.C				*/
/*								*/
/*	Provides a clipping interface to the Amiga area fill.	*/
/*	Uses a modified verion of the Sutherland-Hodgman	*/
/*	Reentrant Polygon Clipper.				*/
/*								*/
/*	ClipInit(xmin,ymin,xmax,ymax)   -- sets clip box	*/
/*	ClipDraw  	-- corresponds to AreaDraw		*/
/*	ClipMove  	-- corresponds to AreaMove		*/
/*	ClipEnd   	-- corresponds to AreaEnd		*/
/*								*/
/*	Dan Silva 3-6-85  created				*/
/*	"	  3-10-85 changed to 2-stage from 4-stage	*/
/*			  to speed up.				*/
/*								*/
/*		Copyright 1985, Electronic Arts			*/
/*--------------------------------------------------------------*/

#include "system.h"


/* #define dbgClip */

#define YES  1
#define NO   0
#define NIL  0
#define local static

typedef char Boolean;
typedef struct { int x,y; } Point;

typedef struct {
    short lastcode,isFirst,fcode;
    Point last,f;
    } clrec;
    
local clrec ud = {0,YES,0,{0,0},{0,0}};
local clrec rl = {0,YES,0,{0,0},{0,0}};

local struct RastPort *clrp;

/* ------ clipping box -------*/
local short clxmin = 0;
local short clymin = 0;
local short clxmax = 319;
local short clymax = 199;
local short anyOutput = NO;


local ClipUD(x,y) int x,y; {
    short code = 0;
    short xp,yclip;
#ifdef dbgClip
    printf("ClipUD x= %d, y = %d\n",x,y);
#endif
    if (y<clymin) code|= 1; else if (y>clymax)  code |= 2;
    if (ud.isFirst) {
	ud.f.x = x; ud.f.y = y;
	ud.fcode = 0;
	ud.isFirst = NO;
	}
    else {
	if (!(code|ud.lastcode)) ClipRL(x,y,0);  /* trivial accept */
	else if (!(code&ud.lastcode)) {   /* --cant reject-- */
	    if (ud.lastcode) { /* last point was out of bounds */
		yclip = (ud.lastcode&1)?clymin: clymax;
		xp = ud.last.x + (yclip-ud.last.y)*(x-ud.last.x)/(y-ud.last.y);
		ClipRL(xp,yclip,ud.lastcode);
		}
	    xp = x; yclip = y;
	    if (code) {  /*---- this point is out---- */
		yclip = (code&1)?clymin: clymax;
		xp = ud.last.x + (yclip-ud.last.y)*(x-ud.last.x)/(y-ud.last.y);
		}
	    ClipRL(xp,yclip,code);
	    }
	}
    ud.last.x = x;
    ud.last.y = y;
    ud.lastcode = code;
    }

local short lastrlcode;

local ClipRL(x,y,udcode) 
    int x,y;		/* point clipped by UD */
    short udcode; 	/* records how it was clipped by UD */
    {
    short code = 0;
    short yp,xclip,lcode,ocode;
#ifdef dbgClip
    printf("ClipRL x= %d, y = %d, code = d\n",x,y,udcode);
#endif
    if (x<clxmin) code|= 4; else if (x>clxmax)  code |= 8;
    if (rl.isFirst) {
	rl.f.x = x; rl.f.y = y;
	rl.fcode = udcode;
	rl.isFirst = NO;
	}
    else {
	if (!(code|lastrlcode)) Output(x,y,udcode);  /* trivial accept */
	else if (!(code&lastrlcode)) 
	    {/* ---- cant reject ----- */
	    lcode = rl.lastcode&udcode;
	    if (lastrlcode) { /* last point was out of bounds */
		xclip = (lastrlcode&4)?clxmin: clxmax;
		yp = rl.last.y+(xclip-rl.last.x)*(y-rl.last.y)/(x-rl.last.x);
		Output(xclip,yp,lcode|lastrlcode);
		}
	    yp = y; xclip = x; ocode = udcode;
	    if (code) {  /* ----- this point is out -----*/
		xclip = (code&4)?clxmin: clxmax;
		ocode = lcode|code;
		yp = rl.last.y + (xclip-rl.last.x)*(y-rl.last.y)/(x-rl.last.x);
		}
	    Output(xclip,yp,ocode);
	    }
	}
    rl.last.x = x;
    rl.last.y = y;
    rl.lastcode = udcode;
    lastrlcode = code;
    }


/*---- the final stage: output the points to the area fill */

local Boolean lastOutCode;
local Point first;
local short firstCode;


local Output(x,y,incode) int x,y,incode; {
    short saveState;
#ifdef dbgClip
    printf("Output x= %d, y = %d, code = %d\n",x,y,incode);
#endif
    if (!anyOutput) { 
	AreaMove(clrp,x,y); 
	anyOutput= YES; 
	first.x = x;
	first.y = y;
	firstCode = incode;
	}
    else 
	{
	if ((lastOutCode&incode))
	    { /* "virtual" edges--ie along screen boundary */
	    saveState = clrp->Flags&AREAOUTLINE;
	    clrp->Flags &= ~AREAOUTLINE;
	    AreaDraw(clrp,x,y);
	    clrp->Flags |= saveState;
	    }
	else AreaDraw(clrp,x,y);
	}
    lastOutCode = incode;
    }

local Close() {
#ifdef dbgClip
    printf(" Close\n");
#endif
    if (!ud.isFirst) ClipUD(ud.f.x,ud.f.y);
    if (!rl.isFirst) ClipRL(rl.f.x,rl.f.y,rl.fcode);
    if (anyOutput) Output(first.x,first.y,firstCode);
    ud.isFirst = rl.isFirst = YES;
    anyOutput = NO;
    }

/* ---------- Interface to Outside World -------------*/

ClipInit(xmin,ymin,xmax,ymax) short xmin,ymin,xmax,ymax;
    {
    clxmin = xmin;    clymin = ymin;    
    clxmax = xmax;    clymax = ymax;
    }

local short drawYet = NO;

ClipDraw(rp,x,y) struct RastPort *rp; short x,y; {
#ifdef dbgClip
    printf(" ClipDraw: x = %d, y = %d\n",x,y);
#endif
    clrp = rp;
    ClipUD(x,y);
    drawYet = YES;
    }

ClipMove(rp,x,y) struct RastPort *rp; short x,y; {
#ifdef dbgClip
    printf(" ClipMove: x = %d, y = %d\n",x,y);
#endif
    if (drawYet) Close(); else { ud.isFirst = rl.isFirst = YES; }
    clrp = rp;
    ClipUD(x,y);
    drawYet = NO;
    }

ClipEnd(rp) struct RastPort *rp; {
    BOOL wasAny = anyOutput;
#ifdef dbgClip
    printf(" Clip END -----------------------\n");
#endif
    
    if (drawYet)  Close();
    WaitBlit();
    if (wasAny) AreaEnd(rp);
    drawYet = NO;
    }

static short dum;
static short dum2 = 0;