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

Author Topic: New here, and a simple question  (Read 3607 times)

0 Members and 1 Guest are viewing this topic.

Asimov

  • Newbie
  • *
  • Posts: 13
    • View Profile
New here, and a simple question
« on: July 25, 2024, 01:07:04 pm »
Hi all,

I have only been messing with SFML for about a day. I made a little bouncing ball thing, but for some reason the animation is choppy. It is like scissoring or going out of phase when it is moving. I am on windows 10, so I am not sure if it is a windows thing. The program is not very long and bare in mind I am still learning how SFML works. I am wondering if it is something to do with the screen buffering.
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(1280, 720), "Screensaver",sf::Style::Fullscreen);

    window.setFramerateLimit(60);
    sf::CircleShape shape(50.f);
    shape.setFillColor(sf::Color::Green);

    sf::Vector2f circlePosition(600,350);
    shape.setPosition(circlePosition);

    float xVelocity =5;
    float yVelocity =5;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close();
        }
        //Physics
        if (circlePosition.x < 0 || circlePosition.x > 1200 -30) xVelocity *= -1;
        if (circlePosition.y < 0 || circlePosition.y > 720 - 100) yVelocity *= -1;

        circlePosition.x += xVelocity;
        circlePosition.y += yVelocity;
        shape.setPosition(circlePosition);

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}
 

person999

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: New here, and a simple question
« Reply #1 on: July 25, 2024, 03:33:04 pm »
Actually its just because of framerate limit you set. You should really read the section "Controlling the framerate" of this documentation : https://www.sfml-dev.org/tutorials/2.6/window-window.php

However, I've made some improvements to the code. Maybe you should try it :
#include <SFML/Graphics.hpp>

int main()
{
    sf::VideoMode desktopMode = sf::VideoMode::getDesktopMode(); //Getting desktop mode of the screen (full screen size)

    sf::RenderWindow window(desktopMode, "Screensaver", sf::Style::Fullscreen);

    //setting some constants
    const float WIN_WIDTH = desktopMode.width;
    const float WIN_HEIGHT = desktopMode.height;

    sf::CircleShape shape(50.f);
    shape.setOrigin(50, 50);
    shape.setFillColor(sf::Color::Green);

    shape.setPosition(shape.getRadius(), WIN_HEIGHT / 2.f); //Ball starts from middle left of the screen

    /* To make movement independent of frame rate*/
    sf::Clock clock;
    float dt = 0;
    float multiplier = 60;

    float xVelocity = 5; //Ball will go forward in the start
    float yVelocity = -5; //Ball will go upward in the start

    sf::Event event; //Moved it out of game loop, because it was useless to move it in loop and creating it in each iteration

    while (window.isOpen())
    {  
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close();
           
        }

        /*Point to remember is that, shapes origin is at its center, so we have to keep this in mind*/
        if (shape.getPosition().x < shape.getOrigin().x || shape.getPosition().x >= (WIN_WIDTH - shape.getOrigin().x)) {
            xVelocity = -xVelocity; //Some basic logic
        }

        if (shape.getPosition().y < shape.getOrigin().x  || shape.getPosition().y >= (WIN_HEIGHT - shape.getOrigin().y)) {
            yVelocity = -yVelocity;
        }

        dt = clock.restart().asSeconds(); //restarting clock on each frame recording time elapsed in the dt variable

        //Using move rather than setPosition
        shape.move(xVelocity * dt * multiplier, yVelocity * dt * multiplier);

        //Drawing stuff
        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

Asimov

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: New here, and a simple question
« Reply #2 on: July 29, 2024, 01:24:28 pm »
Hi Person999,

Removing my framerate line and adding  window.setVerticalSyncEnabled(true);
Solved my tearing problem, after reading the documentation.

I should probably investigate using deltatime at some point too..

I will have a look at the code you posted and see how it differs from mine, after I have had some food.

Asimov

Asimov

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: New here, and a simple question
« Reply #3 on: August 04, 2024, 08:42:57 pm »
Hi person999,

Heh heh just looked at your code. Strangely before I looked back at it I implimented deltaTime. I like the way you get the screen dimensions and stuff.

Going to go through your code slowly and see if I can improve my simple code.

Asimov

 

anything