void JoystickControl::update() { bool isJoyConnected = joystick.update(); // Handle connect event. if (!connected && isJoyConnected) { connected = true; emit message("Joystick connected."); emit joystickConnected(); } // Handle disconnect event. else if (connected && !isJoyConnected) { connected = false; emit message("Joystick disconnected."); emit joystickDisconnected(); } // Do the usual joystick business when connected. if (connected) { QHash<QString, double> txJointVelocity; QHash<QString, double> txJointAngles; QHash<QString, double> txJoystick; // The motion signal is generated using the "minimal carrot" algorithm. // You know how you hold a carrot in front of a donkey so that it starts // moving? The distance of the carrot should be as small as possible, so // that the donkey stops at the carrot if the connection breaks and the // carrot could not be updated. But the carrot distance should be large // enough so that the donkey cannot reach it in one iteration. double joystickThreshold = 0.25; double carrot = speedLimit * 1.0/JOYSTICKRATE + 0.25; bool somethingIsNonZero = false; foreach(const JointInfo& joint, *m_jointConfig) { int axis = joint.joystick_axis; if(axis < 0) continue; if(axis >= joystick.numOfAxes) continue; int js_value = joystick.axis[axis]; if(joint.joystick_invert) js_value = -js_value; if(qAbs(js_value) > joystickThreshold) { txJointAngles[joint.name] = rxJointAngles[joint.name] + js_value * carrot; txJointVelocity[joint.name] = qAbs(js_value) * speedLimit; txJoystick[joint.name] = js_value; somethingIsNonZero = true; } } if (somethingIsNonZero) { emit motionOut(txJointAngles, txJointVelocity); emit joystickOut(txJoystick); } for (int i = 0; i < Joystick::numOfButtons; i++) { if (joystick.buttonPressed[i]) { emit buttonPressed(joystick.button); break; } } } return; }
void InputDriver::update() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_JOYDEVICEADDED: { auto joystick = SDL_JoystickOpen(event.jdevice.which); auto guid = SDL_JoystickGetGUID(joystick); auto instanceId = SDL_JoystickInstanceID(joystick); { std::unique_lock lock { mConfigurationMutex }; auto connected = ConnectedJoystick { }; connected.joystick = joystick; connected.guid = guid; connected.instanceId = instanceId; connected.duplicateId = 0; for (auto &others : mJoysticks) { if (others.guid == connected.guid) { connected.duplicateId = std::max(connected.duplicateId, others.duplicateId + 1); } } for (auto &controller : mConfiguration.controllers) { for (auto &input : controller.inputs) { if (input.joystickGuid == guid && input.joystickDuplicateId == connected.duplicateId) { input.joystick = joystick; input.joystickInstanceId = instanceId; } } } mJoysticks.push_back(connected); } emit joystickConnected(instanceId, guid, SDL_JoystickName(joystick)); break; } case SDL_JOYDEVICEREMOVED: { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickDisconnected(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick)); { std::unique_lock lock { mConfigurationMutex }; for (auto itr = mJoysticks.begin(); itr != mJoysticks.end(); ++itr) { if (itr->joystick == joystick) { itr = mJoysticks.erase(itr); } } for (auto &controller : mConfiguration.controllers) { for (auto &input : controller.inputs) { if (input.joystick == joystick) { input.joystick = nullptr; } } } } SDL_JoystickClose(joystick); } break; } case SDL_JOYBUTTONDOWN: { if (mButtonEventsEnabled) { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickButtonDown(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick), event.jbutton.button); } } break; } case SDL_JOYAXISMOTION: { if (mButtonEventsEnabled) { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickAxisMotion(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick), event.jaxis.axis, translateAxisValue(event.jaxis.value)); } } break; } case SDL_JOYHATMOTION: { if (mButtonEventsEnabled) { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickHatMotion(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick), event.jhat.hat, event.jhat.value); } } break; } // I don't think we actually care about game controllers? case SDL_CONTROLLERDEVICEADDED: qDebug("SDL_CONTROLLERDEVICEADDED"); break; case SDL_CONTROLLERDEVICEREMAPPED: qDebug("SDL_CONTROLLERDEVICEREMAPPED"); break; case SDL_CONTROLLERDEVICEREMOVED: qDebug("SDL_CONTROLLERDEVICEREMOVED"); break; case SDL_CONTROLLERBUTTONDOWN: qDebug("SDL_CONTROLLERBUTTONDOWN"); break; } } QTimer::singleShot(10, Qt::PreciseTimer, this, SLOT(update())); }
void Game::run() { window.setFramerateLimit(160); camera.reset(sf::FloatRect(0, 0, 1920, 1080)); load(); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { switch (event.type) { case sf::Event::Closed: window.close(); break; case sf::Event::MouseButtonPressed: mousePressed(event.mouseButton.button, sf::Vector2i(event.mouseButton.x, event.mouseButton.y)); break; case sf::Event::MouseButtonReleased: mouseReleased(event.mouseButton.button, sf::Vector2i(event.mouseButton.x, event.mouseButton.y)); break; case sf::Event::MouseMoved: mouseMoved(event.mouseMove.x, event.mouseMove.y); case sf::Event::KeyPressed: keyPressed(event.key.code); break; case sf::Event::KeyReleased: keyReleased(event.key.code); break; case sf::Event::JoystickConnected: joystickConnected(event.joystickConnect.joystickId); break; case sf::Event::JoystickDisconnected: joystickDisconnected(event.joystickConnect.joystickId); break; default: break; } } sf::Time deltaTime = time.restart(); update(deltaTime.asSeconds()); if (window.getSize().x / 1920.f <= window.getSize().y / 1080.f) { float yScale = (window.getSize().x * (9.f / 16.f)) / window.getSize().y; camera.setViewport(sf::FloatRect(0.f, (1.f - yScale) / 2, 1.f, yScale)); } else { float xScale = (window.getSize().y * (16.f / 9.f)) / window.getSize().x; camera.setViewport(sf::FloatRect((1.f - xScale) / 2, 0.f, xScale, 1.f)); } window.clear(); window.setView(camera); draw(&window); window.setView(window.getDefaultView()); drawUnscaled(&window); window.display(); } onExit(); }