diff options
author | cinap_lenrek <cinap_lenrek@gmx.de> | 2013-04-22 19:10:09 +0200 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@gmx.de> | 2013-04-22 19:10:09 +0200 |
commit | 8ff7d518c60104f5d6aa033737d079f8f04f9ac0 (patch) | |
tree | 5e3ca81b3c33a106ed6a3191319e03a97d8ddb28 /sys/src/cmd/plot/libplot | |
parent | 05d3cc414dbf9657a394738fd5b7cea36f08c8ca (diff) |
- use the double-buffer buffer to allow redrawing on resize events.
specifing -d on the command line now only disables synchronous
drawing events.
- use threaded mouse and keyboard to allow for asynchronous
receoption of quit messages. this allows plot to exit before drawing
is completed. for programs like mapdemo, this is important.
there were two things that needed to get fixed as a result
- replace fprint(2, ...); exits("bad"); with sysfatal. also get rid
of stdio.
- dpoint needed a mach-dependentent (sic) version. otherwise
points on a resized screen will not be properly placed.
Diffstat (limited to 'sys/src/cmd/plot/libplot')
-rw-r--r-- | sys/src/cmd/plot/libplot/dpoint.c | 3 | ||||
-rw-r--r-- | sys/src/cmd/plot/libplot/fill.c | 3 | ||||
-rw-r--r-- | sys/src/cmd/plot/libplot/machdep.c | 179 | ||||
-rw-r--r-- | sys/src/cmd/plot/libplot/mplot.h | 3 | ||||
-rw-r--r-- | sys/src/cmd/plot/libplot/ppause.c | 1 | ||||
-rw-r--r-- | sys/src/cmd/plot/libplot/pprompt.c | 2 |
6 files changed, 104 insertions, 87 deletions
diff --git a/sys/src/cmd/plot/libplot/dpoint.c b/sys/src/cmd/plot/libplot/dpoint.c index 7d5b6ff87..1e93b8113 100644 --- a/sys/src/cmd/plot/libplot/dpoint.c +++ b/sys/src/cmd/plot/libplot/dpoint.c @@ -1,6 +1,5 @@ #include "mplot.h" void dpoint(double x, double y){ - draw(screen, Rect(SCX(x), SCY(y), SCX(x)+1, SCY(y)+1), getcolor(e1->foregr), - nil, ZP); + m_dpt(x, y); move(x, y); } diff --git a/sys/src/cmd/plot/libplot/fill.c b/sys/src/cmd/plot/libplot/fill.c index a33294e50..12eff9931 100644 --- a/sys/src/cmd/plot/libplot/fill.c +++ b/sys/src/cmd/plot/libplot/fill.c @@ -47,8 +47,7 @@ static void polygon(int cnt[], double *pts[], Windrule w, int v){ edges=(Edge *)malloc(nvert*sizeof(Edge)); if(edges==0){ NoSpace: - fprintf(stderr, "polygon: no space\n"); - exits("malloc failed"); + sysfatal("polygon: no space"); } ylist=(Edge **)malloc(Dy(screen->r)*sizeof(Edge *)); if(ylist==0) goto NoSpace; diff --git a/sys/src/cmd/plot/libplot/machdep.c b/sys/src/cmd/plot/libplot/machdep.c index cc7027438..64a2d7f91 100644 --- a/sys/src/cmd/plot/libplot/machdep.c +++ b/sys/src/cmd/plot/libplot/machdep.c @@ -1,114 +1,135 @@ #include "mplot.h" Image *offscreen; +static int buffer; + +static Point +xlp(Point p) +{ + p.x += screen->r.min.x + 4 - offscreen->r.min.x; + p.y += screen->r.min.y + 4 - offscreen->r.min.y; + return p; +} + +static Rectangle +xlr(Rectangle r) +{ + int dx, dy; + + dx = screen->r.min.x + 4 - offscreen->r.min.x; + dy = screen->r.min.y + 4 - offscreen->r.min.y; + r.min.x += dx; + r.min.y += dy; + r.max.x += dx; + r.max.y += dy; + return r; +} + /* * Clear the window from x0, y0 to x1, y1 (inclusive) to color c */ -void m_clrwin(int x0, int y0, int x1, int y1, int c){ +void +m_clrwin(int x0, int y0, int x1, int y1, int c) +{ draw(offscreen, Rect(x0, y0, x1+1, y1+1), getcolor(c), nil, ZP); + if(offscreen != screen && !buffer) + draw(screen, xlr(Rect(x0, y0, x1+1, y1+1)), getcolor(c), nil, ZP); } /* * Draw text between pointers p and q with first character centered at x, y. * Use color c. Centered if cen is non-zero, right-justified if right is non-zero. * Returns the y coordinate for any following line of text. */ -int m_text(int x, int y, char *p, char *q, int c, int cen, int right){ +int +m_text(int x, int y, char *p, char *q, int c, int cen, int right) +{ Point tsize; - USED(c); - tsize=stringsize(font, p); - if(cen) x -= tsize.x/2; - else if(right) x -= tsize.x; + + tsize = stringsize(font, p); + if(cen) + x -= tsize.x/2; + else if(right) + x -= tsize.x; stringn(offscreen, Pt(x, y-tsize.y/2), getcolor(c), ZP, font, p, q-p); + if(offscreen != screen && !buffer) + stringn(screen, xlp(Pt(x, y-tsize.y/2)), getcolor(c), ZP, font, p, q-p); return y+tsize.y; } /* + * draw point x, y + */ +void +m_dpt(double x, double y) +{ + Image *c; + + c = getcolor(e1->foregr); + draw(offscreen, Rect(SCX(x), SCY(y), SCX(x)+1, SCY(y)+1), c, nil, ZP); + if(offscreen != screen && !buffer) + draw(screen, xlr(Rect(SCX(x), SCY(y), SCX(x)+1, SCY(y)+1)), c, nil, ZP); +} + +/* * Draw the vector from x0, y0 to x1, y1 in color c. * Clipped by caller */ -void m_vector(int x0, int y0, int x1, int y1, int c){ +void +m_vector(int x0, int y0, int x1, int y1, int c) +{ line(offscreen, Pt(x0, y0), Pt(x1, y1), Endsquare, Endsquare, 0, getcolor(c), ZP); -} -char *scanint(char *s, int *n){ - while(*s<'0' || '9'<*s){ - if(*s=='\0'){ - fprint(2, "plot: bad -Wxmin,ymin,xmax,ymax\n"); - exits("bad arg"); - } - s++; - } - *n=0; - while('0'<=*s && *s<='9'){ - *n=*n*10+*s-'0'; - s++; - } - return s; -} -char *rdenv(char *name){ - char *v; - int fd, size; - fd=open(name, OREAD); - if(fd<0) return 0; - size=seek(fd, 0, 2); - v=malloc(size+1); - if(v==0){ - fprint(2, "Can't malloc: %r\n"); - exits("no mem"); - } - seek(fd, 0, 0); - read(fd, v, size); - v[size]=0; - close(fd); - return v; + if(offscreen != screen && !buffer) + line(screen, xlp(Pt(x0, y0)), xlp(Pt(x1, y1)), Endsquare, Endsquare, 0, getcolor(c), ZP); } /* * Startup initialization */ -void m_initialize(char *s){ - static int first=1; +void m_initialize(char*) +{ + static int once; int dx, dy; - USED(s); - if(first){ - if(initdraw(0,0,"plot") < 0) - sysfatal("initdraw: %r"); - einit(Emouse); - clipminx=mapminx=screen->r.min.x+4; - clipminy=mapminy=screen->r.min.y+4; - clipmaxx=mapmaxx=screen->r.max.x-5; - clipmaxy=mapmaxy=screen->r.max.y-5; - dx=clipmaxx-clipminx; - dy=clipmaxy-clipminy; - if(dx>dy){ - mapminx+=(dx-dy)/2; - mapmaxx=mapminx+dy; - } - else{ - mapminy+=(dy-dx)/2; - mapmaxy=mapminy+dx; - } - first=0; - offscreen = screen; + + if(once) + return; + once = 1; + + if(initdraw(nil, nil, "plot") < 0) + sysfatal("initdraw: %r"); +///// einit(Emouse); + offscreen = allocimage(display, insetrect(screen->r, 4), screen->chan, 0, -1); + if(offscreen == nil) + sysfatal("Can't double buffer\n"); + clipminx = mapminx = screen->r.min.x+4; + clipminy = mapminy = screen->r.min.y+4; + clipmaxx = mapmaxx = screen->r.max.x-5; + clipmaxy = mapmaxy = screen->r.max.y-5; + dx = clipmaxx-clipminx; + dy = clipmaxy-clipminy; + if(dx>dy){ + mapminx += (dx-dy)/2; + mapmaxx = mapminx+dy; + } + else{ + mapminy += (dy-dx)/2; + mapmaxy = mapminy+dx; } } /* * Clean up when finished */ -void m_finish(void){ +void m_finish(void) +{ m_swapbuf(); } -void m_swapbuf(void){ - if(offscreen!=screen) - draw(screen, offscreen->r, offscreen, nil, offscreen->r.min); +void m_swapbuf(void) +{ + draw(screen, insetrect(screen->r, 4), offscreen, nil, offscreen->r.min); flushimage(display, 1); } -void m_dblbuf(void){ - if(offscreen==screen){ - offscreen=allocimage(display, insetrect(screen->r, 4), screen->chan, 0, -1); - if(offscreen==0){ - fprintf(stderr, "Can't double buffer\n"); - offscreen=screen; - } - } +void m_dblbuf(void) +{ + buffer = 1; } -/* Assume colormap entry because + +/* * Use cache to avoid repeated allocation. */ struct{ @@ -127,16 +148,14 @@ getcolor(int v) return icache[j].i; i = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, v); - if(i == nil){ - fprint(2, "plot: can't allocate image for color: %r\n"); - exits("allocimage"); - } + if(i == nil) + sysfatal("plot: can't allocate image for color: %r"); for(j=0; j<nelem(icache); j++) if(icache[j].i == nil){ icache[j].v = v; icache[j].i = i; break; } - +if(j == nelem(icache))sysfatal("icache: too small"); return i; } diff --git a/sys/src/cmd/plot/libplot/mplot.h b/sys/src/cmd/plot/libplot/mplot.h index 99e27efd1..6137a62c4 100644 --- a/sys/src/cmd/plot/libplot/mplot.h +++ b/sys/src/cmd/plot/libplot/mplot.h @@ -1,6 +1,6 @@ #include <u.h> #include <libc.h> -#include <stdio.h> +//#include <stdio.h> #include <draw.h> #include <event.h> #define SCX(A) ((((A) - e1->xmin)*e1->scalex + e1->left)+.5) @@ -39,6 +39,7 @@ void m_clrwin(int, int, int, int, int); void m_finish(void); void m_initialize(char *); int m_text(int, int, char *, char *, int, int, int); +void m_dpt(double, double); void m_vector(int, int, int, int, int); void m_swapbuf(void); void m_dblbuf(void); diff --git a/sys/src/cmd/plot/libplot/ppause.c b/sys/src/cmd/plot/libplot/ppause.c index 9fb8863d2..6f0e22e20 100644 --- a/sys/src/cmd/plot/libplot/ppause.c +++ b/sys/src/cmd/plot/libplot/ppause.c @@ -1,7 +1,6 @@ #include "mplot.h" void ppause(void){ char aa[4]; - fflush(stdout); read(0, aa, 4); erase(); } diff --git a/sys/src/cmd/plot/libplot/pprompt.c b/sys/src/cmd/plot/libplot/pprompt.c index 2678ead5f..1e730abf3 100644 --- a/sys/src/cmd/plot/libplot/pprompt.c +++ b/sys/src/cmd/plot/libplot/pprompt.c @@ -1,5 +1,5 @@ #include "mplot.h" void pprompt(void){ - fprintf(stderr, ":"); + fprint(2, ":"); } |