I'm trying to make settings state for my game and my mouse scroll detection is not working. I tried to find solution on sfml website, youtube and forums but my code still doesn't want to work
Here's the function I made to move a view:
void Options::CheckEvents()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) // quit check
this->quit = true;
while(window->pollEvent(this->evnt))
{
if (this->evnt.type == sf::Event::MouseWheelScrolled)
{
std::cout << "[DEBUG]: mouse wheel scrolled\n"; // this one prints, but not always
// (for example 4 prints per 10s of scrolling)
if (evnt.mouseWheelScroll.delta < 0 && view->getCenter().y > WinHeight / 2) // if view center is not too far away
{
std::cout << "[DEBUG]: View moved up\n"; // not printing, even when I remove this ^ check
view->move(sf::Vector2f(0, -view_speed * this->dt));
}
if (evnt.mouseWheelScroll.delta > 0 && view->getCenter().y < WinHeight * 2) // if view center is not too far away
{
std::cout << "[DEBUG]: View moved down\n"; // not printing, even when I remove this ^ check
view->move(sf::Vector2f(0, view_speed * this->dt));
}
}
}
}
Additionally when I move the code out of pollEvent function the view moves only when I move my mouse
OK, I solved this by getting sf::Event evnt from the main program class, not using the local one
void Options::CheckEvents(sf::Event &evnt) // evnt is not from 'this' class, but from the main class that controls the whole program
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) // quit check
{
this->quit = true;
}
if (evnt.type == sf::Event::MouseWheelScrolled)
{
if (evnt.mouseWheelScroll.delta > 0) // moving up
{
view->move(sf::Vector2f(0, -this->view_speed * this->dt)); // dt is delta time
}
else if (evnt.mouseWheelScroll.delta < 0) // moving down
{
view->move(sf::Vector2f(0, this->view_speed * this->dt)); // dt is delta time
}
}
}