MCPcopy Create free account
hub / github.com/SFML/SFML / main

Function main

examples/joystick/Joystick.cpp:179–308  ·  view source on GitHub ↗

/////////////////////////////////////////////////////// Entry point of application \return Application exit code ///////////////////////////////////////////////////////

Source from the content-addressed store, hash-verified

177///
178////////////////////////////////////////////////////////////
179int 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 {

Callers

nothing calls this directly

Calls 15

getStreamFunction · 0.85
setCharacterSizeMethod · 0.80
updateValuesMethod · 0.80
isPresentMethod · 0.80
isOpenMethod · 0.80
setViewMethod · 0.80
clearValuesMethod · 0.80
isEmptyMethod · 0.80
VideoModeClass · 0.50
isKeyPressedFunction · 0.50
setPositionMethod · 0.45

Tested by

no test coverage detected