Skip to content

Instantly share code, notes, and snippets.

/Input.cpp Secret

Created May 11, 2013 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/28be899570c72fe7ae98 to your computer and use it in GitHub Desktop.
Save anonymous/28be899570c72fe7ae98 to your computer and use it in GitHub Desktop.
Direct Input controler class header
//------------------------------------------------------------------------------
// Alien Pastry Engine Framework
//Copyright 2012
//wraps direct input
//------------------------------------------------------------------------------
#pragma once
#include "Input.h"
#include "error.h"
//------------------------------------------------------------------------------
//Type
Input::Input() : input(NULL), keyboard(NULL), mouse(NULL)
{
}
Input::Input(HWND wind) : input(NULL), keyboard(NULL), mouse(NULL)
{
keyboard = NULL;
mouse = NULL;
init(wind);
}
Input::~Input()
{
if(keyboard)
{
keyboard->Unacquire();
keyboard->Release();
}
if(mouse)
{
mouse->Unacquire();
mouse->Release();
}
if(input)
{
input->Release();
}
}
//all input
bool Input::init(HWND wind)
{
HRESULT err = 0;
//install system
err = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**) &input, NULL);
if(err)
{
throw Error(err, "CANNOT CREATE INPUT");
}
//create keyboard
err = input->CreateDevice(GUID_SysKeyboard, &keyboard, NULL);
if(err)
{
throw Error(err, "CANNOT CREATE keyboard");
}
keyboard->SetDataFormat(&c_dfDIKeyboard);
keyboard->SetCooperativeLevel(wind, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
keyboard->Acquire();
//create mouse
err = input->CreateDevice(GUID_SysMouse, &mouse, NULL);
if(err)
{
throw Error(err, "CANNOT CREATE mouse");
}
mouse->SetDataFormat(&c_dfDIMouse);
mouse->SetCooperativeLevel(wind, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
mouse->Acquire();
//any gamepads attached?
for(int ii = 0; ii < 4; ii++)
{
hasGamepad[ii] = isGamepadAttatched(ii);
hasGamepads = hasGamepad[0] || hasGamepad[1] || hasGamepad[2] || hasGamepad[3];
}
//initialize press stamp
pressStamp = 0;
return true;
}
void Input::strobe(void)
{
//hold on to the previous states for input stamping
memcpy(prevKeys, keys, sizeof(keys));
memcpy(prevGamepadState, gamepad, sizeof(gamepad));
//grabstatus of all keys and mouse buttons and refocus if needed
//gatehr with bool
//b = !b
HRESULT err;
err = keyboard->GetDeviceState(sizeof(keys), (LPVOID) &keys);
if((err == DIERR_INPUTLOST )|| (err == DIERR_NOTACQUIRED))
{
keyboard->Acquire();
}
err = mouse->GetDeviceState(sizeof(mouse_state), (LPVOID) &mouse_state);
if((err == DIERR_INPUTLOST )|| (err == DIERR_NOTACQUIRED))
{
mouse->Acquire();
}
//strobe controllers
if(hasGamepads)
{
for(int ii = 0; ii < 4; ii++)
{
ZeroMemory(&gamepad[ii], sizeof(XINPUT_STATE));
XINPUT_STATE state;
DWORD result = XInputGetState(ii, &state);
if(result == 0)
{
gamepad[ii] = state.Gamepad;
}
}
}
pressStamp++;
}
//Gamepads
bool Input::isGamepadAttatched(int padNum)
{
XINPUT_CAPABILITIES cap;
ZeroMemory(&cap, sizeof(XINPUT_CAPABILITIES));
XInputGetCapabilities(padNum, XINPUT_FLAG_GAMEPAD, &cap);
return (cap.SubType == XINPUT_DEVSUBTYPE_GAMEPAD);
}
void Input::vibrate(int padNum = 0, int amount = 65535)
{
XINPUT_VIBRATION vibration;
ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION));
vibration.wLeftMotorSpeed = amount;
vibration.wRightMotorSpeed = amount;
XInputSetState(padNum, &vibration);
}
int Input::isKeyDown(int key)
{
return keys[key] & 0x80;
};
bool Input::keyPressed(int key)
{
if(keys[key] & 0x80 && (keys[key] & 0x80) != (prevKeys[key] & 0x80))
{
return true;
}
else
{
return false;
}
}
bool Input::isGamePadButtonDown(int padNum, WORD button)
{
if(gamepad[padNum].wButtons & button)
{
return true;
}
return false;
}
bool Input::isGamePadButtonUp(int padNum, WORD button)
{
if(gamepad[padNum].wButtons & button)
{
return false;
}
return true;
}
bool Input::wasGamePadButtonPressed(int padNum, WORD button)
{
if((gamepad[padNum].wButtons & button) &
((gamepad[padNum].wButtons & button) != (prevGamepadState[padNum].wButtons & button)))
{
return true;
}
return false;
}
//------------------------------------------------------------------------------
// Alien Pastry Engine Framework
//Copyright 2012
//wraps direct input
//------------------------------------------------------------------------------
#pragma once
#include <dinput.h>
#include <XInput.h>
//------------------------------------------------------------------------------
class Input
{
LPDIRECTINPUT8 input;
LPDIRECTINPUTDEVICE8 keyboard;
char keys[256];
char prevKeys[256];
unsigned long pressStamp;
unsigned long keyPressStamp[256];
LPDIRECTINPUTDEVICE8 mouse;
DIMOUSESTATE mouse_state;
XINPUT_GAMEPAD gamepad[4];
XINPUT_GAMEPAD prevGamepadState[4];
bool hasGamepad[4];
bool hasGamepads;
public:
//Type
Input();
Input(HWND wind);
~Input();
//all input
bool init(HWND wind);
void strobe(void);
//keyboard
int isKeyDown(int key); //{return keys[key] & 0x80; };
bool keyPressed(int key);
//mouse
int isMouseDown(int button) {return mouse_state.rgbButtons[button] & 0x80; };
int mouseX() {return mouse_state.lX; };
int mouseY() {return mouse_state.lY; };
//Gamepads
bool isGamepadAttatched(int padNum);
void vibrate(int padNum, int ammount);
bool isGamePadButtonDown(int padNum, WORD button);
bool isGamePadButtonUp(int padNum, WORD button);
bool wasGamePadButtonPressed(int padNum, WORD button);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment