diff --git a/src/sig/DrawLoop.java b/src/sig/DrawLoop.java index 6fce3fe..56ae550 100644 --- a/src/sig/DrawLoop.java +++ b/src/sig/DrawLoop.java @@ -11,8 +11,6 @@ import sig.engine.Rectangle; import sig.engine.Sprite; import sig.engine.String; import sig.engine.Transform; -import sig.map.Map; -import sig.objects.LevelRenderer; public class DrawLoop { public static Panel panel; diff --git a/src/sig/RabiClone.java b/src/sig/RabiClone.java index 6bcbbac..d2d805a 100644 --- a/src/sig/RabiClone.java +++ b/src/sig/RabiClone.java @@ -12,7 +12,6 @@ import java.util.List; import sig.engine.Panel; import sig.engine.Point; -import sig.engine.Sound; import sig.engine.Transform; import sig.engine.objects.AnimatedObject; import sig.engine.objects.Object; @@ -26,6 +25,7 @@ import sig.objects.actor.RenderedObject; import sig.engine.Action; import sig.engine.Key; import sig.engine.KeyBind; +import sig.engine.MusicPlayer; import sig.engine.PaletteColor; import java.awt.Toolkit; @@ -82,6 +82,8 @@ public class RabiClone{ public static RenderingHints RENDERHINTS = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); + public static MusicPlayer MUSICPLAYER; + public static void main(String[] args) { //Sound s = new Sound(); System.setProperty("sun.java2d.transaccel", "True"); diff --git a/src/sig/engine/MusicPlayer.java b/src/sig/engine/MusicPlayer.java new file mode 100644 index 0000000..2f426e4 --- /dev/null +++ b/src/sig/engine/MusicPlayer.java @@ -0,0 +1,29 @@ +package sig.engine; + +import java.util.List; + +import javax.sound.sampled.Clip; +import javax.sound.sampled.LineEvent; +import javax.sound.sampled.LineListener; +import javax.sound.sampled.LineEvent.Type; + +public class MusicPlayer implements LineListener{ + public static SongThread SongThread; + public static List SoundEffects; + public static boolean songChangeRequested=false; + + public static void init() { + SongThread = new SongThread(); + } + public static void changeMusic(Song newSong) { + SongThread.currentSong=newSong; + MusicPlayer.songChangeRequested=true; + SongThread.run(); + } + @Override + public void update(LineEvent event) { + if (event.getType()==Type.CLOSE||event.getType()==Type.STOP) { + SongThread.run(); //Cause a loop so the music continues playing. + } + } +} diff --git a/src/sig/engine/Song.java b/src/sig/engine/Song.java new file mode 100644 index 0000000..d767abc --- /dev/null +++ b/src/sig/engine/Song.java @@ -0,0 +1,22 @@ +package sig.engine; + +import java.io.File; +import java.io.IOException; + +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.SourceDataLine; +import javax.sound.sampled.UnsupportedAudioFileException; + +public enum Song{ + WOOLOOKOLOGIE(new File("..","Woolookologie.wav")); + + File songFile; + + public Song(File f) { + this.songFile=songFile; + } +} \ No newline at end of file diff --git a/src/sig/engine/SongThread.java b/src/sig/engine/SongThread.java new file mode 100644 index 0000000..a2981ee --- /dev/null +++ b/src/sig/engine/SongThread.java @@ -0,0 +1,51 @@ +package sig.engine; + +import java.io.IOException; + +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.SourceDataLine; +import javax.sound.sampled.UnsupportedAudioFileException; + +import sig.RabiClone; + +public class SongThread extends Thread{ + Song currentSong; + AudioInputStream audioStream; + AudioFormat format; + DataLine.Info info; + SourceDataLine audioLine; + public void run() { + try { + if (audioStream!=null) { + audioLine.close(); + audioStream.close(); + audioStream=null; + } + audioStream = AudioSystem.getAudioInputStream(audioStream); + format = audioStream.getFormat(); + info = new DataLine.Info(SourceDataLine.class,format); + audioLine = (SourceDataLine)AudioSystem.getLine(info); + audioLine.addLineListener(RabiClone.MUSICPLAYER); + audioLine.open(format); + audioLine.start(); + final int BUFFER_SIZE=4096; + byte[] bytesBuffer = new byte[BUFFER_SIZE]; + int bytesRead=-1; + while ((bytesRead=audioStream.read(bytesBuffer))!=-1&&!MusicPlayer.songChangeRequested) { + audioLine.write(bytesBuffer,0,bytesRead); + } + if (!MusicPlayer.songChangeRequested) { + audioLine.drain(); + } else { + audioLine.stop(); + audioLine.flush(); + } + } catch (IOException | UnsupportedAudioFileException | LineUnavailableException e) { + e.printStackTrace(); + } + } +} diff --git a/src/sig/engine/Sound.java b/src/sig/engine/Sound.java deleted file mode 100644 index 03ceaa4..0000000 --- a/src/sig/engine/Sound.java +++ /dev/null @@ -1,39 +0,0 @@ -package sig.engine; - -import java.io.File; -import java.io.IOException; - -import javax.sound.sampled.AudioFormat; -import javax.sound.sampled.AudioInputStream; -import javax.sound.sampled.AudioSystem; -import javax.sound.sampled.DataLine; -import javax.sound.sampled.LineUnavailableException; -import javax.sound.sampled.SourceDataLine; -import javax.sound.sampled.UnsupportedAudioFileException; - -public class Sound{ - public Sound() { - try { - File audioFile = new File("..","Woolookologie.wav"); - AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); - AudioFormat format = audioStream.getFormat(); - DataLine.Info info = new DataLine.Info(SourceDataLine.class,format); - SourceDataLine audioLine = (SourceDataLine)AudioSystem.getLine(info); - audioLine.open(format); - audioLine.start(); - final int BUFFER_SIZE=4096; - byte[] bytesBuffer = new byte[BUFFER_SIZE]; - int bytesRead=-1; - while ((bytesRead=audioStream.read(bytesBuffer))!=-1) { - audioLine.write(bytesBuffer,0,bytesRead); - } - - audioLine.drain(); - audioLine.close(); - audioStream.close(); - } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) { - e.printStackTrace(); - } - - } -} \ No newline at end of file diff --git a/src/sig/objects/BunnyGirls.java b/src/sig/objects/BunnyGirls.java index b53c8b8..ab7b0e3 100644 --- a/src/sig/objects/BunnyGirls.java +++ b/src/sig/objects/BunnyGirls.java @@ -1,6 +1,5 @@ package sig.objects; -import sig.engine.Alpha; import sig.engine.AnimatedSprite; import sig.engine.Panel; import sig.engine.Rectangle;