Game Development Reference
In-Depth Information
Listing 7-8. Texture.java, a Little OpenGL ES Texture Class
package
com.badlogic.androidgames.framework.gl;
import
java.io.IOException;
import
java.io.InputStream;
import
javax.microedition.khronos.opengles.GL10;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.opengl.GLUtils;
import
com.badlogic.androidgames.framework.FileIO;
import
com.badlogic.androidgames.framework.impl.GLGame;
import
com.badlogic.androidgames.framework.impl.GLGraphics;
public class
Texture {
GLGraphics glGraphics;
FileIO fileIO;
String fileName;
int
textureId;
int
minFilter;
int
magFilter;
int
width;
int
height;
public
Texture(GLGame glGame, String fileName) {
this
.glGraphics = glGame.getGLGraphics();
this
.fileIO = glGame.getFileIO();
this
.fileName = fileName;
load();
}
private void
load() {
GL10 gl = glGraphics.getGL();
int
[] textureIds =
new int
[1];
gl.glGenTextures(1, textureIds, 0);
textureId = textureIds[0];
InputStream in =
null
;
try
{
in = fileIO.readAsset(fileName);
Bitmap bitmap = BitmapFactory.
decodeStream
(in);
width = bitmap.getWidth();
height = bitmap.getHeight();
gl.glBindTexture(GL10.
GL_TEXTURE_2D
, textureId);
GLUtils.
texImage2D
(GL10.
GL_TEXTURE_2D
, 0, bitmap, 0);
setFilters(GL10.
GL_NEAREST
, GL10.
GL_NEAREST
);
gl.glBindTexture(GL10.
GL_TEXTURE_2D
, 0);
}
catch
(IOException e) {
throw new
RuntimeException("Couldn't load texture '" + fileName + "'", e);