using System; using System.Collections.Generic; using System.Text; using CommonLang; using CommonUI.Cell.Game; namespace CommonUI.Cell { public class CPJResource : IDisposable { protected CPJLoader loader; protected HashMap images = new HashMap(); protected HashMap sprites = new HashMap(); // ------------------------------------------------------------------------------------- public CPJResource(CPJLoader loader) { this.loader = loader; foreach (ImagesSet img in loader.ImgTable.Values) { CPJAtlas atlas = null; if (img.IsTiles) { atlas = new CPJAtlasTiles(img, this); } else if (img.SplitSize != 0) { atlas = new CPJAtlasSplitGroup(img, this); } else { atlas = new CPJAtlasGroup(img, this); } images.Add(img.Name, atlas); } } public CPJLoader Loader { get { return loader; } } public void Dispose() { if (images != null) { foreach (CPJAtlas img in images.Values) { img.Dispose(); } images = null; } if (sprites != null) { sprites.Clear(); sprites = null; } if (loader != null) { loader.Dispose(); loader = null; } } public CPJAtlas GetAtlas(String key) { return images.Get(key); } public ImagesSet GetSetImages(String key) { return loader.ImgTable.Get(key); } public CSpriteMeta GetSpriteMeta(String key) { if (sprites.ContainsKey(key)) { return sprites.Get(key); } else { SpriteSet ss = loader.SprTable.Get(key); if (ss != null) { CPJAtlas img = images.Get(ss.ImagesName); if (img != null) { CSpriteMeta ret = new CSpriteMeta(ss, img); sprites.Put(key, ret); return ret; } } return null; } } public void ReleaseTexture() { if (images != null) { foreach (CPJAtlas img in images.Values) { img.ReleaseTexture(); } } } //------------------------------------------------------------------------------------- public static CPJResource CreateResource(string file_name) { string prefix = file_name.Substring(0, file_name.LastIndexOf('.')); if (CommonLang.IO.Resource.ExistData(prefix + ".bin")) { CPJLoaderBIN loader = new CPJLoaderBIN(prefix + ".bin"); return new CPJResource(loader); } else { CPJLoaderXML loader = new CPJLoaderXML(file_name); return new CPJResource(loader); } } //------------------------------------------------------------------------------------- } }