controller and mouse support
This commit is contained in:
parent
9e7ba471ae
commit
fa76489449
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
.vscode
|
.vscode
|
||||||
build
|
build
|
||||||
|
main.x86_64
|
@ -2,6 +2,27 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
struct Controller
|
||||||
|
{
|
||||||
|
SDL_GameController* controller;
|
||||||
|
std::string name;
|
||||||
|
std::map<Uint8, bool> buttons;
|
||||||
|
Sint16 leftStickX = 0;
|
||||||
|
Sint16 leftStickY = 0;
|
||||||
|
Sint16 rightStickX = 0;
|
||||||
|
Sint16 rightStickY = 0;
|
||||||
|
Sint16 leftTrigger = 0;
|
||||||
|
Sint16 rightTrigger = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Mouse
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int wheel;
|
||||||
|
std::map<int, bool> buttons;
|
||||||
|
};
|
||||||
|
|
||||||
class Input
|
class Input
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@ -9,6 +30,10 @@ class Input
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
bool exit;
|
bool exit;
|
||||||
|
std::map<int, Controller> controllers;
|
||||||
std::map<SDL_Keycode, bool> activeKeys;
|
std::map<SDL_Keycode, bool> activeKeys;
|
||||||
|
Mouse mouse;
|
||||||
|
|
||||||
|
Input();
|
||||||
void update();
|
void update();
|
||||||
};
|
};
|
@ -1,9 +1,14 @@
|
|||||||
|
// prevent multiple inclusion
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
// class for 2D float vector
|
||||||
class float_vec2
|
class float_vec2
|
||||||
{
|
{
|
||||||
|
// public members
|
||||||
public:
|
public:
|
||||||
|
// x and y coordinates
|
||||||
float x, y;
|
float x, y;
|
||||||
|
// operator functions
|
||||||
float_vec2 operator+(float_vec2 vec)
|
float_vec2 operator+(float_vec2 vec)
|
||||||
{
|
{
|
||||||
return { x + vec.x, y + vec.y };
|
return { x + vec.x, y + vec.y };
|
||||||
@ -26,10 +31,14 @@ class float_vec2
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// class for 2D integer vector
|
||||||
class int_vec2
|
class int_vec2
|
||||||
{
|
{
|
||||||
|
// public members
|
||||||
public:
|
public:
|
||||||
|
// x and y coordinates
|
||||||
int x, y;
|
int x, y;
|
||||||
|
// operator functions
|
||||||
int_vec2 operator+(int_vec2 vec)
|
int_vec2 operator+(int_vec2 vec)
|
||||||
{
|
{
|
||||||
return { x + vec.x, y + vec.y };
|
return { x + vec.x, y + vec.y };
|
||||||
|
@ -79,27 +79,31 @@ int main(int argc, char* argv[])
|
|||||||
quit();
|
quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (engine->input.activeKeys[SDLK_d])
|
if (engine->input.activeKeys[SDLK_d] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_RIGHT] || engine->input.controllers[0].leftStickX > 8000)
|
||||||
{
|
{
|
||||||
animations->setAnimation("walk_right");
|
animations->setAnimation("walk_right");
|
||||||
sprite->move({ 1, 0 });
|
sprite->move({ 1, 0 });
|
||||||
}
|
}
|
||||||
if (engine->input.activeKeys[SDLK_a])
|
if (engine->input.activeKeys[SDLK_a] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_LEFT] || engine->input.controllers[0].leftStickX < -8000)
|
||||||
|
|
||||||
{
|
{
|
||||||
animations->setAnimation("walk_left");
|
animations->setAnimation("walk_left");
|
||||||
sprite->move({ -1, 0 });
|
sprite->move({ -1, 0 });
|
||||||
}
|
}
|
||||||
if (engine->input.activeKeys[SDLK_w])
|
if (engine->input.activeKeys[SDLK_w] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_UP] || engine->input.controllers[0].leftStickY < -8000)
|
||||||
{
|
{
|
||||||
animations->setAnimation("walk_back");
|
animations->setAnimation("walk_back");
|
||||||
sprite->move({ 0, -1 });
|
sprite->move({ 0, -1 });
|
||||||
}
|
}
|
||||||
if (engine->input.activeKeys[SDLK_s])
|
if (engine->input.activeKeys[SDLK_s] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_DOWN] || engine->input.controllers[0].leftStickY > 8000)
|
||||||
{
|
{
|
||||||
animations->setAnimation("walk_front");
|
animations->setAnimation("walk_front");
|
||||||
sprite->move({ 0, 1 });
|
sprite->move({ 0, 1 });
|
||||||
}
|
}
|
||||||
if (!engine->input.activeKeys[SDLK_d] && !engine->input.activeKeys[SDLK_a] && !engine->input.activeKeys[SDLK_w] && !engine->input.activeKeys[SDLK_s])
|
if (!engine->input.activeKeys[SDLK_d] && !engine->input.activeKeys[SDLK_a] && !engine->input.activeKeys[SDLK_w] && !engine->input.activeKeys[SDLK_s]
|
||||||
|
&& !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_RIGHT] && !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_LEFT]
|
||||||
|
&& !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_UP] && !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_DOWN]
|
||||||
|
&& engine->input.controllers[0].leftStickX == 0 && engine->input.controllers[0].leftStickY == 0)
|
||||||
{
|
{
|
||||||
animations->setAnimation("idle");
|
animations->setAnimation("idle");
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,56 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
|
Input::Input()
|
||||||
|
{
|
||||||
|
SDL_Init(SDL_INIT_JOYSTICK);
|
||||||
|
exit = false;
|
||||||
|
}
|
||||||
|
|
||||||
void Input::update()
|
void Input::update()
|
||||||
{
|
{
|
||||||
while (SDL_PollEvent(&event))
|
while (SDL_PollEvent(&event))
|
||||||
{
|
{
|
||||||
if (event.type == SDL_KEYDOWN)
|
switch (event.type)
|
||||||
{
|
{
|
||||||
|
case SDL_CONTROLLERBUTTONDOWN:
|
||||||
|
controllers[event.cbutton.which].buttons[event.cbutton.button] = true;
|
||||||
|
break;
|
||||||
|
case SDL_CONTROLLERBUTTONUP:
|
||||||
|
controllers[event.cbutton.which].buttons[event.cbutton.button] = false;
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
|
mouse.buttons[event.button.button] = true;
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEBUTTONUP:
|
||||||
|
mouse.buttons[event.button.button] = false;
|
||||||
|
break;
|
||||||
|
case SDL_KEYDOWN:
|
||||||
activeKeys[event.key.keysym.sym] = true;
|
activeKeys[event.key.keysym.sym] = true;
|
||||||
}
|
break;
|
||||||
else if (event.type == SDL_KEYUP)
|
case SDL_KEYUP:
|
||||||
{
|
|
||||||
activeKeys.erase(event.key.keysym.sym);
|
activeKeys.erase(event.key.keysym.sym);
|
||||||
}
|
break;
|
||||||
else if (event.type == SDL_QUIT)
|
case SDL_CONTROLLERDEVICEADDED:
|
||||||
{
|
controllers[SDL_NumJoysticks() - 1] = {
|
||||||
|
SDL_GameControllerOpen(SDL_NumJoysticks() - 1),
|
||||||
|
SDL_GameControllerName(SDL_GameControllerOpen(SDL_NumJoysticks() - 1))
|
||||||
|
};
|
||||||
|
printf("Controller added: %s\n", controllers[SDL_NumJoysticks() - 1].name.c_str());
|
||||||
|
break;
|
||||||
|
case SDL_QUIT:
|
||||||
exit = true;
|
exit = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
for (auto& controller : controllers)
|
||||||
|
{
|
||||||
|
controller.second.leftStickX = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_LEFTX);
|
||||||
|
controller.second.leftStickY = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_LEFTY);
|
||||||
|
controller.second.rightStickX = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_RIGHTX);
|
||||||
|
controller.second.rightStickY = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_RIGHTY);
|
||||||
|
controller.second.leftTrigger = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
|
||||||
|
controller.second.rightTrigger = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
|
||||||
|
}
|
||||||
|
SDL_GetMouseState(&mouse.x, &mouse.y);
|
||||||
|
mouse.wheel = event.wheel.y;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user