0. when you plan to write the kok1 demo's mouse management module, you can obtain the absolute position of the mouse in the window (relative to the coordinates of in the window ), then, calculate the coordinates of the location on the map and let the players go over. There are several solutions:
<1> use Windows System mouse pointer, getwindowpos to get the pointer position, and some WIN32API to get the pointer status. This problem is that in the game, the pointer of the mouse is basically not used, or even static images are not used, usually animation, the animation performance is too poor by repeatedly modifying the appearance of the window mouse.
<2> Microsoft provides directinput, which is used as an input control library for games. However, when looking at dinput, we found that there are two modes: relative mode and absolute mode. There are many examples of relative mode, but we cannot find how to use and set absolute mode, how can we get the absolute position of the mouse?
Later, I thought about the answer: Use the relative mode to achieve the same effect.
1. now that we have the relative displacement of the mouse in each frame of the game, we only want to get the absolute coordinates of one parameter, that is, the original coordinates of the mouse, with the original coordinates, you can add the relative displacement of each frame to the original coordinates to obtain the new coordinates. Where does the original coordinates come from? In fact, at the very beginning, I was wondering whether to use getcursorpos to retrieve it. Then I suddenly thought that what players actually saw was the mouse animation displayed in the game, this animation has nothing to do with the system pointer. To put it bluntly, it is an animated genie that moves with the mouse held by the player.
First, give mousemanager. H, which is the class declaration of the mouse Manager:
# Pragma once </P> <p> # include <ddraw. h> <br/> # include <dinput. h> <br/> # include "gamescreen. H "</P> <p> class mousemanager <br/>{< br/> Public: <br/> mousemanager (void); <br/> ~ Mousemanager (void); <br/> int Init (gamescreen * GS); <br/> int refreshdata (gamescreen * GS ); <br/> int draw (gamescreen * GS); <br/> void release (void); </P> <p> int X; // X coordinate of the mouse on the screen <br/> int y; // y coordinate of the mouse on the screen <br/> bool lbutton; // whether to press the current left mouse button <br/> bool rbutton; // whether to press the current right mouse button <br/> int status; // The mouse status 1: Normal, 2: attack, 3: Cast, 4: others </P> <p> PRIVATE: <br/> int width; // width of the mouse image <br/> int height; // height of the mouse image <br/> lpdirectdrawsurface7 * commonsurfaces; // array of pointer animation surfaces in normal state <br/> int animationindex; // animation of the mouse current index <br/> int * commonanimationdata; // animation of the mouse Index Array <br/> int commonanimationdatacount; // total number of mouse animation frames </P> <p> lpdirectinput8 lpdi; // input interface <br/> lpdirectinputdevice8 lpdimouse; // mouse device <br/> dimousestate mouse_state; // mouse status <br/> };
As mentioned above, we can define the start position of the mouse at will, regardless of the Windows mouse:
X = GS-> screenwidth/2-1; <br/> Y = GS-> screenheight/2-1;
At the beginning of the game, draw the mouse pointer at the position X and Y. In addition, at each frame of the game, take out the relative displacement of the mouse, refresh the x and y values, and finally draw the image of the mouse animation at the X and Y positions, the Code is as follows:
Lpdimouse-> getdevicestate (sizeof (dimousestate), (lpvoid) & mouse_state ); </P> <p> // set X and Y <br/> X + = (mouse_state.lx); <br/> Y + = (mouse_state.ly ); </P> <p> If (x> = GS-> screenwidth) <br/> X = GS-> screenwidth-1; <br/> else <br/> If (x <0) <br/> x = 0; </P> <p> If (Y> = GS-> screenheight) <br/> Y = GS-> screenheight-1; <br/> else <br/> If (Y <0) <br/> Y = 0; </P> <p> // set button <br/> If (mouse_state.rgbbuttons [0]) <br/> lbutton = true; <br/> else <br/> lbutton = false; </P> <p> If (mouse_state.rgbbuttons [1]) <br/> rbutton = true; <br/> else <br/> rbutton = false;
2. Conclusion: In fact, this has completed the goal of using the relative mode to obtain absolute coordinates. You only need to take a look at the matter and separate the mouse in the game from the Windows mouse. Because the two have been separated, the following code is simple, but it is easy to lock the mouse in the game window, that is, locking the Windows mouse, however, it has no impact on the game Mouse:
Rect window_rect; <br/> getwindowrect (hwnd, & window_rect); <br/> setcursorpos (window_rect.left + GS-> screenwidth/2, window_rect.top + GS-> screenheight/2 );
3. No picture, no truth. The following figure shows the demo: