Skip to content

Commit

Permalink
application/Application: some more gamepad, works for gamepad now and…
Browse files Browse the repository at this point in the history
… GUIS, unfortunatly i dont have a joystick, lets do this later
  • Loading branch information
andreasdr committed Nov 5, 2023
1 parent 3245b94 commit d9a2980
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
60 changes: 49 additions & 11 deletions src/tdme/application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ int Application::run(int argc, char** argv, const string& title, InputEventHandl
glfwSetKeyCallback(glfwWindow, Application::glfwOnKey);
glfwSetCursorPosCallback(glfwWindow, Application::glfwOnMouseMoved);
glfwSetMouseButtonCallback(glfwWindow, Application::glfwOnMouseButton);
glfwSetJoystickCallback(Application::glfwOnJoystickConnect);
glfwSetScrollCallback(glfwWindow, Application::glfwOnMouseWheel);
glfwSetWindowSizeCallback(glfwWindow, Application::glfwOnWindowResize);
glfwSetWindowCloseCallback(glfwWindow, Application::glfwOnClose);
Expand All @@ -709,14 +710,20 @@ int Application::run(int argc, char** argv, const string& title, InputEventHandl
Console::println("An error occurred: " + string(exception.what()));
}

//
for (auto joystickIdx = GLFW_JOYSTICK_1; joystickIdx <= GLFW_JOYSTICK_16; joystickIdx++) {
if (glfwJoystickPresent(joystickIdx) == true) glfwOnJoystickConnect(joystickIdx, GLFW_CONNECTED);
}

//
while (glfwWindowShouldClose(glfwWindow) == false) {
displayInternal();
if (Engine::getRenderer()->getRendererType() != Renderer::RENDERERTYPE_VULKAN) glfwSwapBuffers(glfwWindow);
//
glfwPollEvents();
//
updateJoystickInput();
for (const auto gamepadIdx: connectedGamepads) updateGamepadInput(gamepadIdx);
for (const auto joystickIdx: connectedJoysticks) updateJoystickInput(joystickIdx);
}
glfwTerminate();
//
Expand Down Expand Up @@ -875,25 +882,56 @@ void Application::glfwOnDrop(GLFWwindow* window, int count, const char** paths)
Application::application->onDrop(pathsVector);
}

void Application::updateJoystickInput() {
void Application::glfwOnJoystickConnect(int joystickIdx, int event) {
if (Application::application == nullptr) return;
//
if (event == GLFW_CONNECTED) {
if (glfwJoystickIsGamepad(joystickIdx) == true) {
Console::println("Application::glfwOnJoystickConnect(): connected gamepad with idx = " + to_string(joystickIdx) + ", name = " + glfwGetJoystickName(joystickIdx));
Application::application->connectedGamepads.insert(joystickIdx);
} else {
Console::println("Application::glfwOnJoystickConnect(): connected joystick with idx = " + to_string(joystickIdx) + ", name = " + glfwGetJoystickName(joystickIdx));
Application::application->connectedJoysticks.insert(joystickIdx);
}
} else if (event == GLFW_DISCONNECTED) {
Console::println("Application::glfwOnJoystickConnect(): disconnected joystick/gamepad with idx = " + to_string(joystickIdx));
Application::application->connectedGamepads.erase(joystickIdx);
Application::application->connectedJoysticks.erase(joystickIdx);
}
}

void Application::updateJoystickInput(int joystickIdx) {
if (Application::inputEventHandler == nullptr) return;
// no joystick support yet
}

void Application::updateGamepadInput(int joystickIdx) {
if (Application::inputEventHandler == nullptr) return;
//
double mouseX, mouseY;
glfwGetCursorPos(glfwWindow, &mouseX, &mouseY);
// TODO: finish me!!!
// Console::println(glfwGetJoystickName(GLFW_JOYSTICK_1));
GLFWgamepadstate gamepadState;
//
auto handleButton = [&](int button, int keyCode, int modifier = KEYBOARD_MODIFIER_NONE) -> void {
if (gamepadState.buttons[button] == GLFW_PRESS &&
this->gamepadState.buttons[button] != GLFW_PRESS) {
auto now = Time::getCurrentMillis();
auto handleButton = [&](int buttonIdx, int keyCode, int modifier = KEYBOARD_MODIFIER_NONE) -> void {
// press
if (gamepadState.buttons[buttonIdx] == GLFW_PRESS &&
joystickButtons[joystickIdx][buttonIdx] == -1LL) {
Application::inputEventHandler->onKeyDown(-1, keyCode, static_cast<int>(mouseX), static_cast<int>(mouseY), false, modifier);
this->gamepadState.buttons[button] = GLFW_PRESS;
joystickButtons[joystickIdx][buttonIdx] = now;
} else
if (gamepadState.buttons[button] == GLFW_RELEASE &&
this->gamepadState.buttons[button] == GLFW_PRESS) {
// release
if (gamepadState.buttons[buttonIdx] == GLFW_RELEASE &&
joystickButtons[joystickIdx][buttonIdx] != -1LL) {
Application::inputEventHandler->onKeyUp(-1, keyCode, static_cast<int>(mouseX), static_cast<int>(mouseY));
this->gamepadState.buttons[button] = GLFW_RELEASE;
joystickButtons[joystickIdx][buttonIdx] = -1LL;
} else
// repeat
if (gamepadState.buttons[buttonIdx] == GLFW_PRESS &&
joystickButtons[joystickIdx][buttonIdx] != 0LL &&
now - joystickButtons[joystickIdx][buttonIdx] >= JOYSTICK_BUTTON_TIME_REPEAT) {
Application::inputEventHandler->onKeyDown(-1, keyCode, static_cast<int>(mouseX), static_cast<int>(mouseY), true, modifier);
joystickButtons[joystickIdx][buttonIdx] = now;
}
};
//
Expand Down
25 changes: 22 additions & 3 deletions src/tdme/application/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <array>
#include <cstdlib>
#include <string>
#include <unordered_set>
#include <vector>

#include <tdme/tdme.h>
Expand All @@ -26,6 +27,7 @@

using std::array;
using std::string;
using std::unordered_set;
using std::vector;

using tdme::application::InputEventHandler;
Expand Down Expand Up @@ -55,6 +57,7 @@ class tdme::application::Application
static constexpr int EXITCODE_SUCCESS { EXIT_SUCCESS };
static constexpr int EXITCODE_FAILURE { EXIT_FAILURE };

static constexpr int64_t JOYSTICK_BUTTON_TIME_REPEAT { 150LL };
/**
* @return renderer
*/
Expand Down Expand Up @@ -362,17 +365,26 @@ class tdme::application::Application

STATIC_DLL_IMPEXT static int mouseCursor;

GLFWgamepadstate gamepadState;
unordered_set<int> connectedJoysticks;
unordered_set<int> connectedGamepads;
array<array<int64_t, 16>, 16> joystickButtons;

/**
* Set application icon
*/
void setIcon();

/**
* Update joystick support
* Update joystick input for given joystick index
* @param joystickIdx joystick index
*/
void updateJoystickInput();
void updateJoystickInput(int joystickIdx);

/**
* Update gamepad input for given gamepad index
* @param gamepadIdx gamepad index
*/
void updateGamepadInput(int gamepadIdx);

/**
* Display function
Expand Down Expand Up @@ -449,4 +461,11 @@ class tdme::application::Application
*/
static void glfwOnDrop(GLFWwindow* window, int count, const char** paths);

/**
* GLFW on joystick connect/disconnect
* @param joystickIdx joystick index
* @param event event
*/
static void glfwOnJoystickConnect(int joystickIdx, int event);

};

0 comments on commit d9a2980

Please sign in to comment.