Alternate Atlas constructor.

This commit is contained in:
NathanSweet 2013-09-28 21:39:45 +02:00
parent 3797b7b7b0
commit 0c5e3b4400

View File

@ -29,8 +29,8 @@
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/ *****************************************************************************/
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -41,27 +41,27 @@ using Windows.Storage;
#endif #endif
namespace Spine { namespace Spine {
public class Atlas { public class Atlas {
List<AtlasPage> pages = new List<AtlasPage>(); List<AtlasPage> pages = new List<AtlasPage>();
List<AtlasRegion> regions = new List<AtlasRegion>(); List<AtlasRegion> regions = new List<AtlasRegion>();
TextureLoader textureLoader; TextureLoader textureLoader;
#if WINDOWS_STOREAPP #if WINDOWS_STOREAPP
private async Task ReadFile(string path, TextureLoader textureLoader) { private async Task ReadFile(string path, TextureLoader textureLoader) {
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation; var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await folder.GetFileAsync(path).AsTask().ConfigureAwait(false); var file = await folder.GetFileAsync(path).AsTask().ConfigureAwait(false);
using (var reader = new StreamReader(await file.OpenStreamForReadAsync().ConfigureAwait(false))) { using (var reader = new StreamReader(await file.OpenStreamForReadAsync().ConfigureAwait(false))) {
try { try {
Load(reader, Path.GetDirectoryName(path), textureLoader); Load(reader, Path.GetDirectoryName(path), textureLoader);
} catch (Exception ex) { } catch (Exception ex) {
throw new Exception("Error reading atlas file: " + path, ex); throw new Exception("Error reading atlas file: " + path, ex);
} }
} }
} }
public Atlas(String path, TextureLoader textureLoader) { public Atlas(String path, TextureLoader textureLoader) {
this.ReadFile(path, textureLoader).Wait(); this.ReadFile(path, textureLoader).Wait();
} }
#else #else
public Atlas (String path, TextureLoader textureLoader) { public Atlas (String path, TextureLoader textureLoader) {
using (StreamReader reader = new StreamReader(path)) { using (StreamReader reader = new StreamReader(path)) {
@ -74,10 +74,16 @@ namespace Spine {
} }
#endif #endif
public Atlas (TextReader reader, String dir, TextureLoader textureLoader) { public Atlas (TextReader reader, String dir, TextureLoader textureLoader) {
Load(reader, dir, textureLoader); Load(reader, dir, textureLoader);
} }
public Atlas (List<AtlasPage> pages, List<AtlasRegion> regions) {
this.pages = pages;
this.regions = regions;
this.textureLoader = null;
}
private void Load (TextReader reader, String imagesDir, TextureLoader textureLoader) { private void Load (TextReader reader, String imagesDir, TextureLoader textureLoader) {
if (textureLoader == null) throw new ArgumentNullException("textureLoader cannot be null."); if (textureLoader == null) throw new ArgumentNullException("textureLoader cannot be null.");
this.textureLoader = textureLoader; this.textureLoader = textureLoader;
@ -204,6 +210,7 @@ namespace Spine {
} }
public void Dispose () { public void Dispose () {
if (textureLoader == null) return;
for (int i = 0, n = pages.Count; i < n; i++) for (int i = 0, n = pages.Count; i < n; i++)
textureLoader.Unload(pages[i].rendererObject); textureLoader.Unload(pages[i].rendererObject);
} }
@ -263,4 +270,4 @@ namespace Spine {
void Load (AtlasPage page, String path); void Load (AtlasPage page, String path);
void Unload (Object texture); void Unload (Object texture);
} }
} }