Admob Interstitial ad - LÖVE-Android

A project to port LÖVE to Android handhelds
Locked
bio1712
Citizen
Posts: 72
Joined: Wed Aug 19, 2015 4:13 pm
Location: Italy

Admob Interstitial ad - LÖVE-Android

Post by bio1712 »

Hello.
Lots of people asked for Ads in LÖVE-Android, but we only were able to display banner ads. (https://bitbucket.org/MartinFelis/love- ... structions)

In these days I worked on interstitial ads and I found a way to show them using LÖVE!.
Probably this is not the best way to do it, but if you want you can try.

We need to add 2 functions: "love.system.isInterstitialLoaded()" and "love.system.showInterstitial()"

Prerequisites:
•Have an Admob account
•Have added google-play-services_lib to your android project

Step 1: Edit JNI files
•Edit jni\love\src\modules\system\wrap_System.h
Add

Code: Select all

int w_isInterstitialLoaded(lua_State *L);
int w_showInterstitial(lua_State *L);
•Edit jni\love\src\modules\system\wrap_System.cpp
Add

Code: Select all

int w_isInterstitialLoaded(lua_State *L)
{
	luax_pushboolean(L, instance()->isInterstitialLoaded());
	return 1;
}

int w_showInterstitial(lua_State *L)
{
	instance()->showInterstitial();
	return 0;
}
And edit "static const luaL_Reg functions[]" like this:

Code: Select all

static const luaL_Reg functions[] =
{
	{ "getOS", w_getOS },
	{ "getProcessorCount", w_getProcessorCount },
	{ "setClipboardText", w_setClipboardText },
	{ "getClipboardText", w_getClipboardText },
	{ "getPowerInfo", w_getPowerInfo },
	{ "openURL", w_openURL },
	{ "vibrate", w_vibrate },
	{ "isInterstitialLoaded", w_isInterstitialLoaded }, //Add this line
	{ "showInterstitial", w_showInterstitial}, // and this one
	{0,0}
}
•Edit jni\love\src\modules\system\System.h
Add

Code: Select all

/**
	 * Checks if the interstitial ad is loaded.
	**/
	virtual bool isInterstitialLoaded() ;
	
	/**
	 * Shows the interstitial ad.
	**/
	virtual void showInterstitial() ;
•Edit jni\love\src\modules\system\System.cpp
Add

Code: Select all

bool System::isInterstitialLoaded() {
#ifdef LOVE_ANDROID
	return love::android::isInterstitialLoaded();
#endif
}

void System::showInterstitial() {
#ifdef LOVE_ANDROID
	love::android::showInterstitial();
#endif
}
•Edit jni\love\src\common\Android.h
Add

Code: Select all

bool isInterstitialLoaded();

void showInterstitial();
•Edit jni\love\src\common\Android.cpp
Add

Code: Select all

bool isInterstitialLoaded()
{
	JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();

	jobject activity = (jobject) SDL_AndroidGetActivity();

	jclass clazz (env->GetObjectClass(activity));
	jmethodID method_id = env->GetMethodID (clazz, "isInterstitialLoaded", "()Z");

	jboolean interstitial_loaded = env->CallBooleanMethod (activity, method_id);

	env->DeleteLocalRef (activity);
	env->DeleteLocalRef (clazz);

	if (interstitial_loaded)
		return true;
	return false;
}

void showInterstitial()
{
	JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();

	jobject activity = (jobject) SDL_AndroidGetActivity();

	jclass clazz (env->GetObjectClass(activity));
	jmethodID showAd_method = env->GetMethodID (clazz, "showInterstitial", "()V");

	env->CallVoidMethod (activity, showAd_method);

	env->DeleteLocalRef (activity);
	env->DeleteLocalRef (clazz);
}
Step 2: add Interstitial ad to your GameActivity.java
•Import AdMob and Interstitial ads into your GameActivity (Read https://developers.google.com/admob/and ... terstitial)

•Edit src\org\love2d\android\GameActivity.java

Code: Select all

boolean adLoaded = false;
[...]
public boolean isInterstitialLoaded() {
		Log.d("GameActivity", "Calling isInterstitialLoaded");
		
		runOnUiThread(new Runnable(){

			@Override
			public void run() { 
	
				if (mInterstitialAd.isLoaded()) {
					adLoaded = true;
					Log.d("GameActivity", "Ad is loaded");
				} else {
					adLoaded = false;
					Log.d("GameActivity", "Ad is not loaded.");
				}
			}
		});
		return adLoaded;
	}
	
	public void showInterstitial() {
		Log.d("GameActivity", "Showing ad!");
		Log.d("GameActivity", "Ad: " + mInterstitialAd);
		runOnUiThread(new Runnable(){

			@Override
			public void run(){ 
				if (mInterstitialAd.isLoaded()){
					mInterstitialAd.show();
					Log.d("GameActivity", "Ad loaded!, showing...");
				} else {
					Log.d("GameActivity", "Ad is NOT loaded!, skipping.");
				}

			};
		});
	}
Now we have done!
(Of course now you have to re-run "ndk-build")

And here is an example:
game.love>main.lua

Code: Select all

function love.load()
	--Admob Interstitial Ad example
	adLoaded = false;
	adTime = 0;
	font = love.graphics.newFont(15);
	sWidth = love.graphics.getWidth();
	sHeight = love.graphics.getHeight();
	text = "Click here to display ad";
end

function love.update(dt)
	adTime = adTime + dt;
	
	if adTime > 1 then
		--Check if ad is loaded
		adLoaded = love.system.isInterstitialLoaded();
		
		--Reset adTime
		adTime = 0;
	end
end

function love.draw()
	--Graphics
	love.graphics.setFont(font);
	love.graphics.setColor(255,0,0);
	love.graphics.rectangle("fill",sWidth/4,sHeight/4,sWidth/2,sHeight/2);
	love.graphics.setColor(255,255,255);
	love.graphics.print(text,(sWidth-text:getWidth())/2,(sHeight-text:getHeight())/2);
	love.graphics.print("Ad loaded: " .. tostring(adLoaded),30,30);
end

function love.mousereleased(x,y,b)
	local x1,x2,y1,y2 = sWidth/4,sWidth/4*3,sHeight/4,sHeight/4*3;
	--Check button
	if x >= x1 and x <= x2 and y >= y1 and y <= y2 and adLoaded then
       --Finally we can show!
		love.system.showInterstitial();
		print("Ad shown");
		--We displayed the ad, so adLoaded = false
		adLoaded = false;
	end
end
Tested on LÖVE-Android 0.9.2a

Warning: this is not 100% stable, you should test your app on several devices and several times.

There might be some hidden bugs there, and if anyone wants to improve it, there are absolutly no problems. (Sorry for English!)
Last edited by bio1712 on Tue Nov 10, 2015 9:27 am, edited 3 times in total.
User avatar
master both
Party member
Posts: 262
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: Admob Interstitial ad - LÖVE-Android

Post by master both »

Thanks for this great contribution!
This was one of the most asked features for the android port, it's a great step in the port's development.
I will try this as soon as possible and see if I can find some bugs. Also you should add this to the wiki.
bio1712
Citizen
Posts: 72
Joined: Wed Aug 19, 2015 4:13 pm
Location: Italy

Re: Admob Interstitial ad - LÖVE-Android

Post by bio1712 »

master both wrote:Thanks for this great contribution!
This was one of the most asked features for the android port, it's a great step in the port's development.
I will try this as soon as possible and see if I can find some bugs. Also you should add this to the wiki.
Thank you!
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Admob Interstitial ad - LÖVE-Android

Post by bobbyjones »

Do you think this can be done using FFI? If it can be done just using ffi that would be cool.
gianmichele
Citizen
Posts: 63
Joined: Tue Jan 14, 2014 11:03 pm

Re: Admob Interstitial ad - LÖVE-Android

Post by gianmichele »

I second that. An example using FFI would be cool.
Also I want to try something similar in iOS, wish me luck as my cpp coding skills are quite basic :P
Locked

Who is online

Users browsing this forum: No registered users and 6 guests