Class dxInput wraps the mouse, keyboard and joystick functionality of DirectInput. It also provides for input capture and replay, and has a slightly better random number generator than the standard C one. It should be used to generate ALL of the input information that goes into a unique game session - when you use dxInput to capture a game session, it will store to file the mouse and key states for every update, as well as the current random generator seed, so everything can be reproduced exactly (so long as the game itself remembers the original positions and states of all the objects).

IMPORTANT: dxInput extends the Singleton class. The Singleton class ensures that only a single instance of dxInput ever gets used. This imposes some restrictions on how to obtain a pointer to the input object --

If you're coding in class StarControl, just use the m_dxInput member variable.
If you're coding in another class, and have included dxInput.h, then to obtain a pointer to a dxInput object, call the static function dxInput::GetSingleton();. i.e.:

dxInput *pInputMgr = dxInput::GetSingleton();

Now pInputMgr points to the same instance as the member variable in CD3DGame.

 




For the mouse button functions, 0 is the left mouse button, 1 the right, 2 usually the middle..

   

    /////////////////////////////////////////////////////////////////////////
    // Key state accessors

    BOOL KeyDown( BYTE key )        { return m_keyDown[key]; }
    BOOL KeyPressed( BYTE key )     { return (m_keyDown[key] && !m_lastKeyDown[key]); }
    BOOL KeyReleased( BYTE key )    { return (!m_keyDown[key] && m_lastKeyDown[key]); }

    BOOL IsKeyboardAcquired()       { return m_bKeyboardAcquired; }


    /////////////////////////////////////////////////////////////////////////
    // Mouse state accessors

    INT  MouseDX()                      { return m_mouseDX; }
    INT  MouseDY()                      { return m_mouseDY; }
    INT  MouseDZ()                      { return m_mouseDZ; }

    BOOL MouseButtonDown( INT button )  { return m_bMouseButDown[button];   }
    BOOL MouseButtonClk( INT button )   { return m_bMouseButClk[button];    }
    BOOL MouseButtonDblClk( INT button ){ return m_bMouseButDblClk[button]; }

    BOOL IsMouseAcquired()              { return m_bMouseAcquired; }

 

 

 

 

 

 

 

 

 

 



Capture and replay... When replaying a capture file, the dxInput::Update() function just takes input states from the file instead of polling the mouse and keyboard. When you're capturing, AppendInputState should be called every tick.


   

    /////////////////////////////////////////////////////////////////////////
    // Capture/Replay

    BOOL AppendInputState();    // add state to current capture

    VOID StartCapture();
    VOID PauseCapture();
    VOID ResumeCapture();
    VOID StopCapture();

    BOOL SaveCaptureFile( LPSTR filename );


    VOID StartReplay();
    VOID PauseReplay();
    VOID ResumeReplay();
    VOID StopReplay();

    BOOL LoadCaptureFile( LPSTR filename );


    BOOL IsRecording()  { return m_bRecording; }
    BOOL IsReplaying()  { return m_bReplaying; }

 

 

 

 

 

 

 

 

 

 

 

 

GetLapTimerInstance returns a "lap timer" on the main game timer. Take a look at the dxLapTimer class - all it does is give elapsed times, based on the value of its "parent" timer. When possible, use a lap timer instead of a regular one, since they don't need to poll the performance timer on the motherboard.

GetLastFrameTime returns the, umm, last frame time. The main game loop handles the updating of this value, so never pass TRUE to it.


   

    /////////////////////////////////////////////////////////////////////////
    // Timing functions

    VOID       StartGameTimer()      { m_gameTimer.Start(); }
    VOID       StopGameTimer()       { m_gameTimer.Stop();  }
    VOID       ResetGameTimer()      { m_gameTimer.Reset(); }
    DOUBLE     GetGameTime()         { return m_gameTimer.GetTime(); }

    dxLapTimer* GetLapTimerInstance(){ return new dxLapTimer( &m_gameTimer ); }

    DOUBLE GetLastFrameTime( BOOL bUpdate = FALSE ) { 
        if ( bUpdate )
            m_lastFrameTime = m_gameTimer.GetElapsedTime();
        return m_lastFrameTime;
    }

 

 

 

 

 

 

 

 

GetRand returns a random number between 0.0 and 1.0, inclusive. GetRandInt returns a random integer between min and max, inclusive. dxInput will seed itself.


   

    /////////////////////////////////////////////////////////////////////////
    // Random number generator

    DOUBLE GetRand();
    INT GetRandInt(int min, int max);