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

Author Topic: Would a buffer help me?  (Read 2373 times)

0 Members and 1 Guest are viewing this topic.

BorisDieKlinge

  • Newbie
  • *
  • Posts: 15
    • View Profile
Would a buffer help me?
« on: July 14, 2010, 12:30:26 am »
Hey.
I'm sorry but i couldn't think of a better subject.
Here is my Porblem:

Code: [Select]

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow App(sf::VideoMode(800,600),"lala");
    App.UseVerticalSync(true);

    sf::Event Event;
    sf::Image Image;
    Image.LoadFromFile("E:/again_i_try_to/test/bin/Debug/x.bmp");

    sf::Sprite Sprite;
    Sprite.SetImage(Image);

    while(App.IsOpened())
    {
        while(App.GetEvent(Event))
        {
            if((Event.Type== sf::Event::KeyPressed) && (Event.Key.Code==sf::Key::Escape))
            App.Close();

            if(Event.Type==sf::Event::Closed)
            App.Close();
        }
        App.Clear(sf::Color(120,21,34));
        Sprite.SetPosition(Event.MouseMove.X,Event.MouseMove.Y);
        App.SetCursorPosition(100,100);

        App.Draw(Sprite);
        App.Display();
    }

    return EXIT_SUCCESS;
}


For some reason the cursor is moving around if you move your mouse. Thats not very surprising since I move it before I Set it back to (100,100). But why can I actually SEE this? Is there a way to fix this problem? Maybe a Buffersystem like in SDL?
I'm aware of the the fact, that I simly could delete the liene "Sprite.SetPosition(Event.MouseMove.X,Event.MouseMove.Y);" but i would like to fix this problem rather to learn than to produce a working code.

Thank you all.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Would a buffer help me?
« Reply #1 on: July 14, 2010, 11:22:29 am »
Your code produces an undefined behaviour, because you use Event.MouseMove outside the event loop; its members may contain anything.

You should use App.GetInput().GetMouseX/Y() instead.
Laurent Gomila - SFML developer

BorisDieKlinge

  • Newbie
  • *
  • Posts: 15
    • View Profile
Would a buffer help me?
« Reply #2 on: July 14, 2010, 12:46:53 pm »
I replaced the line where I moved the corsur by
 Sprite.SetPosition(App.GetInput().GetMouseX(),App.GetInput().GetMouseY());

But the problem remains.
I don't have a clue why...

Maybe somebody may compile my code to see what i mean?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Would a buffer help me?
« Reply #3 on: July 14, 2010, 02:49:36 pm »
Quote
But why can I actually SEE this?

Your application runs at X frames per second, whereas the mouse sends its position to the system Y times per second. In your case, X is probably lesser than Y.
Laurent Gomila - SFML developer

BorisDieKlinge

  • Newbie
  • *
  • Posts: 15
    • View Profile
Would a buffer help me?
« Reply #4 on: July 14, 2010, 03:49:06 pm »
...
But....
well. I understand your argumentation. But see:


App.Clear();
Clears the Screen. This means that the stuff actually displayed on screen is the stuff from the last frame.

App.Draw(Sprite);
Drwas the Sprite onto the Buffer.

App.Display();
Displays the content of the Buffer at the screen.

I actually don't get why the framerates X and Y have something to do with this, as the there should be nothing displayed what happens between App.Clear(); and App.Display();
So...

The Problem is gone as I suddenly, by writing this post, understand what happens     :D

When you call
Sprite.SetPosition(Event.MouseMove.X,Event.MouseMove.Y);
the position of your cursor might be P(100,110).
This means that the Sprite will be drawn at this point P.

App.SetCursorPosition(100,100);
resets the cursor back to P(100,100). But during the time between this App.SetCursorPosition(100,100); and the next Sprite.SetPosition(Event.MouseMove.X,Event.MouseMove.Y); the position of the cursor might chang again to, e.g. P(132,83).

Well, thats it.
Maybe some other noobs like me can make use of my Problem :P

Thank you for your help
greetings BorisDieKlinge
[/code]