Tag: santa race

Universal applications asset naming conventions, directory structure and macros


If you follow me on Twitter you probably noticed that I’m working on 2 Christmas games (if not, please check them here :), and both will be Universal. Since this is the first time I’m doing an Universal App (Game), I had to decide on an asset management strategy: how to manage, name and structure files for iPhone 3GS-, iPod Touch, iPad and iPhone 4?

The most common route is to name iPhone and iPod Touch’s assets as the plain name: texture.png and then for the iPad add a suffix to the file name with  “ipad”: texture-ipad.png. But going through this route add a bunch of if’s to your code to check whether the game is running on the iPad otherwise it is running on the iPhone / iPod Touch:

NSString *fileName;
if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad))
    fileName = @"texture-ipad.png";
    fileName = @"texture.png";

Could you imagine this in a game code? Hundreds (or thousands) of additional work. I came to a simple solution: files always have the same name. The only difference is that iPhone and iPod Touch assets are inside a “graphics” folder, while iPad ones are inside “ipad/graphics”.

Wait – wouldn’t we still need to write those hundreds of if’s? Yes. But before I show you how to solve this issue, let’s talk about Folders References and Groups in Xcode.

Game Assets: Directory Structure

I’m not going to write about this subject, because there are 2 #iDevBlogADay articles (I found them via iDevBlogADay’s delicious account) on this subject and also one from Majic Jungle:

I really recommend that you read them before continuing. I’ll wait.

… ok, are you back? For my directory structure, I’m using David’s recommendations: a GameResources folder inside Resources, and then a Folder Reference (blue folder) to it in Xcode’s project. Also, don’t forget to add the script posted by David that notices Xcode of file changes and that automatically adds/removes/updates them from builds.

Directory Structure

This way you can freely add/remove/update game assets without ever worrying about referencing them on the project. Also you can have multiple files with the same name without problems (as opposite to using folder Groups – the yellow folders).

Xcode Folder References

One line macro for iPad or iPhone detection and for getting a file’s full path according to the device

How to get the path to an iPad or iPhone version of a file without writing if’s everywhere? Through a simple class (and then later a macro):

// Path.h
@interface Path : NSObject {

+ (NSString *)graphicsPath:(NSString*)filePath;
+ (NSString *)resourcePath:(NSString*)filePath;

// Path.m
#import "Path.h"

@implementation Path
+ (NSString *)graphicsPath:(NSString*)filePath
	NSString *finalPath = @"GameResources/";

	if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
		finalPath = [finalPath stringByAppendingString:@"ipad/"];

	finalPath = [finalPath stringByAppendingString:@"graphics/"];
	return [finalPath stringByAppendingString:filePath];

+ (NSString *)resourcePath:(NSString*)filePath
	NSString *finalPath = @"GameResources/";
	return [finalPath stringByAppendingString:filePath];

To load a graphical asset:

// Cocos2D sprite loading
CCSprite *enemy = [CCSprite spriteWithFile:[Path graphicsPath:@"enemies/devil.png"]];
  1. [Path graphicsPath:] first sets the starting path to “GameResources”, then checks if the device is the iPad, if positive, add “ipad” to the path. So far we this path: GameResources/ipad/ or GameResorces/ (in case of iPhone or iPod Touch).
  2. Next it adds the “graphics” folder name  and then your given path, so you could get 2 different responses:
    • iPad: GameResources/ipad/graphics/enemies/devil.png
    • iPhone / iPod Touch: GameResources/graphics/enemies/devil.png

Let’s make it even simpler? Add the following macros to Path.h:

#define graphicsPath(_path) [Path graphicsPath:_path]
#define gameResourcePath(_path) [Path resourcePath:_path]

Add the following line to your ApplicationName_Prefix.pch file:

#import "Path.h"

Now every time you need to load an asset you may use these macros:

// Cocos2D sprite loading for Universal games
CCSprite *enemy = [CCSprite spriteWithFile:graphicsPath(@"enemies/devil.png")];

// Getting path to a sound asset inside GameResources/sounds/enemies
NSString *soundFilePath = gameResourcePath(@"sounds/enemies/devil.caf")];

// Getting path to a plist inside GameResources
NSString *randomFilePath = gameResourcePath(@"state.plist")];

Another useful macro for iPad detection

Even with the directory and naming question solved we may still need to check whether the application is running on the iPad or not, so I would like to share another super simple but useful macro. Add this to your .pch file:

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

Use it this way:

if (IS_IPAD)
    position = 300;
    position = 20;

Christmas Games

Now that I taught you the deep secrets about Universal Applications asset management, I’ll relax and tell a little about the two Christmas games I’m making.

Santa in a Hurry

Santa in a Hurry is about flying your sleigh! But you may be thinking that this is boring! What if I tell you that you are flying AND racing with your Santa holding his sleigh while trying to avoid and slide obstacles and still having to save the Christmas and avoiding different things and the BAD Santas (!?) and dropping presents and seeing nice and warm graphics and listening to Christmas musics and…. and… and…! Well, you will see!

But do you want to know the real hottest thing about Santa in a Hurry?

Once you play and complete “some things”, you will earn a present. And this present may turn itself into iTunes Gift Cards or Game Promo Codes on Dec 25th!

Present CatcherPresent Catcher is all about… catching presents! Oh, again you probably are thinking: that’s so “old”. Not with Present Catcher! Instead of telling you about it I’ll just ask that you start telling your friends about it until I publish the first screenshots and videos. Then you will see what catching presents is really all about!

Here is an exclusive conceptual screenshot of Santa in a Hurry (click to zoom):

Christmas Games - Santa in a Hurry - Night Level

Don’t forget to follow @PlayChristmas and @KarnakGames to be notified about news, and of course, there is an official site: http://karnakgames.com/christmas.

This post is part of iDevBlogADay, a group of indie iOS development blogs featuring two posts per day. You can keep up with iDevBlogADay through the web site, RSS feed, Twitter or delicious.

Santa Race and Present Catcher coming to the AppStore!


Prepare yourself for the BIGGEST CHRISTMAS CHALLENGE ever!

I’m excited to announce that we’re developing two games for the Christmas. They will hopefully be available on the AppStore in 15 days.

Santa Race

Present Catcher

Also, the games will come with some surprises :)
Visit the official site now to see what Santa Race and Present Catcher are all about! http://karnakgames.com/christmas

Follow us @KarnakGames and @PlayChristmas to be notified!