My DirectInput solution

This is a discussion on My DirectInput solution within the Game Programming forums, part of the General Programming Boards category; CDInput.h Code: #ifndef _CDINPUT_ #define _CDINPUT_ #define DIRECTINPUT_VERSION 0x0800 #include <dinput.h> class CDInput { protected: IDirectInput8 *_pDI8; public: CDInput() {}; ...

  1. #1
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586

    My DirectInput solution

    CDInput.h
    Code:
    #ifndef _CDINPUT_
    #define _CDINPUT_
    
    #define DIRECTINPUT_VERSION 0x0800
    #include <dinput.h>
    
    class CDInput
    {
        protected:
            IDirectInput8 *_pDI8;
        public:
            CDInput() {};
            ~CDInput() {if (_pDI8) _pDI8->Release();};
    
            HRESULT Init(HINSTANCE hinst);
    };
    #endif
    CDInput.cpp
    Code:
    #include "CDinput.h"
    
    HRESULT CDInput::Init(HINSTANCE hinst)
    {
        if (FAILED(DirectInput8Create(hinst, 
                                  DIRECTINPUT_VERSION, 
                                  IID_IDirectInput8, 
                                  (void**)&_pDI8, 
                                  NULL)))
        {
            ::MessageBox(0,"Failed to create DirectInput8 interface",0,0);
        }
        return true;
    }
    CKeyboard.h
    Code:
    #include "CDInput.h"
    
    //Inherits from virtual base class CDInput
    
    //Helpers
    #define DIKEYDOWN(data,n) (data[n] & 0x80)
    
    class CKeyboard:virtual public CDInput
    {
        protected:
            IDirectInputDevice8     *_pDID8;
        public:
            //So anyone can access our data - otherwise function overhead per use
            //could inline....but
            UCHAR           keystate[256];
            CKeyboard() {};
            ~CKeyboard() {if (_pDID8) _pDID8->Unacquire();
                          if (_pDID8) _pDID8->Release();};
        
            HRESULT Create(HWND hwnd);
            HRESULT Update(void);
               
    };
    CKeyboard.cpp
    Code:
    #include "CKeyboard.h"
    
    
    
    HRESULT CKeyboard::Create(HWND window)
    {
        
        //Create device
        if (FAILED(_pDI8->CreateDevice(GUID_SysKeyboard, 
                                       &_pDID8, 
                                       NULL)))
        {
            ::MessageBox(0,"Could not create keyboard device",0,0);
        }
        
        
        //Set cooperative level
        if (FAILED(_pDID8->SetCooperativeLevel(window,
                                               DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
        {
            ::MessageBox(0,"Could not set keyboard cooperative level",0,0);
        }
    
        //Set data format
        if (FAILED(_pDID8->SetDataFormat(&c_dfDIKeyboard)))
        {
            ::MessageBox(0,"Could not set keyboard data format",0,0);
        }
    
        //Acquire the keyboard
        if (FAILED(_pDID8->Acquire()))
        {
            ::MessageBox(0,"Could not acquire keyboard device",0,0);
        }
    
        return true;
    } 
    
    HRESULT CKeyboard::Update(void)
    {
        HRESULT hr;
       
        hr=(_pDID8->GetDeviceState(sizeof(keystate),(LPVOID)&keystate));
        if (hr==DIERR_INPUTLOST)
        {
            ::MessageBox(0,"Input lost",0,0);
        }
       
    
    
        return true;
    }
    CMouse.h
    Code:
    #include "CDInput.h"
    
    
    #define LMB         0
    #define RMB         1
    #define MMB         2
    
    class CMouse:virtual public CDInput
    {
        protected:
            IDirectInputDevice8     *_pDID8;
        public:
            CMouse() {};
            ~CMouse() {if (_pDID8) _pDID8->Unacquire();
            if (_pDID8) _pDID8->Release();};
            
            HRESULT Create(HWND window);
            //Share our data with the world
            DIMOUSESTATE            _mousestate;
            HRESULT Update(void);
    };
    CMouse.cpp
    Code:
    #include "CMouse.h"
    
    
    
    HRESULT CMouse::Create(HWND window)
    {
        if (FAILED(_pDI8->CreateDevice(GUID_SysMouse,&_pDID8,NULL)))
        {
            ::MessageBox(0,"Failed to create mouse device",0,0);
    
        }
    
        if (FAILED(_pDID8->SetCooperativeLevel(window,
                                               DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
        {
            ::MessageBox(0,"Failed to set mouse cooperative level",0,0);
        }
    
        if (FAILED(_pDID8->SetDataFormat(&c_dfDIMouse)))
        {
            ::MessageBox(0,"Failed to set mouse data format",0,0);
        }
    
        if (FAILED(_pDID8->Acquire()))
        {
            ::MessageBox(0,"Failed to acquire mouse",0,0);
        }
    
        ::ShowCursor(false);
        return true;
    }
    
    
    HRESULT CMouse::Update(void)
    {
        
        if (FAILED(_pDID8->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&_mousestate)))
        {
            return FALSE;
        }   else return true;
    }
    I'm not sure if this is creating 2 Direct Input interface pointers or not. Since CDInput is a virtual base...isn't there only 1 copy of it??

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    _pDI8 is uninitialized until you call Init(). Creating and destroying an object without calling Init() will result in undefined behaviour.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    I know that.


    But if you call init for each input device...does that create another instance of CDInput and thus create another _pDI8 interface?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    It shouldn't matter unless you are using multiple inheritance. I wouldn't do it this way, however.I think CDInput should be a singleton or you could just use a global IDirectInput device.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'Solution' and 'Project' usage in VC++
    By C+/- in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2007, 08:50 AM
  2. anyone know the solution?
    By heeroyung in forum C Programming
    Replies: 15
    Last Post: 09-30-2005, 06:46 AM
  3. DirectInput and Dev-C++
    By Niara in forum Game Programming
    Replies: 2
    Last Post: 03-11-2005, 09:20 AM
  4. My Unix/Linux SECURITY SOLUTION - pls read
    By bjdea1 in forum Linux Programming
    Replies: 3
    Last Post: 04-11-2004, 09:28 PM
  5. Solution - Israel and Palestine?
    By Vber in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-19-2003, 07:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21