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

Author Topic: sf::Window::SetCursorPosition in OSX troubles  (Read 7280 times)

0 Members and 1 Guest are viewing this topic.

ozzy vbg

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Window::SetCursorPosition in OSX troubles
« on: May 14, 2011, 09:03:34 pm »
I'm trying to get FPS like camera input with SFML in OSX. My code works fine in Windows XP/7 and Ubuntu 10.04 (and later versions). In OSX, it does not.

My problem is that I hardly get any values at all. Mouse events are fired off even though the mouse hasn't moved a single pixel. I get around this by storing the previous position of the mouse and only using SetCursorPosition when this differs from the new value.

I tried figuring out what was causing my problem by increasing the amount of pixels the mouse would be able to move before being re-centered and I noticed that each time I used SetCursorPosition the mouse lost all of it's acceleration, which resulted in my camera coming to a stop for a moment before starting to move again.

So, apparently SetCursorPosition doesn't simply move the cursor, it also resets it's acceleration.

Is there any way this could be fixed in coming versions of SFML? To reposition the mouse cursor without affecting it's current acceleration?

Is there perhaps anyone else in this forum who've had the same problem and overcome it? Any help is appreciated!

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: sf::Window::SetCursorPosition in OSX troubles
« Reply #1 on: May 15, 2011, 01:44:03 pm »
Quote from: "ozzy vbg"
Is there any way this could be fixed in coming versions of SFML? To reposition the mouse cursor without affecting it's current acceleration?
Unfortunately, this is completely dependent on the OS and I can't do anything...
SFML / OS X developer

ozzy vbg

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: sf::Window::SetCursorPosition in OSX troubles
« Reply #2 on: May 15, 2011, 02:50:44 pm »
Quote from: "Hiura"
Unfortunately, this is completely dependent on the OS and I can't do anything...


Thanks for the speedy reply! :D

I've looked at the source code for SFML and it appears you're using a function called CGDisplayMoveCursorToPoint. I googled it a bit and found a thread in where I suspect the poster is experiencing the same problem: http://www.idevgames.com/forums/showthread.php?mode=linear&tid=8561&pid=69266

He gets the advice to try using CGSetLocalEventsSuppressionInterval to get rid of the laggy input. If I owned a mac computer myself I'd gladly try playing around with this but unfortunately I can only try my code on my friends mac on rare occasions.

I'm making a wild guess here that changing the source from...

Code: [Select]
// Move cursor
CGDisplayMoveCursorToPoint([sfPrivAppController primaryScreen],
CGPointMake(absolute.x, absolute.y));


to...

Code: [Select]
// Move cursor
CGSetLocalEventsSuppressionInterval(0);

CGDisplayMoveCursorToPoint([sfPrivAppController primaryScreen],
CGPointMake(absolute.x, absolute.y));


would solve the problem?

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
sf::Window::SetCursorPosition in OSX troubles
« Reply #3 on: May 15, 2011, 02:52:44 pm »
Looks like CGAssociateMouseAndMouseCursorPosition() could fix the issue but I don't know how it could be used with SFML.

Edit: and CGWarpMouseCursorPosition() instead of CGDisplayMoveCursorToPoint() could also be a good point.
Want to play movies in your SFML application? Check out sfeMovie!

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
sf::Window::SetCursorPosition in OSX troubles
« Reply #4 on: May 15, 2011, 03:04:32 pm »
Ok, seems I went through this too quickly. Thanks for the input to both of you. I'll dig into it hopefully soon.

(If you have time, could you create a issue on the tracker ? I'm kind of running out of time now with the school stuff.)
SFML / OS X developer

ozzy vbg

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Window::SetCursorPosition in OSX troubles
« Reply #5 on: May 15, 2011, 03:16:28 pm »
Quote from: "Hiura"
(If you have time, could you create a issue on the tracker ? I'm kind of running out of time now with the school stuff.)


Will do! Thanks for all your work on SFML! Wish I had a mac so I could play around with the source myself and help you out...


EDIT:

https://github.com/SFML/SFML/issues/46

couldn't find how to categorize it under OSX so if someone could fix that it would be great.

ozzy vbg

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Window::SetCursorPosition in OSX troubles
« Reply #6 on: May 16, 2011, 10:11:13 pm »
I believe I've solved the issue now by changing the source code for WindowImplCocoa::SetCursorPosition() and compiling it myself on a virtual OS X install. I managed to get my friend to test it for me on his native OS X and according to him the function works as intended, only now it doesn't cause the cursor to freeze momentarily.

Code: [Select]
void WindowImplCocoa::SetCursorPosition(unsigned int Left, unsigned int Top)
{
NSPoint pos = NSMakePoint ((float) Left, (float) Top);

if (myWrapper) {
// Flip for SFML window coordinate system
pos.y = [[myWrapper window] frame].size.height - pos.y;

// Adjust for view reference instead of window
pos.y -= [[myWrapper window] frame].size.height - [[myWrapper view] frame].size.height;

// Convert to screen coordinates
NSPoint absolute = [[myWrapper window] convertBaseToScreen:pos];

// Flip screen coodinates
absolute.y = [[NSScreen mainScreen] frame].size.height - absolute.y;

// Move cursor
CGSetLocalEventsSuppressionInterval(0);
CGWarpMouseCursorPosition(CGPointMake(absolute.x, absolute.y));
}
}


I'm not sure if the call to CGSetLocalEventsSuppressionInterval() is necessary because I forgot to ask my friend to test the code without that.

Thanks Ceylo for your advice to use CGWarpMouseCursorPosition()! :D

FPS mouse controlled camera in OS X with SFML! Hurray!

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
sf::Window::SetCursorPosition in OSX troubles
« Reply #7 on: May 17, 2011, 10:47:57 pm »
I'll apply something similar to SFML2 (CGSetLocalEventsSuppressionInterval is deprecated). Thanks for your help.

(SFML1.6 won't be updated because 1.7 will not exist.)
SFML / OS X developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
sf::Window::SetCursorPosition in OSX troubles
« Reply #8 on: May 18, 2011, 09:53:45 pm »
It's done but I had to do it completely differently (I couldn't find an alternative to CGSetLocalEventsSuppressionInterval that worked). You can get the new version from github (SFML2 only).
SFML / OS X developer

deathbuffer

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::Window::SetCursorPosition in OSX troubles
« Reply #9 on: June 21, 2011, 07:13:01 pm »
Hi, hope you don't mind hijacking this thread for a question. :)

I'm using SFML2 from Github, and I have a problem on OS X with SetCursorPosition.
If I run a fullscreen mode which is not the native resolution, SetCursorPosition still places the cursor as if it was still using that native resolution. I.e. if my screen is 1024x768, but I use a 800x600 fullscreen mode, and I call SetCursorPosition(400, 300), the cursor is placed at 312/234.
This is specific to Mac, so I get different (and more expected) behavior on other platforms.

Any ideas?
Thanks!

EDIT: Sorry, it's actually the other way around: fixed the example.
Engine programmer at Black Pants Studio
http://tinyandbig.com

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
sf::Window::SetCursorPosition in OSX troubles
« Reply #10 on: June 21, 2011, 09:04:09 pm »
That's probably a bug. I'll check that out next week.
SFML / OS X developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
sf::Window::SetCursorPosition in OSX troubles
« Reply #11 on: June 24, 2011, 06:56:03 pm »
Should be ok now.
SFML / OS X developer

deathbuffer

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::Window::SetCursorPosition in OSX troubles
« Reply #12 on: June 26, 2011, 06:11:42 pm »
Fix confirmed! That's awesome, I can finally ditch SDL on Mac OS :)

Thanks again!!!
Engine programmer at Black Pants Studio
http://tinyandbig.com

 

anything