Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411522 Posts in 69377 Topics- by 58431 Members - Latest Member: Bohdan_Zoshchenko

April 28, 2024, 08:43:00 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Relative mouse input with SDL
Pages: [1]
Print
Author Topic: Relative mouse input with SDL  (Read 8050 times)
muku
Level 10
*****


View Profile
« on: May 08, 2009, 03:02:20 AM »

I feel stupid for not being able to figure this out; even some quick googling hasn't turned anything up.

Anyway, I try to use SDL_GetRelativeMouseState to get incremental mouse input (think controlling a flight simulator with the mouse, that's rather close to my use case). It basically works, but as soon as the mouse leaves the game window, I don't get any more input. I tried to remedy that by calling SDL_WM_GrabInput(1), but that just constrains the mouse pointer to the window; as soon as it hits the border, no further relative motion in that direction is possible. So my last idea was to use SDL_WarpMouse to put the mouse cursor at the window's center every frame, but when I do that I don't get any deltas at all anymore.

So, help! I don't need a mouse pointer, I just want to know how much the user has moved the mouse in any given frame. How do I do that?
Logged
Saint
Level 3
***



View Profile WWW
« Reply #1 on: May 08, 2009, 03:08:34 AM »

Well, if you warp the mouse to the center of the screen and do this *every frame* you can just call SDL_GetMouseState and your delta movement is the offset from the wanted position, like so:

Code:
//Somewhere in the gameloop
int MouseX,MouseY;
SDL_GetMouseState(&MouseX,&MouseY);
int DeltaX = MouseX - HalfScreenX;
int DeltaY = MouseY - HalfScreenY;
SDL_WarpMouse(HalfScreenX,HalfScreenY);
Logged
muku
Level 10
*****


View Profile
« Reply #2 on: May 08, 2009, 03:10:58 AM »

Hm, good idea. Why doesn't this seem to work with SDL_GetRelativeMouseState though? From the name I would have guessed that that is exactly the function I'd want to use...

EDIT: Wait a moment... I guess SDL updates the mouse state when I poll the message queue, huh? Maybe I called WarpMouse at the wrong time and it does work after all when I do it right, will have to check...
« Last Edit: May 08, 2009, 03:24:01 AM by muku » Logged
Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #3 on: May 08, 2009, 08:22:41 AM »

On initialization:

Quote
  SDL_ShowCursor(0);
  SDL_WM_GrabInput(SDL_GRAB_ON);


In your event update loop:

Quote
while ( SDL_PollEvent(&event) ) {
         switch (event.type) {
            case SDL_MOUSEMOTION:
             // use event.motion.xrel and event.motion.yrel


SDL_WM_GrabInput confines the mouse to the window, so you don't have to call WarpMouse every frame or anything like that.
Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic