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

Author Topic: Relative Mouse Movement (Mouse Capture)  (Read 25611 times)

0 Members and 1 Guest are viewing this topic.

Robert42

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Relative Mouse Movement (Mouse Capture)
« Reply #15 on: October 14, 2012, 08:58:24 pm »
can sf::sleep be used in multiple threads?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10281
    • View Profile
    • development blog
    • Email
Re: Relative Mouse Movement (Mouse Capture)
« Reply #16 on: October 14, 2012, 09:01:45 pm »
Sure but you have to call it from each thread, i.e. if just call it in the main thread, thread X won't be affected.
sf::sleep simply lets the current thread sleep for x time units. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

greeniekin

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: Relative Mouse Movement (Mouse Capture)
« Reply #17 on: November 02, 2012, 09:13:55 pm »
I actually have troubles with this in source engine games.

I have 2 screens so even fullscreen(only one screen) I can still loose focus if I move the mouse really quickly to the right and left click.

It infuriates me that the source engine still has not worked that out.

This should be pretty easy to implement. I have played with this before on windows.

Here is all you need. It locks the cursor to an area. You will need to resize it with the window.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms648383(v=vs.85).aspx

XGrabPointer might work on linux. Never used it though.

With mac I have no idea.

I could make a patch for this with the go ahead on the api implementation from Laurent.(not mac. I do not own one)
Of course for the time for him to decide on the api he could implement it himself.


I suppose something like this
void Window::lockMouseCursor(bool locked)
bool Window::isLockMouseCursor()

though sfml uses set and get everywhere. So i suppose

void Window::setMouseCursorLocked(bool locked)
bool Window::getMouseCursorLocked()
« Last Edit: November 02, 2012, 09:29:55 pm by greeniekin »

Oberon

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • My Github profile
Re: Relative Mouse Movement (Mouse Capture)
« Reply #18 on: November 03, 2012, 04:34:34 pm »
If this is ever going to be implemented, Chrome might serve as a reference implementation: http://code.google.com/p/chromium/source/search?q=%3A%3ALockMouse%5C%28%5C%29.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Relative Mouse Movement (Mouse Capture)
« Reply #19 on: November 04, 2012, 11:05:02 am »
thanks! it's indeed useful if we do it one day.
SFML / OS X developer

Robert42

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Relative Mouse Movement (Mouse Capture)
« Reply #20 on: November 04, 2012, 12:07:59 pm »
We could create a fork, implement it by ourselves (1 persen per system) and create a pull request

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: Relative Mouse Movement (Mouse Capture)
« Reply #21 on: January 19, 2013, 08:49:20 pm »
Notes:
  • I call my sf::RenderWindow 'App'
  • _'Name' designates a variable owned by a class
  • Have not tested with 2D SFML, this is logic, so it should be applicable
  • Code Snippets in order of being called

Event triggers for focus:
sf::Event Event;
while (getApp()->GetEvent(Event)){
        if (Event.Type == sf::Event::Closed){
                getApp()->Close();
        }else if (Event.Type == sf::Event::Resized){
                _ScreenSize = sf::Vector2<int> (Event.Size.Width, Event.Size.Height);
                if (_ScreenSize.y < _ScreenSize.x){
                        glViewport(0, (_ScreenSize.y-_ScreenSize.x)/2, _ScreenSize.x, _ScreenSize.x);
                }else{
                        glViewport((_ScreenSize.x-_ScreenSize.y)/2, 0, _ScreenSize.y, _ScreenSize.y);
                }
        }else if (Event.Type == sf::Event::GainedFocus){
                _InFocus = true;
        }else if (Event.Type == sf::Event::LostFocus){
                _InFocus = false;
        }else if (Event.Type == sf::Event::MouseButtonPressed){
                _InFocus = true;
        }else if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)){
                _InFocus = false;
        }
}
 

Resetting the mouse:
if (Cam->_CursorLocked){
        if (_InFocus){
                _App.ShowMouseCursor(false);
                _App.SetCursorPosition(_ScreenSize.x/2,_ScreenSize.y/2);
        }else{
                _App.ShowMouseCursor(true);
        }
}else{
        _App.ShowMouseCursor(true);
}
 

Getting relative mouse movements:
sf::Vector2<float> MouseMove = sf::Vector2<float> (App->GetInput().GetMouseX()-ScreenSize.x/2 ,App->GetInput().GetMouseY()-ScreenSize.y/2);
 

Long live rapid project development! -- Kestrel3D Game-Engine nearing completion