Game Development Community

Open Feint Tutorial

by Eyal Erez · in Torque 2D for the iPhone · 10/25/2009 (8:04 pm) · 10 replies

So after receiving help from so many people on how to set up Open Feint, I figure I’ll make a beginners guide and place all of the different bits together in one thread. I’d like to thank Kevin Ryan , Ronny Bangsund , Sven Bergström , Craig Fortune and John Sear for their help. they did all the work and for most of it, I just copy and paste :) Hope I didn’t forget anyone.

So the first steps are getting the open feint sdk integrated.
Follow these steps http://help.openfeint.com/faqs/guides-2/integrating-the-openfeint-sdk

Step #7. go to you project dir and open iTGB_Prefix.pch and add the prefix.
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "OpenFeintPrefix.pch"
#endif

now build and you will probably get an error. open iPhoneMemory.mm and change the to that:
#ifndef TORQUE_DISABLE_MEMORY_MANAGER
void* FN_CDECL operator new(dsize_t dt, void* ptr)
{
	return (ptr);
}   
#else
#include "new"
#endif

in platform.h you need to change extern void* FN_CDECL operator new(dsize_t size, void* ptr); to that:
#ifndef TORQUE_DISABLE_MEMORY_MANAGER
extern void* FN_CDECL operator new(dsize_t size, void* ptr);
#else
#include <new>
#endif
now you should be able to build with no errors.
Now here's my TGBAppDelegate.mm without the productId and secret which you should replace with yours. please note that I placed the console functions here where ideally they should be placed in consoleFunctions.mm
#import "TGBAppDelegate.h"
#include "platform/platformInput.h"

extern void _iPhoneRunTorqueMain( id appID,  UIView *Window, UIApplication *app );
extern void _iPhoneGameInnerLoop();
extern void _iPhoneGameResignActive();
extern void _iPhoneGameBecomeActive();
extern void _iPhoneGameWillTerminate();
extern void _iPhoneGameChangeOrientation();

#define OPEN_FEINT

#ifdef OPEN_FEINT  
#import "OpenFeint.h"  
#import "OFHighScoreService.h"  
#import "OFHighScore.h"  
#import "OFAchievementService.h"  
#import "OFAchievement.h"  
#import "OpenFeint+Dashboard.h"  
#import "Achievements.h"
#endif  

@implementation TGBAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
	_iPhoneRunTorqueMain( self, window, application );
	
	// http://www.garagegames.com/community/forums/viewthread/98361/3#comments
	
	
	
    NSString *productKey = @"Your_key";  
    NSString *secret = @"your_secret";  
    NSString *displayName = @"your_game";  
	
    NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:  
                              [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight], OpenFeintSettingDashboardOrientation,  
                              [NSNumber numberWithBool:YES], OpenFeintSettingDisableChat,  
                              nil  
							  ];  
	
    [OpenFeint initializeWithProductKey:productKey  
                              andSecret:secret  
                         andDisplayName:displayName  
                            andSettings:settings  
                           andDelegates:nil];  
	
	 
	 
}


bool OpenFeintIsOn = false;

- (void)applicationWillResignActive:(UIApplication *)application {
	_iPhoneGameResignActive();	
	[OpenFeint applicationWillResignActive];
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
	_iPhoneGameBecomeActive();
	[OpenFeint applicationDidBecomeActive];
}

- (void)applicationWillTerminate:(UIApplication *)application {
	_iPhoneGameWillTerminate();
}


- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation{
	_iPhoneGameChangeOrientation();
}

- (void) runMainLoop {
	_iPhoneGameInnerLoop();
}

- (void)dealloc {
	[window release];
	[super dealloc];
}



- (void) dashboardWillAppear
{  
    
	OpenFeintIsOn = true;
}  

- (void) dashboardWillDisappear
{  
    
	OpenFeintIsOn = false;
}  


 
void launchOpenFeintDashboard(S32 dashboardType)  
{  
    if (dashboardType == 0)  
		[OpenFeint launchDashboard];  
    else if (dashboardType == 1)  
		[OpenFeint launchDashboardWithAchievementsPage];  
    else if (dashboardType == 100)  
    {  
		NSString *leaderboardId = @"37873";  // Beginner  
		[OpenFeint launchDashboardWithHighscorePage:leaderboardId];  
    }  
	else if (dashboardType == 101)  
    {  
		NSString *leaderboardId = @"37883";  // Advanced
		[OpenFeint launchDashboardWithHighscorePage:leaderboardId];  
    }  
	else if (dashboardType == 102)  
    {  
		NSString *leaderboardId = @"56223";  // Expret 
		[OpenFeint launchDashboardWithHighscorePage:leaderboardId];  
    }  
}  


void submitOfScore(S32 score, S32 leaderboardNum)  
{  
    NSString *leaderboardId = @"37873";    

    if (leaderboardNum == 1)  leaderboardId = @"37883" ;   
    else if (leaderboardNum == 2) leaderboardId = @"56223" ; 		
	
    [OFHighScoreService setHighScore:score  
					  forLeaderboard:leaderboardId  
						   onSuccess:OFDelegate()  
						   onFailure:OFDelegate()];  
}  


void awardOfAchievement(S32 awardedNum)  
{  
    if (awardedNum == 1)  
        [OFAchievementService unlockAchievement:LIGHT_SPEED];  
	else if (awardedNum == 2)  
        [OFAchievementService unlockAchievement:STARGAZER]; 
	else if (awardedNum == 3)  
        [OFAchievementService unlockAchievement:COMBO_KING]; 
	else if (awardedNum == 4)  
        [OFAchievementService unlockAchievement:BRONZE_MEDAL]; 
	else if (awardedNum == 5)  
        [OFAchievementService unlockAchievement:SILVER_MEDAL]; 
	else if (awardedNum == 6)  
        [OFAchievementService unlockAchievement:GOLD_MEDAL]; 
	else if (awardedNum == 7)  
        [OFAchievementService unlockAchievement:SPACESHIP_DESTROYER]; 
} 




ConsoleFunction(launchOpenFeintDashboard, void, 2, 2, "launchOpenFeintDashboard(leaderboardNum) "  
                "launches the open feint dashboard")  
{  
    argc;  
	
    launchOpenFeintDashboard( dAtoi(argv[1]) );  
}  



ConsoleFunction(submitOfScore, void, 3, 3, "sumbitScore(score) "  
                "awards the passed achievement")  
{  
    argc;  
	
    submitOfScore( dAtoi(argv[1]),dAtoi(argv[2]) );  
} 


ConsoleFunction(awardOfAchievement, void, 2, 2, "awardOfAchievement(awardNum) "  
                "awards the passed achievement")  
{  
    argc;  
	
    awardOfAchievement( dAtoi(argv[1]) );  
}  


bool isOpenFeint()
{
	return OpenFeintIsOn;
}



@end

I've also add the following line to TGBAppDelegate.h
bool isOpenFeint(void);

The isOpenFeint function is used in iPhoneMain.mm to stop the mainLoop from running when openFeint window is open. Here's the hack :
void _iPhoneGameInnerLoop()
{
	if(Game->isRunning()){
		S32 start = Platform::getRealMilliseconds();
		if( isOpenFeint() == false)
		{
			Game->mainLoop();
		}
		S32 time = sgTimeManagerProcessInterval - (start - gLastStart);
		gLastStart = start;
		iPhoneRunEventLoopTimer(time);
	}
	else
	{
		Game->mainShutdown();
	}
}

For the Achievements you will need to add them on your dashboard and add the achievements.h to you project as well as offline support if you like.

This should be it, unless I forgot anything.
Please help me improve it with your insights.

#1
10/25/2009 (11:37 pm)
Stickied! Loving the influx of tutorials recently. Great work Eyal.
#2
10/26/2009 (12:01 am)
Excellent :)

I guess only challenges are missing, if you want to implement those. Oh, and one typo - Search for "elefate" ;)
#3
10/26/2009 (1:06 am)
Nice one Eyal. Good to see my tutorial has helped spur on some similar ones. Can't comment on how well this work as obviously I use Scoreloop and not Openfeint, but Openfeint appears to work in a very similar fashion. :)

Btw, near the top you wrote: "So the first steps are getting the scoreloop sdk integrated"
#4
10/26/2009 (5:14 am)
oops. I've edited the errors :)
Craig, your tutorial was a great introduction for implementing anything non-torque. I learned a lot from it.
Ronny, I actually do want to use challenges so I'm sure you'll here from me next week. I also want to change to custom welcome openFeint screen so that I don't need to load these two full screen png's that takes almost 600k .
#5
10/26/2009 (7:03 am)
Yeah, I'll be on IRC - just check European time :)
#6
10/26/2009 (7:27 am)
IRC ftw =)

Maybe we should reinstate the official IRC Hour?
#7
10/26/2009 (8:17 am)
Why not :) I think it can lead to some good collections of tips for engine users that we can collect for a megawiki of helpful information.

Also, you should be able to edit those png's Eyal. Nice tutorial
#8
10/26/2009 (2:14 pm)
If there are plenty of iPhone peeps on IRC nowadays I think I'll start using it a lot more, currently I only nip on now and then. Sadly I remember the days when it used to be packed full of people, seems quite quiet nowadays in comparison.
#9
10/26/2009 (4:18 pm)
Like I said to Eyal, IRC is something you have to live and breathe to really benefit ;)
It depends on what time of the day you enter if you'll see many, because they do exactly the same thing - nip in and out again because everybody else is doing it, but at different times!

It sometimes picks up 10 AM Vegas time (the GG community's GMT), sometimes nearer the end of the work day (3 PM+). Fridays are busier than Saturdays, and Sundays are sometimes entirely dead.
#10
11/04/2009 (12:15 am)
Great tutorial! Just got OF plugged in.

Agreed with Ronny. A lot of these tips popping up have been in discussion for weeks in the IRC. :)