home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 July & August
/
PCWorld_2006-07-08_cd.bin
/
temacd
/
planearcade
/
planearcade.exe
/
Tank3.bmp
/
input.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2004-10-24
|
8KB
|
381 lines
#include "main.h"
//-------------------------------------------------------------
//GLOBAL
//--------------------------------------------------------------
INPUT Input;
//------------------------------------------------------------------
// Name: INPUT()
// Desc: konstruktor
//------------------------------------------------------------------
INPUT::INPUT()
{
m_pKeyboard = NULL;
m_pMouse = NULL;
m_pDirectInput = NULL;
//inicializacia poli
for (int i=0;i<256;i++)
{
KeyPRESS[i] = false;
KeyDOWN[i] = false;
}
}
//------------------------------------------------------------------
// Name: Initialize()
// Desc: inicializacia Dinput
//------------------------------------------------------------------
void INPUT::Initialize()
{
LogPrint("Nastavujem Direct Input");
//vytvori DirectInput Objekt
if(FAILED(DirectInput8Create(hIns, DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&m_pDirectInput, NULL)))
{
LogPrint(" Nepodarilo sa vytvori¥ m_pDirectInput");
}
else
{
LogPrint(" m_pDirectInput vytvoreny");
}
//KEYBOARD =======================================================================
//Create the keyboard device object
if(FAILED(m_pDirectInput->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL)))
{
LogPrint(" Nepodarilo sa nastavit klavesnicu");
}
else
{
LogPrint(" Klavesnica nastavena");
}
//Set the data format for the keyboard
if(FAILED(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard)))
{
LogPrint(" Nepodarilo sa nastavit format klavesnice");
}
else
{
LogPrint(" Format klavesnice nastaveny");
}
//Set the cooperative level for the keyboard
if(FAILED(m_pKeyboard->SetCooperativeLevel(hWnd,
DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
{
LogPrint(" nepodarilo sa nastavit ccoperative level pre klavesnicu");
}
else
{
LogPrint(" cooperative level pre klavesnicu nastaveny");
}
//Acquire the keyboard
if(m_pKeyboard)
{
LogPrint(" Acquire klavesnica");
m_pKeyboard->Acquire();
}
//JOYSTICK========================================================================
if (JoyEnabled)
{
//Create joystick device
if(FAILED(m_pDirectInput->CreateDevice( GUID_Joystick ,
&m_pJoystick,
NULL )))
{
LogPrint(" Nepodarilo sa nastavit joystick");
}
else
{
LogPrint(" Joystick nastaveny");
}
if (m_pJoystick != NULL)
{
//Set the data format for the joystick
if(FAILED(m_pJoystick->SetDataFormat(&c_dfDIJoystick)))
{
LogPrint(" Nepodarilo sa nastavit format joystick");
}
else
{
LogPrint(" Format joysticku nastaveny");
}
//Set the cooperative level for the joystick
if(FAILED(m_pJoystick->SetCooperativeLevel(hWnd,
DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
{
LogPrint(" nepodarilo sa nastavit ccoperative level pre joystick");
}
else
{
LogPrint(" cooperative level pre joystick nastaveny");
}
//Acquire the joystick
if(m_pJoystick)
{
LogPrint(" Acquire joystick");
m_pJoystick->Acquire();
}
}
}
//MOUSE =======================================================================
//Create the mouse device object
if(FAILED(m_pDirectInput->CreateDevice(GUID_SysMouse, &m_pMouse, NULL)))
{
LogPrint(" Nepodarilo sa nastavit mys ");
}
else
{
LogPrint(" Mys nastavena");
}
//Set the data format for the mouse
if(FAILED(m_pMouse->SetDataFormat(&c_dfDIMouse)))
{
LogPrint(" Nepodarilo sa nastavit formaty mysi");
}
else
{
LogPrint(" Format myse nastavena");
}
//Set the cooperative level for the mouse
if(FAILED(m_pMouse->SetCooperativeLevel(hWnd,
DISCL_FOREGROUND|DISCL_NONEXCLUSIVE)))
{
LogPrint(" nepodarilo sa nastavit ccoperative level pre mys");
}
else
{
LogPrint(" cooperative level pre mys nastaveny");
}
//Acquire the mouse
if(m_pMouse)
{
LogPrint(" Acquire mys");
m_pMouse->Acquire();
}
}
//------------------------------------------------------------------
// Name: CleanUp()
// Desc: Odstrani DInput
//------------------------------------------------------------------
void INPUT::CleanUp()
{
if(m_pKeyboard)
{
m_pKeyboard->Unacquire();
m_pKeyboard->Release();
}
if(m_pMouse)
{
m_pMouse->Unacquire();
m_pMouse->Release();
}
if(m_pJoystick)
{
m_pJoystick->Unacquire();
m_pJoystick->Release();
}
if(m_pDirectInput != NULL)
m_pDirectInput->Release();
}
//------------------------------------------------------------------
// Name: RefreshKeyboard()
// Desc: Ziska stlacene klavesy
//------------------------------------------------------------------
void INPUT::RefreshKeyboard()
{
int i;
if (m_pKeyboard == NULL)
return;
//ziskanie KeyDown
if( FAILED(m_pKeyboard->GetDeviceState(sizeof(KeyDown),
(LPVOID)&KeyDown)))
{
HRESULT hr = m_pKeyboard->Acquire();
while( hr == DIERR_INPUTLOST )
hr = m_pKeyboard->Acquire();
}
//ziskanie KeyDOWN a KeyPRESS
for (i=0;i<256;i++)
{
if (KeyDOWN[i] == false)
{
KeyPRESS[i] = (KEYDOWN(KeyDown,i));
}
else
{
KeyPRESS[i] = false;
}
KeyDOWN[i] = (KEYDOWN(KeyDown,i));
}
}
//------------------------------------------------------------------
// Name: RefreshKeyboard()
// Desc: Ziska stlacene klavesy
//------------------------------------------------------------------
void INPUT::RefreshJoystick()
{
int i;
if (JoyEnabled == 0)
return;
if (m_pJoystick == NULL)
return;
DIJOYSTATE JoyState;
m_pJoystick->Poll();
if( FAILED(m_pJoystick->GetDeviceState( sizeof(DIJOYSTATE),
&JoyState )))
{
ZeroMemory( &JoyState, sizeof(DIJOYSTATE) );
HRESULT hr = m_pJoystick->Acquire();
while( hr == DIERR_INPUTLOST )
hr = m_pJoystick->Acquire();
}
JoyX = ((float)JoyState.lX);
JoyY = ((float)JoyState.lY);
JoyX = (JoyX - (65535/2.0f))/(65535/2.0f);
JoyY = (JoyY - (65535/2.0f))/(65535/2.0f);
float cX = 1.0f;
float cY = 1.0f;
if (JoyX < 0.0f) cX = -1.0f;
if (JoyY < 0.0f) cY = -1.0f;
JoyX = JoyX * JoyX * cX;
JoyY = JoyY * JoyY * cY;
JoyX *= 1.4f;
JoyY *= 1.2f;
if ( JoyState.rgbButtons[0] & 0x80 )
JoyButton1Down = true;
else
JoyButton1Down = false;
if ( JoyState.rgbButtons[1] & 0x80 )
JoyButton2Down = true;
else
JoyButton2Down = false;
}
//------------------------------------------------------------------
// Name: RefreshMouse()
// Desc: Ziska info o mysi
//------------------------------------------------------------------
void INPUT::RefreshMouse()
{
#define MOUSEBUTTONDOWN(key) (key & 0x80)
#define MOUSEBUTTON_LEFT 0
#define MOUSEBUTTON_RIGHT 1
#define MOUSEBUTTON_MIDDLE 2
if (m_pMouse == NULL)
return;
DIMOUSESTATE MouseState;
if( FAILED(m_pMouse->GetDeviceState(sizeof(MouseState),
(LPVOID)&MouseState)))
{
ZeroMemory( &MouseState, sizeof(DIMOUSESTATE) );
HRESULT hr = m_pMouse->Acquire();
while( hr == DIERR_INPUTLOST )
hr = m_pMouse->Acquire();
}
//press-reset
MouseLeftPress = 0;
MouseRightPress = 0;
//Lave tlacitko
if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_LEFT]))
{
if (MouseLeftDown == 0)
MouseLeftPress = 1;
MouseLeftDown = 1;
}
else
{
MouseLeftDown = 0;
}
//Prave tlacitko
if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_RIGHT]))
{
if (MouseRightDown == 0)
MouseRightPress = 1;
MouseRightDown = 1;
}
else
{
MouseRightDown = 0;
}
//solid
POINT MousePos;
ScreenToClient(hWnd,&MousePos);
GetCursorPos(&MousePos);
Mouse.X = (float) MousePos.x ;
Mouse.Y = (float) MousePos.y ;
Mouse.Z = 0.0f;
//relativne
MouseRelative.X = (float)MouseState.lX;
MouseRelative.Y = (float)MouseState.lY;
MouseRelative.Z = (float)MouseState.lZ;
}