Page 1 of 1

Admob Interstitial ad - LÖVE-Android

Posted: Mon Nov 09, 2015 2:15 pm
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!)

Re: Admob Interstitial ad - LÖVE-Android

Posted: Mon Nov 09, 2015 5:15 pm
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.

Re: Admob Interstitial ad - LÖVE-Android

Posted: Mon Nov 09, 2015 5:50 pm
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!

Re: Admob Interstitial ad - LÖVE-Android

Posted: Wed Nov 11, 2015 1:53 am
by bobbyjones
Do you think this can be done using FFI? If it can be done just using ffi that would be cool.

Re: Admob Interstitial ad - LÖVE-Android

Posted: Wed Nov 11, 2015 7:18 am
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