--- /dev/null
-//
-// AnimationContainer.m
-// tanks
-//
-// Created by Doris Chen on 5/12/11.
-// Copyright 2011 __MyCompanyName__. All rights reserved.
-//
-
+ #import "AnimationContainer.h"
+
+ #import "SPMovieClip.h"
+
++
+ ////////////////////////////////////////////////////////////////////////////////////
+ // private method
+ @interface AnimationContainer()
+
+ -(void)parseContentXml:(NSString *)contentPath;
+
+ @end
+
+
+ ////////////////////////////////////////////////////////////////////////////////////
+ @implementation AnimationContainer
+
+ ////////////////////////////////////////////////////////////////////////////////////
+ @synthesize mAnimationLookup;
+ @synthesize mAtlas;
+ @synthesize mAtlasPath;
+ @synthesize mAnimationName;
+
+ ////////////////////////////////////////////////////////////////////////////////////
+ -(id)init {
+ if ((self = [super init])) {
+ self.mAnimationLookup = [[NSMutableDictionary alloc] init];
+ }
+
+ return self;
+ }
+
+ ////////////////////////////////////////////////////////////////////////////////////
+ -(id)initWithContainer:(NSString *)containerPath {
+ if ((self = [super init])) {
+ [self parseContentXml:containerPath];
+ }
+
+ return self;
+ }
+
+ ////////////////////////////////////////////////////////////////////////////////////
+ -(void)dealloc {
+ [mAnimationLookup removeAllObjects];
+ [mAnimationLookup release];
+ [mAtlas release];
+ [mAtlasPath release];
+ [mAnimationName release];
+ [super dealloc];
+ }
+
+ ////////////////////////////////////////////////////////////////////////////////////
+ // need a way to get the VRAM back
+ -(void)releaseVRAM {
+ [mAnimationLookup removeAllObjects]; // remove all the movie clips, since they hold on to stuff in the atlas, but keep the dictionary
+ [mAtlas release]; // remove the atlas
+ mAtlas = nil; // is this extraneous?
+ }
+
+ ////////////////////////////////////////////////////////////////////////////////////
+ -(void)parseContentXml:(NSString *)contentPath {
+ NSAutoreleasePool* autoreleasePool = [[NSAutoreleasePool alloc] init];
+
+ if (!contentPath) return;
+
+ NSString* fullPath = [[NSBundle mainBundle] pathForResource:contentPath ofType:nil];
+ NSURL* xmlUrl = [NSURL fileURLWithPath:fullPath];
+ NSXMLParser* xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlUrl];
+ xmlParser.delegate = self;
+ BOOL success = [xmlParser parse];
+
+ [autoreleasePool drain]; // is this wrong?
+
+ if (!success) {
+ NSLog(@"could not parse content XML %@. Error code: %d, domain: %@",
+ contentPath, xmlParser.parserError.code, xmlParser.parserError.domain);
+ }
+
+ [xmlParser release];
+ }
+
+ ////////////////////////////////////////////////////////////////////////////////////
+ // XML parser callback for the following format:
+ //
+ // <AnimationClips name='foo' xmlPath='dir/foo.xml'>
+ // <Clip name='do_bar' frames='15' />
+ // <Clip name='do_foo' frames='5' />
+ // </AnimationClips>
+ //
+ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
+ namespaceURI: (NSString *)namespaceURI
+ qualifiedName: (NSString *)qName
+ attributes: (NSDictionary *)attributeDict {
+ if ([elementName isEqualToString:@"AnimationClips"])
+ {
+ mAnimationLookup = [[NSMutableDictionary alloc] init];
+ self.mAnimationName = [attributeDict valueForKey:@"name"];
+ self.mAtlasPath = [attributeDict valueForKey:@"xmlPath"];
+ mAtlas = [[SPTextureAtlas alloc] initWithContentsOfFile:mAtlasPath];
+ }
+ else if ([elementName isEqualToString:@"Clip"])
+ {
+ NSString* name = [attributeDict valueForKey:@"name"];
+ int numFrames = [[attributeDict valueForKey:@"frames"] intValue];
+ SPMovieClip* movieClip = [[SPMovieClip alloc] initWithFrames:[mAtlas texturesStartingWith:name] fps:15];
+ [mAnimationLookup setValue:movieClip forKey:name]; // add our clip to the dictionary so we can reference it in the future
+
+ if (movieClip.numFrames != numFrames)
+ {
+ NSLog(@"Number of frames (%d) in XML does not match number of frames (%d) in atlas!", numFrames, movieClip.numFrames);
+ }
+ }
+ }
+
+
+ @end
49F2DADE13764ED6000B6B8C /* SPUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 49F2DA8113764ED6000B6B8C /* SPUtils.m */; };
49F2DADF13764ED6000B6B8C /* Sparrow.h in Headers */ = {isa = PBXBuildFile; fileRef = 49F2DA1D13764ED5000B6B8C /* Sparrow.h */; };
49F2DB341376632E000B6B8C /* Unit.h in Headers */ = {isa = PBXBuildFile; fileRef = 49F2DB321376632E000B6B8C /* Unit.h */; };
- 49F2DB351376632E000B6B8C /* Unit.m in Sources */ = {isa = PBXBuildFile; fileRef = 49F2DB331376632E000B6B8C /* Unit.m */; };
+ 49F2DB351376632E000B6B8C /* Unit.mm in Sources */ = {isa = PBXBuildFile; fileRef = 49F2DB331376632E000B6B8C /* Unit.mm */; };
+ 4B8B2A3213784D2D00CA4076 /* tank-pink.png in Resources */ = {isa = PBXBuildFile; fileRef = 4B8B2A3113784D2D00CA4076 /* tank-pink.png */; };
- 4B8B2A411378E74700CA4076 /* Box2DAppDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B8B2A351378E74700CA4076 /* Box2DAppDelegate.h */; };
- 4B8B2A421378E74700CA4076 /* Box2DAppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B8B2A361378E74700CA4076 /* Box2DAppDelegate.mm */; };
- 4B8B2A431378E74700CA4076 /* Box2DView.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B8B2A371378E74700CA4076 /* Box2DView.h */; };
- 4B8B2A441378E74700CA4076 /* Box2DView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B8B2A381378E74700CA4076 /* Box2DView.mm */; };
- 4B8B2A451378E74700CA4076 /* Delegates.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B8B2A391378E74700CA4076 /* Delegates.h */; };
- 4B8B2A461378E74700CA4076 /* GLES-Render.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B8B2A3A1378E74700CA4076 /* GLES-Render.h */; };
- 4B8B2A471378E74700CA4076 /* GLES-Render.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B8B2A3B1378E74700CA4076 /* GLES-Render.mm */; };
- 4B8B2A481378E74700CA4076 /* iPhoneTest.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B8B2A3C1378E74700CA4076 /* iPhoneTest.h */; };
- 4B8B2A491378E74700CA4076 /* iPhoneTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B8B2A3D1378E74700CA4076 /* iPhoneTest.mm */; };
- 4B8B2A4A1378E74700CA4076 /* iPhoneTestEntries.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B8B2A3E1378E74700CA4076 /* iPhoneTestEntries.mm */; };
- 4B8B2A4B1378E74700CA4076 /* TestEntriesViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B8B2A3F1378E74700CA4076 /* TestEntriesViewController.h */; };
- 4B8B2A4C1378E74700CA4076 /* TestEntriesViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4B8B2A401378E74700CA4076 /* TestEntriesViewController.mm */; };
+ 4B8B2A50137D098500CA4076 /* AnimationContainer.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B8B2A4E137D098500CA4076 /* AnimationContainer.h */; };
+ 4B8B2A51137D098500CA4076 /* AnimationContainer.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B8B2A4F137D098500CA4076 /* AnimationContainer.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
49F2DA7F13764ED6000B6B8C /* SPPoolObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPPoolObject.m; sourceTree = "<group>"; };
49F2DA8013764ED6000B6B8C /* SPUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPUtils.h; sourceTree = "<group>"; };
49F2DA8113764ED6000B6B8C /* SPUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUtils.m; sourceTree = "<group>"; };
- 49F2DAE113765004000B6B8C /* Box2DAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Box2DAppDelegate.h; sourceTree = "<group>"; };
- 49F2DAE213765004000B6B8C /* Box2DAppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = Box2DAppDelegate.mm; sourceTree = "<group>"; };
- 49F2DAE313765004000B6B8C /* Box2DView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Box2DView.h; sourceTree = "<group>"; };
- 49F2DAE413765004000B6B8C /* Box2DView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = Box2DView.mm; sourceTree = "<group>"; };
- 49F2DAE513765004000B6B8C /* Delegates.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Delegates.h; sourceTree = "<group>"; };
- 49F2DAE813765004000B6B8C /* iPhoneTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = iPhoneTest.h; sourceTree = "<group>"; };
- 49F2DAE913765004000B6B8C /* iPhoneTest.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = iPhoneTest.mm; sourceTree = "<group>"; };
- 49F2DAEA13765004000B6B8C /* iPhoneTestEntries.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = iPhoneTestEntries.mm; sourceTree = "<group>"; };
- 49F2DAEB13765004000B6B8C /* TestEntriesViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestEntriesViewController.h; sourceTree = "<group>"; };
- 49F2DAEC13765004000B6B8C /* TestEntriesViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TestEntriesViewController.mm; sourceTree = "<group>"; };
49F2DB321376632E000B6B8C /* Unit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Unit.h; sourceTree = "<group>"; };
- 49F2DB331376632E000B6B8C /* Unit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Unit.m; sourceTree = "<group>"; };
+ 49F2DB331376632E000B6B8C /* Unit.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = Unit.mm; sourceTree = "<group>"; };
+ 4B8B2A3113784D2D00CA4076 /* tank-pink.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "tank-pink.png"; path = "textures/tank-pink.png"; sourceTree = "<group>"; };
- 4B8B2A351378E74700CA4076 /* Box2DAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Box2DAppDelegate.h; path = references/Box2DAppDelegate.h; sourceTree = "<group>"; };
- 4B8B2A361378E74700CA4076 /* Box2DAppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = Box2DAppDelegate.mm; path = references/Box2DAppDelegate.mm; sourceTree = "<group>"; };
- 4B8B2A371378E74700CA4076 /* Box2DView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Box2DView.h; path = references/Box2DView.h; sourceTree = "<group>"; };
- 4B8B2A381378E74700CA4076 /* Box2DView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = Box2DView.mm; path = references/Box2DView.mm; sourceTree = "<group>"; };
- 4B8B2A391378E74700CA4076 /* Delegates.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Delegates.h; path = references/Delegates.h; sourceTree = "<group>"; };
- 4B8B2A3A1378E74700CA4076 /* GLES-Render.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "GLES-Render.h"; path = "references/GLES-Render.h"; sourceTree = "<group>"; };
- 4B8B2A3B1378E74700CA4076 /* GLES-Render.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = "GLES-Render.mm"; path = "references/GLES-Render.mm"; sourceTree = "<group>"; };
- 4B8B2A3C1378E74700CA4076 /* iPhoneTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = iPhoneTest.h; path = references/iPhoneTest.h; sourceTree = "<group>"; };
- 4B8B2A3D1378E74700CA4076 /* iPhoneTest.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = iPhoneTest.mm; path = references/iPhoneTest.mm; sourceTree = "<group>"; };
- 4B8B2A3E1378E74700CA4076 /* iPhoneTestEntries.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = iPhoneTestEntries.mm; path = references/iPhoneTestEntries.mm; sourceTree = "<group>"; };
- 4B8B2A3F1378E74700CA4076 /* TestEntriesViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestEntriesViewController.h; path = references/TestEntriesViewController.h; sourceTree = "<group>"; };
- 4B8B2A401378E74700CA4076 /* TestEntriesViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = TestEntriesViewController.mm; path = references/TestEntriesViewController.mm; sourceTree = "<group>"; };
+ 4B8B2A4E137D098500CA4076 /* AnimationContainer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AnimationContainer.h; path = animation/AnimationContainer.h; sourceTree = "<group>"; };
+ 4B8B2A4F137D098500CA4076 /* AnimationContainer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AnimationContainer.m; path = animation/AnimationContainer.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
49F2D9AE13764666000B6B8C /* src */ = {
isa = PBXGroup;
children = (
- 49F2D9AF13764666000B6B8C /* display */,
- 4B8B2A341378E72700CA4076 /* references */,
- 49F2D9AF13764666000B6B8C /* display */,
- 49F2D9B013764666000B6B8C /* main.m */,
+ 49F2D9B013764666000B6B8C /* main.mm */,
49F2D9B113764666000B6B8C /* physics */,
49F2D9B213764666000B6B8C /* prefix.pch */,
49F2D9B313764666000B6B8C /* render */,
-- 49F2D9B413764666000B6B8C /* RootAppDelegate.h */,
- 49F2D9B513764666000B6B8C /* RootAppDelegate.mm */,
- 49F2D9B513764666000B6B8C /* RootAppDelegate.m */,
49F2D9B613764666000B6B8C /* tanks */,
49F2D9B913764666000B6B8C /* ui */,
);
path = src;
sourceTree = "<group>";
};
-- 49F2D9AF13764666000B6B8C /* display */ = {
-- isa = PBXGroup;
-- children = (
-- );
-- path = display;
-- sourceTree = "<group>";
-- };
49F2D9B113764666000B6B8C /* physics */ = {
isa = PBXGroup;
children = (
path = unit;
sourceTree = "<group>";
};
+ 4B8B2A3313784D3400CA4076 /* textures */ = {
+ isa = PBXGroup;
+ children = (
+ 4B8B2A3113784D2D00CA4076 /* tank-pink.png */,
+ );
+ name = textures;
+ sourceTree = "<group>";
+ };
- 4B8B2A341378E72700CA4076 /* references */ = {
- isa = PBXGroup;
- children = (
- 4B8B2A351378E74700CA4076 /* Box2DAppDelegate.h */,
- 4B8B2A361378E74700CA4076 /* Box2DAppDelegate.mm */,
- 4B8B2A371378E74700CA4076 /* Box2DView.h */,
- 4B8B2A381378E74700CA4076 /* Box2DView.mm */,
- 4B8B2A391378E74700CA4076 /* Delegates.h */,
- 4B8B2A3A1378E74700CA4076 /* GLES-Render.h */,
- 4B8B2A3B1378E74700CA4076 /* GLES-Render.mm */,
- 4B8B2A3C1378E74700CA4076 /* iPhoneTest.h */,
- 4B8B2A3D1378E74700CA4076 /* iPhoneTest.mm */,
- 4B8B2A3E1378E74700CA4076 /* iPhoneTestEntries.mm */,
- 4B8B2A3F1378E74700CA4076 /* TestEntriesViewController.h */,
- 4B8B2A401378E74700CA4076 /* TestEntriesViewController.mm */,
- );
- name = references;
- sourceTree = "<group>";
- };
+ 4B8B2A4D137D090D00CA4076 /* animation */ = {
+ isa = PBXGroup;
+ children = (
+ 4B8B2A4E137D098500CA4076 /* AnimationContainer.h */,
+ 4B8B2A4F137D098500CA4076 /* AnimationContainer.m */,
+ );
+ name = animation;
+ sourceTree = "<group>";
+ };
/* End PBXGroup section */
/* Begin PBXHeadersBuildPhase section */
49F2DADB13764ED6000B6B8C /* SPPoolObject.h in Headers */,
49F2DADD13764ED6000B6B8C /* SPUtils.h in Headers */,
49F2DADF13764ED6000B6B8C /* Sparrow.h in Headers */,
+ 49F2D9CD13764710000B6B8C /* RootAppDelegate.h in Headers */,
+ 49F2D9CF13764710000B6B8C /* RootAppDelegate_iPad.h in Headers */,
+ 49F2D9D013764710000B6B8C /* RootAppDelegate_iPhone.h in Headers */,
+ 49F2D9CE13764710000B6B8C /* Game.h in Headers */,
49F2DB341376632E000B6B8C /* Unit.h in Headers */,
- 4B8B2A411378E74700CA4076 /* Box2DAppDelegate.h in Headers */,
- 4B8B2A431378E74700CA4076 /* Box2DView.h in Headers */,
- 4B8B2A451378E74700CA4076 /* Delegates.h in Headers */,
- 4B8B2A461378E74700CA4076 /* GLES-Render.h in Headers */,
- 4B8B2A481378E74700CA4076 /* iPhoneTest.h in Headers */,
- 4B8B2A4B1378E74700CA4076 /* TestEntriesViewController.h in Headers */,
+ 49DA67D4137847A7004841E9 /* World.h in Headers */,
+ 49E834441380CB61007A6598 /* GLES-Render.h in Headers */,
+ 49E834501380E234007A6598 /* Displayable.h in Headers */,
+ 49E834531380EBB2007A6598 /* Actor.h in Headers */,
+ 49E8345613810618007A6598 /* Active.h in Headers */,
+ 4B8B2A50137D098500CA4076 /* AnimationContainer.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
49F2DADA13764ED6000B6B8C /* SPNSExtensions.m in Sources */,
49F2DADC13764ED6000B6B8C /* SPPoolObject.m in Sources */,
49F2DADE13764ED6000B6B8C /* SPUtils.m in Sources */,
- 49F2D9C413764666000B6B8C /* main.mm in Sources */,
- 49F2DB351376632E000B6B8C /* Unit.m in Sources */,
- 4B8B2A421378E74700CA4076 /* Box2DAppDelegate.mm in Sources */,
- 4B8B2A441378E74700CA4076 /* Box2DView.mm in Sources */,
- 4B8B2A471378E74700CA4076 /* GLES-Render.mm in Sources */,
- 4B8B2A491378E74700CA4076 /* iPhoneTest.mm in Sources */,
- 4B8B2A4A1378E74700CA4076 /* iPhoneTestEntries.mm in Sources */,
- 4B8B2A4C1378E74700CA4076 /* TestEntriesViewController.mm in Sources */,
+ 49F2D9C513764666000B6B8C /* RootAppDelegate.mm in Sources */,
+ 49F2D9C813764666000B6B8C /* RootAppDelegate_iPad.mm in Sources */,
+ 49F2D9CA13764666000B6B8C /* RootAppDelegate_iPhone.mm in Sources */,
+ 49DA67D5137847A7004841E9 /* World.mm in Sources */,
+ 49F2D9C613764666000B6B8C /* Game.mm in Sources */,
+ 49F2DB351376632E000B6B8C /* Unit.mm in Sources */,
+ 49E834451380CB61007A6598 /* GLES-Render.mm in Sources */,
+ 49E834541380EBB2007A6598 /* Actor.mm in Sources */,
+ 4B8B2A51137D098500CA4076 /* AnimationContainer.m in Sources */,
++ 49F2D9C413764666000B6B8C /* main.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};