Skip to content

Commit

Permalink
feat: added controller support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaskowicz1 committed May 13, 2024
1 parent 546a7a6 commit 788a695
Show file tree
Hide file tree
Showing 4 changed files with 410 additions and 90 deletions.
79 changes: 79 additions & 0 deletions include/engine/utilities/input_manager.h
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
#pragma once

#include <functional>
#include "GLFW/glfw3.h"

namespace input {

enum controller_inputs {
INPUT_UP = 0,
INPUT_DOWN = 1,
INPUT_LEFT = 2,
INPUT_RIGHT = 3,
CONTROLLER_BUTTON_UP = 4,
CONTROLLER_BUTTON_DOWN = 5,
CONTROLLER_BUTTON_LEFT = 6,
CONTROLLER_BUTTON_RIGHT = 7,
CONTROLLER_BUTTON_OPTION = 8,
CONTROLLER_BUTTON_START = 9,
CONTROLLER_BUTTON_LEFT_JOYSTICK = 10,
CONTROLLER_BUTTON_RIGHT_JOYSTICK = 11,
CONTROLLER_BUTTON_LEFT_BUMPER = 12,
CONTROLLER_BUTTON_LB = 12,
CONTROLLER_BUTTON_RIGHT_BUMPER = 13,
CONTROLLER_BUTTON_RB = 13,
CONTROLLER_BUTTON_DPAD_UP = 14,
CONTROLLER_BUTTON_DPAD_DOWN = 15,
CONTROLLER_BUTTON_DPAD_LEFT = 16,
CONTROLLER_BUTTON_DPAD_RIGHT = 17,
};

enum input_types {
KeyboardAndMouse = 0,
Controller = 1,
};

class input_manager {

public:
/**
* @brief input_manager constructor.
* @param window The window reference to setup the input_manager.
*/
input_manager(GLFWwindow* window);

~input_manager();

void input_loop();

/**
* @brief Keyboard press event
*
* @param key The key that was pressed (look at `GLFW_KEY_`).
*
*/
std::function<void(int key)> on_keyboard_press{};

/**
* @brief Controller input event
*
* @param input The input of the controller as `controller_inputs`.
*/
std::function<void(controller_inputs input)> on_controller_input{};

/**
* @brief Controller button press event
*
* @param key The controller button as `controller_inputs`.
*/
std::function<void(controller_inputs key)> on_controller_button_press{};

std::vector<int> keys_held{};

input_types current_input_type{};

private:

float deadzone = 0.2;
};

}
7 changes: 5 additions & 2 deletions include/engine/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>
#include "video_manager.h"
#include "glm/vec2.hpp"
#include "utilities/input_manager.h"
#include <glad/gl.h>

class window {
Expand All @@ -18,17 +19,19 @@ class window {

video_manager manager{};

std::unique_ptr<input::input_manager> input_man{nullptr};

private:

struct GLFWwindow* glfw_window{nullptr};

uint16_t frame_width{0}; // vid_reader.height
uint16_t frame_height{0}; // vid_reader.height

bool testing_export{false};

GLuint video_texture;

glm::vec2 vertices[4] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}};

void render_window_bar();

};
131 changes: 131 additions & 0 deletions src/engine/utilities/input_manager.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,132 @@
#include <iostream>
#include "utilities/input_manager.h"

input::input_manager::input_manager(GLFWwindow* window) {
if(!window) {
return;
}

std::cout << "Setting up input manager..." << "\n";

static input_manager *mgr = this;

glfwSetKeyCallback(window, [](GLFWwindow *window, int key, int scancode, int action, int mods) {
mgr->current_input_type = input_types::KeyboardAndMouse;
if(action == GLFW_PRESS) {
mgr->keys_held.emplace_back(key);
mgr->on_keyboard_press(key);
}
});

std::cout << "Input manager has setup successfully!" << "\n";
}

input::input_manager::~input_manager() {
std::cout << "Input manager killed." << "\n";
}

void input::input_manager::input_loop() {
if(glfwJoystickPresent(GLFW_JOYSTICK_1) == GLFW_TRUE) {
int count;
const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &count);

const float x_axis = axes[0];
const float y_axis = axes[1];

if(on_controller_input) {
if(abs(x_axis) >= deadzone) {
if(x_axis < 0) {
on_controller_input(controller_inputs::INPUT_LEFT);
} else {
on_controller_input(controller_inputs::INPUT_RIGHT);
}

current_input_type = input_types::Controller;
}

if(abs(y_axis) >= deadzone) {
if(y_axis < 0) {
on_controller_input(controller_inputs::INPUT_UP);
} else {
on_controller_input(controller_inputs::INPUT_DOWN);
}

current_input_type = input_types::Controller;
}
}

const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &count);

if(on_controller_button_press) {
if(buttons[0] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_DOWN);
current_input_type = input_types::Controller;
}

if(buttons[1] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_RIGHT);
current_input_type = input_types::Controller;
}

if(buttons[2] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_LEFT);
current_input_type = input_types::Controller;
}

if(buttons[3] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_UP);
current_input_type = input_types::Controller;
}

if(buttons[4] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_LEFT_BUMPER);
current_input_type = input_types::Controller;
}

if(buttons[5] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_RIGHT_BUMPER);
current_input_type = input_types::Controller;
}

if(buttons[6] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_OPTION);
current_input_type = input_types::Controller;
}

if(buttons[7] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_START);
current_input_type = input_types::Controller;
}

if(buttons[8] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_LEFT_JOYSTICK);
current_input_type = input_types::Controller;
}

if(buttons[9] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_RIGHT_JOYSTICK);
current_input_type = input_types::Controller;
}

if(buttons[10] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_DPAD_UP);
current_input_type = input_types::Controller;
}

if(buttons[11] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_DPAD_RIGHT);
current_input_type = input_types::Controller;
}

if(buttons[12] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_DPAD_DOWN);
current_input_type = input_types::Controller;
}

if(buttons[13] == GLFW_PRESS) {
on_controller_button_press(controller_inputs::CONTROLLER_BUTTON_DPAD_LEFT);
current_input_type = input_types::Controller;
}
}
}
}
Loading

0 comments on commit 788a695

Please sign in to comment.