1
\$\begingroup\$

I am a beginner making my second game (simple low graphics FPS), in Visual C++ 2010. So far, I only know how to use keyboard controls. I've had source files like keyboard.h and keyboard.ccp, responding to callback functions like kbd.UpIspressed().

How can I do this with mouse controls?

\$\endgroup\$
  • 2
    \$\begingroup\$ So far, we have nothing to work with. We don't know what you're using. Please include what information you can if you want an answer. \$\endgroup\$ – Magus Mar 18 '14 at 18:37
  • \$\begingroup\$ You could catch windows messages or use something like Directinput for simple mouse operations. \$\endgroup\$ – János Turánszki Mar 18 '14 at 18:40
  • \$\begingroup\$ i got this series of tutorials from youtube its 'DirectX game programming...' by ChilliTomato some guy, He had me download a ready-made set of files some Framework ( some Black screen: where all the pixels plotted are displayed). \$\endgroup\$ – Captaincomic Mar 18 '14 at 18:52
  • \$\begingroup\$ You could try using the thing in the framework that does something else, and maybe the other too. \$\endgroup\$ – Magus Mar 18 '14 at 18:59
  • 2
    \$\begingroup\$ My previous comment was designed to leave out all detail that could be used to make it understandable. This site exists for you upon becoming stuck to ask us a question, after having researched what you can, to help you find a solution. If you don't know the problem, we most certainly do not. Context is everything, friend, and my previous comment only contains slightly less than your question. \$\endgroup\$ – Magus Mar 18 '14 at 19:05
3
\$\begingroup\$

If you're using win32 then you can handle mouse and keyboard events using RawInput. Info can be found on MSDN.

You would handle windows messages for raw input devices in the window process. For example, if handling the input for a keyboard and mouse, register those devices in the WM_CREATE case of your application window process like so:

switch( uMsg ){ 
case WM_CREATE:
{
    // register raw input devices
    RAWINPUTDEVICE rID[2];

    // Keyboard
    rID[0].usUsagePage = 1;
    rID[0].usUsage = 6;
    rID[0].dwFlags = 0;
    rID[0].hwndTarget=NULL;

    // Mouse
    rID[1].usUsagePage = 1;
    rID[1].usUsage = 2;
    rID[1].dwFlags = 0;
    rID[1].hwndTarget = hWnd;
    if ( !RegisterRawInputDevices( rID, 2, sizeof( RAWINPUTDEVICE ) ) )
    {
        // handle failure
    }
    ...
    break;
}

Then in the same process handle the raw input messages with WM_INPUT like so:

switch( uMsg )
{
    ...
    case WM_INPUT:
    {       
        RAWINPUT *pRI = NULL;

        // Determine how big the buffer should be
        UINT iBuffer;
        GetRawInputData( ( HRAWINPUT )lParam, RID_INPUT, NULL, &iBuffer, sizeof( RAWINPUTHEADER ) );

        // Allocate a buffer with enough size to hold the raw input data
        LPBYTE lpb = new BYTE[ iBuffer ];
        if( lpb == NULL )
            return 0;

        // Get the raw input data
        UINT readSize = GetRawInputData( ( HRAWINPUT )lParam, RID_INPUT, lpb, &iBuffer, sizeof( RAWINPUTHEADER ) ) ;

        // Validate that read size is as expected
        if( readSize != iBuffer )
            puts( "ERROR: GetRawInputData didn't return correct size!" ) ;
        pRI = ( RAWINPUT* )lpb;             

        // Process the Mouse Messages
        if( pRI->header.dwType== RIM_TYPEMOUSE )
        {
            raw.riProcessMouseMessage( &pRI->data.mouse );
        }

        // Process the Keyboard Messages
        if( pRI->header.dwType== RIM_TYPEKEYBOARD )
        {
            raw.riProcessKeyboardMessage( &pRI->data.keyboard );
        }

        // Destroy the Raw Input Data and Return
        SAFE_DELETE( lpb );
        return 1;
    }
}

The 'raw.riProcessMouseMessage() and raw.riProcessKeyboardMessage() functions come from a class I use to keep track of raw input keyboard and mouse states...I found this on a raw input tutorial( that I can't find right now ). Here are the function definitions:

USHORT keyState[256];
short mouseState[5];
short mzr;
long mxr, myr;

USHORT MASKDOWN[ 5 ] = { RI_MOUSE_LEFT_BUTTON_DOWN, 
RI_MOUSE_RIGHT_BUTTON_DOWN, 
RI_MOUSE_MIDDLE_BUTTON_DOWN, 
RI_MOUSE_BUTTON_4_DOWN, 
RI_MOUSE_BUTTON_5_DOWN };

USHORT MASKUP[ 5 ] = { RI_MOUSE_LEFT_BUTTON_UP, 
RI_MOUSE_RIGHT_BUTTON_UP, 
RI_MOUSE_MIDDLE_BUTTON_UP, 
RI_MOUSE_BUTTON_4_UP, 
RI_MOUSE_BUTTON_5_UP };

void riManager::riProcessMouseMessage( const RAWMOUSE *rmouse )
{
    // Store Mouse Button States
    for(int i = 0; i < 5; i++)
    {       
        if( MASKDOWN[ i ] & rmouse->usButtonFlags )
            mouseState[ i ] = 1;
        else if( MASKUP[ i ] & rmouse->usButtonFlags )
            mouseState[ i ] = 0;            
    }
    // Check Mouse Position Relative Motion
    if ( MOUSE_MOVE_RELATIVE == rmouse->usFlags )   
    {
        mxr = rmouse->lLastX ;      
        myr = rmouse->lLastY ;  
    }

    // Check Mouse Wheel Relative Motion
    mzr = ( RI_MOUSE_WHEEL & rmouse->usButtonFlags ) ? ( short )( rmouse->usButtonData ) : 0;
}
void riManager::riProcessKeyboardMessage( const RAWKEYBOARD *rkey )
{   
    // Store New Key State
    keyState[ rkey->VKey ] &= 0xfe; 
    keyState[ rkey->VKey ] |= 1 - ( RI_KEY_BREAK & rkey->Flags );
}

It will be up to you to manage these variables. Build a class to hold the variables, then you can access the key and mouse states at will.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Somewhere in your code you should already have a message pump. Simply add WM_MOUSEMOVE as one of the message types you handle and check the message parameters to determine mouse position. Alternatively, you can use raw input if you want to support ultra low-latency or sub-pixel motion.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.