/////////////////////////////////////////////////////// Entry point of application \return Application exit code ///////////////////////////////////////////////////////
| 177 | /// |
| 178 | //////////////////////////////////////////////////////////// |
| 179 | int main() |
| 180 | { |
| 181 | // Create the window of the application |
| 182 | sf::RenderWindow window(sf::VideoMode({400, 775}), "Joystick", sf::Style::Close); |
| 183 | window.setVerticalSyncEnabled(true); |
| 184 | |
| 185 | // Open the text font |
| 186 | const sf::Font font("resources/tuffy.ttf"); |
| 187 | |
| 188 | float threshold = 0.1f; |
| 189 | JoystickObject thresholdDisplay{{font, "Threshold:"}, {font, ""}}; |
| 190 | thresholdDisplay.label.setPosition({5.f, 5.f}); |
| 191 | thresholdDisplay.value.setPosition({80.f, 5.f}); |
| 192 | thresholdDisplay.label.setCharacterSize(14); |
| 193 | thresholdDisplay.value.setCharacterSize(14); |
| 194 | |
| 195 | // Update initially displayed joystick values if a joystick is already connected on startup |
| 196 | unsigned int joysticksPresent = 0; |
| 197 | for (unsigned int i = 0; i < sf::Joystick::Count; ++i) |
| 198 | { |
| 199 | joysticks[i] = JoystickDisplay{i, font}; |
| 200 | joysticks[i].updateValues(); |
| 201 | if (joysticks[i].isPresent()) |
| 202 | ++joysticksPresent; |
| 203 | } |
| 204 | window.setSize({400 * std::max(joysticksPresent, 1u), 775}); |
| 205 | |
| 206 | while (window.isOpen()) |
| 207 | { |
| 208 | // Handle events |
| 209 | while (const std::optional event = window.pollEvent()) |
| 210 | { |
| 211 | // Window closed or escape key pressed: exit |
| 212 | if (event->is<sf::Event::Closed>() || |
| 213 | (event->is<sf::Event::KeyPressed>() && |
| 214 | event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Escape)) |
| 215 | { |
| 216 | window.close(); |
| 217 | break; |
| 218 | } |
| 219 | if (const auto* resized = event->getIf<sf::Event::Resized>()) |
| 220 | { |
| 221 | window.setView(sf::View(sf::FloatRect({}, sf::Vector2f(resized->size)))); |
| 222 | } |
| 223 | else if (const auto* joystickButtonPressed = event->getIf<sf::Event::JoystickButtonPressed>()) |
| 224 | { |
| 225 | joysticks[joystickButtonPressed->joystickId].updateValues(); |
| 226 | } |
| 227 | else if (const auto* joystickButtonReleased = event->getIf<sf::Event::JoystickButtonReleased>()) |
| 228 | { |
| 229 | joysticks[joystickButtonReleased->joystickId].updateValues(); |
| 230 | } |
| 231 | else if (const auto* joystickMoved = event->getIf<sf::Event::JoystickMoved>()) |
| 232 | { |
| 233 | joysticks[joystickMoved->joystickId].updateValues(); |
| 234 | } |
| 235 | else if (const auto* joystickConnected = event->getIf<sf::Event::JoystickConnected>()) |
| 236 | { |
nothing calls this directly
no test coverage detected