From 24a764d8c41d6bf13960e4da7cd9d0f06dc1dd52 Mon Sep 17 00:00:00 2001 From: Marco Mustapic Date: Thu, 8 Nov 2012 15:37:26 -0300 Subject: [PATCH] + animations --- PixelKit/PixelKit.xcodeproj/project.pbxproj | 2 +- Pixelwave/Classes/Loaders/PXLoader.m | 2 +- .../PXAnimationAtlasParser.h | 13 ++ .../PXAnimationAtlasParser.m | 162 ++++++++++++++++++ .../TopLevel/DataStructures/PXObjectPool.h | 1 + Pixelwave/Classes/TopLevel/PXTopLevel.m | 2 + .../Classes/Utils/TextureAtlas/PXAtlasFrame.m | 2 +- Pixelwave/Pixelwave.xcodeproj/project.pbxproj | 8 + 8 files changed, 189 insertions(+), 3 deletions(-) create mode 100644 Pixelwave/Classes/Support/Parsers/TextureAtlasParser/TextureAtlasParsers/PXAnimationAtlasParser.h create mode 100644 Pixelwave/Classes/Support/Parsers/TextureAtlasParser/TextureAtlasParsers/PXAnimationAtlasParser.m diff --git a/PixelKit/PixelKit.xcodeproj/project.pbxproj b/PixelKit/PixelKit.xcodeproj/project.pbxproj index 7b06b34..30eb60c 100644 --- a/PixelKit/PixelKit.xcodeproj/project.pbxproj +++ b/PixelKit/PixelKit.xcodeproj/project.pbxproj @@ -252,7 +252,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ - 17C52DAC10DC000C00A7001D /* Pixelwave.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; path = Pixelwave.xcodeproj; sourceTree = PIXELWAVE_SRC; }; + 17C52DAC10DC000C00A7001D /* Pixelwave.xcodeproj */ = {isa = PBXFileReference; path = Pixelwave.xcodeproj; sourceTree = PIXELWAVE_SRC; }; 2D0D876912CD41BC00A887AF /* PKBox2DTouchPicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PKBox2DTouchPicker.h; sourceTree = ""; }; 2D0D876A12CD41BC00A887AF /* PKBox2DTouchPicker.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = PKBox2DTouchPicker.mm; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; 2D74AE74143F4D2B00D7B84E /* PixelKitBox2DUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PixelKitBox2DUtils.h; sourceTree = ""; }; diff --git a/Pixelwave/Classes/Loaders/PXLoader.m b/Pixelwave/Classes/Loaders/PXLoader.m index 44037a9..fefd7ba 100644 --- a/Pixelwave/Classes/Loaders/PXLoader.m +++ b/Pixelwave/Classes/Loaders/PXLoader.m @@ -156,7 +156,7 @@ - (BOOL) _load if (!absPath) { - [self _log:[NSString stringWithFormat:@"file not found."]]; +// [self _log:[NSString stringWithFormat:@"file not found."]]; return NO; } diff --git a/Pixelwave/Classes/Support/Parsers/TextureAtlasParser/TextureAtlasParsers/PXAnimationAtlasParser.h b/Pixelwave/Classes/Support/Parsers/TextureAtlasParser/TextureAtlasParsers/PXAnimationAtlasParser.h new file mode 100644 index 0000000..4405862 --- /dev/null +++ b/Pixelwave/Classes/Support/Parsers/TextureAtlasParser/TextureAtlasParsers/PXAnimationAtlasParser.h @@ -0,0 +1,13 @@ +// +// DRAnimationAtlasParser.h +// DinoRun +// +// Created by Marco Mustapic on 11/7/12. +// Copyright (c) 2012 Spiralstorm Games. All rights reserved. +// + +#import "PXSimpleAtlasParser.h" + +@interface PXAnimationAtlasParser : PXSimpleAtlasParser + +@end diff --git a/Pixelwave/Classes/Support/Parsers/TextureAtlasParser/TextureAtlasParsers/PXAnimationAtlasParser.m b/Pixelwave/Classes/Support/Parsers/TextureAtlasParser/TextureAtlasParsers/PXAnimationAtlasParser.m new file mode 100644 index 0000000..9841bdd --- /dev/null +++ b/Pixelwave/Classes/Support/Parsers/TextureAtlasParser/TextureAtlasParsers/PXAnimationAtlasParser.m @@ -0,0 +1,162 @@ +// +// DRAnimationAtlasParser.m +// DinoRun +// +// Created by Marco Mustapic on 11/7/12. +// Copyright (c) 2012 Spiralstorm Games. All rights reserved. +// + +#import "PXAnimationAtlasParser.h" +#import "CJSONDeserializer.h" + +#import "PXTextureAtlas.h" +#import "PXAtlasFrame.h" +#import "PXClipRect.h" +#import "PXTexturePadding.h" + +#import "PXTextureLoader.h" +#import "PXTextureData.h" + +#import "PXDebug.h" + +@implementation PXAnimationAtlasParser + +// Right now this just does a silly check to see if a certain string exists +// within the file to see if it's the right type. Unfortunately the JSON file +// format doesn't have any good unique features we can use. ++ (BOOL) isApplicableForData:(NSData *)data origin:(NSString *)origin +{ + if (!data) + return NO; + + // If this file came straight from memory (not loaded from a URL), we don't + // know what it's called and as such can't figure out the name of the image. + // This is only a restriction with the current JSON file, and can be removed + // when the file format changes. + if (!origin) + return NO; + + NSString *ext = [[origin pathExtension] lowercaseString]; + if (![ext isEqualToString:@"atlas"]) + return NO; + + + return YES; +} + ++ (void) appendSupportedFileExtensions:(PXLinkedList *)extensions +{ + [extensions addObject:@"atlas"]; +} + +- (BOOL) _parseWithModifier:(id)modifier +{ + ///////////////////////// + // Parse the JSON data // + ///////////////////////// + + NSError *error = nil; + NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error]; + + if (error) + { + NSString *localOrigin = self.origin; + localOrigin = [[localOrigin pathComponents] lastObject]; + + PXDebugLog(@"Couldn't parse file:%@ reason:%@\n", localOrigin ? localOrigin : @"JSON", error); + return NO; + } + + NSDictionary *framesDict = [dict objectForKey:@"frames"]; + + if (!framesDict) + return NO; + + int numFrames = [framesDict count]; + + // No frames, no service. + if (numFrames <= 0) + return NO; + + [self _setupWithTotalFrames:numFrames]; + + + //////////////////////////////// + // Read the texture data info // + //////////////////////////////// + + // Release the old one if it exists + + NSArray * bitmaps = [dict objectForKey:@"bitmaps"]; + for (NSString * bitmapName in bitmaps) + { + NSString *imagePath = nil; + + imagePath = [PXTextureLoader resolvePathForImageFile:bitmapName]; + if (!imagePath) + return NO; + + PXTextureLoader *loader = [[PXTextureLoader alloc] initWithContentsOfFile:imagePath modifier:modifier]; + + if (!loader) + return NO; + + // Require the image to be the same contentScaleFactor as the atlas. + [loader setContentScaleFactor:contentScaleFactor]; + [self _addTextureLoader:loader]; + [loader release]; + } + + ///////////////////////// + // Read the frame data // + ///////////////////////// + + // We'll have to divide all the coordinate values stored in the file by + // the content scale factor to convert them from PIXELS to POINTS. + float invScaleFactor = 1.0f / contentScaleFactor; + + // Start parsing + PXGenericAtlasParserFrame *cFrame = NULL; + + CGRect frame; + + // Loop through all the names + NSString *frameName; + NSDictionary *frameDict; + + for (frameName in framesDict) + { + frameDict = [framesDict objectForKey:frameName]; + if (!framesDict) + return NO; + + frame.origin.x = [[frameDict objectForKey:@"x"] floatValue]; + frame.origin.y = [[frameDict objectForKey:@"y"] floatValue]; + frame.size.width = [[frameDict objectForKey:@"width"] floatValue]; + frame.size.height = [[frameDict objectForKey:@"height"] floatValue]; + + // Convert to points + frame.origin.x *= invScaleFactor; + frame.origin.y *= invScaleFactor; + frame.size.width *= invScaleFactor; + frame.size.height *= invScaleFactor; + + // Register the name + cFrame = [self _addFrameWithName:frameName]; + + // Constant + cFrame->textureDataIndex = [[frameDict objectForKey:@"source"] intValue]; + cFrame->anchorEnabled = NO; // These aren't supported in this format + + // Dynamic + cFrame->clipRect = frame; + cFrame->rotation = 0.0f; + cFrame->paddingEnabled = NO; + + ++cFrame; + } + + return YES; +} + +@end diff --git a/Pixelwave/Classes/TopLevel/DataStructures/PXObjectPool.h b/Pixelwave/Classes/TopLevel/DataStructures/PXObjectPool.h index 7c78cc1..619b702 100644 --- a/Pixelwave/Classes/TopLevel/DataStructures/PXObjectPool.h +++ b/Pixelwave/Classes/TopLevel/DataStructures/PXObjectPool.h @@ -1,3 +1,4 @@ + /* * _____ ___ * /\ _ `\ __ /\_ \ diff --git a/Pixelwave/Classes/TopLevel/PXTopLevel.m b/Pixelwave/Classes/TopLevel/PXTopLevel.m index 536a6eb..2019b5b 100644 --- a/Pixelwave/Classes/TopLevel/PXTopLevel.m +++ b/Pixelwave/Classes/TopLevel/PXTopLevel.m @@ -65,6 +65,7 @@ // PXParser - TextureAtlas #import "PXTPAtlasParser.h" #import "PXZwopAtlasParser.h" +#import "PXAnimationAtlasParser.h" // Font Fusers #import "PXFontFuser.h" @@ -106,6 +107,7 @@ void _PXTopLevelInitialize( ) [PXParser registerParser:[PXTPAtlasParser class] forBaseClass:[PXTextureAtlasParser class]]; [PXParser registerParser:[PXZwopAtlasParser class] forBaseClass:[PXTextureAtlasParser class]]; + [PXParser registerParser:[PXAnimationAtlasParser class] forBaseClass:[PXTextureAtlasParser class]]; ////////////////////////////// Add the Fusers \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ diff --git a/Pixelwave/Classes/Utils/TextureAtlas/PXAtlasFrame.m b/Pixelwave/Classes/Utils/TextureAtlas/PXAtlasFrame.m index 5e47130..aecdc39 100644 --- a/Pixelwave/Classes/Utils/TextureAtlas/PXAtlasFrame.m +++ b/Pixelwave/Classes/Utils/TextureAtlas/PXAtlasFrame.m @@ -182,7 +182,7 @@ - (void) setToTexture:(PXTexture *)texture { texture.textureData = textureData; texture.clipRect = clipRect; - + if (anchor) { [texture setAnchorWithX:anchor.x y:anchor.y]; diff --git a/Pixelwave/Pixelwave.xcodeproj/project.pbxproj b/Pixelwave/Pixelwave.xcodeproj/project.pbxproj index 733e0cb..4ec8573 100644 --- a/Pixelwave/Pixelwave.xcodeproj/project.pbxproj +++ b/Pixelwave/Pixelwave.xcodeproj/project.pbxproj @@ -506,6 +506,8 @@ 52DE2A2B12FB26BA00E25924 /* PXTextureLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 52DE2A2912FB26BA00E25924 /* PXTextureLoader.m */; }; 52DE2A2E12FB26CC00E25924 /* PXSoundLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 52DE2A2C12FB26CC00E25924 /* PXSoundLoader.h */; }; 52DE2A2F12FB26CC00E25924 /* PXSoundLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 52DE2A2D12FB26CC00E25924 /* PXSoundLoader.m */; }; + 649D5503164AE3050032998E /* PXAnimationAtlasParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 649D5501164AE3030032998E /* PXAnimationAtlasParser.h */; }; + 649D5504164AE3050032998E /* PXAnimationAtlasParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 649D5502164AE3040032998E /* PXAnimationAtlasParser.m */; }; AACBBE4A0F95108600F1A2B1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACBBE490F95108600F1A2B1 /* Foundation.framework */; }; /* End PBXBuildFile section */ @@ -1009,6 +1011,8 @@ 52DE2A2912FB26BA00E25924 /* PXTextureLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = PXTextureLoader.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; 52DE2A2C12FB26CC00E25924 /* PXSoundLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXSoundLoader.h; sourceTree = ""; }; 52DE2A2D12FB26CC00E25924 /* PXSoundLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = PXSoundLoader.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + 649D5501164AE3030032998E /* PXAnimationAtlasParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXAnimationAtlasParser.h; sourceTree = ""; }; + 649D5502164AE3040032998E /* PXAnimationAtlasParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PXAnimationAtlasParser.m; sourceTree = ""; }; AA747D9E0F9514B9006C5449 /* Pixelwave_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Pixelwave_Prefix.pch; sourceTree = ""; }; AACBBE490F95108600F1A2B1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; D2AAC07E0554694100DB518D /* libPixelwave.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPixelwave.a; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -1234,6 +1238,8 @@ 2D0E8D0D1353F78100A42B3D /* TextureAtlasParsers */ = { isa = PBXGroup; children = ( + 649D5501164AE3030032998E /* PXAnimationAtlasParser.h */, + 649D5502164AE3040032998E /* PXAnimationAtlasParser.m */, 2D543D2D1360F48D004AA569 /* PXSimpleAtlasParser.h */, 2D543D2E1360F48D004AA569 /* PXSimpleAtlasParser.m */, 2D0E8D1C1353F8A900A42B3D /* PXTPAtlasParser.h */, @@ -2430,6 +2436,7 @@ 2DFA00E9143A4B4900307EA5 /* TBXML.h in Headers */, 2DFA00EB143A4B4900307EA5 /* TBXMLNSDataAdditions.h in Headers */, 521FC507143CA6ED00D9D7BF /* PXTextureModifierPremultiplyAlpha.h in Headers */, + 649D5503164AE3050032998E /* PXAnimationAtlasParser.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -2674,6 +2681,7 @@ 2DFA00EA143A4B4900307EA5 /* TBXML.m in Sources */, 2DFA00EC143A4B4900307EA5 /* TBXMLNSDataAdditions.m in Sources */, 521FC508143CA6ED00D9D7BF /* PXTextureModifierPremultiplyAlpha.m in Sources */, + 649D5504164AE3050032998E /* PXAnimationAtlasParser.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };