<commit_before>//
// Input Implementation
//
#include "../Engine.h"

#include <unordered_map>

namespace sge {

// Map of Keycodes which are currently down.
static std::unordered_map<SDL_Keycode, bool> keyMap;
static std::unordered_map<SDL_Keycode, bool> keysReleased;
static std::unordered_map<SDL_Keycode, bool> keysPressed;

// Mouse Stuff
static std::unordered_map<u32, bool> mouseMap;
static std::unordered_map<u32, bool> mouseReleased;
static std::unordered_map<u32, bool> mousePressed;

static bool mouseLocked;
static Vector2 mousePos;
static Vector2 mouseDelta;

// Other signalled Events caught. These should be flagged and
// handled by the caller.
static bool quit;

// Poll SDL_Event queue and update input state. Should be
// called once per interval before calling other input
// queries.
void Input::Update () {
    keysReleased.clear();
    keysPressed.clear();
    mouseReleased.clear();
    mousePressed.clear();

    mouseDelta.x = mouseDelta.y = 0.0f;

    SDL_Event evt;
    while (SDL_PollEvent(&evt)) {
        switch (evt.type) {
        case SDL_QUIT:
            quit = true;
            break;
        case SDL_KEYDOWN:
            keysPressed[evt.key.keysym.sym] = !keyMap[evt.key.keysym.sym];
            keyMap[evt.key.keysym.sym] = true;
            break;
        case SDL_KEYUP:
            keysReleased[evt.key.keysym.sym] = keyMap[evt.key.keysym.sym];
            keyMap[evt.key.keysym.sym] = false;
            break;
        case SDL_MOUSEMOTION:
            s32 x, y;
            s32 dx, dy;
            SDL_GetMouseState(&x, &y);
            SDL_GetRelativeMouseState(&dx, &dy);

            mousePos.x = static_cast<float>(x);
            mousePos.y = static_cast<float>(y);
            mouseDelta.x += static_cast<float>(dx);
            mouseDelta.y += static_cast<float>(dy);
            break;
        case SDL_MOUSEBUTTONDOWN:
            mousePressed[evt.button.button] = !mouseMap[evt.button.button];
            mouseMap[evt.button.button] = true;
            break;
        case SDL_MOUSEBUTTONUP:
            mouseReleased[evt.button.button] = mouseMap[evt.button.button];
            mouseMap[evt.button.button] = false;
            break;
        default:
            break;
        }
    }
}

//--------------------------
// Keyboard Queries
//--------------------------

bool Input::KeyDown (const SDL_Keycode keyCode) {
    return keyMap[keyCode];
}

bool Input::KeyPressed (const SDL_Keycode keyCode) {
    return keysPressed[keyCode];
}

bool Input::KeyReleased (const SDL_Keycode keyCode) {
    return keysReleased[keyCode];
}

//--------------------------
// Mouse Queries
//--------------------------


void Input::LockMouse () {
    SDL_SetRelativeMouseMode(SDL_TRUE);
    mouseLocked = true;
}

void Input::ReleaseMouse () {
    SDL_SetRelativeMouseMode(SDL_FALSE);
    mouseLocked = false;
}

bool Input::MouseLocked () {
    return mouseLocked;
}

Vector2 Input::GetMousePosition () {
    return mousePos;
}
<commit_msg>Fix camera jerk on entering/exiting mouselock<commit_after>//
// Input Implementation
//
#include "../Engine.h"

#include <unordered_map>

namespace sge {

// Map of Keycodes which are currently down.
static std::unordered_map<SDL_Keycode, bool> keyMap;
static std::unordered_map<SDL_Keycode, bool> keysReleased;
static std::unordered_map<SDL_Keycode, bool> keysPressed;

// Mouse Stuff
static std::unordered_map<u32, bool> mouseMap;
static std::unordered_map<u32, bool> mouseReleased;
static std::unordered_map<u32, bool> mousePressed;

// Cache mouse positions so we can query mouse delta
// multiple times per update.
static bool mouseLocked;
static Vector2 mousePos;
static Vector2 mouseDelta;

// Other signalled Events caught. These should be flagged and
// handled by the caller.
static bool quit;

// Poll SDL_Event queue and update input state. Should be
// called once per interval before calling other input
// queries.
void Input::Update () {
    keysReleased.clear();
    keysPressed.clear();
    mouseReleased.clear();
    mousePressed.clear();

    mouseDelta.x = mouseDelta.y = 0.0f;

    SDL_Event evt;
    while (SDL_PollEvent(&evt)) {
        switch (evt.type) {
        case SDL_QUIT:
            quit = true;
            break;
        case SDL_KEYDOWN:
            keysPressed[evt.key.keysym.sym] = !keyMap[evt.key.keysym.sym];
            keyMap[evt.key.keysym.sym] = true;
            break;
        case SDL_KEYUP:
            keysReleased[evt.key.keysym.sym] = keyMap[evt.key.keysym.sym];
            keyMap[evt.key.keysym.sym] = false;
            break;
        case SDL_MOUSEMOTION:
            s32 x, y, dx, dy;

            SDL_GetMouseState(&x, &y);
            SDL_GetRelativeMouseState(&dx, &dy);

            mousePos.x = static_cast<float>(x);
            mousePos.y = static_cast<float>(y);
            mouseDelta.x += static_cast<float>(dx);
            mouseDelta.y += static_cast<float>(dy);
            break;
        case SDL_MOUSEBUTTONDOWN:
            mousePressed[evt.button.button] = !mouseMap[evt.button.button];
            mouseMap[evt.button.button] = true;
            break;
        case SDL_MOUSEBUTTONUP:
            mouseReleased[evt.button.button] = mouseMap[evt.button.button];
            mouseMap[evt.button.button] = false;
            break;
        default:
            break;
        }
    }
}

//--------------------------
// Keyboard Queries
//--------------------------

bool Input::KeyDown (const SDL_Keycode keyCode) {
    return keyMap[keyCode];
}

bool Input::KeyPressed (const SDL_Keycode keyCode) {
    return keysPressed[keyCode];
}

bool Input::KeyReleased (const SDL_Keycode keyCode) {
    return keysReleased[keyCode];
}

//--------------------------
// Mouse Queries
//--------------------------


void Input::LockMouse () {
    s32 x, y;
    SDL_SetRelativeMouseMode(SDL_TRUE);
    // Throw away any current accumulated mouse delta. Avoids sudden
    // jerk when entering locked mouse state.
    SDL_GetRelativeMouseState(&x, &y);
    mouseLocked = true;
}

void Input::ReleaseMouse () {
    SDL_SetRelativeMouseMode(SDL_FALSE);
    mouseLocked = false;
}

bool Input::MouseLocked () {
    return mouseLocked;
}

Vector2 Input::GetMousePosition () {
    return mousePos;
}

{'fraction_non_alphanumeric': 0.14203270278935556, 'fraction_numerical': 0.0048092337287592175, 'mean_word_length': 2.385241454150841, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3381184', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1940, 'n_words': 435}