controller and mouse support
This commit is contained in:
parent
9e7ba471ae
commit
fa76489449
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
.vscode
|
||||
build
|
||||
build
|
||||
main.x86_64
|
@ -2,6 +2,27 @@
|
||||
#include <map>
|
||||
#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
|
||||
{
|
||||
private:
|
||||
@ -9,6 +30,10 @@ class Input
|
||||
|
||||
public:
|
||||
bool exit;
|
||||
std::map<int, Controller> controllers;
|
||||
std::map<SDL_Keycode, bool> activeKeys;
|
||||
Mouse mouse;
|
||||
|
||||
Input();
|
||||
void update();
|
||||
};
|
@ -1,9 +1,14 @@
|
||||
// prevent multiple inclusion
|
||||
#pragma once
|
||||
|
||||
// class for 2D float vector
|
||||
class float_vec2
|
||||
{
|
||||
// public members
|
||||
public:
|
||||
// x and y coordinates
|
||||
float x, y;
|
||||
// operator functions
|
||||
float_vec2 operator+(float_vec2 vec)
|
||||
{
|
||||
return { x + vec.x, y + vec.y };
|
||||
@ -26,10 +31,14 @@ class float_vec2
|
||||
}
|
||||
};
|
||||
|
||||
// class for 2D integer vector
|
||||
class int_vec2
|
||||
{
|
||||
// public members
|
||||
public:
|
||||
// x and y coordinates
|
||||
int x, y;
|
||||
// operator functions
|
||||
int_vec2 operator+(int_vec2 vec)
|
||||
{
|
||||
return { x + vec.x, y + vec.y };
|
||||
|
@ -77,29 +77,33 @@ int main(int argc, char* argv[])
|
||||
if (engine->input.exit || engine->input.activeKeys[SDLK_ESCAPE])
|
||||
{
|
||||
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");
|
||||
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");
|
||||
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");
|
||||
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");
|
||||
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");
|
||||
}
|
||||
@ -113,7 +117,7 @@ int main(int argc, char* argv[])
|
||||
// Draw sprite
|
||||
animations->draw(engine->renderer);
|
||||
|
||||
// Update screen
|
||||
// Update screen
|
||||
engine->render();
|
||||
}
|
||||
return 0;
|
||||
|
@ -1,20 +1,56 @@
|
||||
#include "input.h"
|
||||
|
||||
Input::Input()
|
||||
{
|
||||
SDL_Init(SDL_INIT_JOYSTICK);
|
||||
exit = false;
|
||||
}
|
||||
|
||||
void Input::update()
|
||||
{
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
if (event.type == SDL_KEYDOWN)
|
||||
{
|
||||
activeKeys[event.key.keysym.sym] = true;
|
||||
}
|
||||
else if (event.type == SDL_KEYUP)
|
||||
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;
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
activeKeys.erase(event.key.keysym.sym);
|
||||
}
|
||||
else if (event.type == SDL_QUIT)
|
||||
{
|
||||
break;
|
||||
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;
|
||||
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