#include <game/input.hpp>

namespace Game {
    const bool* Input::mCurrentKeyStates = nullptr;
    bool* Input::mPreviousKeyStates = nullptr;
    int Input::mNumKeys = 0;

    void Input::update() {
        mPreviousKeyStates = const_cast<bool*>(mCurrentKeyStates);
        mCurrentKeyStates = SDL_GetKeyboardState(&mNumKeys);
    }

    bool Input::isKeyPressed(SDL_Scancode key) {
        if (key < 0 || key >= mNumKeys) return false;
        return mCurrentKeyStates[key];
    }

    bool Input::isKeyJustPressed(SDL_Scancode key) {
        if (key < 0 || key >= mNumKeys) return false;
        return mCurrentKeyStates[key] && (!mPreviousKeyStates || !mPreviousKeyStates[key]);
    }

    bool Input::isKeyJustReleased(SDL_Scancode key) {
        if (key < 0 || key >= mNumKeys) return false;
        return (!mCurrentKeyStates[key]) && mPreviousKeyStates && mPreviousKeyStates[key];
    }
}