SFML community forums

Help => Window => Topic started by: AdrianM on March 08, 2009, 08:32:57 pm

Title: Relative mouse position on mac os x using SetCursorPosition
Post by: AdrianM on March 08, 2009, 08:32:57 pm
I'm trying to create a breakout clone and i'm having problems on mac os x.
I'm calculating a mouse position delta and then using SetCursor pos to position the mouse at the center of the screen. The problem is that i'm getting some stuttering, on mac os x. The PAD moves slow and with big increments in position. Here is some code to replicate the issue:

Edit:

Also with the same code there aren't any problems on windows.

Code: [Select]

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>

#pragma comment(lib,"sfml-graphics-s-d.lib")
#pragma comment(lib,"sfml-window-s-d.lib")
#pragma comment(lib,"sfml-system-s-d.lib")

#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")

void Cleanup()
{
}

int main()
{
// Create the main window
sf::RenderWindow *App = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "asda", sf::Style::Close);

    // Create a clock for measuring time elapsed
    sf::Clock Clock;

    // Set color and depth clear value
    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);

    // Start game loop
    while (App->IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App->GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App->Close();

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App->Close();

            // Resize event : adjust viewport
            if (Event.Type == sf::Event::Resized)
                glViewport(0, 0, Event.Size.Width, Event.Size.Height);
        }

        // Set the active window before using OpenGL commands
        // It's useless here because active window is always the same,
        // but don't forget it if you use multiple windows or controls
        App->SetActive();

        // Clear color and depth buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Apply some transformations
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.f, 0.f, -200.f);

int dispX = App->GetInput().GetMouseX() - 400;
static int posX = 400;
posX += dispX;
if (posX < 0) posX = 0;
if (posX > 800) posX = 800;

if (dispX != 0)
App->SetCursorPosition(400, 300);

glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, 800, 0, 600, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

int w = 100;
int h = 30;
glTranslatef(posX, 20, 0);
glBegin(GL_QUADS);
glVertex2f(-w, -h);
glVertex2f(+w, -h);
glVertex2f(+w, +h);
glVertex2f(-w, +h);
glEnd();

glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);

        // Finally, display rendered frame on screen
        App->Display();
    }

delete App;

    return EXIT_SUCCESS;
}
Title: Relative mouse position on mac os x using SetCursorPosition
Post by: Ceylo on March 09, 2009, 05:22:19 am
I suppose this is an OS issue : when you move the mouse faster, you get a bigger delta for the mouse movement on your screen. But here you always reset the mouse position => the delta of the previous move (from which the movement speed seems to be calculated) keeps being 0 => no delta (or less than one integer unit) ; and when you finally succeed in moving the mouse, it moves too much at a time. That is at least all I can think of.

But.. I wonder why you need to prevent the mouse from moving.
Title: Relative mouse position on mac os x using SetCursorPosition
Post by: AdrianM on March 09, 2009, 12:28:09 pm
Quote from: "Ceylo"

But.. I wonder why you need to prevent the mouse from moving.


I need to prevent the mouse from moving because i don't want it to go outside the window, when the application is in windowed mode. How can i achieve what i want without reseting the mouse to the center of the screen or by any other means?

Edit:
In the past i used glfw and did not have any issues with this kind of behaviour. Under the hood glfw uses this code to position the mouse:


Code: [Select]

    CGDisplayMoveCursorToPoint( kCGDirectMainDisplay,
                                CGPointMake( x, y ) );


Also, i'm using version 1.4
Title: Relative mouse position on mac os x using SetCursorPosition
Post by: Ceylo on March 09, 2009, 04:47:18 pm
The Mac OS X implementation of SFML also uses CGDisplayMoveCursorToPoint().

One problem with preventing the mouse from moving is the user can't go on the File menu to quit your application and you must not expect him to know the shortcut (yeah some don't).

You could hide the mouse while it's on your screen an draw a virtual one (or none if you don't need it), and unhide it when it's outside of your window.
By the way you can always check the mouse position and change *your* values in order to match the window area, that's what you did with..
Code: [Select]
if (posX < 0) posX = 0;
if (posX > 800) posX = 800;


What is exactly the behaviour you wish ? I could make the rectangle follow the mouse or accelerate according to the mouse position without having to prevent the mouse from moving.
Title: Relative mouse position on mac os x using SetCursorPosition
Post by: AdrianM on March 10, 2009, 02:08:30 pm
Quote from: "Ceylo"

What is exactly the behaviour you wish ? I could make the rectangle follow the mouse or accelerate according to the mouse position without having to prevent the mouse from moving.


Yes that's what i want. I managed to do it finally without centering the mouse, i was wondering why it didn't work without the virtual mouse. :D
Title: Relative mouse position on mac os x using SetCursorPosition
Post by: Sallen on October 30, 2009, 04:56:36 pm
Hello there. I the same problem with OSX and SetMousePosition. I'm trying to achieve a FPS-like mouselook camera.

When the mouse cursor reaches the end of the screen it obviously stops, so I can't use the mouse position to calculate the camera rotation right away. I have tried to "reset" the mouse position so it stays at the center of the window, but every time I call SetMousePosition, the mouse movement stops (I have to stop moving it and start again in order for the application to notice I'm still moving the mouse).

Is there any built in way to get the mouse speed in SFML or another way of reseting the mouse cursor seamlessly? Any other techniques for FPS cameras?

Thank you!

This is my first post, so hello everyone :P
Title: Relative mouse position on mac os x using SetCursorPosition
Post by: Ceylo on October 30, 2009, 06:10:25 pm
Quote from: "Sallen"
Is there any built in way to get the mouse speed in SFML or another way of reseting the mouse cursor seamlessly? Any other techniques for FPS cameras?

Well I don't think it would be possible to detect some mouse movement if it reached the end of the screen and thus does no more move :/ .

As for not interrupting the mouse move events I don't have any idea at this time. This seems to be related to the OS event handling.
Title: Relative mouse position on mac os x using SetCursorPosition
Post by: Sallen on October 30, 2009, 07:09:19 pm
I see... In any case there has to be a way of doing an FPS camera on SFML and OSX. Hasn't anyone done it before?

EDIT:
I succeeded using a different approach. I used CGGetLastMouseDelta (from the ApplicationServices framework) to get the mouse delta. This function ignores the screen borders, so you can move the mouse indefinitely. As long as I don't want to click (you would click outside the window) I'm good with this solution.