Skip to content

Commit

Permalink
Android and IOS Admob Integration
Browse files Browse the repository at this point in the history
Google Play Game Services Android Blinking fixed thanks to
cocos2d/cocos2d-x#5320
AdMob added
JNIHelpers add pragma validation just for android and can include on
xcode project.
  • Loading branch information
cpinan committed Apr 14, 2014
1 parent b1d8135 commit e3bf7cc
Show file tree
Hide file tree
Showing 153 changed files with 7,778 additions and 164 deletions.
538 changes: 533 additions & 5 deletions GooglePlayGameServices.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "GooglePlayGameServices/Classes/NativeUtils.cpp"
timestampString = "419211654.663543"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "117"
endingLineNumber = "117"
landmarkName = "NativeUtils::showLeaderboards()"
landmarkType = "5">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
14 changes: 14 additions & 0 deletions GooglePlayGameServices/Classes/AdMobConstants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// AdMobConstants.h
// GooglePlayGameServices
//
// Created by Carlos Pinan on 14/04/14.
//
//

#ifndef GooglePlayGameServices_AdMobConstants_h
#define GooglePlayGameServices_AdMobConstants_h

#define MY_BANNER_UNIT_ID "ca-app-pub-3855348217239115/3776780188"

#endif
33 changes: 28 additions & 5 deletions GooglePlayGameServices/Classes/JNIHelpers.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
//
// JniHelpers.cpp
#include "JniHelpers.h"
#include "cocos2d.h"
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#include <android/log.h>
#endif


unsigned int JniHelpers::jniCommonIntCall(const char* methodName, const char* classPath, const char* arg0) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo,classPath,methodName, "(Ljava/lang/String;)I");
Expand All @@ -21,11 +25,13 @@ unsigned int JniHelpers::jniCommonIntCall(const char* methodName, const char* cl

return (unsigned int)ret;
}
#endif

return 0;
}

unsigned int JniHelpers::jniCommonIntCall(const char* methodName, const char* classPath, const char* arg0, bool looping) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo,classPath,methodName, "(Ljava/lang/String;Z)I");
Expand All @@ -40,11 +46,13 @@ unsigned int JniHelpers::jniCommonIntCall(const char* methodName, const char* cl

return (unsigned int)ret;
}
#endif

return 0;
}

void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath, const char* arg0) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo, classPath, methodName, "(Ljava/lang/String;)V");
Expand All @@ -57,10 +65,11 @@ void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath

minfo.env->DeleteLocalRef(stringArg0);
}

#endif
}

void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath, const char* arg0, bool looping) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo,classPath,methodName, "(Ljava/lang/String;Z)V");
Expand All @@ -74,10 +83,11 @@ void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath
minfo.env->DeleteLocalRef(stringArg0);

}

#endif
}

void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath, const char* arg0, long score) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo,classPath,methodName, "(Ljava/lang/String;J)V");
Expand All @@ -92,10 +102,11 @@ void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath
minfo.env->DeleteLocalRef(stringArg0);

}

#endif
}

void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath, const char* arg0, int numSteps) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo,classPath,methodName, "(Ljava/lang/String;I)V");
Expand All @@ -109,10 +120,11 @@ void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath
minfo.env->DeleteLocalRef(stringArg0);

}

#endif
}

void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo,classPath,methodName, "()V");
Expand All @@ -121,9 +133,11 @@ void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath
{
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
}
#endif
}

void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath, unsigned int arg0) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo,classPath,methodName, "(I)V");
Expand All @@ -132,9 +146,11 @@ void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath
{
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, arg0);
}
#endif
}

float JniHelpers::jniCommonFloatCall(const char* methodName, const char* classPath) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo,classPath,methodName, "()F");
Expand All @@ -144,11 +160,12 @@ float JniHelpers::jniCommonFloatCall(const char* methodName, const char* classPa
jfloat ret = minfo.env->CallStaticIntMethod(minfo.classID, minfo.methodID);
return (float)ret;
}
#endif
return 0.0;
}

void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath, float arg0){
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo, classPath, methodName, "(F)V");
Expand All @@ -157,9 +174,12 @@ void JniHelpers::jniCommonVoidCall(const char* methodName, const char* classPath
{
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, arg0);
}
#endif

}

bool JniHelpers::jniCommonBoolCall(const char* methodName, const char* classPath) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo,classPath,methodName, "()Z");
Expand All @@ -169,10 +189,12 @@ bool JniHelpers::jniCommonBoolCall(const char* methodName, const char* classPath
jboolean ret = minfo.env->CallStaticBooleanMethod(minfo.classID, minfo.methodID);
return (bool)ret;
}
#endif
return false;
}

bool JniHelpers::jniCommonBoolCall(const char* methodName, const char* classPath, unsigned int arg0) {
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
cocos2d::JniMethodInfo minfo;

bool isHave = cocos2d::JniHelper::getStaticMethodInfo(minfo,classPath,methodName, "(I)Z");
Expand All @@ -182,5 +204,6 @@ bool JniHelpers::jniCommonBoolCall(const char* methodName, const char* classPath
jboolean ret = minfo.env->CallStaticBooleanMethod(minfo.classID, minfo.methodID, arg0);
return (bool)ret;
}
#endif
return false;
}
33 changes: 33 additions & 0 deletions GooglePlayGameServices/Classes/NativeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,36 @@ void NativeUtils::showLeaderboard(const char* leaderboardID)
PlayGameSingleton::sharedInstance().showSingleLeaderboard(leaderboardID);
#endif
}

void NativeUtils::initAd()
{
#if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
PlayGameSingleton::sharedInstance().initAd();
#endif
}

void NativeUtils::showAd()
{
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniHelpers::jniCommonVoidCall(
"showAd",
CLASS_NAME);
#endif

#if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
PlayGameSingleton::sharedInstance().showAd();
#endif
}

void NativeUtils::hideAd()
{
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniHelpers::jniCommonVoidCall(
"hideAd",
CLASS_NAME);
#endif

#if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
PlayGameSingleton::sharedInstance().hideAd();
#endif
}
8 changes: 8 additions & 0 deletions GooglePlayGameServices/Classes/NativeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class NativeUtils
static void showAchievements();
static void showLeaderboards();
static void showLeaderboard(const char* leaderboardID);

/*
* AdMob Integration
*/

static void initAd();
static void showAd();
static void hideAd();

/*
* Ouya connection
Expand Down
20 changes: 20 additions & 0 deletions GooglePlayGameServices/ios/AdMobBannerView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// AdMobBannerView.h
// BTEndlessTunnel
//
// Created by NSS on 4/8/14.
//
//

#import <UIKit/UIKit.h>
#import <GADBannerView.h>

@interface AdMobBannerView : UIViewController
{
GADBannerView* bannerView_;
}

-(void)show;
-(void)hide;

@end
82 changes: 82 additions & 0 deletions GooglePlayGameServices/ios/AdMobBannerView.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// AdMobBannerView.m
// BTEndlessTunnel
//
// Created by NSS on 4/8/14.
//
//

#import "AdMobBannerView.h"
#import "AdMobConstants.h"
#import "cocos2d.h"

@implementation AdMobBannerView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"View Did Load AdMobBanner");
// Do any additional setup after loading the view.

bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView_.adUnitID = [NSString stringWithUTF8String:MY_BANNER_UNIT_ID];
bannerView_.rootViewController = self;
bannerView_.adSize = kGADAdSizeSmartBannerLandscape;
bannerView_.translatesAutoresizingMaskIntoConstraints = YES;

[self.view addSubview:bannerView_];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView_ attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];

//cocos2d::CCSize size = cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getFrameSize();
//bannerView_.frame = CGRectMake(0, 0, size.width, size.height * 0.1f);

[bannerView_ loadRequest:[GADRequest request]];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)dealloc
{
bannerView_.delegate = nil;
[bannerView_ release];
[super dealloc];
}

- (void)hide
{
if(!self.view.hidden)
self.view.hidden = YES;
}

- (void)show
{
if(self.view.hidden)
self.view.hidden = NO;
}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
Loading

0 comments on commit e3bf7cc

Please sign in to comment.