On this page
  1. Run it
  2. The code

Overlay

overlay(): a pause menu pushed as a scene of its own, blurred, that freezes the game below; and "Really quit?" drawn by the game scene itself -- an overlay with a panel() in it, which is all a dialog is. The veil takes the clicks, the first button the focus, and the balls go on behind the question. One scene per file under src/scenes/.

Overlay, as it starts

Run it

From a clone of the framework:

rmp example 08_overlay

The code

Every file of examples/ui/08_overlay, as it is at the commit this site documents (on GitHub).

src/main.cpp 32 lines
// ---------------------------------------------------------------------------
// examples/ui/08_overlay/src/main.cpp
//
// rmp::ui::overlay(): a pause menu, a "Really quit?", a game-over card --
// whatever has to be in front of the game, with the game dimmed or blurred
// behind it so that the eye goes where the player has to act.
//
// Two of them, and the difference between them is the point:
//
//   the pause    a scene of its own, PUSHED over the game. Its whole _draw()
//                is one overlay(), blurred; the balls below stop because a
//                pushed scene freezes what is under it by default.
//                src/scenes/pause.cpp.
//   the dialog   "Really quit?", drawn by the game scene itself after its HUD:
//                an overlay() with a panel() in it, which is all a dialog is
//                -- there is no dialog() to learn. Nothing is pushed, so
//                nothing stops: the balls go on behind the question. The
//                overlay takes the clicks, the focus and Enter; the rest is
//                the scene's to decide. src/scenes/game.cpp.
//
// The blur is a real one on every GPU raylib drives, OpenGL ES 2.0 and WebGL
// included. raylib's software renderer has no shaders, so the screenshots CI
// takes of this example show the veil alone.
//
// Run it: `rmp example 08_overlay`.
// ---------------------------------------------------------------------------

#include <rmp/app.h>

#include "scenes/game.h"

RMP_GAME(GameScene);
src/scenes/game.cpp 74 lines
#include "game.h"

#include "pause.h"

#include <rmp/app.h>
#include <rmp/object.h>
#include <rmp/ui.h>

#include <array>

namespace {

// Where each ball starts, how fast it goes, and how it looks.
struct Ball {
    Vector2 position;
    Vector2 velocity;
    float radius;
    Color color;
};

constexpr std::array BALLS{
    Ball{ { 120, 110 }, { 120, 80 }, 34, RED },
    Ball{ { 300, 330 }, { -150, 95 }, 26, ORANGE },
    Ball{ { 420, 160 }, { 100, -120 }, 40, GOLD },
    Ball{ { 560, 300 }, { -90, -70 }, 30, LIME },
    Ball{ { 680, 120 }, { 140, 110 }, 24, SKYBLUE },
    Ball{ { 220, 220 }, { -110, -130 }, 36, VIOLET },
};

} // namespace

void GameScene::_ready() {
    // Something to be in front of: balls bouncing off the edges of the
    // window. A veil over an empty screen shows nothing, and a blur less.
    for (const Ball &b : BALLS) {
        rmp::Shape shape = rmp::circle(b.radius);
        shape.color = b.color;
        spawn({ .position = b.position,
                .shape = shape,
                .velocity = b.velocity,
                .edges = rmp::Edge::BOUNCE });
    }
}

void GameScene::_draw() {
    // The HUD. This scene reads no keys, so it is no game being PLAYED, and
    // its first button has the focus, as a menu's would: Enter pauses.
    rmp::ui::begin({ .placement = rmp::ui::Align::TOP_RIGHT });
    if (rmp::ui::button("Pause")) rmp::Scene::push<PauseScene>();
#if !defined(PLATFORM_IOS)
    // Inert on iPhone: Apple rejects apps that terminate themselves.
    if (rmp::ui::button("Quit")) _asking = true;
#endif
    rmp::ui::end();

    // The dialog. After end(), never inside it: an overlay is a frame of its
    // own. The panel is the box; the overlay is the veil behind it, the
    // clicks it takes from everything under it, and the focus on its first
    // button -- Stay, so that Enter is the answer that loses nothing.
    if (_asking) {
        rmp::ui::overlay({ .dim = 0.6f }, [&] {
            rmp::ui::panel([&] {
                rmp::ui::text("Really quit?");
                rmp::ui::text("The balls will stop for good.",
                              { .color = rmp::ui::ColorRole::MUTED });
                rmp::ui::row([&] {
                    if (rmp::ui::button("Stay")) _asking = false;
                    if (rmp::ui::button("Quit", { .style = rmp::ui::Variant::DANGER }))
                        rmp::app::quit();
                });
            });
        });
    }
}
src/scenes/game.h 11 lines
#pragma once
#include <rmp/scene.h>

class GameScene : public rmp::Scene {
public:
    void _ready() override;
    void _draw() override;

private:
    bool _asking = false; // "Really quit?" is on screen
};
src/scenes/pause.cpp 13 lines
#include "pause.h"

#include <rmp/ui.h>

void PauseScene::_draw() {
    // blur: how far, in design units -- CSS's blur(), the Gaussian's standard
    // deviation. dim: how far towards the veil's colour, black unless you say
    // otherwise. Six and 0.4 say "not now" and still show where you were.
    rmp::ui::overlay({ .blur = 6, .dim = 0.4f }, [] {
        rmp::ui::text("Paused", { .size = rmp::ui::Size::LARGE });
        if (rmp::ui::button("Resume")) rmp::Scene::pop();
    });
}
src/scenes/pause.h 9 lines
#pragma once
#include <rmp/scene.h>

// Every default untouched: pushed, it freezes the game, keeps it on screen
// behind the overlay, and takes the input.
class PauseScene : public rmp::Scene {
public:
    void _draw() override;
};