0
votes
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;
using namespace sf;

int main()
{

    RenderWindow window(VideoMode(800, 600), "Lolyan", Style::Default);

    Event event;

    CircleShape circle(30.0f, 30.0f);
    circle.setFillColor(Color::Blue);
    circle.setPosition(40, 530);

    window.setFramerateLimit(200);

    window.draw(circle);
    window.display();

    int velocityX = 0, velocityY = 0;
    Vector2i mousePosition;

    while (window.isOpen()) {


        circle.move(velocityX / 2, velocityY / 2);


        window.draw(circle);


        window.display();


        while (window.pollEvent(event)) {

            window.clear(Color::Black);



            if (event.type == Event::Closed) {
                window.close();
            }


            else if (event.type == Event::KeyPressed ) {

                cout << velocityX << endl;

                switch (event.key.code) {

                case Keyboard::A:
                    velocityX = -5;
                    break;

                case Keyboard::D:
                    velocityX = 5;
                    break;

                case Keyboard::S:
                    velocityY = 5;
                    break;

                case Keyboard::W:
                    velocityY = -5;
                    break;
                }

            }


            else if (event.type == Event::MouseButtonPressed) {
                circle.setFillColor(Color::Red);
            }


            else if (event.type == Event::MouseButtonReleased) {
                circle.setFillColor(Color::Blue);

            }

            else if (event.type == Event::MouseMoved) {
                mousePosition = Mouse::getPosition(window);
                circle.setPosition(mousePosition.x - 30, mousePosition.y - 30);

            }

            else if (event.type == Event::KeyReleased) {
                velocityX = 0;
                velocityY = 0;
            }






            window.display();

        }


    }

    return 0;
}

So this is not the first method I tried to move the ball. The first method is every event when a key is pressed the x or y changed, but with this method the movement wasn't as smooth as I want so I tried this method where when example: I press w the velocity (velocityY) becomes 5 and every frame of the game the position changes. And it works smoothly but a glitchy contrail runs before the ball disapering and reappearing sometimes. I found out to that when i move the mouse in the window (knowing that I implemented an event that takes mouse movement) the ball moves very smoothly.

1
You are clearing the window inside the event loop. It might be better to move it outside. Put it immediately before you start drawing the circle. - Galik
Also you call window.display() in the event loop and also outside. You should call that only once. windoe.clear(); /* draw window stuff */ window.display();. - Galik
It is probably best to work from an example in the instructions and modify that: sfml-dev.org/documentation/2.5.1/… - Galik
thanks @Galik as you said I was forgetting window.clear() Thanks again. - Nicolas Azzi

1 Answers

0
votes

Extending the answer in comments, a proper implementation of a game loop with should look like follows:

while (window.isOpen())
{
   // Event processing
   sf::Event event;
   while (window.pollEvent(event))
   {
       // Request for closing the window
       if (event.type == sf::Event::Closed)
           window.close();
   }

   // Clear the whole window before rendering a new frame
   window.clear();

   // Draw some graphical entities
   window.draw(sprite);
   window.draw(circle);
   window.draw(text);

   // End the current frame and display its contents on screen
   window.display();
}

Code extracted from SFML tutorials

But, since you're working with physics, I recommend you this answer about setting a constant game tick, to avoid that kind of glitches when elements collide.