se.angergard.engine.graphics.Texture.java Source code

Java tutorial

Introduction

Here is the source code for se.angergard.engine.graphics.Texture.java

Source

/********************************************************************************
 *
 *   Copyright 2014 Theodor Angergard
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *******************************************************************************/

package se.angergard.engine.graphics;

import java.io.*;
import java.nio.*;

import org.lwjgl.*;
import org.lwjgl.opengl.*;

import se.angergard.engine.interfaces.*;
import se.angergard.test.util.*;
import de.matthiasmann.twl.utils.*;
import de.matthiasmann.twl.utils.PNGDecoder.Format;

/** A helper class, it's purpose is to hold the texture id and being able to bind it to the graphic's card.
 * @author Theodor */
public class Texture implements Disposable {

    /** Only constructor for Texture, final texture id, can't be changed. BROKEN BROKEN BROKEN
     * @param TEXTURE_ID */
    public Texture(String path) {

        int texID = -1;
        InputStream in = null;
        PNGDecoder decoder = null;
        try {
            in = new FileInputStream(MainLoop.getPathToRes() + path);

            decoder = new PNGDecoder(in);

            ByteBuffer buf = BufferUtils.createByteBuffer(decoder.getWidth() * decoder.getHeight() * 4);
            decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
            buf.flip();

            texID = GL11.glGenTextures();
            loadTexture(buf, texID, decoder.getWidth(), decoder.getHeight());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (texID == -1) {
            new Exception("Failed to create texture with path = " + path).printStackTrace();
        }

        this.TEXTURE_ID = texID;
        this.WIDTH = decoder.getWidth();
        this.HEIGHT = decoder.getHeight();
    }

    public Texture(int textureID, int width, int height) {
        this.TEXTURE_ID = textureID;
        this.WIDTH = width;
        this.HEIGHT = height;
    }

    public Texture(int width, int height) {
        this.TEXTURE_ID = GL11.glGenTextures();
        this.WIDTH = width;
        this.HEIGHT = height;
        loadTexture(null, TEXTURE_ID, width, height);
    }

    private void loadTexture(ByteBuffer buffer, int textureID, int width, int height) {
        GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);

        GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA,
                GL11.GL_UNSIGNED_BYTE, buffer);

        // Not all computer will support this, fix //TODO
        GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);

        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);

        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);
    }

    /** the Texture ID */
    private final int TEXTURE_ID;

    /** the original texture's width and height */
    public final int WIDTH, HEIGHT;

    /** Bind the Texture ID to the graphic's card */
    public void bind() {
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, TEXTURE_ID);
    }

    /** @return textureID Returns the texture's ID */
    public int getTextureID() {
        return TEXTURE_ID;
    }

    public boolean equals(Texture texture) {
        if (texture == null) {
            return false;
        }
        return TEXTURE_ID == texture.TEXTURE_ID;
    }

    @Override
    public void dispose() {
        GL11.glDeleteTextures(TEXTURE_ID);
    }

}