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

Method run

examples/event_handling/EventHandling.cpp:119–320  ·  view source on GitHub ↗

///////////////////////////////////////////////////////

Source from the content-addressed store, hash-verified

117
118 ////////////////////////////////////////////////////////////
119 void run()
120 {
121 // This example is for demonstration purposes only
122 // All the following forms of event handling have equivalent behavior
123 // In your own code you should decide which form of event handling
124 // suits your needs best and use a single form of event handling
125 while (m_window.isOpen())
126 {
127 if (m_handlerType == HandlerType::Classic)
128 {
129 // The "classical form" of event handling
130 // Poll/Wait for events in a loop and handle them
131 // individually based on their concrete type
132 while (const std::optional event = m_window.pollEvent())
133 {
134 if (event->is<sf::Event::Closed>())
135 {
136 m_window.close();
137 break;
138 }
139
140 if (const auto* keyPress = event->getIf<sf::Event::KeyPressed>())
141 {
142 m_log.emplace_back(
143 "Key Pressed: " + sf::Keyboard::getDescription(keyPress->scancode).toAnsiString());
144
145 // When the enter key is pressed, switch to the next handler type
146 if (keyPress->code == sf::Keyboard::Key::Enter)
147 {
148 m_handlerType = HandlerType::Visitor;
149 m_handlerText.setString("Current Handler: Visitor");
150 }
151 }
152 else if (const auto* keyRelease = event->getIf<sf::Event::KeyReleased>())
153 {
154 m_log.emplace_back(
155 "Key Released: " + sf::Keyboard::getDescription(keyRelease->scancode).toAnsiString());
156 }
157 else if (const auto* mouseMoved = event->getIf<sf::Event::MouseMoved>())
158 {
159 m_log.emplace_back("Mouse Moved: " + vec2ToString(mouseMoved->position));
160 }
161 else if (event->is<sf::Event::MouseButtonPressed>())
162 {
163 m_log.emplace_back("Mouse Pressed");
164 }
165 else if (const auto* touchBegan = event->getIf<sf::Event::TouchBegan>())
166 {
167 m_log.emplace_back("Touch Began: " + vec2ToString(touchBegan->position));
168 }
169 else if (const auto* touchEnded = event->getIf<sf::Event::TouchEnded>())
170 {
171 m_log.emplace_back("Touch Ended: " + vec2ToString(touchEnded->position));
172 }
173 else if (const auto* touchMoved = event->getIf<sf::Event::TouchMoved>())
174 {
175 m_log.emplace_back("Touch Moved: " + vec2ToString(touchMoved->position));
176 }

Callers 1

mainFunction · 0.45

Calls 14

vec2ToStringFunction · 0.85
VisitorClass · 0.85
isOpenMethod · 0.80
toAnsiStringMethod · 0.80
eraseMethod · 0.80
getDescriptionFunction · 0.50
pollEventMethod · 0.45
closeMethod · 0.45
setStringMethod · 0.45
beginMethod · 0.45
clearMethod · 0.45
setPositionMethod · 0.45

Tested by

no test coverage detected