xref: /plan9-contrib/sys/src/cmd/plot/libplot/machdep.c (revision 3e12c5d1bb89fc02707907988834ef147769ddaf)
1 #include "mplot.h"
2 Bitmap *offscreen=&screen;
3 /*
4  * Clear the window from x0, y0 to x1, y1 (inclusive) to color c
5  */
6 void m_clrwin(int x0, int y0, int x1, int y1, int c){
7 	int y, hgt;
8 	x1++;
9 	y1++;
10 	if(c<=0)
11 		bitblt(offscreen, Pt(x0, y0), offscreen, Rect(x0, y0, x1, y1), Zero);
12 	else if(c>=(2<<screen.ldepth)-1)
13 		bitblt(offscreen, Pt(x0, y0), offscreen, Rect(x0, y0, x1, y1), F);
14 	else{
15 		segment(offscreen, Pt(x0, y0), Pt(x1, y0), c, S);
16 		for(y=y0+1,hgt=1;y<y1;y+=hgt,hgt*=2){
17 			if(y+hgt>y1) hgt=y1-y;
18 			bitblt(offscreen, Pt(x0, y), offscreen, Rect(x0, y0, x1, y0+hgt), S);
19 		}
20 	}
21 }
22 /*
23  * Draw text between pointers p and q with first character centered at x, y.
24  * Use color c.  Centered if cen is non-zero, right-justified if right is non-zero.
25  * Returns the y coordinate for any following line of text.
26  * Bug: color is ignored.
27  */
28 int m_text(int x, int y, char *p, char *q, int c, int cen, int right){
29 	Point tsize;
30 	USED(c);
31 	*q='\0';
32 	tsize=strsize(font, p);
33 	if(cen) x -= tsize.x/2;
34 	else if(right) x -= tsize.x;
35 	string(offscreen, Pt(x, y-tsize.y/2), font, p, S|D);
36 	return y+tsize.y;
37 }
38 /*
39  * Draw the vector from x0, y0 to x1, y1 in color c.
40  * Clipped by caller
41  */
42 void m_vector(int x0, int y0, int x1, int y1, int c){
43 	if(c<0) c=0;
44 	if(c>(1<<(1<<screen.ldepth))-1) c=(2<<screen.ldepth)-1;
45 	segment(offscreen, Pt(x0, y0), Pt(x1, y1), c, S);
46 }
47 /*
48  * Startup initialization
49  */
50 void m_initialize(char *s){
51 	static int first=1;
52 	int dx, dy;
53 	USED(s);
54 	if(first){
55 		binit(0,0,0);
56 		clipminx=mapminx=screen.r.min.x+4;
57 		clipminy=mapminy=screen.r.min.y+4;
58 		clipmaxx=mapmaxx=screen.r.max.x-5;
59 		clipmaxy=mapmaxy=screen.r.max.y-5;
60 		dx=clipmaxx-clipminx;
61 		dy=clipmaxy-clipminy;
62 		if(dx>dy){
63 			mapminx+=(dx-dy)/2;
64 			mapmaxx=mapminx+dy;
65 		}
66 		else{
67 			mapminy+=(dy-dx)/2;
68 			mapmaxy=mapminy+dx;
69 		}
70 		first=0;
71 	}
72 }
73 /*
74  * Clean up when finished
75  */
76 void m_finish(void){
77 	m_swapbuf();
78 }
79 void m_swapbuf(void){
80 	if(offscreen!=&screen)
81 		bitblt(&screen, offscreen->r.min, offscreen, offscreen->r, S);
82 	bflush();
83 }
84 void m_dblbuf(void){
85 	if(offscreen==&screen){
86 		offscreen=balloc(inset(screen.r, 4), screen.ldepth);
87 		if(offscreen==0){
88 			fprintf(stderr, "Can't double buffer\n");
89 			offscreen=&screen;
90 		}
91 	}
92 }
93