purplelilgirl

makes games & other things

10 notes

I’m always out of energy!

So I started playing social games (The Sims Social, Zynga’s Adventure World and Gardens of Time), and what annoys me the most is the energy bar. So I’m playing and I’m on a roll, hipping snakes to death and all, and suddenly I ran of energy, and then I can’t do anything! Whoever came up with the idea of having that energy bar in social games is brilliant, because that is a good idea of tricking your players into buying energy with actual money. Sigh.

I will probably write short reviews of these games soon.

Filed under social games energy sims social adventure world gardens of time

42 notes

Mini Tutorial: How to capture video of iPhone app in Cocos2D?

Someone asked me before if I knew how to do record the screen in Cocos2d as a video. I didn’t know how to record a video, so this guy sent me some codes, but his problem is that his code is recording the screen (taking screenshots) as a UIWindow. So my idea for him was to replace his screenshot code with AWScreenshot (by Manucorporat, search the Cocos2d forums for his code).

And here are the code bits:

#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAssetWriter.h>
#import <CoreVideo/CVPixelBuffer.h>
#import <CoreMedia/CMTime.h>


#import “AWScreenshot.h”

#define FRAME_WIDTH 320
#define FRAME_HEIGHT 480
#define TIME_SCALE 60 // frames per second

-(void) startScreenRecording
{  
    NSLog(@”start screen recording”);
   
    // create the AVAssetWriter
    NSString *moviePath = [[self pathToDocumentsDirectory] stringByAppendingPathComponent: @”video.mov”];
    if ([[NSFileManager defaultManager] fileExistsAtPath:moviePath])
    {   [[NSFileManager defaultManager] removeItemAtPath:moviePath error:nil];
    }
   
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
    NSError *movieError = nil;
   
    [assetWriter release];
    assetWriter = [[AVAssetWriter alloc] initWithURL:movieURL
                                            fileType: AVFileTypeQuickTimeMovie
                                               error: &movieError];
    NSDictionary *assetWriterInputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                              AVVideoCodecH264, AVVideoCodecKey,
                                              [NSNumber numberWithInt:FRAME_WIDTH], AVVideoWidthKey,
                                              [NSNumber numberWithInt:FRAME_HEIGHT], AVVideoHeightKey,
                                              nil];
    assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType: AVMediaTypeVideo
                                                          outputSettings:assetWriterInputSettings];
    assetWriterInput.expectsMediaDataInRealTime = YES;
    [assetWriter addInput:assetWriterInput];
   
    [assetWriterPixelBufferAdaptor release];
    assetWriterPixelBufferAdaptor =  [[AVAssetWriterInputPixelBufferAdaptor  alloc]
                                     initWithAssetWriterInput:assetWriterInput
                                     sourcePixelBufferAttributes:nil];
    [assetWriter startWriting];
   
    firstFrameWallClockTime = CFAbsoluteTimeGetCurrent();
    [assetWriter startSessionAtSourceTime: CMTimeMake(0, TIME_SCALE)];
   
    // start writing samples to it
    [assetWriterTimer release];
    assetWriterTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                        target:self
                                                      selector:@selector (writeSample:)
                                                      userInfo:nil
                                                       repeats:YES] ;
   
}

-(void) stopScreenRecording
{   [assetWriterTimer invalidate];
    assetWriterTimer = nil;
   
    [assetWriter finishWriting];
    NSLog (@”finished writing”);
}

As you can see startScreenRecording is calls writeSample.

-(void) writeSample: (NSTimer*) _timer
{   if (assetWriterInput.readyForMoreMediaData)
    {
        CVReturn cvErr = kCVReturnSuccess;
       
        // get screenshot image!
        CGImageRef image = (CGImageRef) [[self createARGBImageFromRGBAImage:[self screenshot]] CGImage];
       
        // prepare the pixel buffer
        CVPixelBufferRef pixelBuffer = NULL;
        CFDataRef imageData= CGDataProviderCopyData(CGImageGetDataProvider(image));
        cvErr = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,
                                             FRAME_WIDTH,
                                             FRAME_HEIGHT,
                                             kCVPixelFormatType_32ARGB,
                                             (void*)CFDataGetBytePtr(imageData),
                                             CGImageGetBytesPerRow(image),
                                             NULL,
                                             NULL,
                                             NULL,
                                             &pixelBuffer);
       
        // calculate the time
        CFAbsoluteTime thisFrameWallClockTime = CFAbsoluteTimeGetCurrent();
        CFTimeInterval elapsedTime = thisFrameWallClockTime - firstFrameWallClockTime;
        //NSLog (@”elapsedTime: %f”, elapsedTime);
        CMTime presentationTime =  CMTimeMake (elapsedTime * TIME_SCALE, TIME_SCALE);
       
        // write the sample
        BOOL appended = [assetWriterPixelBufferAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:presentationTime];
   
        if (appended)
        {   NSLog (@”appended sample at time %lf”, CMTimeGetSeconds(presentationTime));
        } else
        {   NSLog (@”failed to append”);
            [self stopScreenRecording];
        }
    }
}

And the code I used to take screenshot:

- (UIImage*)screenshot
{   return [AWScreenshot takeAsImage];
}

Notice how I called [[self createARGBImageFromRGBAImage: [self screenshot]], it’s because my UIImage is a RGBAImage, while the CVPixelBuffer’s format type is kCVPixelFormatType_32ARGB, so I had to fix thing so they match or else, my video would come up in weird tints.

I found the Googled for the createARGBImageFromRGBAImage code, and here it is:

-(UIImage *) createARGBImageFromRGBAImage: (UIImage*)image
{   CGSize dimensions = [image size];
   
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * dimensions.width;
    NSUInteger bitsPerComponent = 8;
   
    unsigned char *rgba = malloc(bytesPerPixel * dimensions.width * dimensions.height);
    unsigned char *argb = malloc(bytesPerPixel * dimensions.width * dimensions.height);
   
    CGColorSpaceRef colorSpace = NULL;
    CGContextRef context = NULL;
   
    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(rgba, dimensions.width, dimensions.height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault); // kCGBitmapByteOrder32Big
    CGContextDrawImage(context, CGRectMake(0, 0, dimensions.width, dimensions.height), [image CGImage]);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
   
    for (int x = 0; x < dimensions.width; x++) {
        for (int y = 0; y < dimensions.height; y++) {
            NSUInteger offset = ((dimensions.width * y) + x) * bytesPerPixel;
            argb[offset + 0] = rgba[offset + 3];
            argb[offset + 1] = rgba[offset + 0];
            argb[offset + 2] = rgba[offset + 1];
            argb[offset + 3] = rgba[offset + 2];
        }
    }
   
    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(argb, dimensions.width, dimensions.height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrderDefault); // kCGBitmapByteOrder32Big
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    image = [UIImage imageWithCGImage: imageRef];
    CGImageRelease(imageRef);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
   
    free(rgba);
    free(argb);
   
    return image;
}

And there we go, I managed to record the screen of my Cocos2d app and then save it as a video file.

My next problem is, how do I add audio to my video?

Filed under capture video iphone app development cocos2d tutorial video iphone app

53 notes

Review: iPhone JavaScript Cookbook

iPhone JavaScript Cookbook

https://www.packtpub.com/sites/default/files/imagecache/productview/1086EXP_iPhone%20JavaScript%20Development%20Cookbook_cov.jpg

This book is written by Arturo Fernandez Montoro.

This book is written for web dev guys, I guess, who wants to write native looking web applications for the iPhone. The book introduces a lot of frameworks to make life easier. the frameworks include native looking assets for user interface building, which you can call with just a line of code. This book also teaches the readers how to work with data using from SQL and AJAX.

Since this is a cookbook, it includes a lot of recipes that readers can copy and paste and revise.

If you want to make applications for the iPhone that look like applications for the iPhone, without having to learn Objective C, the iPhone SDK and all, then this book is a great guide for you. :)  

Link: http://www.packtpub.com/iphone-javascript-cookbook/book/mid/170811hge054

Filed under iphone javascript cookbook packt publishing

8 notes

New iOS and Android Books from Packt

iPhone JavaScript Cookbook

iPhone JavaScript Cookbook

Written by Arturo Fernandez Montoro

Overview of iPhone JavaScript Cookbook

  • Build web applications for iPhone with a native look feel using only JavaScript, CSS, and XHTML
  • Develop applications faster using frameworks
  • Integrate videos, sound, and images into your iPhone applications
  • Work with data using SQL and AJAX
  • Write code to integrate your own applications with famous websites such as Facebook, Twitter, and Flickr
  • These practical recipes include code and screenshots offering a clear step-by-step guide using different frameworks

Link: http://www.packtpub.com/iphone-javascript-cookbook/book/mid/170811hge054

Cocoa and Objective-C Cookbook

Cocoa and Objective-C Cookbook
Written by Jeff Hawkins

Overview of Cocoa and Objective-C Cookbook

  • Develop Cocoa applications using advanced UI concepts
  • Implement the latest Objective-C features and incorporate them into your applications
  • Debug Cocoa applications using advanced tools and techniques
  • Add advanced animation and multimedia to your Cocoa applications

Link: http://www.packtpub.com/cocoa-and-objective-c-cookbook/book/mid/170811c2v5x9

Core Data iOS Essentials

Core Data iOS Essentials
Written by B.M.Harwani

Overview of Core Data iOS Essentials

  • Covers the essential skills you need for working with Core Data in your applications.
  • Particularly focused on developing fast, light weight data-driven iOS applications.
  • Builds a complete example application. Every technique is shown in context.
  • Completely practical with clear, step-by-step instructions.


Link: http://www.packtpub.com/core-data-ios-essentials/book/mid/170811chs5cu

Flash Development for Android Cookbook

Flash Development for Android Cookbook
Written by Joseph Labrecque

Overview of Flash Development for Android Cookbook

  • The quickest way to solve your problems with building Flash applications for Android
  • Contains a variety of recipes to demonstrate mobile Android concepts and provide a solid foundation for your ideas to grow
  • Learn from a practical set of examples how to take advantage of multitouch, geolocation, the accelerometer, and more
  • Optimize and configure your application for worldwide distribution through the Android Market

Link: http://www.packtpub.com/flash-development-for-android-cookbook/book/mid/170811i8nwzh

Android Application Testing Guide

Android Application Testing Guide

Written by Diego Torres Milano

Overview of Android Application Testing Guide

  • The first and only book that focuses on testing Android applications
  • Step-by-step approach clearly explaining the most efficient testing methodologies
  • Real world examples with practical test cases that you can reuse

Link: http://www.packtpub.com/android-application-testing-guide/book/mid/170811fja1lj



Anyone interested in obtaining a free review copy in order to review the book in preferably under 2 weeks time can contact Packt’s online marketing representative Shaveer at shaveeri@packtpub.com with the subject line “book name- review request”. It can be for more than 1 book as well.

Filed under books packt publishing ios android

4 notes

Real Life Angry Birds Toy, gift from my little sis for my birthday.
My sis and I though it&#8217;d be fun if it&#8217;s Pigs that hit the birds.

Real Life Angry Birds Toy, gift from my little sis for my birthday.

My sis and I though it’d be fun if it’s Pigs that hit the birds.

Filed under angry birds toy