// xrubik started Nov 3, 2005 // basic version 1.0 completed Nov 4, 2005 // // Thanks to Ch. Tronche for getting me started with X11 // Programmed by Mark Longridge #include #include // Every Xlib program must include this #include #include #include // I include this to test return values the lazy way #include // So we got the profile for 10 seconds #include #define NIL (0) // A name for the void pointer #define ScreenSizeX 400 #define ScreenSizeY 240 #define D 3 /* Cube is size '3' */ #define SIDES 6 #define SIZE_X 15 #define SIZE_Y 16 #define SEP_X 15 #define SEP_Y 16 #define TOP 0 #define DOWN 5 #define FRONT 2 #define BACK 4 #define LEFT 1 #define RIGHT 3 short d=3; short q[SIDES][D][D], q1[SIDES][D][D]; short s1, s2, s3, s4; short ss[SIDES]; /* Pointers for cube placement */ short tt[SIDES]; short posx[SIDES][D]; short posy[SIDES][D]; short size_x, size_y, sep_x, sep_y; Display *dpy; Window w; GC gc; KeySym ks; Pixmap m; int xp, yp; int flag; int a; int ax, ay, az; int whiteColor, blackColor, thescreen; double x,y,t,s; double x2, y2; char text[255]; /* a char buffer for KeyPress Events */ XColor col[10]; XColor white, red, yellow, orange, green, blue, grey; XColor purple, brown, pink, cyan, beige, magenta; XColor darkgreen, darkred, darkblue, darkgrey; XColor violet, gold, lightgreen, turquoise; Colormap thecolormap; void drawit(int l); void top(); void down(); void left(); void right(); void front(); void back(); void refresh_image(); void initial_sides(); void rotate_face(short z); void calc_cube(); void initial_pts(); void reset_size(); void initial_pts() { ss[0] =1; tt[0] =0; ss[1] =0; tt[1] =1; ss[2] =1; tt[2] =1; ss[3] =2; tt[3] =1; ss[4] =3; tt[4] =1; ss[5] =1; tt[5] =2; } void reset_size() { initial_sides (); refresh_image (); } void top() { short y,z; z = TOP; rotate_face(z); for (y=0; y<=d-1; y++) { q[LEFT][0][y] = q1[FRONT][0][y]; q[BACK][0][y] = q1[LEFT][0][y]; q[RIGHT][0][y]= q1[BACK][0][y]; q[FRONT][0][y]= q1[RIGHT][0][y]; } refresh_image(); } void down() { short y,z; z = DOWN; rotate_face (z); for (y=0; y<=d-1; y++) { q[LEFT][d-1][y] = q1[BACK][d-1][y]; q[BACK][d-1][y] = q1[RIGHT][d-1][y]; q[RIGHT][d-1][y]= q1[FRONT][d-1][y]; q[FRONT][d-1][y]= q1[LEFT][d-1][y]; } refresh_image(); } void front() { short x,z; z = FRONT; rotate_face (z); for (x=0; x<=d-1; x++) { z = 2-x; q[LEFT][z][d-1] = q1[DOWN][0][z]; q[TOP][d-1][x] = q1[LEFT][z][d-1]; q[RIGHT][x][0] = q1[TOP][d-1][x]; q[DOWN][0][z] = q1[RIGHT][x][0]; } refresh_image(); } void back() { short x,z; z = BACK; rotate_face (z); for (x=0; x<=d-1; x++) { z = 2-x; q[LEFT][x][0] = q1[TOP][0][z]; q[TOP][0][x] = q1[RIGHT][x][2]; q[RIGHT][x][2]= q1[DOWN][d-1][z]; q[DOWN][d-1][x]=q1[LEFT][x][0]; } refresh_image(); } void left () { short x,z; z = LEFT; rotate_face (z); for (x=0; x<=d-1; x++) { z = 2-x; q[TOP][x][0] = q1[BACK][z][d-1]; q[FRONT][x][0] = q1[TOP][x][0]; q[DOWN][x][0] = q1[FRONT][x][0]; q[BACK][x][d-1] =q1[DOWN][z][0]; } refresh_image(); } void right () { short x,z; z = RIGHT; rotate_face (z); for (x=0; x<=d-1; x++) { z= 2-x; q[TOP][x][d-1] = q1[FRONT][x][d-1]; q[FRONT][x][d-1] = q1[DOWN][x][d-1]; q[DOWN][x][d-1] = q1[BACK][z][0]; q[BACK][x][0] = q1[TOP][z][2]; } refresh_image(); } void refresh_image() { short x,y,z; for (z=0; z<=SIDES-1; z++) for (x=0; x<=d-1; x++) for (y=0; y<=d-1; y++) q1[z][x][y] = q[z][x][y]; } void initial_sides() { short x,y,z; for (z=0; z<=SIDES-1; z++) for (x=0; x<=d-1; x++) for (y=0; y<=d-1; y++) q[z][x][y] = z; } void calc_cube() { short x,y; short s,t; short offset_x, offset_y; short c; c=0; offset_y=0; offset_x=20; for (s=0; s<=2; s++) { for (t=0; t<=1; t++) { for (x=0; x<= d-1; x++) { posy[c][x] = size_y*x+(tt[c]*(sep_x+(size_y*(d-2))))+offset_y; for (y=0; y<= d-1; y++) { posx[c][y] = size_x*y+(ss[c]*(sep_y+(size_x*(d-2))))+offset_x; } } c++; } } } void rotate_face (short z) { short x,y; for (x=0; x<=d-1; x++) for (y=0; y<=d-1; y++) { s1 = y; s2 = x; s3 = 2-x; s4 = y; q[z][s1][s2] = q1[z][s3][s4]; } } void drawit(int l) { short x,y,z,c; int tempx, tempy; int rc; unsigned short s,t; unsigned int sizex, sizey; unsigned int offset_x, offset_y; c=0; offset_x = 10; offset_y = 20; sizex=30; sizey=20; for (s=0; s<=2; s++) { for (t=0; t<=1; t++) { for (x=0; x<=d-1; x++) { for (y=0; y<=d-1; y++) { XSetForeground (dpy, gc, col[q[c][x][y]].pixel); tempx = sizex*y+(ss[c]*(65+(sizex*(d-2))))+offset_x; tempy = sizey*x+(tt[c]*(43+(sizey*(d-2))))+offset_y; rc = XFillRectangle (dpy, m, gc, tempx, tempy, sizex, sizey); } } c++; } } XCopyArea (dpy, m, w, gc, 0, 0, ScreenSizeX, ScreenSizeY, 0, 0); XFlush(dpy); } void init_x() { // Open the display dpy = XOpenDisplay(NIL); assert(dpy); // Get some colors blackColor = BlackPixel(dpy, DefaultScreen(dpy)); whiteColor = WhitePixel(dpy, DefaultScreen(dpy)); // Create the window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, ScreenSizeX, ScreenSizeY, 0, blackColor, blackColor); XSetStandardProperties(dpy,w,"xrubik version 1.0","Hi",None,NULL,0,NULL); // We want to get MapNotify events XSelectInput(dpy, w, StructureNotifyMask | ExposureMask | KeyPressMask); // "Map" the window (that is, make it appear on the screen) XClearWindow (dpy, w); XMapWindow(dpy, w); // Create a "Graphics Context" gc = XCreateGC(dpy, w, 0, NIL); thescreen = DefaultScreen(dpy); thecolormap = DefaultColormap(dpy,thescreen); m = XCreatePixmap (dpy, w, ScreenSizeX, ScreenSizeY, DefaultDepth(dpy, thescreen)); // Tell the GC we draw using the white color XSetForeground(dpy, gc, whiteColor); XAllocNamedColor(dpy, thecolormap, "white", &white, &col[0]); XAllocNamedColor(dpy, thecolormap, "red", &red, &col[1]); XAllocNamedColor(dpy, thecolormap, "yellow", &yellow, &col[2]); XAllocNamedColor(dpy, thecolormap, "orange", &orange, &col[3]); XAllocNamedColor(dpy, thecolormap, "green", &green, &col[4]); XAllocNamedColor(dpy, thecolormap, "blue", &blue, &col[5]); XAllocNamedColor(dpy, thecolormap, "grey", &grey, &col[6]); XSetForeground (dpy, gc, grey.pixel); XFillRectangle (dpy, m, gc, 0, 0, ScreenSizeX, ScreenSizeY); } main() { /* Set up the attributes of the window */ init_x(); size_x = SIZE_X; size_y = SIZE_Y; sep_x = SEP_X; sep_y = SEP_Y; initial_pts(); initial_sides (); refresh_image (); calc_cube(); flag = 0; /* Wait for the MapNotify event */ while (flag == 0) { XEvent e; XNextEvent(dpy, &e); switch (e.type) { case Expose: XSetForeground(dpy, gc, whiteColor); drawit(1); break; case MapNotify: break; case KeyPress: ks = XKeycodeToKeysym (dpy, e.xkey.keycode, 0); switch (ks) { case XK_t: top(); drawit(1); break; case XK_u: top(); drawit(1); break; case XK_r: right(); drawit(1); break; case XK_l: left(); drawit(1); break; case XK_f: front(); drawit(1); break; case XK_b: back(); drawit(1); break; case XK_d: down(); drawit(1); break; case XK_z: reset_size(); drawit(1); break; case XK_q: flag=1; printf ("\n"); break; } } } XDestroyWindow (dpy, w); XFreePixmap (dpy, m); XCloseDisplay(dpy); }