Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Enum and texture  (Read 559 times)
0 Members and 1 Guest are viewing this topic.
Offline Zoub

Junior Newbie





« Posted 2013-09-04 10:59:19 »

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  Smiley 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 :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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!
Offline StrideColossus
« Reply #1 - Posted 2013-09-04 12:01:23 »

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.

1  
2  
3  
4  
5  
6  
7  
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

Welcome!   Grin
Offline davedes
« Reply #2 - Posted 2013-09-04 12:44:44 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zoub

Junior Newbie





« Reply #3 - Posted 2013-09-04 13:27:22 »

Thank you for your responses  Grin , 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 ?
Online actual

JGO Coder


Medals: 24



« Reply #4 - Posted 2013-09-04 14:43:00 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
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.
Offline Zoub

Junior Newbie





« Reply #5 - Posted 2013-09-05 11:42:15 »

Ok, that's right, it's kinda stupid to load texture in enum, i'm aware of that now Grin . 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?
Offline StrideColossus
« Reply #6 - Posted 2013-09-05 12:00:02 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

PocketCrafter7 (14 views)
2014-11-28 16:25:35

PocketCrafter7 (9 views)
2014-11-28 16:25:09

PocketCrafter7 (10 views)
2014-11-28 16:24:29

toopeicgaming1999 (76 views)
2014-11-26 15:22:04

toopeicgaming1999 (67 views)
2014-11-26 15:20:36

toopeicgaming1999 (16 views)
2014-11-26 15:20:08

SHC (30 views)
2014-11-25 12:00:59

SHC (28 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!