#define OUTLINE 1
Boolean cairoAnimate;
static cairo_surface_t *csBoardWindow, *csBoardBackup, *csDualBoard;
+static cairo_surface_t *pngPieceImages[2][(int)BlackPawn+4]; // png 256 x 256 images
static cairo_surface_t *pngPieceBitmaps[2][(int)BlackPawn]; // scaled pieces as used
static cairo_surface_t *pngPieceBitmaps2[2][(int)BlackPawn+4]; // scaled pieces in store
static cairo_surface_t *pngBoardBitmap[2];
csDualBoard = cstmp;
}
-void
-NewSurfaces ()
-{
- // delete surfaces after size becomes invalid, so they will be recreated
- if(csBoardWindow) cairo_surface_destroy(csBoardWindow);
- if(csBoardBackup) cairo_surface_destroy(csBoardBackup);
- if(csDualBoard) cairo_surface_destroy(csDualBoard);
- csBoardWindow = csBoardBackup = csDualBoard = NULL;
-}
-
#define BoardSize int
void
InitDrawingSizes (BoardSize boardSize, int flags)
oldWidth = boardWidth; oldHeight = boardHeight;
CreateGrid();
- NewSurfaces();
/*
* Inhibit shell resizing.
sscanf(pixels[0], "%*d %*d %d", &f);
sscanf(appData.whitePieceColor+1, "%x", &w);
sscanf(appData.blackPieceColor+1, "%x", &b);
+ res = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, p->size, p->size);
+ stride = cairo_image_surface_get_stride(res);
+ buf = (int *) cairo_image_surface_get_data(res);
for(i=0; i<f; i++) {
ch[i] = pixels[i+1][0];
colcode[i] = 0;
- if(strstr(pixels[i+1], "black")) colcode[i] = 0xFF000000 + (color ? b : 0);
- if(strstr(pixels[i+1], "white")) colcode[i] = 0xFF000000 + w;
+ if(strstr(pixels[i+1], "black")) colcode[i] = 0xFF000000; // 0xFF000000 + (color ? b : 0);
+ if(strstr(pixels[i+1], "white")) colcode[i] = 0xFFFFFFCC; // 0xFF000000 + w;
}
- stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, p->size);
- buf = (int *) malloc(p->size*stride);
for(i=0; i<p->size; i++) {
for(j=0; j<p->size; j++) {
char c = pixels[i+f+1][j];
buf[i*p->size + j] = colcode[k];
}
}
- res = cairo_image_surface_create_for_data((unsigned char *) buf, CAIRO_FORMAT_ARGB32, p->size, p->size, stride);
- if(cairo_surface_status(res) != CAIRO_STATUS_SUCCESS) { printf("bad pixmap convert\n"); exit(1); }
+ cairo_surface_mark_dirty(res);
return res;
}
char buf[MSG_SIZ];
cairo_surface_t *img, *cs;
cairo_t *cr;
- static cairo_surface_t *pngPieceImages[2][(int)BlackPawn+4]; // png 256 x 256 images
- if(!*appData.pngDirectory) img = ConvertPixmap(color, piece); else
- if(pngPieceImages[color][piece] == NULL) { // if PNG file for this piece was not yet read, read it now and store it
- snprintf(buf, MSG_SIZ, "%s/%s%s.png", appData.pngDirectory, color ? "Black" : "White", pngPieceNames[piece]);
- img = cairo_image_surface_create_from_png (buf);
- if(cairo_surface_status(img) != CAIRO_STATUS_SUCCESS) img = ConvertPixmap(color, piece);
+ if((img = pngPieceImages[color][piece]) == NULL) { // if PNG file for this piece was not yet read, read it now and store it
+ if(!*appData.pngDirectory) img = ConvertPixmap(color, piece); else {
+ snprintf(buf, MSG_SIZ, "%s/%s%s.png", appData.pngDirectory, color ? "Black" : "White", pngPieceNames[piece]);
+ img = cairo_image_surface_create_from_png (buf);
+ if(cairo_surface_status(img) != CAIRO_STATUS_SUCCESS) img = ConvertPixmap(color, piece);
+ }
}
pngPieceImages[color][piece] = img;
// create new bitmap to hold scaled piece image (and remove any old)
cairo_set_source_surface (cr, img, 0, 0);
cairo_paint (cr);
cairo_destroy (cr);
+ { // operate on bitmap to color it (king-size hack...)
+ int stride = cairo_image_surface_get_stride(cs)/4;
+ int *buf = (int *) cairo_image_surface_get_data(cs);
+ int i, j, p;
+ sscanf(color ? appData.blackPieceColor+1 : appData.whitePieceColor+1, "%x", &p); // replacement color
+ cairo_surface_flush(cs);
+ for(i=0; i<squareSize; i++) for(j=0; j<squareSize; j++) {
+ int r, a;
+ float f;
+ unsigned int c = buf[i*stride + j];
+ a = c >> 24; r = c >> 16 & 255; // alpha and red, where red is the 'white' weight, since white is #FFFFCC in the source images
+ f = (color ? a - r : r)/255.; // fraction of black or white in the mix that has to be replaced
+ buf[i*stride + j] = c & 0xFF000000; // alpha channel is kept at same opacity
+ buf[i*stride + j] += ((int)(f*(p&0xFF0000)) & 0xFF0000) + ((int)(f*(p&0xFF00)) & 0xFF00) + (int)(f*(p&0xFF)); // add desired fraction of new color
+ if(color) buf[i*stride + j] += r | r << 8 | r << 16; // details on black pieces get their weight added in pure white
+ }
+ cairo_surface_mark_dirty(cs);
+ }
}
void
}
void
-InitDrawingParams ()
+InitDrawingParams (int reloadPieces)
{
+ int i, p;
MakeColors();
+ if(reloadPieces)
+ for(i=0; i<2; i++) for(p=0; p<BlackPawn+4; p++) {
+ if(pngPieceImages[i][p]) cairo_surface_destroy(pngPieceImages[i][p]);
+ pngPieceImages[i][p] = NULL;
+ }
CreateAnyPieces();
}
void
DrawSeekOpen ()
{
- int boardWidth = lineGap + BOARD_WIDTH * (squareSize + lineGap);
- int boardHeight = lineGap + BOARD_HEIGHT * (squareSize + lineGap);
- if(!csBoardWindow) {
- csBoardWindow = GetOutputSurface(&mainOptions[W_BOARD], 0, 0);
- csBoardBackup = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, boardWidth, boardHeight);
- }
+ csBoardWindow = (cairo_surface_t *) mainOptions[W_BOARD].choice;
}
void
}
void
-DoDrawGrid(cairo_surface_t *cs)
+DrawGrid()
{
/* draws a grid starting around Nx, Ny squares starting at x,y */
int i;
DrawSeekOpen();
/* get a cairo_t */
- cr = cairo_create (cs);
+ cr = cairo_create (csBoardWindow);
cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
SetPen(cr, lineGap, "#000000", 0);
}
void
-DrawGrid()
-{
- DoDrawGrid(csBoardWindow);
- if(!dual) DoDrawGrid(csBoardBackup);
-}
-
-void
-DoDrawBorder (cairo_surface_t *cs, int x, int y, int type)
+DrawBorder (int x, int y, int type)
{
cairo_t *cr;
DrawSeekOpen();
case 1: col = appData.highlightSquareColor; break;
case 2: col = appData.premoveHighlightColor; break;
}
- cr = cairo_create(cs);
+ cr = cairo_create(csBoardWindow);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
cairo_rectangle(cr, x, y, squareSize+lineGap, squareSize+lineGap);
SetPen(cr, lineGap, col, 0);
cairo_stroke(cr);
-}
-
-void
-DrawBorder (int x, int y, int type)
-{
- DoDrawBorder(csBoardWindow, x, y, type);
- if(!dual) DoDrawBorder(csBoardBackup, x, y, type);
+ cairo_destroy(cr);
+ DrawExpose(NULL, x, y, squareSize+2*lineGap, squareSize+2*lineGap);
}
static int
DoDrawDot(csBoardWindow, marker, x, y, r);
}
-static void
-DoDrawOneSquare (cairo_surface_t *dest, int x, int y, ChessSquare piece, int square_color, int marker, char *string, int align)
+void
+DrawOneSquare (int x, int y, ChessSquare piece, int square_color, int marker, char *string, int align)
{ // basic front-end board-draw function: takes care of everything that can be in square:
// piece, background, coordinate/count, marker dot
cairo_t *cr;
+ DrawSeekOpen();
+
if (piece == EmptySquare) {
- BlankSquare(dest, x, y, square_color, piece, 1);
+ BlankSquare(csBoardWindow, x, y, square_color, piece, 1);
} else {
- pngDrawPiece(dest, piece, square_color, x, y);
+ pngDrawPiece(csBoardWindow, piece, square_color, x, y);
}
if(align) { // square carries inscription (coord or piece count)
int xx = x, yy = y;
cairo_text_extents_t te;
- cr = cairo_create (dest);
+ cr = cairo_create (csBoardWindow);
cairo_select_font_face (cr, "Sans",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
}
if(marker) { // print fat marker dot, if requested
- DoDrawDot(dest, marker, x + squareSize/4, y+squareSize/4, squareSize/2);
+ DoDrawDot(csBoardWindow, marker, x + squareSize/4, y+squareSize/4, squareSize/2);
}
}
-void
-DrawOneSquare (int x, int y, ChessSquare piece, int square_color, int marker, char *string, int align)
-{
- DrawSeekOpen();
- DoDrawOneSquare (csBoardWindow, x, y, piece, square_color, marker, string, align);
- if(!dual)
- DoDrawOneSquare (csBoardBackup, x, y, piece, square_color, marker, string, align);
-}
-
/**** Animation code by Hugh Fisher, DCS, ANU. ****/
/* Masks for XPM pieces. Black and white pieces can have
void CopyRectangle (AnimNr anr, int srcBuf, int destBuf,
int srcX, int srcY, int width, int height, int destX, int destY)
{
- cairo_t *cr;// = cairo_create (c_animBufs[anr+destBuf]);
- cr = cairo_create (c_animBufs[anr+destBuf]);
- if(c_animBufs[anr+srcBuf] == csBoardWindow)
- cairo_set_source_surface (cr, csBoardBackup, destX - srcX, destY - srcY);
- else
+ cairo_t *cr = cairo_create (c_animBufs[anr+destBuf]);
cairo_set_source_surface (cr, c_animBufs[anr+srcBuf], destX - srcX, destY - srcY);
cairo_rectangle (cr, destX, destY, width, height);
cairo_fill (cr);
cairo_destroy (cr);
- if(c_animBufs[anr+destBuf] == csBoardWindow) {
- cr = cairo_create (csBoardBackup); // also draw to backup
- cairo_set_source_surface (cr, c_animBufs[anr+srcBuf], destX - srcX, destY - srcY);
- cairo_rectangle (cr, destX, destY, width, height);
- cairo_fill (cr);
- cairo_destroy (cr);
- }
+ if(c_animBufs[anr+destBuf] == csBoardWindow)
+ DrawExpose(NULL, destX, destY, squareSize, squareSize);
}
void
DrawPolygon (Pnt arrow[], int nr)
{
DoDrawPolygon(csBoardWindow, arrow, nr);
- if(!dual) DoDrawPolygon(csBoardBackup, arrow, nr);
+// if(!dual) DoDrawPolygon(csBoardBackup, arrow, nr);
}