Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next: Support Seamless Texture & Enhance Stone Quality #366

Merged
merged 6 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 108 additions & 6 deletions src/main/java/featurecat/lizzie/gui/BoardRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.awt.font.TextAttribute;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -45,6 +46,9 @@ public class BoardRenderer {
private BufferedImage cachedStonesShadowImage = null;
private Zobrist cachedZhash = new Zobrist(); // defaults to an empty board

private BufferedImage cachedBlackStoneImage = null;
private BufferedImage cachedWhiteStoneImage = null;

private BufferedImage branchStonesImage = null;
private BufferedImage branchStonesShadowImage = null;

Expand Down Expand Up @@ -639,8 +643,10 @@ private void drawWoodenBoard(Graphics2D g) {
if (uiConfig.getBoolean("fancy-board")) {
// fancy version
int shadowRadius = (int) (boardLength * MARGIN / 6);
Image boardImage = theme.getBoard();
g.drawImage(boardImage == null ? theme.getBoard() : boardImage, x - 2 * shadowRadius, y - 2 * shadowRadius, boardLength + 4 * shadowRadius, boardLength + 4 * shadowRadius, null);
BufferedImage boardImage = theme.getBoard();
// Support seamless texture
drawTextureImage(g, boardImage == null ? theme.getBoard() : boardImage, x - 2 * shadowRadius, y - 2 * shadowRadius, boardLength + 4 * shadowRadius, boardLength + 4 * shadowRadius);

g.setStroke(new BasicStroke(shadowRadius * 2));
// draw border
g.setColor(new Color(0, 0, 0, 50));
Expand Down Expand Up @@ -752,7 +758,9 @@ private void drawStone(Graphics2D g, Graphics2D gShadow, int centerX, int center
if (uiConfig.getBoolean("fancy-stones")) {
drawShadow(gShadow, centerX, centerY, false);
Image stone = theme.getBlackStone(new int[]{x, y});
g.drawImage(stone, centerX - stoneRadius, centerY - stoneRadius, stoneRadius * 2 + 1, stoneRadius * 2 + 1, null);
// Enhance draw quality
int size = stoneRadius * 2 + 1;
g.drawImage(getScaleStone(stone, color, size, size), centerX - stoneRadius, centerY - stoneRadius, size, size, null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there is also a lot of copy pasting here... For fancy-stones, I think the entire switch statement can be replaced by 6 lines:

if (uiConfig.getBoolean("fancy-stones")) {
    boolean isBlack = color == BLACK || color == BLACK_GHOST || color == BLACK_CAPTURED;
    boolean isGhost = color == BLACK_GHOST || color == WHITE_GHOST;
    drawShadow(gShadow, centerX, centerY, isGhost);
    Image stone = isBlack ? theme.getBlackStone(new int[]{x, y}) : theme.getWhiteStone(new int[]{x, y});
    int size = stoneRadius * 2 + 1;
    g.drawImage(getScaleStone(stone, color, size, size), centerX - stoneRadius, centerY - stoneRadius, size, size, null);
} else {
  // code for non-fancy stones
}

By the way, do you know why do we keep two code paths here? Could we keep only fancy-stones and remove the other one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you're right.
Because there are 'fancy-stone' settings in the configuration file, some people may like simple styles.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have simplified code.

} else {
drawShadow(gShadow, centerX, centerY, true);
g.setColor(Color.BLACK);
Expand All @@ -765,7 +773,9 @@ private void drawStone(Graphics2D g, Graphics2D gShadow, int centerX, int center
if (uiConfig.getBoolean("fancy-stones")) {
drawShadow(gShadow, centerX, centerY, false);
Image stone = theme.getWhiteStone(new int[]{x, y});
g.drawImage(stone, centerX - stoneRadius, centerY - stoneRadius, stoneRadius * 2 + 1, stoneRadius * 2 + 1, null);
// Enhance draw quality
int size = stoneRadius * 2 + 1;
g.drawImage(getScaleStone(stone, color, size, size), centerX - stoneRadius, centerY - stoneRadius, size, size, null);
} else {
drawShadow(gShadow, centerX, centerY, true);
g.setColor(Color.WHITE);
Expand All @@ -779,7 +789,9 @@ private void drawStone(Graphics2D g, Graphics2D gShadow, int centerX, int center
if (uiConfig.getBoolean("fancy-stones")) {
drawShadow(gShadow, centerX, centerY, true);
Image stone = theme.getBlackStone(new int[]{x, y});
g.drawImage(stone, centerX - stoneRadius, centerY - stoneRadius, stoneRadius * 2 + 1, stoneRadius * 2 + 1, null);
// Enhance draw quality
int size = stoneRadius * 2 + 1;
g.drawImage(getScaleStone(stone, color, size, size), centerX - stoneRadius, centerY - stoneRadius, size, size, null);
} else {
drawShadow(gShadow, centerX, centerY, true);
g.setColor(new Color(0, 0, 0));//, uiConfig.getInt("branch-stone-alpha")));
Expand All @@ -791,7 +803,9 @@ private void drawStone(Graphics2D g, Graphics2D gShadow, int centerX, int center
if (uiConfig.getBoolean("fancy-stones")) {
drawShadow(gShadow, centerX, centerY, true);
Image stone = theme.getWhiteStone(new int[]{x, y});
g.drawImage(stone, centerX - stoneRadius, centerY - stoneRadius, stoneRadius * 2 + 1, stoneRadius * 2 + 1, null);
// Enhance draw quality
int size = stoneRadius * 2 + 1;
g.drawImage(getScaleStone(stone, color, size, size), centerX - stoneRadius, centerY - stoneRadius, size, size, null);
} else {
drawShadow(gShadow, centerX, centerY, true);
g.setColor(new Color(255, 255, 255));//, uiConfig.getInt("branch-stone-alpha")));
Expand All @@ -805,6 +819,94 @@ private void drawStone(Graphics2D g, Graphics2D gShadow, int centerX, int center
}
}

/**
* Get scaled stone, if cached then return cached
*/
public BufferedImage getScaleStone(Image img, Stone color, int width, int height) {
BufferedImage stone = null;
switch (color) {
case BLACK:
case BLACK_CAPTURED:
case BLACK_GHOST:
if (cachedBlackStoneImage == null || cachedBlackStoneImage.getWidth() != width || cachedBlackStoneImage.getHeight() != height) {
stone = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire block is copy pasted, could you please factor it out?

Graphics2D g2 = stone.createGraphics();
g2.drawImage(img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
g2.dispose();
cachedBlackStoneImage = stone;
} else {
return cachedBlackStoneImage;
}
break;
case WHITE:
case WHITE_CAPTURED:
case WHITE_GHOST:
if (cachedWhiteStoneImage == null || cachedWhiteStoneImage.getWidth() != width || cachedWhiteStoneImage.getHeight() != height) {
stone = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = stone.createGraphics();
g2.drawImage(img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
g2.dispose();
cachedWhiteStoneImage = stone;
} else {
return cachedWhiteStoneImage;
}
break;
default:
}
return stone;
}

/**
* Draw scale smooth image, enhanced display quality
*/
public void drawScaleSmoothImage(Graphics2D g, BufferedImage img, int x, int y, int width, int height, ImageObserver observer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code?

BufferedImage newstone = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = newstone.createGraphics();
g2.drawImage(img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH), 0, 0, observer);
g2.dispose();
g.drawImage(newstone, x, y, width, height, observer);
}

/**
* Draw scale smooth image, enhanced display quality, less and faster than drawScaleSmoothImage
*/
public void drawScaleImage(Graphics2D g, BufferedImage img, int x, int y, int width, int height, ImageObserver observer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also dead code??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the both functions are dead code, but the drawScaleSmoothImage can provide high quality scale draw, the drawScaleImage can provide better quality than the default and faster than the drawScaleSmoothImage, so I think it might be useful to keep it.
Of course, if it doesn't allow exists, I will remove them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think it might be useful to commit these functions even if they are unused, then please put the code inside a comment with an explanation of what it does differently that the one that's actually used...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have described & disabled the both functions.

BufferedImage newstone = (BufferedImage)img;
int w = img.getWidth();
int h = img.getHeight();
do {
if (w > width) {
w /= 2;
if (w < width) {
w = width;
}
}
if (h > height) {
h /= 2;
if (h < height) {
h = height;
}
}
BufferedImage tmp = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.drawImage(newstone, 0, 0, w, h, null);
g2.dispose();
newstone = tmp;
}
while (w != width || h != height);
g.drawImage(newstone, x, y, width, height, observer);
}

/**
* Draw texture image
*/
public void drawTextureImage(Graphics2D g, BufferedImage img, int x, int y, int width, int height) {
TexturePaint paint = new TexturePaint(img, new Rectangle(0, 0, img.getWidth(), img.getHeight()));
g.setPaint(paint);
g.fill(new Rectangle(x, y, width, height));
}

/**
* Fills in a circle centered at (centerX, centerY) with radius $radius$
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/featurecat/lizzie/gui/LizzieFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ private Graphics2D createBackground() {
BufferedImage background = boardRenderer.theme.getBackground();
int drawWidth = Math.max(background.getWidth(), getWidth());
int drawHeight = Math.max(background.getHeight(), getHeight());

g.drawImage(background, 0, 0, drawWidth, drawHeight, null);
// Support seamless texture
boardRenderer.drawTextureImage(g, background, 0, 0, drawWidth, drawHeight);

return g;
}
Expand Down