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

Author Topic: Speedup when moving mouse  (Read 4860 times)

0 Members and 1 Guest are viewing this topic.

Ninjaboi

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • http://www.ninjasoftcorp.webs.com/index.html
Speedup when moving mouse
« on: September 19, 2011, 12:39:57 pm »
When I move the cursor inside the window while having other things going on ( animation, sprite movement ), everything goes much faster. I haven't limited frame rate yet, though when I attempted to I got no change in result. My problem is exactly like the person who created this thread:

http://www.dreamincode.net/forums/topic/241952-sfml-speedup-with-mouse-movement/

The Youtube video he provides has the exact same problem, and his code is structured just like mine. If my code needs to be shown, I can, though I see no difference in the design we both have.

I'm using SFML 1.6 ( stable ), compiling in Visual Studio 2008 ( C++ ).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Speedup when moving mouse
« Reply #1 on: September 19, 2011, 01:08:08 pm »
Your game logic is inside the event loop, so it gets executed only when something happens -- a mouse move for example. Anything that doesn't react to a sf::Event should be outside the event loop.
Laurent Gomila - SFML developer

Ninjaboi

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • http://www.ninjasoftcorp.webs.com/index.html
Speedup when moving mouse
« Reply #2 on: September 19, 2011, 01:28:54 pm »
Thanks for the swift reply  8) .

I've taken the clearing, drawing, and displaying out of the event loop, and put them right outside the event loop ( into the main game loop ). It actually became faster than before ( frame issue perhaps? ).

EDIT: From a bit more testing, I believe that the speedup is actually due to the window refreshing every time I move the mouse. Does the window do that every time I move the mouse? If so, I might like to know how to disable that ( or work around it ).

Ninjaboi

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • http://www.ninjasoftcorp.webs.com/index.html
Speedup when moving mouse
« Reply #3 on: September 20, 2011, 01:21:34 pm »
Code: [Select]
// Continually update the starup window until closed.
while( Window.IsOpen() )
{

// Process events.
while ( Window.GetEvents() )
{

// Close window : exit
if ( Window.WindowEventsType() == "Closed" )
{

// Tells that the window is closing.
DebugMessageString( "Window is being closed..." );

// Closes the window.
Window.CloseWindow();

}

// Close window : exit when pressing Escape "ESC".
if ( Window.KeyboardKeyPressed() == "Escape" )
{

// Tells that the window is closing.
DebugMessageString( "Window is being closed..." );

// Closes the window.
Window.CloseWindow();

}

// Plays a sound when pressing "Tab".
if ( Window.KeyboardKeyPressed() == "Tab" )
{

bolt.PlaySound();

}

// Moves green square to the right.
if ( Window.KeyboardKeyPressed() == "Right" )
{

playerMoveRight = true;

}

// Moves green square to the left.
if ( Window.KeyboardKeyPressed() == "Left" )
{

playerMoveLeft = true;

}

// Moves green square to the up.
if ( Window.KeyboardKeyPressed() == "Up" )
{

playerMoveUp = true;

}

// Moves green square to the down.
if ( Window.KeyboardKeyPressed() == "Down" )
{

playerMoveDown = true;

}

// Stops movement going to the right.
if ( Window.KeyboardKeyReleased() == "Right" )
{

playerMoveRight = false;

}

// Stops movement going to the left.
if ( Window.KeyboardKeyReleased() == "Left" )
{

playerMoveLeft = false;

}

// Stops movement going to the up.
if ( Window.KeyboardKeyReleased() == "Up" )
{

playerMoveUp = false;

}

// Stops movement going to the down.
if ( Window.KeyboardKeyReleased() == "Down" )
{

playerMoveDown = false;

}

if( playerMoveLeft == true )
{

green.MoveX( -10 );

}

if( playerMoveRight == true )
{

green.MoveX( 10 );

}

if( playerMoveUp == true )
{

green.MoveY( -10 );

}

if( playerMoveDown == true )
{

green.MoveY( 10 );

}

// Left mouse button clicked.
if ( Window.MouseEventsType() == "MouseButtonPressed" )
{

// New button click event.
if( New.IsClicked( Window ) == true )
{

// Tells that the "New" button has been clicked.
DebugMessageString( "Mouse cursor clicked the \"New\" button..." );

}

// Load button click event.
if( Window.MouseLocationX() >= 70 && Window.MouseLocationX() <= 170 &&
Window.MouseLocationY() >= 360 && Window.MouseLocationY() <= 410 )
{

// Tells that the "Load" button has been clicked.
DebugMessageString( "Mouse cursor clicked the \"Load\" button..." );

}

// Tells where the mouse was clicked.
DebugMessageInt( Window.MouseLocationX() );
DebugMessageInt( Window.MouseLocationY() );

}

}

// Draws the menu.
Window.Render( menu.GetSprite() );
// Draws the green box.
Window.Render( green.GetSprite() );

// Displays the updated window.
Window.Update();

}


That's my event loop, followed by my rendering and updating functions. I have no idea what the problem is, other than the fact that everything inside the window goes much faster if I'm moving the mouse.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Speedup when moving mouse
« Reply #4 on: September 20, 2011, 01:55:44 pm »
Your game logic is still inside the event loop. It's not a problem of refresh speed, it's just that your entities move slower because you move them only when an event is triggered.

Your code also mixes two separate concepts:
- reacting to an event ("play a sound when tab is pressed")
- doing something as long as a key is pressed ("move entity if arrow key is down")

The first thing shuold be implemented in the event loop, using sf::Event.
The second thing should be implemented in the game loop (outside the event loop), using sf::Keyboard/sf::Mouse/sf::Joystick (SFML 2) or sf::Input (SFML 1).
Laurent Gomila - SFML developer

Ninjaboi

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • http://www.ninjasoftcorp.webs.com/index.html
Speedup when moving mouse
« Reply #5 on: September 20, 2011, 04:23:04 pm »
Ah! I'm such an idiot  :lol: . It took me a bit to process what you meant, and now I see what you mean. This is my new and improved code that actually works:

Code: [Select]
// Continually update the starup window until closed.
      while( Window.IsOpen() )
      {

         // Process events.
         while ( Window.GetEvents() )
         {

            // Close window : exit
            if ( Window.WindowEventsType() == "Closed" )
            {

               // Tells that the window is closing.
               DebugMessageString( "Window is being closed..." );
             
               // Closes the window.
               Window.CloseWindow();
             
            }

            // Close window : exit when pressing Escape "ESC".
            if ( Window.KeyboardKeyPressed() == "Escape" )
            {

               // Tells that the window is closing.
               DebugMessageString( "Window is being closed..." );
             
               // Closes the window.
               Window.CloseWindow();
             
            }

            // Plays a sound when pressing "Tab".
            if ( Window.KeyboardKeyPressed() == "Tab" )
            {

               bolt.PlaySound();
             
            }

            // Moves green square to the right.
            if ( Window.KeyboardKeyPressed() == "Right" )
            {

               playerMoveRight = true;
             
            }
             
            // Moves green square to the left.
            if ( Window.KeyboardKeyPressed() == "Left" )
            {

               playerMoveLeft = true;
             
            }

            // Moves green square to the up.
            if ( Window.KeyboardKeyPressed() == "Up" )
            {

               playerMoveUp = true;
             
            }

            // Moves green square to the down.
            if ( Window.KeyboardKeyPressed() == "Down" )
            {

               playerMoveDown = true;
             
            }

            // Stops movement going to the right.
            if ( Window.KeyboardKeyReleased() == "Right" )
            {

               playerMoveRight = false;
             
            }
             
            // Stops movement going to the left.
            if ( Window.KeyboardKeyReleased() == "Left" )
            {

               playerMoveLeft = false;
             
            }

            // Stops movement going to the up.
            if ( Window.KeyboardKeyReleased() == "Up" )
            {

               playerMoveUp = false;
             
            }

            // Stops movement going to the down.
            if ( Window.KeyboardKeyReleased() == "Down" )
            {

               playerMoveDown = false;
             
            }

            // Left mouse button clicked.
            if ( Window.MouseEventsType() == "MouseButtonPressed" )
            {

               // New button click event.
               if( New.IsClicked( Window ) == true )
               {
               
                  // Tells that the "New" button has been clicked.
                  DebugMessageString( "Mouse cursor clicked the \"New\" button..." );
               
               }

               // Load button click event.
               if( Window.MouseLocationX() >= 70 && Window.MouseLocationX() <= 170 &&
                  Window.MouseLocationY() >= 360 && Window.MouseLocationY() <= 410 )
               {
               
                  // Tells that the "Load" button has been clicked.
                  DebugMessageString( "Mouse cursor clicked the \"Load\" button..." );
               
               }

               // Tells where the mouse was clicked.
               DebugMessageInt( Window.MouseLocationX() );
               DebugMessageInt( Window.MouseLocationY() );
             
            }

         }

            if( playerMoveLeft == true )
            {
             
               green.MoveX( -10 );
             
            }

            if( playerMoveRight == true )
            {
             
               green.MoveX( 10 );
             
            }

            if( playerMoveUp == true )
            {
             
               green.MoveY( -10 );
             
            }

            if( playerMoveDown == true )
            {
             
               green.MoveY( 10 );
             
            }


         // Draws the menu.
         Window.Render( menu.GetSprite() );
         // Draws the green box.
         Window.Render( green.GetSprite() );

         // Displays the updated window.
         Window.Update();
       
      }


Thanks a ton Laurent! I need to get more sleep before I start this again, in order to prevent that from happening again  8) .

The event that is triggered by pressing down the 'Tab' key does use sf::Event and is in the event loop. I simply made it where I was working with strings rather than events outside my wrapper library.

Code: [Select]
// Processes the event(s) to see what key on the keyboard is being released.
std::string Window::KeyboardKeyPressed()
{
if ( ( Event.Type == sf::Event::KeyPressed ) && ( Event.Key.Code == sf::Key::Tab ) )
{

return "Tab";

}
}


It does use an sf::Event, and it is processed in the event loop ( from what i can tell ). Though then again, if you are able to elaborate on what you mean, that would be nice. I simply didn't want to have to type that out each time I wanted to work with checking for events, so I just wanted to have to check for strings being returned from that particular function  :D . I cut down all the other tested events in that function ( to only show the important one ), but that's the same thing for all the other events that could be checked by SFML.

As for the second thing you mentioned, I believe I corrected that now. The events are tested in the event loop, and the game logic is outside with the rest in the game loop. The events change bool variables to be tested in the game loop to see if the sprite needs to be moved a certain direction.

Again, thank you for all the help! <3 SFML ( sorry SDL, it just wasn't working out  :wink:  ).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Speedup when moving mouse
« Reply #6 on: September 20, 2011, 04:52:48 pm »
Your code looks perfect now, I have nothing else to say ;)
Laurent Gomila - SFML developer

 

anything