chiark / gitweb /
can resize
[moebius2.git] / project.c
index 0921edfb52291b4c0f8addb866e920b026bf6b8c..521af66786895c6b7f5a51603a689cfe4db46b6a 100644 (file)
--- a/project.c
+++ b/project.c
@@ -91,10 +91,10 @@ static Display *display;
 static Pixmap pixmap, doublebuffers[2];
 static Window window;
 static GC linegc, fillgc;
-static int wwidth=WSZ, wheight=WSZ, wmaxdim=WSZ, currentbuffer;
-static int ncut;
+static int wwidth=WSZ, wheight=WSZ, wmindim=WSZ, wmaxdim=WSZ;
+static int ncut, currentbuffer, x11depth, x11screen;
 
-static double scale= WSZ * 0.3;
+static double scale= 0.3;
 static double eye_z= -10, eye_x= 0;
 static double cut_z= -9;
 
@@ -117,8 +117,8 @@ static void drawtriangle(const Triangle *t) {
     if (z < cut_z) { ncut++; return; }
     
     double zezezp= eye_z / (eye_z - z);
-    points[i].x= scale * (zezezp * (x - eye_x) + eye_x) + WSZ/2;
-    points[i].y= scale * (zezezp *  y                 ) + WSZ/2;
+    points[i].x= scale * wmindim * (zezezp * (x - eye_x) + eye_x) + wwidth/2;
+    points[i].y= scale * wmindim * (zezezp *  y                 ) + wheight/2;
   }
   points[3]= points[0];
 
@@ -129,21 +129,28 @@ static void drawtriangle(const Triangle *t) {
 static const unsigned long core_event_mask=
   ButtonPressMask|ButtonReleaseMask|StructureNotifyMask|ButtonMotionMask;
 
+static void mkpixmaps(void) {
+  for (currentbuffer=0; currentbuffer<2; currentbuffer++) {
+    XA( pixmap= XCreatePixmap(display,window,wwidth,wheight,x11depth) );
+    doublebuffers[currentbuffer]= pixmap;
+  }
+  currentbuffer= 0;
+}
+
 static void display_prepare(void) {
   XGCValues gcv;
   XSetWindowAttributes wa;
-  int screen, depth;
   XVisualInfo vinfo;
   XSizeHints hints;
   
   XA( display= XOpenDisplay(0) );
-  screen= DefaultScreen(display);
-  depth= DefaultDepth(display,screen);
-  XA( XMatchVisualInfo(display,screen,depth, TrueColor,&vinfo) );
+  x11screen= DefaultScreen(display);
+  x11depth= DefaultDepth(display,x11screen);
+  XA( XMatchVisualInfo(display,x11screen,x11depth, TrueColor,&vinfo) );
   
   wa.event_mask= core_event_mask;
   XA( window= XCreateWindow(display, DefaultRootWindow(display),
-                           0,0, wwidth,wheight, 0,depth,
+                           0,0, wwidth,wheight, 0,x11depth,
                            InputOutput, vinfo.visual,
                            CWEventMask, &wa) );
 
@@ -151,22 +158,17 @@ static void display_prepare(void) {
   hints.x= 10;
   hints.y= 10;
   XSetWMNormalHints(display,window,&hints);
-  
-  for (currentbuffer=0; currentbuffer<2; currentbuffer++) {
-    XA( pixmap= XCreatePixmap(display,window,wwidth,wheight,depth) );
-    doublebuffers[currentbuffer]= pixmap;
-  }
+
+  mkpixmaps();
 
   gcv.function= GXcopy;
   gcv.plane_mask= ~0UL;
-  gcv.foreground= WhitePixel(display,screen);
+  gcv.foreground= WhitePixel(display,x11screen);
   linegc= XCreateGC(display,pixmap, GCFunction|GCPlaneMask|GCForeground, &gcv);
   
   gcv.function= GXclear;
   gcv.plane_mask= ~0UL;
   fillgc= XCreateGC(display,pixmap, GCFunction|GCPlaneMask, &gcv);
-
-  currentbuffer= 0;
 }
 
 static void display_conformation(void) {
@@ -305,7 +307,21 @@ static void event_motion(int x, int y) {
   drag_position(x,y);
 }
 
-static void event_config(XConfigureEvent *e) { printf("configure\n"); }
+static void event_config(XConfigureEvent *e) {
+  if (e->width == wwidth && e->height == wheight)
+    return;
+
+  wwidth= e->width;  wheight= e->height;
+  wmaxdim= wwidth > wheight ? wwidth : wheight;
+  wmindim= wwidth < wheight ? wwidth : wheight;
+  
+  XA( XSetWindowBackground(display,window,BlackPixel(display,x11screen)) );
+  for (currentbuffer=0; currentbuffer<2; currentbuffer++)
+    XA( XFreePixmap(display, doublebuffers[currentbuffer]) );
+
+  mkpixmaps();
+  show();
+}
 
 int main(int argc, const char *const *argv) {
   XEvent event;