Add Sound support to Loader and Makefile.
This commit is contained in:
parent
1f65979f41
commit
2f6eddcb46
2
Makefile
2
Makefile
|
@ -5,7 +5,7 @@ game.jar: clean
|
||||||
find src/ -type f -name '*.java' -print0 | xargs -0 javac -cp src/ -d compiled/
|
find src/ -type f -name '*.java' -print0 | xargs -0 javac -cp src/ -d compiled/
|
||||||
cp -r res/* compiled/
|
cp -r res/* compiled/
|
||||||
echo 'Main-Class: de.teamteamteam.spacescooter.Main' >compiled/manifest.txt
|
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
|
run: game.jar
|
||||||
java -jar game.jar
|
java -jar game.jar
|
||||||
|
|
Binary file not shown.
|
@ -29,6 +29,7 @@ public class Main {
|
||||||
GameConfig.windowWidth = 800;
|
GameConfig.windowWidth = 800;
|
||||||
GameConfig.windowHeight = 600;
|
GameConfig.windowHeight = 600;
|
||||||
|
|
||||||
|
//Instantiate the GameFrame
|
||||||
final GameFrame gameFrame = new GameFrame();
|
final GameFrame gameFrame = new GameFrame();
|
||||||
|
|
||||||
//Initialize SuperScreen and add to GameFrame, so we can call doPaint() on it.
|
//Initialize SuperScreen and add to GameFrame, so we can call doPaint() on it.
|
||||||
|
|
|
@ -1,23 +1,40 @@
|
||||||
package de.teamteamteam.spacescooter.utility;
|
package de.teamteamteam.spacescooter.utility;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
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;
|
import de.teamteamteam.spacescooter.screen.LoadingScreen;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This Loader prefetches all required resources for the Game, such as images or
|
||||||
|
* sounds.
|
||||||
|
*/
|
||||||
public class Loader {
|
public class Loader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HashTable containing loaded BufferedImages
|
||||||
|
*/
|
||||||
private static Hashtable<String, BufferedImage> images;
|
private static Hashtable<String, BufferedImage> images;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HashTable containing loaded AudioInputStreams
|
||||||
|
*/
|
||||||
|
private static Hashtable<String, AudioInputStream> sounds;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Loader.images = new Hashtable<String, BufferedImage>();
|
Loader.images = new Hashtable<String, BufferedImage>();
|
||||||
|
Loader.sounds = new Hashtable<String, AudioInputStream>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private constructor, this class will never be instantiated.
|
* Private constructor, this class will never be instantiated.
|
||||||
*/
|
*/
|
||||||
|
@ -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) {
|
public static BufferedImage getBufferedImageByFilename(String filename) {
|
||||||
if(CodeEnvironment.isJar()) {
|
if(CodeEnvironment.isJar()) {
|
||||||
|
@ -34,9 +51,21 @@ public class Loader {
|
||||||
return Loader.images.get(filename.replace("/", File.separator));
|
return Loader.images.get(filename.replace("/", File.separator));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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) {
|
public static void load(LoadingScreen loadingScreen) {
|
||||||
// get a list of stuff to load and create buffered images
|
// 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++) {
|
for(int i=0; i<elements.length; i++) {
|
||||||
String e = elements[i];
|
String e = elements[i];
|
||||||
if(e.endsWith(".png")) {
|
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);
|
Loader.addBufferedImageByFilename(e);
|
||||||
}
|
}
|
||||||
|
if(e.endsWith(".wav")) {
|
||||||
|
if(GameConfig.DEBUG)
|
||||||
|
System.out.println("Creating AudioInputStream for: " + e);
|
||||||
|
Loader.addAudioInputStreamByFilename(e);
|
||||||
|
}
|
||||||
loadingScreen.increaseCurrentProcessed();
|
loadingScreen.increaseCurrentProcessed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load a BufferedImage by filename.
|
* Load a BufferedImage by relative filename.
|
||||||
*/
|
*/
|
||||||
private static void addBufferedImageByFilename(String filename) {
|
private static void addBufferedImageByFilename(String filename) {
|
||||||
try {
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue