1

我正在开发一款游戏,我遇到了一个问题,当我告诉它在关机时释放所有东西时,我的 OpenGL 代码不会删除纹理,这是一个例外:

Exception in thread "main" java.lang.NullPointerException
at org.lwjgl.opengl.GL11.glDeleteTextures(GL11.java:732)
at com.magicalrealmsofherith.OpenGL.releaseAll(OpenGL.java:29)
at com.magicalrealmsofherith.client.MRoH.stop(MRoH.java:100)
at com.magicalrealmsofherith.client.MRoH.run(MRoH.java:60)
at java.lang.Thread.run(Unknown Source)
at com.magicalrealmsofherith.client.MRoH.main(MRoH.java:22)

这是我的代码(有点大):

public class OpenGL {

private static HashMap<String, Integer> textureMap = new HashMap<String, Integer>();
private static HashMap<Integer, BufferedImage> imageMap = new HashMap<Integer, BufferedImage>();
private static List<Integer> textureList = new ArrayList<Integer>();

public static void releaseAll()
{
    for(int i : textureList)
    {
        glDeleteTextures(i);
    }
    textureList.clear();
    imageMap.clear();
    textureMap.clear();
}

public static int getTextureId(String texture) {
    if(textureMap.containsKey(texture))
        return textureMap.get(texture);
    else
    {
        setupTexture(texture);
        return textureMap.get(texture);
    }
}

public static void bindTexutre(String texture) {
    bindTexture(getTextureId(texture));
}

public static void bindTexture(int textureId){
    glBindTexture(GL_TEXTURE_2D, textureId);
}

public static void setupTexture(String textureName) {

    BufferedImage texture;
    try
    {
        texture = ImageIO.read((OpenGL.class.getClassLoader()).getResourceAsStream(textureName));
    }
    catch(Exception e)
    {
        try
        {
            texture = ImageIO.read(new File(textureName));
        }
        catch(Exception ex)
        {
            texture = new BufferedImage(64, 64, 2);
            Graphics g = texture.getGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, 64, 64);
            g.setColor(Color.black);
            g.drawString("texturemiss", 1, 10);
            g.dispose();
        }
    }

    setupTexture(texture, textureName);
}

public static void setupTexture(BufferedImage texture, String textureName) {
    int id = glGenTextures();
    glBindTexture(GL_TEXTURE_2D, id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.getWidth(), texture.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, decodePNG(texture, true));
    textureMap.put(textureName, id);
    imageMap.put(id, texture);
    textureList.add(id);
    glBindTexture(GL_TEXTURE_2D, 0);
}

public static ByteBuffer decodePNG(BufferedImage texture, boolean alpha) {
    ByteBuffer buffer = BufferUtils.createByteBuffer(texture.getWidth() * texture.getHeight() * (alpha == true ? 4 : 3));
    int[] pixels = new int[texture.getWidth() * texture.getHeight()];
    texture.getRGB(0, 0, texture.getWidth(), texture.getHeight(), pixels, 0, texture.getWidth());
    for(int y = 0; y < texture.getHeight(); y++)
    {
        for(int x = 0; x < texture.getWidth(); x++)
        {
            int pixel = pixels[y * texture.getWidth() + x];
            buffer.put((byte) ((pixel >> 16) & 0xFF));
            buffer.put((byte) ((pixel >> 8) & 0xFF));
            buffer.put((byte) (pixel & 0xFF));
            if(alpha)
                buffer.put((byte) ((pixel >> 24) & 0xFF));
        }
    }
    buffer.flip();
    return buffer;
}
}
4

1 回答 1

2

在蒂姆的帮助下,我解决了自己的问题。对于将要使用此代码的其他任何人;你必须先OpenGL.releaseAll();打电话Display.destroy();

于 2012-06-02T08:20:23.247 回答