⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 window.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* This file contains code for X-CHESS.   Copyright (C) 1986 Free Software Foundation, Inc.This file is part of X-CHESS.X-CHESS is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY.  No author or distributoraccepts responsibility to anyone for the consequences of using itor for whether it serves any particular purpose or works at all,unless he says so in writing.  Refer to the X-CHESS General PublicLicense for full details.Everyone is granted permission to copy, modify and redistributeX-CHESS, but only under the conditions described in theX-CHESS General Public License.   A copy of this license issupposed to have been given to you along with X-CHESS so youcan know your rights and responsibilities.  It should be in afile named COPYING.  Among other things, the copyright noticeand this notice must be preserved on all copies.  *//* RCS Info: $Revision: 1.5 $ on $Date: 86/11/26 12:11:15 $ *           $Source: /users/faustus/xchess/RCS/window.c,v $ * Copyright (c) 1986 Wayne A. Christopher, U. C. Berkeley CAD Group *	Permission is granted to do anything with this code except sell it *	or remove this message. * * Deal with the two (or one) windows. */#include "xchess.h"#include <X11/Xutil.h>#include <sys/time.h>#include "pawn.bitmap"#include "rook.bitmap"#include "knight.bitmap"#include "bishop.bitmap"#include "queen.bitmap"#include "king.bitmap"#include "pawn_outline.bitmap"#include "rook_outline.bitmap"#include "knight_outline.bitmap"#include "bishop_outline.bitmap"#include "queen_outline.bitmap"#include "king_outline.bitmap"#include "pawn_mask.bitmap"#include "rook_mask.bitmap"#include "knight_mask.bitmap"#include "bishop_mask.bitmap"#include "queen_mask.bitmap"#include "king_mask.bitmap"#include "shade.bitmap"#include "xchess.cur"#include "xchess_mask.cur"#include "xchess.icon"windata *win1, *win2;bool win_flashmove = false;extern bool setup();extern void service(), drawgrid(), icon_refresh();boolwin_setup(disp1, disp2)	char *disp1, *disp2;{	win1 = alloc(windata);	if (!oneboard)		win2 = alloc(windata);	if (!setup(disp1, win1) || (!oneboard && !setup(disp2, win2)))		return (false);	if (blackflag) {		win1->color = BLACK;		win1->flipped = true;	} else		win1->color = WHITE;	win_drawboard(win1);	if (!oneboard) {		win2->color = BLACK;		win2->flipped = true;		win_drawboard(win2);	}		return(true);}/* Draw the chess board... */voidwin_drawboard(win)	windata *win;{	int i, j;	drawgrid(win);	/* Now toss on the squares... */	for (i = 0; i < SIZE; i++)		for (j = 0; j < SIZE; j++)			win_erasepiece(j, i, win->color);	return;}/* Draw one piece. */voidwin_drawpiece(p, y, x, wnum)	piece *p;	int y, x;	color wnum;{    char *bits, *maskbits, *outline;    windata *win;    char buf[BSIZE];    XImage *tmpImage;    Pixmap tmpPM, maskPM;    XGCValues gc;    if (oneboard || (wnum == win1->color))	win = win1;    else	win = win2;    if (win->flipped) {	y = SIZE - y - 1;	x = SIZE - x - 1;    }    /*      if (debug)      fprintf(stderr, "draw a %s at (%d, %d) on board %d\n",      piecenames[(int) p->type], y, x, wnum);      */    if ((x < 0) || (x > 7) || (y < 0) || (y > 7)) exit(1);    switch (p->type) {    case PAWN:	bits = pawn_bits;	maskbits = pawn_mask_bits;	outline = pawn_outline_bits;	break;    case ROOK:	bits = rook_bits;	maskbits = rook_mask_bits;	outline = rook_outline_bits;	break;    case KNIGHT:	bits = knight_bits;	maskbits = knight_mask_bits;	outline = knight_outline_bits;	break;    case BISHOP:	bits = bishop_bits;	maskbits = bishop_mask_bits;	outline = bishop_outline_bits;	break;    case QUEEN:	bits = queen_bits;	maskbits = queen_mask_bits;	outline = queen_outline_bits;	break;    case KING:	bits = king_bits;	maskbits = king_mask_bits;	outline = king_outline_bits;	break;    default:	fprintf(stderr,		"Internal Error: win_drawpiece: bad piece type %d\n",		p->type);    }    /* There are two things we can do... If this is a black and white     * display, we have to shade the square and use an outline if the piece     * is white.  We also have to use a mask...  Since we don't want     * to use up too many bitmaps, create the mask bitmap, put the bits,     * and then destroy it.     */    if (win->bnw && (p->color == WHITE))	bits = outline;    if (win->bnw && !iswhite(win, x, y)) {	XSetState(win->display, DefaultGC(win->display, 0),		  BlackPixel(win->display, 0),		  WhitePixel(win->display, 0), GXcopy, AllPlanes);		tmpPM = XCreateBitmapFromData(win->display, win->boardwin,				shade_bits, SQUARE_WIDTH, SQUARE_HEIGHT);	XCopyPlane(win->display, tmpPM, win->boardwin, DefaultGC(win->display, 0),		   0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,		   x * (SQUARE_WIDTH + BORDER_WIDTH),		   y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);	XFreePixmap(win->display, tmpPM);		XSetFunction(win->display, DefaultGC(win->display, 0),		     GXandInverted);	maskPM = XCreateBitmapFromData(win->display, win->boardwin,				      maskbits, SQUARE_WIDTH, SQUARE_HEIGHT);	XCopyPlane(win->display, maskPM, win->boardwin, DefaultGC(win->display, 0),		   0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,		   x * (SQUARE_WIDTH + BORDER_WIDTH),		   y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);	XFreePixmap(win->display, maskPM);	XSetFunction(win->display, DefaultGC(win->display, 0),		     GXor);	tmpPM = XCreateBitmapFromData(win->display, win->boardwin,				bits, SQUARE_WIDTH, SQUARE_HEIGHT);	XCopyPlane(win->display, tmpPM, win->boardwin, DefaultGC(win->display, 0),		   0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,		   x * (SQUARE_WIDTH + BORDER_WIDTH),		   y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);	XFreePixmap(win->display, tmpPM);	XSetFunction(win->display, DefaultGC(win->display, 0), GXcopy);    } else if (win->bnw){	XSetState(win->display, DefaultGC(win->display, 0),		  BlackPixel(win->display, 0),		  WhitePixel(win->display, 0), GXcopy, AllPlanes);	tmpPM = XCreateBitmapFromData(win->display, win->boardwin,				bits, SQUARE_WIDTH, SQUARE_HEIGHT);	XCopyPlane(win->display, tmpPM, win->boardwin, DefaultGC(win->display, 0),		   0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,		   x * (SQUARE_WIDTH + BORDER_WIDTH),		   y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);	XFreePixmap(win->display, tmpPM);    } else {	XSetState(win->display, DefaultGC(win->display, 0),		 ((p->color == WHITE) ? win->whitepiece.pixel :		  			win->blackpiece.pixel),		  (iswhite(win, x, y) ? win->whitesquare.pixel :		   			win->blacksquare.pixel),		  GXcopy, AllPlanes);	tmpPM = XCreateBitmapFromData(win->display, win->boardwin,				bits, SQUARE_WIDTH, SQUARE_HEIGHT);	XCopyPlane(win->display, tmpPM, win->boardwin, DefaultGC(win->display, 0),		   0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,		   x * (SQUARE_WIDTH + BORDER_WIDTH),		   y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);	XFreePixmap(win->display, tmpPM);    }    if (!record_english) {	gc.foreground = win->textcolor.pixel;	if (iswhite(win, x, y) || win->bnw)	    gc.background = win->whitesquare.pixel;	else	    gc.background = win->blacksquare.pixel;	gc.font = win->small->fid;	    	XChangeGC(win->display, DefaultGC(win->display, 0),		  GCForeground | GCBackground | GCFont, &gc);	    	if (!x) {	    sprintf(buf, " %d", SIZE - y);	    XDrawImageString(win->display, win->boardwin,			     DefaultGC(win->display, 0),			     1, (y + 1) * (SQUARE_HEIGHT + 					   BORDER_WIDTH) - BORDER_WIDTH + 			     win->small->max_bounds.ascent - 1, buf, 2);	}	if (y == SIZE - 1) {	    sprintf(buf, "%c", 'A' + x);	    XDrawImageString(win->display, win->boardwin,			     DefaultGC(win->display, 0),			     x * (SQUARE_WIDTH + BORDER_WIDTH) + 1,			     SIZE * (SQUARE_HEIGHT + BORDER_WIDTH) - BORDER_WIDTH + 			     win->small->max_bounds.ascent - 1, buf, 1);	}    }    return;}voidwin_erasepiece(y, x, wnum)	int y, x;	color wnum;{    windata *win;    char buf[BSIZE];    XGCValues gc;    Pixmap tmpPM;        if (oneboard || (wnum == win1->color))	win = win1;    else	win = win2;		    if (win->flipped) {	y = SIZE - y - 1;	x = SIZE - x - 1;    }    /*      if (debug)      fprintf(stderr, "erase square (%d, %d) on board %d\n", y, x,      wnum);      */    if ((x < 0) || (x > 7) || (y < 0) || (y > 7)) exit(1);    if (win->bnw && !iswhite(win, x, y)) {	XSetState(win->display, DefaultGC(win->display, 0),		  BlackPixel(win->display, 0),		  WhitePixel(win->display, 0), GXcopy, AllPlanes);	tmpPM = XCreateBitmapFromData(win->display, win->boardwin,				shade_bits, SQUARE_WIDTH, SQUARE_HEIGHT);	XCopyPlane(win->display, tmpPM, win->boardwin, DefaultGC(win->display, 0),		   0, 0, SQUARE_WIDTH, SQUARE_HEIGHT,		   x * (SQUARE_WIDTH + BORDER_WIDTH),		   y * (SQUARE_HEIGHT + BORDER_WIDTH), 1);	XFreePixmap(win->display, tmpPM);    } else {	XSetFillStyle(win->display, DefaultGC(win->display, 0),		      FillSolid);	XSetForeground(win->display, DefaultGC(win->display, 0),		       iswhite(win, x, y) ? win->whitesquare.pixel :		       win->blacksquare.pixel);	XFillRectangle(win->display, win->boardwin,		       DefaultGC(win->display, 0),		       x * (SQUARE_WIDTH + BORDER_WIDTH),		       y * (SQUARE_HEIGHT + BORDER_WIDTH),		       SQUARE_WIDTH, SQUARE_HEIGHT);    }    if (!record_english) {	gc.foreground = win->textcolor.pixel;	if (iswhite(win, x, y) || win->bnw)	    gc.background = win->whitesquare.pixel;	else	    gc.background = win->blacksquare.pixel;	gc.font = win->small->fid;	    	XChangeGC(win->display, DefaultGC(win->display, 0),		  GCForeground | GCBackground | GCFont, &gc);	    	if (!x) {	    sprintf(buf, " %d", SIZE - y);	    XDrawImageString(win->display, win->boardwin,			     DefaultGC(win->display, 0),			     1, (y + 1) * (SQUARE_HEIGHT + 					   BORDER_WIDTH) - BORDER_WIDTH + 			     win->small->max_bounds.ascent - 1, buf, 2);	}	if (y == SIZE - 1) {	    sprintf(buf, "%c", 'A' + x);	    XDrawImageString(win->display, win->boardwin,			     DefaultGC(win->display, 0),			     x * (SQUARE_WIDTH + BORDER_WIDTH) + 1,			     SIZE * (SQUARE_HEIGHT + BORDER_WIDTH) - BORDER_WIDTH + 			     win->small->max_bounds.ascent - 1, buf, 1);	}    }        return;}voidwin_flash(m, wnum)	move *m;	color wnum;{	windata *win;	int sx, sy, ex, ey, i;	if (oneboard || (wnum == win1->color))		win = win1;	else		win = win2;			if (win->flipped) {		sx = SIZE - m->fromx - 1;		sy = SIZE - m->fromy - 1;		ex = SIZE - m->tox - 1;		ey = SIZE - m->toy - 1;	} else {		sx = m->fromx;		sy = m->fromy;		ex = m->tox;		ey = m->toy;	}	sx = sx * (SQUARE_WIDTH + BORDER_WIDTH) + SQUARE_WIDTH / 2;	sy = sy * (SQUARE_HEIGHT + BORDER_WIDTH) + SQUARE_HEIGHT / 2;	ex = ex * (SQUARE_WIDTH + BORDER_WIDTH) + SQUARE_WIDTH / 2;	ey = ey * (SQUARE_HEIGHT + BORDER_WIDTH) + SQUARE_HEIGHT / 2;	XSetFunction(win->display, DefaultGC(win->display, 0), GXinvert);	XSetLineAttributes(win->display, DefaultGC(win->display, 0),		0, LineSolid, 0, 0);	for (i = 0; i < num_flashes * 2; i++) {	    XDrawLine(win->display,win->boardwin,		      DefaultGC(win->display, 0),		      sx, sy, ex, ey);	}		XSetFunction(win->display, DefaultGC(win->display, 0), GXcopy);	return;}/* Handle input from the players. */voidwin_process(quick)	bool quick;{	int i, rfd = 0, wfd = 0, xfd = 0;	struct timeval timeout;	timeout.tv_sec = 0;	timeout.tv_usec = (quick ? 0 : 500000);	if (XPending(win1->display))		service(win1);	if (!oneboard) {	    if (XPending(win1->display))		service(win2);	}	if (oneboard)		rfd = 1 << win1->display->fd;	else		rfd = (1 << win1->display->fd) | (1 << win2->display->fd);	if (!(i = select(32, &rfd, &wfd, &xfd, &timeout)))		return;	if (i == -1) {		perror("select");		exit(1);	}	if (rfd & (1 << win1->display->fd))		service(win1);	if (!oneboard && (rfd & (1 << win2->display->fd)))		service(win2);	return;}static voidservice(win)	windata *win;{	XEvent ev;	while(XPending(win->display)) {		XNextEvent(win->display, &ev);		if (TxtFilter(win->display, &ev))			continue;		if (ev.xany.window == win->boardwin) {			switch (ev.type) {			    case ButtonPress:				button_pressed(&ev, win);				break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -