Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (711)
Games in Android Showcase (213)
games submitted by our members
Games in WIP (785)
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  
  SpriteAtlas Made Easy  (Read 4135 times)
0 Members and 1 Guest are viewing this topic.
Offline StumpyStrust
« Posted 2013-10-27 07:25:43 »

Well this is not quite as easy as actually packing things before hand, (and not as fast) but is great for prototyping and adding stuff without having to repack every time. I made these classes when I made my Atlas engine which is crap but it was nice not having to deal with json/xml and still get atlases.

Here is the paste bin for the 3 classes. <---- Atlas class <---- BufferedImageUtils class <---- TextureRegion class.

Loads images onto a BufferedImage via java2d and a given name for the image. It will save the region coordinates of each image. Once all images are added, it will create a texture from the BufferedImage. You can then grab any region that was loaded. Here is example usage.

      Atlas a1 = new Atlas(1024, 1024, "a1");
      a1.addTexStrip(BufferedImageUtils.loadImageStrip("res/bloodSplat.png", 2, 2, 4, 128, 128), "bloodSplash");
      a1.addTex(BufferedImageUtils.loadImage("res/player/idle.png", true), "playerTopIdle");
      a1.addTex(BufferedImageUtils.loadImage("res/player/botIdle.png", true), "playerBotIdle");
      a1.addTex(BufferedImageUtils.loadImage("res/player/fire1.png", true), "playerFire1");
      a1.addTex(BufferedImageUtils.loadImage("res/player/fire2.png", true), "playerFire2");

//later in code
                TextureRegion topIdle,botIdle,fire1,fire2;
      topIdle = a1.getTexRegion("playerTopIdle");
      botIdle = a1.getTexRegion("playerBotIdle");
      fire1 = a1.getTexRegion("playerFire1");
      fire2 = a1.getTexRegion("playerFire2");
//loading a strip of images into an array of TextureRegions
      FxBloodSplat b = new FxBloodSplat(l.x + Rnd.random(1), l.y + Rnd.random(1), a1.getRegion("bloodSplash", 3), (int) a);

   public static void drawImage(float x, float y, float w, float h, TextureRegion tex, Vector4f color)
      glBindTexture(GL_TEXTURE_2D, tex.texID);
         glColor4f(color.x, color.y, color.z, color.w);
         glTexCoord2f(tex.region[0], tex.region[1]);
         glVertex2f(x - w / 2, y - h / 2);
         glTexCoord2f(tex.region[2], tex.region[3]);
         glVertex2f(x + w / 2, y - h / 2);
         glTexCoord2f(tex.region[4], tex.region[5]);
         glVertex2f(x + w / 2, y + h / 2);
         glTexCoord2f(tex.region[6], tex.region[7]);
         glVertex2f(x - w / 2, y + h / 2);

This is quick and dirty and can very much be improved. It is fast enough for smaller games but for a larger project, use libGDX with their oh so nice texture packer. This should be an easy introduction for people looking to play with texture atlases before delving into libGDX or their own versions. I use it a bunch for my non-libgdx stuff as I am too damn lazy to use all the fancy texture packers.


Offline Danny02
« Reply #1 - Posted 2013-10-27 21:55:26 »

I did a java compiler "addon" a while back, to do just something like this. You specify all the single images in your code and at compile-time a texture atlass is created automaticly by the compiler.

take a look, perhaps you will like it Smiley:
Offline StumpyStrust
« Reply #2 - Posted 2013-10-27 23:32:16 »

I remember reading when you first posted that and was like, "I am not quite sure how that works." Now I am like, "Hot reloading images? **** yeah!" I may look into that lib in the feature. I am not a huge fan of annotations (probably from lack of experience). I like specifying all resources before hand which is what your lib does but not in the actually code.

+1 btw.

If you specify the same resource twice, will it load it twice?

        @InjectResource(file = "resources/textures/ball.png")
        private TextureAtlasElement normal;
        @InjectResource(file = "resources/textures/ball.png")
        private TextureAtlasElement glow;

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2013-10-28 04:43:53 »

The thing is, I created a very flexible resource system. You can extend it very easily with your own file formats, caches, processors(like the atlass creation) and own hot reload functionality.

I did the AutoAtlas in just 1 hour and it was only intended as a proof of concept, take a look at the source. Beside the Atlass stuff I "stole" from libgdx, the whole project is only some couple lines of code.

Every resource gets process only once(by every registered resource processor). How many times a resource is loaded on runtime depends on your caching strategy.

You could probably implement the same thing for your Texture Atlas implementation in a couple of lines too.

ps: the annotations are the only way in Java to hook yourself into the compiler. Take a look at annotation processing if you are interested. I btw hid as much as possible of the complexity of this.
Offline Nate

« JGO Bitwise Duke »

Medals: 167
Projects: 4
Exp: 14 years

Esoteric Software

« Reply #4 - Posted 2013-10-28 21:41:58 »

FWIW and a bit off topic from the OP, libgdx has AssetManager which does ref counting of assets, assets can have dependencies, and assets can do loading on the GL thread, a separate thread, or both (which enables a loading screen to be shown while loading assets). It was a huge pain to get right, but it works well.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

numerical (371 views)
2017-02-21 07:32:16

numerical (372 views)
2017-02-21 07:31:46

theagentd (484 views)
2017-02-18 13:42:33

theagentd (482 views)
2017-02-18 13:35:16

h.pernpeintner (1647 views)
2017-01-24 22:39:11

h.pernpeintner (1633 views)
2017-01-24 22:38:32

Galdo (2203 views)
2017-01-12 13:44:09

Archive (2161 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2699 views)
2016-12-16 03:58:39

0AndrewShepherd0 (2355 views)
2016-12-15 21:50:57
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 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‑
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!