From 82d2a64097bc05e34cd568e469c8018ef9716e5a Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Thu, 8 Mar 2007 19:17:23 +0000 Subject: [PATCH] Added a TexturLoader and Nodes, Sprites and some modifications --- src/ei/engine/math/Vector2I.java | 25 ++ src/ei/engine/scene/Node.java | 76 ++++++ src/ei/engine/scene/Sprite.java | 153 ++++++++++++ src/ei/engine/state/GameStateManager.java | 5 + src/ei/engine/util/Texture.java | 151 ++++++++++++ src/ei/engine/util/TextureLoader.java | 277 ++++++++++++++++++++++ 6 files changed, 687 insertions(+) create mode 100644 src/ei/engine/math/Vector2I.java create mode 100644 src/ei/engine/scene/Node.java create mode 100644 src/ei/engine/scene/Sprite.java create mode 100644 src/ei/engine/util/Texture.java create mode 100644 src/ei/engine/util/TextureLoader.java diff --git a/src/ei/engine/math/Vector2I.java b/src/ei/engine/math/Vector2I.java new file mode 100644 index 0000000..bbe7976 --- /dev/null +++ b/src/ei/engine/math/Vector2I.java @@ -0,0 +1,25 @@ +package ei.engine.math; + +public class Vector2I { + private int x; + private int y; + + public Vector2I(int x, int y){ + this.x = x; + this.y = y; + } + + /** + * @return the x value in the vector + */ + public int getX(){ + return x; + } + + /** + * @return the y value in the vector + */ + public int getY(){ + return y; + } +} diff --git a/src/ei/engine/scene/Node.java b/src/ei/engine/scene/Node.java new file mode 100644 index 0000000..3cd7e82 --- /dev/null +++ b/src/ei/engine/scene/Node.java @@ -0,0 +1,76 @@ +package ei.engine.scene; + +import java.util.ArrayList; + +public class Node extends Sprite { + /** the sprites of this node */ + protected ArrayList sprites; + + public Node(String ref) { + super(ref); + sprites = new ArrayList(); + } + + /** + * Add a Sprite + * @param s the Sprite to add + * @return false if the Sprite alredy exist else true + */ + public boolean add(Sprite s){ + if(!sprites.contains(s)){ + sprites.add(s); + return true; + } + return false; + } + + /** + * get a Sprite + * @param name the name of Sprite to get + * @return null if the Sprite wasnt found + */ + public Sprite get(String name){ + int i = getId(name); + if(i >= 0){ + return sprites.get(i); + } + return null; + } + + /** + * remove a Sprite + * @param name the name of Sprite to remove + * @return false if the Sprite alredy exist else true + */ + public boolean remove(String name){ + int i = getId(name); + if(i >= 0){ + sprites.remove(i); + return true; + } + return false; + } + + /** + * Draw all the sprites in the node + */ + public void render() { + for(int i=0; i table = new HashMap(); + + /** The colour model including alpha for the GL image */ + private ColorModel glAlphaColorModel; + + /** The colour model for the GL image */ + private ColorModel glColorModel; + + /** + * Create a new texture loader based on the game panel + * + * @param gl The GL content in which the textures should be loaded + */ + public TextureLoader() { + glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), + new int[] {8,8,8,8}, + true, + false, + ComponentColorModel.TRANSLUCENT, + DataBuffer.TYPE_BYTE); + + glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), + new int[] {8,8,8,0}, + false, + false, + ComponentColorModel.OPAQUE, + DataBuffer.TYPE_BYTE); + } + + /** + * Create a new texture ID + * + * @return A new texture ID + */ + private int createTextureID(){ + IntBuffer tmp = createIntBuffer(1); + GL11.glGenTextures(tmp); + return tmp.get(0); + } + + /** + * Load a texture + * + * @param resourceName The location of the resource to load + * @return The loaded texture + * @throws IOException Indicates a failure to access the resource + */ + public Texture getTexture(String resourceName) throws IOException { + Texture tex = (Texture) table.get(resourceName); + + if (tex != null) { + return tex; + } + + tex = getTexture(resourceName, + GL11.GL_TEXTURE_2D, // target + GL11.GL_RGBA, // dst pixel format + GL11.GL_LINEAR, // min filter (unused) + GL11.GL_LINEAR); + + table.put(resourceName,tex); + + return tex; + } + + /** + * Load a texture into OpenGL from a image reference on + * disk. + * + * @param resourceName The location of the resource to load + * @param target The GL target to load the texture against + * @param dstPixelFormat The pixel format of the screen + * @param minFilter The minimising filter + * @param magFilter The magnification filter + * @return The loaded texture + * @throws IOException Indicates a failure to access the resource + */ + public Texture getTexture(String resourceName, + int target, + int dstPixelFormat, + int minFilter, + int magFilter) throws IOException + { + int srcPixelFormat = 0; + + // create the texture ID for this texture + int textureID = createTextureID(); + Texture texture = new Texture(target,textureID); + + // bind this texture + GL11.glBindTexture(target, textureID); + + BufferedImage bufferedImage = loadImage(resourceName); + texture.setWidth(bufferedImage.getWidth()); + texture.setHeight(bufferedImage.getHeight()); + + if (bufferedImage.getColorModel().hasAlpha()) { + srcPixelFormat = GL11.GL_RGBA; + } else { + srcPixelFormat = GL11.GL_RGB; + } + + // convert that image into a byte buffer of texture data + ByteBuffer textureBuffer = convertImageData(bufferedImage,texture); + + if (target == GL11.GL_TEXTURE_2D) + { + GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter); + GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter); + } + + // produce a texture from the byte buffer + GL11.glTexImage2D(target, + 0, + dstPixelFormat, + get2Fold(bufferedImage.getWidth()), + get2Fold(bufferedImage.getHeight()), + 0, + srcPixelFormat, + GL11.GL_UNSIGNED_BYTE, + textureBuffer ); + + return texture; + } + + /** + * Get the closest greater power of 2 to the fold number + * + * @param fold The target number + * @return The power of 2 + */ + private int get2Fold(int fold) { + int ret = 2; + while (ret < fold) { + ret *= 2; + } + return ret; + } + + /** + * Convert the buffered image to a texture + * + * @param bufferedImage The image to convert to a texture + * @param texture The texture to store the data into + * @return A buffer containing the data + */ + private ByteBuffer convertImageData(BufferedImage bufferedImage,Texture texture) { + ByteBuffer imageBuffer = null; + WritableRaster raster; + BufferedImage texImage; + + int texWidth = 2; + int texHeight = 2; + + // find the closest power of 2 for the width and height + // of the produced texture + while (texWidth < bufferedImage.getWidth()) { + texWidth *= 2; + } + while (texHeight < bufferedImage.getHeight()) { + texHeight *= 2; + } + + texture.setTextureHeight(texHeight); + texture.setTextureWidth(texWidth); + + // create a raster that can be used by OpenGL as a source + // for a texture + if (bufferedImage.getColorModel().hasAlpha()) { + raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,4,null); + texImage = new BufferedImage(glAlphaColorModel,raster,false,new Hashtable()); + } else { + raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,3,null); + texImage = new BufferedImage(glColorModel,raster,false,new Hashtable()); + } + + // copy the source image into the produced image + Graphics g = texImage.getGraphics(); + g.setColor(new Color(0f,0f,0f,0f)); + g.fillRect(0,0,texWidth,texHeight); + g.drawImage(bufferedImage,0,0,null); + + // build a byte buffer from the temporary image + // that be used by OpenGL to produce a texture. + byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData(); + + imageBuffer = ByteBuffer.allocateDirect(data.length); + imageBuffer.order(ByteOrder.nativeOrder()); + imageBuffer.put(data, 0, data.length); + imageBuffer.flip(); + + return imageBuffer; + } + + /** + * Load a given resource as a buffered image + * + * @param ref The location of the resource to load + * @return The loaded buffered image + * @throws IOException Indicates a failure to find a resource + */ + private BufferedImage loadImage(String ref) throws IOException { + URL url = TextureLoader.class.getClassLoader().getResource(ref); + + if (url == null) { + throw new IOException("Cannot find: "+ref); + } + + BufferedImage bufferedImage = ImageIO.read(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(ref))); + + return bufferedImage; + } + + /** + * Creates an integer buffer to hold specified ints + * - strictly a utility method + * + * @param size how many int to contain + * @return created IntBuffer + */ + protected IntBuffer createIntBuffer(int size) { + ByteBuffer temp = ByteBuffer.allocateDirect(4 * size); + temp.order(ByteOrder.nativeOrder()); + + return temp.asIntBuffer(); + } + + public static TextureLoader getTextureLoaderInstance(){ + if(instace == null){ + instace = new TextureLoader(); + } + return instace; + } +} \ No newline at end of file