Add Sound support to Loader and Makefile.

This commit is contained in:
Jan Philipp Timme 2014-11-05 09:02:44 +01:00
parent 1f65979f41
commit 2f6eddcb46
4 changed files with 61 additions and 8 deletions

View File

@ -5,7 +5,7 @@ game.jar: clean
find src/ -type f -name '*.java' -print0 | xargs -0 javac -cp src/ -d compiled/
cp -r res/* compiled/
echo 'Main-Class: de.teamteamteam.spacescooter.Main' >compiled/manifest.txt
jar cvfe game.jar 'de.teamteamteam.spacescooter.Main' -C compiled de -C compiled images
jar cvfe game.jar 'de.teamteamteam.spacescooter.Main' -C compiled de -C compiled images -C compiled sounds
run: game.jar
java -jar game.jar

BIN
res/sounds/abgang.wav Normal file

Binary file not shown.

View File

@ -29,6 +29,7 @@ public class Main {
GameConfig.windowWidth = 800;
GameConfig.windowHeight = 600;
//Instantiate the GameFrame
final GameFrame gameFrame = new GameFrame();
//Initialize SuperScreen and add to GameFrame, so we can call doPaint() on it.

View File

@ -1,20 +1,37 @@
package de.teamteamteam.spacescooter.utility;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import de.teamteamteam.spacescooter.screen.LoadingScreen;
/**
* This Loader prefetches all required resources for the Game, such as images or
* sounds.
*/
public class Loader {
/**
* HashTable containing loaded BufferedImages
*/
private static Hashtable<String, BufferedImage> images;
/**
* HashTable containing loaded AudioInputStreams
*/
private static Hashtable<String, AudioInputStream> sounds;
static {
Loader.images = new Hashtable<String, BufferedImage>();
Loader.sounds = new Hashtable<String, AudioInputStream>();
}
@ -25,7 +42,7 @@ public class Loader {
/**
* Return the loaded BufferedImage by its filename.
* Return the loaded BufferedImage by its relative filename.
*/
public static BufferedImage getBufferedImageByFilename(String filename) {
if(CodeEnvironment.isJar()) {
@ -36,7 +53,19 @@ public class Loader {
}
/**
* Perform the initial loading of everything and show the progress on the given LoadingScreen.
* Return the loaded AudioInputStream by its relative filename.
*/
public static AudioInputStream getAudioInputStreamByFilename(String filename) {
if(CodeEnvironment.isJar()) {
return Loader.sounds.get(filename);
} else {
return Loader.sounds.get(filename.replace("/", File.separator));
}
}
/**
* Perform the initial loading of everything and show the progress on the
* given LoadingScreen.
*/
public static void load(LoadingScreen loadingScreen) {
// get a list of stuff to load and create buffered images
@ -46,15 +75,21 @@ public class Loader {
for(int i=0; i<elements.length; i++) {
String e = elements[i];
if(e.endsWith(".png")) {
if (GameConfig.DEBUG) System.out.println("Creating BufferedImage for: " + e);
if(GameConfig.DEBUG)
System.out.println("Creating BufferedImage for: " + e);
Loader.addBufferedImageByFilename(e);
}
if(e.endsWith(".wav")) {
if(GameConfig.DEBUG)
System.out.println("Creating AudioInputStream for: " + e);
Loader.addAudioInputStreamByFilename(e);
}
loadingScreen.increaseCurrentProcessed();
}
}
/**
* Load a BufferedImage by filename.
* Load a BufferedImage by relative filename.
*/
private static void addBufferedImageByFilename(String filename) {
try {
@ -67,4 +102,21 @@ public class Loader {
}
/**
* Load an AudioInputStream by relative filename.
*/
private static void addAudioInputStreamByFilename(String filename) {
try {
AudioInputStream sound = AudioSystem.getAudioInputStream(new BufferedInputStream(Loader.class.getClassLoader().getResourceAsStream(filename)));
Loader.sounds.put(filename, sound);
} catch (IOException e) {
System.err.println("Unable to load AudioInputStream: " + filename);
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
System.err.println("Unsupported AudioFormat in file: " + filename);
e.printStackTrace();
}
}
}