Skip to content

Two players in your game

The starter game has one greeting, which the arrow keys, WASD and every pad move together. This page gives it a second greeting for a second player: player 1 moves the first with WASD or the first pad, and player 2 the second with the arrow keys or the second pad, on one keyboard or two pads. Then, optionally, it splits the window in two, each half following one player, as the engine’s local-multiplayer sample does. It is all in your game’s code; the engine reads the keys and pads and draws the views. It takes about 20 minutes, and the split screen 15 more.

Before you start, do Make a first change, so that you know the edit, build and run loop and have a copy of your game to go back to. Make a fresh copy now with that page’s step 1 line.

Every command runs in the x64 Native Tools Command Prompt, in your project’s folder:

Terminal window
cd /d C:\dev\mygame
  1. Give the state a second greeting. The state’s header lists what it keeps. Open it:

    Terminal window
    notepad game\states\hello_state.h

    Search with Ctrl+F for hint_ = nullptr; and add a line after it, so the two read:

    labrador::Label* hint_ = nullptr;
    labrador::Label* second_greeting_ = nullptr;

    Then search for Motion motion_;, near the end, and add a line after it:

    Motion motion_;
    Motion second_motion_;

    A Motion is where a greeting is: the point it circles and how far round it is. Player 2’s greeting needs its own. Save and close Notepad.

  2. Split the keyboard in two. Open the state’s code:

    Terminal window
    notepad game\states\hello_state.cpp

    Search for Vector2F keyboard_direction(. This function turns the keys into a direction, and reads both the arrows and WASD:

    samples/minimal/states/hello_state.cpp
    Vector2F keyboard_direction(const Keyboard& keyboard)
    {
    Vector2F direction = Vector2F::zero;
    if (keyboard.held(Key::left) || keyboard.held(Key::a))
    {
    direction.x -= 1.0f;
    }
    if (keyboard.held(Key::right) || keyboard.held(Key::d))
    {
    direction.x += 1.0f;
    }
    if (keyboard.held(Key::up) || keyboard.held(Key::w))
    {
    direction.y -= 1.0f;
    }
    if (keyboard.held(Key::down) || keyboard.held(Key::s))
    {
    direction.y += 1.0f;
    }
    return direction;

    Select from Vector2F keyboard_direction( down to the } after return direction;, and paste these two functions in its place:

    Vector2F wasd_direction(const Keyboard& keyboard)
    {
    Vector2F direction = Vector2F::zero;
    if (keyboard.held(Key::a))
    {
    direction.x -= 1.0f;
    }
    if (keyboard.held(Key::d))
    {
    direction.x += 1.0f;
    }
    if (keyboard.held(Key::w))
    {
    direction.y -= 1.0f;
    }
    if (keyboard.held(Key::s))
    {
    direction.y += 1.0f;
    }
    return direction;
    }
    // The same for player 2, on the arrow keys.
    Vector2F arrows_direction(const Keyboard& keyboard)
    {
    Vector2F direction = Vector2F::zero;
    if (keyboard.held(Key::left))
    {
    direction.x -= 1.0f;
    }
    if (keyboard.held(Key::right))
    {
    direction.x += 1.0f;
    }
    if (keyboard.held(Key::up))
    {
    direction.y -= 1.0f;
    }
    if (keyboard.held(Key::down))
    {
    direction.y += 1.0f;
    }
    return direction;
    }

    Make sure the old function is gone: one left behind and no longer used builds on Windows and stops the browser build.

  3. Place and add the second greeting. In init(), search for this->motion_.centre =. Its two lines put player 1’s greeting 120 pixels left of the middle:

    samples/minimal/states/hello_state.cpp
    this->motion_.centre = Vector2F(resolution.x * 0.5f - 120.0f,
    resolution.y * 0.5f);

    Click at the end of the second, after ;, press Enter, and paste:

    this->second_motion_.centre = Vector2F(resolution.x * 0.5f - 120.0f,
    resolution.y * 0.5f + 100.0f);

    That starts player 2 100 pixels below player 1. Then search for labrador::Colour::dark_gray));, the end of the lines that add the hint; if you have done A camera that follows the player, it ends labrador::Colour::dark_gray);, with one ), so search for that. Click at the end of that line, press Enter twice, and paste:

    this->second_greeting_ = this->scene_->add(std::make_unique<Label>(
    L"Hello from player 2.", font_name, this->second_motion_.centre,

    While you are there, change the hint’s words, two lines above, so that the line reads:

    L"Player 1: WASD or pad 1. Player 2: arrows or pad 2. B or Escape asks to quit.", font_name,
  4. Give each player a pad. In update(), search for A single-player game accepts. These lines, a comment and then the code it describes, read every pad:

    In v0.3.0 the code under that comment uses an inline loop instead. The replacement has the same endpoints in both versions: begin at the comment and stop immediately before if (quit_requested), leaving that if and its body in place.

    samples/minimal/states/hello_state.cpp
    // A single-player game accepts every slot: the system need not put its
    // only pad in slot 0. This loop only reads the four slots; what they mean
    // together is the rules' combine_pads, which tests/motion_tests.cpp checks
    // with a pad alone in slot 1 or 3, with slot 0 emptied, and with no pad.
    bool quit_requested = keyboard.pressed(Key::escape);
    std::array<PadReading, Gamepads::max_count> readings;
    for (int slot = 0; slot < Gamepads::max_count; ++slot)
    {
    readings[slot].quit_pressed = pads.pressed(slot, GamepadButton::b);
    readings[slot].stick = apply_deadzone(pads.state(slot).left_stick,
    stick_deadzone);
    }
    const PadsInput pad_input = combine_pads(readings);
    quit_requested |= pad_input.quit_requested;
    const Vector2F stick_direction = pad_input.stick;

    The starter adds every pad’s stick together, so that one player’s pad works in whichever slot Windows put it. With two players, each needs a pad of their own, and the comment is no longer true. Replace from the comment’s first line through all the pad-reading code before if (quit_requested) with:

    bool quit_requested = keyboard.pressed(Key::escape);
    for (int slot = 0; slot < Gamepads::max_count; ++slot)
    {
    quit_requested |= pads.pressed(slot, GamepadButton::b);
    }
    // One pad each: player 1's is the pad in slot 0, player 2's the pad in
    // slot 1. An empty slot reads as a stick at rest.
    const Vector2F first_stick =
    apply_deadzone(pads.state(0).left_stick, stick_deadzone);
    const Vector2F second_stick =
    apply_deadzone(pads.state(1).left_stick, stick_deadzone);

    B on any pad still asks to quit. Windows numbers pads from slot 0 in the order they were connected; the first pad plugged in is player 1’s.

  5. Move both. Search for this->motion_ = step_motion(:

    samples/minimal/states/hello_state.cpp
    this->motion_ = step_motion(this->motion_,
    stick_direction,
    keyboard_direction(keyboard), dt);

    Replace those three lines with:

    this->motion_ = step_motion(this->motion_,
    first_stick, wasd_direction(keyboard), dt);
    this->second_motion_ = step_motion(this->second_motion_,
    second_stick, arrows_direction(keyboard), dt);

    Both use the same rules, step_motion from game\rules\motion.h, each with its own player’s stick and keys. Then search for this->greeting_->set_position( and add two lines after it, so the three read:

    this->greeting_->set_position(greeting_position(this->motion_));
    this->second_greeting_->set_position(
    greeting_position(this->second_motion_));
  6. Dim and restore both. Search for labrador::Colour::dim_gray);, in on_suspend(), and add a line after it; then in on_resume(), just below, do the same, so that the two functions read:

    void HelloState::on_suspend()
    {
    this->greeting_->set_colour(labrador::Colour::dim_gray);
    this->second_greeting_->set_colour(labrador::Colour::dim_gray);
    }
    void HelloState::on_resume()
    {
    this->greeting_->set_colour(labrador::Colour::white);
    this->second_greeting_->set_colour(labrador::Colour::light_sky_blue);
    }

    Save and close Notepad.

  7. Build, test and run. Quit the game if it is open, then run the line from Make a first change’s step 5:

    Terminal window
    cmake --build --preset x64-debug && ctest --preset x64-debug && out\build\x64-debug\game\MyGame.exe

    It compiles hello_state.cpp and main.cpp, which includes the header; on a fast desktop the line took 5 seconds before the game opened. It ends like this:

    Output
    [3/5] Building CXX object game\CMakeFiles\MyGame.dir\states\hello_state.cpp.obj
    [4/5] Building CXX object game\CMakeFiles\MyGame.dir\main.cpp.obj
    [5/5] Linking CXX executable game\MyGame.exe
    Test project C:/dev/mygame/out/build/x64-debug
    Start 1: MyGameTests
    1/2 Test #1: MyGameTests ...................... Passed 0.02 sec
    Start 2: MyGameSmokeTests
    2/2 Test #2: MyGameSmokeTests ................. Passed 0.78 sec
    100% tests passed, 0 tests failed out of 2

    The game opens with two greetings circling, the white one above and the blue Hello from player 2. below it. Hold W and the right arrow together: the white one goes up and the blue one right, at the same time. Escape dims both under the quit question.

Some keyboards cannot report many keys held at once: with three or four keys down, one more is ignored until another is let go. This is the keyboard, not the game. Two pads, or a second keyboard’s keys, do not share that limit; two keyboards plugged into one computer act as one.

With the two players far apart, one window cannot show both. This section gives each player half the window, the top for player 1 and the bottom for player 2, each with a camera that follows its own player. Do A camera that follows the player first: it adds the signposts that show each half moving, takes the hint out of the world and adds the camera lines that this section changes. Its steps work on top of this page’s.

  1. Make room for two views. Each view costs memory set aside when the game starts, and the starter sets aside one. Open game\main.cpp:

    Terminal window
    notepad game\main.cpp

    Search for view_capacity and change the 1 to 2, so the line reads:

    options.view_capacity = 2;

    Save and close Notepad, then make the same change in game\main_web.cpp, for the browser:

    Terminal window
    notepad game\main_web.cpp
  2. Choose the two-player layout. Open game\states\hello_state.cpp, search for this->scene_ = std::make_unique<Scene>(, in init(), and add a line after it, so the two read:

    this->scene_ = std::make_unique<Scene>(nullptr, nullptr);
    this->app_->viewport_manager()->set_layout(ScreenLayout::two_player);

    The engine’s two-player layout is the window cut across the middle, with a line two pixels thick between the halves. Then search for Vector2F(24.0f, resolution.y - 40.0f), where the hint is placed, and change it to Vector2F(24.0f, 12.0f): each half is only half as high, so the hint goes at the top of each half.

  3. One camera per player. Search for // The camera: the point the greeting circles, the lines the camera page added at the end of update(), and replace them, from that comment down to the ); after Camera(, with:

    // Split screen: player 1 in the top half, player 2 in the bottom, each
    // half with a camera on its own player.
    const ViewportManager& viewports = *this->app_->viewport_manager();
    const Viewport top = viewports.player_viewport(0);
    const Viewport bottom = viewports.player_viewport(1);
    this->scene_->clear_views();
    this->scene_->add_view(top,
    Camera(this->motion_.centre - top.size() * 0.5f, 1.0f));
    this->scene_->add_view(bottom,
    Camera(this->second_motion_.centre - bottom.size() * 0.5f, 1.0f));

    If you added the world’s edge from the camera page, these lines replace that too, and nothing uses its limits any more: search for constexpr float world_min, near the top, and delete that line, the world_max line under it and the two comment lines above them. Left in, they build on Windows and stop the browser build. The hint is drawn into each half by the lines in draw, which run once per view. Save and close Notepad.

  4. Show the quit question over both halves. The question draws into the first view, which is now only the top half, and it sits below the middle of the window, so it would not show. Open its code:

    Terminal window
    notepad game\states\confirm_state.cpp

    Search for DrawList list = renderer.view(0); and add lines after it, so that they read:

    DrawList list = renderer.view(0);
    // Over the whole window, not just the top half of a split screen, and
    // with no camera.
    list.set_viewport(this->app_->viewport_manager()->fullscreen_viewport());
    list.set_camera(Camera::default_camera);

    Save and close Notepad.

  5. Build, test and run with the line from step 7. It compiles main.cpp, hello_state.cpp and confirm_state.cpp, about 5 seconds. The window is cut in two, each half with the hint along its top and its own signposts. The white greeting is in the middle of the top half and the blue one in the middle of the bottom half; each half also shows the other player when they are close. Hold W and the left arrow: the top half’s signposts move down and the bottom half’s move right. Escape shows the question across the middle.

Touch markers are placed through the first view only, so in the bottom half they land in the wrong place; the camera page’s line for them uses this->scene_->view(0). The local-multiplayer sample goes further: a lobby where each player joins with a key or a pad of their choice, a title bar in each half, and one world updated once for both.

The line numbers in these messages are those of a starter with only Make a first change and this page’s steps done; changes from other pages move them.

If one greeting does not move, check its keys in step 2 and which function each step_motion line in step 5 calls. If the quit question does not show in the split screen, step 4 is missing.

Build the page as Build for the browser shows, in a prompt where you have run emsdk_env.bat:

Terminal window
cmake --build --preset wasm-debug && ctest --preset wasm-debug

The keys are the same in the page. The line under the game, in game\web\shell.html, still describes one player: search for Escape asks to quit; Enter says yes. and change the words between the quotation marks on that line, for example to:

"Player 1: WASD or pad 1. Player 2: the arrows or pad 2. Escape asks to quit; Enter says yes.";

On 31 October 2026, in a starter project made from a starter ZIP of this tree, with Visual Studio 2026’s compiler 19.51 and Emscripten 6.0.12, every step above was made as worded by an agent editing the files with tools rather than Notepad, and the build and tests ran as shown, with the project’s folder in the quoted output changed to C:\dev\mygame. The game was started and driven with key presses from a script, and screenshots showed both greetings, the white one moved up and the blue one right after W and the right arrow were held together for half a second, and both dimmed under the quit question. The split screen was made on top of these steps and the camera page’s, and screenshots showed the two halves with their signposts and hints, each half following its player after W and the left arrow, and, once step 4 was added, the quit question across the middle; before step 4 the question did not show. The page was built under wasm-debug, its test passed, and headless Chrome showed both greetings moved after KeyW and ArrowRight were held together, and the split screen with both halves moved and the quit question across the middle. Each message above was made on purpose, the stick_direction one and the start-up test’s set_view_count(2) failure included. No gamepad was available: the pads’ lines were built, and which pad moves which player is read from the code.

A second agent then followed the page as worded in another fresh starter, after Make a first change’s steps 2 to 4 and the camera page. It built and tested the result on x64-debug and wasm-debug; in headless Chrome both players moved together, and the split screen showed the quit question only once step 4 of that section was done. It sent no keys to Windows windows after its first runs. The page was corrected for what it found: step 4 left the starter’s comment about every slot sharing one greeting, which is no longer true, so it now replaces the comment with the lines; and the browser build’s unused function message named line 67, where the old function is only if the new ones are pasted above it. The agent that made the corrections repeated steps 1 to 7 as now worded in a third fresh starter: they built and passed on x64-debug and wasm-debug, and a screenshot of the game’s own window, taken without sending it keys, showed both greetings and the new hint. With the comment replaced, step 5 left undone gives C2065 for stick_direction at line 187 and then C3861 at 188; the header’s line left out gives C2039 at 122; and the old function left where it was builds on Windows and stops the browser build at 43:11. On 2026-11-04 the starter’s slot loop became a tested rule with shorter comments, which moves the first two messages three lines up, to 184 and 185, as the messages above now say; that was counted from the change, not seen in a build.

A reviewer then did the camera page, its world’s edge included, then this page and the split screen, in another fresh starter. On Windows each stage built and passed, and screenshots showed the second greeting, player 1 followed up after W, and the split screen with the top half’s signposts moved down and the bottom half’s right after W and the left arrow, both greetings dimmed and the question across the middle. After the camera page, step 3’s search for the hint’s end found nothing, since that page leaves one ), and the browser build stopped with unused variable 'world_min' at 37:18 and 'world_max' at 38:18 until the world’s limits were deleted; the page was corrected for both, and the browser build then passed its test.

Development documentation (unreleased). Built from Labrador aceb130 of 2026-11-20.

All documentation versions