aboutsummaryrefslogtreecommitdiff
path: root/PGRAPH.C
blob: c839ba19a45b225dbccc014ef1dcf80eaab58619 (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
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
/*----------------------------------------------------------------------*/
/*									*/
/*     	  pgraph.c  -- prism graphics primitives			*/
/*									*/
/*	routines starting with "P" take Physical Device Coords		*/
/*	routines starting with "V" take Virtual Device Coords		*/
/*									*/
/*----------------------------------------------------------------------*/

#include <system.h>
#include <prism.h>

#define local static

extern SHORT xShft,yShft;

/* ------  The Current Destination RastPort and Clip Box ---- */
#define NGRSTACK 4

struct RastPort *rpStack[NGRSTACK];
Box clbStack[NGRSTACK];
struct RastPort *curRP = NULL;


Box bigBox = {-1000,-1000, 2000, 2000};

Box clipBox = {0};  /* THIS IS IN SCREEN COORDS */
BoxBM canvas = { {0,0,400,640},NULL};
SHORT grStPtr = 0;
SHORT clipMaxX=0,clipMaxY=0;
    
SetClipBox(r) Box *r; {
    clipBox = *r;
    clipMaxX = clipBox.x + clipBox.w - 1;
    clipMaxY = clipBox.y + clipBox.h - 1;
    }

PushGrDest(rp,clBox) struct RastPort *rp; Box *clBox; {
    rpStack[grStPtr] = curRP;
    clbStack[grStPtr] = clipBox;
    curRP = rp;
    canvas.bm = rp->BitMap;
    SetClipBox(clBox);
    grStPtr++;
    }
    
PopGrDest() {
    grStPtr--;
    curRP = rpStack[grStPtr];
    canvas.bm = curRP->BitMap;
    SetClipBox(&clbStack[grStPtr]);
    }

/* ------------	 MultiColor Fill Pattern -------------	*/
UWORD *curFilPat = NULL;
SetFPat(pattern) UWORD *pattern; { curFilPat = pattern; }
        
/* ------------	 Current Pattern ------------------*/
#define PATSIZE 3
UWORD solidpat[1]={0xffff};
UWORD checkpat[8]={0xaaaa,0x5555,0xaaaa,0x5555,0xaaaa,0x5555,0xaaaa,0x5555};
SetPattern(pattern) USHORT *pattern; {
    SetAfPt(curRP, pattern, (pattern==solidpat)? 0: PATSIZE);  
    }

SolidPattern() { SetPattern(solidpat); }

/* ------------	 Current Pattern, FGCol, BGCol ------	*/
SetFGCol(c) int c; { SetAPen(curRP,c); }
SetBGCol(c) int c; { SetBPen(curRP,c); }

/* general subroutine to replace one pixel value with another in table */
/* replpix(xtab,oldpx,newpx) UBYTE *xtab; UBYTE oldpx,newpx; {    } */

InitPGraph() { 
    curRP = NULL;
    grStPtr = 0;
    }
    	
/* --------- the Paint Mode: how are bits combined in painting ----*/
PaintMode paintMode=Color, svPntMd=Color;
BYTE svmode,svfgpen;
UBYTE curMinterm = COOKIEOP;
SHORT curxpc = 0; /* the current transparent color */
USHORT *svpat;

UBYTE drModes[NPaintModes] = {
	JAM2,		/* Mask */
	JAM2,		/* Color */
	JAM2,		/* Replace */
	JAM2,		/* Smear */
	JAM2,		/* Shade */
	JAM2,		/* Blend */
	JAM2,		/* Cycle Paint*/
	COMPLEMENT	/* Xor */
	};	

void PWritePix(), PShadePix(), PSmearPix(), PBlendPix();

void (*CurPWritePix)();
void (*wrPixTable[])() = { PWritePix,PWritePix,PWritePix,
	PSmearPix , PShadePix,  PBlendPix, PWritePix, PWritePix};

void SetPaintMode(mode) PaintMode mode; {
    paintMode = mode;
    SetDrMd(curRP,drModes[mode]);
    CurPWritePix = wrPixTable[mode];
    curMinterm = (mode==Xor)? XORMASK: COOKIEOP;
    }

/**--------- Temporary Erase mode  for right button -----*/

BOOL erasing = NO;

JamSvPntMd(md) SHORT md;{ svPntMd = md; }

TempErase() { 
    SetDrMd(curRP,JAM2);
    svfgpen = curRP->FgPen;
    SetAPen(curRP,curxpc);
    erasing = YES;
    }
    
ClearErase() { 
    if (erasing) {
	SetDrMd(curRP,drModes[paintMode]);
	SetAPen(curRP,svfgpen);
	erasing = NO;  
	}
    }

/* The transparent color */
SetXPCol(col) SHORT col; {   curxpc = col;    }
PaintMode tsvPntMd;
SHORT tsvfgpen;
#define WHITE 31

TempMode(md,fg,ptrn)   PaintMode md; SHORT fg; USHORT *ptrn; {
    tsvfgpen = curRP->FgPen;
    svpat = curRP->AreaPtrn;
    tsvPntMd = paintMode;
    SetAPen(curRP,fg);
    SetPaintMode(md);
    SetPattern(ptrn);
    }
	
TempXOR() { TempMode(Xor,WHITE,solidpat);  }

RestoreMode() {
    SetAPen(curRP,tsvfgpen);
    SetPaintMode(tsvPntMd);
    SetPattern(svpat);
    }
    
/*------------------ Graphical Operations ----------------------*/
/* operations beginning with "V" take Virtual coordinates     	*/
/* operations beginning with "P" take Physical coordinates    	*/
/*--------------------------------------------------------------*/

#ifdef THEIRWAY
#define FixX(x)   ( (x) - clipBox.x )
#define FixY(y)   ( (y) - clipBox.y )
#else
#define FixX(x)   (x) 
#define FixY(y)   (y) 
#endif

/*-----	Physical Coordinate write pixel 			*/
void PWritePix(x,y) int x,y; {
#ifndef THEIRWAY
    if (BoxContains(&clipBox,x,y))
#endif
	WritePixel(curRP,FixX(x),FixY(y));
    }

/*----- Physical Coordinate read pixel */   
int PReadPix(x,y) int x,y; { return(ReadPixel(curRP,FixX(x),FixY(y)));  }

TempColWrPix(x,y,col) SHORT x,y,col; {
    SHORT sv = curRP->FgPen;
    SetAPen(curRP,col);
    PWritePix(x,y);
    SetAPen(curRP,sv);
    }
	    
extern Range *shadeRange, cycles;
	        
void PShadePix(x,y) {
    SHORT newc = PReadPix(x,y);
    if (erasing) {
	if (shadeRange)   if ((newc<=cycles.low)||(newc>cycles.high)) return;
	newc--;
	}
    else {
	if (shadeRange)  if ((newc<cycles.low)||(newc>=cycles.high)) return;
	newc++;
	}
    TempColWrPix(x,y,newc);
    }

extern BOOL firstDown;
SHORT smlast;
void PSmearPix(x,y) {
    SHORT sv = PReadPix(x,y); 
    if (!firstDown) TempColWrPix(x,y,smlast);
    else firstDown = NO;
    smlast = sv;
    }

local BOOL lastIn;
void PBlendPix(x,y) { 
    SHORT sv = PReadPix(x,y);
    BOOL thisIn = (shadeRange==0)||(sv>=cycles.low)&&(sv<=cycles.high);
    if (!firstDown) {
	if (thisIn&&lastIn)
	TempColWrPix(x,y,(sv+smlast)/2); 
	}
    else firstDown = NO;
    smlast = sv;
    lastIn = thisIn;
    }

/* ------------ Horizontal line ---------------- */

void PHorizLine(x1,y,w) SHORT x1,y,w; {
#ifdef THEIRWAY
    x1 -= clipBox.x;    y -= clipBox.y;
#endif
    Move(curRP,x1,y); Draw(curRP,x1+w-1,y);
    } 

void PatHLine(x1,y,w) int x1,y,w; {
    int x2;
#ifdef THEIRWAY
    x1 -= clipBox.x;    y -= clipBox.y;
#endif
    if ((y<clipBox.y)||(y>clipMaxY)) return;
    x2 = x1 + w -1;
    x1=MAX(x1,clipBox.x); x2 = MIN(x2,clipMaxX);
    if (x1>x2) return;
    HLineBlt(curRP,x1,y,x2,curMinterm);
    } 

/* ------------ Vertical line ---------------- */
void PVertLine(x1,y,h) SHORT x1,y,h; {
#ifdef THEIRWAY
    x1 -= clipBox.x;    y -= clipBox.y;
#endif
    if ((x1>= clipBox.x)&&(x1<=clipMaxX)) {
      Move(curRP,x1,y); Draw(curRP,x1,y+h-1);
	}
    } 

#ifdef THEIRWAY
PFillBox(b) Box *b;{
    RectFill(curRP, b->x- clipBox.x, b->y - clipBox.y, b->x + b->w - 1, b->y + b->h - 1);  
    }
#else
PFillBox(b) Box *b; {
    Box c;
    if (BoxAnd(&c,b,&clipBox)) 
	RectFill(curRP, c.x, c.y,c.x+c.w-1,c.y+c.h-1);
    }
#endif

PPatternBox(b,pat,fg,bg,op) Box *b; SHORT *pat,fg,bg,op; {
    USHORT *svpat,svfg,svbg,svop;
    svpat = curRP->AreaPtrn;
    svfg = curRP->FgPen;
    svbg = curRP->BgPen;
    svop = curRP->AOlPen;
    SetPattern(pat);
    SetFGCol(fg);
    SetBGCol(bg);
    SetOPen(curRP,op);
    PFillBox(b);
    SetPattern(svpat);
    SetFGCol(svfg);
    SetBGCol(svbg);
    SetOPen(curRP,svop);
    BNDRYOFF(curRP);
    }
  
PThinFrame(bx) Box *bx;{
    PatHLine(bx->x,bx->y,bx->w);
    PatHLine(bx->x,bx->y+bx->h-1,bx->w);
    PVertLine(bx->x,bx->y+1, bx->h-2);
    PVertLine(bx->x + bx->w -1,bx->y+1, bx->h-2);
    }

PInvertBox(b) Box *b; {
    TempXOR();
    PFillBox(b);
    RestoreMode();
    }

PInvertXHair(x,y) int x,y; {
    TempXOR();
    PHorizLine(clipBox.x,y,clipBox.w);
    PVertLine(x,clipBox.y,clipBox.h);
    RestoreMode();
    }

PFloodArea(x,y) {
    FillArea(x,y); 
    
/*  Flood(curRP, 1, FixX(x), FixY(y));  */
    }

/* set to flood area now */
PFloodBndry(x,y) { Flood(curRP, 1, FixX(x), FixY(y)); }