Game Development Reference
In-Depth Information
import
android.graphics.Rect;
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;
public int
width;
public int
height;
boolean
mipmapped;
We add only one new member, called
mipmapped
, which stores whether the texture has a mipmap
chain or not.
public
Texture(GLGame glGame, String fileName) {
this
(glGame, fileName,
false
);
}
public
Texture(GLGame glGame, String fileName,
boolean
mipmapped) {
this
.glGraphics = glGame.getGLGraphics();
this
.fileIO = glGame.getFileIO();
this
.fileName = fileName;
this
.mipmapped = mipmapped;
load();
}
For compatibility, we leave the old constructor in, which calls the new constructor. The new
constructor takes a third argument that lets us specify whether we want the texture to be
mipmapped.
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);
if
(mipmapped) {
createMipmaps(gl, bitmap);
}
else
{
gl.glBindTexture(GL10.
GL_TEXTURE_2D
, textureId);
GLUtils.
texImage2D
(GL10.
GL_TEXTURE_2D
, 0, bitmap, 0);