
/*
 * Copyright (c) 1991    Jon Wesener 
 * Gaming Software
 *
 * 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.  No representations are made about the
 * suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 *
 */

#ident	"@(#)bif.c	1.4 :/usr/key/jojow/X/src/guts/SCCS/s.bif.c 3/19/91 10:15:20 "

/* bif.c - check and update number of players of tank */

#include <stdio.h>
#include <rpc/rpc.h>
#include "tankmsg.h"

#include <signal.h>
#include <string.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "bif.h"

CLIENT *cl;
char *server;
char msgbuf[256];

#ifndef NULL
#define NULL        0L
#endif

#define MASK	0x40

#define SECOND		1000000L
#define MINUTE		(60 * SECOND)
#define DELAYCNT	(1 * MINUTE)


Display *display;
Window  root, window;
int     screen, depth, black, white;
GC      copyGC;
KeySym  last_key;
int     ascent, descent;
Font    font;

long    frame_delay = 30000L;

int		plyrcnt = 0;

main(ac, av)
	int     ac;
	char   *av[];

{
	XEvent  event;
	unsigned long event_mask;
	int     dummy;
	XCharStruct chars;
	char *getenv();


	/* open the display */
	display = XOpenDisplay(NULL);
	screen = DefaultScreen(display);
	root = DefaultRootWindow(display);
	depth = DefaultDepth(display, screen);
	black = BlackPixel(display, screen);
	white = WhitePixel(display, screen);

	/* get a font */
	font = XLoadFont(display, "fixed");
	XQueryTextExtents(display, font, "000000", 6, &dummy,
			  &ascent, &descent, &chars);

	create_GCs();
	create_cells();
	create_window(ac, av);

	server = getenv("STANKSERVER");

	/* the command line overrides the environment variable */
	if( ac == 2 )
		server = av[1];

	if( !server ) {
		(void)fprintf(stderr,"usage: %s host\n",av[0]);
		exit(1);
	}

	/* create client handle */

	cl = clnt_create(server,MESSAGEPROG,MESSAGEVERS,"tcp");
	if( cl == NULL ) {
		/* couldn't establish connection */
		clnt_pcreateerror(server);
		exit(1);
	}

	/* select the event mask for the window */
	event_mask = ExposureMask;
	XSelectInput(display, window, event_mask);

	/* display the window */
	XMapWindow(display, window);
	while (True) {
		XNextEvent(display, &event);
		if (event.xany.window != window)
			continue;
		if (event.type == Expose)
			break;
	}

	/* go to it */
	bif_away();

	/* exit */
	do_exit();
}


/*
 * The following function contains the main game loop.
 */
bif_away()
{
	XEvent  event;
	int new;


	draw_bif();

	for(;;) {

		/* find out the number of players */
		new = getplayercnt();

		while (QLength(display) > 0) {
			XNextEvent(display, &event);
			if (event.xany.window != window)
				continue;
			switch (event.type) {
			case Expose:
				draw_bif();
				break;
			default:
				break;
			}
		}

		if( new != plyrcnt ) {
			if( new > plyrcnt )
				XBell(display,0);
			plyrcnt = new;
			draw_bif();
		}

		/*-- Synchronization And Delay ------------------------------*/

		XSync(display, False);

		usleep((unsigned)DELAYCNT);
	} 
}


do_exit()
{
	XUnmapWindow(display, window);
	XFlush(display);
	XCloseDisplay(display);
	exit(1);
}

create_window(ac, av)
	int     ac;
	char   *av[];

{
	XSizeHints hints;
	XWMHints xwmh;
	char   *name = "tankbif";

	window = XCreateSimpleWindow(display, root, 0, 0, BIF_WXSIZE,
				     BIF_WYSIZE, 1, white, black);

	hints.flags = PSize | PMinSize | PMaxSize;
	hints.width = hints.min_width = hints.max_width = BIF_WXSIZE;
	hints.height = hints.min_height =
		hints.max_height = BIF_WYSIZE;

	XSetStandardProperties(display, window, name, name, NULL,
			       av, ac, &hints);

	xwmh.input = False;
	xwmh.initial_state = NormalState;
	xwmh.flags = StateHint;
	XSetWMHints(display, window, &xwmh);
}


create_GCs()
{
	XGCValues gcv;
	unsigned long mask;

	mask = GCForeground | GCBackground | GCFunction |
		GCFont | GCGraphicsExposures;

	/* context for copying; used for animation */
	gcv.foreground = black;
	gcv.background = white;
	gcv.font = font;
	gcv.function = GXcopy;
	gcv.graphics_exposures = False;
	copyGC = XCreateGC(display, root, mask, &gcv);
}


draw_bif()
{
	Pixmap	pm;
	int i;
	

	for( i = 0 ; i < MAXBIF ; i++ ) {
		pm = ( i < plyrcnt ) ? tcells[i][0] : tcells[i][3];
		XCopyArea(display,pm,window,copyGC,0,0,TANK_LEN,TANK_LEN,
			i*TANK_LEN,0);
	}
}


getplayercnt()
{
	int *result;
	static void *tmp;
	
	/* call remote procedure */
	result = getplyrcnt_1(&tmp,cl);

	if( result == NULL ) {
		/* an error occurred while calling the server */
		clnt_perror(cl,server);
		exit(1);
	}

	return *result;
}

/* Lint Output
*/
