Simple code to illustratue the mouse movement in FPS game

After checking the sample in ATI SDK's doc, I understand how to implement the mouse movement in FPS game:
Camera moves according mouse and mouse always stays in the center of the screen without cursor.

Let's use the following code snippet to swipe all the guessing:

 1static POINT cursorPos;            //current position
 2
 3switch (message){
 4    case WM_MOUSEMOVE:
 5        // Mouse look
 6        if (captureMouse)    //captureMouse means the camera is moving with the mouse
 7        {    
 8            POINT point = { app->getWidth() / 2, app->getHeight() / 2 };
 9
10                    //Utility method to set camera rotation to specified offsets
11            app->rotateView(0.003f * (point.y - GETY(lParam)), 0.003f * (point.x - GETX(lParam)));
12
13                    //set the mouse position as the center of screen
14            ClientToScreen(hwnd, &point);
15            SetCursorPos(point.x, point.y);
16
17        }
 
18        else            //if the mouse click the gui control(e.g. menu item), the app need to handle the control event now
19        {
20            app->onMouseMove(GETX(lParam), GETY(lParam), (wParam & MK_LBUTTON) != 0, (wParam & MK_MBUTTON) != 0, (wParam & MK_RBUTTON) != 0);
21        }

22        break;
23    case WM_LBUTTONDOWN:        //capture the "mouse click" event when mouse down
24        SetCapture(hwnd);
25                    // if the mouse is not captured then capture the mouse unless the user clicks a GUI element
26        if (!captureMouse && !app->onMouseClick(GETX(lParam), GETY(lParam), LEFT_BUTTON, true))
27        {
28            
29            cursorPos.x = GETX(lParam);
30            cursorPos.y = GETY(lParam);
31            ClientToScreen(hwnd, &cursorPos);
32
33            captureMouse = true;    //captured!
34            ShowCursor(FALSE);    //hide mouse cursor
35                        //set the mouse position as the center of screen
36            POINT point = { app->getWidth() / 2, app->getHeight() / 2 };
37            ClientToScreen(hwnd, &point);
38            SetCursorPos(point.x, point.y);
39        }

40        break;
41    }

U can see if we take out of the crap of setting of windows cursor, the controlling of mouse movement is just 30 lines of code,  aint it kind of magic?
The codes above is implemnted in windows, I mean, the thinking is the same and there is not much work to do if u want to use in Linux.

posted on 2007-01-27 20:46  SigEric  阅读(262)  评论(0)    收藏  举报

导航