Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.dbi.games.fortress.engine.graphics; import java.awt.color.ColorSpace; import java.awt.image.ColorModel; import java.awt.image.ComponentColorModel; import java.awt.image.DataBuffer; import java.io.IOException; import java.nio.IntBuffer; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.TextureLoader; /** * * @author Falken */ public class TextureManager { private ColorModel alphaColorModel; private ColorModel colorModel; private IntBuffer textureIDBuffer = BufferUtils.createIntBuffer(1); private static final List<Integer> powersOfTwo = Arrays.asList(2, 4, 8, 16, 32, 64, 128, 256, 512, 1024); private Map<String, Texture> textureIndex = new HashMap<String, Texture>(); public TextureManager() { alphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 }, true, false, ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE); colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 0 }, false, false, ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE); } public int newTextureID() { GL11.glGenTextures(textureIDBuffer); return textureIDBuffer.get(0); } public Texture getTexture(String textureResource) { if (textureIndex.containsKey(textureResource)) { return textureIndex.get(textureResource); } Texture tex; try { tex = TextureLoader.getTexture("PNG", getClass().getClassLoader().getResourceAsStream(textureResource)); } catch (Exception ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to load texture correctly: " + textureResource, ex); return null; } textureIndex.put(textureResource, tex); return tex; } }