Merge branch 'gallium-0.1' into gallium-tex-surfaces
[mesa.git] / progs / xdemos / manywin.c
index 797029cc4cb5a6e4a3c917c307ac5986a2cdbea9..cfea555210af8c615803a4347baa6260f0ef2eca 100644 (file)
@@ -1,8 +1,7 @@
-/* $Id: manywin.c,v 1.2 2000/12/02 20:33:05 brianp Exp $ */
-
 /*
- * Create N GLX windows/contexts and render to them in round-robin
- * order.
+ * Create N GLX windows/contexts and render to them in round-robin order.
+ * Also, have the contexts share all texture objects.
+ * Press 'd' to delete a texture, 'u' to unbind it.
  *
  * Copyright (C) 2000  Brian Paul   All Rights Reserved.
  * 
 
 #include <GL/gl.h>
 #include <GL/glx.h>
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
+#include <X11/keysym.h>
 
 
 /*
@@ -51,13 +53,13 @@ struct head {
 static struct head Heads[MAX_HEADS];
 static int NumHeads = 0;
 static GLboolean SwapSeparate = GL_TRUE;
-
+static GLuint TexObj = 0;
 
 
 static void
 Error(const char *display, const char *msg)
 {
-   fprintf(stderr, "Error on display %s - %s\n", display, msg);
+   fprintf(stderr, "Error on display %s - %s\n", XDisplayName(display), msg);
    exit(1);
 }
 
@@ -130,8 +132,14 @@ AddHead(const char *displayName, const char *name)
                               None, (char **)NULL, 0, &sizehints);
    }
 
-
-   ctx = glXCreateContext(dpy, visinfo, NULL, True);
+   if (NumHeads == 0) {
+      ctx = glXCreateContext(dpy, visinfo, NULL, True);
+   }
+   else {
+      /* share textures & dlists with 0th context */
+      printf("sharing\n");
+      ctx = glXCreateContext(dpy, visinfo, Heads[0].Context, True);
+   }
    if (!ctx) {
       Error(displayName, "Couldn't create GLX context");
       return NULL;
@@ -142,8 +150,29 @@ AddHead(const char *displayName, const char *name)
    if (!glXMakeCurrent(dpy, win, ctx)) {
       Error(displayName, "glXMakeCurrent failed");
       printf("glXMakeCurrent failed in Redraw()\n");
-      return;
+      return NULL;
+   }
+
+   if (NumHeads == 0) {
+      /* create texture object now */
+      static const GLubyte checker[2][2][4] = {
+         { {255, 255, 255, 255}, {  0,   0,   0, 255} },
+         { {  0,   0,   0,   0}, {255, 255, 255, 255} }
+      };
+      glGenTextures(1, &TexObj);
+      assert(TexObj);
+      glBindTexture(GL_TEXTURE_2D, TexObj);
+      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGB,
+                   GL_UNSIGNED_BYTE, checker);
+      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+   }
+   else {
+      /* bind 0th context's texture in this context too */
+      assert(TexObj);
+      glBindTexture(GL_TEXTURE_2D, TexObj);
    }
+   glEnable(GL_TEXTURE_2D);
 
    /* save the info for this head */
    {
@@ -163,6 +192,18 @@ AddHead(const char *displayName, const char *name)
 }
 
 
+static void
+DestroyHeads(void)
+{
+   int i;
+   for (i = 0; i < NumHeads; i++) {
+      XDestroyWindow(Heads[i].Dpy, Heads[i].Win);
+      glXDestroyContext(Heads[i].Dpy, Heads[i].Context);
+      XCloseDisplay(Heads[i].Dpy);
+   }
+}
+
+
 static void
 Redraw(struct head *h)
 {
@@ -183,9 +224,9 @@ Redraw(struct head *h)
    glPushMatrix();
    glRotatef(h->Angle, 0, 0, 1);
    glBegin(GL_TRIANGLES);
-   glVertex2f(0, 0.8);
-   glVertex2f(-0.8, -0.7);
-   glVertex2f(0.8, -0.7);
+   glTexCoord2f(0.5, 1.0);   glVertex2f(0, 0.8);
+   glTexCoord2f(0.0, 0.0);   glVertex2f(-0.8, -0.7);
+   glTexCoord2f(1.0, 0.0);   glVertex2f(0.8, -0.7);
    glEnd();
    glPopMatrix();
 
@@ -238,7 +279,30 @@ EventLoop(void)
                      Resize(h, event.xconfigure.width, event.xconfigure.height);
                      break;
                   case KeyPress:
-                     return;
+                     {
+                        char buf[100];
+                        KeySym keySym;
+                        XComposeStatus stat;
+                        XLookupString(&event.xkey, buf, sizeof(buf), &keySym, &stat);
+                        switch (keySym) {
+                           case XK_Escape:
+                              exit(0);
+                              break;
+                           case XK_d:
+                           case XK_D:
+                              printf("Delete Texture in window %d\n", i);
+                              glXMakeCurrent(h->Dpy, h->Win, h->Context);
+                              glDeleteTextures(1, &TexObj);
+                              break;
+                           case XK_u:
+                           case XK_U:
+                              printf("Unbind Texture in window %d\n", i);
+                              glXMakeCurrent(h->Dpy, h->Win, h->Context);
+                              glBindTexture(GL_TEXTURE_2D, 0);
+                              break;
+                        }
+                     }
+                     break;
                   default:
                      /*no-op*/ ;
                }
@@ -269,9 +333,9 @@ static void
 PrintInfo(const struct head *h)
 {
    printf("Name: %s\n", h->DisplayName);
-   printf("  Display:     0x%x\n", h->Dpy);
-   printf("  Window:      0x%x\n", h->Win);
-   printf("  Context:     0x%x\n", h->Context);
+   printf("  Display:     %p\n", (void *) h->Dpy);
+   printf("  Window:      0x%x\n", (int) h->Win);
+   printf("  Context:     0x%lx\n", (long) h->Context);
    printf("  GL_VERSION:  %s\n", h->Version);
    printf("  GL_VENDOR:   %s\n", h->Vendor);
    printf("  GL_RENDERER: %s\n", h->Renderer);
@@ -281,9 +345,10 @@ PrintInfo(const struct head *h)
 int
 main(int argc, char *argv[])
 {
+   char *dpyName = NULL;
    int i;
+
    if (argc == 1) {
-      struct head *h;
       printf("manywin: open N simultaneous glx windows\n");
       printf("Usage:\n");
       printf("  manywin [-s] numWindows\n");
@@ -299,6 +364,10 @@ main(int argc, char *argv[])
          if (strcmp(argv[i], "-s") == 0) {
             SwapSeparate = GL_FALSE;
          }
+         else if (strcmp(argv[i], "-display") == 0 && i < argc) {
+            dpyName = argv[i+1];
+            i++;
+         }
          else {
             n = atoi(argv[i]);
          }
@@ -311,7 +380,7 @@ main(int argc, char *argv[])
          char name[100];
          struct head *h;
          sprintf(name, "%d", i);
-         h = AddHead(":0", name);
+         h = AddHead(dpyName, name);
          if (h) {
             PrintInfo(h);
          }
@@ -319,5 +388,6 @@ main(int argc, char *argv[])
    }
 
    EventLoop();
+   DestroyHeads();
    return 0;
 }