Make Claws glyph available in non-Chu variants
[xboard.git] / draw.c
diff --git a/draw.c b/draw.c
index 930b02c..3cb5727 100644 (file)
--- a/draw.c
+++ b/draw.c
@@ -109,7 +109,7 @@ static cairo_surface_t *pngPieceImages[2][(int)BlackPawn+4];   // png 256 x 256
 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 RsvgHandle *svgPieces[2][(int)BlackPawn+4]; // vector pieces in store
-static cairo_surface_t *pngBoardBitmap[2];
+static cairo_surface_t *pngBoardBitmap[2], *pngOriginalBoardBitmap[2];
 int useTexture, textureW[2], textureH[2];
 
 #define pieceToSolid(piece) &pieceBitmap[SOLID][(piece) % (int)BlackPawn]
@@ -141,7 +141,7 @@ SelectPieces(VariantClass v)
        int p;
        for(p=0; p<=(int)WhiteKing; p++)
           pngPieceBitmaps[i][p] = pngPieceBitmaps2[i][p]; // defaults
-       if(v == VariantShogi) {
+       if(v == VariantShogi && BOARD_HEIGHT != 7) { // no exceptions in Tori Shogi
           pngPieceBitmaps[i][(int)WhiteCannon] = pngPieceBitmaps2[i][(int)WhiteTokin];
           pngPieceBitmaps[i][(int)WhiteNightrider] = pngPieceBitmaps2[i][(int)WhiteKing+2];
           pngPieceBitmaps[i][(int)WhiteGrasshopper] = pngPieceBitmaps2[i][(int)WhiteKing+3];
@@ -162,10 +162,14 @@ SelectPieces(VariantClass v)
           pngPieceBitmaps[i][(int)WhiteNightrider] = pngPieceBitmaps2[i][(int)WhiteLion];
        }
        if(v == VariantChu) {
-          pngPieceBitmaps[i][(int)WhiteNightrider] = pngPieceBitmaps2[i][(int)WhiteKing+1];
-          pngPieceBitmaps[i][(int)WhiteUnicorn] = pngPieceBitmaps2[i][(int)WhiteCat];
-          pngPieceBitmaps[i][(int)WhiteSilver]  = pngPieceBitmaps2[i][(int)WhiteSword];
-          pngPieceBitmaps[i][(int)WhiteFalcon]  = pngPieceBitmaps2[i][(int)WhiteDagger];
+          pngPieceBitmaps[i][(int)WhiteNightrider] = pngPieceBitmaps2[i][(int)WhiteClaw];
+          pngPieceBitmaps[i][(int)WhiteClaw]    = pngPieceBitmaps2[i][(int)WhiteNightrider];
+          pngPieceBitmaps[i][(int)WhiteUnicorn] = pngPieceBitmaps2[i][(int)WhiteHorned];
+          pngPieceBitmaps[i][(int)WhiteSilver]  = pngPieceBitmaps2[i][(int)WhiteStag];
+          pngPieceBitmaps[i][(int)WhiteFalcon]  = pngPieceBitmaps2[i][(int)WhiteEagle];
+          pngPieceBitmaps[i][(int)WhiteHorned]  = pngPieceBitmaps2[i][(int)WhiteUnicorn];
+          pngPieceBitmaps[i][(int)WhiteStag]    = pngPieceBitmaps2[i][(int)WhiteSilver];
+          pngPieceBitmaps[i][(int)WhiteEagle]   = pngPieceBitmaps2[i][(int)WhiteFalcon];
        }
     }
 }
@@ -202,6 +206,7 @@ InitDrawingSizes (BoardSize boardSize, int flags)
 
     oldWidth = boardWidth; oldHeight = boardHeight;
     CreateGrid();
+    CreateAnyPieces(0); // redo texture scaling
 
     /*
      * Inhibit shell resizing.
@@ -237,26 +242,58 @@ ExposeRedraw (Option *graph, int x, int y, int w, int h)
 static void
 CreatePNGBoard (char *s, int kind)
 {
+    float w, h;
+    static float n[2] = { 1., 1. };
     if(!appData.useBitmaps || s == NULL || *s == 0 || *s == '*') { useTexture &= ~(kind+1); return; }
+    textureW[kind] = 0; // prevents bitmap from being used if not succesfully loaded
     if(strstr(s, ".png")) {
        cairo_surface_t *img = cairo_image_surface_create_from_png (s);
        if(img) {
-           useTexture |= kind + 1; pngBoardBitmap[kind] = img;
-           textureW[kind] = cairo_image_surface_get_width (img);
-           textureH[kind] = cairo_image_surface_get_height (img);
+           char c, *p = s, *q;
+           int r, f;
+           if(pngOriginalBoardBitmap[kind]) cairo_surface_destroy(pngOriginalBoardBitmap[kind]);
+           if(n[kind] != 1.) cairo_surface_destroy(pngBoardBitmap[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.;
+           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
+           } 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]++;
+           } else {
+               while(squareSize > n[kind]*w || squareSize > n[kind]*h) n[kind]++;
+           }
+           if(n[kind] == 1.) pngBoardBitmap[kind] = img; else {
+               // create scaled-up copy of the raw png image when it was too small
+               cairo_surface_t *cs = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, n[kind]*w, n[kind]*h);
+               cairo_t *cr = cairo_create(cs);
+               pngBoardBitmap[kind] = cs; textureW[kind] *= n[kind]; textureH[kind] *= n[kind];
+//             cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
+               cairo_scale(cr, n[kind], n[kind]);
+               cairo_set_source_surface (cr, img, 0, 0);
+               cairo_paint (cr);
+               cairo_destroy (cr);
+           }
        }
     }
 }
 
 char *pngPieceNames[] = // must be in same order as internal piece encoding
 { "Pawn", "Knight", "Bishop", "Rook", "Queen", "Advisor", "Elephant", "Archbishop", "Marshall", "Gold", "Commoner",
-  "Canon", "Nightrider", "CrownedBishop", "CrownedRook", "Princess", "Chancellor", "Hawk", "Lance", "Cobra", "Unicorn", "Lion",
-  "GoldPawn", "HSword", "PromoHorse", "PromoDragon", "Leopard", "PromoSword", "Prince", "Phoenix", "Kylin", "PromoRook", "PromoHSword",
-  "Dolphin", "Chancellor", "Unicorn", "Hawk", "Sword", "Princess", "HCrown", "Knight", "Elephant", "PromoBishop", "King",
+  "Canon", "Nightrider", "CrownedBishop", "CrownedRook", "Crown", "Chancellor", "Hawk", "Lance", "Cobra", "Unicorn", "Lion",
+  "GoldPawn", "Claw", "PromoHorse", "PromoDragon", "GoldLance", "PromoSword", "Prince", "Phoenix", "Kylin", "PromoRook", "PromoHSword",
+  "Dolphin", "Sword", "Leopard", "HSword", "GoldSilver", "Princess", "HCrown", "Knight", "Elephant", "PromoBishop", "King",
   "Claw", "GoldKnight", "GoldLance", "GoldSilver", NULL
 };
 
-char *backupPiece[] = { "King", "Queen", "Lion" }; // pieces that map on other when not kanji
+char *backupPiece[] = { "Princess", NULL, NULL, NULL, NULL, NULL, NULL,
+                       NULL, NULL, NULL, NULL, NULL, NULL, "King", "Queen", "Lion" }; // pieces that map on other when not kanji
 
 RsvgHandle *
 LoadSVG (char *dir, int color, int piece, int retry)
@@ -267,9 +304,11 @@ LoadSVG (char *dir, int color, int piece, int retry)
   GError *svgerror=NULL;
   cairo_surface_t *img;
   cairo_t *cr;
+  char *name = (retry ? backupPiece[piece - WhiteGrasshopper] : pngPieceNames[piece]);
 
-    snprintf(buf, MSG_SIZ, "%s/%s%s.svg", dir, color ? "Black" : "White",
-             retry ? backupPiece[piece - WhiteMonarch] : pngPieceNames[piece]);
+    if(!name) return NULL;
+
+    snprintf(buf, MSG_SIZ, "%s/%s%s.svg", dir, color ? "Black" : "White", name);
 
     if(svg || *dir && (svg = rsvg_handle_new_from_file(buf, &svgerror))) {
 
@@ -287,7 +326,7 @@ LoadSVG (char *dir, int color, int piece, int retry)
 
       return svg;
     }
-    if(!retry && piece >= WhiteMonarch && piece <= WhiteNothing) // pieces that are only different in kanji sets
+    if(!retry && piece >= WhiteGrasshopper && piece <= WhiteNothing) // pieces that are only different in kanji sets
         return LoadSVG(dir, color, piece, 1);
     if(svgerror)
        g_error_free(svgerror);
@@ -381,9 +420,9 @@ CreatePNGPieces ()
 }
 
 void
-CreateAnyPieces ()
+CreateAnyPieces (int p)
 {   // [HGM] taken out of main
-    CreatePNGPieces();
+    if(p) CreatePNGPieces();
     CreatePNGBoard(appData.liteBackTextureFile, 1);
     CreatePNGBoard(appData.darkBackTextureFile, 0);
 }
@@ -399,7 +438,7 @@ InitDrawingParams (int reloadPieces)
        if(svgPieces[i][p]) rsvg_handle_close(svgPieces[i][p], NULL);
        svgPieces[i][p] = NULL;
     }
-    CreateAnyPieces();
+    CreateAnyPieces(1);
 }
 
 // [HGM] seekgraph: some low-level drawing routines (by JC, mostly)