BASIC

Author Topic: Adding Admob & Basic Game Controller Support  (Read 168 times)

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1558
    • View Profile
Hi All,
For GLB users that are interested in Android Studio development and want controller and in-app Ads.
Below are KEY() controller mappings and Android Studio project source changes required.


GAME CONTROLLER - GLB change only - no change in Android Studio
============

STANDARD USB GAMEPAD / XBOX Button Mappings

(see pic)

e.g. IF KEY(96)=TRUE THEN A_BUTTON=PRESSED




ANDROID ADMOB with Android Studio - Source changes to SDLGlbasic.java etc.
========================

Step 1

AndroidManifest.xml - add meta info

<application
        android:isGame="true"
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:allowBackup="true"
        android:banner="@mipmap/banner"
        android:hardwareAccelerated="true">
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713" />
<!-- replace testid with own id -->



Step 2
open build.gradle file within the "app" folder in Android Studio
add the bolded item to dependancies....

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.google.android.gms:play-services-ads:18.2.0'

Step 3

Open SDLGlbasic.java in AStudio

At the "imports section" add following

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;


Add following variable in bold into main class:

public class SDLGlbasic extends SDLActivity {

    public static InterstitialAd mInterstitialAd;


Step 4

in OnCreate method after line

AndroidId = "";


Add following >

     // init ads
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });

// ******interstitial ads

       
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); // replace test id with your own
       

        mInterstitialAd.loadAd(new AdRequest.Builder().build());

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                // Load the next interstitial.
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }

        });


Step 5

Find the onResume method:

After the glbasicOnPause command add >


        mInterstitialAd.show();


@Override
    protected void onResume() {
        Log.i("glbasic", "OnResume()");
        glbasicOnPause(0);
        mInterstitialAd.show();
        SDLActivity.setWindowStyle(true);
        mIsPaused = false;
        if (androidAudio != null) { androidAudio.resumeMusic(); }

        super.onResume();
    }


Now you should have ads showing when app is 'returned to' after user leaves or jumps out.

Good luck!






« Last Edit: 2019-Nov-21 by MrPlow »
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2640
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #1 on: 2019-Nov-18 »
Well done Mr Plow, I know what a PITA this stuff can be!  :good:
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3690
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #2 on: 2019-Nov-20 »
please note, the stick order in x-box controllers does NOT follow the google standard, more precision the triggers and the right stick. Im thinks you can add this is line around 557 to perform a auto remapping, im should have do that long time ago:

Code: GLBasic [Select]
        if (SDLGlbasic.LastJoyname.contains("ouya")) { remapping = "0:1:4:2:3:5:9:9:9:9:"; }
        if (SDLGlbasic.JoyMicrosoftProfile.contains("xbox")) { remapping = "0:1:4:2:3:5:9:9:9:9:"; }
        if (SDLGlbasic.JoyMicrosoftProfile.contains("x-box")) { remapping = "0:1:4:2:3:5:9:9:9:9:"; }
 

Etc seach after the ouya one as its used the same order as the x-box one as you can see.

btw give me the java file, and im is happy to include and send it to Gernot!

The above is why you should include a remapping, mostly if you want to use theright stick and a triggers. if you only use left stick/dpad and the face buttons, then there should not been in any conflict at all! They does follow  the Google standard quite well, but not allways throught (Ouya example). But most of time they do.

just to been noticed, you are risk to get your account banned to show ads in app enter and app exit. This is not a good user experience anyway at all. Use a Java call. etc do something like this (this is not a real code, but give you idea as not checked my self, all java calls from glbasic is called to glb_JAVACALL in SDLGlbasic.java):

Code: GLBasic [Select]
        // === SHOW A ADS ===
        if (tokens[0].equals("Interstitial")) {
                        mInterstitialAd.show();
            return result;
        }
 
« Last Edit: 2019-Nov-20 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1558
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #3 on: 2019-Nov-20 »
Thanks SF

The only changes I made to SDLGlbasic was the Admob stuff - but some user might only want to include it optionally (for paid or educational apps)

I spent over a week on the SDLGlbasic trying to get mInterstitialAd.show() to work, but the Admob would not launch within the SDL process for some reason. The Javacall worked - as I logged it - just didn't have any affect, maybe you know why?

The best way I got it to work was to add a show at the OnResume {} - which is very good and acceptable for Admob policies.

I have removed the onPause instance already...just to be safe.

The loadrequest happens automatically and you can control capping via Admob console.


Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1558
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #4 on: 2019-Nov-20 »
Also SF,

That remapping is already in my file

Code: GLBasic [Select]
        // **** STICK REMAP ****
                // Im guess there is two different major remapping. One is the Google Layout, which
                // does not require a remapping, and that one Microsoft using with thier xbox. The
                // right stick and triggers conflict with each other. Ouya seens follow xbox.  
        if (SDLGlbasic.LastJoyname.contains("ouya")) { remapping = "0:1:4:2:3:5:9:9:9:9:"; }
        if (SDLGlbasic.LastJoyname.contains("xbox")) { remapping = "0:1:4:2:3:5:9:9:9:9:"; }
        if (SDLGlbasic.LastJoyname.contains("x-box")) { remapping = "0:1:4:2:3:5:9:9:9:9:";
 
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3690
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #5 on: 2019-Nov-20 »
ah, ok. howover xbox and x-box was not included in the standard and should been added in the next version.

Here is a another version you can try out, which is howover untested for java integration:

https://www.dropbox.com/s/voql9dblyt657dc/exampleadswork.zip?dl=0

Here in strings.xml has been used to set ads appid and the unitid. Im have not did that for the manifest yet as im should have do that as its possible to do.

Also there is 3 glbasic calls you can do "ads_load", "ads_status" and "ads_show". Remember its might take time for the ads to load, so if you call show too early, then nothing is shown as the ads wasent loaded. here is what the ads_status is for.

There was some bits missing to the grandle file you have forgot, other its cant import the required files.
« Last Edit: 2019-Nov-21 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1558
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #6 on: 2019-Nov-21 »
Thanks SF

I will try your file!!

Also updated steps above to include dependancy for build.gradle file.
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1558
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #7 on: 2019-Nov-21 »
Hi SF,

That dropbox link doesn't work.

Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3690
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #8 on: 2019-Nov-21 »
property happens when im did updated the file a little bit. fixed.
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4148
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: Adding Admob & Basic Game Controller Support
« Reply #9 on: 2019-Nov-21 »
Cheers Mr Plow (and of course SpaceFractal) :)
I came. I saw. I played.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1558
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #10 on: 2019-Dec-02 »
Hi SF,

Just wondering if you have been able to get the ads.show() method to run and advert within the SDL game loop process yet?

I would really like to get this working so I can use ads between levels / stages etc.


Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3690
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #11 on: Yesterday at 11:18 PM »
its untested and havent tested it under my apss or something like that, just some java code here. The main issue is how the main activty works and the admon works as its should pause the main activty before shows the ads.

You do should get some logs if you have called it correct.
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.