MouseHit() does not work sometimes

BlitzMax Forums/BlitzMax Programming/MouseHit() does not work sometimes

Robert Cummings(Posted 2006) [#1]
Any clues why MouseHit() isn't reliable on my pc? It's a dual core machine and very fast, however MouseHit often 'misses' or fails to pick up my clicks. I find I need to call it a LOT in order to get some kind of response from it, as in more than once per loop!

I don't have this problem with keyboard input nor do I have a problem with any other games or applications.


tonyg(Posted 2006) [#2]
What if you use mh1=mousehit(1) once each loop?


Robert Cummings(Posted 2006) [#3]
I do this.

I have also tried directly calling MouseHit(1) several times. Both require being called several times to get a response on this PC.


Grey Alien(Posted 2006) [#4]
hmm. In BlitzPlus I got unreliability but this was due me calling it more than once and each call cleans out the buffer (of one hit at least). So in the end I used TonyG's method of only calling once per loop. Also have you used flushmouse anywhere in your loop? If so you might not be giving enough time between the flush and the mousehit for a new hit to be registered. I did this by accident too and it resulted in unreliable mouse performance.


IPete2(Posted 2006) [#5]
One Eyed Jack - how are you synching to the vblank? Maybe the mouse is only polled once a vblank - if you are going two or three flips before a vblank occurs maybe the mouse can miss a call?

for example the mouse code in the BMax docs is as follows:


' mousehit.bmx

Graphics 640,480

While Not KeyHit(KEY_ESCAPE)
	Cls
	If MouseHit(1) DrawRect 0,0,200,200
	If MouseHit(2) DrawRect 200,0,200,200
	If MouseHit(3) DrawRect 400,0,200,200
	Flip 
Wend




if you put a 1 or a 0 after the flip, the mousehit doesnot seem to register all the time and so only occasionally draws the rectangle. Using a -1 seems to get a result though.

Just a thought.

IPete2.


Robert Cummings(Posted 2006) [#6]
Hi Grey, no flushmouse.

IPete2, I am just using Flip without parameters. The game is graphically heavy but there is only one place I call the mouse input gathering routine. I find calling it more often picks up the MouseHit() more frequently.

Could it be an issue with dual core cpus?


Dreamora(Posted 2006) [#7]
Could be.
Did you install the AMD drivers for powersaving (or something like that) from their page? If this is not installed, there are known issues that can cause quite a lot as the time calculation in running apps is incorrect as the stepping / energy saving stuff does not work as the apps assume it does.

There is as well a WinXP hotfix that might be needed additionally (although its normally only needed for Intel dualcores).


Robert Cummings(Posted 2006) [#8]
I have installed the hotfix and power saving.


Grey Alien(Posted 2006) [#9]
Maybe you can post a demo for others to test?


Robert Cummings(Posted 2006) [#10]
I can't post my whole game.


Grey Alien(Posted 2006) [#11]
What about an .exe (unless it's secret) or a sub section that still shows the symptoms?


Robert Cummings(Posted 2006) [#12]
It's secret (you know how it is)...

I have isolated absolutely EVERYTHING! I even removed all the timing code till I had basically Ipete2's code (only it's drawing a lot more) and I'm having this problem with flip 1, 0 or -1...

I'm getting sparodic inputs from MouseHit (mousedown seems fine, but then it would be hard to tell with mousedown).


Grey Alien(Posted 2006) [#13]
If your game has a decent framerate, why not jsut use MouseDown once per frame and set a global variable based on the state?


Robert Cummings(Posted 2006) [#14]
I may just have to do this. It's crazy though? I'm pulling my hair out here man!


Grey Alien(Posted 2006) [#15]
I hate that kinda crazy technical crap. It drives me mad and wastes soo much time. That's why most peoples plans go to pot, because they don't put in contingency for sh*t like this.


Robert Cummings(Posted 2006) [#16]
Ok after a lot of fiddling about, and also trying events, I've come to the conclusion that mousehit() seems to clear the backlog every time it's called.

Thats the problem, I think.


Robert Cummings(Posted 2006) [#17]
OK

The more I render, the less responsive the mouse gets, despite the framerate being in the high 500's.

MouseX() and MouseY() respond constantly and fine. It's just a problem with mouse buttons. F@@@@:):):)! thing.

I'm guessing we'll see more bugs like this as more people make mouse-controlled games. Most people do not make mouse controlled games in Blitzmax presently.


Grey Alien(Posted 2006) [#18]
oh dear, my next game is mouse controlled. I'll let you know if it cocks up on mine. If not, I'll post a beta for testing.


TartanTangerine (was Indiepath)(Posted 2006) [#19]
Does it work if your do a PeekEvent?


Dreamora(Posted 2006) [#20]
OEJ: Is this perhaps somehow connected to the fake try to get around mouselag? (reinserting a different mouse lag issue)

Otherwise, you might try one thing:

Let the user decide the FPS. then do the drawing timed (ie if tdiff > 1000/wished_fps renderscene() ) This way you get around 20'000 - 100'000 main loop runs which is even higher than the timer precision and thus should be able to get all mouseevents without any problem.


Grey Alien(Posted 2006) [#21]
OEJ: Is this perhaps somehow connected to the fake try to get around mouselag? (reinserting a different mouse lag issue)
God I hope not!


AlexO(Posted 2006) [#22]
OK

The more I render, the less responsive the mouse gets, despite the framerate being in the high 500's.



when you say 'render' are you just using basic DrawImage/Text commands? There shouldn't be a reason why it doesn't come out 'sometimes'. I'm currently working on a blitzmax project and I call mousehit() and mousedown() once per loop and it has not been unresponsive, and my render functions have become a bit more complex than the standard blitzmax draws. Try that example that IPete2 posted from the blitzDocs. If that works for you then there's something in your mainloop that is calling mouseHit().

If you happen to be using IGlass btw and call IGL_RefreshGUI() that would cause even ur single mouseHit() call to not work sometimes, because it calls it inside that function.


Damien Sturdy(Posted 2006) [#23]
This is why i prefer custom input routines. One "Updateinput" command whenever you need to update it. If mousehit is broken, you can use mousedown in a custom routine to detect it.

On a side note, I'm not having any mousedown problems and i'm rendering pretty intensive 3D.

[edit] Quckly realised I dont even use the mousehit commands :)


skidracer(Posted 2006) [#24]
Rob, I haven't been able to reproduce your problem here and have double checked module source without any luck.

I would try adding a PollSystem at the same point you are flushing the graphics pipe with GrabPixmap. If that fails, you might want to consider sending me a build to see if I can reproduce your problem here.


Digital Anime(Posted 2006) [#25]
This topic solved mine issue...
I use FlushMouse() to make sure after each loop MouseZ() = 0
But I forgot that it had a great effect on all other buttons as well
By putting MouseHit() direct above the FlushMouse my game I'm working on works smoothly again


JazzieB(Posted 2006) [#26]
Glad you've solved the issue. For future reference, and I think that this was pointed out above somewhere, MouseHit() returns the number of times a mouse button has been pressed since the last time that MouseHit() was called. This count is then reset to 0 (which is why you should only call it once per loop) or when you use FlushMouse.


luigikart(Posted 2007) [#27]
One time I made a game where you slide the numbers around and put them in order. I called Keyhit by saying for example:
If (board(0) = 4) And (KeyHit(203))
board(0) = board(1)
board(1) = 4
...
EndIf
It didn't work.
Then I tried saying: If KeyHit(203) Then leftkey = 1
My new code:
If (board(0) = 4) And (leftkey = 1)
board(0) = board(1)
board(1) = 4
...
EndIf
It worked.
(By the way incase you were wondering, if a tile = 4 it is blank in this program.)
So instead of calling MouseHit directly, do what I did with KeyHit. It might work.


Amon(Posted 2007) [#28]
?


plash(Posted 2007) [#29]
?


I second that..


GfK(Posted 2007) [#30]
This one time.... at band camp....


TomToad(Posted 2007) [#31]
Ok after a lot of fiddling about, and also trying events, I've come to the conclusion that mousehit() seems to clear the backlog every time it's called.

Yes it does. This is the proper behavior. If you call MouseHit() then click the button 10 times, then call Mousehit() again, it is reset to 0, not 9.
If for some reason, there is going to be significant time between Mousehit() calls and its possible for the button to be clicked more than once between calls, then you need to check the value returned to determine the number of times it is hit. Something like this.
Local NumMouseHits1:Int = 0

...

Local MouseHit1:Int = False
If NumMouseHits 'Check to see if Mousehits still need to be processed
    MouseHit1 = True
   NumMouseHits :- 1
Else
    NumMouseHits = MouseHit(1) 'Store number of times Mouse has been pressed since last call
    If NumMouseHits 'Was the button pressed at least once?
          MouseHit1 = True
          NumMouseHits :- 1
    End If
End If

If MouseHit1
   'Process Mouse Hits here
End If



TomToad(Posted 2007) [#32]
One time I made a game where you slide the numbers around and put them in order. I called Keyhit by saying for example:
If (board(0) = 4) And (KeyHit(203))
board(0) = board(1)
board(1) = 4
...
EndIf
It didn't work.
Then I tried saying: If KeyHit(203) Then leftkey = 1
My new code:
If (board(0) = 4) And (leftkey = 1)
board(0) = board(1)
board(1) = 4
...
EndIf
It worked.


Actually it would have worked if you had typed
If KeyHit(203) And (board(0) = 4)

You see, if one part of the And is false, then the other part will never get processed,