📄 draw.c
字号:
#ifndef lintstatic char sccsid[] = "@(#)draw.c 1.1 92/07/30 Copyr 1985 Sun Micro";#endif/* * Copyright (c) 1985 by Sun Microsystems, Inc. */#include <suntool/tool_hs.h>#include "defs.h"static short gray25_data[] = {#include <images/square_25.pr>};mpr_static(gray25_patch, 16, 16, 1, gray25_data);static short gray75_data[] = {#include <images/square_75.pr>};mpr_static(gray75_patch, 16, 16, 1, gray75_data);#define OVERLAP (CircDiam / 3)extern struct toolsw *brdsw;extern struct pixwin *brdwin;extern struct pixfont *msgfont;extern struct rect brdrect; /* 2 sets of two triangles each for top and bottom and for two */ /* colors 0 and 2 are for top, 1 and 3 for bottom; 0 and 1 are */ /* light gray and 2 and 3 are dark gray */static struct pixrect *Triangle[4];static struct pixrect *Disk; /* circle pattern for playing pieces */static struct pixrect *Circle; /* outline of a circle for white piece */static struct pixrect *EraseBuf;/* memory for reconstructing the scene */ /* behind a piece which is removed */extern struct pixrect *dice[6]; /* die faces */extern struct pixrect *dblecubes[7][2]; /* doubling cube faces */static int TriHeight, /* height of a point on the board */ TriWidth, /* width of a point */ CircDiam; /* diameter of a playing piece */static struct timeval flashdelay = {0, 50000};changeboardsize(){ struct pixrect *pattern[2]; struct pixrect *stencil[2]; int midx, xval, c, x, y, d; float height, slope; /* First, get the required dimensions */ win_getsize(brdsw->ts_windowfd, &brdrect); TriWidth = brdrect.r_width / 15; if (TriWidth % 2 == 0) TriWidth--; TriHeight = (brdrect.r_height * 3) / 7; /* Next, make the required pixrects */ for (c = 0; c < 4; c++) { if (Triangle[c] != 0) pr_destroy(Triangle[c]); Triangle[c] = mem_create(TriWidth, TriHeight, 1); } for (c = 0; c < 2; c++) pattern[c] = mem_create(TriWidth, TriHeight, 1); for (c = 0; c < 2; c++) stencil[c] = mem_create(TriWidth, TriHeight, 1); /* Next, make two gray shades */ pr_replrop(pattern[0], 0, 0, TriWidth, TriHeight, PIX_SRC, &gray25_patch, 0, 0); pr_replrop(pattern[1], 0, 0, TriWidth, TriHeight, PIX_SRC, &gray75_patch, 0, 0); /* Next, make two triangle patterns (one up and one down) */ slope = (float) TriHeight / (float) TriWidth * 2; midx = TriWidth / 2; for (height=TriHeight, xval=0; height > 0; height -= slope, xval++) { pr_vector(stencil[0], midx - xval, (int)height, midx - xval, 0, PIX_SRC, 1); pr_vector(stencil[0], midx + xval, (int)height, midx + xval, 0, PIX_SRC, 1); pr_vector(stencil[1], midx - xval, TriHeight - (int)height, midx - xval, TriHeight, PIX_SRC, 1); pr_vector(stencil[1], midx + xval, TriHeight - (int)height, midx + xval, TriHeight, PIX_SRC, 1); } /* Next, stencil gray through pattern to make a gray triangle */ pr_stencil(Triangle[0], 0, 0, TriWidth, TriHeight, PIX_SRC, stencil[0], 0, 0, pattern[0], 0, 0); pr_stencil(Triangle[1], 0, 0, TriWidth, TriHeight, PIX_SRC, stencil[1], 0, 0, pattern[0], 0, 0); pr_stencil(Triangle[2], 0, 0, TriWidth, TriHeight, PIX_SRC, stencil[0], 0, 0, pattern[1], 0, 0); pr_stencil(Triangle[3], 0, 0, TriWidth, TriHeight, PIX_SRC, stencil[1], 0, 0, pattern[1], 0, 0); /* Finally, draw a solid black border around triangle */ for (c = 0; c < 3; c += 2) { pr_vector(Triangle[c], 0, 0, midx, TriHeight, PIX_SRC, 1); pr_vector(Triangle[c], TriWidth, 0, midx, TriHeight, PIX_SRC, 1); pr_vector(Triangle[c+1], 0, TriHeight, midx, 0, PIX_SRC, 1); pr_vector(Triangle[c+1], midx, 0, TriWidth, TriHeight, PIX_SRC, 1); } /* Now, make circle playing pieces */ CircDiam = TriHeight / 5; if (CircDiam > TriWidth-2) CircDiam = TriWidth-2; if (Disk != 0) { pr_destroy(Disk); pr_destroy(Circle); pr_destroy(EraseBuf); } Disk = mem_create(CircDiam, CircDiam, 1); Circle = mem_create(CircDiam, CircDiam, 1); EraseBuf = mem_create(TriWidth, CircDiam, 1); /* Draw filled circle pattern */ x = 0; y = CircDiam / 2 - 1; d = 3 - 2 * (CircDiam / 2 - 1); while (x < y) { pr_vector(Disk, CircDiam/2-y, CircDiam/2+x, CircDiam/2+y, CircDiam/2+x, PIX_SRC, 1); pr_vector(Disk, CircDiam/2-x, CircDiam/2+y, CircDiam/2+x, CircDiam/2+y, PIX_SRC, 1); pr_vector(Disk, CircDiam/2-y, CircDiam/2-x, CircDiam/2+y, CircDiam/2-x, PIX_SRC, 1); pr_vector(Disk, CircDiam/2-x, CircDiam/2-y, CircDiam/2+x, CircDiam/2-y, PIX_SRC, 1); pr_put(Circle, CircDiam/2-y, CircDiam/2+x, 1); pr_put(Circle, CircDiam/2+y, CircDiam/2+x, 1); pr_put(Circle, CircDiam/2-x, CircDiam/2+y, 1); pr_put(Circle, CircDiam/2+x, CircDiam/2+y, 1); pr_put(Circle, CircDiam/2-y, CircDiam/2-x, 1); pr_put(Circle, CircDiam/2+y, CircDiam/2-x, 1); pr_put(Circle, CircDiam/2-x, CircDiam/2-y, 1); pr_put(Circle, CircDiam/2+x, CircDiam/2-y, 1); if (d < 0) { d = d + 4 * x + 6; } else { d = d + 4 * (x - y) + 10; y--; } x++; } if (x == y) { pr_vector(Disk, CircDiam/2-y, CircDiam/2+x, CircDiam/2+y, CircDiam/2+x, PIX_SRC, 1); pr_vector(Disk, CircDiam/2-x, CircDiam/2+y, CircDiam/2+x, CircDiam/2+y, PIX_SRC, 1); pr_vector(Disk, CircDiam/2-y, CircDiam/2-x, CircDiam/2+y, CircDiam/2-x, PIX_SRC, 1); pr_vector(Disk, CircDiam/2-x, CircDiam/2-y, CircDiam/2+x, CircDiam/2-y, PIX_SRC, 1); pr_put(Circle, CircDiam/2-y, CircDiam/2+x, 1); pr_put(Circle, CircDiam/2+y, CircDiam/2+x, 1); pr_put(Circle, CircDiam/2-x, CircDiam/2+y, 1); pr_put(Circle, CircDiam/2+x, CircDiam/2+y, 1); pr_put(Circle, CircDiam/2-y, CircDiam/2-x, 1); pr_put(Circle, CircDiam/2+y, CircDiam/2-x, 1); pr_put(Circle, CircDiam/2-x, CircDiam/2-y, 1); pr_put(Circle, CircDiam/2+x, CircDiam/2-y, 1); } /* Clean up memory we no longer need */ for (c = 0; c < 2; c++) { pr_destroy(pattern[c]); pr_destroy(stencil[c]); }}redisplay(){ pw_lock(brdwin, &brdrect); drawboard(); drawpieces(); drawdice(BOTH, NOFLASH); drawcube(); drawscore(BOTH); pw_unlock(brdwin);}staticdrawboard(){ int xpos, c; pw_writebackground(brdwin, 0, 0, brdrect.r_width, brdrect.r_height, PIX_SRC); xpos = TriWidth; for (c = 0; c < 6; c++) { pw_write(brdwin, xpos, 0, TriWidth, TriHeight, PIX_SRC, Triangle[c % 2 == 0 ? 0 : 2], 0, 0); pw_write(brdwin, xpos, brdrect.r_height - TriHeight, TriWidth, TriHeight, PIX_SRC, Triangle[c % 2 == 0 ? 3 : 1], 0, 0); xpos += TriWidth; } xpos += TriWidth; for (c = 0; c < 6; c++) { pw_write(brdwin, xpos, 0, TriWidth, TriHeight, PIX_SRC, Triangle[c % 2 == 0 ? 0 : 2], 0, 0); pw_write(brdwin, xpos, brdrect.r_height - TriHeight, TriWidth, TriHeight, PIX_SRC, Triangle[c % 2 == 0 ? 3 : 1], 0, 0); xpos += TriWidth; } pw_vector(brdwin, TriWidth, 0, TriWidth, brdrect.r_height, PIX_SRC, 1); pw_vector(brdwin, 7*TriWidth, 0, 7*TriWidth, brdrect.r_height, PIX_SRC, 1); pw_vector(brdwin, 8*TriWidth, 0, 8*TriWidth, brdrect.r_height, PIX_SRC, 1); pw_vector(brdwin, 14*TriWidth, 0, 14*TriWidth, brdrect.r_height, PIX_SRC, 1);}drawpieces(){ int c; for (c = 0; c < 26; c++) drawpoint(c);}drawpoint(point) /* draw the pieces on a point */int point;{ if (point == BAR) { putpiecesonpnt(leftcoor(point), 1, humanboard[point], humancolor); putpiecesonpnt(leftcoor(point), 0, computerboard[point], computercolor); } else if (point == HOME) { putpiecesonpnt(leftcoor(point), 0, humanboard[point], humancolor); putpiecesonpnt(leftcoor(point), 1, computerboard[point], computercolor); } else { putpiecesonpnt(leftcoor(point), isontop(point), humanboard[point], humancolor); putpiecesonpnt(leftcoor(point), isontop(point), computerboard[point], computercolor); }}putpiecesonpnt(xcor, top, number, color)int xcor, top, number, color;{ int offset, /* if more than 5 pieces on point, must draw */ /* others on top of first 5 offset by a small */ /* amount so bottom ones are partially exposed */ ycor, /* height at which current piece should be drawn */ piece; /* counter for number of pieces to draw */ if (number == 0) return; offset = OVERLAP; if (top) { /* top half of board */ for (ycor=0, piece=0; piece < number; ycor += CircDiam, piece++) { if (piece == 5 || piece == 10) { ycor = offset; offset += OVERLAP; } drawpieceat(xcor, ycor, color); } } else { /* bottom half of board */ for (ycor = brdrect.r_height-CircDiam, piece = 0; piece < number; ycor -= CircDiam, piece++) { if (piece == 5 || piece == 10) { ycor = brdrect.r_height - offset - CircDiam; offset += OVERLAP; } drawpieceat(xcor, ycor, color); } }}/* The following procedure must be EXACTLY analogous to drawhiddenpieceat() */drawpieceat(x, y, color)int x, y, color;{ pw_stencil(brdwin, x, y, CircDiam, CircDiam, PIX_SRC | PIX_COLOR(color == BLACK), Disk, 0, 0, 0, 0, 0); if (color == WHITE) /* must circle the white piece in black */ pw_write(brdwin, x, y, CircDiam, CircDiam, PIX_SRC | PIX_DST, Circle, 0, 0); else /* and black Pieces in white */ pw_stencil(brdwin, x, y, CircDiam, CircDiam, PIX_SRC, Circle, 0, 0, 0, 0, 0);}leftcoor(col)int col;{ if (col == BAR) return(7 * TriWidth + (TriWidth - CircDiam) / 2); if (col == HOME) return(14 * TriWidth + (TriWidth - CircDiam) / 2); if (isontop(col)) return((col - 12 + (col >= 19 ? 1 : 0)) * TriWidth + (TriWidth - CircDiam) / 2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -