Added back changes from audio branch. byte[] is stored in base resource and different Resources can extract their data from it.

This commit is contained in:
2025-09-30 23:42:00 +02:00
parent 7c970d9a4d
commit d432887860
4 changed files with 53 additions and 52 deletions

View File

@@ -5,51 +5,23 @@ import java.io.*;
public class AudioAsset extends BaseResource implements LoadableResource { public class AudioAsset extends BaseResource implements LoadableResource {
private AudioInputStream audioInputStream = null;
private Clip clip = null;
private boolean isLoaded = false;
public AudioAsset(final File audioFile) { public AudioAsset(final File audioFile) {
super(audioFile); super(audioFile);
} }
public AudioInputStream getAudioStream() { // Gets a new clip to play
return this.audioInputStream; public Clip getNewClip() throws LineUnavailableException, UnsupportedAudioFileException, IOException {
// 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 // Generates a new audio stream from byte array
public void load() { private AudioInputStream getAudioStream() throws UnsupportedAudioFileException, IOException {
try { return AudioSystem.getAudioInputStream(this.getInputStream());
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);
}
} }
@Override
public void unload() {
this.clip.stop();
this.clip.flush();
this.clip.close();
this.clip = null;
this.isLoaded = false;
}
@Override
public boolean isLoaded() {
return this.isLoaded;
}
public Clip getClip() {
if (!this.isLoaded) this.load();
return this.clip;
}
} }

View File

@@ -1,27 +1,48 @@
package org.toop.framework.asset.resources; package org.toop.framework.asset.resources;
import java.io.*; import java.io.*;
import java.nio.file.Files;
public abstract class BaseResource { public abstract class BaseResource {
private byte[] rawData;
final InputStream stream;
final File file; final File file;
private boolean isLoaded = false;
BaseResource(final File file) { BaseResource(final File file) {
this.file = file; this.file = file;
}
public boolean isLoaded() {
return this.isLoaded;
}
public void load() throws FileNotFoundException{
try { try {
this.stream = new BufferedInputStream(new FileInputStream(file)); this.rawData = Files.readAllBytes(file.toPath());
} catch (FileNotFoundException e) { isLoaded = true;
} catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public InputStream getInputStream() { public void unload() {
return this.stream; this.rawData = null;
this.isLoaded = false;
} }
public File getFile() { public File getFile() {
return this.file; return this.file;
} }
public InputStream getInputStream() {
if (!isLoaded){
try {
this.load();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
return new BufferedInputStream(new ByteArrayInputStream(this.rawData));
}
} }

View File

@@ -13,9 +13,9 @@ public class ImageAsset extends BaseResource implements LoadableResource {
} }
@Override @Override
public void load() { public void load() throws FileNotFoundException {
if (!this.isLoaded) { if (!this.isLoaded) {
this.image = new Image(this.stream); this.image = new Image(this.getInputStream());
this.isLoaded = true; this.isLoaded = true;
} }
} }
@@ -32,7 +32,11 @@ public class ImageAsset extends BaseResource implements LoadableResource {
} }
public Image getImage() { public Image getImage() {
if (!this.isLoaded) load(); if (!this.isLoaded) try {
load();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
return image; return image;
} }
} }

View File

@@ -32,7 +32,11 @@ public class SoundManager {
} }
private void handlePlaySound(AudioEvents.PlayAudio event) { 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) { private void handleStopSound(AudioEvents.StopAudio event) {
@@ -45,7 +49,7 @@ public class SoundManager {
this.audioResources.put(audioAsset.getName(), audioAsset.getResource()); 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); AudioAsset asset = audioResources.get(audioFileName);
// Return -1 which indicates resource wasn't available // Return -1 which indicates resource wasn't available
@@ -54,7 +58,7 @@ public class SoundManager {
} }
// Get a new clip from resource // 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 supposed to loop make it loop, else just start it once
if (loop) { if (loop) {