home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / windows / x / 19486 < prev    next >
Encoding:
Text File  |  1992-11-23  |  7.3 KB  |  331 lines

  1. Newsgroups: comp.windows.x
  2. Path: sparky!uunet!orca!orca!pmartz
  3. From: pmartz@dsd.es.com (Paul Martz)
  4. Subject: Re: How to open a TrueColor window?
  5. Message-ID: <1992Nov23.194338.15217@dsd.es.com>
  6. Sender: usenet@dsd.es.com
  7. Nntp-Posting-Host: bambam-gw
  8. Reply-To: pmartz@dsd.es.com (Paul Martz)
  9. Organization: Evans & Sutherland Computer Corp., Salt Lake City, UT
  10. References:  <1992Nov19.031410.22186%ptlai@et.nthu.edu.tw>
  11. Date: Mon, 23 Nov 92 19:43:38 GMT
  12. Lines: 317
  13.  
  14. In article <1992Nov19.031410.22186%ptlai@et.nthu.edu.tw>, ptlai@et.nthu.edu.tw (Pao-Tzung Lai) writes:
  15. > I have a problem in Xlib programming:
  16. > I want to create a window with TrueColor (depth 24) visual under a
  17. > parent window (root window) which visual is PseudoColor (depth 8).
  18. > Does anyone have experience on this? Any hint is appreciated.
  19.  
  20. This code looks for "-24" on the command line and opens up 24bit
  21. TrueColor if it is there. Hope it's not too confusing, it does a bunch
  22. of wierd colormap stuff too...
  23. -- 
  24.  
  25.     -paul    pmartz@dsd.es.com
  26.          Evans & Sutherland
  27.  
  28. ------------------------------------------------------
  29. #include <stdio.h>
  30. #include <math.h>
  31. #include <sys/time.h>
  32.  
  33. #include <Xlib.h>
  34. #include <Xutil.h>
  35.  
  36.  
  37. #define MAPW    100
  38. #define MAPH    100
  39. #define BDWIDTH    10
  40. #define LONGW    11
  41. #define LONGH    10
  42.  
  43. void loop ();
  44. double bif_time ();
  45.  
  46. Display    *dpy;
  47. Window    win;
  48. Pixmap    pmap, bdpmap, longpmap, d1pmap;
  49. GC    gc, d1gc;
  50. Colormap    cmap;
  51. XColor    colors[256];
  52. unsigned    line1, line2, line3, line4, line5, line6;
  53.  
  54. int    planemask;
  55. int    depth;
  56. int    class;
  57. int    nocmap;
  58.  
  59. int        gx = 0, gy = 0;
  60. unsigned int    gw = 128, gh = 128;
  61.  
  62.  
  63. main (argc, argv)
  64.      int    argc;
  65.      char    *argv[];
  66. {
  67.     int    i;
  68.  
  69.     depth = 8;
  70.     planemask = 0xff;
  71.     class = TrueColor;
  72.     cmap = 0;
  73.     for (i = 1; i<argc; i++ ) {
  74.     if ((strncmp(argv[i],"-24",strlen(argv[i]))) == 0) {
  75.         planemask = 0xffffff;
  76.         depth = 24;
  77.     }
  78.     if ((strncmp(argv[i],"-direct",strlen(argv[i]))) == 0) {
  79.         class = DirectColor;
  80.     }
  81.     if ((strncmp(argv[i],"-nocmap",strlen(argv[i]))) == 0) {
  82.         nocmap = 1;
  83.     }
  84.     if ((strncmp(argv[i],"-geom",strlen(argv[i]))) == 0) {
  85.         XParseGeometry (argv[i+1], &gx, &gy, &gw, &gh);
  86.     }
  87.     }
  88.  
  89.     if (openWindow ())
  90.     exit (1);
  91.  
  92.     loop ();
  93.  
  94.     XDestroyWindow (dpy, win);
  95.     XCloseDisplay (dpy);
  96.  
  97.     return (0);
  98. }
  99.  
  100.  
  101.  
  102. int openWindow ()
  103. {
  104.     XSizeHints xsh;
  105.     XSetWindowAttributes    xswa;
  106.     XVisualInfo    vi, *viret;
  107.     int    nitems, i;
  108.     XGCValues    gcvals;
  109.     Colormap    *imaps;
  110.  
  111.  
  112.     if (!(dpy = XOpenDisplay (NULL))) {
  113.     fprintf (stderr, "openWindow: Can't open display.\n");
  114.     return (1);
  115.     }
  116.     XSynchronize (dpy, 1);
  117.  
  118.  
  119.     if (depth == 24) {
  120.     vi.depth = 24;
  121.     vi.class = class;
  122.     }
  123.     else {
  124.     vi.depth = 8;
  125.     vi.class = PseudoColor;
  126.     }
  127.     if (viret = XGetVisualInfo (dpy, VisualDepthMask | VisualClassMask,
  128.                     &vi, &nitems)) {
  129.         printf ("using depth %d\n", viret[0].depth);
  130.     }
  131.     else
  132.       printf ("No visuals of depth %d\n", vi.depth);
  133.  
  134.     if (depth == 24) {
  135.     line1 = 0xf1ff80;
  136.     line2 = 0xdf83f2;
  137.     line3 = 0x4040f3;
  138.     line4 = 0xdf6600;
  139.     line5 = 0x0;
  140.     line6 = 0x00ff00;
  141.  
  142.     if (class == TrueColor)
  143.         cmap = XCreateColormap (dpy, RootWindow (dpy, 0),
  144.                     viret[0].visual, AllocNone);
  145.  
  146.     else {
  147.         cmap = XCreateColormap (dpy, RootWindow (dpy, 0),
  148.                     viret[0].visual, AllocAll);
  149.  
  150.         colors[0].pixel = line1;
  151.         colors[0].red = 0x8000;
  152.         colors[0].green = 0xff00;
  153.         colors[0].blue = 0xf100;
  154.         colors[0].flags = DoRed | DoGreen | DoBlue;
  155.         colors[1].pixel = line2;
  156.         colors[1].red = 0xf200;
  157.         colors[1].green = 0x8300;
  158.         colors[1].blue = 0xdf00;
  159.         colors[1].flags = DoRed | DoGreen | DoBlue;
  160.         colors[2].pixel = line3;
  161.         colors[2].red = 0xf300;
  162.         colors[2].green = 0x4000;
  163.         colors[2].blue = 0x4000;
  164.         colors[2].flags = DoRed | DoGreen | DoBlue;
  165.         colors[3].pixel = line4;
  166.         colors[3].red = 0x0000;
  167.         colors[3].green = 0x6600;
  168.         colors[3].blue = 0xdf00;
  169.         colors[3].flags = DoRed | DoGreen | DoBlue;
  170.         colors[4].pixel = line5;
  171.         colors[4].red = 0x0;
  172.         colors[4].green = 0x0;
  173.         colors[4].blue = 0x0;
  174.         colors[4].flags = DoRed | DoGreen | DoBlue;
  175.         colors[5].pixel = line6;
  176.         colors[5].red = 0x0;
  177.         colors[5].green = 0xff00;
  178.         colors[5].blue = 0x0;
  179.         colors[5].flags = DoRed | DoGreen | DoBlue;
  180.  
  181.         XStoreColors(dpy, cmap, colors, 6);
  182.     }
  183.     }
  184.     else {
  185.     if (!nocmap) {
  186.     cmap = XCreateColormap (dpy, RootWindow (dpy, 0),
  187.                 viret[0].visual, AllocAll);
  188.  
  189.     line1 = 2;
  190.     line2 = 3;
  191.     line3 = 4;
  192.     line4 = 5;
  193.     line5 = 6;
  194.     line6 = 7;
  195.  
  196.     colors[0].pixel = line1;
  197.     colors[0].red = 0x8000;
  198.     colors[0].green = 0xff00;
  199.     colors[0].blue = 0xf100;
  200.     colors[0].flags = DoRed | DoGreen | DoBlue;
  201.     colors[1].pixel = line2;
  202.     colors[1].red = 0xf200;
  203.     colors[1].green = 0x8300;
  204.     colors[1].blue = 0xdf00;
  205.     colors[1].flags = DoRed | DoGreen | DoBlue;
  206.     colors[2].pixel = line3;
  207.     colors[2].red = 0xf300;
  208.     colors[2].green = 0x4000;
  209.     colors[2].blue = 0x4000;
  210.     colors[2].flags = DoRed | DoGreen | DoBlue;
  211.     colors[3].pixel = line4;
  212.     colors[3].red = 0x0000;
  213.     colors[3].green = 0x6600;
  214.     colors[3].blue = 0xdf00;
  215.     colors[3].flags = DoRed | DoGreen | DoBlue;
  216.     colors[4].pixel = line5;
  217.     colors[4].red = 0x0;
  218.     colors[4].green = 0x0;
  219.     colors[4].blue = 0x0;
  220.     colors[4].flags = DoRed | DoGreen | DoBlue;
  221.     colors[5].pixel = line6;
  222.     colors[5].red = 0x0;
  223.     colors[5].green = 0xff00;
  224.     colors[5].blue = 0x0;
  225.     colors[5].flags = DoRed | DoGreen | DoBlue;
  226.  
  227.     XStoreColors(dpy, cmap, colors, 6);
  228.     }
  229.     else {
  230.  
  231.     cmap = XCreateColormap (dpy, RootWindow (dpy, 0),
  232.                 viret[0].visual, AllocAll);
  233.  
  234.     line1 = 2;
  235.     line2 = 3;
  236.     line3 = 4;
  237.     line4 = 5;
  238.     line5 = 6;
  239.     line6 = 7;
  240.  
  241.     colors[0].pixel = line4;
  242.     colors[0].red = 0xff00;
  243.     colors[0].green = 0x0000;
  244.     colors[0].blue = 0x0000;
  245.  
  246.     XStoreColors(dpy, cmap, colors, 1);
  247.     }
  248.     }
  249.     XInstallColormap (dpy, cmap);
  250.  
  251.     xsh.flags  = (PPosition | PSize);
  252.     xsh.width = gw;
  253.     xsh.height = gh;
  254.     xsh.x = gx;
  255.     xsh.y = gy;
  256.     xswa.background_pixel = BlackPixel (dpy, DefaultScreen (dpy));
  257.     xswa.border_pixel = line4;
  258.     xswa.colormap = cmap;
  259.     win = XCreateWindow (dpy, RootWindow (dpy, DefaultScreen(dpy)),
  260.              xsh.x, xsh.y, xsh.width, xsh.height, BDWIDTH,
  261.              vi.depth, InputOutput,
  262.              viret[0].visual,
  263.              CWBackPixel | CWColormap | CWBorderPixel,
  264.              &xswa);
  265.     XSetStandardProperties (dpy, win, "Test", "test", None, NULL, 0,
  266.                 &xsh);
  267.  
  268.  
  269.     pmap = XCreatePixmap (dpy, win, MAPW, MAPH, vi.depth);
  270.     d1pmap = XCreatePixmap (dpy, win, MAPW, MAPH, 1);
  271.     bdpmap = XCreatePixmap (dpy, win, BDWIDTH, BDWIDTH, vi.depth);
  272.     longpmap = XCreatePixmap (dpy, win, LONGW, LONGH, vi.depth);
  273.  
  274.     XSelectInput (dpy, win, ButtonPressMask | KeyPressMask | ExposureMask);
  275.  
  276.     gcvals.plane_mask = planemask;
  277.     gc = XCreateGC (dpy, win, GCPlaneMask, &gcvals);
  278.     d1gc = XCreateGC (dpy, d1pmap, 0, &gcvals);
  279.  
  280.     XMapWindow (dpy, win);
  281.  
  282.     XFree (viret);
  283.     return (0);
  284. }
  285.  
  286.  
  287.  
  288. void loop ()
  289. {
  290.     XEvent    event;
  291.     int    i;
  292.  
  293.  
  294.     while (1) {
  295.     XNextEvent (dpy, &event);
  296.     if (event.type == ButtonPress) {
  297.         return;
  298.     }
  299.     else if ((event.type == KeyPress) ||
  300.          (event.type == Expose)) {
  301.         do_stuff ();
  302.     }
  303.     }
  304. }
  305.  
  306.  
  307.  
  308. do_stuff (bp)
  309.     unsigned    bp;
  310. {
  311.     XPoint    pts[20];
  312.     XRectangle    rects[2];
  313.     XImage    *fooimage;
  314.     double    startTime, endTime, theTime;
  315.     XWindowAttributes    attrs;
  316.     int    i;
  317.  
  318.  
  319.     XClearWindow (dpy, win);
  320.  
  321.     XSetForeground (dpy, gc, 0xffffff);
  322.     for (i=0; i<20; i++) {
  323.     pts[i].y = 20;
  324.     pts[i].x = i * 11;
  325.     }
  326.     XDrawPoints (dpy, win, gc, pts, 20, CoordModeOrigin);
  327.  
  328.     printf ("window refresh complete\n");
  329. }
  330.