Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  Generated Enums for Resources  (Read 507 times)
0 Members and 1 Guest are viewing this topic.
Offline davedes
« Posted 2013-05-26 21:09:28 »

I've never really been a fan of using Strings and hash maps to store and use my game's images. It leads to ugly code, and a small performance impact (I know... premature optimization blah blah). There is also no compile-time checking or content assist, which I've fallen in love with since the introduction of enums and generics.

So here is a crazy idea... Let's say you're already pre-processing your game's assets into texture atlases with a tool like Texturepacker-GUI or TexturePacker2. During this step, we could also generate a Java source file for an Enum class which contains all of our resources. For example, take the following input images that will be packed into a TextureAtlas:
1  
2  
3  
4  
grass.png
font_small.png
pbar1.png
blur_box.png


Our pre-processing step generates the following Enum source code:
1  
2  
3  
4  
5  
6  
public enum Images {
   grass,
   font_small,
   pbar1,
   blur_box,
}


Then, we could render a particular sprite like so:

1  
2  
3  
4  
5  
6  
7  
8  
   //convenience method...
  public AtlasRegion getRegion(Images image) {
      return atlas.getRegions().get(image.ordinal());
   }
   
   public void render() {
      batch.draw( getRegion(Images.font_small), 0, 0 );
   }


And with a little tweaking could generate nested public static enums for sub-directories in our input, so that we could do things like this:
Images.hud.text.font_small;


It would also simplify things like animations. For example, you could generate a final array after your enum declarations:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public enum Images {
    //generated from image file names
   grass,
    playerWalk1,
    playerWalk2,
    playerWalk3;
   
    //generated arrays, for images with associates indices  
   public static final Images[] playerWalk = { playerWalk1, playerWalk2, playerWalk3 };
}


It would be ideal if you could re-generate the atlas and enum source automatically whenever the image folder is changed.

Anyways.... Just a crazy thought. Probably not a great idea in practice, but it sounds interesting on paper.

Offline Roquen
« Reply #1 - Posted 2013-05-26 22:41:30 »

I can't see any purpose.  Just generate an ID if you want a unique numeric value for each and call it a day.
Offline Danny02
« Reply #2 - Posted 2013-05-26 23:23:40 »

btw I already did something like that
- a compiler plugin which automaticly packs all images which are used by your game into a texture atlass at compile time.
- a infrastructure which would enable custom generation stuff

you can find on the forum
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2013-05-27 07:17:08 »

It's faster for me to type manually.

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.

xsi3rr4x (31 views)
2014-04-15 18:08:23

BurntPizza (28 views)
2014-04-15 03:46:01

UprightPath (43 views)
2014-04-14 17:39:50

UprightPath (26 views)
2014-04-14 17:35:47

Porlus (42 views)
2014-04-14 15:48:38

tom_mai78101 (64 views)
2014-04-10 04:04:31

BurntPizza (124 views)
2014-04-08 23:06:04

tom_mai78101 (224 views)
2014-04-05 13:34:39

trollwarrior1 (190 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!