changeset 138:9eb791e2fa17

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.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 25 Nov 2011 11:04:09 +0200
parents d90f4eaef8e9
children be9cc2ee3c16
files game/Engine.java game/Piece.java
diffstat 2 files changed, 61 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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)