1
Window / Re: Can't compile when using handleEvents method of the Window class
« on: February 19, 2025, 05:23:01 pm »
I don't think I can build a project with this feature! Even the example code in the repo can't be compiled without error (and it gives the same error but for more event handlers). Right now, I will stick to the poll method.
Thank you.
Thank you.
The handleEvents is still quite new to me as well. If I understood the documentation correctly, you should be able to pass the eventHandler instance directly, as you have operator() overloads for the different types, so you don't need to use lambdas that then call the handler.
Personally, I'd probably go with some event/input handler that just defines different functions for the different event types and then have the lambda call those functions.
Something like the following code, but that's really just a preference (also untested code).class InputHandler
{
public:
void handleKeyPress(sf::RenderWindow& window, const sf::Event::KeyPressed& event)
{
switch (event.code) {
case sf::Keyboard::Key::Escape:
window.close();
break;
case sf::Keyboard::Key::Space:
std::cout << "Space key pressed!" << std::endl;
break;
default:
std::cout << "Key pressed: " << static_cast<int>(event.code) << std::endl;
break;
}
}
};
int main() {
auto window = sf::RenderWindow{sf::VideoMode{{800, 600}}, "SFML Window"};
auto inputHandler = InputHandler{};
while (window.isOpen())
{
window.handleEvents(
// Window close handler
[&window](const sf::Event::Closed&)
{
window.close();
},
// Key press handler - using member function through lambda
[&window, &inputHandler](const sf::Event::KeyPressed& event)
{
inputHandler.handleKeyPress(window, event);
});
}
}