chiark / gitweb /
merge into history old stuff found on chiark
[moebius.git] / dualx11.cc
1 /*
2  * X11 functions
3  */
4
5 #include "dualx11.hh"
6 #include "parameter.hh"
7
8 static Parameter<int> dualx11size("dualx11size", "Dual X11 window size",
9                                   500, 100, 10, 10000);
10
11 DualX11Output::DualX11Output() {
12   display= XOpenDisplay(0);
13   Colormap cmap= DefaultColormap(display,DefaultScreen(display));
14
15   XAllocColorCells(display,cmap,
16                    False, // contig
17                    planemasks,
18                    2, // nplanes
19                    &pixelbase,
20                    1); // ncolors
21   XColor colours[4], *cp= colours;
22   for (int i=0; i<2; i++)
23     for (int j=0; j<2; j++, cp++) {
24       cp->pixel= pixelbase + (i? planemasks[0] :0) + (j? planemasks[1] :0);
25       cp->red= i?65535:0;  cp->green= j?/*65535*/32000:0;  cp->blue= 0;
26       cp->flags= DoRed|DoGreen|DoBlue;
27     }
28   XStoreColors(display,cmap,colours,4);
29   
30   window= XCreateSimpleWindow(display,
31                               DefaultRootWindow(display),
32                               0,0, dualx11size,dualx11size, 0,0, pixelbase);
33
34   XGCValues gcvalues;
35   int i;
36   for (i=0; i<2; i++) {
37     gcvalues.plane_mask= planemasks[i];
38     // First, the fabric
39     gcvalues.function= GXclear;
40     fabric[i]= XCreateGC(display,window,
41                          GCFunction|GCPlaneMask,
42                          &gcvalues);
43     // Then, the mesh
44     gcvalues.function= GXset;
45     mesh[i]= XCreateGC(display,window,
46                        GCFunction|GCPlaneMask,
47                        &gcvalues);
48   }
49   XSelectInput(display,window,0);
50   XMapWindow(display,window);
51   XFlush(display);
52 }
53
54 void DualX11Output::startimage() {
55   XClearWindow(display,window);
56 }
57
58 void DualX11Output::endimage() {
59   XFlush(display);
60 }
61
62 DualX11Output::~DualX11Output() {
63   XCloseDisplay(display);
64 }
65
66 void DualX11Output::drawcell(const Point* list, int n, Colour colour) {
67   static Parameter<double>
68     eyeseparation("eyesep", "Distance from projection eye to origin", 
69                   0.3, .1, 0., 100.);
70
71   for (int i=0; i<2; i++) {
72     GC fill;
73     bool draw;
74     
75     Point::seteyex(eyeseparation*(i-0.5));
76
77     switch (colour) {
78     case grid:       fill= fabric[i]; draw= true;  break;
79     case solidblack: fill= fabric[i]; draw= false; break;
80     case solidwhite: fill= mesh[i];   draw= false; break;
81     default: abort();
82     }
83
84     XPoint xp[n+1];
85     for (int j=0; j<n; j++) {
86       Onscreen here= Onscreen(list[j]);
87       xp[j].x= (int)((here.x+1.0)*(dualx11size*0.5));
88       xp[j].y= (int)((-here.y+1.0)*(dualx11size*0.5));
89     }
90
91     XFillPolygon(display,window,fill,xp,n,Nonconvex,CoordModeOrigin);
92     if (draw) {
93       xp[n]= xp[0];
94       XDrawLines(display,window,mesh[i],xp,n+1,CoordModeOrigin);
95     }
96   }
97   Point::seteyex(0);
98 }