Please give constructive criticism, since I did all of this with minimal knowledge of openAL, and it is my first try at a ‘resource manager.’ This file is just so that I can load resources, and not have to make a new Object for every single entity I have. Like say I had an entity: I do not want to initialize the same texture/sound for every single entity! I want to load it once, and then reference it later!
package com.wessles.MERCury;
import java.io.*;
import java.util.Vector;
import org.newdawn.slick.openal.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import static org.lwjgl.openal.AL10.*;
/**
* @from MERCury
* @author wessles
* @website www.wessles.com
*/
public class ResourceManager {
private Vector<Texture> textures = new Vector<Texture>();
/**
* Used to generate an {@code int src} corresponding to your .wav (more sound types coming soon!) file (location specified in {@code String location}) that you can later reference in use of {@code playSound(int src), pauseSound(int src), stopSound(int src),} and {@code rewindSound(int src)}.
*
* @param location
* The location of your .wav file (more sound types coming soon!).
* @return An id that corresponds to the sound that you gave. You can get that sound back by using this id in {@code playSound(int src);}
* @throws FileNotFoundException
*/
public int loadSound(String location) throws FileNotFoundException {
WaveData data = WaveData.create(new BufferedInputStream(new FileInputStream(location)));
int buffer = alGenBuffers();
alBufferData(buffer, data.format, data.data, data.samplerate);
data.dispose();
int source = alGenSources();
alSourcei(source, AL_BUFFER, buffer);
return source;
}
/**
* Plays a sound corresponding to the {@code src} that you pass in.
*
* @param src
* Reference to the sound's source.
*/
public void playSound(int src) {
alSourcePlay(src);
}
/**
* Pauses a sound corresponding to the {@code src} that you pass in.
*
* @param src
* Reference to the sound's source.
*/
public void pauseSound(int src) {
alSourcePause(src);
}
/**
* Stops a sound corresponding to the {@code src} that you pass in.
*
* @param src
* Reference to the sound's source.
*/
public void stopSound(int src) {
alSourceStop(src);
}
/**
* Rewinds a sound corresponding to the {@code src} that you pass in.
*
* @param src
* Reference to the sound's source.
*/
public void rewindSound(int src) {
alSourceRewind(src);
}
/**
* Generates an {@code int src} corresponding to your texture that may be used in {@code getTexture(int src);}.
*
* @param format
* The format of the image you wish to load. I.E. "PNG" "JPG"
* @param location
* The location of the image you wish to load. I.E. "res/genericuseforexpamples.png"
* @return An {@code int src} corresponding to your texture that may be used in {@code getTexture(int src);}.
* @throws IOException
*/
public int loadTexture(String format, String location) throws IOException {
textures.add(TextureLoader.getTexture(format, this.getClass().getResourceAsStream(location)));
return textures.size()-1;
}
/**
* Gives you a texture based off of a source id, given at {@code loadTexture();}.
* @param src The source id corresponding to the texture you wish to load, given at {@code loadTexture();}.
* @return The texture corresponding to the source id you got with {@code loadTexture();}.
*/
public Texture getTexture(int src) {
return textures.get(src);
}
}
And yes, I am not doing all this from scratch I am using slick-util, and wrapping it. Dont judge!