1
\$\begingroup\$

I have been trying to make a simple platformer game with SFML. Here is my main method where draw and update functions call the draw and update functions of all my entities which in turn update the variables and draw updates the position of the sprite which is to be rendered.

int main() {
sf::RenderWindow window(sf::VideoMode(0, 0), "My First SFML Game", sf::Style::Fullscreen);
int frames = 0;


if(!init()) {
    std::cout << "Could not initialize\n";
    return 1;
}

sf::Clock clock, frameClock;

while(window.isOpen()) {
    sf::Event event;
    while(window.pollEvent(event)) {
        switch(event.type) {
        case sf::Event::Closed:
            window.close();
            break;
        case sf::Event::KeyPressed:
            if(event.key.code == sf::Keyboard::Escape)
                window.close();
            break;
        }
    }

    //keyboard handler
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
        if(player->velX > -7) player->velX--;
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
        if(player->velX < 7) player->velX++;
    }

    if(frameClock.getElapsedTime().asSeconds() >= 1/FPS) {
        update();
        draw(window);
        window.display();
        window.clear();

        frames++;
        frameClock.restart();
    }

    if(clock.getElapsedTime().asSeconds() >= 1) {
        std::cout << frames << " frames per second\n";
        clock.restart();
        frames = 0;
    }
}
}

There is a strange flicker when I press left or right arrow keys (actually, almost every second). However, it did not happen when I repeatedly pressed prt sc. It also does not happen when with that much intensity (but it still happens a little bit) when I switch out of the full screen mode.

\$\endgroup\$
1
\$\begingroup\$

I enabled vertical sync by window.setVerticalSyncEnabled. It gives a low frame rate when you are not in full screen mode. The frame rate returns to normal in the full screen mode and it does not flicker so yay!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.