diff --git a/framework/src/main/java/org/toop/framework/asset/resources/AudioAsset.java b/framework/src/main/java/org/toop/framework/asset/resources/AudioAsset.java index 6bcd809..9a4a679 100644 --- a/framework/src/main/java/org/toop/framework/asset/resources/AudioAsset.java +++ b/framework/src/main/java/org/toop/framework/asset/resources/AudioAsset.java @@ -6,51 +6,26 @@ import java.io.*; @FileExtension({"wav"}) public class AudioAsset extends BaseResource implements LoadableResource { - private AudioInputStream audioInputStream = null; - private Clip clip = null; - private boolean isLoaded = false; - - public AudioAsset(final File audioFile) { super(audioFile); } - public AudioInputStream getAudioStream() { - return this.audioInputStream; - } - - @Override - public void load() { - try { - this.audioInputStream = AudioSystem.getAudioInputStream(this.stream); - Clip clip = AudioSystem.getClip(); - clip.open(this.audioInputStream); - this.clip = clip; - this.isLoaded = true; - } catch (UnsupportedAudioFileException | LineUnavailableException e) { - throw new RuntimeException(e); - } catch (IOException e) { // TODO: Error handling - throw new RuntimeException(e); + // Gets a new clip to play + public Clip getNewClip() throws LineUnavailableException, UnsupportedAudioFileException, IOException { + if(!this.isLoaded()){ + this.load(); } + + // Get a new clip from audio system + Clip clip = AudioSystem.getClip(); + + // Insert a new audio stream into the clip + clip.open(this.getAudioStream()); + return clip; } - @Override - public void unload() { - this.clip.stop(); - this.clip.flush(); - this.clip.close(); - this.clip = null; - this.isLoaded = false; + // Generates a new audio stream from byte array + private AudioInputStream getAudioStream() throws UnsupportedAudioFileException, IOException { + return AudioSystem.getAudioInputStream(this.getInputStream()); } - - @Override - public boolean isLoaded() { - return this.isLoaded; - } - - public Clip getClip() { - if (!this.isLoaded) this.load(); - return this.clip; - } - } diff --git a/framework/src/main/java/org/toop/framework/asset/resources/BaseResource.java b/framework/src/main/java/org/toop/framework/asset/resources/BaseResource.java index 860cbf8..95ce7aa 100644 --- a/framework/src/main/java/org/toop/framework/asset/resources/BaseResource.java +++ b/framework/src/main/java/org/toop/framework/asset/resources/BaseResource.java @@ -1,27 +1,53 @@ package org.toop.framework.asset.resources; import java.io.*; +import java.nio.file.Files; public abstract class BaseResource { - - final InputStream stream; + private byte[] rawData; final File file; + private boolean isLoaded = false; BaseResource(final File file) { this.file = file; + } + + public boolean isLoaded() { + return this.isLoaded; + } + + public void load() throws FileNotFoundException { + this.loadRawData(); + isLoaded = true; + } + + private void loadRawData() throws FileNotFoundException{ try { - this.stream = new BufferedInputStream(new FileInputStream(file)); - } catch (FileNotFoundException e) { + this.rawData = Files.readAllBytes(file.toPath()); + } catch (IOException e) { throw new RuntimeException(e); } } - public InputStream getInputStream() { - return this.stream; + public void unload(){ + this.unloadRawData(); + isLoaded = false; + } + + private void unloadRawData() { + this.rawData = null; } public File getFile() { return this.file; } + public InputStream getInputStream() throws FileNotFoundException { + if (!isLoaded){ + // Manually load the data, makes sure it doesn't call subclass load() + loadRawData(); + isLoaded = true; + } + return new BufferedInputStream(new ByteArrayInputStream(this.rawData)); + } } diff --git a/framework/src/main/java/org/toop/framework/asset/resources/ImageAsset.java b/framework/src/main/java/org/toop/framework/asset/resources/ImageAsset.java index 0524bda..5f12d58 100644 --- a/framework/src/main/java/org/toop/framework/asset/resources/ImageAsset.java +++ b/framework/src/main/java/org/toop/framework/asset/resources/ImageAsset.java @@ -7,33 +7,31 @@ import java.io.FileNotFoundException; @FileExtension({"png"}) public class ImageAsset extends BaseResource implements LoadableResource { private Image image; - private boolean isLoaded = false; public ImageAsset(final File file) { super(file); } @Override - public void load() { - if (!this.isLoaded) { - this.image = new Image(this.stream); - this.isLoaded = true; + public void load() throws FileNotFoundException { + if (!this.isLoaded()) { + super.load(); // Make sure that base class (byte[]) is loaded + this.image = new Image(this.getInputStream()); } } @Override public void unload() { this.image = null; - this.isLoaded = false; - } - - @Override - public boolean isLoaded() { - return this.isLoaded; + super.unload(); } public Image getImage() { - if (!this.isLoaded) load(); + if (!this.isLoaded()) try { + load(); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } return image; } } diff --git a/framework/src/main/java/org/toop/framework/audio/SoundManager.java b/framework/src/main/java/org/toop/framework/audio/SoundManager.java index e4155cb..2727358 100644 --- a/framework/src/main/java/org/toop/framework/audio/SoundManager.java +++ b/framework/src/main/java/org/toop/framework/audio/SoundManager.java @@ -31,7 +31,11 @@ public class SoundManager { } private void handlePlaySound(AudioEvents.PlayAudio event) { - this.playSound(event.fileNameNoExtensionAndNoDirectory(), event.loop()); + try { + this.playSound(event.fileNameNoExtensionAndNoDirectory(), event.loop()); + } catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) { + throw new RuntimeException(e); + } } private void handleStopSound(AudioEvents.StopAudio event) { @@ -44,7 +48,7 @@ public class SoundManager { this.audioResources.put(audioAsset.getName(), audioAsset.getResource()); } - private long playSound(String audioFileName, boolean loop) { + private long playSound(String audioFileName, boolean loop) throws UnsupportedAudioFileException, LineUnavailableException, IOException { AudioAsset asset = audioResources.get(audioFileName); // Return -1 which indicates resource wasn't available @@ -53,7 +57,7 @@ public class SoundManager { } // Get a new clip from resource - Clip clip = asset.getClip(); + Clip clip = asset.getNewClip(); // If supposed to loop make it loop, else just start it once if (loop) {