Merge remote-tracking branch 'origin/assetManager' into assetManager

This commit is contained in:
lieght
2025-10-01 00:13:04 +02:00
4 changed files with 63 additions and 60 deletions

View File

@@ -6,51 +6,26 @@ import java.io.*;
@FileExtension({"wav"}) @FileExtension({"wav"})
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 {
if(!this.isLoaded()){
this.load();
} }
@Override // Get a new clip from audio system
public void load() {
try {
this.audioInputStream = AudioSystem.getAudioInputStream(this.stream);
Clip clip = AudioSystem.getClip(); Clip clip = AudioSystem.getClip();
clip.open(this.audioInputStream);
this.clip = clip; // Insert a new audio stream into the clip
this.isLoaded = true; clip.open(this.getAudioStream());
} catch (UnsupportedAudioFileException | LineUnavailableException e) { return clip;
throw new RuntimeException(e);
} catch (IOException e) { // TODO: Error handling
throw new RuntimeException(e);
}
} }
@Override // Generates a new audio stream from byte array
public void unload() { private AudioInputStream getAudioStream() throws UnsupportedAudioFileException, IOException {
this.clip.stop(); return AudioSystem.getAudioInputStream(this.getInputStream());
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,53 @@
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 {
this.loadRawData();
isLoaded = true;
}
private void loadRawData() throws FileNotFoundException{
try { try {
this.stream = new BufferedInputStream(new FileInputStream(file)); this.rawData = Files.readAllBytes(file.toPath());
} catch (FileNotFoundException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public InputStream getInputStream() { public void unload(){
return this.stream; this.unloadRawData();
isLoaded = false;
}
private void unloadRawData() {
this.rawData = null;
} }
public File getFile() { public File getFile() {
return this.file; 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));
}
} }

View File

@@ -7,33 +7,31 @@ import java.io.FileNotFoundException;
@FileExtension({"png"}) @FileExtension({"png"})
public class ImageAsset extends BaseResource implements LoadableResource { public class ImageAsset extends BaseResource implements LoadableResource {
private Image image; private Image image;
private boolean isLoaded = false;
public ImageAsset(final File file) { public ImageAsset(final File file) {
super(file); super(file);
} }
@Override @Override
public void load() { public void load() throws FileNotFoundException {
if (!this.isLoaded) { if (!this.isLoaded()) {
this.image = new Image(this.stream); super.load(); // Make sure that base class (byte[]) is loaded
this.isLoaded = true; this.image = new Image(this.getInputStream());
} }
} }
@Override @Override
public void unload() { public void unload() {
this.image = null; this.image = null;
this.isLoaded = false; super.unload();
}
@Override
public boolean isLoaded() {
return this.isLoaded;
} }
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

@@ -31,7 +31,11 @@ public class SoundManager {
} }
private void handlePlaySound(AudioEvents.PlayAudio event) { private void handlePlaySound(AudioEvents.PlayAudio event) {
try {
this.playSound(event.fileNameNoExtensionAndNoDirectory(), event.loop()); 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) {
@@ -44,7 +48,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
@@ -53,7 +57,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) {