Annotation of researchv9/X11/src/X.V11R1/demos/muncher/muncher.c, revision 1.1

1.1     ! root        1: /* 
        !             2:  * $Locker:  $ 
        !             3:  */ 
        !             4: static char    *rcsid = "$Header: muncher.c,v 1.3 87/09/08 08:08:17 swick Exp $";
        !             5: /******************************************************************************
        !             6:  * Description:
        !             7:  *     The famous munching squares.
        !             8:  *
        !             9:  * Brought to you by Jef Poskanzer.
        !            10:  *
        !            11:  * Copyright (C) 1987 by UniSoft Systems.  Permission to use, copy,
        !            12:  * modify, and distribute this software and its documentation for any
        !            13:  * purpose and without fee is hereby granted, provided that this copyright
        !            14:  * notice appear in all copies and in all supporting documentation.  No
        !            15:  * representation is made about the suitability of this software for any
        !            16:  * purpose.  It is provided "as is" without express or implied warranty.
        !            17:  *
        !            18:  * Arguments:
        !            19:  *     -r              display on root window instead of creating a new one
        !            20:  *     -s seed         use this for the seed
        !            21:  *     =wxh+x+y        X geometry for new window (default 256x256 centered)
        !            22:  *     host:display    X display on which to run
        !            23:  *****************************************************************************/
        !            24: 
        !            25: 
        !            26: #include <X11/Xlib.h>
        !            27: #include <X11/Xatom.h>
        !            28: #include <X11/Xutil.h>
        !            29: #include <stdio.h>
        !            30: #include <ctype.h>
        !            31: 
        !            32: 
        !            33: extern GC XCreateGC();
        !            34: extern long time();
        !            35: 
        !            36: #ifdef BSD
        !            37: extern long random();
        !            38: #else
        !            39: extern int rand();
        !            40: #endif
        !            41: 
        !            42: /* Some good seeds - if the user does not specify one, one of these gets
        !            43:    chosen randomly. */
        !            44: int seeds[] =
        !            45:        {
        !            46:        0x0001, 0x0002, 0x0101, 0x0666, 0x1111, 0x1212, 0x1249, 0x2222,
        !            47:        0x3333, 0x4001, 0x4444, 0x5252, 0x5555, 0x6666, 0x8001, 0x8010
        !            48:        };
        !            49: 
        !            50: 
        !            51: main(argc, argv)
        !            52: int argc;
        !            53: char **argv;
        !            54:        {
        !            55:        char **ap;
        !            56:        char *display = NULL;
        !            57:        char *geom = NULL;
        !            58:        int useRoot = 0;
        !            59:        int seed = 0;
        !            60:        char *usage = "usage: %s [=WIDxHGT+XOFF+YOFF] [-r] [-s SEED]\n";
        !            61: 
        !            62:        Window win;
        !            63:        int winX, winY, winW, winH;
        !            64:        XSetWindowAttributes xswa;
        !            65:        Display *dpy;
        !            66:        Screen *scr;
        !            67:        GC gc;
        !            68:        XEvent xev;
        !            69: 
        !            70: #define BATCHSIZE 400
        !            71:        XPoint points[BATCHSIZE];
        !            72:        int size, n, nmask;
        !            73:        register int acc, i, x, y;
        !            74:        int xoffset, yoffset;
        !            75: 
        !            76:        /* Process arguments: */
        !            77:        ap = argv;
        !            78:        while (*++ap)
        !            79:                {
        !            80:                if (**ap == '=') 
        !            81:                        geom = *ap;
        !            82:                else if (index(*ap, ':'))
        !            83:                        display = *ap;
        !            84:                else if (!strcmp(*ap, "-r"))
        !            85:                        useRoot = 1;
        !            86:                else if (!strcmp(*ap, "-s"))
        !            87:                        {
        !            88:                        if ( *++ap )
        !            89:                                {
        !            90:                                if ( sscanf( *ap, "%x", &seed ) != 1 )
        !            91:                                        if ( sscanf( *ap, "0x%x", &seed ) != 1 )
        !            92:                                                {
        !            93:                                                fprintf( stderr, usage, *argv );
        !            94:                                                exit( 1 );
        !            95:                                                }
        !            96:                                }
        !            97:                        else
        !            98:                                {
        !            99:                                fprintf( stderr, usage, *argv );
        !           100:                                exit( 1 );
        !           101:                                }
        !           102:                        }
        !           103:                else
        !           104:                        {
        !           105:                        fprintf( stderr, usage, *argv );
        !           106:                        exit( 1 );
        !           107:                        }
        !           108:                }
        !           109: 
        !           110:        if (!(dpy= XOpenDisplay(display)))
        !           111:                {
        !           112:                perror("Cannot open display\n");
        !           113:                exit(-1);
        !           114:                }
        !           115: 
        !           116:        scr = DefaultScreenOfDisplay(dpy);
        !           117: 
        !           118:        /* Set up window parameters, create and map window if necessary: */
        !           119:        if (useRoot)
        !           120:                {
        !           121:                win = DefaultRootWindow(dpy);
        !           122:                winX = 0;
        !           123:                winY = 0;
        !           124:                winW = DisplayWidth(dpy, DefaultScreen(dpy));
        !           125:                winH = DisplayHeight(dpy, DefaultScreen(dpy));
        !           126:                }
        !           127:        else
        !           128:                {
        !           129:                winW = 256;
        !           130:                winH = 256;
        !           131:                winX = (WidthOfScreen(scr) - winW) >> 1;
        !           132:                winY = (HeightOfScreen(scr) - winH) >> 1;
        !           133:                if (geom) 
        !           134:                        XParseGeometry(geom, &winX, &winY, &winW, &winH);
        !           135: 
        !           136:                xswa.event_mask = 0;
        !           137:                xswa.background_pixel = BlackPixelOfScreen(scr);
        !           138:                win = XCreateWindow(dpy, RootWindowOfScreen(scr),
        !           139:                    winX, winY, winW, winH, 0, 
        !           140:                    DefaultDepthOfScreen(scr), InputOutput,
        !           141:                    DefaultVisualOfScreen(scr),
        !           142:                    CWEventMask | CWBackPixel, &xswa);
        !           143:                XChangeProperty(dpy, win, XA_WM_NAME, XA_STRING, 8, 
        !           144:                                PropModeReplace, "Muncher", 3);
        !           145:                XMapWindow(dpy, win);
        !           146:                }
        !           147: 
        !           148:        /* Set up a graphics context: */
        !           149:        gc = XCreateGC(dpy, win, 0, NULL);
        !           150:        XSetForeground(dpy, gc, WhitePixelOfScreen(scr));
        !           151:        XSetFunction(dpy, gc, GXinvert);
        !           152:        /* XSetFunction(dpy, gc, GXcopy); */
        !           153: 
        !           154:        /* Initialize munch algorithm. */
        !           155:        size = ( winW < winH ? winW : winH );
        !           156:        if ( size <= 0 ) size = 1;
        !           157:        for ( n = 30, nmask = 0x40000000; n >= 0; n--, nmask >>= 1 )
        !           158:                if ( size & nmask )
        !           159:                        break;
        !           160:        size = 1 << n;
        !           161:        nmask = size - 1;
        !           162:        xoffset = ( winW - size ) / 2;
        !           163:        yoffset = ( winH - size ) / 2;
        !           164:        if ( seed == 0 )
        !           165:                {
        !           166: #ifdef BSD
        !           167:                srandom((int) time(0) % 231);
        !           168:                seed = seeds[random() % (sizeof(seeds)/sizeof(seeds[0]) )];
        !           169: #else
        !           170:                srand((int) time(0) % 231);
        !           171:                seed = seeds[rand() % (sizeof(seeds)/sizeof(seeds[0]) )];
        !           172: #endif
        !           173:                }
        !           174:        printf( "size = %d, seed = 0x%x\n", size, seed );
        !           175:        acc = 0;
        !           176: 
        !           177:        /* Loop forever computing and drawing batches of points. */
        !           178:        for (;;)
        !           179:                {
        !           180:                if (XPending(dpy))
        !           181:                        XNextEvent(dpy, &xev);
        !           182:                
        !           183:                for ( i=0; i < BATCHSIZE; i++ )
        !           184:                        {
        !           185:                        x = acc & nmask;
        !           186:                        y = ( ( acc >> n ) & nmask ) ^ x;
        !           187: 
        !           188:                        points[i].x = x + xoffset;
        !           189:                        points[i].y = y + yoffset;
        !           190: 
        !           191:                        acc += seed;
        !           192:                        }
        !           193: 
        !           194:                XDrawPoints(dpy, win, gc, points, BATCHSIZE, CoordModeOrigin);
        !           195:                XSync(dpy, 0);
        !           196:                }
        !           197:        }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.