Added a TexturLoader and Nodes, Sprites and some modifications

This commit is contained in:
Ziver Koc 2007-03-08 19:17:23 +00:00
parent 330607c72b
commit 82d2a64097
6 changed files with 687 additions and 0 deletions

View file

@ -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;
}
}

View file

@ -0,0 +1,76 @@
package ei.engine.scene;
import java.util.ArrayList;
public class Node extends Sprite {
/** the sprites of this node */
protected ArrayList<Sprite> sprites;
public Node(String ref) {
super(ref);
sprites = new ArrayList<Sprite>();
}
/**
* 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<sprites.size() ;i++){
sprites.get(i).render();
}
}
/**
* Searches for the given name of a sprite
* @param name The name of the sprite
* @return The index of the sprite
*/
private int getId(String name){
for(int i=0; i<sprites.size() ;i++){
if(sprites.get(i).getName().equals(name)){
return i;
}
}
return -1;
}
}

View file

@ -0,0 +1,153 @@
package ei.engine.scene;
import java.io.IOException;
import org.lwjgl.opengl.GL11;
import ei.engine.math.Vector2I;
import ei.engine.util.Texture;
import ei.engine.util.TextureLoader;
/**
* Implementation of sprite that uses an OpenGL quad and a texture
* to render a given image to the screen.
*
* @author Kevin Glass
* @author Brian Matzon
* @author Ziver Koc
*/
public class Sprite {
/** The texture that stores the image for this sprite */
private Texture texture;
/** The location of this sprite in pixels*/
private Vector2I location;
/** The name of this sprite */
private String name;
/**
* Create a new empty sprite
*
* @param window The window in which the sprite will be displayed
* @param ref A reference to the image on which this sprite should be based
*/
public Sprite(String name) {
this.name = name;
texture = null;
location = new Vector2I(0,0);
}
/**
* Create a new sprite from a specified image.
*
* @param name The name of the sprite
* @param ref A reference to the image on which this sprite should be based
*/
public Sprite(String name, String ref) {
try {
this.name = name;
texture = TextureLoader.getTextureLoaderInstance().getTexture(ref);
location = new Vector2I(texture.getImageWidth(),texture.getImageHeight());
} catch (IOException e) {
// a tad abrupt, but our purposes if you can't find a
// sprite's image you might as well give up.
System.err.println("Unable to load texture: "+ref);
System.exit(0);
}
}
/**
* Get the width of this sprite in pixels
*
* @return The width of this sprite in pixels
*/
public int getWidth() {
if(texture == null){
return 0;
}
return texture.getImageWidth();
}
/**
* Get the height of this sprite in pixels
*
* @return The height of this sprite in pixels
*/
public int getHeight() {
if(texture == null){
return 0;
}
return texture.getImageHeight();
}
/**
* @return The name of this sprite
*/
public String getName() {
return name;
}
/**
* Get the height of this sprite in pixels
*
* @return The height of this sprite in pixels
*/
public int getLocation() {
if(texture == null){
return 0;
}
return texture.getImageHeight();
}
/**
* set the location of this sprite in pixels
* @param x The x coordinate of the sprite
* @param y The y coordinate of the sprite
*/
public void setLocation(int x, int y) {
setLocation(new Vector2I(x,y));
}
/**
* set the location of this sprite in pixels
* @param v The location of the sprite
*/
public void setLocation(Vector2I v) {
location = v;
}
/**
* Draw the sprite
*/
public void render() {
// store the current model matrix
GL11.glPushMatrix();
// bind to the appropriate texture for this sprite
texture.bind();
// translate to the right location and prepare to draw
GL11.glTranslatef(location.getX(), location.getY(), 0);
GL11.glColor3f(1,1,1);
// draw a quad textured to match the sprite
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(0, texture.getImageWidth());
GL11.glVertex2f(0, texture.getImageHeight());
GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
GL11.glVertex2f(texture.getImageWidth(),texture.getImageHeight());
GL11.glTexCoord2f(texture.getWidth(), 0);
GL11.glVertex2f(texture.getImageWidth(),0);
}
GL11.glEnd();
// restore the model view matrix to prevent contamination
GL11.glPopMatrix();
}
}

View file

@ -91,6 +91,11 @@ public class GameStateManager {
return false;
}
/**
* Searches for the given name of a GameState
* @param name The name of the GameState
* @return The index of the GameState
*/
private int getId(String name){
for(int i=0; i<gameStates.size() ;i++){
if(gameStates.get(i).getName().equals(name)){

View file

@ -0,0 +1,151 @@
package ei.engine.util;
import org.lwjgl.opengl.GL11;
/**
* A texture to be bound within JOGL. This object is responsible for
* keeping track of a given OpenGL texture and for calculating the
* texturing mapping coordinates of the full image.
*
* Since textures need to be powers of 2 the actual texture may be
* considerably bigged that the source image and hence the texture
* mapping coordinates need to be adjusted to matchup drawing the
* sprite against the texture.
*
* @author Kevin Glass
* @author Brian Matzon
*/
public class Texture {
/** The GL target type */
private int target;
/** The GL texture ID */
private int textureID;
/** The height of the image */
private int height;
/** The width of the image */
private int width;
/** The width of the texture */
private int texWidth;
/** The height of the texture */
private int texHeight;
/** The ratio of the width of the image to the texture */
private float widthRatio;
/** The ratio of the height of the image to the texture */
private float heightRatio;
/**
* Create a new texture
*
* @param target The GL target
* @param textureID The GL texture ID
*/
public Texture(int target,int textureID) {
this.target = target;
this.textureID = textureID;
}
/**
* Bind the specified GL context to a texture
*
* @param gl The GL context to bind to
*/
public void bind() {
GL11.glBindTexture(target, textureID);
}
/**
* Set the height of the image
*
* @param height The height of the image
*/
public void setHeight(int height) {
this.height = height;
setHeight();
}
/**
* Set the width of the image
*
* @param width The width of the image
*/
public void setWidth(int width) {
this.width = width;
setWidth();
}
/**
* Get the height of the original image
*
* @return The height of the original image
*/
public int getImageHeight() {
return height;
}
/**
* Get the width of the original image
*
* @return The width of the original image
*/
public int getImageWidth() {
return width;
}
/**
* Get the height of the physical texture
*
* @return The height of physical texture
*/
public float getHeight() {
return heightRatio;
}
/**
* Get the width of the physical texture
*
* @return The width of physical texture
*/
public float getWidth() {
return widthRatio;
}
/**
* Set the height of this texture
*
* @param texHeight The height of the texture
*/
public void setTextureHeight(int texHeight) {
this.texHeight = texHeight;
setHeight();
}
/**
* Set the width of this texture
*
* @param texWidth The width of the texture
*/
public void setTextureWidth(int texWidth) {
this.texWidth = texWidth;
setWidth();
}
/**
* Set the height of the texture. This will update the
* ratio also.
*/
private void setHeight() {
if (texHeight != 0) {
heightRatio = ((float) height)/texHeight;
}
}
/**
* Set the width of the texture. This will update the
* ratio also.
*/
private void setWidth() {
if (texWidth != 0) {
widthRatio = ((float) width)/texWidth;
}
}
}

View file

@ -0,0 +1,277 @@
package ei.engine.util;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.HashMap;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import org.lwjgl.opengl.GL11;
/**
* A utility class to load textures for lwjgl. This source is based
* on a texture that can be found in the Java Gaming (www.javagaming.org)
* Wiki. It has been simplified slightly for explicit 2D graphics use.
*
* OpenGL uses a particular image format. Since the images that are
* loaded from disk may not match this format this loader introduces
* a intermediate image which the source image is copied into. In turn,
* this image is used as source for the OpenGL texture.
*
* @author Kevin Glass
* @author Brian Matzon
* @author Ziver koc
*/
public class TextureLoader {
private static TextureLoader instace;
/** The table of textures that have been loaded in this loader */
private static HashMap<String, Texture> table = new HashMap<String, Texture>();
/** 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;
}
}