2015-11-23 15:52:11 +01:00
|
|
|
/*
|
2016-02-21 16:50:27 +01:00
|
|
|
Copyright (C) 2015-2016 Parallel Realities
|
2015-11-23 15:52:11 +01:00
|
|
|
|
|
|
|
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 2
|
|
|
|
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, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "input.h"
|
|
|
|
|
|
|
|
static SDL_Texture *mousePointer;
|
|
|
|
|
|
|
|
void initInput(void)
|
|
|
|
{
|
|
|
|
memset(&app.mouse, 0, sizeof(Mouse));
|
2016-03-03 19:03:07 +01:00
|
|
|
|
2015-11-23 15:52:11 +01:00
|
|
|
mousePointer = getTexture("gfx/input/mousePointer.png");
|
2016-03-03 19:03:07 +01:00
|
|
|
|
2015-11-24 08:16:48 +01:00
|
|
|
SDL_QueryTexture(mousePointer, NULL, NULL, &app.mouse.w, &app.mouse.h);
|
2015-11-23 15:52:11 +01:00
|
|
|
}
|
|
|
|
|
2016-03-05 13:35:19 +01:00
|
|
|
void doKeyDown(SDL_KeyboardEvent *event)
|
|
|
|
{
|
|
|
|
if (event->keysym.scancode >= 0 && event->keysym.scancode < MAX_KEYBOARD_KEYS && event->repeat == 0)
|
|
|
|
{
|
|
|
|
app.keyboard[event->keysym.scancode] = 1;
|
|
|
|
app.lastKeyPressed = event->keysym.scancode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void doKeyUp(SDL_KeyboardEvent *event)
|
|
|
|
{
|
|
|
|
if (event->keysym.scancode >= 0 && event->keysym.scancode < MAX_KEYBOARD_KEYS)
|
|
|
|
{
|
|
|
|
app.keyboard[event->keysym.scancode] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 15:52:11 +01:00
|
|
|
void doMouseDown(SDL_MouseButtonEvent *event)
|
|
|
|
{
|
2016-03-03 19:03:07 +01:00
|
|
|
if (event->button >= 0 && event->button < MAX_MOUSE_BUTTONS)
|
|
|
|
{
|
|
|
|
app.mouse.button[event->button] = 1;
|
2016-03-05 13:35:19 +01:00
|
|
|
app.lastButtonPressed = event->button;
|
2016-03-03 19:03:07 +01:00
|
|
|
}
|
2015-11-23 15:52:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void doMouseUp(SDL_MouseButtonEvent *event)
|
|
|
|
{
|
2016-03-03 19:03:07 +01:00
|
|
|
if (event->button >= 0 && event->button < MAX_MOUSE_BUTTONS)
|
|
|
|
{
|
|
|
|
app.mouse.button[event->button] = 0;
|
|
|
|
}
|
2015-11-23 15:52:11 +01:00
|
|
|
}
|
|
|
|
|
2016-03-03 19:03:07 +01:00
|
|
|
/*
|
|
|
|
* Note: the following assumes that SDL_BUTTON_X1 and SDL_BUTTON_X2 are 4 and 5, respectively. They usually are.
|
|
|
|
*/
|
2015-11-24 08:16:48 +01:00
|
|
|
void doMouseWheel(SDL_MouseWheelEvent *event)
|
2015-11-23 15:52:11 +01:00
|
|
|
{
|
2015-11-24 08:16:48 +01:00
|
|
|
if (event->y == -1)
|
|
|
|
{
|
|
|
|
app.mouse.button[SDL_BUTTON_X1] = 1;
|
2016-03-05 13:35:19 +01:00
|
|
|
app.lastButtonPressed = SDL_BUTTON_X1;
|
2015-11-24 08:16:48 +01:00
|
|
|
}
|
2016-03-03 19:03:07 +01:00
|
|
|
|
2015-11-24 08:16:48 +01:00
|
|
|
if (event->y == 1)
|
|
|
|
{
|
|
|
|
app.mouse.button[SDL_BUTTON_X2] = 1;
|
2016-03-05 13:35:19 +01:00
|
|
|
app.lastButtonPressed = SDL_BUTTON_X2;
|
2015-11-24 08:16:48 +01:00
|
|
|
}
|
2015-11-23 15:52:11 +01:00
|
|
|
}
|
|
|
|
|
2016-02-14 12:07:15 +01:00
|
|
|
void doMouseMotion(SDL_MouseMotionEvent *event)
|
|
|
|
{
|
|
|
|
app.mouse.dx = event->xrel;
|
|
|
|
app.mouse.dy = event->yrel;
|
|
|
|
}
|
|
|
|
|
2015-11-23 15:52:11 +01:00
|
|
|
void drawMouse(void)
|
|
|
|
{
|
|
|
|
int x, y;
|
2016-03-03 19:03:07 +01:00
|
|
|
|
2015-11-23 15:52:11 +01:00
|
|
|
SDL_GetMouseState(&x, &y);
|
2016-03-03 19:03:07 +01:00
|
|
|
|
2015-11-23 15:52:11 +01:00
|
|
|
app.mouse.x = x * app.scaleX;
|
|
|
|
app.mouse.y = y * app.scaleY;
|
2016-03-03 19:03:07 +01:00
|
|
|
|
2015-11-23 15:52:11 +01:00
|
|
|
blit(mousePointer, app.mouse.x, app.mouse.y, 1);
|
|
|
|
}
|
2016-02-23 08:19:31 +01:00
|
|
|
|
|
|
|
void clearInput(void)
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
2016-03-05 14:49:07 +01:00
|
|
|
|
|
|
|
memset(app.keyboard, 0, sizeof(int) * MAX_KEYBOARD_KEYS);
|
|
|
|
memset(app.mouse.button, 0, sizeof(int) * MAX_MOUSE_BUTTONS);
|
2016-03-03 19:03:07 +01:00
|
|
|
|
2016-02-23 08:19:31 +01:00
|
|
|
while (SDL_PollEvent(&event))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|