Enum and texture

Hello everyone,
I’m new on this forum and very glad i found it because i’m developing a java game with LWJGL for my own enjoyment and i didn’t know about this community :slight_smile: The game i develop is a mix between a card game and a tower defense, somewhat basic in the programming but i’m a beginner (and french so excuse me if my english is weird).
I have a question to which i didn’t find any answer. here goes :
In my game, the player can choose between multiple clans and i wondered where i can load the texture for the different clans ? Can i take my textures in an enum without loading them ? But the problem is I don’t want to load the texture of the other clans if they’re not being used. Here the code :

public enum Clan {
	
	HUMAN("human.png"), ORC("orc.png");
	
	private Texture texture = null;
	
	private int texIdCards = 0;
	
	private Clan(String texCard){
		try{
			texture = TextureLoader.getTexture("PNG" , ResourceLoader.getResourceAsStream(texCard));
         texIdCards = texture.getTextureID();
		} catch (Exception e){
			System.out.println("Can't load Textures : " + texture);
		}
	}
   public int getTexIdCards(){
		return texIdCards;
	}
}

Will the program load all the texture right when i’ll run it ? If the battle is Human versus Human, I don’t want the Orc texture to take any space in computer’s memory, so is it a good idea to put the loader Texture in an enum ?

Thank you very much!

Yes I expect that code will load all your textures since the constructor for each enum constant will be invoked when you first use that enum class.

You might want to consider lazy-loading of your texture rather than loading it in the constructor?

e.g.

public int getTexIdCards(){
     if( texture == null ) {
          // load the texture and its ID here instead
     }

      return texIdCards;
   }

[quote]I’m new on this forum and very glad i found it because i’m developing a java game with LWJGL for my own enjoyment and i didn’t know about this community
[/quote]
Welcome! ;D

The problem is that you need to load textures on the GL thread. Also, it’s usually wise to have a preloader of some sort at the beginning of your game, rather than make the game hang with a black screen.

Another option is to just store the String reference for each enum value. Though maybe enums are not the right solution for this.

Thank you for your responses ;D , I will consider the two options. Correct me if i’m wrong, but if i load the texture in the enum function, i do must write another “texture = null” or “texture.release()” function that frees the memory, right ?

If you really want to go the enum route and do not want to load unnecessary textures, try pushing the texture loading out of the constructor.


public enum Clan {
   
   HUMAN("human.png"), ORC("orc.png");

   private String imageFile;
   private int textIdCards;

   private Clan(String file) {
      imageFile = file;
   }


   public void loadTexture() {
        try{
         texture = TextureLoader.getTexture("PNG" , ResourceLoader.getResourceAsStream(imageFile));
         texIdCards = texture.getTextureID();
      } catch (Exception e){
         System.out.println("Can't load Textures : " + texture);
      }
   }

   public int getTexIdCards(){
      return texIdCards;
   }
}

Then you can call Clan.HUMAN.loadTexture() or Clan.ORC.loadTexture() as needed.

Ok, that’s right, it’s kinda stupid to load texture in enum, i’m aware of that now ;D . But the problem now , if i’m not wrong, is that OpenGL is single threaded right? I’ve tried to load textures in a separate thread and i’ve got the following message : ‘java.lang.RuntimeException: No OpenGL context found in the current thread’.
I think the best way is to load Image and convert it to ImageBuffer. if i do this, the thread return the ImageBuffer in the OpenGL thread, convert it to Texture and release the ImageBuffer. Do you think this would be more efficient?

Yes, you have to upload the texture to the GPU on the OpenGL thread, otherwise you get the runtime exception you encountered.

And yes, the best approach is to load the image on a separate background thread, and then create / upload the texture in the OpenGL thread where the context is available. That way the rendering thread is still running (i.e. you’re still seeing something) while the image is loading.

But if you’re just putting together a small demo then I wouldn’t complicate matters by having a separate thread, just do it all in one fell swoop. Once you’ve got that working you can look at separating them into multiple threads latter perhaps.