home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume13 / jetpack / part03 / initx.c < prev    next >
C/C++ Source or Header  |  1992-04-10  |  5KB  |  171 lines

  1. /*    initx.c : routines that set up the X resources and colors.
  2. */
  3.  
  4. #include "copyright.h"
  5. #include "defs.h"
  6.  
  7. Display            *display;
  8. Colormap        colormap;
  9. Window            rootwindow;
  10. int                screen, depth;
  11. unsigned long    blackpixel, whitepixel;
  12. XFontStruct        *fontinfo, *bigfontinfo;
  13.  
  14. /*    initialize the colortable with the names of the colors (from rgb.txt)
  15. */
  16. struct colors    ctable[] = {
  17.     { "black"        },
  18.     { "red"            },
  19.     { "orange"        },
  20.     { "yellow"        },
  21.     { "RoyalBlue2"    },
  22.     { "grey50"        },
  23.     { "green"        },
  24.     { "white"        }
  25. };
  26.  
  27. /*    Depending on your system, the fontnames listed here may not be
  28.     complete enough to pick the right font. The system I developed on
  29.     has 475 fonts, so these should be specific enough. But just in
  30.     case, here are the full fontnames:
  31.  
  32.     small font:
  33.     -misc-fixed-medium-r-normal--10-70-100-100-c-60-iso8859-1
  34.  
  35.     big font:
  36.     -adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1
  37.  
  38.     It also might be that you won't have these fonts. If not, pick two
  39.     fonts that look good. The program adjusts things on the screen for
  40.     whatever fonts the program uses, so almost anything should do.
  41.     Just pick a pretty small font for the small font, and a reasonably
  42.     large font for the big font. Use xfontsel to make this easier
  43.     since it makes it really easy to pick fonts, and you can paste the
  44.     resulting fontname right into the code.
  45. */
  46.  
  47. static char    *fontname = "-*-fixed-medium-r-normal--*-70-*-*-c-*-*-*";
  48. static char    *bigfontname = "-*-courier-bold-r-normal--*-180-*-*-m-150-*-*";
  49.  
  50. /*    init_X opens the display and sets up all the color stuff
  51. */
  52. init_X()
  53. {
  54.     display = XOpenDisplay(NULL);
  55.     if (display == NULL) {
  56.        fprintf(stderr, "Jetpack : Cannot connect to X Server %s\n",
  57.                XDisplayName(NULL));
  58.        exit(1);
  59.     }
  60.     rootwindow = DefaultRootWindow(display);
  61.     screen = DefaultScreen(display);
  62.     colormap = DefaultColormap(display, screen);
  63.     depth = DefaultDepth(display, screen);
  64.        blackpixel = BlackPixel(display, screen);
  65.        whitepixel = WhitePixel(display, screen);
  66.     set_up_colors();
  67.     set_up_contexts();
  68. }
  69.  
  70. /*    set_up_contexts creates the graphics smallgcs used in the game, and
  71.     stores them in the ctable array. The fonts are created here too.
  72. */
  73. set_up_contexts()
  74. {
  75.     int            i;
  76.     XGCValues    values;
  77.     Font        small, big;
  78.  
  79.     fontinfo = XLoadQueryFont(display, fontname);
  80.     if(fontinfo == NULL) {
  81.         fprintf(stderr, "Jetpack : Couldn't load %s font.\n",fontname);
  82.         exit(1);
  83.     }
  84.     bigfontinfo = XLoadQueryFont(display, bigfontname);
  85.     if(bigfontinfo == NULL) {
  86.         fprintf(stderr, "Jetpack : Couldn't load %s font.\n",bigfontname);
  87.         exit(1);
  88.     }
  89.     big = bigfontinfo->fid;
  90.     small = fontinfo->fid;
  91.     values.font = small;
  92.     values.function = GXcopy;
  93.     ctable[CBLACK].smallgc = XCreateGC(display, rootwindow,
  94.                                         GCFunction|GCFont, &values);
  95.     values.font = big;
  96.     ctable[CBLACK].biggc = XCreateGC(display, rootwindow,
  97.                                         GCFunction|GCFont, &values);
  98.     values.function = GXor;
  99.     for(i=1; i<NCOLORS; i++) {
  100.         values.font = small;
  101.         ctable[i].smallgc = XCreateGC(display, rootwindow,
  102.                                         GCFunction|GCFont, &values);
  103.         values.font = big;
  104.         ctable[i].biggc = XCreateGC(display, rootwindow,
  105.                                     GCFunction|GCFont, &values);
  106.     }
  107.     for(i=0; i<NCOLORS; i++) {
  108.         XSetForeground(display, ctable[i].smallgc,
  109.                         ctable[i].pixelvalue);
  110.         XSetBackground(display, ctable[i].smallgc,
  111.                         ctable[CBLACK].pixelvalue);
  112.         XSetForeground(display, ctable[i].biggc,
  113.                         ctable[i].pixelvalue);
  114.         XSetBackground(display, ctable[i].biggc,
  115.                         ctable[CBLACK].pixelvalue);
  116.     }
  117. }
  118.  
  119. /*    set_up_colors creates the colors and stores them in the graphics
  120.     smallgcs. If the system is mono, all colors are white except CBLACK.
  121.     This isn't very elegant, but I use the colors for so many different
  122.     items that it would be impossible to set some of them to black and not
  123.     lose some important images. It's still playable.
  124. */
  125. set_up_colors()
  126. {
  127.     int                i;
  128.     XColor            tmpcolor;
  129.     unsigned long    pixel, planes[3];
  130.  
  131.     if(DisplayCells(display, screen) <= 2) {
  132.         for(i=0; i<NCOLORS; i++) {
  133.             if(i == CBLACK) ctable[i].pixelvalue = blackpixel;
  134.             else ctable[i].pixelvalue = whitepixel;
  135.         }
  136.     } else {
  137.         XAllocColorCells(display, colormap, False, planes, 3, &pixel, 1);
  138.         for(i=0; i<NCOLORS; i++) {
  139.             XParseColor(display, colormap, ctable[i].name, &tmpcolor);
  140.             switch(i) {
  141.                 case CBLACK:
  142.                     tmpcolor.pixel = pixel;
  143.                     break;
  144.                 case CRED:
  145.                     tmpcolor.pixel = pixel | planes[0];
  146.                     break;
  147.                 case CORANGE:
  148.                     tmpcolor.pixel = pixel | planes[1];
  149.                     break;
  150.                 case CYELLOW:
  151.                     tmpcolor.pixel = pixel | planes[1] | planes[0];
  152.                     break;
  153.                 case CBLUE:
  154.                     tmpcolor.pixel = pixel | planes[2];
  155.                     break;
  156.                 case CGREY:
  157.                     tmpcolor.pixel = pixel | planes[2] | planes[0];
  158.                     break;
  159.                 case CGREEN:
  160.                     tmpcolor.pixel = pixel | planes[2] | planes[1];
  161.                     break;
  162.                 case CWHITE:
  163.                     tmpcolor.pixel = pixel | planes[2] | planes[1] | planes[0];
  164.                     break;
  165.             }
  166.             XStoreColor(display, colormap, &tmpcolor);
  167.             ctable[i].pixelvalue = tmpcolor.pixel;
  168.         }
  169.     }
  170. }
  171.