Path: blob/master/SLICK_HOME/src/org/newdawn/slick/opengl/TextureImpl.java
1461 views
package org.newdawn.slick.opengl;12import java.nio.ByteBuffer;3import java.nio.ByteOrder;4import java.nio.IntBuffer;56import org.lwjgl.BufferUtils;7import org.newdawn.slick.opengl.renderer.SGL;8import org.newdawn.slick.opengl.renderer.Renderer;910/**11* A texture to be bound within JOGL. This object is responsible for12* keeping track of a given OpenGL texture and for calculating the13* texturing mapping coordinates of the full image.14*15* Since textures need to be powers of 2 the actual texture may be16* considerably bigged that the source image and hence the texture17* mapping coordinates need to be adjusted to matchup drawing the18* sprite against the texture.19*20* @author Kevin Glass21* @author Brian Matzon22*/23public class TextureImpl implements Texture {24/** The renderer to use for all GL operations */25protected static SGL GL = Renderer.get();2627/** The last texture that was bound to */28static Texture lastBind;2930/**31* Retrieve the last texture bound through the texture interface32*33* @return The last texture bound34*/35public static Texture getLastBind() {36return lastBind;37}3839/** The GL target type */40private int target;41/** The GL texture ID */42private int textureID;43/** The height of the image */44private int height;45/** The width of the image */46private int width;47/** The width of the texture */48private int texWidth;49/** The height of the texture */50private int texHeight;51/** The ratio of the width of the image to the texture */52private float widthRatio;53/** The ratio of the height of the image to the texture */54private float heightRatio;55/** If this texture has alpha */56private boolean alpha;57/** The reference this texture was loaded from */58private String ref;59/** The name the texture has in the cache */60private String cacheName;6162/**63* For subclasses to utilise64*/65protected TextureImpl() {66}6768/**69* Create a new texture70*71* @param ref The reference this texture was loaded from72* @param target The GL target73* @param textureID The GL texture ID74*/75public TextureImpl(String ref, int target,int textureID) {76this.target = target;77this.ref = ref;78this.textureID = textureID;79lastBind = this;80}8182/**83* Set the name this texture is stored against in the cache84*85* @param cacheName The name the texture is stored against in the cache86*/87public void setCacheName(String cacheName) {88this.cacheName = cacheName;89}9091/**92* @see org.newdawn.slick.opengl.Texture#hasAlpha()93*/94public boolean hasAlpha() {95return alpha;96}9798/**99* @see org.newdawn.slick.opengl.Texture#getTextureRef()100*/101public String getTextureRef() {102return ref;103}104105/**106* If this texture has alpha107*108* @param alpha True, If this texture has alpha109*/110public void setAlpha(boolean alpha) {111this.alpha = alpha;112}113114/**115* Clear the binding of the texture116*/117public static void bindNone() {118lastBind = null;119GL.glDisable(SGL.GL_TEXTURE_2D);120}121122/**123* Clear slick caching of the last bound texture so that an124* external texture binder can play with the context before returning125* control to slick.126*/127public static void unbind() {128lastBind = null;129}130131/**132* @see org.newdawn.slick.opengl.Texture#bind()133*/134public void bind() {135if (lastBind != this) {136lastBind = this;137GL.glEnable(SGL.GL_TEXTURE_2D);138GL.glBindTexture(target, textureID);139}140}141142/**143* Set the height of the image144*145* @param height The height of the image146*/147public void setHeight(int height) {148this.height = height;149setHeight();150}151152/**153* Set the width of the image154*155* @param width The width of the image156*/157public void setWidth(int width) {158this.width = width;159setWidth();160}161162/**163* @see org.newdawn.slick.opengl.Texture#getImageHeight()164*/165public int getImageHeight() {166return height;167}168169/**170* @see org.newdawn.slick.opengl.Texture#getImageWidth()171*/172public int getImageWidth() {173return width;174}175176/**177* @see org.newdawn.slick.opengl.Texture#getHeight()178*/179public float getHeight() {180return heightRatio;181}182183/**184* @see org.newdawn.slick.opengl.Texture#getWidth()185*/186public float getWidth() {187return widthRatio;188}189190/**191* @see org.newdawn.slick.opengl.Texture#getTextureHeight()192*/193public int getTextureHeight() {194return texHeight;195}196197/**198* @see org.newdawn.slick.opengl.Texture#getTextureWidth()199*/200public int getTextureWidth() {201return texWidth;202}203204/**205* Set the height of this texture206*207* @param texHeight The height of the texture208*/209public void setTextureHeight(int texHeight) {210this.texHeight = texHeight;211setHeight();212}213214/**215* Set the width of this texture216*217* @param texWidth The width of the texture218*/219public void setTextureWidth(int texWidth) {220this.texWidth = texWidth;221setWidth();222}223224/**225* Set the height of the texture. This will update the226* ratio also.227*/228private void setHeight() {229if (texHeight != 0) {230heightRatio = ((float) height)/texHeight;231}232}233234/**235* Set the width of the texture. This will update the236* ratio also.237*/238private void setWidth() {239if (texWidth != 0) {240widthRatio = ((float) width)/texWidth;241}242}243244/**245* @see org.newdawn.slick.opengl.Texture#release()246*/247public void release() {248IntBuffer texBuf = createIntBuffer(1);249texBuf.put(textureID);250texBuf.flip();251252GL.glDeleteTextures(texBuf);253254if (lastBind == this) {255bindNone();256}257258if (cacheName != null) {259InternalTextureLoader.get().clear(cacheName);260} else {261InternalTextureLoader.get().clear(ref);262}263}264265/**266* @see org.newdawn.slick.opengl.Texture#getTextureID()267*/268public int getTextureID() {269return textureID;270}271272/**273* Set the OpenGL texture ID for this texture274*275* @param textureID The OpenGL texture ID276*/277public void setTextureID(int textureID) {278this.textureID = textureID;279}280281/**282* Creates an integer buffer to hold specified ints283* - strictly a utility method284*285* @param size how many int to contain286* @return created IntBuffer287*/288protected IntBuffer createIntBuffer(int size) {289ByteBuffer temp = ByteBuffer.allocateDirect(4 * size);290temp.order(ByteOrder.nativeOrder());291292return temp.asIntBuffer();293}294295/**296* @see org.newdawn.slick.opengl.Texture#getTextureData()297*/298public byte[] getTextureData() {299ByteBuffer buffer = BufferUtils.createByteBuffer((hasAlpha() ? 4 : 3) * texWidth * texHeight);300bind();301GL.glGetTexImage(SGL.GL_TEXTURE_2D, 0, hasAlpha() ? SGL.GL_RGBA : SGL.GL_RGB, SGL.GL_UNSIGNED_BYTE,302buffer);303byte[] data = new byte[buffer.limit()];304buffer.get(data);305buffer.clear();306307return data;308}309}310311312