home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
games
/
volume15
/
gtetris3
/
part01
/
tetris.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-27
|
12KB
|
398 lines
/*
# GENERIC X-WINDOW-BASED TETRIS
#
# tetris.c
#
###
#
# Copyright (C) 1992, 1993 Qiang Alex Zhao, azhao@cs.arizona.edu
# Computer Science Dept, University of Arizona
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of the author not be
# used in advertising or publicity pertaining to distribution of the
# software without specific, written prior permission.
#
# This program is distributed in the hope that it will be "playable",
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
*/
#include "tetris.h"
/*** variables ***/
char myDisplayName[256];
Display *display;
int screen_num;
Visual *visual;
Bool useColor = True;
Colormap colormap;
Window mainWin, blockWin;
Cursor theCursor;
XFontStruct *bigFont, *tinyFont;
unsigned long fg, bg;
XSizeHints sizehints = {
PMinSize | PMaxSize | PPosition | PSize | USSize,
0, 0, /* x, y */
0, 0, /* w, h */
0, 0, /* min w, min h */
0, 0, /* max w, max h */
0, 0, /* inc, not set */
0, 0, 0, 0 /* aspect ratio, not set */
};
XSizeHints iconsizehints = {
PPosition | PSize,
0, 0, /* x, y */
48, 48, /* w, h */
0, 0, /* min w, min h */
0, 0, /* max w, max h */
0, 0, /* inc, not set */
0, 0, 0, 0 /* aspect ratio, not set */
};
XWMHints wmhints = {
InputHint | StateHint | IconPixmapHint,
True, /* input mode */
NormalState, /* normal */
0, /* icon pixmap */
0, /* icon window */
0, 0, /* icon position */
0 /* icon mask pixmap - not used */
};
char myHome[FILENAMELEN];
int level = 3, prefilled = 0, score = 0, rows = 0;
Bool showNext = False, beep = False;
score_t myscore;
static int opTableEntries = 15;
static XrmOptionDescRec opTable[] = {
{"-s", ".scoresOnly", XrmoptionIsArg, (caddr_t) NULL},
{"-l", "*startLevel", XrmoptionSepArg, (caddr_t) NULL},
{"-p", "*preFilledLines", XrmoptionSepArg, (caddr_t) NULL},
{"-showNext", "*showNext", XrmoptionNoArg, (caddr_t) "on"},
{"-beep", "*beep", XrmoptionNoArg, (caddr_t) "on"},
{"-display", ".display", XrmoptionSepArg, (caddr_t) NULL},
{"-geometry", "*geometry", XrmoptionSepArg, (caddr_t) NULL},
{"-iconGeometry", "*iconGeometry", XrmoptionSepArg, (caddr_t) NULL},
{"-background", "*background", XrmoptionSepArg, (caddr_t) NULL},
{"-bg", "*background", XrmoptionSepArg, (caddr_t) NULL},
{"-foreground", "*foreground", XrmoptionSepArg, (caddr_t) NULL},
{"-fg", "*foreground", XrmoptionSepArg, (caddr_t) NULL},
{"-bigFont", "*bigFont", XrmoptionSepArg, (caddr_t) NULL},
{"-tinyFont", "*tinyFont", XrmoptionSepArg, (caddr_t) NULL},
{"-xrm", NULL, XrmoptionResArg, (caddr_t) NULL}
};
static XrmDatabase cmmDB = (XrmDatabase) NULL, rDB = (XrmDatabase) NULL;
static void parseOpenDisp();
static void Usage();
static void getDefaults();
/* ------------------------------------------------------------------ */
void
main(argc, argv)
int argc;
char *argv[];
{
(void) fprintf(stderr,
" GENERIC TETRIS V2.0.1\n");
(void) fprintf(stderr,
"Copyright (C) 1992-93 Q. Alex Zhao, azhao@cs.arizona.edu\n");
(void) fprintf(stderr,
" Computer Science Dept, University of Arizona\n\n");
(void) fprintf(stderr,
"GENERIC TETRIS comes with ABSOLUTELY NO WARRANTY.\n\n");
parseOpenDisp(&argc, argv);
getDefaults();
inits(argc, argv);
playing();
/* never come to here */
}
/* ------------------------------------------------------------------ */
static void
parseOpenDisp(argc, argv)
int *argc;
char *argv[];
{
struct passwd *pw;
XrmValue value;
char *str_type[20];
XVisualInfo vtmp, *vinfo;
int n = 1;
XrmInitialize();
myDisplayName[0] = '\0';
XrmParseCommand(&cmmDB, opTable, opTableEntries, argv[0], argc, argv);
/* check for any arguments left */
if (*argc != 1) {
Usage(argv[0]);
}
/* only print out the scores? */
if (XrmGetResource(cmmDB, "tetris.scoresOnly", "Tetris.ScoresOnly",
str_type, &value) == True) {
showScores(0);
exit(0);
}
/* get display now */
if (XrmGetResource(cmmDB, "tetris.display", "Tetris.Display",
str_type, &value) == True)
(void) strncpy(myDisplayName, value.addr, (int) value.size);
if (!(display = XOpenDisplay(myDisplayName))) {
(void) fprintf(stderr, "%s: Can't open display '%s'\n",
argv[0], XDisplayName(myDisplayName));
exit(1);
}
screen_num = DefaultScreen(display);
visual = DefaultVisual(display, screen_num);
colormap = DefaultColormap(display, screen_num);
vtmp.visualid = XVisualIDFromVisual(visual);
if ((vinfo = XGetVisualInfo(display, VisualIDMask, &vtmp, &n)) != NULL) {
if ((vinfo->class == GrayScale) || (vinfo->class == StaticGray)) {
useColor = False;
}
XFree((unsigned char *) vinfo);
} else {
useColor = False;
}
/* setup user information */
(void) gethostname(myscore.myhost, NAMELEN);
setpwent();
pw = getpwuid(getuid());
endpwent();
if (pw == NULL) { /* impossible? */
(void) sprintf(myscore.myname, "%d", getuid());
myHome[0] = '.';
myHome[1] = '/';
myHome[2] = '\0';
} else {
(void) strncpy(myscore.myname, pw->pw_name, NAMELEN);
(void) strncpy(myHome, pw->pw_dir, FILENAMELEN);
}
myscore.myname[NAMELEN - 1] = '\0';
myscore.myhost[NAMELEN - 1] = '\0';
myHome[FILENAMELEN - 1] = '\0';
}
/* ------------------------------------------------------------------ */
static void
Usage(argv0)
char *argv0;
{
(void) fprintf(stderr,
"Usage: %s [-s] [-l <starting level>] [-p <prefilled rows>]\n", argv0);
(void) fprintf(stderr,
" [-display <display>] [-geometry <geometry>] [-iconGeometry\n");
(void) fprintf(stderr,
" <icon geometry>] [-fg <foreground>] [-bg <background>]\n");
(void) fprintf(stderr,
" [-bigFont <font>] [-tinyFont <font>] [-showNext] [-beep]\n");
(void) fprintf(stderr,
" [-xrm <resource specifications>]\n");
exit(1);
}
/* ------------------------------------------------------------------ */
static void
getDefaults()
{
XrmDatabase homeDB, serverDB, appDB;
char filenamebuf[FILENAMELEN];
char *filename = &filenamebuf[0];
char *env;
char *classname = "Tetris";
char name[255], geoStr[20], icongeoStr[20];
XrmValue value;
char *str_type[20];
int x, y;
unsigned int w, h;
long flags;
(void) strcpy(name, "/usr/lib/X11/app-defaults/");
(void) strcat(name, classname);
/* Get application defaults file, if any */
appDB = XrmGetFileDatabase(name);
(void) XrmMergeDatabases(appDB, &rDB);
if (XResourceManagerString(display) != NULL) {
serverDB = XrmGetStringDatabase(XResourceManagerString(display));
} else {
(void) strcpy(filename, myHome);
(void) strcat(filename, "/.Xdefaults");
serverDB = XrmGetFileDatabase(filename);
}
XrmMergeDatabases(serverDB, &rDB);
if ((env = getenv("XENVIRONMENT")) == NULL) {
int len;
env = &filenamebuf[0];
(void) strcpy(env, myHome);
len = strlen(env);
(void) gethostname(env + len, FILENAMELEN - len);
}
homeDB = XrmGetFileDatabase(env);
XrmMergeDatabases(homeDB, &rDB);
XrmMergeDatabases(cmmDB, &rDB);
/* starting levels */
if (XrmGetResource(cmmDB, "tetris.startLevel", "Tetris.StartLevel",
str_type, &value) == True) {
if ((sscanf(value.addr, "%d", &level) <= 0) ||
(level < 0) || (level >= NUM_LEVELS)) {
(void) fprintf(stderr, "Tetris: Invalid level.\n");
exit(1);
}
}
/* prefilled lines */
if (XrmGetResource(cmmDB, "tetris.preFilledLines", "Tetris.PreFilledLines",
str_type, &value) == True) {
if ((sscanf(value.addr, "%d", &prefilled) <= 0) ||
(prefilled < 0) || (prefilled >= ROWS - THINGSIZE)) {
(void) fprintf(stderr, "Tetris: Invalid prefilled lines.\n");
exit(1);
}
}
/* show next */
if (XrmGetResource(cmmDB, "tetris.showNext", "Tetris.ShowNext",
str_type, &value) == True) {
showNext = True;
}
/* beep */
if (XrmGetResource(cmmDB, "tetris.beep", "Tetris.Beep",
str_type, &value) == True) {
beep = True;
}
/*** get foreground/background colors ***/
if (XrmGetResource(rDB, "tetris.foreground", "Tetris.Foreground",
str_type, &value) == True) {
(void) strncpy(name, value.addr, (int) value.size);
fg = getColor(name);
} else
fg = BlackPixel(display, screen_num);
if (XrmGetResource(rDB, "tetris.background", "Tetris.Background",
str_type, &value) == True) {
(void) strncpy(name, value.addr, (int) value.size);
bg = getColor(name);
} else
bg = WhitePixel(display, screen_num);
if (bg == fg) {
bg = WhitePixel(display, screen_num);
fg = BlackPixel(display, screen_num);
}
/*** get geometry info ***/
if (XrmGetResource(rDB, "tetris.geometry", "Tetris.Geometry",
str_type, &value) == True) {
(void) strncpy(geoStr, value.addr, (int) value.size);
} else {
geoStr[0] = '\0';
}
flags = XParseGeometry(geoStr, &x, &y, &w, &h);
if ((WidthValue | HeightValue) & flags)
Usage("tetris");
if (XValue & flags) {
if (XNegative & flags)
x = DisplayWidth(display, screen_num) + x - sizehints.width;
sizehints.x = x;
}
if (YValue & flags) {
if (YNegative & flags)
y = DisplayHeight(display, screen_num) + y - sizehints.height;
sizehints.y = y;
}
/*** get icon geometry info ***/
if (XrmGetResource(rDB, "tetris.iconGeometry", "Tetris.IconGeometry",
str_type, &value) == True) {
(void) strncpy(icongeoStr, value.addr, (int) value.size);
} else {
icongeoStr[0] = '\0';
}
flags = XParseGeometry(icongeoStr, &x, &y, &w, &h);
if ((WidthValue | HeightValue) & flags)
Usage("tetris");
if (XValue & flags) {
if (XNegative & flags)
x = DisplayWidth(display, screen_num) + x - iconsizehints.width;
wmhints.flags |= IconPositionHint;
wmhints.icon_x = x;
iconsizehints.x = x;
}
if (YValue & flags) {
if (YNegative & flags)
y = DisplayHeight(display, screen_num) + y - iconsizehints.height;
wmhints.flags |= IconPositionHint;
wmhints.icon_y = y;
iconsizehints.y = y;
}
/*** get fonts ***/
if (XrmGetResource(rDB, "tetris.bigFont", "tetris.BigFont",
str_type, &value) == True) {
(void) strncpy(name, value.addr, (int) value.size);
} else {
(void) strcpy(name, BIGFONT);
}
if ((bigFont = XLoadQueryFont(display, name)) == NULL) {
(void) fprintf(stderr, "Tetris: can't open font '%s'.\n", name);
exit(1);
}
if (XrmGetResource(rDB, "tetris.tinyFont", "tetris.TinyFont",
str_type, &value) == True) {
(void) strncpy(name, value.addr, (int) value.size);
} else {
(void) strcpy(name, TINYFONT);
}
if ((tinyFont = XLoadQueryFont(display, name)) == NULL) {
(void) fprintf(stderr, "Tetris: can't open font '%s'.\n", name);
exit(1);
}
/*
* clean up
*/
XrmDestroyDatabase(rDB);
}
/* ------------------------------------------------------------------ */