Skip to content

Instantly share code, notes, and snippets.

@NelsonMinar
Created May 25, 2013 20:45
Show Gist options
  • Save NelsonMinar/5650715 to your computer and use it in GitHub Desktop.
Save NelsonMinar/5650715 to your computer and use it in GitHub Desktop.
The oldest "fun" program of mine I could find, from November 1990. Sadly I've lost all my code from earlier, like the amazing 4 person "Snake" game I wrote in Apple ][ assembler when I was about 12 years old.
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#ifdef BIGBITMAPS
const unsigned tileWidth = 16;
const unsigned tileHeight = 16;
const char tile0Name[] = "./big0.bm";
const char tile1Name[] = "./big1.bm";
#else
const unsigned tileWidth = 8;
const unsigned tileHeight = 8;
const char tile0Name[] = "./small0.bm";
const char tile1Name[] = "./small1.bm";
#endif BIGBITMAPS
Display* disp;
int screen;
int rootWindow = 0;
void usage(char * progname);
int nextBit();
class PinchotWindow {
public:
PinchotWindow (char*); // geometry of window
void drawTile (GC, unsigned, unsigned); // which pixmap, row, and col
Window win;
unsigned winWidth, winHeight;
private:
unsigned long white, black;
Window useNewWindow(char *);
Window useRoot();
};
PinchotWindow::PinchotWindow(char* geom) {
win = rootWindow ? useRoot() : useNewWindow(geom);
}
Window
PinchotWindow::useNewWindow(char* geom) {
int winXLoc, winYLoc;
XParseGeometry(geom, &winXLoc, &winYLoc, &winWidth, &winHeight);
Window win = XCreateSimpleWindow(disp, DefaultRootWindow(disp),
winXLoc, winYLoc, winWidth * tileWidth,
winHeight * tileHeight, 0, 0, black);
XSetWindowAttributes windowAttrs;
windowAttrs.event_mask = ExposureMask;
XChangeWindowAttributes(disp, win, CWEventMask, &windowAttrs);
XMapWindow(disp, win);
XEvent xev;
XNextEvent(disp, &xev); // get the expose
if (xev.type != Expose)
fprintf (stderr, "Bad Event %d\n", xev.type), exit(1);
return win;
}
Window
PinchotWindow::useRoot() {
winWidth = DisplayWidth(disp, screen) / tileWidth;
winHeight = DisplayHeight(disp, screen) / tileHeight;
Window win = RootWindow(disp, screen);
XSetWindowAttributes windowAttrs;
windowAttrs.backing_store = Always;
windowAttrs.win_gravity = CenterGravity;
XChangeWindowAttributes(disp, win, CWBackingStore | CWWinGravity,
&windowAttrs);
return win;
}
void
PinchotWindow::drawTile(GC tile, unsigned row, unsigned col) {
XFillRectangle(disp, win, tile, col*tileWidth, row*tileHeight,
tileWidth, tileHeight);
}
void usage(char * progName) {
fprintf (stderr,"Usage: %s [-d displayname] [-g geometry]\n", progName);
exit(1);
}
#ifdef WHOLECHAR
int nextBit() {
static unsigned bitCount = 7;
static int lastRead;
if (bitCount == 7) { // read 7 bits
lastRead = getchar();
if (lastRead == EOF)
return EOF;
bitCount = 0;
}
return (lastRead >> bitCount++) & 0x01; // return the bitCounth bit
}
#else
int nextBit() {
int t;
return ((t = getchar()) == EOF ? EOF : t & 0x01);
}
#endif WHOLECHAR
int main(int argc, char** argv) {
char *serverName, *geometryString;
serverName = malloc(81);
geometryString = malloc(81);
strcpy(geometryString, "10x10");
serverName[0] = '\0';
for (int i = 1; i < argc; i++) {
char* arg = argv[i];
if (arg[0] == '-')
switch (arg[1]) {
case 'd': // new display name
if (++i >= argc)
usage(argv[0]);
serverName = argv[i];
continue;
case 'g': // new geometry
if (++i >= argc)
usage(argv[0]);
geometryString=argv[i];
continue;
case 'r': // root window
rootWindow = 1;
continue;
default:
usage(argv[0]);
}
else
usage(argv[0]);
}
disp = XOpenDisplay(serverName);
screen = XDefaultScreen(disp);
int black = BlackPixel(disp, DefaultScreen(disp));
int white = WhitePixel(disp, DefaultScreen(disp));
PinchotWindow mywindow(geometryString);
unsigned uKillMe;
int sKillMe;
Pixmap tile0Pixmap, tile1Pixmap;
XReadBitmapFile(disp, mywindow.win, tile0Name, &uKillMe, &uKillMe,
&tile0Pixmap, &sKillMe, &sKillMe);
XReadBitmapFile(disp, mywindow.win, tile1Name, &uKillMe, &uKillMe,
&tile1Pixmap, &sKillMe, &sKillMe);
XGCValues gcv;
gcv.foreground = white;
gcv.background = black;
gcv.graphics_exposures = False; // We don't want these.
gcv.fill_style = FillStippled;
gcv.stipple = tile0Pixmap;
GC tile0GC = XCreateGC(disp, mywindow.win, GCForeground | GCBackground |
GCGraphicsExposures | GCFillStyle | GCStipple, &gcv);
gcv.stipple = tile1Pixmap;
GC tile1GC = XCreateGC(disp, mywindow.win, GCForeground | GCBackground |
GCGraphicsExposures | GCFillStyle | GCStipple, &gcv);
unsigned bit = 0;
for (unsigned l = 0; l < mywindow.winHeight; l++) {
for (unsigned j = 0; j < mywindow.winWidth && ((bit = nextBit()) != EOF); j++)
mywindow.drawTile(bit ? tile0GC : tile1GC, l, j);
if (bit == EOF)
break;
}
XFlush(disp);
if (!rootWindow)
for (;;)
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment