Mouse Input

Chapter 14 of the textbook explains how to get input from the mouse. (See pages 604 to 611.) The engine provides the CZenMouse class, which is a useful class because it hides a lot of the details of using DirectInput. For example, it gives back absolute coordinates rather than coordinates that are relative to the last position.

Page 611 shows a good summary explaining how to incorporate the CZenMouse class into your program.

Note: If you want to get the screen coordinates of the cursor then you should use GetCursorPosition. If you use GetMousePos then you will get the amount the mouse has moved since the last poll, which is probably not what you want. The book doesn't say much (if anything) about GetCursorPosition but you can find it in engine.h and engine.cpp to see how it works.

Our basic approach for mouse input will be the following:

Custom Mouse Cursor

Pages 729 to 736 explain how to use a custom mouse cursor.

There is good news and bad news about using custom mouse cursors. The good news is that you already have code in engine.cpp to set it up. The bad news is that the code might have caused problems since you don't have the image file. I apologize if that code caused problems. Anyhow, you can now go to the Code page and download the cursor image file, which is named plusCursor.dds.

If you look on page 733 you will see code similar to the code that is in CZenMouse::Initialize. Here are the statements that I used. These are already in engine.cpp:

// Create a new surface for the mouse pointer image
g_pDevice->CreateImageSurface( 16, 16, D3DFMT_A8R8G8B8, &m_pCursorSurf );
// Load the image file from disk
D3DXLoadSurfaceFromFile( m_pCursorSurf, 0, 0, "plusCursor.dds", 0, D3DX_FILTER_NONE, 0, 0 );
// Set the hotspot for the cursor
g_pDevice->SetCursorProperties( 8, 8, m_pCursorSurf );

In addition to those changes to engine.cpp you will also need to add lines of code like the ones shown on p. 736. I don't use g_Keyboard, and I moved the two method calls for g_Mouse to Display::tick().

Some people have reported problems with the cursor not tracking the same as an image drawn at the cursor's location. I think that if you set up the custom cursor correctly you will not have that problem.

Cursor Problems in Windows XP

If you use the CZenMouse::Initialize function that comes with the book you will not see the cursor if you are running in Windows XP. You can fix that problem by declaring the mouse cooperative level as nonexclusive, as a member of the class reported in the Direct X forum.

To make the change, find CZenMouse::Initialize in engine.cpp and change DISCL_EXCLUSIVE to DISCL_NONEXCLUSIVE.

This is not necessarily the best fix for this problem, but it is probably the easiest. If we get a chance we will look at other possibilities later.