Game Development Community

Greystripe Intergration Tutorial

by John Sear · in iTorque 2D · 11/03/2009 (8:33 am) · 2 replies

It's a day later than promised but here goes:

Before a month or 2 ago I was totally new to Objective C Programming although I do have quite a bit of C++ experience.

Greystripe was really easy to implement, a lot easier than other Advertising solutions and from the looks of it easier than Scoreloop / Openfeint.

This is my first tutorial ever so sorry if its hard to understand!

Firstly the TGBAppDelegate.mm file. (The header file requires no changes)

Add the following lines to the top of the TGBAppDelegate.mm file.

#import "GreystripeDelegate.h"
#import "GreystripeSDK.h"
#import "platformiPhone/platformiPhone.h" // This is added since I have some functions declared in it.

This allows you to call the Greystripe Ad functions which are used to set it up!

Next in your applicationDidFinishLaunching function in the same file add the 2 GSInit lines so that the function looks like this:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

	GSInit(" YOUR APP ID FROM GREYSTRIPE ");
	GSSetDelegate( self );

	_iPhoneRunTorqueMain( self, window, application );	
}

Remember to use your own App ID which you will find on the Greystripe Website in your account.

Now you can add these functions at the bottom of the same file to handle what happens when an Ad is displayed, these can just be left empty or even not used at all:

- (void) greystripeDisplayWillOpen
{
	// Called when an Ad Opens
}
- (void) greystripeDisplayWillClose
{
	// Called when an Ad Closes
        bigAdStop(); // See later for what this does.
}
- (void) greystripeDidReceiveMemoryWarning
{
	// Called when Greystripe recieves a Low Memory Warning.
}

Now that Greystripe has been initialized all you need to do to display a Greystripe Ad is to call GSDisplayAd();, this is how i've implemented it with a Console command:

This code is inside my iPhoneMain.mm file.

void displayBigAd() 
{  
        // This sets the GS ad's to be displayed in Landscape, set to 0 for portrait.
	GSSetRelativeRotation(90.0f);

        // isBigAdDisplayed is a bool which holds when an advert is displayed, 
        // true is returned from GSDisplayAd() when an advert is show, else it returns false.
	isBigAdDisplayed = GSDisplayAd();
}

ConsoleFunction(displayBigAd, void, 0, 0, "Displays Greystripe Ad")  
{  
    displayBigAd();
}

void bigAdStop()
{
        // Gets called when Advert Closes.
	isBigAdDisplayed = false;
}

And inside my platformiPhone.h file, at the bottom, I have added the Variables and Functions so that they can be called from anywhere which includes this Header.

static bool isBigAdDisplayed = false;
void bigAdStop(void);
bool isBigAd(void);

So that you can check if an advert is displayed from Scripts i've added a console function to get the status of the isBigAdDisplayed variable in the iPhoneMain.mm

bool isBigAd()
{
	return isBigAdDisplayed;
}

ConsoleFunction(bigAdDisplayed, bool, 0, 0, "Check if Big Ad")  
{  
    isBigAd();
}

After doing all of this I realized it still crashed every now and then, to stop this i read the Documentation for Greystripe and it tells you to stop all Update and Rendering while an Ad is displayed, for this I did the following: Compare this function with yours in iPhoneMain.mm

void _iPhoneGameInnerLoop()
{
	if(Game->isRunning()){
		S32 start = Platform::getRealMilliseconds();
		if( isBigAd() == false )
		{
			Game->mainLoop();
		}
		
		S32 time = sgTimeManagerProcessInterval - (start - gLastStart);
		gLastStart = start;
		iPhoneRunEventLoopTimer(time);
	}
	else
	{
		Game->mainShutdown();
	}
}

This is a messy way to implement it i'm sure, but i'm quite new to Objective C so most of it was trial and error. It works fine for me now and the Final version of my app which is Ad funded has been uploaded today!

Any critisism is welcome and if you notice anything wrong with what i've done let me know, i'm always looking to improve.

Thanks!

#1
11/03/2009 (12:15 pm)
Thanks John, this is great!
I was missing the GSSetDelegate().
Everything works now :)
#2
11/03/2009 (3:45 pm)
Great tutorial. Really happy to see other users banding together and getting this sort of stuff out there :)