Blame


1 4314729d 2004-04-14 devnull #include "mplot.h"
2 67e4fce4 2004-04-19 devnull int mapminx, mapminy, mapmaxx, mapmaxy;
3 4314729d 2004-04-14 devnull Image *offscreen;
4 4314729d 2004-04-14 devnull /*
5 4314729d 2004-04-14 devnull * Clear the window from x0, y0 to x1, y1 (inclusive) to color c
6 4314729d 2004-04-14 devnull */
7 4314729d 2004-04-14 devnull void m_clrwin(int x0, int y0, int x1, int y1, int c){
8 4314729d 2004-04-14 devnull draw(offscreen, Rect(x0, y0, x1+1, y1+1), getcolor(c), nil, ZP);
9 4314729d 2004-04-14 devnull }
10 4314729d 2004-04-14 devnull /*
11 4314729d 2004-04-14 devnull * Draw text between pointers p and q with first character centered at x, y.
12 4314729d 2004-04-14 devnull * Use color c. Centered if cen is non-zero, right-justified if right is non-zero.
13 4314729d 2004-04-14 devnull * Returns the y coordinate for any following line of text.
14 4314729d 2004-04-14 devnull */
15 4314729d 2004-04-14 devnull int m_text(int x, int y, char *p, char *q, int c, int cen, int right){
16 4314729d 2004-04-14 devnull Point tsize;
17 4314729d 2004-04-14 devnull USED(c);
18 4314729d 2004-04-14 devnull tsize=stringsize(font, p);
19 4314729d 2004-04-14 devnull if(cen) x -= tsize.x/2;
20 4314729d 2004-04-14 devnull else if(right) x -= tsize.x;
21 4314729d 2004-04-14 devnull stringn(offscreen, Pt(x, y-tsize.y/2), getcolor(c), ZP, font, p, q-p);
22 4314729d 2004-04-14 devnull return y+tsize.y;
23 4314729d 2004-04-14 devnull }
24 4314729d 2004-04-14 devnull /*
25 4314729d 2004-04-14 devnull * Draw the vector from x0, y0 to x1, y1 in color c.
26 4314729d 2004-04-14 devnull * Clipped by caller
27 4314729d 2004-04-14 devnull */
28 4314729d 2004-04-14 devnull void m_vector(int x0, int y0, int x1, int y1, int c){
29 4314729d 2004-04-14 devnull line(offscreen, Pt(x0, y0), Pt(x1, y1), Endsquare, Endsquare, 0, getcolor(c), ZP);
30 4314729d 2004-04-14 devnull }
31 4314729d 2004-04-14 devnull char *scanint(char *s, int *n){
32 4314729d 2004-04-14 devnull while(*s<'0' || '9'<*s){
33 4314729d 2004-04-14 devnull if(*s=='\0'){
34 4314729d 2004-04-14 devnull fprint(2, "plot: bad -Wxmin,ymin,xmax,ymax\n");
35 4314729d 2004-04-14 devnull exits("bad arg");
36 4314729d 2004-04-14 devnull }
37 4314729d 2004-04-14 devnull s++;
38 4314729d 2004-04-14 devnull }
39 4314729d 2004-04-14 devnull *n=0;
40 4314729d 2004-04-14 devnull while('0'<=*s && *s<='9'){
41 4314729d 2004-04-14 devnull *n=*n*10+*s-'0';
42 4314729d 2004-04-14 devnull s++;
43 4314729d 2004-04-14 devnull }
44 4314729d 2004-04-14 devnull return s;
45 4314729d 2004-04-14 devnull }
46 4314729d 2004-04-14 devnull char *rdenv(char *name){
47 4314729d 2004-04-14 devnull char *v;
48 4314729d 2004-04-14 devnull int fd, size;
49 4314729d 2004-04-14 devnull fd=open(name, OREAD);
50 4314729d 2004-04-14 devnull if(fd<0) return 0;
51 4314729d 2004-04-14 devnull size=seek(fd, 0, 2);
52 4314729d 2004-04-14 devnull v=malloc(size+1);
53 4314729d 2004-04-14 devnull if(v==0){
54 4314729d 2004-04-14 devnull fprint(2, "Can't malloc: %r\n");
55 4314729d 2004-04-14 devnull exits("no mem");
56 4314729d 2004-04-14 devnull }
57 4314729d 2004-04-14 devnull seek(fd, 0, 0);
58 4314729d 2004-04-14 devnull read(fd, v, size);
59 4314729d 2004-04-14 devnull v[size]=0;
60 4314729d 2004-04-14 devnull close(fd);
61 4314729d 2004-04-14 devnull return v;
62 4314729d 2004-04-14 devnull }
63 4314729d 2004-04-14 devnull /*
64 4314729d 2004-04-14 devnull * Startup initialization
65 4314729d 2004-04-14 devnull */
66 4314729d 2004-04-14 devnull void m_initialize(char *s){
67 4314729d 2004-04-14 devnull static int first=1;
68 4314729d 2004-04-14 devnull int dx, dy;
69 4314729d 2004-04-14 devnull USED(s);
70 4314729d 2004-04-14 devnull if(first){
71 2e65dc0d 2005-01-11 devnull if(initdraw(0,0,"plot") < 0)
72 2e65dc0d 2005-01-11 devnull sysfatal("plot: can't open display: %r");
73 4314729d 2004-04-14 devnull einit(Emouse);
74 4314729d 2004-04-14 devnull clipminx=mapminx=screen->r.min.x+4;
75 4314729d 2004-04-14 devnull clipminy=mapminy=screen->r.min.y+4;
76 4314729d 2004-04-14 devnull clipmaxx=mapmaxx=screen->r.max.x-5;
77 4314729d 2004-04-14 devnull clipmaxy=mapmaxy=screen->r.max.y-5;
78 4314729d 2004-04-14 devnull dx=clipmaxx-clipminx;
79 4314729d 2004-04-14 devnull dy=clipmaxy-clipminy;
80 4314729d 2004-04-14 devnull if(dx>dy){
81 4314729d 2004-04-14 devnull mapminx+=(dx-dy)/2;
82 4314729d 2004-04-14 devnull mapmaxx=mapminx+dy;
83 4314729d 2004-04-14 devnull }
84 4314729d 2004-04-14 devnull else{
85 4314729d 2004-04-14 devnull mapminy+=(dy-dx)/2;
86 4314729d 2004-04-14 devnull mapmaxy=mapminy+dx;
87 4314729d 2004-04-14 devnull }
88 4314729d 2004-04-14 devnull first=0;
89 4314729d 2004-04-14 devnull offscreen = screen;
90 4314729d 2004-04-14 devnull }
91 4314729d 2004-04-14 devnull }
92 4314729d 2004-04-14 devnull /*
93 4314729d 2004-04-14 devnull * Clean up when finished
94 4314729d 2004-04-14 devnull */
95 4314729d 2004-04-14 devnull void m_finish(void){
96 4314729d 2004-04-14 devnull m_swapbuf();
97 4314729d 2004-04-14 devnull }
98 4314729d 2004-04-14 devnull void m_swapbuf(void){
99 4314729d 2004-04-14 devnull if(offscreen!=screen)
100 4314729d 2004-04-14 devnull draw(screen, offscreen->r, offscreen, nil, offscreen->r.min);
101 4314729d 2004-04-14 devnull flushimage(display, 1);
102 4314729d 2004-04-14 devnull }
103 4314729d 2004-04-14 devnull void m_dblbuf(void){
104 4314729d 2004-04-14 devnull if(offscreen==screen){
105 4314729d 2004-04-14 devnull offscreen=allocimage(display, insetrect(screen->r, 4), screen->chan, 0, -1);
106 4314729d 2004-04-14 devnull if(offscreen==0){
107 4314729d 2004-04-14 devnull fprintf(stderr, "Can't double buffer\n");
108 4314729d 2004-04-14 devnull offscreen=screen;
109 4314729d 2004-04-14 devnull }
110 4314729d 2004-04-14 devnull }
111 4314729d 2004-04-14 devnull }
112 4314729d 2004-04-14 devnull /* Assume colormap entry because
113 4314729d 2004-04-14 devnull * Use cache to avoid repeated allocation.
114 4314729d 2004-04-14 devnull */
115 4314729d 2004-04-14 devnull struct{
116 4314729d 2004-04-14 devnull int v;
117 4314729d 2004-04-14 devnull Image *i;
118 4314729d 2004-04-14 devnull }icache[32];
119 4314729d 2004-04-14 devnull
120 4314729d 2004-04-14 devnull Image*
121 4314729d 2004-04-14 devnull getcolor(int v)
122 4314729d 2004-04-14 devnull {
123 4314729d 2004-04-14 devnull Image *i;
124 4314729d 2004-04-14 devnull int j;
125 4314729d 2004-04-14 devnull
126 4314729d 2004-04-14 devnull for(j=0; j<nelem(icache); j++)
127 4314729d 2004-04-14 devnull if(icache[j].v==v && icache[j].i!=nil)
128 4314729d 2004-04-14 devnull return icache[j].i;
129 4314729d 2004-04-14 devnull
130 4314729d 2004-04-14 devnull i = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, v);
131 4314729d 2004-04-14 devnull if(i == nil){
132 4314729d 2004-04-14 devnull fprint(2, "plot: can't allocate image for color: %r\n");
133 4314729d 2004-04-14 devnull exits("allocimage");
134 4314729d 2004-04-14 devnull }
135 4314729d 2004-04-14 devnull for(j=0; j<nelem(icache); j++)
136 4314729d 2004-04-14 devnull if(icache[j].i == nil){
137 4314729d 2004-04-14 devnull icache[j].v = v;
138 4314729d 2004-04-14 devnull icache[j].i = i;
139 4314729d 2004-04-14 devnull break;
140 4314729d 2004-04-14 devnull }
141 4314729d 2004-04-14 devnull
142 4314729d 2004-04-14 devnull return i;
143 4314729d 2004-04-14 devnull }