The Barebone Project

Sparrow contains two projects that can be the basis for your games. The “Barebone” project is one of it. It provides just the minimal code you need to create a Sparrow powered application; perfect to get started with Sparrow without any distractions!

When you start the Barebone application, you will see a red rectangle on the screen. This rectangle was rendered with Sparrow — if you can see it, everything was set up correctly.

Now have a look at the source code of the project.

Application Delegate

This is the startup class of any iOS project. Normally, you'd initialize your UIKit objects in this class. Since we're creating a Sparrow game here, we set up Sparrow instead — which turns out to be quite simple.

@interface AppDelegate : NSObject <UIApplicationDelegate> 
@implementation AppDelegate
    SPViewController *_viewController;
    UIWindow *_window;
- (BOOL)application:(UIApplication *)application 
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    // create a full-screen window
    CGRect screenBounds = [UIScreen mainScreen].bounds;
    _window = [[UIWindow alloc] initWithFrame:screenBounds];
    // start up Sparrow
    _viewController = [[SPViewController alloc] init];
    [_viewController startWithRoot:[Game class] supportHighResolutions:YES doubleOnPad:YES];
    // activate the window
    [_window setRootViewController:_viewController];
    [_window makeKeyAndVisible];
    return YES;

As you can see, most of the code is actually responsible for standard UIKit setup. To start up Sparrow, we create an instance of the “SPViewController” class and call one of its “start” methods.

If you want to make some adjustments on Sparrow properties, this is the right place to do so. Just access the respective properties of the view controller — here are a few of the most common ones:

_viewController.showStats = YES;
_viewController.multitouchEnabled = YES;
_viewController.preferredFramesPerSecond = 60;

You may have noticed that we're passing a class called “Game” to the start method. This will be the root object of your game, and it's also part of the project.

Game Class

This is the starting point of your game. Everything you do in Sparrow will be somehow initiated from this class.

@interface Game : SPSprite
@implementation Game
- (id)init
    if ((self = [super init]))
        // this is where the code of your game will start.
        // in this sample, we add just a simple quad
        SPQuad *quad = [SPQuad quadWithWidth:100 height:100];
        quad.color = 0xff0000; // 0xRRGGBB -> this is red
        quad.x = 50;
        quad.y = 50;
        [self addChild:quad];
    return self;

As you can see, the Game class inherits from SPSprite. That's a container object that can take up any display object. To create a game, you populate this container with your content.

In the sample, the content is just a simple SPQuad (a red rectangle). It has a color (red), a size (100×100) and a position (50, 50). And it is a child of the game instance. It has to be, otherwise it would not be rendered. This is a part of the “display tree” architecture of Sparrow, into which we will dive in the following section.

Hit the “Run” button to see the project in action. Congratulations, you have just started your first Sparrow project!

Next section: Display Objects

  manual/the_barebone_project.txt · Last modified: 2013/05/31 08:18 by daniel
Powered by DokuWiki