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);
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;
}
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}
}
Add
Code: Select all
/**
* Checks if the interstitial ad is loaded.
**/
virtual bool isInterstitialLoaded() ;
/**
* Shows the interstitial ad.
**/
virtual void showInterstitial() ;
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
}
Add
Code: Select all
bool isInterstitialLoaded();
void showInterstitial();
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);
}
•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.");
}
};
});
}
(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
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!)