Skip to content

Commit

Permalink
Improved formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
r3econ committed Feb 15, 2014
1 parent 27bd857 commit d90fbb8
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 73 deletions.
Binary file not shown.
Expand Up @@ -8,14 +8,16 @@

#import <UIKit/UIKit.h>

@interface MasterViewController : UIViewController {

@interface MasterViewController : UIViewController
{
IBOutlet UIImageView *imageView;
IBOutlet UILabel *lengthLabel;
IBOutlet UILabel *locationLabel;
IBOutlet UIView *settingsView;
IBOutlet UISwitch *customRangeSwitch;
}

- (IBAction)buttonAction:(id)sender;
- (IBAction)locationValueChanged:(id)sender;
- (IBAction)lengthValueChanged:(id)sender;
Expand Down
68 changes: 45 additions & 23 deletions SpriteAnimationDemo/SpriteAnimationDemo/MasterViewController.m
Expand Up @@ -9,29 +9,40 @@
#import "MasterViewController.h"
#import "UIImage+Sprite.h"


@implementation MasterViewController

- (void)dealloc {

- (void)dealloc
{
[settingsView release];
[locationLabel release];
[lengthLabel release];
[customRangeSwitch release];
[super dealloc];
}

- (void)didReceiveMemoryWarning {

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}


#pragma mark - View lifecycle

- (void)viewDidLoad {

- (void)viewDidLoad
{
[super viewDidLoad];

[self setTitle:NSLocalizedString(@"Sprite Animation", @"")];
[self switchValueChanged:customRangeSwitch];
}

- (void)viewDidUnload {

- (void)viewDidUnload
{
[imageView release];
imageView = nil;
[settingsView release];
Expand All @@ -45,12 +56,15 @@ - (void)viewDidUnload {
[super viewDidUnload];
}

- (IBAction)buttonAction:(id)sender {

if ([imageView isAnimating]) {
- (IBAction)buttonAction:(id)sender
{
if ([imageView isAnimating])
{
[imageView stopAnimating];
[((UIButton *)sender) setTitle:NSLocalizedString(@"Start animation!", @"") forState:UIControlStateNormal];


[((UIButton *)sender) setTitle:NSLocalizedString(@"Start animation!", @"")
forState:UIControlStateNormal];
return;
}

Expand All @@ -60,18 +74,18 @@ - (IBAction)buttonAction:(id)sender {
//
UIImage *spriteSheet = [UIImage imageNamed:@"explosion_4_39_128_debug"];

if ([customRangeSwitch isOn]) {

NSRange range = NSMakeRange([locationLabel.text intValue], [lengthLabel.text intValue]);
if ([customRangeSwitch isOn])
{
NSRange range = NSMakeRange([locationLabel.text intValue],
[lengthLabel.text intValue]);

NSArray *arrayWithSprites = [spriteSheet spritesWithSpriteSheetImage:spriteSheet
inRange:range
spriteSize:CGSizeMake(128, 128)];
[imageView setAnimationImages:arrayWithSprites];

}
else {
else
{
NSArray *arrayWithSprites = [spriteSheet spritesWithSpriteSheetImage:spriteSheet
spriteSize:CGSizeMake(128, 128)];
[imageView setAnimationImages:arrayWithSprites];
Expand All @@ -85,33 +99,41 @@ - (IBAction)buttonAction:(id)sender {
[imageView setAnimationDuration:animationDuration];
[imageView startAnimating];

[((UIButton *)sender) setTitle:NSLocalizedString(@"Stop animation!", @"") forState:UIControlStateNormal];
[((UIButton *)sender) setTitle:NSLocalizedString(@"Stop animation!", @"")
forState:UIControlStateNormal];
}

- (IBAction)locationValueChanged:(id)sender {


- (IBAction)locationValueChanged:(id)sender
{
locationLabel.text = [NSString stringWithFormat:@"%.0f", ((UIStepper *)sender).value];
}

- (IBAction)lengthValueChanged:(id)sender {


- (IBAction)lengthValueChanged:(id)sender
{
lengthLabel.text = [NSString stringWithFormat:@"%.0f", ((UIStepper *)sender).value];
}

- (IBAction)switchValueChanged:(id)sender {


- (IBAction)switchValueChanged:(id)sender
{
UISwitch *sw = (UISwitch *)sender;
[settingsView setUserInteractionEnabled:[sw isOn]];

[UIView beginAnimations:nil context:nil];

if ([sw isOn]) {
if ([sw isOn])
{
[settingsView setAlpha:1.0];
}
else {
else
{
[settingsView setAlpha:0.2];
}

[UIView commitAnimations];
}


@end
Expand Up @@ -40,7 +40,8 @@
@param size the size of the single chunk
@returns the array of UIImages, each representing a single animation frame
*/
-(NSArray *)spritesWithSpriteSheetImage:(UIImage *)image spriteSize:(CGSize)size;
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image spriteSize:(CGSize)size;


/**
The method returns an array with UIImages. Original sprite sheet is sliced into smaller chunks, each of the specified size.
Expand All @@ -49,5 +50,7 @@
@param size the size of the single chunk
@returns the array of UIImages, each representing a single animation frame
*/
-(NSArray *)spritesWithSpriteSheetImage:(UIImage *)image inRange:(NSRange)range spriteSize:(CGSize)size;
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image inRange:(NSRange)range spriteSize:(CGSize)size;


@end
Expand Up @@ -27,18 +27,27 @@

#import "UIImage+Sprite.h"


@implementation UIImage (Sprite)

-(NSArray *)spritesWithSpriteSheetImage:(UIImage *)image spriteSize:(CGSize)size {

return [self spritesWithSpriteSheetImage:self inRange:NSMakeRange(0, lroundf(MAXFLOAT))
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image
spriteSize:(CGSize)size
{

return [self spritesWithSpriteSheetImage:self
inRange:NSMakeRange(0, lroundf(MAXFLOAT))
spriteSize:size];
}

-(NSArray *)spritesWithSpriteSheetImage:(UIImage *)image inRange:(NSRange)range spriteSize:(CGSize)size {

if (!image || CGSizeEqualToSize(size, CGSizeZero) || range.length == 0) return nil;
NSLog(@"%i %i", range.location, range.length);
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image
inRange:(NSRange)range
spriteSize:(CGSize)size
{
if (!image || CGSizeEqualToSize(size, CGSizeZero) || range.length == 0)
return nil;

CGImageRef spriteSheet = [image CGImage];
NSMutableArray *tempArray = [[[NSMutableArray alloc] init] autorelease];

Expand All @@ -54,42 +63,48 @@ -(NSArray *)spritesWithSpriteSheetImage:(UIImage *)image inRange:(NSRange)range
int startPosition = range.location;

// Extracting initial I & J values from range info
//
if (startPosition != 0) {

for (int k=1; k<=maxI; k++) {

if (startPosition != 0)
{
for (int k=1; k<=maxI; k++)
{
int d = k * maxI;

if (d/startPosition == 1) {
if (d/startPosition == 1)
{
startI = maxI - (d % startPosition);
break;
}
else if (d/startPosition > 1) {
else if (d/startPosition > 1)
{
startI = startPosition;
break;
}

startJ++;

}
}

int positionX = startI * size.width;
int positionY = startJ * size.height;
BOOL isReady = NO;

while (positionY < height) {

while (positionX < width) {

CGImageRef sprite = CGImageCreateWithImageInRect(spriteSheet, CGRectMake(positionX, positionY, size.width, size.height));
while (positionY < height)
{
while (positionX < width)
{
CGImageRef sprite = CGImageCreateWithImageInRect(spriteSheet,
CGRectMake(positionX,
positionY,
size.width,
size.height));
[tempArray addObject:[UIImage imageWithCGImage:sprite]];

CGImageRelease(sprite);

length++;
if (length == range.length) {

if (length == range.length)
{
isReady = YES;
break;
}
Expand All @@ -106,4 +121,5 @@ -(NSArray *)spritesWithSpriteSheetImage:(UIImage *)image inRange:(NSRange)range
return [NSArray arrayWithArray:tempArray];
}


@end
17 changes: 10 additions & 7 deletions UIImage+Sprite.h
Expand Up @@ -4,7 +4,7 @@
// Created by Rafal Sroka on 11-11-17.
// Copyright (c) 2011 Rafal Sroka. All rights reserved.
//
// This code is distributed under the terms and conditions of the MIT license.
// This code is distributed under the terms and conditions of the MIT license.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
Expand All @@ -28,26 +28,29 @@
#import <Foundation/Foundation.h>

/**
Useful UIImage category for handling sprite sheets. This add-on smoothes the way of extracting images from a sprite sheet (texture atlas).
Note that for performance reasons you should not fire these methods every time you want to get the array of animation frames.
Useful UIImage category for handling sprite sheets. This add-on smoothes the way of extracting images from a sprite sheet (texture atlas).
Note that for performance reasons you should not fire these methods every time you want to get the array of animation frames.
You should rather fire it once and store the output array somewhere. This is because the Core Graphics image manipulation operations (especially on large images) are not so fast and your application may slow down.
*/
@interface UIImage (Sprite)

/**
The method returns an array with UIImages. Original sprite sheet is sliced into smaller chunks, each of the specified size.
@param image the image with sprite sheet (texture atlas)
@param size the size of the single chunk (in pixels)
@param size the size of the single chunk
@returns the array of UIImages, each representing a single animation frame
*/
+ (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image spriteSize:(CGSize)size;
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image spriteSize:(CGSize)size;


/**
The method returns an array with UIImages. Original sprite sheet is sliced into smaller chunks, each of the specified size.
@param image the image with sprite sheet (texture atlas)
@param range the range of the chunks
@param size the size of the single chunk (in pixels)
@param size the size of the single chunk
@returns the array of UIImages, each representing a single animation frame
*/
+ (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image inRange:(NSRange)range spriteSize:(CGSize)size;
- (NSArray *)spritesWithSpriteSheetImage:(UIImage *)image inRange:(NSRange)range spriteSize:(CGSize)size;


@end

0 comments on commit d90fbb8

Please sign in to comment.