'ResourceManager' lwjgl [Criticism Accepted Warmly!]

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 :slight_smile: I am using slick-util, and wrapping it. Dont judge!

I don’t understand. Why use slick util when you can creates your own texture loader? This code isn’t honestly too useful unless you provide a different way to load the textures. Anyone can make this, what’s hard and useful to share is a texture processor.

I’m sorry, I just don’t like the fact that people post half finished, already showed off a million other times, pieces of code :stuck_out_tongue: If you have some new amazing algorithm, yeah post it, but a resource loader isn’t a new idea, its been around forever so I just don’t see the need to post it here!

It doesn’t neccessarily have to be new, just uncommon enough for people to learn something.

I made a post a long while ago about an extremely simple and 2x faster AABB collision detection. I by no means was the first to discover it (although I came up with it myself), but it was unknown enough that I posted it, and people have linked to it a few times.

So, @OP: Don’t be discouraged to post code like this. Just 1) Make sure it’s complete; 2) Make sure it’s uncommonly used, or even completely new; and 3) Make sure it is better than the current ‘favoured’ approach in some way.

I don’t like Slick-Util as well. You can instead code your loading mechanism in less than a 100 lines. I have it in my LWJGL Tutorial Series at here Textures

letting aside the other criticism in this thread.

Some basics about your code, which hopefully helps you to code better in the future:

  • Use a real class for your sounds, this int id is some implementation detail of OpenAL. You can then also put the play,pause … commands In this SoundCLip class.
  • Do you know and need what makes Vector special? If not use an ArrayList (the Oracle docs about the collection classes are a must read).
  • An more Advanced step would be to try to generalize things. One generic resource cache for one resource type. Then in your app you would have one sound cache, one texture cache and so on.

ps: You can read about my very advanced resource managment system in this old thread

Why are you using a Vector? If you’re just holding a list of stuff…use a List…

This proves you haven’t even run the code once. :clue:

2001

ok let me write you this in more words as riven uses.

Vectors are outdated and slow. Slow because all operations are synchronized.
The general recommendation is to use ArrayLists.

Also, using Java benchmark before Java 1.4 have no value whatsoever.