• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Tim Lawton

DirectInput not working, no errors or breaks?

5 posts in this topic

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
[CODE]
//--------------------------------------------------\\
// 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
[/CODE]
input.cpp
[CODE]
//--------------------------------------------------\\
// 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;
}
[/CODE]


MoveCamera.h
[CODE]
//--------------------------------------------------\\
// 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
[/CODE]


MoveCamera.cpp
[CODE]
//--------------------------------------------------\\
// 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;
}
[/CODE]

The handle input function inside Graphics.cpp
[CODE]
/* 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;
}
[/CODE]



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

Share this post


Link to post
Share on other sites
DirectInput is deprecated for keyboard input, so unless you have a good reason, don't use it for this in a new project.

[url="http://www.gamedev.net/blog/233/entry-1567278-reasons-not-to-use-directinput-for-keyboard-input/"]See this old thread.[/url]
0

Share this post


Link to post
Share on other sites
[quote name='jwezorek' timestamp='1355782142' post='5011831']
DirectInput is deprecated for keyboard input, so unless you have a good reason, don't use it for this in a new project.
[/quote]

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.
1

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?
1

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]
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0