329 lines
8.2 KiB
C++
329 lines
8.2 KiB
C++
// IAEngine: 2D Game Engine by IA
|
|
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#include <IAEngine/Engine.hpp>
|
|
#include <InputManager.hpp>
|
|
|
|
namespace ia::iae
|
|
{
|
|
EXTERN SDL_Window *g_windowHandle;
|
|
|
|
BOOL InputManager::s_keys[256];
|
|
BOOL InputManager::s_prevKeys[256];
|
|
Vec2 InputManager::s_pointerPosition{};
|
|
InputManager::KeyboardGamePadMapping InputManager::s_keyboardGamePadMapping{};
|
|
|
|
BOOL InputManager::s_buttonA{};
|
|
BOOL InputManager::s_buttonB{};
|
|
BOOL InputManager::s_buttonC{};
|
|
BOOL InputManager::s_buttonD{};
|
|
INT16 InputManager::s_verticalAxis{};
|
|
INT16 InputManager::s_horizontalAxis{};
|
|
|
|
BOOL InputManager::s_onScreenGamePadEnabled{};
|
|
BOOL InputManager::s_keyboardGamePadEnabled{};
|
|
|
|
VOID InputManager::Initialize()
|
|
{
|
|
memset(s_keys, 0, sizeof(s_keys));
|
|
memset(s_prevKeys, 0, sizeof(s_prevKeys));
|
|
}
|
|
|
|
VOID InputManager::Terminate()
|
|
{
|
|
}
|
|
|
|
VOID InputManager::OnSDLEvent(IN SDL_Event *event)
|
|
{
|
|
memcpy(s_prevKeys, s_keys, sizeof(s_prevKeys));
|
|
switch (event->type)
|
|
{
|
|
case SDL_EVENT_KEY_DOWN:
|
|
s_keys[event->key.scancode] = true;
|
|
break;
|
|
|
|
case SDL_EVENT_KEY_UP:
|
|
s_keys[event->key.scancode] = false;
|
|
break;
|
|
|
|
case SDL_EVENT_MOUSE_MOTION:
|
|
s_pointerPosition = {event->motion.x, event->motion.y};
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
VOID InputManager::SwitchModeToText()
|
|
{
|
|
SDL_StartTextInput(g_windowHandle);
|
|
}
|
|
|
|
VOID InputManager::SwitchModeToAction()
|
|
{
|
|
SDL_StopTextInput(g_windowHandle);
|
|
}
|
|
|
|
Vec2 InputManager::GetPointerPosition()
|
|
{
|
|
return s_pointerPosition;
|
|
}
|
|
|
|
BOOL InputManager::IsKeyDown(IN InputKey key)
|
|
{
|
|
return s_keys[(UINT8) key];
|
|
}
|
|
|
|
BOOL InputManager::WasKeyPressed(IN InputKey key)
|
|
{
|
|
return !s_prevKeys[(UINT8) key] && s_keys[(UINT8) key];
|
|
}
|
|
|
|
BOOL InputManager::WasKeyReleased(IN InputKey key)
|
|
{
|
|
return s_prevKeys[(UINT8) key] && !s_keys[(UINT8) key];
|
|
}
|
|
|
|
VOID InputManager::SetupOnScreenGamePad()
|
|
{
|
|
s_onScreenGamePadEnabled = true;
|
|
}
|
|
|
|
VOID InputManager::SetupKeyboardGamePad(IN CONST KeyboardGamePadMapping &mapping)
|
|
{
|
|
s_keyboardGamePadEnabled = true;
|
|
s_keyboardGamePadMapping = mapping;
|
|
}
|
|
|
|
BOOL InputManager::GetButtonA()
|
|
{
|
|
return s_buttonA;
|
|
}
|
|
|
|
BOOL InputManager::GetButtonB()
|
|
{
|
|
return s_buttonB;
|
|
}
|
|
|
|
BOOL InputManager::GetButtonC()
|
|
{
|
|
return s_buttonC;
|
|
}
|
|
|
|
BOOL InputManager::GetButtonD()
|
|
{
|
|
return s_buttonD;
|
|
}
|
|
|
|
INT16 InputManager::GetVerticalAxis()
|
|
{
|
|
return s_verticalAxis;
|
|
}
|
|
|
|
INT16 InputManager::GetHorizontalAxis()
|
|
{
|
|
return s_horizontalAxis;
|
|
}
|
|
|
|
IVec2 InputManager::GetDirectionalInput()
|
|
{
|
|
return IVec2{GetHorizontalAxis(), GetVerticalAxis()};
|
|
}
|
|
|
|
VOID InputManager::SetButtonA(IN BOOL value)
|
|
{
|
|
s_buttonA = value;
|
|
}
|
|
|
|
VOID InputManager::SetButtonB(IN BOOL value)
|
|
{
|
|
s_buttonB = value;
|
|
}
|
|
|
|
VOID InputManager::SetButtonC(IN BOOL value)
|
|
{
|
|
s_buttonC = value;
|
|
}
|
|
|
|
VOID InputManager::SetButtonD(IN BOOL value)
|
|
{
|
|
s_buttonD = value;
|
|
}
|
|
|
|
VOID InputManager::SetVerticalAxis(IN INT16 value)
|
|
{
|
|
s_verticalAxis = value;
|
|
}
|
|
|
|
VOID InputManager::SetHorizontalAxis(IN INT16 value)
|
|
{
|
|
s_horizontalAxis = value;
|
|
}
|
|
|
|
VOID InputManager::Draw()
|
|
{
|
|
if (s_onScreenGamePadEnabled)
|
|
{
|
|
//Engine::SetRenderState_Texture(0);
|
|
//Engine::SetRenderState_FlippedH(false);
|
|
//Engine::SetRenderState_FlippedV(false);
|
|
//Engine::SetRenderState_ColorOverlay({0xFF, 0xFF, 0xFF, 0xFF});
|
|
//Engine::SetRenderState_TextureOffset({0, 0});
|
|
//Engine::SetRenderState_CameraRelative(false);
|
|
//Engine::SetRenderState_Transform({300.0f, 500.0f},
|
|
// {100.0f, 100.0f},
|
|
// 0);
|
|
//Engine::DrawGeometry(Engine::GetGeometry_Circle(), 0xFF, 0);
|
|
}
|
|
}
|
|
|
|
VOID InputManager::Update()
|
|
{
|
|
s_buttonA = false;
|
|
s_buttonB = false;
|
|
s_buttonC = false;
|
|
s_buttonD = false;
|
|
s_verticalAxis = 0;
|
|
s_horizontalAxis = 0;
|
|
|
|
if (s_onScreenGamePadEnabled)
|
|
{
|
|
}
|
|
|
|
if (s_keyboardGamePadEnabled)
|
|
{
|
|
s_buttonA |= IsKeyDown(s_keyboardGamePadMapping.ButtonA);
|
|
s_buttonB |= IsKeyDown(s_keyboardGamePadMapping.ButtonB);
|
|
s_buttonC |= IsKeyDown(s_keyboardGamePadMapping.ButtonC);
|
|
s_buttonD |= IsKeyDown(s_keyboardGamePadMapping.ButtonD);
|
|
s_verticalAxis +=
|
|
IsKeyDown(s_keyboardGamePadMapping.AxisDown) + IsKeyDown(s_keyboardGamePadMapping.AxisUp) * -1;
|
|
s_horizontalAxis +=
|
|
IsKeyDown(s_keyboardGamePadMapping.AxisRight) + IsKeyDown(s_keyboardGamePadMapping.AxisLeft) * -1;
|
|
}
|
|
}
|
|
} // namespace ia::iae
|
|
|
|
namespace ia::iae
|
|
{
|
|
VOID Engine::Input_SwitchModeToText()
|
|
{
|
|
InputManager::SwitchModeToText();
|
|
}
|
|
|
|
VOID Engine::Input_SwitchModeToAction()
|
|
{
|
|
InputManager::SwitchModeToAction();
|
|
}
|
|
|
|
Vec2 Engine::Input_GetPointerPosition()
|
|
{
|
|
return InputManager::GetPointerPosition();
|
|
}
|
|
|
|
BOOL Engine::Input_IsKeyDown(IN InputKey key)
|
|
{
|
|
return InputManager::IsKeyDown(key);
|
|
}
|
|
|
|
BOOL Engine::Input_WasKeyPressed(IN InputKey key)
|
|
{
|
|
return InputManager::WasKeyPressed(key);
|
|
}
|
|
|
|
BOOL Engine::Input_WasKeyReleased(IN InputKey key)
|
|
{
|
|
return InputManager::WasKeyReleased(key);
|
|
}
|
|
|
|
VOID Engine::Input_SetupOnScreenGamePad()
|
|
{
|
|
InputManager::SetupOnScreenGamePad();
|
|
}
|
|
|
|
VOID Engine::Input_SetupKeyboardGamePad(IN InputKey axisLeft, IN InputKey axisRight, IN InputKey axisDown,
|
|
IN InputKey axisUp, IN InputKey buttonA, IN InputKey buttonB,
|
|
IN InputKey buttonC, IN InputKey buttonD)
|
|
{
|
|
InputManager::SetupKeyboardGamePad({axisLeft, axisRight, axisDown, axisUp, buttonA, buttonB, buttonC, buttonD});
|
|
}
|
|
|
|
BOOL Engine::Input_GetButtonA()
|
|
{
|
|
return InputManager::GetButtonA();
|
|
}
|
|
|
|
BOOL Engine::Input_GetButtonB()
|
|
{
|
|
return InputManager::GetButtonB();
|
|
}
|
|
|
|
BOOL Engine::Input_GetButtonC()
|
|
{
|
|
return InputManager::GetButtonC();
|
|
}
|
|
|
|
BOOL Engine::Input_GetButtonD()
|
|
{
|
|
return InputManager::GetButtonD();
|
|
}
|
|
|
|
INT16 Engine::Input_GetVerticalAxis()
|
|
{
|
|
return InputManager::GetVerticalAxis();
|
|
}
|
|
|
|
INT16 Engine::Input_GetHorizontalAxis()
|
|
{
|
|
return InputManager::GetHorizontalAxis();
|
|
}
|
|
|
|
IVec2 Engine::Input_GetDirectionalInput()
|
|
{
|
|
return InputManager::GetDirectionalInput();
|
|
}
|
|
|
|
VOID Engine::Input_SetButtonA(IN BOOL value)
|
|
{
|
|
InputManager::SetButtonA(value);
|
|
}
|
|
|
|
VOID Engine::Input_SetButtonB(IN BOOL value)
|
|
{
|
|
InputManager::SetButtonB(value);
|
|
}
|
|
|
|
VOID Engine::Input_SetButtonC(IN BOOL value)
|
|
{
|
|
InputManager::SetButtonC(value);
|
|
}
|
|
|
|
VOID Engine::Input_SetButtonD(IN BOOL value)
|
|
{
|
|
InputManager::SetButtonD(value);
|
|
}
|
|
|
|
VOID Engine::Input_SetVerticalAxis(IN INT16 value)
|
|
{
|
|
InputManager::SetVerticalAxis(value);
|
|
}
|
|
|
|
VOID Engine::Input_SetHorizontalAxis(IN INT16 value)
|
|
{
|
|
InputManager::SetHorizontalAxis(value);
|
|
}
|
|
} // namespace ia::iae
|