home *** CD-ROM | disk | FTP | other *** search
- Path: uunet!ogicse!zephyr.ens.tek.com!master!saab!billr
- From: billr@saab.CNA.TEK.COM (Bill Randle)
- Newsgroups: comp.sources.games
- Subject: v15i002: gtetris - Generic Tetris for X11, Part02/02
- Message-ID: <3646@master.CNA.TEK.COM>
- Date: 23 Sep 92 19:13:02 GMT
- Article-I.D.: master.3646
- Sender: news@master.CNA.TEK.COM
- Lines: 394
- Approved: billr@saab.CNA.TEK.COM
-
- Submitted-by: "Qiang Alex Zhao" <azhao@cs.arizona.edu>
- Posting-number: Volume 15, Issue 2
- Archive-name: gtetris/Part02
- Environment: X11R4/5, Xlib
-
-
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of archive 2 (of 2)."
- # Contents: Makefile die.c tscores.c
- # Wrapped by billr@saab on Wed Sep 23 12:10:55 1992
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'Makefile' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'Makefile'\"
- else
- echo shar: Extracting \"'Makefile'\" \(2093 characters\)
- sed "s/^X//" >'Makefile' <<'END_OF_FILE'
- X# GENERIC X-WINDOW-BASED TETRIS
- X#
- X# Makefile
- X#
- X###
- X#
- X# Copyright (C) 1992 Qiang Alex Zhao
- X# Computer Science Dept, University of Arizona
- X# azhao@cs.arizona.edu
- X#
- X# All Rights Reserved
- X#
- X# Permission to use, copy, modify, and distribute this software and
- X# its documentation for any purpose and without fee is hereby granted,
- X# provided that the above copyright notice appear in all copies and
- X# that both that copyright notice and this permission notice appear in
- X# supporting documentation, and that the name of the author not be
- X# used in advertising or publicity pertaining to distribution of the
- X# software without specific, written prior permission.
- X#
- X# This program is distributed in the hope that it will be "playable",
- X# but WITHOUT ANY WARRANTY; without even the implied warranty of
- X# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- X#
- X
- X
- X### Configurable Settings
- X
- X# destination directory
- XDEST = /r/cas/usr/azhao/pub
- X
- X# score file
- XSCOREFILE = $(DEST)/.tetris.scores
- X
- X# path to X header files
- XXINCLUDE = /usr/include/X11
- X
- X# path to X library files
- XXLIBS = /usr/lib/X11
- X
- X# the C compiler, used as linker ;o)
- XCC = cc
- X
- X# optimization flages
- XCFLAGS = -O -I$(XINCLUDE) -DSCOREFILE='"$(SCOREFILE)"'
- XLDFLAGS = -O -L$(XLIBS)
- X
- X### Fixed settings
- X
- XMAKEFILE = Makefile
- X
- XPROGT = tetris
- XPROGS = tscores
- X
- XHDRS = tetris.h \
- X data.h
- X
- XLDLIBS = -lX11 -lm
- X
- XSRCS = tetris.c \
- X die.c \
- X tscores.c
- X
- XOBJT = tetris.o \
- X die.o
- X
- XOBJS = tscores.o
- X
- XPRINT = enscript -2rG
- X
- X
- X### Dependencies/Actions
- X
- Xall: $(PROGT) $(PROGS)
- X
- X$(PROGT): $(OBJT)
- X @echo -n "Linking $(PROGT) ... "
- X @$(CC) $(LDFLAGS) $(OBJT) $(LDLIBS) -o $(PROGT)
- X @echo "Done."
- X
- X$(PROGS): $(OBJS)
- X @echo -n "Linking $(PROGS) ... "
- X @$(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(PROGS)
- X @echo "Done."
- X
- Xclean:
- X rm -rf *.o $(PROGT) $(PROGS)
- X
- Xinstall: $(PROGT) $(PROGS)
- X @echo Installing $(PROGT) and $(PROGS) in $(DEST)
- X install -s $(PROGT) $(PROGS) $(DEST)
- X
- Xprint:
- X $(PRINT) $(HDRS) $(SRCS)
- X
- X### dependencies for each object
- Xtetris.o: tetris.c tetris.h data.h
- Xdie.o: die.c tetris.h
- Xtscore.o: tscore.c tetris.h
- X
- END_OF_FILE
- if test 2093 -ne `wc -c <'Makefile'`; then
- echo shar: \"'Makefile'\" unpacked with wrong size!
- fi
- # end of 'Makefile'
- fi
- if test -f 'die.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'die.c'\"
- else
- echo shar: Extracting \"'die.c'\" \(4448 characters\)
- sed "s/^X//" >'die.c' <<'END_OF_FILE'
- X/*
- X# GENERIC X-WINDOW-BASED TETRIS
- X#
- X# die.c
- X#
- X###
- X#
- X# Copyright (C) 1992 Qiang Alex Zhao
- X# Computer Science Dept, University of Arizona
- X# azhao@cs.arizona.edu
- X#
- X# All Rights Reserved
- X#
- X# Permission to use, copy, modify, and distribute this software and
- X# its documentation for any purpose and without fee is hereby granted,
- X# provided that the above copyright notice appear in all copies and
- X# that both that copyright notice and this permission notice appear in
- X# supporting documentation, and that the name of the author not be
- X# used in advertising or publicity pertaining to distribution of the
- X# software without specific, written prior permission.
- X#
- X# This program is distributed in the hope that it will be "playable",
- X# but WITHOUT ANY WARRANTY; without even the implied warranty of
- X# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- X#
- X*/
- X
- X#include "tetris.h"
- X
- Xvoid
- Xbanner(field, str)
- X field_t *field;
- X char *str;
- X{
- X int mlen = strlen(str);
- X int x, y, w, ascent, descent;
- X
- X ascent = field->tfont->ascent;
- X descent = field->tfont->descent;
- X w = XTextWidth(field->tfont, str, mlen);
- X x = (field->winwidth - w) / 2;
- X y = (field->winheight - ascent - descent) / 2;
- X
- X XFillRectangle(disp, field->win, gc_w2, x - 5, y - ascent - 5,
- X w + 10, ascent + descent + 10);
- X XDrawString(disp, field->win, gc_ttx, x, y, str, mlen);
- X}
- X
- Xvoid
- Xdie(field)
- X field_t *field;
- X{
- X time_t tloc;
- X score_t myscore;
- X struct passwd *tmp;
- X
- X time(&tloc);
- X
- X banner(field, DIE_MESG);
- X XFlush(disp);
- X sleep(1);
- X
- X gethostname(myscore.myhost, NAMELEN);
- X myscore.myhost[NAMELEN-1] = '\0';
- X setpwent();
- X tmp = getpwuid(getuid());
- X if (tmp == NULL) /* should not occur */
- X sprintf(myscore.myname, "%d", getuid());
- X else
- X strncpy(myscore.myname, tmp->pw_name, NAMELEN);
- X endpwent();
- X myscore.myname[NAMELEN-1] = '\0';
- X
- X sprintf(myscore.score, "%9d", field->score);
- X sprintf(myscore.level, "%3d", field->level);
- X sprintf(myscore.lines, "%4d", field->lines);
- X strcpy(myscore.mytime, asctime(localtime(&tloc)));
- X fprintf(stderr, "\t%s", myscore.mytime);
- X fprintf(stderr, "Your final score is %d, at level %d with %d raws.\n\n",
- X field->score, field->level, field->lines);
- X
- X addHighScore(&myscore, field->score);
- X showHighScores(SHOWNSCORES);
- X
- X XCloseDisplay(disp);
- X exit(0);
- X}
- X
- Xvoid
- XaddHighScore(myscore, snum)
- X score_t *myscore;
- X int snum;
- X{
- X char buff[2][SCORELEN];
- X int fd;
- X int tmp, ptmp, s1, s2;
- X int mycount = 0;
- X Bool saved = False, trickle = False;
- X
- X fd = open(SCOREFILE, O_CREAT | O_RDWR, 0644);
- X if (fd == -1) {
- X fprintf(stderr, "Cannot write the score-file!\n");
- X return;
- X }
- X flock(fd, LOCK_EX);
- X
- X tmp = 0;
- X ptmp = 1;
- X bcopy((char *) myscore, buff[1], SCORELEN);
- X
- X while (read(fd, buff[tmp], SCORELEN) == SCORELEN) {
- X sscanf(((score_t *)buff[tmp])->score, " %d", &s1);
- X if (!saved && (s1 <= snum)) {
- X trickle = True;
- X saved = True;
- X mycount++;
- X }
- X if (!strncmp(myscore->myname,
- X ((score_t *)buff[tmp])->myname, NAMELEN)) {
- X mycount++;
- X }
- X /* Then check if we should trickle the score */
- X if (trickle) {
- X lseek(fd, (off_t) -SCORELEN, L_INCR);
- X write(fd, buff[ptmp], SCORELEN);
- X ptmp = tmp;
- X tmp = (tmp + 1) % 2;
- X }
- X /*
- X * As we trickle, we add up records owned by me. Once we hit max,
- X * we throw it away, and stop trickling.
- X */
- X if ((mycount > MAXSCORES) || ((mycount == MAXSCORES) && !trickle)) {
- X trickle = False;
- X break;
- X }
- X } /* while */
- X
- X if (trickle) {
- X write(fd, buff[ptmp], SCORELEN);
- X }
- X if (!saved && (mycount < MAXSCORES)) {
- X write(fd, (char *) myscore, SCORELEN);
- X }
- X
- X flock(fd, LOCK_UN);
- X close(fd);
- X}
- X
- Xvoid
- XshowHighScores(ub)
- X int ub;
- X{
- X int fd, i;
- X score_t curs;
- X
- X fd = open(SCOREFILE, O_RDONLY, 0644);
- X if (fd == -1)
- X return;
- X
- X i = 0;
- X fprintf(stderr, " # USER SCORE L R HOST DATE\n");
- X
- X while (read(fd, (char *) &curs, SCORELEN) == SCORELEN) {
- X i++;
- X if (i<= ub)
- X fprintf(stderr, "%2d %-12s%9s %3s %4s %-12s %-s",
- X i, curs.myname, curs.score, curs.level, curs.lines,
- X curs.myhost, curs.mytime);
- X }
- X close(fd);
- X fprintf(stderr, "There are %d scores to date\n", i);
- X}
- X
- END_OF_FILE
- if test 4448 -ne `wc -c <'die.c'`; then
- echo shar: \"'die.c'\" unpacked with wrong size!
- fi
- # end of 'die.c'
- fi
- if test -f 'tscores.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'tscores.c'\"
- else
- echo shar: Extracting \"'tscores.c'\" \(1474 characters\)
- sed "s/^X//" >'tscores.c' <<'END_OF_FILE'
- X/*
- X# GENERIC X-WINDOW-BASED TETRIS
- X#
- X# tscores.c
- X#
- X###
- X#
- X# Copyright (C) 1992 Qiang Alex Zhao
- X# Computer Science Dept, University of Arizona
- X# azhao@cs.arizona.edu
- X#
- X# All Rights Reserved
- X#
- X# Permission to use, copy, modify, and distribute this software and
- X# its documentation for any purpose and without fee is hereby granted,
- X# provided that the above copyright notice appear in all copies and
- X# that both that copyright notice and this permission notice appear in
- X# supporting documentation, and that the name of the author not be
- X# used in advertising or publicity pertaining to distribution of the
- X# software without specific, written prior permission.
- X#
- X# This program is distributed in the hope that it will be "playable",
- X# but WITHOUT ANY WARRANTY; without even the implied warranty of
- X# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- X#
- X*/
- X
- X#include "tetris.h"
- X
- Xvoid
- Xmain()
- X{
- X int fd, i;
- X score_t curs;
- X
- X fprintf(stderr, "\tGENERIC TETRIS HALL OF FAME\n\n");
- X fd = open(SCOREFILE, O_RDONLY, 0644);
- X if (fd == -1)
- X return;
- X
- X i = 0;
- X fprintf(stderr, " # USER SCORE L R HOST DATE\n");
- X
- X while (read(fd, (char *) &curs, SCORELEN) == SCORELEN) {
- X i++;
- X fprintf(stderr, "%4d %-12s%9s %3s %4s %-12s %-s", i,
- X curs.myname, curs.score, curs.level, curs.lines,
- X curs.myhost, curs.mytime);
- X }
- X close(fd);
- X fprintf(stderr, "There are %d scores to date\n", i);
- X}
- X
- END_OF_FILE
- if test 1474 -ne `wc -c <'tscores.c'`; then
- echo shar: \"'tscores.c'\" unpacked with wrong size!
- fi
- # end of 'tscores.c'
- fi
- echo shar: End of archive 2 \(of 2\).
- cp /dev/null ark2isdone
- MISSING=""
- for I in 1 2 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have unpacked both archives.
- rm -f ark[1-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
-