Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
tutorials:creating_a_sparrow_project_from_scratch_xcode_4 [2014/03/14 13:39]
daniel [How to create a Sparrow project from scratch (Xcode 4)]
tutorials:creating_a_sparrow_project_from_scratch_xcode_4 [2014/07/03 10:32] (current)
daniel [How to create a Sparrow project from scratch (Xcode 4)]
Line 1: Line 1:
 +====== How to create a Sparrow project from scratch (Xcode 4) ======
  
 +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!
 +
 +<note warning>
 +This article is about Sparrow 1.x!\\ ​
 +For Sparrow 2.x, please visit [[Creating a Sparrow Project from Scratch]].
 +</​note>​
 +
 +==== 1. Set up the Sparrow project reference =====
 +
 +  - Create a new "​Window-based"​ iPhone Application
 +  - Open your project in Xcode
 +  - In the "​Project Navigator"​ pane of Xcode, add the Sparrow project (Sparrow.xcodeproj) by selecting the project root and hitting ''​Option+Cmd+A''​ (add to project). Choose the following settings in the file dialog:
 +    * Copy items (...): no
 +    * Folders: Create Groups for any added folders
 +    * Add to Target: (choose your target)
 +  - Click on the Sparrow project in the "​Project Navigator"​ and open up the File Inspector (⌥⌘1).
 +  - In the "​Location"​ drop-down menu, choose: Relative to ''​SPARROW_SRC''​
 +
 +==== 2. Configure the Library Dependencies,​ Linking, and Header Files ====
 +
 +Build Settings:
 +
 +  - Select your project in the "​Project Navigator"​. Then click on the tab "Build Settings"​ and select the "​All"​ and "​Combined"​ buttons directly below.
 +  - Scroll down to the "​Search Paths" section.
 +  - Set "User Header Search Paths" to: ''​${SPARROW_SRC}'',​ recursive: yes
 +  - Still in the Build-settings,​ under "Other Linker Flags" add: ''​-ObjC''​ and ''​-all_load''​ **(this is important!)**
 +
 +Build Phases:
 +
 +  - Change to the tab "Build Phases"​.
 +  - In the group "Link Binary with Libraries",​ click on the "​+"​.
 +  - Add the following frameworks:
 +    * **iOS:**
 +      * AudioToolbox
 +      * AVFoundation
 +      * OpenAL
 +      * OpenGLES
 +      * QuartzCore
 +      * libz.dylib
 +    * **Workspace:​**
 +      * libSparrow.a
 +  - The Frameworks now appear in the root of your project in the "​Project Navigator"​. Drag them into the "​Frameworks"​ folder.
 +
 +==== 3. Create the base Source Classes ====
 +
 +//Create Game.h://
 +
 +<code objc>
 +#import <​Foundation/​Foundation.h>​
 +#import "​Sparrow.h"​
 +
 +@interface Game : SPStage
 +
 +@end
 +</​code>​
 +
 +//Create Game.m://
 +
 +<code objc>
 +#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
 +</​code>​
 +
 +//Modify ProjectAppDelegate.h://​
 +
 +<code objc>
 +#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
 +</​code>​
 +
 +//Modify ProjectAppDelegate.m://​
 +
 +<code objc>
 +#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
 +</​code>​
 +
 +==== 4. Create a Sparrow View ====
 +
 +  - Open "​MainWindow.xib"​ by selecting it
 +  - Drag a "​UIView"​ into the main application window (from the "​Object Library",​ which is part of the "​Utilities"​-View on the right)
 +  - In the "​Identity Inspector",​ change the class from "​UIView"​ to "​SPView"​.
 +  - If you need support for multitouch, activate it in the "​Attributes Inspector"​
 +  - Make a connection to the ''​sparrowView''​-property of the Application Delegate (select the application delegate on the left side of the window, enter the "​Connections Inspector",​ and draw a line from the ''​sparrowView''​-property to the new view).
 +
 +==== 5. Phew! Done! (Hopefully) ====
 +
 +If everything works, you should see a window with a red square. Congratulations!
  tutorials/creating_a_sparrow_project_from_scratch_xcode_4.txt · Last modified: 2014/07/03 10:32 by daniel
 
Powered by DokuWiki