Is there a way to get initial position of mouse in SDL 2.0 ? I try to get mouse coordinates by SDL_GetMouseState(&mouse_x,&mouse_y), however I get the result I expected only after using the function SDL_PollEvent() and also I can't see a value other than (0,0) if the mouse has not been moved at least once since begining of the program.Although I don't check SDL_MOUSEMOTION and connect SDL_GetMouseState() to it, I get mouse coordinates only when mouse is moved.So what's wrong? Or is SDL_GetMouseState() suitable to do so?
Edit : Why, Why is there no any answer?
Try calling SDL_PumpEvents() before SDL_GetMouseState().
The SDL updates the position of the mouse internally in SDL_PrivateSendMouseMotion, which is called by various mouse related functions in the same file.
These functions are called in the event loop processing function WIN_WindowProc in response to the mouse events dispatched by Windows.
Thus, if you do not move the mouse, no event is dispatched and the SDL does not know where the mouse is. The solution is to wait for a mouse event before requesting the position and to find a workaround until this event.
I know it's late, but just chipping in with a resolution I found.
If you get zero coordinates, call SDL_GetGlobalMouseState(&x, &y) then offset x and y by the window.x and window.y coordinates.
Related
I'm trying to adjust user mouse input (slow down/fasten mouse pointer) by calculating and setting the new position of the pointer every mouseMoveEvent().
It looks like the pointer is drawn in it's normal position before mouseMoveEvent() is executed. This results in a bad looking, twitching cursor.
Here is an example of how I would try to keep the pointer in a place, ignoring user input.
Expected behaviour: The pointer stays steady in one place when I move the mouse.
Observed bahaviour: The pointer jiggles. For a fraction of a second, a new pointer is drawn in the position where the mouse is moved, after which it's moved back to it's oldPosition.
void MainWindow::mouseMoveEvent(QMouseEvent *)
{
// [event->accept();] Setting this does not help
// [setCursor(Qt::BlankCursor);] Setting this does not help
QCursor::setPos(oldPosition);
// [setCursor(Qt::ArrowCursor);]
}
P.S. May be there are other ways to adjust mouse sensitivity in qt embedded. Am I doing it right?
Short answer: No.
Unfortunately, there is no way to adjust mouse sensitivity using only QT libraries.
The twitching you are experiencing is because the OS does the work of drawing the mouse pointer, the frequency of which can be different from how frequently you receive mouse move events from the OS.
There is also no way to tell QT or the OS to send you mouse move events before every frame the cursor is drawn.
Workaround: You could hide the mouse pointer completely, and draw your own. This way you will have full control over when the cursor is drawn avoiding the glitches.
I am developing a brush just like the brush in mspaint, but I cannot get all the pixels from
WM_MOUSEMOVE when the mouse move over the pixels.only can get a set of desultory points.
You can use GetMouseMovePointsEx to get a history of the last 64 mouse points, which may include points that were never delivered to your app via WM_MOUSEMOVE.
But you can often improve the performance in this sort of application by ignoring the actual mouse position that WM_MOUSEMOVE sends you and instead query the pointer position directly with GetCursorPos.
Either way, you'll never get enough resolution from the mouse to draw a smooth joined-up line - the best you can do is draw straight lines (or even interpolate splines) between the coordinates.
At the moment I simply use the WM_MOUSEMOVE message, but it limits the mouse movement to the maximum resolution. So what's the best way of capturing the mouse with Win32 (on a OpenGl window)? I don't want to use freeglut or any extra library.
For games and realtime DirectInput is very suitable, it's moderately hard to use.
That is not core win32 api, the winapi way of getting the input is either GetCursorPos/SetCursorPos driven by your own update loop, so you query and reset with your own frequency.
Or SetCapture and then upon WM_MOUSEMOVE you call SetCursorPos.
The point of setting the cursor pos is to give room for movement so you can get the delta, the amount the cursor moved since the last update and then put it back the cursor into the center of your window.
If you want to be able to capture mouse events after the mouse has existed the window, then you might want to look into the SetCapture function
If your problem is that you want to make a FPS game and you want your character to be able to spin in a continuous motion, then you want to set the mouse position to the center of the window after each mouse move event and handle input based on the difference between the position of the cursor when the mouse move event is fired and the center of the screen. To set the position of the mouse you can use the SetCursorPos function.
Your best bets are to either use DirectInput (which can be a bit of a pain to set up) or RawInput.
There is a fairly comprehensive example available in the Using RawInput page (See example 2).
I'm attempting to move the camera via gluLookAt in my OpenGL app similar to how the camera moves in a FPS game. It works, however, I would like to center the mouse after moving it, to disallow the mouse from ever reaching the edge of the screen. After doing some research, I found glutWarpPointer is a viable option, however, I get the sense that professional game developers only utilize the basic OpenGL commands, instead of something like glutWarpPointer, or glutSolidCube, or glutSolidSphere. Am I correct in assuming this?
In my WndProc function, I set the look for my gluLookAt under the WM_MOUSEMOVE message in my switch block. I also realize that SendInput would achieve what I want, but from my understanding SendInput would trigger another WM_MOUSEMOVE when the cursor goes to the middle of the screen, essentially reversing my original movement of the mouse, and not moving the camera's look at all.
Any ideas?
Edit: I don't think there exists a function that does not call a WM_MOUSEMOVE message. I think the proper route is to create a flag that determines whether the WM_MOUSEMOVE was invoked by a SetCursorPos, and if it was, do not change the camera's look.
Use SetCursorPos to relocate the mouse cursor if you're on Windows.
I'm currently writing a c++ console application that grabs the mouse position at regular intervals and sends it to another visual application where it is used to drive some 3d graphics in real time. The visual app is closed source and cannot be altered outside it's limited plug-in functionality.
Currently I'm using the GetCursorPos() function which is easy and fast enough, but I'm running into the issue that all of the data is clipped based on the current screen resolution of 1920x1600 so that all x values are between 0 and 1920 and all y values are between 0 and 1600 no matter how far the mouse is physically moved.
I need to get the mouse position before it's clipped at the edge of the screen, or possibly the deltas which I could use to calculate the current position.
I've seen some references to the windows MouseMove event but I would really not want to implement a window to make it work or especially have it as the active to receive those events.
I'm working in a windows environment and a language change is not feasible.
I might be wrong, but in Win32 land you don't get mouse move messages when the mouse is at the edge of the screen because, well, the mouse isn't moving. The usual way to get an infinite mouse area is to do the following:
Hide the mouse, get exclusive access and record position
Centre mouse to window
When mouse moves, get delta from centre of screen to current position
Centre mouse to window again
The next mouse move should have a delta of (0,0), so ignore it
Go to 3 until end of mouse move operation
Reset position, show the mouse and release exclusive access
If you didn't hide the mouse, then you'd see the mouse moving a small distance and then snapping back to the centre position, which looks nasty.
This method does require a message pump for the mouse move messages so the console application idea probably won't work with this. Can you create a full screen invisible window for grabbing the mouse?
Just get the position, and move it to the center and return the delta yourself
This is how FPS games do it
I don't have any direct experience with raw input, which is probably what you need to tap into. According to MSDN, you have to register the device, then setup your winproc to accept the WM_INPUT messages and then do your calculations based on the raw data.
Here's another relevant link.