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

Function main

examples/keyboard/Keyboard.cpp:752–865  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

750///
751////////////////////////////////////////////////////////////
752int main()
753{
754 // Create the main window
755 sf::RenderWindow window(sf::VideoMode({1280, 720}), "Keyboard", sf::Style::Titlebar | sf::Style::Close);
756 window.setFramerateLimit(25);
757
758 // Load sound buffers
759 const sf::SoundBuffer errorSoundBuffer(resourcesDir() / "error_005.ogg");
760 const sf::SoundBuffer pressedSoundBuffer(resourcesDir() / "mouseclick1.ogg");
761 const sf::SoundBuffer releasedSoundBuffer(resourcesDir() / "mouserelease1.ogg");
762
763 // Create sound objects to play them upon keyboard events
764 sf::Sound errorSound(errorSoundBuffer);
765 sf::Sound pressedSound(pressedSoundBuffer);
766 sf::Sound releasedSound(releasedSoundBuffer);
767
768 // Open the font used for all texts
769 const sf::Font font(resourcesDir() / "Tuffy.ttf");
770
771 // Create object to display all scancodes descriptions, related events and real-time state
772 KeyboardView keyboardView(font);
773 keyboardView.setPosition({16, 16});
774
775 // Create text to display information about keyboard events and key codes real-time state
776 ShinyText keyPressedText(makeShinyText(font, "Key Pressed", {16, 575}));
777 ShinyText keyReleasedText(makeShinyText(font, "Key Released", {300, 575}));
778 ShinyText textEnteredText(makeShinyText(font, "Text Entered", {600, 575}));
779 sf::Text keyPressedCheckText(makeText(font, "", {900, 575}));
780
781 sf::Clock clock;
782 while (window.isOpen())
783 {
784 // Handle events
785 while (const std::optional event = window.pollEvent())
786 {
787 // Window closed: exit
788 if (event->is<sf::Event::Closed>())
789 {
790 window.close();
791 break;
792 }
793
794 // Window size changed: adjust view appropriately
795 if (const auto* resized = event->getIf<sf::Event::Resized>())
796 window.setView(sf::View(sf::FloatRect({}, sf::Vector2f(resized->size))));
797
798 // Key events: update text and play sound
799 if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>())
800 {
801 keyPressedText.setString(keyEventDescription("Key Pressed", *keyPressed));
802 if (somethingIsOdd(*keyPressed))
803 {
804 keyPressedText.shine(sf::Color::Red);
805 errorSound.play();
806 }
807 else
808 {
809 keyPressedText.shine(sf::Color::Green);

Callers

nothing calls this directly

Calls 15

makeShinyTextFunction · 0.85
makeTextFunction · 0.85
keyEventDescriptionFunction · 0.85
somethingIsOddFunction · 0.85
textEventDescriptionFunction · 0.85
keyIdentifierFunction · 0.85
setFramerateLimitMethod · 0.80
isOpenMethod · 0.80
setViewMethod · 0.80
shineMethod · 0.80
restartMethod · 0.80
resourcesDirFunction · 0.70

Tested by

no test coverage detected