2011年3月28日 星期一

Accelerometer Test

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
    // Convert the coordinates to 'landscape' coords
    // since they are always in 'portrait' coordinates
    CGPoint converted = ccp( (float)-acceleration.y, (float)acceleration.x);

}

CCSpriteBatchNode Updated to CCSpriteSheet

CCSpriteBatchNode *waterDropBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"waterDropSeq.png"];
        [self addChild:waterDropBatchNode z:4 tag:kTagSpriteBatchNode];
        
        waterDropSprite = [CCSprite spriteWithTexture:waterDropBatchNode.texture rect:CGRectMake(0, 0, 120, 120)];
        [waterDropBatchNode addChild:waterDropSprite];
        
        //CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
        
        NSMutableArray *animFrames = [NSMutableArray array];
        
        int frameCount = 0;
        for (int y = 0; y < 3; y++) {
            for (int x = 0; x < 4; x++) {
                CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:waterDropBatchNode.texture rect:CGRectMake(x*120,y*120,120,120)];
                [animFrames addObject:frame];
                
                frameCount++;
                
                if (frameCount == 11)
                    break;
            }
        }
        
        
CCAnimation *animation = [CCAnimation animationWithFrames:animFrames];
[waterDropSprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithDuration:1.0f animation:animation restoreOriginalFrame:NO] ]];

        [waterDropSprite setPosition:ccp(160, 240)];

FMDB Database for iphone

Create db:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"MyDatabase.db"];
FMDatabase
*db = [FMDatabase databaseWithPath:dbPath] ;
if (![db open]) {
NSLog(@“Could not open db.”);
return ;
}
Create table:
[db executeUpdate:@"CREATE TABLE PersonList (Name text, Age integer, Sex integer, Phone text, Address text, Photo blob)"];
Insert data:
[db executeUpdate:@"INSERT INTO PersonList (Name, Age, Sex, Phone, Address, Photo) VALUES (?,?,?,?,?,?)",
@"Jone", [NSNumber numberWithInt:20], [NSNumber numberWithInt:0], @“091234567”, @“Taiwan, R.O.C”, [NSData dataWithContentsOfFile: filepath]];
Update data:
[db executeUpdate:@"UPDATE PersonList SET Age = ? WHERE Name = ?",[NSNumber numberWithInt:30],@“John”];
Get Data:
FMResultSet *rs = [db executeQuery:@"SELECT Name, Age, FROM PersonList"];
while ([rs next]) {
NSString *name = [rs stringForColumn:@"Name"];
int age = [rs intForColumn:@"Age"];
}
[rs close];
Search Address:
NSString *address = [db stringForQuery:@"SELECT Address FROM PersonList WHERE Name = ?",@"John”];
//找年齡
int age = [db intForQuery:@"SELECT Age FROM PersonList WHERE Name = ?",@"John”];

2011年3月14日 星期一

Data Formats for iphone



We’re actually going to start with the audio encoding rather than the file format, because the encoding is actually the most important part.
Here are the data formats supported by the iPhone and a description of each:
  • AAC: AAC stands for “Advanced Audio Coding”, and it was designed to be the successor of MP3. As you would guess, it compresses the original sound, resulting disk savings but lower quality. However, the loss of quality is not always noticeable depending on what you set the bit rate to (more on this later). In practice, AAC usually does better compression than MP3, especially at bit rates below 128kbit/s (again more on this later).
  • HE-AAC: HE-AAC is a superset of AAC, where the HE stands for “high efficiency.” HE-AAC is optimized for low bit rate audio such as streaming audio.
  • AMR: AMR stands for “Adaptive Multi-Rate” and is another encoding optimized for speech, featuring very low bit rates.
  • ALAC: Also known as “Apple Lossless”, this is an encoding that compresses the audio data without losing any quality. In practice, the compression is about 40-60% of the original data. The algorithm was designed so that data could be decompressed at high speeds, which is good for devices such as the iPod or iPhone.
  • iLBC: This is yet another encoding optimized for speech, good for voice over IP and streaming audio.
  • IMA4: This is a compression format that gives you 4:1 compression on 16-bit audio files. This is an important encoding for the iPhone, the reasons of which we will discuss later.
  • linear PCM: This stands for linear pulse code modulation, and describes the technique used to convert analog sound data into a digital format. In simple terms, this just means uncompressed data. Since the data is uncompressed, it is the fastest to play and is the preferred encoding for audio on the iPhone when space is not an issue.
  • μ-law and a-law: As I understand it, these are alternate encodings to convert analog data into digital format, but are more optimized for speech than linear PCM.
  • MP3: And of course the format we all know and love, MP3. MP3 is still a very popular format after all of these years, and is supported by the iPhone.

That looks like a big list, but there are actually just a few that are the preferred encodings to use. To know which to use, you have to first keep this in mind:
  • You can play linear PCM, IMA4, and a few other formats that are uncompressed or simply compressed quite quickly and simultaneously with no issues.
  • For more advanced compression methods such as AAC, MP3, and ALAC, the iPhone does have hardware support to decompress the data quickly – but the problem is it can only handle one file at a time. Therefore, if you play more than one of these encodings at a time, they will be decompressed in software, which is slow.
So to pick your data format, here are a couple rules that generally apply:
  • If space is not an issue, just encode everything with linear PCM. Not only is this the fastest way for your audio to play, but you can play multiple sounds simultaneously without running into any CPU resource issues.
  • If space is an issue, most likely you’ll want to use AAC encoding for your background music and IMA4 encoding for your sound effects.

2010年11月29日 星期一

Cocos2d - CCSprite, CCSpriteSheet and CCAnimation

//Adding an Animated Sprite to Scene


















animationSheet = [CCSpriteSheet spriteSheetWithFile:@"animation_sheet.png"];
sprite = [CCSprite spriteWithTexture:animationSheet.texture rect:CGRectMake(0, 0, 60, 90)];
[animationSheet addChild:sprite];
CCAnimation *animation = [CCAnimation animationWithName:@"nameOfYrAnimation" delay:0.1f];
int frameCount = 0;
for (int y = 0; y < 3; y++) {
    for (int x = 0; x < 5; x++) {
        CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:animationSheet.texture rect:CGRectMake(x*60,y*90,60,90)];
        [animation addFrame:frame];
frameCount++;
if (frameCount == 14)
    break;
}
}
CCAnimate *action = [CCAnimate actionWithAnimation:animation];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:action];
[sprite runAction:repeat];

2010年11月27日 星期六

Parallel Tracking and Mapping + AR on an iPhone 3G



Parallel Tracking and Mapping technology. It's a AR application without using any ARCode pattern. User can find any Plane for the camera image for processing. Also it's an open source project on MAC/PC. 

http://ewokrampage.wordpress.com/

2010年9月13日 星期一

"Nike+ iPhone"







Using the iphone 3GS/4 and the nike+ sensor inside the shoes, the data can be captured and uploaded to the server. The sporty music playlist inside your iPod will be played during your exercise.
Competition started!!!

The sensor will capturing the footstep distance, running miles, location by your iphone build-in GPS with map using Bluetooth... But it's hard to hack the nike+ sensor which is lock by apple and nike...

It's possible to make a game using the running or walking data in daytime, and play with it in night time... Like the virtual Augmented Reality game. Should be fun..!!!