Input class in Thread

Static polling API for keyboard / mouse / gamepad state. Call it from OnUpdate. The engine snapshots every device once per frame, before any script runs, so all systems in a frame observe the same input and the Was… edge queries are exact.

The header core/Input.h pulls in no windowing backend — game code sees only the Thread::Key, Thread::Mouse, Thread::GamepadButton and Thread::GamepadAxis enums, never a GLFW symbol.

Keyboard

IsKeyPressed(Key)boolTrue while the key is held.
WasKeyPressed(Key)boolTrue only on the frame the key went down.
WasKeyReleased(Key)boolTrue only on the frame the key came up.

Mouse

IsMouseButtonPressed(Mouse)boolTrue while held.
WasMouseButtonPressed(Mouse)boolButton-down edge.
WasMouseButtonReleased(Mouse)boolButton-up edge.
GetMousePosition()pair<float,float>Window-space pixel coordinates.
GetMouseDelta()pair<float,float>Movement since the previous frame. Reads zero across cursor warps, cursor-mode changes and alt-tab, so mouse-look never jumps.
GetMouseScroll()pair<float,float>Wheel movement accumulated this frame (y is the usual wheel).
SetMousePosition(x, y)voidWarp the cursor.
SetCursorMode(CursorMode)voidNormal, Hidden or Locked (captured, for mouse-look).
ShowCursor(bool)voidShorthand: true → Normal, false → Locked.
InvalidateMouseDelta()voidForget the remembered cursor position, so the next frame's delta reads zero.

You almost never need to guard the delta yourself. Warps, cursor-mode changes and regaining window focus all drop the remembered position for you, so a controller can read GetMouseDelta() on the very first frame without seeding anything. Re-implementing a “skip the first frame” check on top of that is the usual way to introduce a stutter rather than remove one.

InvalidateMouseDelta() exists for the case the engine cannot see: something outside it moved the pointer. Call it before the next frame is sampled — the engine does exactly that on the focus edge, above Input::Update() in the main loop. Calling it later in the frame silences the frame after the one that needed it, which is no help at all.

Gamepad

Indices 0 .. Input::MaxGamepads-1. Sticks and triggers report [-1, 1]; SetGamepadDeadzone() controls the threshold the action mapping uses to treat an axis as active.

IsGamepadConnected(pad = 0)boolA mapped controller is present.
IsGamepadButtonPressed(GamepadButton, pad = 0)boolTrue while held.
WasGamepadButtonPressed(GamepadButton, pad = 0)boolButton-down edge.
WasGamepadButtonReleased(GamepadButton, pad = 0)boolButton-up edge.
GetGamepadAxis(GamepadAxis, pad = 0)floatRaw axis value.

Action mapping

Bind a named action to any mix of keys, mouse buttons, gamepad buttons and gamepad axes, then query it by name — the gameplay code never mentions a device. scale is the signed weight of a binding, so -1/+1 on opposite keys builds a movement axis.

BindActionKey(name, Key, scale = 1)voidAdd a key binding.
BindActionMouse(name, Mouse, scale = 1)voidAdd a mouse-button binding.
BindActionGamepadButton(name, GamepadButton, scale = 1, pad = 0)voidAdd a gamepad-button binding.
BindActionAxis(name, GamepadAxis, scale = 1, pad = 0)voidAdd a gamepad-axis binding.
ClearAction(name) / ClearAllActions()voidDrop bindings (rebinding).
IsActionPressed(name)boolAny binding active.
WasActionPressed(name)boolAction went active this frame.
WasActionReleased(name)boolAction went inactive this frame.
GetActionValue(name)floatSigned sum of active contributions, clamped to [-1, 1].

Display names

GetKeyName(), GetMouseName(), GetGamepadButtonName() and GetGamepadAxisName() return a stable ASCII label ("Left Shift", "D-Pad Up", …) for rebinding menus and on-screen key prompts. They never return null.

Key

Thread::Key: A..Z, D0..D9, F1..F25, KP0..KP9, Space, Enter, Escape, LeftShift, LeftControl, arrow keys, etc. Thread::Mouse: Left, Right, Middle, Button3..Button7.

Example

// Once, at startup — WASD or the left stick, whichever the player uses.
Thread::Input::BindActionKey("MoveX", Thread::Key::D, +1.0f);
Thread::Input::BindActionKey("MoveX", Thread::Key::A, -1.0f);
Thread::Input::BindActionKey("MoveZ", Thread::Key::S, +1.0f);
Thread::Input::BindActionKey("MoveZ", Thread::Key::W, -1.0f);
Thread::Input::BindActionAxis("MoveX", Thread::GamepadAxis::LeftX);
Thread::Input::BindActionAxis("MoveZ", Thread::GamepadAxis::LeftY);
Thread::Input::BindActionKey("Jump", Thread::Key::Space);
Thread::Input::BindActionGamepadButton("Jump", Thread::GamepadButton::A);

void OnUpdate(float ts) override {
    auto& tc = GetComponent<Thread::TransformComponent>();
    const float speed = 4.0f;
    tc.Translation.x += Thread::Input::GetActionValue("MoveX") * speed * ts;
    tc.Translation.z += Thread::Input::GetActionValue("MoveZ") * speed * ts;

    // Fires once per press, on either the key or the pad.
    if (Thread::Input::WasActionPressed("Jump")) Jump();

    // Mouse-look: the delta is already zeroed across cursor warps.
    auto [dx, dy] = Thread::Input::GetMouseDelta();
    m_Yaw -= dx * 0.0022f;
}

Input klasa w Thread

Statyczne API odpytywania stanu klawiatury, myszy i pada. Wołaj je z OnUpdate. Silnik robi migawkę każdego urządzenia raz na klatkę, zanim wykona się jakikolwiek skrypt, więc wszystkie systemy w klatce widzą to samo wejście, a zapytania o zbocza Was… są dokładne.

Nagłówek core/Input.h nie wciąga żadnego backendu okien — kod gry widzi tylko enumy Thread::Key, Thread::Mouse, Thread::GamepadButton i Thread::GamepadAxis, nigdy symbolu GLFW.

Klawiatura

IsKeyPressed(Key)boolTrue, dopóki klawisz jest wciśnięty.
WasKeyPressed(Key)boolTrue tylko w klatce, w której klawisz został wciśnięty.
WasKeyReleased(Key)boolTrue tylko w klatce, w której klawisz został puszczony.

Mysz

IsMouseButtonPressed(Mouse)boolTrue, dopóki wciśnięty.
WasMouseButtonPressed(Mouse)boolZbocze wciśnięcia.
WasMouseButtonReleased(Mouse)boolZbocze puszczenia.
GetMousePosition()pair<float,float>Współrzędne w pikselach okna.
GetMouseDelta()pair<float,float>Ruch od poprzedniej klatki. Daje zero przy przeskokach kursora, zmianach trybu kursora i alt-tabie, więc rozglądanie myszą nigdy nie skacze.
GetMouseScroll()pair<float,float>Ruch kółka zebrany w tej klatce (y to zwykłe kółko).
SetMousePosition(x, y)voidPrzenosi kursor.
SetCursorMode(CursorMode)voidNormal, Hidden albo Locked (przechwycony, do rozglądania).
ShowCursor(bool)voidSkrót: true → Normal, false → Locked.
InvalidateMouseDelta()voidZapomina zapamiętaną pozycję kursora, więc delta w następnej klatce wynosi zero.

Prawie nigdy nie musisz sam zabezpieczać delty. Przeskoki, zmiany trybu kursora i odzyskanie fokusu okna same kasują zapamiętaną pozycję, więc kontroler może czytać GetMouseDelta() już w pierwszej klatce bez inicjalizacji. Dopisanie własnego „pomiń pierwszą klatkę” to zwykle sposób na wprowadzenie przycięcia, a nie jego usunięcie.

InvalidateMouseDelta() jest na przypadek, którego silnik nie widzi: coś poza nim przesunęło wskaźnik. Wołaj to przed próbkowaniem następnej klatki — silnik robi dokładnie tak przy zmianie fokusu, nad Input::Update() w pętli głównej. Wywołanie później w klatce wycisza klatkę po tej, która tego potrzebowała, czyli nic nie daje.

Pad

Indeksy 0 .. Input::MaxGamepads-1. Gałki i spusty dają [-1, 1]; SetGamepadDeadzone() ustala próg, od którego mapowanie akcji uznaje oś za aktywną.

IsGamepadConnected(pad = 0)boolPodłączony jest zmapowany kontroler.
IsGamepadButtonPressed(GamepadButton, pad = 0)boolTrue, dopóki wciśnięty.
WasGamepadButtonPressed(GamepadButton, pad = 0)boolZbocze wciśnięcia.
WasGamepadButtonReleased(GamepadButton, pad = 0)boolZbocze puszczenia.
GetGamepadAxis(GamepadAxis, pad = 0)floatSurowa wartość osi.

Mapowanie akcji

Przypisz nazwanej akcji dowolną mieszankę klawiszy, przycisków myszy, przycisków i osi pada, a potem pytaj o nią po nazwie — kod gry nigdy nie wspomina urządzenia. scale to waga ze znakiem, więc -1/+1 na przeciwnych klawiszach daje oś ruchu.

BindActionKey(name, Key, scale = 1)voidDodaje przypisanie klawisza.
BindActionMouse(name, Mouse, scale = 1)voidDodaje przypisanie przycisku myszy.
BindActionGamepadButton(name, GamepadButton, scale = 1, pad = 0)voidDodaje przypisanie przycisku pada.
BindActionAxis(name, GamepadAxis, scale = 1, pad = 0)voidDodaje przypisanie osi pada.
ClearAction(name) / ClearAllActions()voidUsuwa przypisania (zmiana klawiszy).
IsActionPressed(name)boolKtóreś przypisanie jest aktywne.
WasActionPressed(name)boolAkcja stała się aktywna w tej klatce.
WasActionReleased(name)boolAkcja przestała być aktywna w tej klatce.
GetActionValue(name)floatSuma aktywnych wkładów ze znakiem, przycięta do [-1, 1].

Nazwy do wyświetlania

GetKeyName(), GetMouseName(), GetGamepadButtonName() i GetGamepadAxisName() zwracają stałą etykietę ASCII ("Left Shift", "D-Pad Up", …) do menu zmiany klawiszy i podpowiedzi na ekranie. Nigdy nie zwracają null.

Key

Thread::Key: A..Z, D0..D9, F1..F25, KP0..KP9, Space, Enter, Escape, LeftShift, LeftControl, strzałki itd. Thread::Mouse: Left, Right, Middle, Button3..Button7.

Przykład

// Raz, przy starcie — WASD albo lewa gałka, czego gracz używa.
Thread::Input::BindActionKey("MoveX", Thread::Key::D, +1.0f);
Thread::Input::BindActionKey("MoveX", Thread::Key::A, -1.0f);
Thread::Input::BindActionKey("MoveZ", Thread::Key::S, +1.0f);
Thread::Input::BindActionKey("MoveZ", Thread::Key::W, -1.0f);
Thread::Input::BindActionAxis("MoveX", Thread::GamepadAxis::LeftX);
Thread::Input::BindActionAxis("MoveZ", Thread::GamepadAxis::LeftY);
Thread::Input::BindActionKey("Jump", Thread::Key::Space);
Thread::Input::BindActionGamepadButton("Jump", Thread::GamepadButton::A);

void OnUpdate(float ts) override {
    auto& tc = GetComponent<Thread::TransformComponent>();
    const float speed = 4.0f;
    tc.Translation.x += Thread::Input::GetActionValue("MoveX") * speed * ts;
    tc.Translation.z += Thread::Input::GetActionValue("MoveZ") * speed * ts;

    // Raz na wciśnięcie, z klawisza albo pada.
    if (Thread::Input::WasActionPressed("Jump")) Jump();

    // Rozglądanie myszą: delta jest już wyzerowana przy przeskokach kursora.
    auto [dx, dy] = Thread::Input::GetMouseDelta();
    m_Yaw -= dx * 0.0022f;
}