]> www.infradead.org Git - users/mchehab/andors-trail.git/commitdiff
Refactoring of TileManager that loads image tiles.
authoroskar.wiksten <oskar.wiksten@08aca716-68be-ccc6-4d58-36f5abd142ac>
Sat, 5 Nov 2011 13:24:59 +0000 (13:24 +0000)
committeroskar.wiksten <oskar.wiksten@08aca716-68be-ccc6-4d58-36f5abd142ac>
Sat, 5 Nov 2011 13:24:59 +0000 (13:24 +0000)
git-svn-id: https://andors-trail.googlecode.com/svn/trunk@201 08aca716-68be-ccc6-4d58-36f5abd142ac

AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/MainActivity.java
AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/ShopActivity.java
AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MovementController.java
AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/tiles/TileCache.java
AndorsTrail/src/com/gpl/rpg/AndorsTrail/resource/tiles/TileManager.java
AndorsTrail/src/com/gpl/rpg/AndorsTrail/util/TimedMessageTask.java
AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/QuickitemView.java

index 44ad7bb8a9de958142a4e198a3b0254ae6afd55f..beff64ac538f6fe1148fac6a6c76bfc470441a11 100644 (file)
@@ -174,12 +174,11 @@ public final class MainActivity extends Activity {
         L.log("onResume");
         if (!AndorsTrailApplication.getApplicationFromActivity(this).setup.isSceneReady) return;
 
+               if (world.model.uiSelections.isInCombat) {
+                       view.combatController.setCombatSelection(world.model.uiSelections.selectedMonster, world.model.uiSelections.selectedPosition);
+                       view.combatController.enterCombat(CombatController.BEGIN_TURN_CONTINUE);
+               }
         view.gameRoundController.resume();
-
-        if (world.model.uiSelections.isInCombat) {
-               view.combatController.setCombatSelection(world.model.uiSelections.selectedMonster, world.model.uiSelections.selectedPosition);
-               view.combatController.enterCombat(CombatController.BEGIN_TURN_CONTINUE);
-        }
     }
 
        @Override
index 66faaed0472008945e4a382d5b3f4d1e9339a1d4..839cd91228509b47912aad5bf3a502b0ac465f7d 100644 (file)
@@ -75,7 +75,7 @@ public final class ShopActivity extends TabActivity implements OnContainerItemCl
         
         HashSet<Integer> iconIDs = world.tileManager.getTileIDsFor(container_buy);
         iconIDs.addAll(world.tileManager.getTileIDsFor(player.inventory));
-        TileCollection tiles = world.tileManager.tileCache.loadTilesFor(iconIDs, res);
+        TileCollection tiles = world.tileManager.loadTilesFor(iconIDs, res);
         buyListAdapter = new ShopItemContainerAdapter(this, tiles, world.tileManager, player, container_buy, this, false);
         sellListAdapter = new ShopItemContainerAdapter(this, tiles, world.tileManager, player, player.inventory, this, true);
                shoplist_buy.setAdapter(buyListAdapter);
index 1c3ff011a8d45e80633f12078a47257c9b51ca15..e33a7fa1211b5b0e1073fecdab4ed04f9721c8b3 100644 (file)
@@ -265,11 +265,15 @@ public final class MovementController implements TimedMessageTask.Callback {
        }
 
        public static void cacheCurrentMapData(final Resources res, final WorldContext world, final PredefinedMap nextMap) {
+               L.log("-> cacheCurrentMapData");
+               long start = System.currentTimeMillis();
                LayeredTileMap mapTiles = TMXMapTranslator.readLayeredTileMap(res, world.tileManager.tileCache, nextMap);
                TileCollection cachedTiles = world.tileManager.loadTilesFor(nextMap, mapTiles, world, res);
                world.model.currentTileMap = mapTiles;
                world.tileManager.currentMapTiles = cachedTiles;
                world.tileManager.cacheAdjacentMaps(res, world, nextMap);
+               long duration = System.currentTimeMillis() - start;
+               L.log("  <- cacheCurrentMapData " + duration + "ms");
        }
        
        
index 59481797091cfe067a713a9c2cbe33b5680f6622..7563c2c0725e95f1551814c80587b1ed4a25b6ad 100644 (file)
@@ -61,7 +61,7 @@ public final class TileCache {
        
        public TileCollection loadTilesFor(Collection<Integer> iconIDs, Resources r) { return loadTilesFor(iconIDs, r, null); }
        public TileCollection loadTilesFor(Collection<Integer> iconIDs, Resources r, TileCollection result) {
-               cleanQueue();
+               if (AndorsTrailApplication.DEVELOPMENT_DEBUGMESSAGES) L.log("TileCache::loadTilesFor({" + iconIDs.size() + " items})");
                int maxTileID = 0;
                HashMap<ResourceFileTileset, HashMap<Integer, ResourceFileTile>> tilesToLoadPerSourceFile = new HashMap<ResourceFileTileset, HashMap<Integer, ResourceFileTile>>();
                for(int tileID : iconIDs) {
@@ -75,6 +75,7 @@ public final class TileCache {
                        maxTileID = Math.max(maxTileID, tileID);
                }
                
+               boolean hasLoadedTiles = false;
                if (result == null) result = new TileCollection(maxTileID);
                for(Entry<ResourceFileTileset, HashMap<Integer, ResourceFileTile>> e : tilesToLoadPerSourceFile.entrySet()) {
                        TileCutter cutter = null;
@@ -91,7 +92,9 @@ public final class TileCache {
                                                if (AndorsTrailApplication.DEVELOPMENT_DEBUGMESSAGES) {
                                                        L.log("Loading tiles from tileset " + e.getKey().tilesetName);
                                                }
+                                               if (!hasLoadedTiles) cleanQueue();
                                                cutter = new TileCutter(e.getKey(), r);
+                                               hasLoadedTiles = true;
                                        }
                                        
                                        bitmap = cutter.createTile(tile.localID);
@@ -102,7 +105,7 @@ public final class TileCache {
                        
                        if (cutter != null) cutter.recycle();
                }
-               cleanQueue();
+               if (hasLoadedTiles) cleanQueue();
                return result;
        }
        
index 1abd823710b0914ce76c3d77383308d8fcb610d7..343c8f5cdc80d00464ab65fe2d462ed36542adfa 100644 (file)
@@ -1,5 +1,6 @@
 package com.gpl.rpg.AndorsTrail.resource.tiles;
 
+import java.util.HashMap;
 import java.util.HashSet;
 
 import android.content.res.Resources;
@@ -24,6 +25,7 @@ import com.gpl.rpg.AndorsTrail.model.map.MapObject;
 import com.gpl.rpg.AndorsTrail.model.map.MonsterSpawnArea;
 import com.gpl.rpg.AndorsTrail.model.map.PredefinedMap;
 import com.gpl.rpg.AndorsTrail.model.map.TMXMapTranslator;
+import com.gpl.rpg.AndorsTrail.util.L;
 
 public final class TileManager {
        public static final int CHAR_HERO = 1;
@@ -46,13 +48,17 @@ public final class TileManager {
     public float scale;
 
     
-       public final TileCache tileCache = new TileCache();
+    public final TileCache tileCache = new TileCache();
        public final TileCollection preloadedTiles = new TileCollection(72);
        public TileCollection currentMapTiles;
        public TileCollection adjacentMapTiles;
        private final HashSet<Integer> preloadedTileIDs = new HashSet<Integer>();
        
        
+       public TileCollection loadTilesFor(HashSet<Integer> tileIDs, Resources r) {
+               return tileCache.loadTilesFor(tileIDs, r);
+       }
+       
        public TileCollection loadTilesFor(ItemContainer container, Resources r) {
                return tileCache.loadTilesFor(getTileIDsFor(container), r);
        }
@@ -142,10 +148,24 @@ public final class TileManager {
         tileCache.loadTilesFor(preloadedTileIDs, r, preloadedTiles);
        }
 
+       private HashMap<String, HashSet<Integer>> tileIDsPerMap = new HashMap<String, HashSet<Integer>>();
+       private void addTileIDsFor(HashSet<Integer> dest, String mapName, final Resources res, final WorldContext world) {
+               HashSet<Integer> cachedTileIDs = tileIDsPerMap.get(mapName);
+               if (cachedTileIDs == null) {
+                       PredefinedMap adjacentMap = world.maps.findPredefinedMap(mapName);
+                       if (adjacentMap == null) return;
+                       LayeredTileMap adjacentMapTiles = TMXMapTranslator.readLayeredTileMap(res, tileCache, adjacentMap);
+                       cachedTileIDs = getTileIDsFor(adjacentMap, adjacentMapTiles, world);
+                       tileIDsPerMap.put(mapName, cachedTileIDs);
+               }
+               dest.addAll(cachedTileIDs);
+       }
        public void cacheAdjacentMaps(final Resources res, final WorldContext world, final PredefinedMap nextMap) {
                (new AsyncTask<Void, Void, Void>()  {
                        @Override
                        protected Void doInBackground(Void... arg0) {
+                               L.log("-> cacheAdjacentMaps");
+                               long start = System.currentTimeMillis();
                                adjacentMapTiles = null;
                                
                                HashSet<String> adjacentMapNames = new HashSet<String>();
@@ -157,13 +177,14 @@ public final class TileManager {
                                
                                HashSet<Integer> tileIDs = new HashSet<Integer>();
                                for (String mapName : adjacentMapNames) {
-                                       PredefinedMap adjacentMap = world.maps.findPredefinedMap(mapName);
-                                       if (adjacentMap == null) continue;
-                                       LayeredTileMap adjacentMapTiles = TMXMapTranslator.readLayeredTileMap(res, tileCache, adjacentMap);
-                                       tileIDs.addAll(getTileIDsFor(adjacentMap, adjacentMapTiles, world));
+                                       addTileIDsFor(tileIDs, mapName, res, world);
                                }
+                               long duration = System.currentTimeMillis() - start;
+                               L.log("  -- cacheAdjacentMaps " + duration + "ms");
                                
                                adjacentMapTiles = tileCache.loadTilesFor(tileIDs, res);
+                               duration = System.currentTimeMillis() - start;
+                               L.log("  <- cacheAdjacentMaps " + duration + "ms");
                                return null;
                        }
                }).execute();
index 425a9513e0b53db237e1b2cde28d5f8a995b3d0e..5a2a4d1b26d056a6a9e9f662074c0dfbe14487b9 100644 (file)
@@ -23,12 +23,13 @@ public final class TimedMessageTask extends Handler {
                if (!isAlive) return;
            if (!hasQueuedTick) return;
        hasQueuedTick = false;
-       if (tick()) queueAnotherTick();
+       tick();
     }
        
-       private boolean tick() {
+       private void tick() {
                nextTickTime = System.currentTimeMillis() + interval;
-               return callback.onTick(this);
+               boolean continueTicking = callback.onTick(this);
+               if (continueTicking) queueAnotherTick();
     }
 
     private void sleep(long delayMillis) {
@@ -56,7 +57,7 @@ public final class TimedMessageTask extends Handler {
        public void start() {
                isAlive = true;
                if (shouldCauseTickOnStart()) tick();
-               queueAnotherTick();
+               else queueAnotherTick();
        }
 
        public void stop() {
index 9f21baa5002020a69622fbb3535602f89baf7677..26e6d18fb01d521c8cd62734602630abfccb65ae 100644 (file)
@@ -1,5 +1,7 @@
 package com.gpl.rpg.AndorsTrail.view;
 
+import java.util.HashSet;
+
 import android.R.color;
 import android.content.Context;
 import android.content.res.Resources;
@@ -15,6 +17,7 @@ import com.gpl.rpg.AndorsTrail.activity.MainActivity;
 import com.gpl.rpg.AndorsTrail.context.ViewContext;
 import com.gpl.rpg.AndorsTrail.context.WorldContext;
 import com.gpl.rpg.AndorsTrail.model.item.ItemType;
+import com.gpl.rpg.AndorsTrail.resource.tiles.TileCollection;
 import com.gpl.rpg.AndorsTrail.resource.tiles.TileManager;
 
 public class QuickitemView extends LinearLayout implements OnClickListener {
@@ -22,7 +25,9 @@ public class QuickitemView extends LinearLayout implements OnClickListener {
 
        private final WorldContext world;
        private final ViewContext view;
-       private final QuickButton[] items = new QuickButton[NUM_QUICK_SLOTS];
+       private final QuickButton[] buttons = new QuickButton[NUM_QUICK_SLOTS];
+       private final HashSet<Integer> loadedTileIDs = new HashSet<Integer>();
+       private TileCollection tiles = null;
 
        public QuickitemView(Context context, AttributeSet attrs) {
                super(context, attrs);
@@ -37,9 +42,9 @@ public class QuickitemView extends LinearLayout implements OnClickListener {
                this.setBackgroundColor(res.getColor(color.transparent));
 
                TypedArray quickButtons = res.obtainTypedArray(R.array.quick_buttons);
-               for(int i = 0; i <items.length ; ++i) {
-                       items[i] = (QuickButton)findViewById(quickButtons.getResourceId(i, -1));
-                       QuickButton item = items[i];
+               for(int i = 0; i < buttons.length; ++i) {
+                       buttons[i] = (QuickButton)findViewById(quickButtons.getResourceId(i, -1));
+                       QuickButton item = buttons[i];
                        item.setIndex(i);
                        world.tileManager.setImageViewTileForUIIcon(item, TileManager.iconID_shop);
                        item.setOnClickListener(this);
@@ -48,7 +53,7 @@ public class QuickitemView extends LinearLayout implements OnClickListener {
        }
        
        public boolean isQuickButtonId(int id){
-               for(QuickButton item: items){
+               for(QuickButton item: buttons){
                        if(item.getId()==id)
                                return true;
                }
@@ -71,21 +76,46 @@ public class QuickitemView extends LinearLayout implements OnClickListener {
        }
        
        public void refreshQuickitems() {
+               loadItemTypeImages();
+               
                for (int i = 0; i < NUM_QUICK_SLOTS; ++i){
-                       QuickButton item = items[i];
+                       QuickButton item = buttons[i];
                        ItemType type = world.model.player.inventory.quickitem[i];
-                       if(type==null) {
+                       if (type == null) {
                                world.tileManager.setImageViewTileForUIIcon(item, TileManager.iconID_shop);
                                item.setEmpty(true);
                        } else {
-                               world.tileManager.setImageViewTileForSingleItemType(item, type, getResources());
+                               world.tileManager.setImageViewTile(item, type, tiles);
                                item.setEmpty(!world.model.player.inventory.hasItem(type.id));
                        }
                }
        }
        
+       private void loadItemTypeImages() {
+               boolean shouldLoadImages = false;
+               for (ItemType type : world.model.player.inventory.quickitem) {
+                       if (type == null) continue;
+                       if (!loadedTileIDs.contains(type.iconID)) {
+                               shouldLoadImages = true;
+                               break;
+                       }
+               }
+               if (!shouldLoadImages) return;
+               
+               HashSet<Integer> iconIDs = new HashSet<Integer>();
+               
+               for (ItemType type : world.model.player.inventory.quickitem) {
+                       if (type == null) continue;
+                       iconIDs.add(type.iconID);
+               }
+               
+               loadedTileIDs.clear();
+               loadedTileIDs.addAll(iconIDs);
+               tiles = world.tileManager.loadTilesFor(iconIDs, getResources());
+       }
+
        public void registerForContextMenu(MainActivity mainActivity) {
-               for(QuickButton item: items)
+               for(QuickButton item: buttons)
                        mainActivity.registerForContextMenu(item);
        }
 }