changeset 22:afde253ec705

More work.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 31 Jan 2011 21:28:59 +0200
parents df494a65bf8c
children 0741dc117808
files game/Engine.java game/Sound.java game/SoundElement.java
diffstat 3 files changed, 243 insertions(+), 213 deletions(-) [+]
line wrap: on
line diff
--- a/game/Engine.java	Mon Jan 31 19:46:41 2011 +0200
+++ b/game/Engine.java	Mon Jan 31 21:28:59 2011 +0200
@@ -15,211 +15,6 @@
 import java.io.*;
 import game.*;
 
-import javax.sound.sampled.*;
-
-
-enum Sound
-{
-    PIECE_PLACED("placed.wav", true),
-
-    MUSIC_GAME1("gamemusic.wav", false);
-
-
-    private final String name;
-    private final boolean effect;
-
-    Sound(String name, boolean effect)
-    {
-        this.name = name;
-        this.effect = effect;
-    }
-
-    public String getName()
-    {
-        return this.name;
-    }
-    
-    public boolean isEffect()
-    {
-        return effect;
-    }
-}
-
-
-class SoundElement implements Runnable
-{
-    private final String name;
-    private Clip clip;
-    private AudioInputStream stream;
-    private AudioFormat format;
-    private SourceDataLine line;
-    private Thread playThread;
-    private int loopCount;
-    private boolean doPlay;
-    
-    SoundElement(String filename, boolean effect) throws IOException
-    {
-        this.name = filename;
-
-        ResourceLoader res = new ResourceLoader(name);
-        if (res == null || res.getStream() == null)
-        {
-            throw new IOException("Could not load audio resource '"+name+"'.\n");
-        }
-
-        try {
-            stream = AudioSystem.getAudioInputStream(res.getStream());
-        }
-        catch (UnsupportedAudioFileException e) {
-            throw new IOException("Unsupported audio file format for '"+name+"'.\n");
-        }
-        catch (IOException e)
-        {
-            throw new IOException("Could not load audio resource '"+name+"'.\n");
-        }
-
-        format = stream.getFormat();
-
-        if (effect) {
-            System.out.print("Loading '"+name+"' as a clip\n");
-            try {
-                clip = AudioSystem.getClip();
-                clip.open(stream);
-            }
-            catch (LineUnavailableException e)
-            {
-                throw new IOException("Line unavailable for '"+name+"'.\n");
-            }
-            finally {
-                stream.close();
-            }
-        }
-        else
-        {
-            clip = null;
-            System.out.print("Loading '"+name+"' as stream\n");
-            
-            try {
-                SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
-                System.out.print("info: "+stream.getFrameLength() + ", " + format.getFrameSize() + "\n");
-                line = (SourceDataLine) AudioSystem.getLine(info);
-                line.open(format);
-            }
-            catch (LineUnavailableException e) {
-                throw new IOException("Line unavailable for '"+name+"'.\n");
-            }
-        }
-    }
-
-    public boolean isClip()
-    {
-        return (clip != null && line == null);
-    }
-
-    public void play()
-    {
-        System.out.print("Sound("+name+").play()\n");
-        if (isClip())
-        {
-            clip.setFramePosition(0);
-            clip.start();
-        }
-        else
-        {
-            if (playThread == null)
-            {
-                doPlay = true;
-                loopCount = 1;
-                playThread = new Thread(this);
-                playThread.start();
-            }
-        }
-    }
-    
-    public void loop(int n)
-    {
-        System.out.print("Sound("+name+").loop("+n+")\n");
-        if (isClip())
-        {
-            clip.setFramePosition(0);
-            if (n < 0)
-                clip.loop(Clip.LOOP_CONTINUOUSLY);
-            else
-                clip.loop(n);
-        }
-        else
-        {
-            if (playThread == null)
-            {
-                doPlay = true;
-                loopCount = n;
-                playThread = new Thread(this);
-                playThread.start();
-            }
-        }
-    }
-    
-    public void stop()
-    {
-        if (isClip())
-        {
-            if (clip.isRunning())
-                clip.stop();
-        }
-        else
-        {
-            if (playThread != null)
-            {
-                playThread.interrupt();
-                doPlay = false;
-                playThread = null;
-            }
-        }
-    }
-    
-    public boolean isPlaying()
-    {
-        if (isClip())
-            return clip.isRunning();
-        else
-            return (playThread != null && doPlay);
-    }
-
-    public void run()
-    {
-        line.start();
-        byte[] buf = new byte[line.getBufferSize()];
-
-        while (doPlay && (loopCount > 0 || loopCount == -1))
-        {
-            try {
-                int numRead = 0;
-                while ((numRead = stream.read(buf, 0, buf.length)) >= 0 && doPlay)
-                {
-                    int offset = 0;
-                    while (offset < numRead)
-                    {
-                        System.out.print("audioThread: offs="+offset+", numread="+numRead+"\n");
-                        offset += line.write(buf, offset, numRead - offset);
-                    }
-                }
-                line.drain();
-
-                System.out.print("audioThread: stream.reset()\n");
-                stream.reset();
-            }
-            catch (IOException e) {
-            }
-
-            if (loopCount > 0)
-                loopCount--;
-        }
-        
-        line.stop();
-        doPlay = false;
-    }
-}
-
 
 class PathInfo
 {
@@ -405,12 +200,14 @@
     BufferedImage lautaBG = null, lautaBGScaled = null;
     Dimension oldDim;
     float clock;
+
+/*
     SoundElement[] sounds;
-
     public SoundElement snd(Sound snd)
     {
         return sounds[snd.ordinal()];
     }
+*/
 
     public Engine()
     {
@@ -424,12 +221,14 @@
             ResourceLoader res = new ResourceLoader("graphics/board.png");
             lautaBG = ImageIO.read(res.getStream());
 
+/*
             sounds = new SoundElement[16];
             for (Sound s : Sound.values())
             {
                 System.out.print(s +" = "+ s.ordinal() +"\n");
                 sounds[s.ordinal()] = new SoundElement("sounds/" + s.getName(), s.isEffect());
             }
+*/
         }
         catch (IOException e)
         {
@@ -452,9 +251,10 @@
             System.out.print("Engine(): requesting focus\n");
             requestFocus();
         }
-        
+
+/*        
         snd(Sound.MUSIC_GAME1).loop(-1);
-//        snd(Sound.PIECE_PLACED).loop(-1);
+*/
     }
 
     public void startThreads()
@@ -477,6 +277,14 @@
             animEnable = false;
             animThread = null;
         }
+
+/*
+        for (Sound s : Sound.values())
+        {
+            if (snd(s) != null)
+                snd(s).stop();
+        }
+*/
     }
 
     public void mousePressed(MouseEvent e) { }
@@ -533,7 +341,6 @@
    
     public void keyPressed(KeyEvent e)
     {
-        System.out.print("running "+ snd(Sound.MUSIC_GAME1) + "\n");
         switch (e.getKeyCode())
         {
             case KeyEvent.VK_LEFT:
@@ -548,8 +355,8 @@
 
             case KeyEvent.VK_ENTER:
                 lauta.pieceFinishTurn();
-                snd(Sound.PIECE_PLACED).stop();
-                snd(Sound.PIECE_PLACED).play();
+//                snd(Sound.PIECE_PLACED).stop();
+//                snd(Sound.PIECE_PLACED).play();
                 break;
         }
     }
@@ -559,8 +366,6 @@
         while (animEnable)
         {
             clock++;
-            
-//            System.out.print("clock=" + clock + "\n");
 
             lauta.animate(clock);
             
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game/Sound.java	Mon Jan 31 21:28:59 2011 +0200
@@ -0,0 +1,36 @@
+/*
+ * Ristipolku Game Engine
+ * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
+ */
+package game;
+
+
+public enum Sound
+{
+    // Music loops
+    MUSIC_GAME1	   ("gamemusic.wav",  true),
+
+    // Sound effects
+    PIECE_PLACED   ("placed.wav",     false);
+
+
+
+    private final String name;
+    private final boolean effect;
+
+    Sound(String name, boolean streaming)
+    {
+        this.name = name;
+        this.streaming = streaming;
+    }
+
+    public String getName()
+    {
+        return this.name;
+    }
+    
+    public boolean isStreaming()
+    {
+        return streaming;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game/SoundElement.java	Mon Jan 31 21:28:59 2011 +0200
@@ -0,0 +1,189 @@
+/*
+ * Ristipolku Game Engine
+ * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
+ */
+package game;
+
+import java.util.*;
+import java.io.*;
+import game.*;
+import javax.sound.sampled.*;
+
+
+class SoundElement implements Runnable
+{
+    private final String name;
+
+    private AudioInputStream stream;
+    private AudioFormat format;
+    private SourceDataLine line;
+    private Thread playThread;
+    private int loopCount;
+    private boolean playing, streaming;
+    private int volume = 100;
+
+    byte[] buffer;
+    int length;
+    
+    
+    SoundElement(String filename, boolean streaming) throws IOException
+    {
+        this.name = filename;
+        this.streaming = streaming;
+
+        ResourceLoader res = new ResourceLoader(name);
+        if (res == null || res.getStream() == null)
+        {
+            throw new IOException("Could not load audio resource '"+name+"'.\n");
+        }
+
+
+        try {
+            stream = AudioSystem.getAudioInputStream(res.getStream());
+            format = stream.getFormat();
+        }
+        catch (UnsupportedAudioFileException e) {
+            throw new IOException("Unsupported audio file format for '"+name+"'.\n");
+        }
+        catch (IOException e)
+        {
+            throw new IOException("Could not load audio resource '"+name+"'.\n");
+        }
+
+
+        if (streaming) {
+            System.out.print("Loading '"+name+"' as a clip\n");
+            try {
+                clip = AudioSystem.getClip();
+                clip.open(stream);
+            }
+            catch (LineUnavailableException e)
+            {
+                throw new IOException("Line unavailable for '"+name+"'.\n");
+            }
+            finally {
+                stream.close();
+            }
+        }
+        else
+        {
+            clip = null;
+            System.out.print("Loading '"+name+"' as stream\n");
+            
+            try {
+                SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
+                System.out.print("info: "+stream.getFrameLength() + ", " + format.getFrameSize() + "\n");
+                line = (SourceDataLine) AudioSystem.getLine(info);
+                line.open(format);
+            }
+            catch (LineUnavailableException e) {
+                throw new IOException("Line unavailable for '"+name+"'.\n");
+            }
+        }
+    }
+
+    public void play()
+    {
+        System.out.print("Sound("+name+").play()\n");
+        if (streaming)
+        {
+            clip.setFramePosition(0);
+            clip.start();
+        }
+        else
+        {
+            if (playThread == null)
+            {
+                doPlay = true;
+                loopCount = 1;
+                playThread = new Thread(this);
+                playThread.start();
+            }
+        }
+    }
+    
+    public void loop(int n)
+    {
+        System.out.print("Sound("+name+").loop("+n+")\n");
+        if (isClip())
+        {
+            clip.setFramePosition(0);
+            if (n < 0)
+                clip.loop(Clip.LOOP_CONTINUOUSLY);
+            else
+                clip.loop(n);
+        }
+        else
+        {
+            if (playThread == null)
+            {
+                doPlay = true;
+                loopCount = n;
+                playThread = new Thread(this);
+                playThread.start();
+            }
+        }
+    }
+    
+    public void stop()
+    {
+        if (isClip())
+        {
+            if (clip.isRunning())
+                clip.stop();
+        }
+        else
+        {
+            if (playThread != null)
+            {
+                playThread.interrupt();
+                doPlay = false;
+                playThread = null;
+            }
+        }
+    }
+    
+    public boolean isPlaying()
+    {
+        if (isClip())
+            return clip.isRunning();
+        else
+            return (playThread != null && line.isRunning());
+    }
+
+    public void run()
+    {
+        line.start();
+        byte[] buf = new byte[line.getBufferSize()];
+
+        while (doPlay && (loopCount > 0 || loopCount == -1))
+        {
+            try {
+                int numRead = 0;
+                while ((numRead = stream.read(buf, 0, buf.length)) >= 0 && doPlay)
+                {
+                    int offset = 0;
+                    while (offset < numRead)
+                    {
+                        System.out.print("audioThread: offs="+offset+", numread="+numRead+"\n");
+                        offset += line.write(buf, offset, numRead - offset);
+                    }
+                }
+                line.drain();
+
+                System.out.print("audioThread: stream.reset()\n");
+                stream.reset();
+            }
+            catch (IOException e) {
+            }
+
+            if (loopCount > 0)
+                loopCount--;
+        }
+        
+        line.stop();
+        doPlay = false;
+    }
+}
+
+