Sometimes, you want to use Sparrow without having the option to use the app scaffold or the Xcode template. This can happen e.g. when you want to integrate Sparrow into an existing project. This is a topic that pops up regularly in the forum.
That's why I wrote this tutorial. Don't worry, it's not difficult — most of the steps have to be followed in any non-trivial iPhone application. However, there are some things that many of us are not accustomed to, like using Xcode's source tree variables.
This tutorial starts from scratch. When you follow it step by step, the outcome will be a project just like the app scaffold that's part of Sparrow. Good luck!
Option+Cmd+A
(add to project)SPARROW_SRC
UTF-8
${SPARROW_SRC}
, recursive: yes-ObjC
and -all_load
(this is important!)Create Game.h:
#import <Foundation/Foundation.h> #import "Sparrow.h" @interface Game : SPStage @end
Create Game.m:
#import "Game.h" @implementation Game - (id)initWithWidth:(float)width height:(float)height { if (self = [super initWithWidth:width height:height]) { // this is where the code of your game will start. // in this sample, we add just a simple quad to see // if it works. SPQuad *quad = [SPQuad quadWithWidth:100 height:100]; quad.color = 0xff0000; quad.x = 50; quad.y = 50; [self addChild:quad]; } return self; } @end
Modify ProjectAppDelegate.h:
#import <UIKit/UIKit.h> #import "Sparrow.h" @interface ProjectAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; SPView *sparrowView; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet SPView *sparrowView; @end
Modify ProjectAppDelegate.m:
#import "ProjectAppDelegate.h" #import "Game.h" @implementation ProjectAppDelegate @synthesize window; @synthesize sparrowView; - (void)applicationDidFinishLaunching:(UIApplication *)application { SP_CREATE_POOL(pool); [SPStage setSupportHighResolutions:YES]; [SPAudioEngine start]; Game *game = [[Game alloc] init]; sparrowView.stage = game; [game release]; [window makeKeyAndVisible]; [sparrowView start]; SP_RELEASE_POOL(pool); } - (void)applicationWillResignActive:(UIApplication *)application { [sparrowView stop]; } - (void)applicationDidBecomeActive:(UIApplication *)application { [sparrowView start]; } - (void)dealloc { [sparrowView release]; [window release]; [super dealloc]; } @end
sparrowView
-property of the Application Delegate (in the connections-tab of the Application Delegate, drag a line from the sparrowView
-property to the new view).If everything works, you should see a window with a red square. Congratulations!