Mercurial > hg > ristipolku
comparison game/Engine.java @ 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 | a33fdb1de11c |
children | be9cc2ee3c16 |
comparison
equal
deleted
inserted
replaced
137:d90f4eaef8e9 | 138:9eb791e2fa17 |
---|---|
203 static final int boardSize = 9; | 203 static final int boardSize = 9; |
204 static final int boardMiddle = 4; | 204 static final int boardMiddle = 4; |
205 Piece[][] board; | 205 Piece[][] board; |
206 float pscale, ptime; | 206 float pscale, ptime; |
207 | 207 |
208 public boolean flagGameOver; | 208 public boolean flagGameOver, flagBoardIsDirty; |
209 int gameScore; | 209 int gameScore; |
210 | 210 |
211 Piece currPiece, nextPiece; | 211 Piece currPiece, nextPiece; |
212 int currX, currY, currPoint; | 212 int currX, currY, currPoint; |
213 | 213 |
240 | 240 |
241 currPiece = null; | 241 currPiece = null; |
242 nextPiece = new Piece(PieceType.ACTIVE); | 242 nextPiece = new Piece(PieceType.ACTIVE); |
243 | 243 |
244 flagGameOver = false; | 244 flagGameOver = false; |
245 flagBoardIsDirty = true; | |
245 pieceFinishTurn(); | 246 pieceFinishTurn(); |
246 gameScore = 0; | 247 gameScore = 0; |
247 } | 248 } |
248 | 249 |
249 public void paintBackPlate(Graphics2D g) | 250 public void paintBackPlate(Graphics2D g) |
253 g.draw(new RoundRectangle2D.Float(getScaledX(), getScaledY(), | 254 g.draw(new RoundRectangle2D.Float(getScaledX(), getScaledY(), |
254 boardSize * pscale, boardSize * pscale, | 255 boardSize * pscale, boardSize * pscale, |
255 pscale / 5, pscale / 5)); | 256 pscale / 5, pscale / 5)); |
256 } | 257 } |
257 | 258 |
258 public void paint(Graphics2D g) | 259 public void paintBoard(Graphics2D g, boolean drawCurrent) |
259 { | 260 { |
260 for (int y = 0; y < boardSize; y++) | 261 for (int y = 0; y < boardSize; y++) |
261 for (int x = 0; x < boardSize; x++) | 262 for (int x = 0; x < boardSize; x++) |
262 if (board[x][y] != null) | 263 if (board[x][y] != null) |
263 { | 264 { |
264 board[x][y].paint(g, | 265 if ((drawCurrent && board[x][y] == currPiece) || |
265 getScaledX() + (x * pscale), | 266 (!drawCurrent && board[x][y] != currPiece)) |
266 getScaledY() + (y * pscale), | 267 { |
267 pscale - pscale / 10); | 268 board[x][y].paint(g, |
268 } | 269 getScaledX() + (x * pscale), |
270 getScaledY() + (y * pscale), | |
271 pscale - pscale / 10); | |
272 } | |
273 } | |
274 } | |
275 | |
276 public void paint(Graphics2D g) | |
277 { | |
278 paintBoard(g, true); | |
269 | 279 |
270 Lock read = pointLock.readLock(); | 280 Lock read = pointLock.readLock(); |
271 read.lock(); | 281 read.lock(); |
272 try | 282 try |
273 { | 283 { |
320 y >= getScaledY() && | 330 y >= getScaledY() && |
321 x < getScaledX() + boardSize * pscale && | 331 x < getScaledX() + boardSize * pscale && |
322 y < getScaledY() + boardSize * pscale); | 332 y < getScaledY() + boardSize * pscale); |
323 } | 333 } |
324 | 334 |
335 public boolean isBoardDirty() | |
336 { | |
337 if (flagBoardIsDirty) | |
338 { | |
339 flagBoardIsDirty = false; | |
340 return true; | |
341 } | |
342 else | |
343 return false; | |
344 } | |
345 | |
325 public void animate(float time) | 346 public void animate(float time) |
326 { | 347 { |
327 ptime = time; | 348 ptime = time; |
328 for (int y = 0; y < boardSize; y++) | 349 for (int y = 0; y < boardSize; y++) |
329 for (int x = 0; x < boardSize; x++) | 350 for (int x = 0; x < boardSize; x++) |
330 if (board[x][y] != null) | 351 if (board[x][y] != null) |
331 { | 352 { |
332 board[x][y].animate(time); | 353 board[x][y].animate(time); |
354 if (board[x][y] != currPiece && board[x][y].active) | |
355 flagBoardIsDirty = true; | |
333 } | 356 } |
334 | 357 |
335 Lock write = pointLock.writeLock(); | 358 Lock write = pointLock.writeLock(); |
336 write.lock(); | 359 write.lock(); |
337 try | 360 try |
384 public void pieceCreateNew() | 407 public void pieceCreateNew() |
385 { | 408 { |
386 currPiece = nextPiece; | 409 currPiece = nextPiece; |
387 currPiece.changed(); | 410 currPiece.changed(); |
388 nextPiece = new Piece(PieceType.ACTIVE); | 411 nextPiece = new Piece(PieceType.ACTIVE); |
412 flagBoardIsDirty = true; | |
389 } | 413 } |
390 | 414 |
391 public void pieceSwapCurrent() | 415 public void pieceSwapCurrent() |
392 { | 416 { |
393 if (!flagGameOver) | 417 if (!flagGameOver) |
646 } | 670 } |
647 | 671 |
648 public void paintComponent(Graphics g) | 672 public void paintComponent(Graphics g) |
649 { | 673 { |
650 Graphics2D g2 = (Graphics2D) g; | 674 Graphics2D g2 = (Graphics2D) g; |
651 boolean scaleChanged = false; | 675 boolean scaleChanged = false, |
676 updateBoard = lauta.isBoardDirty(); | |
652 | 677 |
653 // Rescale if parent component size has changed | 678 // Rescale if parent component size has changed |
654 Dimension dim = getSize(); | 679 Dimension dim = getSize(); |
655 if (G.screenDim == null || !dim.equals(G.screenDim)) | 680 if (G.screenDim == null || !dim.equals(G.screenDim)) |
656 { | 681 { |
660 // Rescale IDM GUI widgets | 685 // Rescale IDM GUI widgets |
661 widgets.setScale(dw, dh); | 686 widgets.setScale(dw, dh); |
662 G.screenDim = dim; | 687 G.screenDim = dim; |
663 | 688 |
664 // Rescale background image | 689 // Rescale background image |
690 // Rescale fonts | |
691 G.fonts[1] = G.fonts[0].deriveFont(24f * dw); | |
692 G.fonts[2] = G.fonts[0].deriveFont(64f * dw); | |
693 G.fonts[3] = G.fonts[0].deriveFont(32f * dw); | |
694 | |
695 System.out.print("scale changed\n"); | |
696 scaleChanged = true; | |
697 updateBoard = true; | |
698 } | |
699 | |
700 if (updateBoard) | |
701 { | |
702 System.out.print("updateBoard()\n"); | |
665 G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); | 703 G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); |
666 Graphics2D gimg = G.lautaBGScaled.createGraphics(); | 704 Graphics2D gimg = G.lautaBGScaled.createGraphics(); |
667 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, | 705 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
668 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | 706 RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
669 | 707 |
708 gimg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
709 RenderingHints.VALUE_ANTIALIAS_ON); | |
710 | |
711 gimg.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, | |
712 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | |
713 | |
670 gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); | 714 gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); |
671 lauta.paintBackPlate(gimg); | 715 lauta.paintBackPlate(gimg); |
672 | 716 lauta.paintBoard(gimg, false); |
673 // Rescale fonts | |
674 G.fonts[1] = G.fonts[0].deriveFont(24f * dw); | |
675 G.fonts[2] = G.fonts[0].deriveFont(64f * dw); | |
676 G.fonts[3] = G.fonts[0].deriveFont(32f * dw); | |
677 | |
678 System.out.print("scale changed\n"); | |
679 scaleChanged = true; | |
680 } | 717 } |
681 | 718 |
682 // Get font metrics against current Graphics2D context | 719 // Get font metrics against current Graphics2D context |
683 if (G.metrics == null || scaleChanged) | 720 if (G.metrics == null || scaleChanged) |
684 { | 721 { |