controller and mouse support
This commit is contained in:
		@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user