Hello,
As you may notice I’m new to this forum. Been checking some threads about textures and images, but I haven’t found any good answers to my problems.
Since a couple of months, me and a few classmates have been working on a school project. We intend to create a 2D game in Java, using Open GL. More specific, our project is to create a sidescrolling platform game. We intent to use an array (i.e 1000x600) of integers as a map. Each coordinate corresponds to an image that will be shown in that location. Now, this means a lot of images at the screen at the same time, an I’m pretty sure you can figure out my problem. The game runs very slow.
This is our class we use to create ImageObjects:
/* ------------------------------------------- ImageObject.java ------------------------------------------------------- */
import java.awt.;
import java.awt.image.;
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.event.;
import net.java.games.jogl.;
public class ImageObject {
private byte[] imgRGBA;
private int height;
private int width;
private int x;
private int y;
public ImageObject(String filename) {
loadImage(filename);
x = 0;
y = 0;
}
public ImageObject(String filename, int X, int Y) {
loadImage(filename);
x = X;
y = Y;
}
public void draw (GL gl) {
gl.glBlendFunc (GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable (GL.GL_BLEND);
gl.glRasterPos2i (x, y);
gl.glDrawPixels (width, height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, imgRGBA);
}
public void setXY (int X, int Y) {
x = X;
y = Y;
}
public void setX (int X) {
x = X;
}
public void setY (int Y) {
y = Y;
}
public int getX () {
return x;
}
public int getY () {
return y;
}
public int getHeight () {
return height;
}
public int getWidth () {
return width;
}
public void loadImage (String filename) {
Image i = Toolkit.getDefaultToolkit().createImage (filename);
MediaTracker tracker = new MediaTracker(new Canvas());
tracker.addImage (i, 0);
try {
tracker.waitForAll();
} catch (InterruptedException ie) {}
height = i.getHeight(null);
width = i.getWidth(null);
//System.out.println ("Got "+filename+" image, width=" +
width + ", height=" + height);
// turn it into a BufferedImage
WritableRaster raster =
Raster.createInterleavedRaster (DataBuffer.TYPE_BYTE,
width,
height,
4,
null);
ComponentColorModel colorModel=
new ComponentColorModel (ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,8},
true,
false,
ComponentColorModel.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
BufferedImage BuffedImg =
new BufferedImage (colorModel, // color model
raster,
false, // isRasterPremultiplied
null); // properties
Graphics2D g = BuffedImg.createGraphics();
// use an AffineTransformation to draw upside-down in the java
// sense, which will make it right-side-up in OpenGL
AffineTransform gt = new AffineTransform();
gt.translate (0, height);
gt.scale (1, -1d);
g.transform (gt);
g.drawImage (i, null, null);
// now retrieve the underlying byte array from BuffedImg
DataBufferByte imgBuf = (DataBufferByte)raster.getDataBuffer();
imgRGBA = imgBuf.getData();
g.dispose();
}
}
/* ------------------------------------------- EOF ------------------------------------------------------- */
Now, is there a faster way to load/draw images?
Edit: Please note that we already thought of only drawing the images within the screen borders.
Cheers,
Emil Olofsson
emilo35@hotmail.com