so, i had the idea that i would need to propper manage my resources if i want a eficient code. And i tought of this:
Create a Package called ResourcePath, it will contain only Enums such as this one:
package ResourcePath;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public enum TextureResourcePath {
TextureTechWall("Textures\","TechWall.jpg", 0),
TextureRoughMetal("Textures\","rough_metal_texture.png", 1),
TextureLava("Textures\","Lava.jpg", 2),
TextureFlare("Textures\","Flaresmall.jpg", 3)
;
private String path;
private String name;
private int id;
public static final String rooturl = "file:" + System.getProperty("user.dir")+ "/Resources/";
public static final String rootpath = System.getProperty("user.dir")+ "\Resources\";
private TextureResourcePath(String path, String name, int id){
this.path = path;
this.name = name;
this.id = id;
}
public String getPath(){
return path;
}
public String getName(){
return name;
}
public int getId() {
return id;
}
@Override
public String toString() {
return rootpath + path + name;
}
public URL toURL(){
try {
return new URL(rooturl + path + name);
} catch (MalformedURLException ex) {
Logger.getLogger(ResourcePath.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
}
it will store all possible texture paths inside the Resources directory right on your Dist directory, they would be somethign like: "distResourcesTexturesFlaresmall.jpg", this can become either a String for a File or a URL for a.. URL!
using this i can create the GraphicsManager: (what is not yet ready, but using the same idea here comes the SoundManager that is ready ;D)
package MainPack;
import ResourcePath.SoundResourcePath;
import com.jme.renderer.Camera;
import com.jmex.audio.AudioSystem;
import com.jmex.audio.AudioTrack;
import com.jmex.audio.AudioTrack.TrackType;
public class SoundManager {
private static AudioSystem audio;
private static final AudioTrack[] SFX = new AudioTrack[SoundResourcePath.values().length];
private static void preloadSFX() {
for(SoundResourcePath r : SoundResourcePath.values())
SFX[r.getId()] = getSFX(r);
}
public static void preload(Camera cam){
audio = AudioSystem.getSystem();
audio.getEar().trackOrientation(cam);
audio.getEar().trackPosition(cam);
preloadSFX();
}
public static void playSFX(SoundResourcePath resource){
SFX[resource.getId()].play();
}
private static AudioTrack getSFX(SoundResourcePath resource) {
// Create a non-streaming, looping, positional sound clip.
AudioTrack sound = audio.createAudioTrack(resource.toURL(), false);
System.out.println(sound);
sound.setType(TrackType.HEADSPACE);
sound.setRelative(false);
sound.setLooping(false);
return sound;
}
}
so it will not only preload ALL sounds on your game at startup, but also it will let you play ANY of then with simply one line:
SoundManager.playSFX(SoundResourcePath.Whateversoundyouwish);
is it a good idea or i waste memory/cpu or whatever?