Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: limited framerate issue  (Read 909 times)

0 Members and 1 Guest are viewing this topic.

lilbigwill99

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
limited framerate issue
« on: November 18, 2016, 08:45:58 am »
I'm not even sure how to word my problem, but the problem is that I was told that by limiting the framerate, the program would be able to run on any computer the same, just maybe a bit laggy but still the same. the way the youtube video explained it, he had a sprite move a certain distance multiplied by the elapsed time between the last frame. all of that made sense. But now I'm having a problem trying to implement that same idea into events. The problem for me is that sometimes the paddle skips when going up, and I really don't know why. I have tried this several times with almost the same results.  Is there a simple fix for this? I'm using W for up, which is using the limited framerate to move, where as S is down, and it moves without a limited framerate. Here's my code:



#include <iostream>
#include <sfml/graphics.hpp>

int main()
{

    sf::RenderWindow window(sf::VideoMode(480, 480), "Pong");
    sf::Event event;

    sf::RectangleShape Paddle1(sf::Vector2f(40, 100));
    Paddle1.setOrigin(20, 50);
    Paddle1.setPosition(50, 240);
    Paddle1.setFillColor(sf::Color::Red);
    Paddle1.setOutlineThickness(-1);
    Paddle1.setOutlineColor(sf::Color::Black);

    window.setFramerateLimit(60);





    while(window.isOpen())
    {

        sf::Clock clock;
        sf::Time time = clock.getElapsedTime();

        while(window.pollEvent(event))
        {

            if(event.type == sf::Event::Closed)
            {

                window.close();
            }




            if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
            {
                Paddle1.move(0, -0.5 * time.asMicroseconds());

                std::cout << Paddle1.getPosition().y << std::endl;
            }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            {
                Paddle1.move(0, 10);
                std::cout << Paddle1.getPosition().y << std::endl;
            }




            clock.restart().asMicroseconds();


        }//ends polling




        window.clear(sf::Color::White);

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


        window.draw(Paddle1);


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

        window.display();

    }//ends game loop



    return 0;
}









 







« Last Edit: November 18, 2016, 08:47:33 am by lilbigwill99 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10836
    • View Profile
    • development blog
    • Email
limited framerate issue
« Reply #1 on: November 18, 2016, 08:50:38 am »
You should put thr clock outside of the game loop and call restart() to get the time. Then you should read the official tutorial on event handling. Don't mix events and sf::Keyboard.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lilbigwill99

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: limited framerate issue
« Reply #2 on: November 18, 2016, 04:37:02 pm »
Thank you for the easy fix haha. I will check out the official tutorial because YouTube only helps so much :)

 

anything