changeset 25:bbac3e4a4b9b

Cleanups, etc.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 01 Feb 2011 18:05:19 +0200
parents 1be98362e5e9
children 3d4cc47df31a
files Makefile game/Engine.java
diffstat 2 files changed, 71 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Feb 01 16:22:46 2011 +0200
+++ b/Makefile	Tue Feb 01 18:05:19 2011 +0200
@@ -1,7 +1,7 @@
 # Settings, directories
 RUN=Ristipolku.class
 
-RESOURCES=graphics/*.png graphics/*.jpg sounds/*.wav
+RESOURCES=graphics/*.png graphics/*.jpg graphics/font.ttf sounds/*.wav
 
 CLASSES=game/Piece.java game/PieceType.java game/Engine.java game/Interpolate.java game/ResourceLoader.java
 
--- a/game/Engine.java	Tue Feb 01 16:22:46 2011 +0200
+++ b/game/Engine.java	Tue Feb 01 18:05:19 2011 +0200
@@ -9,6 +9,7 @@
 import java.awt.event.*;
 import java.awt.image.*;
 import java.awt.event.*;
+import java.awt.font.*;
 import javax.imageio.*;
 import javax.swing.*;
 import java.util.*;
@@ -194,14 +195,18 @@
 public class Engine extends JPanel
                     implements Runnable, KeyListener, MouseListener
 {
+    long startTime;
+    float gameClock, gameFrames;
     Thread animThread;
     boolean animEnable = false;
+
+    Font fontMain, font1, font2;
     GameBoard lauta = null;
     BufferedImage lautaBG = null, lautaBGScaled = null;
-    Dimension oldDim;
-    float clock, frames;
+    Dimension lautaDim;
 
     SoundElement[] sounds;
+
     public SoundElement snd(Sound snd)
     {
         return sounds[snd.ordinal()];
@@ -209,16 +214,31 @@
 
     public Engine()
     {
-        BufferedImage img;
-        clock = 0;
+        // Initialize globals
+        System.out.print("Engine() constructor\n");
+        
+        gameClock = 0;
+        gameFrames = 0;
+        startTime = new Date().getTime();
 
-        System.out.print("Engine() constructor\n");
 
+        // Load resources
         try
         {
             ResourceLoader res = new ResourceLoader("graphics/board.jpg");
             lautaBG = ImageIO.read(res.getStream());
 
+            try {
+                res = new ResourceLoader("graphics/font.ttf");
+                fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream());
+                font1 = fontMain.deriveFont(24f);
+                font2 = fontMain.deriveFont(32f);
+            }
+            catch (FontFormatException e)
+            {
+                System.out.print("Could not initialize fonts.\n");
+            }
+
             sounds = new SoundElement[16];
             for (Sound s : Sound.values())
             {
@@ -251,6 +271,47 @@
         snd(Sound.MUSIC_GAME1).loop(-1);
     }
 
+
+    public void paintComponent(Graphics g)
+    {
+        Graphics2D g2 = (Graphics2D) g;
+        
+        // Use antialiasing when rendering the game elements
+        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+                            RenderingHints.VALUE_ANTIALIAS_ON);
+
+        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
+                            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+
+        // Rescale background if component size has changed
+        Dimension dim = getSize();
+        if (lautaDim == null || !dim.equals(lautaDim))
+        {
+            lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
+            Graphics2D gimg = lautaBGScaled.createGraphics();
+            gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+                                  RenderingHints.VALUE_INTERPOLATION_BICUBIC);
+
+            gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); 
+            lautaDim = dim;
+
+            System.out.print("scale changed\n");
+        }
+        
+        // Background, pieces
+        g2.drawImage(lautaBGScaled, 0, 0, null);
+        lauta.paint(g2, 100, 150, 60);
+
+        // Scores
+        g2.setFont(font1);
+        g2.setPaint(Color.white);
+        
+        
+        // Other elements
+        long currTime = new Date().getTime();
+        g2.drawString("fps = "+ ((currTime - startTime) / gameFrames), dim.width - 120, 20);
+    }
+
     public void startThreads()
     {
         System.out.print("startThreads()\n");
@@ -294,35 +355,6 @@
         }
     }
 
-    public void paintComponent(Graphics g)
-    {
-        Graphics2D g2 = (Graphics2D) g;
-        
-        // Use antialiasing when rendering the game elements
-        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
-                            RenderingHints.VALUE_ANTIALIAS_ON);
-
-        // Rescale background if component size has changed
-        Dimension dim = getSize();
-        if (oldDim == null || !dim.equals(oldDim))
-        {
-            lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
-            Graphics2D gimg = lautaBGScaled.createGraphics();
-            gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
-                                  RenderingHints.VALUE_INTERPOLATION_BICUBIC);
-
-            gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); 
-            oldDim = dim;
-            System.out.print("scale changed\n");
-        }
-        
-        // Background, pieces
-        g2.drawImage(lautaBGScaled, 0, 0, null);
-        lauta.paint(g2, 100, 150, 60);
-
-        // Other elements
-    }
-
     public void keyTyped(KeyEvent e)
     {
     }
@@ -357,17 +389,14 @@
     {
         while (animEnable)
         {
-            clock++;
+            gameClock++;
 
-            lauta.animate(clock);
+            lauta.animate(gameClock);
             
-            if (clock % 3 == 1)
+            if (gameClock % 3 == 1)
             {
                 repaint();
-                frames++;
-                
-                if (frames % 10 == 1)
-                System.out.print("fps = "+ ((clock * 10) / frames) +"\n");
+                gameFrames++;
             }
             
             try {