SFML community forums

Help => General => Topic started by: dabo on July 06, 2008, 01:45:08 pm

Title: Something strange... (Mouse click)
Post by: dabo on July 06, 2008, 01:45:08 pm
There seems to be something strange going on when it comed to reacting to mouse clicks. I have some buttons (images, no gui) where the user can click and everything works until the 3rd click (99.9% of the time). After that you randomly have to click the button once/twice before anything happens. If I run my game with visual studios 2008 pro (start debugging) this problem never occurs but if I close visual studios and double click the executable it does. Today I discovered that the problem occures if I don't move the mouse at all during the first 3 clicks, If I move it before I click the 3rd time it works.  :?

I have tried it on 3 computers, my laptop gets the behavior described above and the other two get the problem as well, but not necessarily on the 3rd click.

I have designed my game in two completely different ways (exept for the event-loop) but the problem shows up for both. I have tried events and inputs for mouse clicks but both have the same problem. I'm linking with the static libraries.

This is the event-loop:
Code: [Select]
sf::Event eEvent;
while(renderer::getInstance()->getRenderWindow()->GetEvent(eEvent))
{
if(eEvent.Type == sf::Event::KeyPressed && eEvent.Key.Code == sf::Key::Escape)
exit(EXIT_SUCCESS);

else if(eEvent.Type == sf::Event::MouseButtonPressed && eEvent.MouseButton.Button == sf::Mouse::Left)
{
int mouseX = renderer::getInstance()->getRenderWindow()->GetInput().GetMouseX();
int mouseY = renderer::getInstance()->getRenderWindow()->GetInput().GetMouseY();
int hit = -1;

for(int i = 0; i < 5; i++)
{
if(v_iClickables[i].Contains(mouseX, mouseY)) //check clickable regions (sf::IntRect)
{
hit = i;
break;
}
}

         //if a button was pressed, act accordingly
if(hit == 0)
exit(EXIT_SUCCESS);
else if(hit == 1)
; //to be implemented
else if(hit == 2)
{
if(!bIsSpinning) //if wheels are not spinning, start spinning
{
for(int i = 0; i < signed(v_iWheelsLeftToSpin.size()); i++)
v_iWheelsLeftToSpin[i] = sf::Randomizer::Random(5, 19);

v_iWheelsLeftToSpin[0] += 10;

bIsSpinning = true;
}
}
else if(hit == 3)
; //to be implemented
else if(hit == 4)
; //to be implemented
}
}


I should be able to put together an example if you want to see the problem in action. I have no idea why the problem only occurs if you run the executable and not when you are debugging it with visual studio :?
Title: Something strange... (Mouse click)
Post by: Laurent on July 06, 2008, 01:54:32 pm
Sounds weird :|

Quote
I should be able to put together an example if you want to see the problem in action

Yep, it would help a lot as I have no clue.
Title: Something strange... (Mouse click)
Post by: dabo on July 06, 2008, 02:15:10 pm
Ok here it is, use the SPIN-button in-game.

Click BAIL-button / esc-key to quit

Download link (http://www.dabostudios.net/game.rar)

Note, it might not happen the 3rd time you click, it's been different with the 3 computers I have tried it on.
Title: Something strange... (Mouse click)
Post by: Laurent on July 06, 2008, 03:09:50 pm
I got the same issue with your game, however I couldn't reproduce it in a simple application.

So what I suggest is that you try to reduce your code to a minimal and complete app which reproduces the problem.
Title: Something strange... (Mouse click)
Post by: dabo on July 06, 2008, 03:25:55 pm
Ok, I will try that.
Title: Something strange... (Mouse click)
Post by: dabo on July 06, 2008, 05:31:12 pm
I have managed to remove a lot of code and the problem is still there. There isen't much code now so I would be happy if someone could take a look at it, I might be using sfml wrong (noone else has this problem) or it's my game logic (which I will look over again later). I have stared at this code for a very long time now and I think I've gone blind  :)

Use the same download link as before for the updated version. I have removed the interface in this version but the buttons are at the same postions as before still clickable.

Note: Build a project with the code and see for yourself how well it works as long as you run it from visual studio.

I have also noticed that when the problem occurs the cursor-arrow changes to an hour-glass very quickly...
Title: Something strange... (Mouse click)
Post by: dewyatt on July 06, 2008, 07:10:57 pm
It does seem to have a small problem.

Just glancing through the code, I'd suggest you clean it up.
That may help you find the problem here.
Instead of all these if statements, use a switch.
And in your execute() loop, you might want to use statics as an optimization since it's called a lot.
Title: Something strange... (Mouse click)
Post by: dabo on July 06, 2008, 09:39:28 pm
After removing the code that calculates the new positions for the sprites and draw them the problem is gone. I don't understand how that code can mess up event handling, and only when I'm not debugging it in visual studio (which means debugging the game for the error is useless)  :? . I don't know how to write the code I removed differently, I mean it has nothing to do with events and still I get this damn behavior.
Title: Something strange... (Mouse click)
Post by: dabo on July 06, 2008, 11:40:14 pm
If it wasn't weird enough already here comes more.

I have gotten two versions to work (only tested on my laptop, gonna try the other once tomorrow)

1. Using any mouse button but the left one on button pressed.

2. Using the left mouse button on button released.

 :?  :?  :?

Pretty much everything but the way I want works it looks like, but I guess I could go with button released instead of pressed. But I really want to know why left pressed doesn't work!

I have uploaded another file, try it and see if it works on your computer as well. There are two executables this time, each showing one of the scenarios above. Notice though how the hour-glass shows up for a short period of time only when pressing the left mouse button, never for the others. Nothing make sense right now, so it's probably something obvious :)

Download link (http://www.dabostudios.net/game2.rar)
Title: Something strange... (Mouse click)
Post by: dabo on July 09, 2008, 12:38:31 pm
Has anyone done some more digging into this? I myself can't let it go, nothing makes sense, that's probably why I don't have a clue how to fix this. I'm considering rewriting the entire code.
Title: Something strange... (Mouse click)
Post by: Laurent on July 09, 2008, 12:48:31 pm
Sorry, same for me (totally weird and no clue about what's happening) :(

And I can't even reproduce it in a simple code, except yours.
Title: Something strange... (Mouse click)
Post by: dewyatt on July 09, 2008, 03:31:50 pm
Yeah this really is weird.
I nearly rewrote the whole thing in a little framework.
If run under the debugger, it's all fine.
But when you run it alone, that weird thing happens.

I suggest you try to rewrite it in a different way.
This must be some weird bug in MSVC/CRT or SFML. Who knows.
Title: Something strange... (Mouse click)
Post by: Mindiell on July 09, 2008, 05:17:04 pm
I got this when logging events :
Quote
Left Click detected !
hit = 2
hit on 2
Start spinning
37.9295
Stop spinning
45.8926
45.8932
45.8939
45.9785
51.8179
51.8191
51.8198
51.8823
55.9586
59.2977
Left Click detected !
hit = 2
hit on 2
Start spinning
59.4034

I pressed LeftButton 4 times, and get only two real events. At 45 and 51, it didn't detect my click

And finally I found !
Quote
Left Click detected !
hit = 2
hit on 2
Start spinning
Button released !
Stop spinning
Button released !
Button released !
Button released !
Left Click detected !
hit = 2
hit on 2
Start spinning
Button released !

Sometime, the buttonpressed event seems to be erased by the buttonreleased event...

For information, I'm not clicking 3 times, but I'm just waiting for a few seconds instead...
Title: Something strange... (Mouse click)
Post by: dabo on July 09, 2008, 09:21:56 pm
Mindiell: So it has something to do with sfml?

I mean there's no way my code to spin and draw the wheels can mess up the event's right? how could it? Have you also noticed how the cursor switches to the hour-glass quickly sometimes only when pressing the left mouse button? never with any other button.

I builded the game with Code::Blocks and got the same behaviour.

Thanks everyone for trying.
Title: Something strange... (Mouse click)
Post by: dewyatt on July 09, 2008, 09:33:05 pm
Quote from: "dabo"
Mindiell: So it has something to do with sfml?

I mean there's no way my code to spin and draw the wheels can mess up the event's right? how could it? Have you also noticed how the cursor switches to the hour-glass quickly sometimes only when pressing the left mouse button? never with any other button.

I builded the game with Code::Blocks and got the same behaviour.

Thanks everyone for trying.

It seems like it has something to do with SFML to me.

I don't know how your code could screw with events.
And yes, I noticed the cursor changes to an hourglass for some reason.

I do wonder if this problem exists under linux as well.
Title: Something strange... (Mouse click)
Post by: Avency on July 09, 2008, 09:47:44 pm
I tried it under linux (wine and linux native build) and everything seemed to work.
Title: Something strange... (Mouse click)
Post by: dabo on July 09, 2008, 10:07:04 pm
Ok thanks for trying under linux.

The weirdest of all is that any button but the left one seem to works.

If I was to rewrite the code, any ideas how? In my honest opinion it's written pretty straight forward as it is.
Title: Something strange... (Mouse click)
Post by: Mindiell on July 09, 2008, 10:21:21 pm
I noticed the fast change when left button is clicked sometime. I thought it was something with window focus, it seems not.
I'm keeping up with it...
Title: Something strange... (Mouse click)
Post by: Mindiell on July 09, 2008, 10:35:43 pm
Tried on my home computer : no problem... It seems I can't reproduce it, all is running well !

I use the same C:B version here and at the office, the only difference I can see now is that we use XP at the office, and I'm still using W2K at home.

When speaking about Window focus, maybe XP is doing something that w2k is not ?

So it's maybe not SFML...
Title: Something strange... (Mouse click)
Post by: dabo on July 09, 2008, 11:19:59 pm
Hmm ok. All 3 computers I've tried running the game on uses Win XP Pro, so it might be XP's fault.

As you have been saying Mindiell, it looks like there is something going on with window focus.

I logged all event types coming when I do a left button press.

On success the following event types were generated:
8 - MouseButtonPressed
9 - MouseButtonReleased

On failure the following event types were generated:
2 - LostFocus
3 - GainedFocus
9 - MouseButtonReleased (that's why MouseButtonReleased works without any problems)

The questions are; Where does the MouseButtonPressed event go and why does the window loose focus (only the left button makes the window lose focus)?

EDIT: I've also noticed that the window seems to lose focus randomly for seemingly no reason. At least we know the hour-glass appears because of the window losing focus.

I've tried it myself under linux and everything seem to work.
Title: Something strange... (Mouse click)
Post by: Mindiell on July 10, 2008, 10:53:41 am
I'll test it on another computer with XP Home to see if the same thing happens. I'll let you know...
Title: Something strange... (Mouse click)
Post by: dabo on July 10, 2008, 12:09:55 pm
I've removed most of the code in the game-loop and added a short piece of code that will decrease a counter for about the same amount of time as it took for the wheels to stop spinning:

Code: [Select]
while(1)
{
sf::Event eEvent;
while(App.GetEvent(eEvent))
{
if(eEvent.Type != 10)
std::cout << eEvent.Type << std::endl;

if(eEvent.Type == sf::Event::KeyPressed && eEvent.Key.Code == sf::Key::Escape)
exit(EXIT_SUCCESS);

else if(eEvent.Type == sf::Event::MouseButtonPressed && eEvent.MouseButton.Button == sf::Mouse::Left)
{
int mouseX = App.GetInput().GetMouseX();
int mouseY = App.GetInput().GetMouseY();
int hit = -1;

for(int i = 0; i < 5; i++)
{
if(v_iClickables[i].Contains(mouseX, mouseY)) //check clickable regions
{
hit = i; //button i was pressed
break;
}
}

//if a button was pressed, act accordingly
switch(hit)
{
case 0: exit(EXIT_SUCCESS);

case 2: if(!bIsSpinning) //if wheels are not spinning, start spinning
{
for(int i = 0; i < signed(v_iWheelsLeftToSpin.size()); i++)

spin += sf::Randomizer::Random(5, 19);


spin += 10;

bIsSpinning = true; //the wheels are spinning
}
break;
}
}
}

                                //New code
if(spin <= 0)
bIsSpinning = false;
else
{
offset = (offset + 1) % 96;

if(offset == 0)
spin -= 2;
}
                                //New code end

std::stringstream ss2;
ss2 << spin;
sf::String str(ss2.str());
str.SetPosition(10.f, 10.f);
App.Draw(str);

App.Display();
}


Even this short code fails to work. Does the time waiting for the wheels to stop/counter to reach 0 (this example) cause the window to lose focus? Seems weird to me.

Is visual studio forcing the window to have focus as the problem doesn't appear debugging it?
Title: Something strange... (Mouse click)
Post by: Mindiell on July 10, 2008, 12:27:53 pm
Quote from: "dabo"
Is visual studio forcing the window to have focus as the problem doesn't appear debugging it?
I have the problem even if under my EDI (C::B, I d'ont use VS).
Title: Something strange... (Mouse click)
Post by: dabo on July 10, 2008, 12:43:54 pm
Quote from: "Mindiell"
Quote from: "dabo"
Is visual studio forcing the window to have focus as the problem doesn't appear debugging it?
I have the problem even if under my EDI (C::B, I d'ont use VS).


Yea I've tried code::blocks myself, and the problem always exists. That's why I was wondering if visual studio is forcing focus on the window while debugging while code::block doesn't.

...I tried speeding up the wheels, I changed:

Code: [Select]
1000.f * App.GetFrameTime();

to

Code: [Select]
2000.f * App.GetFrameTime();

the problem seems gone as long as I click to spin the wheels again right after they have stopped, if I wait for a while the problem is there again. It looks like if you just sit there and do nothing, the window seems to lose focus after a while. That can't be right can it?
Title: Something strange... (Mouse click)
Post by: dabo on July 10, 2008, 12:55:45 pm
I think I have found something!

When creating the renderWindow if I use the style Fullscreen or None it seems to work! Using the other styles don't work. Could you guys try that as well and see if it works for you?
Title: Something strange... (Mouse click)
Post by: dewyatt on July 10, 2008, 11:08:33 pm
I confirmed it doesn't happen with style set to None or Fullscreen.
I have also confirmed it never happens when a debugger is attached.
You can attach a debugger while the app is running (even after the problem occurs) and it will not occur.
It's very odd.
I feel like this is maybe a windows quirk. Maybe windows is doing something different behind the scenes when a debugger is present.
I just don't know!
Title: Something strange... (Mouse click)
Post by: dabo on July 10, 2008, 11:13:12 pm
I have no problem using Style::None in the final version of the game but it sure is strange. Thanks for confirming it.
Title: Something strange... (Mouse click)
Post by: dewyatt on August 01, 2008, 03:12:36 am
Sorry to revive a slightly old thread but...

I've actually experienced this in my game recently.
In debug mode I've had it happen a few times.
In release mode I've had it happen at least once.
Just putting that out there.
I feel like this has to be a bug in SFML.
I mean, you don't see any other windows games/apps having this problem.
Anyways, it's been pretty rare so I'm not too concerned about it.
Title: Something strange... (Mouse click)
Post by: dabo on August 01, 2008, 11:48:24 am
It would be really interesting to know what causes it.
Title: Something strange... (Mouse click)
Post by: dewyatt on August 01, 2008, 03:20:16 pm
Quote from: "dabo"
It would be really interesting to know what causes it.

Indeed.