comparison game/Engine.java @ 170:b9bc493ae53c

Modularize and clean up code.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 02 Mar 2017 15:57:15 +0200
parents 159252f9480d
children 5070e57ebbfc
comparison
equal deleted inserted replaced
169:32b1c41e194a 170:b9bc493ae53c
42 } 42 }
43 43
44 aboutSecret = false; 44 aboutSecret = false;
45 45
46 setPos(150f, 150f); 46 setPos(150f, 150f);
47 setSize(675f, 420f); 47 setSize(720f, 420f);
48 } 48 }
49 49
50 public void setTextFont(Font font, FontMetrics metrics) 50 public void setTextFont(Font font, FontMetrics metrics)
51 { 51 {
52 textFont = font; 52 textFont = font;
65 } 65 }
66 66
67 public void setCurrPos(float x, float y) 67 public void setCurrPos(float x, float y)
68 { 68 {
69 setCurrPos(new IDMPoint(x, y)); 69 setCurrPos(new IDMPoint(x, y));
70 }
71
72 public void setCurrPosScaledRel(float x, float y)
73 {
74 setCurrPos(new IDMPoint(getScaledRelX(x), getScaledRelY(y)));
70 } 75 }
71 76
72 public void drawString(Graphics2D g, String text) 77 public void drawString(Graphics2D g, String text)
73 { 78 {
74 Paint savePaint = g.getPaint(); 79 Paint savePaint = g.getPaint();
110 g.setPaint(savePaint); 115 g.setPaint(savePaint);
111 } 116 }
112 117
113 public void paint(Graphics2D g) 118 public void paint(Graphics2D g)
114 { 119 {
115 int x = getScaledX(), y = getScaledY(),
116 w = getScaledWidth(), h = getScaledHeight();
117
118 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.7f)); 120 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.7f));
119 g.fill(new RoundRectangle2D.Float(x, y, w, h, 10, 10)); 121 g.fill(new RoundRectangle2D.Float(getScaledX(), getScaledY(), getScaledWidth(), getScaledHeight(), 10, 10));
120 122
121 setTextFont(G.fonts[3], G.metrics[3]); 123 setTextFont(G.fonts[3], G.metrics[3]);
122 setTextPaint(Color.white); 124 setTextPaint(Color.white);
123 125
124 setCurrPos(x + 20, y + 30); 126 setCurrPosScaledRel(20, 30);
125 drawString(g, "RistiPolku (CrossPaths) v"+ G.version +"\n"); 127 drawString(g, "RistiPolku (CrossPaths) v"+ G.version +"\n");
126 128
127 setTextFont(G.fonts[1], G.metrics[1]); 129 setTextFont(G.fonts[1], G.metrics[1]);
128 if (aboutSecret) 130 if (aboutSecret)
129 { 131 {
130 g.drawImage(aboutImg, x + 20, y + 55, 132 g.drawImage(aboutImg, (int) getScaledRelX(20), (int) getScaledRelY(55),
131 aboutImg.getWidth(), aboutImg.getHeight(), null); 133 aboutImg.getWidth(), aboutImg.getHeight(), null);
132 134
133 setCurrPos(x + 225, y + 75); 135 setCurrPosScaledRel(225, 75);
134 drawString(g, 136 drawString(g,
135 "Dedicated to my\n" + 137 "Dedicated to my\n" +
136 "favorite woman\n" + 138 "favorite woman\n" +
137 "in the world."); 139 "in the world.");
138 140
139 setCurrPos(x + 370, y + 175); 141 setCurrPosScaledRel(370, 175);
140 drawString(g, "- Matti"); 142 drawString(g, "- Matti");
141 } 143 }
142 else 144 else
143 { 145 {
144 setTextPaint(Color.yellow); 146 setTextPaint(Color.yellow);
158 drawString(g, 160 drawString(g,
159 "Arrow keys / mouse wheel\n"+ 161 "Arrow keys / mouse wheel\n"+
160 "Enter / mouse click\n"+ 162 "Enter / mouse click\n"+
161 "Space bar\n"); 163 "Space bar\n");
162 164
163 currPos.x += 330; 165 currPos.x += getScaledX(330);
164 currOffs.y = old.y; 166 currOffs.y = old.y;
165 drawString(g, 167 drawString(g,
166 "- Rotate piece\n" + 168 "- Rotate piece\n" +
167 "- Place/lock piece\n" + 169 "- Place/lock piece\n" +
168 "- Swap piece\n"); 170 "- Swap piece\n");
169 171
170 currPos.x -= 330; 172 currPos.x -= getScaledX(330);
171 setTextPaint(Color.green); 173 setTextPaint(Color.green);
172 drawString(g, 174 drawString(g,
173 "\nObjective: Create a path long as possible by rotating\n"+ 175 "\nObjective: Create a path long as possible by rotating\n"+
174 "and placing pieces. More points will be awarded for\n"+ 176 "and placing pieces. More points will be awarded for\n"+
175 "advancing the path by several segments per turn." 177 "advancing the path by several segments per turn."
624 626
625 try { 627 try {
626 res = new ResourceLoader("graphics/font.ttf"); 628 res = new ResourceLoader("graphics/font.ttf");
627 629
628 G.fonts = new Font[G.numFonts]; 630 G.fonts = new Font[G.numFonts];
629 G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); 631 G.baseFont = Font.createFont(Font.TRUETYPE_FONT, res.getStream());
630 G.fonts[1] = G.fonts[0].deriveFont(24f); 632
631 G.fonts[2] = G.fonts[0].deriveFont(64f); 633 setupDerivedFonts(1.0f);
632 G.fonts[3] = G.fonts[0].deriveFont(32f);
633 } 634 }
634 catch (FontFormatException e) 635 catch (FontFormatException e)
635 { 636 {
636 dbg("Could not initialize fonts.\n"); 637 dbg("Could not initialize fonts.\n");
637 } 638 }
687 { 688 {
688 gameFrames = 0; 689 gameFrames = 0;
689 lauta.startNewGame(); 690 lauta.startNewGame();
690 } 691 }
691 692
693 public void setupDerivedFonts(float scale)
694 {
695 G.fonts[0] = G.baseFont.deriveFont(16f * scale);
696 G.fonts[1] = G.baseFont.deriveFont(22f * scale);
697 G.fonts[2] = G.baseFont.deriveFont(62f * scale);
698 G.fonts[3] = G.baseFont.deriveFont(32f * scale);
699 }
700
692 public void paintComponent(Graphics g) 701 public void paintComponent(Graphics g)
693 { 702 {
694 Graphics2D g2 = (Graphics2D) g; 703 Graphics2D g2 = (Graphics2D) g;
695 boolean scaleChanged = false, 704 boolean scaleChanged = false,
696 updateBoard = lauta.isBoardDirty(); 705 updateBoard = lauta.isBoardDirty();
697 706
698 // Rescale if parent component size has changed 707 // Rescale if parent component size has changed
699 Dimension dim = getSize(); 708 Dimension dim = getSize();
700 if (G.screenDim == null || !dim.equals(G.screenDim)) 709 if (G.screenDim == null || !dim.equals(G.screenDim) || G.metrics == null)
701 { 710 {
702 float dw = dim.width / 1024.0f,
703 dh = dim.height / 768.0f;
704
705 // Rescale IDM GUI widgets
706 widgets.setScale(dw, dh);
707 G.screenDim = dim;
708
709 // Rescale background image
710 // Rescale fonts
711 G.fonts[1] = G.fonts[0].deriveFont(24f * dw);
712 G.fonts[2] = G.fonts[0].deriveFont(64f * dw);
713 G.fonts[3] = G.fonts[0].deriveFont(32f * dw);
714
715 dbg("Scale changed.\n"); 711 dbg("Scale changed.\n");
716 scaleChanged = true; 712 scaleChanged = true;
717 updateBoard = true; 713 updateBoard = true;
714
715 float dw = dim.width / 1024.0f,
716 dh = dim.height / 768.0f;
717
718 G.screenDim = dim;
719
720 // Rescale fonts
721 setupDerivedFonts(dw);
722
723 // Get font metrics against current Graphics2D context
724 G.metrics = new FontMetrics[G.numFonts];
725 for (int i = 0; i < G.numFonts; i++)
726 G.metrics[i] = g2.getFontMetrics(G.fonts[i]);
727
728 // Rescale IDM GUI widgets
729 widgets.setScale(dw, dh);
718 } 730 }
719 731
720 if (updateBoard) 732 if (updateBoard)
721 { 733 {
722 // dbg("updateBoard()\n"); 734 // dbg("updateBoard()\n");
734 gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null); 746 gimg.drawImage(G.lautaBG, 0, 0, dim.width, dim.height, null);
735 lauta.paintBackPlate(gimg); 747 lauta.paintBackPlate(gimg);
736 lauta.paintBoard(gimg, false); 748 lauta.paintBoard(gimg, false);
737 } 749 }
738 750
739 // Get font metrics against current Graphics2D context
740 if (G.metrics == null || scaleChanged)
741 {
742 G.metrics = new FontMetrics[G.numFonts];
743 for (int i = 0; i < G.numFonts; i++)
744 G.metrics[i] = g2.getFontMetrics(G.fonts[i]);
745 }
746 751
747 // Draw background image, pieces, widgets 752 // Draw background image, pieces, widgets
748 g2.drawImage(G.lautaBGScaled, 0, 0, null); 753 g2.drawImage(G.lautaBGScaled, 0, 0, null);
749 754
750 // Use antialiasing when rendering the game elements 755 // Use antialiasing when rendering the game elements
838 public void keyReleased(KeyEvent e) { } 843 public void keyReleased(KeyEvent e) { }
839 844
840 public void keyPressed(KeyEvent e) 845 public void keyPressed(KeyEvent e)
841 { 846 {
842 // Handle keyboard input 847 // Handle keyboard input
848
849 // About box is modal, so pass key events to it when active
843 if (widgets.containsObject(aboutBox)) 850 if (widgets.containsObject(aboutBox))
844 aboutBox.keyPressed(e); 851 aboutBox.keyPressed(e);
845 else 852 else
846 widgets.keyPressed(e); 853 widgets.keyPressed(e);
847 } 854 }