Get region from TextureAtlas by file name AND page name

Any community contributions to libgdx go here! Some may get included in the core API when permission is granted.

Get region from TextureAtlas by file name AND page name

Postby Thotep » Tue May 31, 2011 1:35 pm

I have several levels in my game and for every level I'm using a different TextureAtlas. But there are some entities with the same names (but different textures) in every level... I wanted to be able to find regions in TextureAtlas specifying the page name. Example:
Code: Select all
TextureRegion hero = atlas.findRegion("hero", "level1"); //when I'm on level 1


Here's the exported SVN patch:

Code: Select all
Index: TextureAtlas.java
===================================================================
--- TextureAtlas.java   (revision 1771)
+++ TextureAtlas.java   (working copy)
@@ -79,13 +79,17 @@
       BufferedReader reader = new BufferedReader(new InputStreamReader(packFile.read()), 64);
       try {
          Texture pageImage = null;
+         String pageName = null;
          while (true) {
             String line = reader.readLine();
             if (line == null) break;
-            if (line.trim().length() == 0)
+            if (line.trim().length() == 0) {
                pageImage = null;
+               pageName = null;
+            }
             else if (pageImage == null) {
                FileHandle file = imagesDir.child(line);
+               pageName = line;
 
                // FIXME - Actually load in the requested format.
                Format format = Format.valueOf(readValue(reader));
@@ -123,6 +127,7 @@
 
                AtlasRegion region = new AtlasRegion(pageImage, left, top, width, height);
                region.name = line;
+               region.page = pageName;
                region.rotate = rotate;
 
                readTuple(reader);
@@ -209,6 +214,21 @@
       }
       return null;
    }
+   
+   /**
+    * Returns the first region found with the specified name and page. This method uses string comparison to find the region, so
+    * the result should be cached rather than calling this method multiple times.
+    * @return The region, or null.
+    */
+   public AtlasRegion findRegion (String name, String page) {
+      for (int i = 0, n = regions.size(); i < n; i++) {
+         AtlasRegion region = regions.get(i);
+         if (!region.name.equals(name)) continue;
+         if (!region.page.equals(page)) continue;
+         return region;
+      }
+      return null;
+   }
 
    /**
     * Returns all regions with the specified name, ordered by smallest to largest {@link AtlasRegion#index index}. This method
@@ -341,6 +361,11 @@
        * packer.
        */
       public String name;
+      
+      /**
+       * The name of the page file.
+       */
+      public String page;
 
       /**
        * The offset from the left of the original image to the left of the packed image, after whitespace was removed for packing.
@@ -388,6 +413,7 @@
          setRegion(region);
          index = region.index;
          name = region.name;
+         page = region.page;
          offsetX = region.offsetX;
          offsetY = region.offsetY;
          packedWidth = region.packedWidth;


Any chances for including this in libgdx?
@thotep, made Prophour23, currently working on PANZERBOMB. My blog: http://thesecretpie.com
Thotep
 
Posts: 51
Joined: Fri Dec 03, 2010 7:41 am

Re: Get region from TextureAtlas by file name AND page name

Postby appel » Wed Jun 29, 2011 11:39 pm

I think this is a good idea.

But what I'd really like to see is the ability of retrieving a "region" by the directory structure they were in. So if you have your source textures in a structure like:

textures/tanks/bigtank.png
textures/tanks/smalltank.png
textures/soldiers/infantry.png

You would be able to do:

atlas.getRegion("textures/tanks/bigtank.png");
atlas.getRegion("textures/tanks/smalltank.png");
atlas.getRegion("textures/soldiers/infantry.png");

But they should all belong to the same atlas.

Currently I have to put all my textures in the same directory, because some textures may share same names, e.g.:
textures/bigtank/base.png
textures/smalltank/base.png

doing atlas.findRegion("base") wouldn't work very well, so I have to force myself to rename it to bigtankbase.png and smalltankbase.png.
appel
 
Posts: 8
Joined: Wed Jun 29, 2011 1:53 pm

Re: Get region from TextureAtlas by file name AND page name

Postby mzechner » Thu Jun 30, 2011 8:51 am

I'll add your path tonight. Thanks a bunch.
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm


Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 1 guest