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

Author Topic: Making SFML smoother and more accurate  (Read 8648 times)

0 Members and 1 Guest are viewing this topic.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Making SFML smoother and more accurate
« on: June 17, 2013, 04:13:32 pm »
I'm getting through the input commands and such as you'll all know looking at my posts  but one thing I'm finding quite annoying is how glitchy everything looks at the moment when I put the code in. I see different things like velocity in other peoples' code when it comes to input though again these all seem to be for 1.6 versions of SFML.

Are there commands out there that can help me with the problems I'm having? For instance, when I move with the keys my object sort of just blinks across the screen rather than move in a smooth way. There's that same sort of effect with camera command as well and so on. There's also this annoying thing where when it comes to my mouse input that the object the mouse cursor moves is away from the actual cursor as opposed to on it or in the centre.
« Last Edit: June 17, 2013, 04:42:28 pm by Lethn »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Making SFML smoother and more accurate
« Reply #1 on: June 17, 2013, 04:53:03 pm »
Your description of the problem is not vrey clear. Showing some code could help.
Laurent Gomila - SFML developer

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #2 on: June 17, 2013, 04:58:49 pm »
Alright, I'll eat some pizza first and then post some stuff up :D

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #3 on: June 17, 2013, 05:37:55 pm »
#include <Iostream>
#include <SFML/Graphics.hpp>

int main()
{

    sf::RenderWindow window(sf::VideoMode(800, 600), "Lethn 2D Engine");

sf::Texture texturebackground;
if (!texturebackground.loadFromFile( "background.png" ) )
return EXIT_FAILURE;
sf::Sprite spritebackground(texturebackground);

sf::Texture player;
if ( !player.loadFromFile( "player.png" ) )
return EXIT_FAILURE;
sf::Sprite playersprite ( player );


sf::View TopDownCamera;
TopDownCamera.reset ( sf::FloatRect ( 0, 0, 800, 600 ) );



    while (window.isOpen())
    {


        sf::Event event;
        while (window.pollEvent(event))


            if ( sf::Keyboard::isKeyPressed( sf::Keyboard:: W  ) )


            {
                playersprite.move ( 0, -20 );
            }

            else if ( sf::Keyboard::isKeyPressed( sf::Keyboard:: A  ) )

            {
                playersprite.move ( -20, 0 );
            }

            else if ( sf::Keyboard::isKeyPressed( sf::Keyboard:: D  ) )

            {
                playersprite.move ( 20, 0 );
            }

            else if ( sf::Keyboard::isKeyPressed( sf::Keyboard:: S  ) )

            {
                playersprite.move ( 0, 20 );
            }

            else if ( sf::Keyboard::isKeyPressed( sf::Keyboard:: Up ) )

            {
                TopDownCamera.move ( 0, -10 );
            }

            else if ( sf::Keyboard::isKeyPressed( sf::Keyboard:: Down ))

            {
                TopDownCamera.move( 0, 10 );
            }

            else if ( sf::Keyboard::isKeyPressed( sf::Keyboard:: Left ) )

            {
                TopDownCamera.move ( -10, 0 );
            }

            else if ( sf::Keyboard::isKeyPressed( sf::Keyboard:: Right ) )

            {
                TopDownCamera.move ( 10, 0 );
            }
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.setView( TopDownCamera );
        window.draw ( spritebackground );
        window.draw( playersprite );
        window.display();
    }

    return 0;
}

stoney153

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #4 on: June 17, 2013, 05:43:00 pm »
You know the whole 'Blinking across the screen' could just be because you haven't limited the frame rate, like for modern computers a square on the screen isn't exactly demanding, or maybe you're moving it by too many pixels at once (like moving it 50 pixels a time will make it jump like hell). A plethora of answers for such a broad statement :P Also, this may be off topic, but I'd really like to know more about the pizza... ... ...  :o

---You just posted code---
Try using something like myx+=10 (I think sprite.getPosition().x +=10 would also work) for the movement. Or use less pixels because that's a fairly big jump it's making.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #5 on: June 17, 2013, 05:48:15 pm »
Why are all those sf::Keyboard::isKeyPressed inside the event loop?

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #6 on: June 17, 2013, 06:01:43 pm »
stoney153 : Thanks for the code stoney153, could you go into a bit of detail on what you just posted so I know where you're coming from with it? On the numbers front I've only seen < 10 used in move examples etc. I can understand that it was pretty broad so I probably need to look at more code and mess with what I have some more. I understand the differences between this code and other games' code but for the sake of a reference, when you look at other games you'll notice that they all move ultra smooth when you move the camera around or the character even if the animations are extremely basic.

One game I'm thinking of is World of Goo which I thought was very nicely put together, when you click and drag the goo balls about there's no jittering or warping about the place, I was hoping to learn how to get that sort of effect now I have basic functions running.

G. : Well I'm just going by what I see on other examples and the documentation etc. G. so if there's a better way let me know. I know it's not that necessary for input with coordinates but when it comes to mouse events etc. codeblocks would complain at me so I guess I'm just keeping it all in one place so it's organised, it doesn't seem to interfere with the rest of the code either way so I've left it there.
« Last Edit: June 17, 2013, 06:07:46 pm by Lethn »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #7 on: June 17, 2013, 06:22:54 pm »
Actually, just realised I can post a gameplay video of World of Goo so you know what I'm on about :D



When the camera and objects move there's no jittering or anything like that and the FPS is very smooth, just like you said I guess it must be the FPS in that case as well as what you posted.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Making SFML smoother and more accurate
« Reply #8 on: June 17, 2013, 06:28:49 pm »
If you find code examples that show sf::Keyboard or sf::Mouse function calls inside the event loop, please notify me, I'll remove them ;)

Your code structure is wrong, please read the events and real-time inputs tutorials. And I'm sure it's even written in read in one of these tutorials.
Laurent Gomila - SFML developer

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #9 on: June 17, 2013, 06:34:41 pm »
Ohhh, double checked, so it may have been somewhere else on the internet where I saw that, I honestly don't get it though, what makes it wrong? It doesn't seem to do anything bad to the code right away so is it something to do with the order it compiles?

« Last Edit: June 17, 2013, 06:37:00 pm by Lethn »

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #10 on: June 17, 2013, 06:53:27 pm »
There are several reasons it is wrong.

1. I could be wrong on this one, but iirc, until you've finished processing events for the frame, the Keyboard and Mouse buffers won't be up to date. If you want the sf::Keyboard and sf::Mouse functions to give you up to data data, you must call them after the event loop.

2. When your game is running, there may be frames where more than one event is processed. If multiple events are processed, your sprite will move multiple times in that frame when it should only move once. Conversely, if no events occur in a frame (which is usually more likely than multiple events in a frame), then your sprite won't move at all when it should.
I use the latest build of SFML2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Making SFML smoother and more accurate
« Reply #11 on: June 17, 2013, 06:53:42 pm »
You update your game only when an event occurs. An event is "a key was pressed", "the mouse moved", "the window was closed", etc. But keeping a key pressed doesn't trigger any event, so if you don't do anything else like moving the mouse, nothing happens and your game is not updated. The only code that should be inside the event loop is code that reacts to such events.
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Making SFML smoother and more accurate
« Reply #12 on: June 17, 2013, 06:54:20 pm »
Quote
1. I could be wrong on this one, but iirc, until you've finished processing events for the frame, the Keyboard and Mouse buffers won't be up to date. If you want the sf::Keyboard and sf::Mouse functions to give you up to data data, you must call them after the event loop.
This is wrong in SFML 2, there's no more link between events and real-time inputs.
Laurent Gomila - SFML developer

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #13 on: June 17, 2013, 06:57:34 pm »
Alright, thanks for that guys I understand it all better now, so if I've made the right conclusion, my problem is I need to look at frame rate and the amount of pixels my objects are being moved by and how to manipulate all of that better. Oh and only put events in the Event loop :D
« Last Edit: June 17, 2013, 07:00:16 pm by Lethn »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #14 on: June 17, 2013, 07:41:59 pm »
Am I thinking about the layout of this the right way?

Quote
playersprite.move ( playersprite.getPosition( window ).x +=0, playersprite.getPosition( window ).y -=10 );

Trying to combine my code with what stoney153 posted, I am just messing around with it right now, codeblocks is complaining about the way I've written it.
« Last Edit: June 17, 2013, 07:44:03 pm by Lethn »

 

anything