Author Topic: Android: Adding Admob & Basic Game Controller Support (+fixes)  (Read 898 times)

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1614
    • 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 in the bottom)

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

Bear in mind. Game Controllers is on Android threated as a keyboard, and NOT as a joystick. That means some controllers might have wierd bindings (even SDL own code is not aware of this issue really). Im often  sen that with right stick as well the analoge triggers (currectly not shown in the pics). If you want to support them, im is highly recommend you to add a "remapping" feature to your game. Also in few cases even buttons does not follow a standard, but its not so hash today anymore.

Im have currectly only automapped few joypads im have (OUYA and x-box, which is untested). OUYA used wierd bindings. If you have a gamepads that dont follow the standards, let me know (Space Fractal). Im can auto remap them in one line.

Also currectly only one player is supported. If you are gonna to support Android TV. You can do use that too.

This is the main reason KEY() is now returns a FLOAT and not as INT. This is due analoge controllers and what its might have mapped to. That is include TILT (when enabled)! By defaults the returned values does still default returns 0 and 1.

Also checkout strings.xml for settings.



ANDROID ADMOB with Android Studio

Requires the *New* versions of SDLGlbasic.java and Interstitial.java AStudio files. The new update from SF (Im also might edit this post) works perfectly for instant placement of Interstitial Ads in your Apps. This update is still not pushed to steam.


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 -->

....
  <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
    </application>

</manifest>


Step 2

Open string.xml in the res > values folder
Add the following:

<string name="MobileAds_setAdUnitId">ca-app-pub-3940256099942544/1033173712</string>

Note: When live you can replace testid with your own app advert id provided by Admob console



Step 3
open build.gradle file within the "app" folder in Android Studio
add the bolded item to dependancies, so its look like this (its near the bottom of it and the old one can been replaced):

Code: (glbasic) [Select]
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.gms:play-services-ads:18.3.0'
}

in the soon steam update, then the lines has allready been added for new projects, but admob just commented out.

Step 4

Download Admob.zip bottom of the post (no longer using Dropbox).

** New version of SDLGlbasic.java is being pushed to Steam soon as well **


Open Android Studio, right click on SDLGlbasic.java and then let it Open in Explorer.

Those 3 files should been placed and eventuelt replace the Java files in that folder.

Open SDLGlbasic.java in AStudio

 ** New version of SDLGlbasic.java is being pushed to Steam soon, so you can skip this step over **

Step 5

In GLBASIC when invoking the ads there is no need to re-load ads - the admob listener takes care of that.

Code: (glbasic) [Select]
?IFDEF ANDROID
IMPORT "C" const char* android_JAVACALL(const char* string)
?ENDIF

FUNCTION JavaCall$: callStr$
?IFDEF ANDROID
STATIC res$
res$ = android_JAVACALL(callStr$)
RETURN res$
?ENDIF
ENDFUNCTION

The above function let you call Java function from Glbasic.

// Run after a command press or touchscreen to start a gameloop / next level / or jump back to main menu
// This call the ads its self and also load a new one.
// Also if a ads has been failed, no ads will been shown, but will try again next time you using it.

JavaCall$("ads_show")

// This returns the current status of ads, if something is happens, fails or succes.
Statust$=JavaCall$("ads_status")

Good luck!

Thanks to SF for sticking it out and getting it sorted in the end!

------

SPACE FRACTAL: This works DOES work now, and im ends with to use tilt, keys and touch events to pickup the JavaCalls from a Activity.
« Last Edit: Today at 01:18 AM by spacefractal »
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2648
    • 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)

Online spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3761
    • 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: 1614
    • 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: 1614
    • 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 MrPlow

  • Prof. Inline
  • *****
  • Posts: 1614
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #5 on: 2020-Feb-10 »
Sorry SF,

I was just Google-ing and looking a possible *lightbulb* solution :)
I spent a couple of weeks at it myself without any joy - so I know how you feel.

I will keep trying  - if I find a workaround I will post it here...


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

Online spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3761
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #6 on: 2020-Feb-10 »
the main issue is Activity is gone when trying to call it outside, dispite im passing it. So its look like its a bug found in admob. its just even more annoying as its dont crash correctly or return with any errors at all.
« Last Edit: 2020-Feb-10 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Online spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3761
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #7 on: 2020-Feb-10 »
actuelly got it working, but want to do this thread clean until mrplow have updated the thread. So all comments should been in the other thread about failed attempt.

Tilt Sensor function have its uses here.
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: 1614
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #8 on: 2020-Feb-11 »
Updated the notes above  =D

If I need to re-edit to clarify let me know...
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Online spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3761
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #9 on: 2020-Feb-11 »
im have edited your post a bit with the downloads while we waiting for the steam update (Step 4). Now Admob is FINALLY working fine. Im will been pushing this to steam soon. But im want this thread to been a bit clean, so im do not regreet im splitted this thread. As least this version working and both me and mrplow got success with it (as lest with the test ads IDS we are using here).

Please Note: The steam version does NOT force Admob if you dont want to use it. Its being checking the grandle file, you using it or not, then its use one of the two java file, where admob is implemeted, while the other one is just using empty classes.
« Last Edit: 2020-Feb-11 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Online spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3761
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #10 on: 2020-Feb-12 »
there is slight issue on the gamemapping issue. top left and top right has same value :-D. They cant been that.
« Last Edit: 2020-Feb-12 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: 1614
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #11 on: 2020-Feb-12 »
Oops yes, will change that.
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Online spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3761
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #12 on: 2020-Feb-12 »
Also there was a very slight bug, so the right trigger used wrong constant. That is just fixed. KEYCODE_BUTTON_L2 should have been KEYCODE_BUTTON_R1 in SDLGlbasic.java. Howover this is only happens with Ouya, and joypads that require remapping. Defaults values should works out of the box throught.

Ouya is the only jopad im could auto remap. Im dont own all joypads. If you have such a joypad that dont follow the standard, let me now. All you do is let me know the name of it and all KEY() values its uses. Im should do a simple test app for that.

But howover as long you add a remap feature to your game, all joypads would been useable, regardless which mapping they have. This is IMPORTANT on Android.
« Last Edit: 2020-Feb-12 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Online spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3761
    • View Profile
    • Space Fractal
Re: Android: Adding Admob & Basic Game Controller Support (+fixes)
« Reply #13 on: Yesterday at 09:05 PM »
im updated the joypad image a little bit as there was one wrong a value and the tilt function. Mr Plow, fell free to clear it up with right font.
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.