# Stack

> The whole model, one scene per file under src/scenes/: a menu that changes to a game, and a game that pushes a pause menu onto itself.

Source: https://omardev29.github.io/rmp-docs/examples/scenes/01_stack.html

The whole model, one scene per file under `src/scenes/`: a menu that changes to a game, and a game that pushes a pause menu onto itself. The pause scene contains **no policy** — freezing the world is what the defaults already do, and its whole `_draw()` is one `rmp::ui::overlay()` — and nothing in the game knows it exists. A HUD over a scene being played: the arrows move the player, not the focus, and Tab walks into the HUD. Plus `rmp::global<T>()` for the score that survives the transition. **Start here.**

## Run it

From a clone of the framework:

```shell
rmp example 01_stack
```

## Keys

Read from the code below: the actions it defines with `rmp::input::action`, the ones that come defined that it reads by name, and the keys it asks raylib about directly.

| Action | Keys |
|---|---|
| `move_left` | `A`, `←`, `gamepad left face left` |
| `move_right` | `D`, `→`, `gamepad left face right` |
| `move_up` | `W`, `↑`, `gamepad left face up` |
| `move_down` | `S`, `↓`, `gamepad left face down` |

## The code

Every file of `examples/scenes/01_stack`, as it is at the commit this site documents ([on GitHub](https://github.com/omardev29/raylib_multiplatform/tree/982bf2a1e307c49776bff84211260703b8de6895/examples/scenes/01_stack)).

**`src/main.cpp` 39 lines**

[src/main.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/scenes/01_stack/src/main.cpp)

```cpp
// ---------------------------------------------------------------------------
// examples/scenes/01_stack/src/main.cpp
//
// The whole scene model: a menu that goes to a game, and a game that puts a
// pause overlay on top of itself. One scene per file under src/scenes/, the
// way Godot does it and the way a real project is laid out -- src/ is globbed
// recursively by all four build systems, so a new scene file costs no CMake,
// no Gradle and no Xcode edit.
//
// Four things are worth watching for, because they are what the stack buys
// you and none of them is written here:
//
//   1. PauseScene contains NO POLICY. The defaults are a pause menu -- what is
//      underneath freezes, stays visible, and stops receiving input -- so
//      pushing it stops the world. It works the same with five enemies as with
//      ten thousand, because what is suspended is the SCENE. Its whole _draw()
//      is one rmp::ui::overlay(): the veil, the clicks it swallows and the
//      focus on Resume come with it.
//   2. Nothing in GameScene knows the pause menu exists. There is no
//      `if (paused) return;` at the top of _update, and no `player.freeze()`.
//   3. `Scene::change<T>()` from inside a _draw() is safe. The transition is
//      deferred to the end of the frame, so the scene that asked for it
//      survives the rest of the frame it asked from.
//   4. The arrows move the player and not the focus. GameScene reads them, so
//      it is a scene being played, and its HUD's Pause button takes the keys
//      only when the player walks into it with Tab.
//
// Run it: `rmp example 01_stack`.
// ---------------------------------------------------------------------------

#include <rmp/app.h>

#include "scenes/main_menu.h"

// Four lines in a real project, and one of them is this. RMP_GAME opens the
// window from [window] in raylib_multiplatform.toml, enters this scene, and
// writes the entry point for whichever of the seventeen targets you are
// building.
RMP_GAME(MainMenuScene);
```

**`include/progress.h` 9 lines**

[include/progress.h](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/scenes/01_stack/include/progress.h)

```cpp
#pragma once
// State that has to survive a scene change lives in exactly one place. A scene
// owns its state and loses it on the way out, which is right for the enemies
// and wrong for the score. Every scene reaches this through
// rmp::global<Progress>(), and there is exactly one for the life of the app.
struct Progress {
    int best_score = 0;
    int runs = 0;
};
```

**`src/scenes/game.cpp` 56 lines**

[src/scenes/game.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/scenes/01_stack/src/scenes/game.cpp)

```cpp
#include "game.h"

#include "pause.h"
#include "progress.h"

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

#include <string>

namespace {
constexpr float PLAYER_SPEED = 220.0f; // units per second
} // namespace

void GameScene::_ready() {
    ++rmp::global<Progress>().runs;

    // The player: a dot, kept inside the window by Edge::CLAMP.
    rmp::Shape dot = rmp::circle(14);
    dot.color = rmp::ui::current_theme().primary;
    _player = spawn({ .position = { 400, 260 }, .shape = dot, .edges = rmp::Edge::CLAMP })
                  .handle();
}

void GameScene::_update(float delta) {
    // No pause check. When PauseScene is on top, this is not called at all.
    _elapsed += delta;
    _score = static_cast<int>(_elapsed * 10.0f);

    // The arrows, WASD, the d-pad and the left stick, in one call. Reading
    // them is what makes this a scene being PLAYED -- see the HUD below.
    const Vector2 move = rmp::input::vector();
    if (_player) _player->velocity = { move.x * PLAYER_SPEED, move.y * PLAYER_SPEED };
}

void GameScene::_draw() {
    // A HUD over a game being played. This scene reads the arrows above, so
    // the framework does not hand its Pause button the focus: the arrows,
    // Enter and Space stay the game's, a click or a tap presses it, and Tab
    // walks into it from the keyboard (Escape walks back out). A menu that is
    // the whole screen -- the main menu, the pause menu -- reads no keys of
    // its own, and its first button has the focus from the start.
    rmp::ui::begin({ .placement = rmp::ui::Align::TOP_LEFT });
    rmp::ui::text("Level " + std::to_string(_level));
    // Safe: rmp::ui interns every string it is given into a frame arena, so a
    // temporary like this one outlives the layout that reads it.
    rmp::ui::text("Score " + std::to_string(_score));
    if (rmp::ui::button("Pause")) rmp::Scene::push<PauseScene>();
    rmp::ui::end();
}

void GameScene::_end() {
    Progress &p = rmp::global<Progress>();
    if (_score > p.best_score) p.best_score = _score;
}
```

**`src/scenes/game.h` 22 lines**

[src/scenes/game.h](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/scenes/01_stack/src/scenes/game.h)

```cpp
#pragma once
#include <rmp/object.h>
#include <rmp/scene.h>

class GameScene : public rmp::Scene {
public:
    // Constructor arguments cross a transition the way arguments normally
    // cross into an object: Scene::change<GameScene>(3) forwards to here. No
    // std::any, no dictionary, no globals.
    explicit GameScene(int level) : _level(level) {}

    void _ready() override;
    void _update(float delta) override;
    void _draw() override;
    void _end() override;

private:
    int _level = 1;
    int _score = 0;
    float _elapsed = 0.0f;
    rmp::Handle<rmp::Object> _player;
};
```

**`src/scenes/main_menu.cpp` 31 lines**

[src/scenes/main_menu.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/scenes/01_stack/src/scenes/main_menu.cpp)

```cpp
#include "main_menu.h"

#include "game.h"
#include "progress.h"

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

#include <string>

void MainMenuScene::_draw() {
    rmp::ui::begin();
    rmp::ui::text("Scene stack example");

    // change<T>() from inside _draw(), which is the case the deferral exists
    // for: this scene is destroyed by that call, and it is the scene the call
    // is being made from.
    if (rmp::ui::button("Play")) rmp::Scene::change<GameScene>(1);

    const Progress &p = rmp::global<Progress>();
    if (p.runs > 0) {
        rmp::ui::text("Best " + std::to_string(p.best_score),
                      { .color = rmp::ui::ColorRole::MUTED });
    }

#if !defined(PLATFORM_IOS)
    // Inert on iPhone: Apple rejects apps that terminate themselves.
    if (rmp::ui::button("Quit")) rmp::app::quit();
#endif
    rmp::ui::end();
}
```

**`src/scenes/main_menu.h` 7 lines**

[src/scenes/main_menu.h](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/scenes/01_stack/src/scenes/main_menu.h)

```cpp
#pragma once
#include <rmp/scene.h>

class MainMenuScene : public rmp::Scene {
public:
    void _draw() override;
};
```

**`src/scenes/pause.cpp` 17 lines**

[src/scenes/pause.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/scenes/01_stack/src/scenes/pause.cpp)

```cpp
#include "pause.h"

#include "main_menu.h"

#include <rmp/ui.h>

void PauseScene::_draw() {
    // overlay() is a frame of its own, in front of everything drawn before it
    // -- the game underneath, its world AND its HUD -- dimmed behind a veil.
    // The veil takes every click, so the HUD's Pause under it presses
    // nothing, and the first button has the focus, so Enter is Resume.
    rmp::ui::overlay([] {
        rmp::ui::text("Paused");
        if (rmp::ui::button("Resume")) rmp::Scene::pop();
        if (rmp::ui::button("Give up")) rmp::Scene::change<MainMenuScene>();
    });
}
```

**`src/scenes/pause.h` 10 lines**

[src/scenes/pause.h](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/scenes/01_stack/src/scenes/pause.h)

```cpp
#pragma once
#include <rmp/scene.h>

// Every default untouched. This is the whole example's point:
// push<PauseScene>() freezes the game, keeps it on screen behind the menu, and
// takes the input -- and not one line in this class says so.
class PauseScene : public rmp::Scene {
public:
    void _draw() override;
};
```
