Ads
rmp::ads shows Google AdMob ads of two
kinds: interstitials, the full-screen ads between levels, and rewarded ads, which
the player chooses to watch in exchange for something. They are real on Android once the game
turns them on, and do nothing on every other platform, so the same
game code builds on all 17 targets without a single #ifdef.
Every ad goes the same way#
An ad has to be downloaded before it can be shown, and that takes seconds. So each kind is three calls — ask for one early, check whether it has arrived, show it — and a shown ad is used up:
| Interstitial | Rewarded | |
|---|---|---|
| Start loading one | request_interstitial() | request_rewarded() |
| Has it loaded? | is_interstitial_loaded() | is_rewarded_loaded() |
| Show it | show_interstitial() | show_rewarded() |
| The reward | take_reward_earned() and reward_amount() |
Request as soon as you know you might want one — when the game starts, when a level starts —
and again after each one is shown, because showing it consumes it. Showing when nothing has
loaded does nothing. A request that fails — no connection, no ad to fill it — leaves
is_*_loaded() false and is not retried for you; asking again is the game's
decision.
Interstitials#
An interstitial belongs at a natural break, where the player has stopped anyway: between two levels, after a game over. Request it while the level is being played, and show it on the way out if it made it in time:
class GameScene : public rmp::Scene {
void _ready() override { rmp::ads::request_interstitial(); } // loads while the level is played
};
class GameOverScene : public rmp::Scene {
void _draw() override {
rmp::ui::begin();
if (rmp::ui::button("Play again")) {
if (rmp::ads::is_interstitial_loaded()) rmp::ads::show_interstitial();
rmp::Scene::change<GameScene>();
}
rmp::ui::end();
}
};An ad that has not arrived is simply skipped: the player is never kept waiting for one.
Rewarded ads#
A rewarded ad is a deal with the player — watch this, get that — and the deal is only kept when
the player watched enough of it. So the reward does not come from show_rewarded(). It
arrives later, through take_reward_earned(), which is true once for each reward
earned and clears itself; reward_amount() is then the amount set on the ad unit in
AdMob. A player who closes the ad early gets nothing, and no code has to say so.
class ShopScene : public rmp::Scene {
void _ready() override { rmp::ads::request_rewarded(); }
void _update(float delta) override {
if (rmp::ads::take_reward_earned()) {
_coins += rmp::ads::reward_amount();
rmp::ads::request_rewarded(); // the next one, for next time
}
}
void _draw() override {
rmp::ui::begin();
rmp::ui::text("Coins: " + std::to_string(_coins));
if (rmp::ads::is_rewarded_loaded()) {
if (rmp::ui::button("Watch an ad for coins")) rmp::ads::show_rewarded();
} else {
rmp::ui::button("Ad not ready", { .enabled = false });
}
rmp::ui::end();
}
int _coins = 0;
};Two things in it are worth copying:
- The button is only live when there is an ad. A button that does nothing when pressed reads as a broken game; a disabled one says "not yet" honestly.
- The reward is polled, every frame. The flag waits until it is taken, so it does not matter on which frame the ad network reports it.
Do not put progress behind an ad that may never load. On a bad connection
is_rewarded_loaded() can stay false for a long time, and a player who cannot continue
without an ad is a player who uninstalls.
examples/ads/02_rewarded is this flow, end to end; examples/ads/01_interstitial shows both kinds side by side.
Everywhere but Android#
Off Android — the desktops, the web, iOS — every call compiles and does nothing:
is_*_loaded() is always false, take_reward_earned() never fires and
reward_amount() is 0. The code above runs unchanged on a laptop: the buttons say "not
ready" and the rest of the game is there to work on. Ads on iOS are not implemented.
rmp::ads forwards each call to a C function in <admob.h>, one for
each of the calls above, with the same meaning and the same no-op off Android. A C source file in
the game, which has no namespace to call into, calls them directly — and so does a game written
entirely in plain C, which has
<admob.h> on its include path on every target:
#include <admob.h>
void level_finished(void) {
if (IsInterstitialAdLoaded()) ShowInterstitialAd();
RequestInterstitialAd();
}
void every_frame(int *coins) {
if (TakeRewardEarned()) *coins += GetRewardAmount();
}Configuring AdMob#
Ads are off until the game turns
them on: [android.admob] enabled is false by default, and rmp new
writes false into a new game's .toml. (The framework's own
.toml has it on, so that its CI builds the ads path.) Turning them on is the switch and
three ids, in [android.admob]:
[android.admob]
enabled = true
app_id = "ca-app-pub-3940256099942544~3347511713"
interstitial_id = "ca-app-pub-3940256099942544/1033173712"
rewarded_id = "ca-app-pub-3940256099942544/5224354917"app_id- Your AdMob app's id. It goes into the Android manifest of the release build.
interstitial_id,rewarded_id- The ad unit each kind is loaded from.
Your
ids reach the release build only. The Android debug build — the one rmp android makes,
the one you tap through yourself, and the one Firebase Test Lab's Robo crawler taps at random —
carries Google's test ids whatever the .toml says, the app id in its manifest
included, so it shows test ads even after your own ids are in. A release build carries your ids
and serves real ads wherever it is installed, your own phone included, so do not tap the ads in
it.
Each id is a string, in
quotes, whether ads are on or not. While they are on, each one is also checked when the
configuration is read: it cannot be empty, and it has to have AdMob's shape — ca-app-pub-
and sixteen digits, then ~ and ten digits for the app id, or / and ten
digits for an ad unit. The Google Mobile Ads library stops the app at startup over an app id it
cannot read, so a wrong one is refused before a build starts, at its line, with the reason. An app
id pasted where an ad unit goes is named for what it is:
[android.admob]
enabled = true
interstitial_id = "ca-app-pub-3940256099942544~3347511713" raylib_multiplatform.toml:3: interstitial_id = "ca-app-pub-3940256099942544~3347511713"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
configure: error: [android.admob] interstitial_id = 'ca-app-pub-3940256099942544~3347511713' is not an AdMob ad unit id.
An AdMob ad unit id looks like ca-app-pub-<16 digits>/<10 digits> -- this one has a '~', which makes it the app id.
Copy it from the AdMob console, or set [android.admob] enabled = false.With ads off#
[android.admob]
enabled = falseWith enabled = false, the default, AdMob is not in the Android build at all: no Google Mobile
Ads library, no advertising-id permission, nothing started when the app opens, and nothing to
declare about advertising ids in Google Play's Data safety form. The game's code does not change:
the calls keep compiling and do nothing, exactly as they do on desktop. The same happens on its
own when android is not among the targets the game builds, however it was left out —
there is no Android build to put ads in.
With ads on, the app asks for the internet permission whatever
[android.permissions] internet says, because an ad has to be downloaded.
enabled is a switch, and only true or false is
accepted:
[android.admob]
enabled = "no" raylib_multiplatform.toml:2: enabled = "no"
^^^^^^^^^^^^^^
configure: error: [android.admob] enabled = 'no' must be true or false.