chiark / gitweb /
sotextpit
[moebius.git] / x11.cc
1 /*
2  * X11 functions
3  */
4
5
6 #include "x11.hh"
7 #include "parameter.hh"
8
9 static Parameter<int> x11size("x11size", "X11 window size", 500, 100, 10, 10000);
10
11 static unsigned long colour_pixel(Display *display, Colormap cmap,
12                                   const char *name) {
13   XColor colour;
14   Status st;
15   st= XAllocNamedColor(display,cmap,name,&colour,&colour);
16   if (!st) {
17     cerr << "cannot allocate colour " << name << ", quitting\n";
18     exit(1);
19   }
20   return colour.pixel;
21 }
22
23 X11Output::X11Output() {
24   XGCValues gcvalues;
25
26   display= XOpenDisplay(0);
27   window= XCreateSimpleWindow(display,
28                               DefaultRootWindow(display),
29                               0,0, x11size,x11size, 0,0,0);
30
31   Colormap cmap= DefaultColormap(display,DefaultScreen(display));
32   black= colour_pixel(display,cmap,"black");
33   white= colour_pixel(display,cmap,"white");
34
35   gcvalues.foreground= gcvalues.background= black;
36   fabric= XCreateGC(display,window,GCForeground|GCBackground,&gcvalues);
37
38   gcvalues.foreground= gcvalues.background= white;
39
40   mesh= XCreateGC(display,window,GCForeground|GCBackground,&gcvalues);
41
42   XSelectInput(display,window,0);
43   XMapWindow(display,window);
44   XFlush(display);
45 }
46
47 void X11Output::startimage() {
48   XClearWindow(display,window);
49 }
50
51 void X11Output::endimage() {
52   XFlush(display);
53 }
54
55 X11Output::~X11Output() {
56   XCloseDisplay(display);
57 }
58
59 void X11Output::drawcell(const Point* list, int n, Colour colour) {
60   GC fill;
61   bool draw;
62   
63   XPoint xp[n+1];
64   for (int i=0; i<n; i++) {
65     Onscreen here= Onscreen(list[i]);
66     xp[i].x= (int)((here.x+1.0)*(x11size*0.5));
67     xp[i].y= (int)((-here.y+1.0)*(x11size*0.5));
68   }
69   switch (colour) {
70   case grid:       fill= fabric; draw= true;  break;
71   case solidblack: fill= fabric; draw= false; break;
72   case solidwhite: fill= mesh;   draw= false; break;
73   default: abort();
74   }
75   XFillPolygon(display,window,fill,xp,n,Nonconvex,CoordModeOrigin);
76   if (draw) {
77     xp[n]= xp[0];
78     XDrawLines(display,window,mesh,xp,n+1,CoordModeOrigin);
79   }
80 }