• Advertisement
Sign in to follow this  

DirectInput not working, no errors or breaks?

This topic is 1862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there, I recently implemented 2 new classes into my project to give the camera realistic movement (eg, acceleration), I believe everything is setup correctly. The input class handles the keyboard input and acquires the keyboard state, the MoveCamera class calculates how the camera moves, but doesn't know anything about the keyboard states, it simply does maths.

Then it all comes together in my Graphics class, under a function called Handleinput(), although there no errors, the movement keys dont seem to be registering, and I have no idea whats up. I was hoping someone could take a look at it for me.

If you require to see more code do not hesitate to ask, thanks!


input.h

//--------------------------------------------------\\
// Input.h - handles user input \\
// Keyboard \\
//--------------------------------------------------\\
#ifndef _INPUT_H
#define _INPUT_H
//pre-processing directives
#define DIRECTINPUT_VERSION 0x0800
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")
#include <dinput.h>
class Input
{
public:
Input();
~Input();
bool Init(HINSTANCE, HWND, int, int);
bool Frame();
void Shutdown();
bool IsEscapePressed(); //Esc for quitting
bool IsUpPressed(); //Move camera up
bool IsDownPressed(); //Move camera down
bool IsWPressed(); //Move camera forward
bool IsSPressed(); //Move camera backwards
bool IsAPressed(); //Rotate camera left
bool IsDPressed(); //Rotate camera right
bool IsQPressed(); //Rotate camera up
bool IsEPressed(); //Rotate camera down
private:
bool ReadKeyboard();
IDirectInput8* directInput;
IDirectInputDevice8* keyboard;
unsigned char keyboardState[256];
int rScreenWidth, rScreenHeight;
};
#endif

input.cpp

//--------------------------------------------------\\
// Input.cpp - handles user input \\
// Keyboard &amp; Mouse devices \\
//--------------------------------------------------\\
#include "Input.h"
/* Constructor &amp; Deconstructor */
Input::Input()
{
directInput = 0;
keyboard = 0;
}
Input::~Input(){}
/* Initialize Input Devices */
bool Input::Init(HINSTANCE hinstance, HWND hwnd, int screenWidth, int screenHeight)
{
HRESULT r;
//Store the screen size which will be used for positioning the mouse cursor.
rScreenWidth = screenWidth;
rScreenHeight = screenHeight;
//Initialize the input device - Keyboard
r = DirectInput8Create(hinstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&amp;directInput, NULL);
if(FAILED(r))
{return false;}
//Create device for keyboard
r = directInput->CreateDevice(GUID_SysKeyboard, &amp;keyboard, NULL);
if(FAILED(r))
{return false;}
//Set the data format
r = keyboard->SetDataFormat(&amp;c_dfDIKeyboard);
if(FAILED(r))
{return false;}
//Set the key strokes to be exclusive to this application
r = keyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
if(FAILED(r))
{return false;}
//Acquire keyboard
r = keyboard->Acquire();
if(FAILED(r))
{return false;}
return true;
}
/* Update and Process each frame the Input components */
bool Input::Frame()
{
bool r;
r = ReadKeyboard();
if(!r)
{return false;}
return true;
}
/* Read the Keyboard device */
bool Input::ReadKeyboard()
{
HRESULT r;
r = keyboard->GetDeviceState(sizeof(keyboardState), (LPVOID)&amp;keyboardState);
if(FAILED(r))
{
//If the keyboard lost focus or was not acquired then try to get control back.
if((r == DIERR_INPUTLOST) || (r == DIERR_NOTACQUIRED))
{keyboard->Acquire();}
else
{return false;}
}

return true;
}
/* Get Key presses */
/* Escape Key */
bool Input::IsEscapePressed()
{
if(keyboardState[DIK_ESCAPE] &amp; 0x80)
{return true;}
return false;
}
/* Up Arrow Key */
bool Input::IsUpPressed()
{
if(keyboardState[DIK_UP] &amp; 0x80)
{return true;}
return false;
}
/* Down Arrow Key */
bool Input::IsDownPressed()
{
if(keyboardState[DIK_DOWN] &amp; 0x80)
{return true;}
return false;
}
/* W Key */
bool Input::IsWPressed()
{
if(keyboardState[DIK_W] &amp; 0x80)
{return true;}
return false;
}
/* S Key */
bool Input::IsSPressed()
{
if(keyboardState[DIK_S] &amp; 0x80)
{return true;}
return false;
}
/* A Key */
bool Input::IsAPressed()
{
if(keyboardState[DIK_A] &amp; 0x80)
{return true;}
return false;
}
/* D Key */
bool Input::IsDPressed()
{
if(keyboardState[DIK_D] &amp; 0x80)
{return true;}
return false;
}
/* Q Key */
bool Input::IsQPressed()
{
if(keyboardState[DIK_Q] &amp; 0x80)
{return true;}
return false;
}
/* E Key */
bool Input::IsEPressed()
{
if(keyboardState[DIK_E] &amp; 0x80)
{return true;}
return false;
}
/* Shutdown, Release and Delete the Input objects we are using */
void Input::Shutdown()
{
if(keyboard)
{
keyboard->Unacquire();
keyboard->Release();
keyboard = 0;
}
if(directInput)
{
directInput->Release();
directInput = 0;
}
return;
}



MoveCamera.h

//--------------------------------------------------\\
// MoveCamera.h - Allows the camera to move \\
// \\
//--------------------------------------------------\\
#ifndef _MOVECAMERA_H
#define _MOVECAMERA_H
#include <math.h>
class MoveCamera
{
public:
MoveCamera();
~MoveCamera();
void SetPos(float, float, float);
void SetRot(float, float, float);
void GetPos(float&, float&, float&);
void GetRot(float&, float&, float&);
void SetFrameTime(float); //Keep the camera in sync with the application
/* Movement Keys */
void MoveForward(bool); //W
void MoveBackwards(bool); //S
void MoveUp(bool); //Q
void MoveDown(bool); //E
/* Rotation keys */
void TurnLeft(bool); //A
void TurnRight(bool); //D
void LookUp(bool); //Up Arrow
void LookDown(bool); //Down Arrow
private:
float posX, posY, posZ;
float rotX, rotY, rotZ;
float frameTime;
float forwardSpeed; //W
float backwardSpeed; //S
float upwardSpeed; //Q
float downwardSpeed; //E
float LookLeftSpeed; //A
float LookRightSpeed; //D
float LookDownSpeed; //Up Arrow
float LookUpSpeed; //Down Arrow
};
#endif



MoveCamera.cpp

//--------------------------------------------------\\
// MoveCamera.cpp - Allows the camera to move \\
// \\
//--------------------------------------------------\\
#include "MoveCamera.h"
/* Constructor & Deconstructor */
MoveCamera::MoveCamera()
{
posX = 0.0f;
posY = 0.0f;
posZ = 0.0f;
rotX = 0.0f;
rotY = 0.0f;
rotZ = 0.0f;
frameTime = 0.0f;
forwardSpeed = 0.0f;
backwardSpeed = 0.0f;
upwardSpeed = 0.0f;
downwardSpeed = 0.0f;
LookLeftSpeed = 0.0f;
LookRightSpeed = 0.0f;
LookDownSpeed = 0.0f;
LookUpSpeed = 0.0f;
}
MoveCamera::~MoveCamera(){}
/* Set the camera Position */
void MoveCamera::SetPos(float x, float y, float z)
{
posX = x;
posY = y;
posZ = z;
return;
}
/* Set the camera Rotation */
void MoveCamera::SetRot(float x, float y, float z)
{
rotX = x;
rotY = y;
rotZ = z;
return;
}
/* Get the camera Position */
void MoveCamera::GetPos(float& x, float& y, float& z)
{
x = posX;
y = posY;
z = posZ;
return;
}
/* Get the camera Rotation */
void MoveCamera::GetRot(float& x, float& y, float& z)
{
x = rotX;
y = rotY;
z = rotZ;
return;
}
/* Set Frame Time to sync camera movement to FPS */
void MoveCamera::SetFrameTime(float time)
{
frameTime = time;
return;
}
/*
The functions here use simple math to give the movement a realistic acceleration.
The speed of each direction is added to the frameTime to sync is probably.
Maths adopted from the Rastertek.com tutorials.
*/
/* Move Forward */
void MoveCamera::MoveForward(bool keydown)
{
float rads;
if(keydown)
{
forwardSpeed += frameTime * 0.001f;
if(forwardSpeed > (frameTime * 0.03f))
{forwardSpeed = frameTime * 0.03f;}
}
else
{
forwardSpeed -= frameTime * 0.0007f;
if(forwardSpeed < 0.0f)
{forwardSpeed = 0.0f;}
}
//Update new position
rads = rotY * 0.0174532925f;
posX += sinf(rads) * forwardSpeed;
posZ += cosf(rads) * forwardSpeed;
return;
}
/* Move Backwards */
void MoveCamera::MoveBackwards(bool keydown)
{
float rads;
if(keydown)
{
backwardSpeed += frameTime * 0.001f;
if(backwardSpeed > (frameTime * 0.03f))
{backwardSpeed = frameTime * 0.03f;}
}
else
{
backwardSpeed -= frameTime * 0.0007f;
if(backwardSpeed < 0.0f)
{backwardSpeed = 0.0f;}
}
//Update new position
rads = rotY * 0.0174532925f;
posX -= sinf(rads) * backwardSpeed;
posZ -= cosf(rads) * backwardSpeed;
return;
}
/* Move Upwards */
void MoveCamera::MoveUp(bool keydown)
{
if(keydown)
{
upwardSpeed += frameTime * 0.003f;
if(upwardSpeed > (frameTime * 0.03f))
{upwardSpeed = frameTime * 0.03f;}
}
else
{
upwardSpeed -= frameTime * 0.002f;
if(upwardSpeed < 0.0f)
{upwardSpeed = 0.0f;}
}
//Update new position
posY += upwardSpeed;
return;
}
/* Move Downwards */
void MoveCamera::MoveDown(bool keydown)
{
if(keydown)
{
downwardSpeed += frameTime * 0.003f;
if(downwardSpeed > (frameTime * 0.03f))
{downwardSpeed = frameTime * 0.03f;}
}
else
{
downwardSpeed -= frameTime * 0.002f;
if(downwardSpeed < 0.0f)
{downwardSpeed = 0.0f;}
}
//Update new position
posY -= downwardSpeed;
return;
}
/* Rotate Left */
void MoveCamera::TurnLeft(bool keydown)
{
if(keydown)
{
LookLeftSpeed += frameTime * 0.01f;
if(LookLeftSpeed > (frameTime * 0.15f))
{LookLeftSpeed = frameTime * 0.15f;}
}
else
{
LookLeftSpeed -= frameTime * 0.005f;
if(LookLeftSpeed < 0.0f)
{LookLeftSpeed = 0.0f;}
}
//Update new position
rotY -= LookLeftSpeed;
//Keep the value of rotY between 0-360
if(rotY < 0.0f)
{rotY += 360.0f;}
return;
}
/* Rotate Right */
void MoveCamera::TurnRight(bool keydown)
{
if(keydown)
{
LookRightSpeed += frameTime * 0.01f;
if(LookRightSpeed > (frameTime * 0.15f))
{LookRightSpeed = frameTime * 0.15f;}
}
else
{
LookRightSpeed -= frameTime* 0.005f;
if(LookRightSpeed < 0.0f)
{LookRightSpeed = 0.0f;}
}
//Update new position
rotY += LookRightSpeed;
//Keep the value of rotY between 0-360
if(rotY > 360.0f)
{rotY -= 360.0f;}
return;
}
/* Rotate Down */
void MoveCamera::LookDown(bool keydown)
{
if(keydown)
{
LookDownSpeed += frameTime * 0.01f;
if(LookDownSpeed > (frameTime * 0.15f))
{LookDownSpeed = frameTime * 0.15f;}
}
else
{
LookDownSpeed -= frameTime* 0.005f;
if(LookDownSpeed < 0.0f)
{LookDownSpeed = 0.0f;}
}
//Update the position
rotX += LookDownSpeed;
//Keep the rotation maximum 90 degrees
if(rotX < -90.0f)
{rotX = -90.0f;}
return;
}
/* Rotate Up */
void MoveCamera::LookUp(bool keydown)
{
if(keydown)
{
LookUpSpeed += frameTime * 0.01f;
if(LookUpSpeed > (frameTime * 0.15f))
{LookUpSpeed = frameTime * 0.15f;}
}
else
{
LookUpSpeed -= frameTime* 0.005f;
if(LookUpSpeed < 0.0f)
{LookUpSpeed = 0.0f;}
}
//Update the position
rotX -= LookUpSpeed;
//Keep the rotation maximum 90 degrees
if(rotX > 90.0f)
{rotX = 90.0f;}
return;
}


The handle input function inside Graphics.cpp

/* Handle Input handles are user input via the keyboard */
bool Graphics::HandleInput(float frameTime)
{
bool keyDown;
float posX, posY, posZ;
float rotX, rotY, rotZ;
//Set the frame time used for updating positions
movecamera->SetFrameTime(frameTime);
//Handle the Key strokes
keyDown = input->IsAPressed();
movecamera->TurnLeft(keyDown);
keyDown = input->IsDPressed();
movecamera->TurnRight(keyDown);
keyDown = input->IsWPressed();
movecamera->MoveForward(keyDown);
keyDown = input->IsSPressed();
movecamera->MoveBackwards(keyDown);
keyDown = input->IsQPressed();
movecamera->MoveUp(keyDown);
keyDown = input->IsEPressed();
movecamera->MoveDown(keyDown);
keyDown = input->IsUpPressed();
movecamera->LookUp(keyDown);
keyDown = input->IsDownPressed();
movecamera->LookDown(keyDown);
//Get position &amp; rotation
movecamera->GetPos(posX, posY, posZ);
movecamera->GetRot(rotX, rotY, rotZ);
//Set position of the camera
camera->SetPosition(posX, posY, posZ);
camera->SetRotation(rotX, rotY, rotZ);
return true;
}




Sorry for the very long spinets of code, but would be extremely grateful to anyone who could tell me whats going wrong

Share this post


Link to post
Share on other sites
Advertisement

DirectInput is deprecated for keyboard input, so unless you have a good reason, don't use it for this in a new project.


DirectInput may be deprecated but that doesn't mean that it's broken. In this case the OP has done something wrong and there's a risk that the error will still be there even after moving to something else.

Share this post


Link to post
Share on other sites
Also, you need to disambiguate between keyboard input as in gamepad keys, or actual text entry. It is not advisable to use for text entry, but as gamepad replacement it's perfectly fine. DirectInput is the only API that gets you access to every input device under one hood.

To the OP: Are you calling input->Frame() somewhere?

Share this post


Link to post
Share on other sites
Hi,
Some pointers from my side;
- I would do the checking for specific keys in a 'CheckInput' function in your main programm,
resulting in a specific camera movement or rotation being (I'll post some pseudo code).
This way you prevent executing all functions that are possible with a specific key.
- is there a specific reason your using DISCL_FOREGROUND and DISCL_EXCLUSIVE?
(I use DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)
- I would check for input in my main loop, where I also render

[source lang="cpp"]// main loop of the program
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if(active & !_d3d.mDeviceLost)
{
_timer.CalcFps();

// handle keyboard and mouse input
_dinput.CheckKeyboard();
_dinput.CheckMouse();
HandleInput();

// game logics here
// render 3d scene

}
}
}

// global function in main cpp file

void HandleInput()
{
if(_dinput.mBuffer[DIK_W] & 0x80) cam.MoveForward(0.1f * _timer.mSpeedFactor);
if(_dinput.mBuffer[DIK_S] & 0x80) cam.MoveForward(-0.1f * _timer.mSpeedFactor);
}

// where the checkmouse and check keyboard functions are part of your directinput class
// something with Acquire for both 'dins' and GetDeviceState, save results in buffers, i.e.
// _dinput.mBuffer[/source]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement