Mercurial > hg > ristipolku
comparison game/Engine.java @ 133:881deac2daf8
Some more work on scaling widgets, etc.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 24 Nov 2011 21:50:41 +0200 |
parents | 67b2322fda91 |
children | 4c0dec72e2f0 |
comparison
equal
deleted
inserted
replaced
132:333df3e1155b | 133:881deac2daf8 |
---|---|
202 { | 202 { |
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; |
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; |
228 startNewGame(); | 228 startNewGame(); |
229 } | 229 } |
230 | 230 |
231 public void startNewGame() | 231 public void startNewGame() |
232 { | 232 { |
233 | |
233 board = new Piece[boardSize][boardSize]; | 234 board = new Piece[boardSize][boardSize]; |
234 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); | 235 board[boardMiddle][boardMiddle] = new Piece(PieceType.START); |
235 | 236 |
236 currX = boardMiddle; | 237 currX = boardMiddle; |
237 currY = boardMiddle; | 238 currY = boardMiddle; |
565 { | 566 { |
566 // Initialize globals | 567 // Initialize globals |
567 System.out.print("Engine() constructor\n"); | 568 System.out.print("Engine() constructor\n"); |
568 | 569 |
569 // Sound system | 570 // Sound system |
570 G.smgr = new SoundManager(new AudioFormat(22050, 16, 1, true, false), 16); | 571 G.smgr = new SoundManager(new AudioFormat(22050, 16, 1, true, false), 1); |
571 | 572 |
572 // Load resources | 573 // Load resources |
573 try | 574 try |
574 { | 575 { |
575 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); | 576 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
576 G.lautaBG = ImageIO.read(res.getStream()); | 577 G.lautaBG = ImageIO.read(res.getStream()); |
577 | 578 |
578 try { | 579 try { |
579 res = new ResourceLoader("graphics/font.ttf"); | 580 res = new ResourceLoader("graphics/font.ttf"); |
580 | 581 |
581 G.fonts = new Font[G.numFonts]; | 582 G.fonts = new Font[G.numFonts]; |
582 G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); | 583 G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); |
583 G.fonts[1] = G.fonts[0].deriveFont(24f); | 584 G.fonts[1] = G.fonts[0].deriveFont(24f); |
584 G.fonts[2] = G.fonts[0].deriveFont(64f); | 585 G.fonts[2] = G.fonts[0].deriveFont(64f); |
585 G.fonts[3] = G.fonts[0].deriveFont(32f); | 586 G.fonts[3] = G.fonts[0].deriveFont(32f); |
586 | |
587 } | 587 } |
588 catch (FontFormatException e) | 588 catch (FontFormatException e) |
589 { | 589 { |
590 System.out.print("Could not initialize fonts.\n"); | 590 System.out.print("Could not initialize fonts.\n"); |
591 } | 591 } |
644 } | 644 } |
645 | 645 |
646 public void paintComponent(Graphics g) | 646 public void paintComponent(Graphics g) |
647 { | 647 { |
648 Graphics2D g2 = (Graphics2D) g; | 648 Graphics2D g2 = (Graphics2D) g; |
649 boolean scaleChanged = false; | |
649 | 650 |
650 // Rescale if parent component size has changed | 651 // Rescale if parent component size has changed |
651 Dimension dim = getSize(); | 652 Dimension dim = getSize(); |
652 if (G.screenDim == null || !dim.equals(G.screenDim)) | 653 if (G.screenDim == null || !dim.equals(G.screenDim)) |
653 { | 654 { |
655 float dw = dim.width / 1024.0f, | |
656 dh = dim.height / 768.0f; | |
657 | |
654 // Rescale IDM GUI widgets | 658 // Rescale IDM GUI widgets |
655 widgets.setScale(dim.width / 1024.0f, dim.height / 768.0f); | 659 widgets.setScale(dw, dh); |
656 G.screenDim = dim; | 660 G.screenDim = dim; |
657 | 661 |
658 // Rescale background image | 662 // Rescale background image |
659 G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); | 663 G.lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB); |
660 Graphics2D gimg = G.lautaBGScaled.createGraphics(); | 664 Graphics2D gimg = G.lautaBGScaled.createGraphics(); |
662 RenderingHints.VALUE_INTERPOLATION_BICUBIC); | 666 RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
663 | 667 |
664 gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); | 668 gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); |
665 lauta.paintBackPlate(gimg); | 669 lauta.paintBackPlate(gimg); |
666 | 670 |
671 // Rescale fonts | |
672 G.fonts[1] = G.fonts[0].deriveFont(24f * dw); | |
673 G.fonts[2] = G.fonts[0].deriveFont(64f * dw); | |
674 G.fonts[3] = G.fonts[0].deriveFont(32f * dw); | |
675 | |
667 System.out.print("scale changed\n"); | 676 System.out.print("scale changed\n"); |
677 scaleChanged = true; | |
668 } | 678 } |
669 | 679 |
670 // Get font metrics against current Graphics2D context | 680 // Get font metrics against current Graphics2D context |
671 if (G.metrics == null) | 681 if (G.metrics == null || scaleChanged) |
672 { | 682 { |
673 G.metrics = new FontMetrics[G.numFonts]; | 683 G.metrics = new FontMetrics[G.numFonts]; |
674 for (int i = 0; i < G.numFonts; i++) | 684 for (int i = 0; i < G.numFonts; i++) |
675 G.metrics[i] = g2.getFontMetrics(G.fonts[i]); | 685 G.metrics[i] = g2.getFontMetrics(G.fonts[i]); |
676 } | 686 } |