Reputation: 435
I have noticed that If I specify all of my textures as static such as:
public static Texture backgroundTexture = null;
public static Texture woodTexture = null;
public static Texture titleTexture = null;
public static Texture buttonStart = null;
public static Texture buttonStartPressed = null;
public static void initTextures(){
try{
backgroundTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/background.png"));
woodTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/wood.png"));
titleTexture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/titleTex.png"));
buttonStart = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/startReady.png"));;
buttonStartPressed = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/startPressed.png"));;
}catch(Exception e){System.out.println(e.toString());}
That my textures work perfectly fine, But I would imagine, that as I amass more textures I would start raping my computers RAM.
So I decided that I should use this to dynamically load the textures as I need them:
public static Texture loadTexture(String location){
Texture tex = null;
try{
tex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/"+location+".png"));
}catch(Exception e){
System.err.println("Failed loading Texture: "+location);
}
if(tex==null){
System.err.println("We had a freaking null texture! it was called: "
+ location);
}
return tex;
}
But when I do this after a certain amount of time the entire texture becomes white(thats my default draw color). But the other ones that are static still work fine. Can anyone explain why this happens?
Upvotes: 1
Views: 255
Reputation: 614
It sounds like the texture object you are overwriting with loadTexture() is being garbage collected, and the previously allocated GPU resource that you had assisted to your geometry is discarded. If you are not binding the new resource provided by loadTexture() with glBindTexture() that could explain why the texture doesn't update.
Upvotes: 3