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

Author Topic: slow reaction to mouse event  (Read 3835 times)

0 Members and 4 Guests are viewing this topic.

grim

  • Guest
slow reaction to mouse event
« on: February 05, 2014, 06:07:08 am »
The mouse event that checks if my mouse is on my button takes to long to react. The average time it takes to react is about 4 to 5 seconds.  That's insane, am I doing something wrong?

#include "MainMenuState.h"
#include "State.h"
#include "SEngine.h"
#include <SFML/Graphics.hpp>

MainMenuState MainMenuState::mainMenuStart; // static MainMenuState variable

/* Load() method for MainMenuState, initializes all variables in MainMenuState.
 */

void MainMenuState::Load(){
        backGround = sf::RectangleShape(sf::Vector2f(640.0f,480.0f));
        menu = backGround.getLocalBounds();
        backGround.setOrigin(menu.left,menu.top);
        playText.loadFromFile("Resources\\images\\Play_Button_Sheet.png");
        playButton.setSize(sf::Vector2f(350.0f, 50.0f));
        playButton.setTexture(&playText);
        playButton.setTextureRect(sf::IntRect(0,0,350,50));
        playButton.setPosition(menu.left + 200.0f, menu.top + 300.0f);
        backGround.setFillColor(sf::Color::Blue);
        playArmed = false;
};//Load()

/* Handling method for MainMenuState, handles events that may occur in the
   MainMenuState. The only parameter it takes is a reference to the state
   machine, SEngine.*/

void MainMenuState::Handling(SEngine* gameEng){
       
        while(gameEng->getWindow()->pollEvent(event)){
                        if(event.type == sf::Event::Closed){
                                gameEng->notRunning();
                                gameEng->getWindow()->close();
                        }
                        else if (event.type == sf::Event::MouseMoved){
                                if(((sf::Mouse::getPosition().x >= playButton.getPosition().x) && (sf::Mouse::getPosition().x <= playButton.getPosition().x + playButton.getSize().x))
                                        && ((sf::Mouse::getPosition().y >= playButton.getPosition().y) && (sf::Mouse::getPosition().y >= playButton.getPosition().y + playButton.getSize().y))){
                                                playButton.setTextureRect(sf::IntRect(0,51,350,50));
                                                playArmed = true;
                                }else{
                                        playButton.setTextureRect(sf::IntRect(0,0,350,50));
                                        playArmed = false;
                                }
                        }
                        else if (event.type == sf::Event::MouseButtonPressed){
                                if(event.mouseButton.button == sf::Mouse::Left){
                                        //change to playstate
                                }
                        }
                }

};//Handling()

/* Paint method for MainMenuState, draws the components of this state onto the screen.
   The only parameter it takes is a reference to the state machine, SEngine.*/

void MainMenuState::Paint(SEngine* gameEng){
        gameEng->getWindow()->clear();
        gameEng->getWindow()->draw(backGround);
        gameEng->getWindow()->draw(playButton);
        gameEng->getWindow()->display();
};//Paint()

/* Update method for MainMenuState, updates certain values or components that may
   have been affected by events in Handling method. The only parameter it takes
   is a reference to the state machine, SEngine.*/

void MainMenuState::Update(SEngine* gameEng){
       
};//Update()

void MainMenuState::TidyUp(){
};//TidyUp()

void MainMenuState::Halt(){
};//Halt()

void MainMenuState::Continue(){
};//Continue()

Pheonix

  • Newbie
  • *
  • Posts: 27
  • Using SFML 2.1 with Win/Visual C++ (Newbie !!!)
    • View Profile
Re: slow reaction to mouse event
« Reply #1 on: February 05, 2014, 06:55:21 am »
I'm a newbie to SFML, but I think it may be because you're using Event which is slower.

Try something like: if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
Using SFML 2.1 with Win/Visual C++ (Newbie !!!)

grim

  • Guest
Re: slow reaction to mouse event
« Reply #2 on: February 05, 2014, 07:01:55 am »
I'm a newbie to SFML, but I think it may be because you're using Event which is slower.

Try something like: if(sf::Mouse::isButtonPressed(sf::Mouse::Left))

The mouse clicking event is not my problem at the moment, especially since it doesn't do anything.  The problem I am having is that when the mouse movement is detected and its position is checked against the position of the button, it takes too long for the subtexture to change.

Pheonix

  • Newbie
  • *
  • Posts: 27
  • Using SFML 2.1 with Win/Visual C++ (Newbie !!!)
    • View Profile
Re: slow reaction to mouse event
« Reply #3 on: February 05, 2014, 07:09:37 am »
Try and see if there is an alternative to using Event for mouse moved/position similar to my previous example.
Using SFML 2.1 with Win/Visual C++ (Newbie !!!)

grim

  • Guest
Re: slow reaction to mouse event
« Reply #4 on: February 05, 2014, 07:37:37 am »
I tried creating a sf::Vector2i variable that would update in Update() and use that to compare positions but that didn't help.  I also discovered a second problem with code for some reason the boundaries defined in the mouse position event is not in the same position as the button.

 I attached a screenshot showing what I mean, the black box I drew on the screenshot indicates where the has to be positioned in order for the mouse position event to occur (which is still incredibly slow).

grim

  • Guest
Re: slow reaction to mouse event
« Reply #5 on: February 05, 2014, 07:55:08 am »
Okay I figured out the boundary problem, wasn't paying attention and forgot to pass the window into the getPositon() method for the mouse.  As for the slow reaction time it seems to have to do with the texture because I switched to just using a rectangleshape that changes colors when the mouse position event occurs.  Was I using the texture object incorrectly or something?

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: slow reaction to mouse event
« Reply #6 on: February 05, 2014, 07:20:10 pm »
You should not use sf::Mouse::getPosition() at events. Watch the tutorial.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: slow reaction to mouse event
« Reply #7 on: February 05, 2014, 07:51:24 pm »
Seriously people, stop with the "events are slow, use realtime input".

Events are not slow. You can write a huge application that relies completely on events for input without any problems.

Events and the realtime input functions serve different purposes, but event slowness is not one of those purposes.
If it takes 4-5 seconds to react to an event you are doing it wrong - no way around that.

In my own code I deal with a ton of events every single frame, update physics, update GUI elements, do network handling and much more and at 60fps I have loads of CPU time to spare every frame.

Please stop guessing about where performance problems hide and blame events as a reflex. Instead, measure performance, by using a profiler and then fix the actual performance bottlenecks rather than just guessing based on gut feeling and intuition (which never (well, very rarely) works).

Also make sure that basic stuff is in order, like updated graphics card drivers, building with optimizations enabled, enable compiler warnings, make sure you know the language and library you use well (yes, I know, that can take years)  etc etc.

And for the love of $DEITY, check your designs; don't do pointless work - check your loops, use standard algorithms (<algorithm> and friends), use RAII and not manual memory management, banish global variables (and yes, that includes horrors like singletons) and read the tutorials (they really are very good) and be systematic in your bug hunting (and use a debugger).

OK. Rant over. Just had to get that off my chest. It's amazing how many times the same mistakes are made again and again.
« Last Edit: February 05, 2014, 07:57:35 pm by Jesper Juhl »

 

anything