Integrating AdMob into an iTGB game
by BeyondtheTech · in iTorque 2D · 07/09/2009 (10:13 am) · 18 replies
With the likely possibility that most games and apps get pirated immediately or eventually, regardless of anti-cracking solutions implemented, I figured that the next best method of retaining income for our work is through integrated advertising in the game.
With many developers profiting from AdMob ads embedded into their "free" apps, I wonder if it's possible and not too difficult to get it implemented into iTGB somehow.
http://www.admob.com/iphone
With many developers profiting from AdMob ads embedded into their "free" apps, I wonder if it's possible and not too difficult to get it implemented into iTGB somehow.
http://www.admob.com/iphone
#3
Note: make just replace the xxxxxxxxx with your valid publisher id.
in platformiPhone/iPhoneOGLVideo.mm
In the header:
iPhoneOGLVideo.h
09/17/2009 (8:12 pm)
I managed to integrate iTGB 1.2 + Admob. It was relatively simple once you figure out where to put the code.Note: make just replace the xxxxxxxxx with your valid publisher id.
in platformiPhone/iPhoneOGLVideo.mm
- (void)layoutSubviews {
[EAGLContext setCurrentContext:context];
//-Mat was in swapBuffers
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[self destroyFramebuffer];
[self createFramebuffer];
isLayedOut = true;
#ifdef ADMOB
adMobAd = [AdMobView requestAdWithDelegate:self];
[adMobAd retain];
[self.window addSubview:adMobAd];
#endif
}
#ifdef ADMOB
- (NSString *)publisherId {
return @"xxxxxxxxxx"; // this should be prefilled; if not, get it from www.admob.com
}
- (UIColor *)adBackgroundColor {
return [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
- (UIColor *)primaryTextColor {
return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
- (UIColor *)secondaryTextColor {
return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
// Sent when an ad request failed to load an ad
- (void)didFailToReceiveAd:(AdMobView *)adView {
//NSLog(@"AdMob: Did fail to receive ad");
[adMobAd release];
adMobAd = nil;
// we could start a new ad request here, but in the interests of the user's battery life, let's not
}
//- (BOOL)useTestAd {
// return YES;
//}
// Sent when an ad request loaded an ad; this is a good opportunity to attach
// the ad view to the hierachy.
- (void)didReceiveAd:(AdMobView *)adView {
//NSLog(@"AdMob: Did receive ad");
// get the view frame
// put the ad at the top of the screen
adMobAd.frame = CGRectMake(0, 0, 320, 48);
[self.window addSubview:adMobAd];
// if you want to rotate or set alpha..
//[adMobAd setOpaque:NO];
//[adMobAd setAlpha:0.8];
//[adMobAd setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
[refreshTimer invalidate];
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:AD_REFRESH_PERIOD target:self selector:@selector(refreshAd:) userInfo:nil repeats:YES];
}
// Request a new ad. If a new ad is successfully loaded, it will be animated into location.
- (void)refreshAd:(NSTimer *)timer {
[adMobAd requestFreshAd];
}
- (BOOL)mayAskForLocation {
return YES; // this should be prefilled; if not, see AdMobProtocolDelegate.h for instructions
}
#endif
...
- (void)dealloc {
if ([EAGLContext currentContext] == context) {
[EAGLContext setCurrentContext:nil];
}
#ifdef ADMOB
[adMobAd release];
[refreshTimer invalidate];
#endif
[context release];
[super dealloc];
}In the header:
iPhoneOGLVideo.h
#ifdef ADMOB
#import "AdMobView.h"
#import "AdMobDelegateProtocol.h"
#define AD_REFRESH_PERIOD 30.0
#endif
....
struct EAGLContext;
/*
This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass.
The view content is basically an EAGL surface you render your OpenGL scene into.
Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel.
*/
@interface iPhoneOGLVideo : UIView <UIAccelerometerDelegate> {
@private
/* The pixel dimensions of the backbuffer */
GLint backingWidth;
GLint backingHeight;
EAGLContext *context;
#ifdef ADMOB
AdMobView *adMobAd;
NSTimer *refreshTimer;
#endif
/* OpenGL names for the renderbuffer and framebuffers used to render to this view */
GLuint viewRenderbuffer, viewFramebuffer;
/* OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist) */
GLuint depthRenderbuffer;
bool isLayedOut;
CGFloat currentAngle;//for knowing our current oriantion
}
- (void)swapBuffers;
- (void)rotateByAngle:(CGFloat)angle;//rotate BY a certain degree
- (void)rotateToAngle:(CGFloat)angle;//rotate TO a certain degree
- (void)centerOnPoint:(CGPoint)point;//set the center position
@end
#4
10/11/2009 (1:02 pm)
thanks lin chear for the code, Do you know a way to hide the ad trough a torquescript??
#5
11/23/2009 (2:01 am)
The adMobAd object is a subclass of UIView, so you can simply do adMobAd.hidden = true; to hide it. You could wrap this in a ConsoleMethod to turn it on/off.
#6
I am unable to obtain what object called layoutsubviews so I can then call my hide and show functions to show and hide the ad.
solved problem. Made "-" type functions to hide and show adMobAd with *adMobAd and *refreshtimer as @public
Used
01/10/2010 (6:11 pm)
How do you access the AdMob object in source code? I am unable to obtain what object called layoutsubviews so I can then call my hide and show functions to show and hide the ad.
solved problem. Made "-" type functions to hide and show adMobAd with *adMobAd and *refreshtimer as @public
Used
void Platform::minimizeWindow(){
[platState.ctx hideAd]
}and restoreWindow() to do the opposite
#7
So Change this code:
To:
02/16/2010 (5:49 pm)
Also Apple has declared that you cannot use core location to deliver ads in the application. Without providing some feature or benefit for doing so.So Change this code:
- (BOOL)mayAskForLocation {
return YES; // this should be prefilled; if not, see AdMobProtocolDelegate.h for instructions
} To:
- (BOOL)mayAskForLocation {
return NO; // this should be prefilled; if not, see AdMobProtocolDelegate.h for instructions
}
#8
Due to that I personally would recommend to not use it anymore or just as "another one" and instead use something like MobClix as primary which has average fillrates in the 90 - 95% range.
02/17/2010 (2:20 am)
One thing on AdMob: Their handling of ad filling especially with enabled cross advertisement has gotten significantly worse in the past 4 - 6 months. We talk about fill rates in the range of 60-70% especially with enabled cross advertising (which according their own speaking is meant to fill the empty spots not create even more of them).Due to that I personally would recommend to not use it anymore or just as "another one" and instead use something like MobClix as primary which has average fillrates in the 90 - 95% range.
#9
Uses other ad services (AdMob, Mobclix, Quattro, TapJoy, Greystripe), has its own ads (from what I can tell) and in-app purchases included with the service. Should give a pretty good fill rate, in theory.
02/17/2010 (12:23 pm)
Here's yet another alternative: www.appads.com/Uses other ad services (AdMob, Mobclix, Quattro, TapJoy, Greystripe), has its own ads (from what I can tell) and in-app purchases included with the service. Should give a pretty good fill rate, in theory.
#10
03/30/2010 (7:27 pm)
@Lin Chear - Have you gotten the latest admob (3-22-10 build) to work? I"m getting two build errors. If I replace libadmob.a with the nothumb version it builds but crashes when you click on some ads.
#11
On one video on youtube the guy copies some header files into the classes folder, but i dont have that in my iTGB project, I opened up the iTGB project in the finder and there was a classes folder so i copied it there... followed the code changes as mentioned above, but still no joy.
The other strange thing is in the docs it talks about connecting up the view to the AdViewController type, but that is not listed in the classes list. I do have an AdMobView though.
04/21/2010 (3:00 pm)
(admob) Has anyone got any simple instructions on how to do this, followed the instructions, but i see a flash of the view window on simulator boot up, but do not see any ads.On one video on youtube the guy copies some header files into the classes folder, but i dont have that in my iTGB project, I opened up the iTGB project in the finder and there was a classes folder so i copied it there... followed the code changes as mentioned above, but still no joy.
The other strange thing is in the docs it talks about connecting up the view to the AdViewController type, but that is not listed in the classes list. I do have an AdMobView though.
#12
But from the last block it sounds like the video is for an old admob view version not the new one
04/21/2010 (4:12 pm)
you don't see adds on the simulator unless you have added the simulator a file on the simulator build target while you added the right one on the device.But from the last block it sounds like the video is for an old admob view version not the new one
#13
Not sure what you mean by added the simulator file on the simulator build.? Could you expand on that for me?
04/22/2010 (5:15 am)
Yeah i kinda thought that the video might be out of date....it looks like the Admobview is the new class name.Not sure what you mean by added the simulator file on the simulator build.? Could you expand on that for me?
#14
Anyone have any further advice.... why these things are not easier i will never know :(
If anyone has a test (i.e blank) iTGB project with admob in that would be of great use :D
04/22/2010 (10:39 am)
Have now tried it on the device and nothing appears.... :(Anyone have any further advice.... why these things are not easier i will never know :(
If anyone has a test (i.e blank) iTGB project with admob in that would be of great use :D
#15
AdMob has 2 .a files per target iPhone OS base version
One for the device and one for the simulator. to have it working in the simulator you must use the simulator one. The device one is arm compiled and will not work on the simulator
04/22/2010 (5:49 pm)
As for adding the simulator file:AdMob has 2 .a files per target iPhone OS base version
One for the device and one for the simulator. to have it working in the simulator you must use the simulator one. The device one is arm compiled and will not work on the simulator
#16
04/23/2010 (5:21 am)
Hmm ok thats interesting, i was pretty sure i added all relevant files to the simulator version, and so that in turn would have worked automatically... will have to give it a final go tonight (if i cannot get it to work, and no one has a blank iTGB app i can look at, i might as well move on).
#17
is no one using Admob? Does no one have a blank iTGB app they could send me?
04/23/2010 (4:17 pm)
Nope that didnt work Marc....is no one using Admob? Does no one have a blank iTGB app they could send me?
#18
05/12/2010 (2:15 am)
The workaround described above only works (perfectly) with an older AdMob SDK.
Torque 3D Owner Marc Dreamora Schaerer
Gayasoft
They have a sample application with the sdk that you can check out and use as base and then init it for example in the appdelegate
An additional thing you might want to consider to raise popularity and potentially earn money is Scoreloop (http://www.scoreloop.com/) if you already haven't