X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=draw.c;h=82072bfc9f15ed46a78e5c4efd4a1604ddebe040;hb=c538ad128cef9fea034c4c67405cfb51dc2b6ce3;hp=89f48511e3de015e3f4a2a3c8be5ee7d0e088e40;hpb=d887d2f82c4e05350c23a7563cf6d5dd3efe04d9;p=xboard.git diff --git a/draw.c b/draw.c index 89f4851..82072bf 100644 --- a/draw.c +++ b/draw.c @@ -131,9 +131,34 @@ void SwitchWindow (int main) { currBoard = (main ? &mainOptions[W_BOARD] : &dualOptions[3]); - csBoardWindow = DRAWABLE(currBoard); +// CsBoardWindow = DRAWABLE(currBoard); } + +static void +NewCanvas (Option *graph) +{ + cairo_t *cr; + int w = graph->max, h = graph->value; + if(graph->choice) cairo_surface_destroy((cairo_surface_t *) graph->choice); + graph->choice = (char**) cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h); + // paint white, to prevent weirdness when people maximize window and drag pieces over space next to board + cr = cairo_create ((cairo_surface_t *) graph->choice); + cairo_rectangle (cr, 0, 0, w, h); + cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0); + cairo_fill(cr); + cairo_destroy (cr); + graph->min &= ~REPLACE; +} + +static cairo_surface_t * +CsBoardWindow (Option *opt) +{ // test before every draw event if we need to resize the canvas + if(opt->min & REPLACE) NewCanvas(opt); + return DRAWABLE(opt); +} + + void SelectPieces(VariantClass v) { @@ -244,6 +269,8 @@ ExposeRedraw (Option *graph, int x, int y, int w, int h) cairo_destroy(cr); } +static int modV[2], modH[2]; + static void CreatePNGBoard (char *s, int kind) { @@ -261,13 +288,14 @@ CreatePNGBoard (char *s, int kind) useTexture |= kind + 1; pngOriginalBoardBitmap[kind] = img; w = textureW[kind] = cairo_image_surface_get_width (img); h = textureH[kind] = cairo_image_surface_get_height (img); - n[kind] = 1.; + n[kind] = 1.; modV[kind] = modH[kind] = -1; while((q = strchr(p+1, '-'))) p = q; // find last '-' if(strlen(p) < 11 && sscanf(p, "-%dx%d.pn%c", &f, &r, &c) == 3 && c == 'g') { if(f == 0 || r == 0) f = BOARD_WIDTH, r = BOARD_HEIGHT; // 0x0 means 'fits any', so make it fit textureW[kind] = (w*BOARD_WIDTH)/f; // sync cutting locations with square pattern textureH[kind] = (h*BOARD_HEIGHT)/r; n[kind] = r*squareSize/h; // scale to make it fit exactly vertically + modV[kind] = r; modH[kind] = f; } else if((p = strstr(s, "xq")) && (p == s || p[-1] == '/')) { // assume full-board image for Xiangqi while(0.8*squareSize*BOARD_WIDTH > n[kind]*w || 0.8*squareSize*BOARD_HEIGHT > n[kind]*h) n[kind]++; @@ -323,6 +351,15 @@ LoadSVG (char *dir, int color, int piece, int retry) if(!svg && *dir) { svg = rsvg_handle_new_from_file(buf, &svgerror); + if(!svg) { // failed! If -pid name starts with "sub_" we try to load the piece from the parent directory + char *p = buf, *q; + safeStrCpy(buf, dir, MSG_SIZ); + while((q = strchr(p, '/'))) p = q + 1; + if(!strncmp(p, "sub_", 4)) { + if(p == buf) safeStrCpy(buf, ".", MSG_SIZ); else p[-1] = NULLCHAR; // strip last directory off path + return LoadSVG(buf, color, piece, retry); + } + } if(!svg && *appData.inscriptions) { // if there is no piece-specific SVG, but we make inscriptions, try general background snprintf(buf, MSG_SIZ, "%s/%sTile.svg", dir, color ? "Black" : "White"); svg = rsvg_handle_new_from_file(buf, &svgerror); @@ -486,7 +523,7 @@ void DrawSeekAxis( int x, int y, int xTo, int yTo ) cairo_t *cr; /* get a cairo_t */ - cr = cairo_create (csBoardWindow); + cr = cairo_create (CsBoardWindow(currBoard)); cairo_move_to (cr, x, y); cairo_line_to(cr, xTo, yTo ); @@ -501,7 +538,7 @@ void DrawSeekAxis( int x, int y, int xTo, int yTo ) void DrawSeekBackground( int left, int top, int right, int bottom ) { - cairo_t *cr = cairo_create (csBoardWindow); + cairo_t *cr = cairo_create (CsBoardWindow(currBoard)); cairo_rectangle (cr, left, top, right-left, bottom-top); @@ -515,7 +552,7 @@ void DrawSeekBackground( int left, int top, int right, int bottom ) void DrawSeekText(char *buf, int x, int y) { - cairo_t *cr = cairo_create (csBoardWindow); + cairo_t *cr = cairo_create (CsBoardWindow(currBoard)); cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, @@ -534,7 +571,7 @@ void DrawSeekText(char *buf, int x, int y) void DrawSeekDot(int x, int y, int colorNr) { - cairo_t *cr = cairo_create (csBoardWindow); + cairo_t *cr = cairo_create (CsBoardWindow(currBoard)); int square = colorNr & 0x80; colorNr &= 0x7F; @@ -560,7 +597,8 @@ void DrawSeekDot(int x, int y, int colorNr) void InitDrawingHandle (Option *opt) { - csBoardWindow = DRAWABLE(opt); +// CsBoardWindow = DRAWABLE(opt); + currBoard = opt; } void @@ -598,7 +636,7 @@ DrawGrid() cairo_t *cr; /* get a cairo_t */ - cr = cairo_create (csBoardWindow); + cr = cairo_create (CsBoardWindow(currBoard)); cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE); SetPen(cr, lineGap, "#000000", 0); @@ -630,7 +668,7 @@ DrawBorder (int x, int y, int type, int odd) case 2: col = appData.premoveHighlightColor; break; default: col = "#808080"; break; // cannot happen } - cr = cairo_create(csBoardWindow); + cr = cairo_create(CsBoardWindow(currBoard)); cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE); cairo_rectangle(cr, x+odd/2., y+odd/2., squareSize+lineGap, squareSize+lineGap); SetPen(cr, lineGap, col, 0); @@ -646,6 +684,7 @@ CutOutSquare (int x, int y, int *x0, int *y0, int kind) int nx = x/(squareSize + lineGap), ny = y/(squareSize + lineGap); *x0 = 0; *y0 = 0; if(textureW[kind] < squareSize || textureH[kind] < squareSize) return 0; + if(modV[kind] > 0) nx %= modH[kind], ny %= modV[kind]; // tile fixed-format board periodically to extend it if(textureW[kind] < W*squareSize) *x0 = (textureW[kind] - squareSize) * nx/(W-1); else @@ -665,7 +704,7 @@ DrawLogo (Option *opt, void *logo) int w, h; if(!opt) return; - cr = cairo_create(DRAWABLE(opt)); + cr = cairo_create(CsBoardWindow(opt)); cairo_rectangle (cr, 0, 0, opt->max, opt->value); cairo_set_source_rgba(cr, 0.5, 0.5, 0.5, 1.0); cairo_fill(cr); // paint background in case logo does not exist @@ -759,7 +798,7 @@ DoDrawDot (cairo_surface_t *cs, int marker, int x, int y, int r) void DrawDot (int marker, int x, int y, int r) { // used for atomic captures; no need to draw on backup - DoDrawDot(csBoardWindow, marker, x, y, r); + DoDrawDot(CsBoardWindow(currBoard), marker, x, y, r); GraphExpose(currBoard, x-r, y-r, 2*r, 2*r); } @@ -798,7 +837,7 @@ DrawText (char *string, int x, int y, int align) cairo_text_extents_t te; cairo_t *cr; - cr = cairo_create (csBoardWindow); + cr = cairo_create (CsBoardWindow(currBoard)); cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); @@ -857,10 +896,10 @@ DrawOneSquare (int x, int y, ChessSquare piece, int square_color, int marker, ch // piece, background, coordinate/count, marker dot if (piece == EmptySquare) { - BlankSquare(csBoardWindow, x, y, square_color, piece, 1); + BlankSquare(CsBoardWindow(currBoard), x, y, square_color, piece, 1); } else { - pngDrawPiece(csBoardWindow, piece, square_color, x, y); - if(appData.inscriptions[0]) InscribeKanji(csBoardWindow, piece, x, y); + pngDrawPiece(CsBoardWindow(currBoard), piece, square_color, x, y); + if(appData.inscriptions[0]) InscribeKanji(CsBoardWindow(currBoard), piece, x, y); } if(align) { // square carries inscription (coord or piece count) @@ -869,7 +908,7 @@ DrawOneSquare (int x, int y, ChessSquare piece, int square_color, int marker, ch } if(marker) { // print fat marker dot, if requested - DoDrawDot(csBoardWindow, marker, x + squareSize/4, y+squareSize/4, squareSize/2); + DoDrawDot(CsBoardWindow(currBoard), marker, x + squareSize/4, y+squareSize/4, squareSize/2); } } @@ -888,7 +927,7 @@ InitAnimState (AnimNr anr) { if(c_animBufs[anr]) cairo_surface_destroy (c_animBufs[anr]); if(c_animBufs[anr+2]) cairo_surface_destroy (c_animBufs[anr+2]); - c_animBufs[anr+4] = csBoardWindow; + c_animBufs[anr+4] = CsBoardWindow(currBoard); c_animBufs[anr+2] = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, squareSize, squareSize); c_animBufs[anr] = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, squareSize, squareSize); } @@ -928,13 +967,13 @@ void CopyRectangle (AnimNr anr, int srcBuf, int destBuf, int srcX, int srcY, int width, int height, int destX, int destY) { cairo_t *cr; - c_animBufs[anr+4] = csBoardWindow; + c_animBufs[anr+4] = CsBoardWindow(currBoard); 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) // suspect that GTK needs this! + if(c_animBufs[anr+destBuf] == CsBoardWindow(currBoard)) // suspect that GTK needs this! GraphExpose(currBoard, destX, destY, width, height); } @@ -970,7 +1009,7 @@ DoDrawPolygon (cairo_surface_t *cs, Pnt arrow[], int nr) void DrawPolygon (Pnt arrow[], int nr) { - DoDrawPolygon(csBoardWindow, arrow, nr); + DoDrawPolygon(CsBoardWindow(currBoard), arrow, nr); // if(!dual) DoDrawPolygon(csBoardBackup, arrow, nr); } @@ -1008,7 +1047,7 @@ DrawSegment (int x, int y, int *lastX, int *lastY, int penType) static int curX, curY; if(penType != PEN_NONE) { - cairo_t *cr = cairo_create(DRAWABLE(disp)); + cairo_t *cr = cairo_create(CsBoardWindow(disp)); cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE); cairo_move_to (cr, curX, curY); cairo_line_to (cr, x,y); @@ -1027,7 +1066,7 @@ DrawRectangle (int left, int top, int right, int bottom, int side, int style) { cairo_t *cr; - cr = cairo_create (DRAWABLE(disp)); + cr = cairo_create (CsBoardWindow(disp)); cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE); cairo_rectangle (cr, left, top, right-left, bottom-top); switch(side) @@ -1054,7 +1093,7 @@ DrawEvalText (char *buf, int cbBuf, int y) { // the magic constants 8 and 5 should really be derived from the font size somehow cairo_text_extents_t extents; - cairo_t *cr = cairo_create(DRAWABLE(disp)); + cairo_t *cr = cairo_create(CsBoardWindow(disp)); /* GTK-TODO this has to go into the font-selection */ cairo_select_font_face (cr, "Sans",