2018-05-20 00:02:39 +02:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "input.h"
|
|
|
|
#include "vector2d.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
input_init(Input *input)
|
|
|
|
{
|
|
|
|
input->keyState = 0;
|
|
|
|
input->lastKeyState = 0;
|
|
|
|
input->mouseButtonState = 0;
|
|
|
|
input->lastMouseButtonState = 0;
|
|
|
|
input->mouseX = 0;
|
|
|
|
input->mouseY = 0;
|
2018-05-20 16:53:16 +02:00
|
|
|
input->lastMouseX = 0;
|
|
|
|
input->lastMouseY = 0;
|
|
|
|
input->modKeyState = 0;
|
|
|
|
input->lastModKeyState = 0;
|
2018-05-20 00:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void input_reset(Input *input)
|
|
|
|
{
|
|
|
|
input->lastKeyState = input->keyState;
|
|
|
|
input->lastMouseButtonState = input->mouseButtonState;
|
2018-05-20 16:53:16 +02:00
|
|
|
input->lastModKeyState = input->modKeyState;
|
2018-05-20 00:02:39 +02:00
|
|
|
input->keyState = 0;
|
|
|
|
input->mouseButtonState = 0;
|
2018-05-20 16:53:16 +02:00
|
|
|
input->modKeyState = 0;
|
|
|
|
input->lastMouseX = input->mouseX;
|
|
|
|
input->lastMouseY = input->mouseY;
|
2018-05-20 00:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Uint64
|
|
|
|
get_event_key(SDL_Event *event)
|
|
|
|
{
|
|
|
|
switch (event->key.keysym.sym) {
|
|
|
|
case SDLK_UP:
|
|
|
|
case SDLK_k:
|
|
|
|
case SDLK_w:
|
|
|
|
return KEY_UP;
|
|
|
|
case SDLK_DOWN:
|
|
|
|
case SDLK_j:
|
|
|
|
case SDLK_s:
|
2018-05-20 16:53:16 +02:00
|
|
|
return KEY_DOWN;
|
2018-05-20 00:02:39 +02:00
|
|
|
case SDLK_LEFT:
|
|
|
|
case SDLK_h:
|
|
|
|
case SDLK_a:
|
|
|
|
return KEY_LEFT;
|
|
|
|
case SDLK_RIGHT:
|
|
|
|
case SDLK_l:
|
|
|
|
case SDLK_d:
|
|
|
|
return KEY_RIGHT;
|
|
|
|
case SDLK_0:
|
|
|
|
return KEY_NUM0;
|
|
|
|
case SDLK_1:
|
|
|
|
return KEY_NUM1;
|
|
|
|
case SDLK_2:
|
|
|
|
return KEY_NUM2;
|
|
|
|
case SDLK_3:
|
|
|
|
return KEY_NUM3;
|
|
|
|
case SDLK_4:
|
|
|
|
return KEY_NUM4;
|
|
|
|
case SDLK_5:
|
|
|
|
return KEY_NUM5;
|
|
|
|
case SDLK_6:
|
|
|
|
return KEY_NUM6;
|
|
|
|
case SDLK_7:
|
|
|
|
return KEY_NUM7;
|
|
|
|
case SDLK_8:
|
|
|
|
return KEY_NUM8;
|
|
|
|
case SDLK_9:
|
|
|
|
return KEY_NUM9;
|
|
|
|
case SDLK_ESCAPE:
|
|
|
|
return KEY_ESC;
|
|
|
|
case SDLK_RETURN:
|
|
|
|
return KEY_ENTER;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 16:53:16 +02:00
|
|
|
static Uint32
|
|
|
|
get_event_modkey(SDL_Event *event)
|
|
|
|
{
|
|
|
|
if (event->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
|
|
|
|
switch (event->key.keysym.sym) {
|
|
|
|
case SDLK_s:
|
|
|
|
return KEY_CTRL_S;
|
|
|
|
case SDLK_m:
|
|
|
|
return KEY_CTRL_M;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-20 00:02:39 +02:00
|
|
|
static Uint32
|
|
|
|
get_event_mousebutton(SDL_Event *event)
|
|
|
|
{
|
|
|
|
switch (event->button.button) {
|
|
|
|
case SDL_BUTTON_LEFT:
|
|
|
|
return MBUTTON_LEFT;
|
|
|
|
case SDL_BUTTON_MIDDLE:
|
|
|
|
return MBUTTON_MIDDLE;
|
|
|
|
case SDL_BUTTON_RIGHT:
|
|
|
|
return MBUTTON_RIGHT;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
input_handle_event(Input *input, SDL_Event *event)
|
|
|
|
{
|
2018-05-20 16:53:16 +02:00
|
|
|
if (event->type == SDL_KEYDOWN) {
|
|
|
|
Uint32 key;
|
|
|
|
if ((key = get_event_modkey(event)))
|
|
|
|
input->modKeyState |= key;
|
|
|
|
else
|
|
|
|
input->keyState |= get_event_key(event);
|
|
|
|
}
|
|
|
|
else if (event->type == SDL_KEYUP) {
|
|
|
|
Uint32 key;
|
|
|
|
if ((key = get_event_modkey(event)))
|
|
|
|
input->modKeyState &= ~key;
|
|
|
|
else
|
|
|
|
input->keyState &= ~get_event_key(event);
|
|
|
|
}
|
2018-05-20 00:02:39 +02:00
|
|
|
else if (event->type == SDL_MOUSEBUTTONDOWN)
|
|
|
|
input->mouseButtonState |= get_event_mousebutton(event);
|
|
|
|
else if (event->type == SDL_MOUSEBUTTONUP)
|
|
|
|
input->mouseButtonState &= ~get_event_mousebutton(event);
|
|
|
|
else if (event->type == SDL_MOUSEMOTION) {
|
|
|
|
input->mouseX = event->motion.x;
|
|
|
|
input->mouseY = event->motion.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
input_key_is_pressed(Input *input, Uint64 key)
|
|
|
|
{
|
|
|
|
return (input->keyState & key) && !(input->lastKeyState & key);
|
|
|
|
}
|
|
|
|
|
2018-05-20 16:53:16 +02:00
|
|
|
bool
|
|
|
|
input_modkey_is_pressed(Input *input, Uint32 key)
|
|
|
|
{
|
|
|
|
return (input->modKeyState & key)
|
|
|
|
&& !(input->lastModKeyState & key);
|
|
|
|
}
|
|
|
|
|
2018-05-20 00:02:39 +02:00
|
|
|
bool
|
|
|
|
input_key_is_released(Input *input, Uint64 key)
|
|
|
|
{
|
|
|
|
return (input->lastKeyState & key) && !(input->keyState & key);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
input_key_is_down(Input *input, Uint64 key)
|
|
|
|
{
|
|
|
|
return input->keyState & key;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
input_mousebutton_is_pressed(Input *input, Uint8 button)
|
|
|
|
{
|
|
|
|
return (input->mouseButtonState & button)
|
|
|
|
&& !(input->lastMouseButtonState & button);
|
|
|
|
}
|
2018-05-20 16:53:16 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
input_mouse_moved(Input *input)
|
|
|
|
{
|
|
|
|
return (input->mouseX != input->lastMouseX)
|
|
|
|
|| (input->mouseY != input->lastMouseY);
|
|
|
|
}
|