#include #include #include #include #include #include /* include ufo2_xpm */ #include "ufo2.xpm" #define WIN_W 512 #define WIN_H 512 #define WIN_X 100 #define WIN_Y 100 #define BORDER 4 #ifndef M_PI #define M_PI 3.14159265358979 #endif #define deg2rad(x) (x * M_PI / 180.0) typedef struct _Pixmaps { Pixmap pix; Pixmap mask; } Pixmaps; static void update(Display *dpy, Drawable win, GC gc, double ang, Pixmaps pixs); /* get a color value from a color name */ static unsigned long get_color(Display *dpy, char *colorname) { Colormap cmap; XColor near_color, true_color; cmap = DefaultColormap(dpy, DefaultScreen(dpy)); XAllocNamedColor(dpy, cmap, colorname, &near_color, &true_color); return near_color.pixel; } int main(void) { Display *dpy; Window root; Window win; int screen; XEvent evt; unsigned long black, white; GC gc; Pixmaps pixs; int busy_loop; double ang; /* open display */ dpy = XOpenDisplay(NULL); root = DefaultRootWindow(dpy); screen = DefaultScreen(dpy); /* get color black and white */ white = WhitePixel(dpy, screen); black = BlackPixel(dpy, screen); /* create window */ win = XCreateSimpleWindow(dpy, root, WIN_X, WIN_Y, WIN_W, WIN_H, BORDER, black, white); XSelectInput(dpy, win, KeyPressMask | ExposureMask); XMapWindow(dpy, win); /* wait display window */ do { XNextEvent(dpy, &evt); } while (evt.type != Expose); XMoveWindow(dpy, win, WIN_X, WIN_Y); /* create graphics context */ gc = XCreateGC(dpy, win, 0, 0); XSetGraphicsExposures(dpy, gc, False); /* load image */ if (XpmCreatePixmapFromData(dpy, win, ufo2_xpm, &pixs.pix, &pixs.mask, NULL)) { fprintf(stderr, "Error not load xpm."); return 1; } ang = 0; busy_loop = 1; while (busy_loop) { /* event */ while (XPending(dpy)) { XNextEvent(dpy, &evt); switch (evt.type) { /* case Expose: if (evt.xexpose.count == 0) { } break; */ case KeyPress: busy_loop = 0; break; default: break; } } update(dpy, win, gc, ang, pixs); ang += 1.0; if (ang >= 360) ang -= 360; XFlush(dpy); usleep(16 * 1000); /* wait */ } XFreePixmap(dpy, pixs.pix); XFreePixmap(dpy, pixs.mask); XFreeGC(dpy, gc); XDestroyWindow(dpy, win); XCloseDisplay(dpy); return 0; } /* update and draw */ static void update(Display *dpy, Drawable win, GC gc, double ang, Pixmaps pixs) { XWindowAttributes xgwa; int scrw, scrh; int r, x, y, w, h; int r2, x2, y2, w2, h2; /* get window attributes */ XGetWindowAttributes(dpy, win, &xgwa); scrw = xgwa.width; /* window width */ scrh = xgwa.height; /* window height */ w = 96; h = 96; r = 200; x = r * cos(deg2rad(ang)) + (scrw / 2) - (w / 2); y = r * sin(deg2rad(ang)) + (scrh / 2) - (h / 2); w2 = 128; h2 = 128; r2 = 128; x2 = r2 * cos(deg2rad(-ang)) + (scrw / 2) - (w2 / 2); y2 = r2 * sin(deg2rad(-ang)) + (scrh / 2) - (h2 / 2); /* clear window */ XClearWindow(dpy, win); /* draw shape */ XSetForeground(dpy, gc, get_color(dpy, "blue")); XFillRectangle(dpy, win, gc, x - 8, y - 8, w + 16, h + 16); XSetForeground(dpy, gc, get_color(dpy, "cyan")); XFillArc(dpy, win, gc, x, y, w, h, 0, 360 * 64); /* draw image (Pixmap) */ XSetClipMask(dpy, gc, pixs.mask); /* set clip mask */ XSetClipOrigin(dpy, gc, x2, y2); /* set clip origin */ XCopyArea(dpy, pixs.pix, win, gc, 0, 0, w2, h2, x2, y2); /* copy Pixmap */ XSetClipMask(dpy, gc, None); /* reset clip */ }