# HG changeset patch # User Matti Hamalainen # Date 1322211849 -7200 # Node ID 9eb791e2fa17175a16a953cc5f4c6c66cdee1670 # Parent d90f4eaef8e9093289aa1b3ab33ef85888cdc823 Optimize board updating logic, so that the old placed tiles need not to be redrawn from scratch on each screen update, as they do not change usually. diff -r d90f4eaef8e9 -r 9eb791e2fa17 game/Engine.java --- a/game/Engine.java Thu Nov 24 22:58:05 2011 +0200 +++ b/game/Engine.java Fri Nov 25 11:04:09 2011 +0200 @@ -205,7 +205,7 @@ Piece[][] board; float pscale, ptime; - public boolean flagGameOver; + public boolean flagGameOver, flagBoardIsDirty; int gameScore; Piece currPiece, nextPiece; @@ -242,6 +242,7 @@ nextPiece = new Piece(PieceType.ACTIVE); flagGameOver = false; + flagBoardIsDirty = true; pieceFinishTurn(); gameScore = 0; } @@ -255,17 +256,26 @@ pscale / 5, pscale / 5)); } - public void paint(Graphics2D g) + public void paintBoard(Graphics2D g, boolean drawCurrent) { for (int y = 0; y < boardSize; y++) for (int x = 0; x < boardSize; x++) if (board[x][y] != null) { - board[x][y].paint(g, - getScaledX() + (x * pscale), - getScaledY() + (y * pscale), - pscale - pscale / 10); + if ((drawCurrent && board[x][y] == currPiece) || + (!drawCurrent && board[x][y] != currPiece)) + { + board[x][y].paint(g, + getScaledX() + (x * pscale), + getScaledY() + (y * pscale), + pscale - pscale / 10); + } } + } + + public void paint(Graphics2D g) + { + paintBoard(g, true); Lock read = pointLock.readLock(); read.lock(); @@ -322,6 +332,17 @@ y < getScaledY() + boardSize * pscale); } + public boolean isBoardDirty() + { + if (flagBoardIsDirty) + { + flagBoardIsDirty = false; + return true; + } + else + return false; + } + public void animate(float time) { ptime = time; @@ -330,6 +351,8 @@ if (board[x][y] != null) { board[x][y].animate(time); + if (board[x][y] != currPiece && board[x][y].active) + flagBoardIsDirty = true; } Lock write = pointLock.writeLock(); @@ -386,6 +409,7 @@ currPiece = nextPiece; currPiece.changed(); nextPiece = new Piece(PieceType.ACTIVE); + flagBoardIsDirty = true; } public void pieceSwapCurrent() @@ -648,7 +672,8 @@ public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; - boolean scaleChanged = false; + boolean scaleChanged = false, + updateBoard = lauta.isBoardDirty(); // Rescale if parent component size has changed Dimension dim = getSize(); @@ -662,14 +687,6 @@ G.screenDim = dim; // Rescale background image - G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); - Graphics2D gimg = G.lautaBGScaled.createGraphics(); - gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, - RenderingHints.VALUE_INTERPOLATION_BICUBIC); - - gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); - lauta.paintBackPlate(gimg); - // Rescale fonts G.fonts[1] = G.fonts[0].deriveFont(24f * dw); G.fonts[2] = G.fonts[0].deriveFont(64f * dw); @@ -677,6 +694,26 @@ System.out.print("scale changed\n"); scaleChanged = true; + updateBoard = true; + } + + if (updateBoard) + { + System.out.print("updateBoard()\n"); + G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); + Graphics2D gimg = G.lautaBGScaled.createGraphics(); + gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, + RenderingHints.VALUE_INTERPOLATION_BICUBIC); + + gimg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + + gimg.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, + RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + + gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); + lauta.paintBackPlate(gimg); + lauta.paintBoard(gimg, false); } // Get font metrics against current Graphics2D context diff -r d90f4eaef8e9 -r 9eb791e2fa17 game/Piece.java --- a/game/Piece.java Thu Nov 24 22:58:05 2011 +0200 +++ b/game/Piece.java Fri Nov 25 11:04:09 2011 +0200 @@ -24,7 +24,8 @@ boolean rotationChanged, rotationActive, typeChanged, typeActive, - stateChanged, stateActive; + stateChanged, stateActive, + active; float currAngle, rotationTime, rotationSpeed, typeTime, typeValue, throbTime; @@ -208,6 +209,8 @@ public void animate(float time) { + active = false; + if (rotationChanged) { rotationTime = time; @@ -232,6 +235,8 @@ currAngle = lerpRotation.start; rotationActive = false; } + + active = true; } if (typeChanged) @@ -252,6 +257,9 @@ typeValue = lerpType.start; typeActive = false; } + + + active = true; } if (stateChanged)