Debut input tuto

This commit is contained in:
GolfOcean334 2024-03-28 11:03:09 +01:00
parent 16db21608a
commit 41b2b9e024
2 changed files with 281 additions and 22 deletions

View File

@ -3,6 +3,9 @@
InputClass::InputClass()
{
m_directInput = 0;
m_keyboard = 0;
m_mouse = 0;
}
@ -15,40 +18,262 @@ InputClass::~InputClass()
{
}
void InputClass::Initialize()
bool InputClass::Initialize(HINSTANCE hinstance, HWND hwnd, int screenWidth, int screenHeight)
{
int i;
HRESULT result;
// Initialize all the keys to being released and not pressed.
for (i = 0; i < 256; i++)
// Store the screen size which will be used for positioning the mouse cursor.
m_screenWidth = screenWidth;
m_screenHeight = screenHeight;
// Initialize the location of the mouse on the screen.
m_mouseX = 0;
m_mouseY = 0;
// Initialize the main direct input interface.
result = DirectInput8Create(hinstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_directInput, NULL);
if (FAILED(result))
{
m_keys[i] = false;
return false;
}
// Initialize the direct input interface for the keyboard.
result = m_directInput->CreateDevice(GUID_SysKeyboard, &m_keyboard, NULL);
if (FAILED(result))
{
return false;
}
// Set the data format. In this case since it is a keyboard we can use the predefined data format.
result = m_keyboard->SetDataFormat(&c_dfDIKeyboard);
if (FAILED(result))
{
return false;
}
// Set the cooperative level of the keyboard to not share with other programs.
result = m_keyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
if (FAILED(result))
{
return false;
}
// Now acquire the keyboard.
result = m_keyboard->Acquire();
if (FAILED(result))
{
return false;
}
// Initialize the direct input interface for the mouse.
result = m_directInput->CreateDevice(GUID_SysMouse, &m_mouse, NULL);
if (FAILED(result))
{
return false;
}
// Set the data format for the mouse using the pre-defined mouse data format.
result = m_mouse->SetDataFormat(&c_dfDIMouse);
if (FAILED(result))
{
return false;
}
// Set the cooperative level of the mouse to share with other programs.
result = m_mouse->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
if (FAILED(result))
{
return false;
}
// Acquire the mouse.
result = m_mouse->Acquire();
if (FAILED(result))
{
return false;
}
return true;
}
//void InputClass::Initialize()
//{
// int i;
//
//
// // Initialize all the keys to being released and not pressed.
// for (i = 0; i < 256; i++)
// {
// m_keys[i] = false;
// }
//
// return;
//}
//void InputClass::KeyDown(unsigned int input)
//{
// // If a key is pressed then save that state in the key array.
// m_keys[input] = true;
// return;
//}
//
//
//void InputClass::KeyUp(unsigned int input)
//{
// // If a key is released then clear that state in the key array.
// m_keys[input] = false;
// return;
//}
//
//
//bool InputClass::IsKeyDown(unsigned int key)
//{
// // Return what state the key is in (pressed/not pressed).
// return m_keys[key];
//}
void InputClass::Shutdown()
{
// Release the mouse.
if (m_mouse)
{
m_mouse->Unacquire();
m_mouse->Release();
m_mouse = 0;
}
// Release the keyboard.
if (m_keyboard)
{
m_keyboard->Unacquire();
m_keyboard->Release();
m_keyboard = 0;
}
// Release the main interface to direct input.
if (m_directInput)
{
m_directInput->Release();
m_directInput = 0;
}
return;
}
void InputClass::KeyDown(unsigned int input)
bool InputClass::Frame()
{
// If a key is pressed then save that state in the key array.
m_keys[input] = true;
bool result;
// Read the current state of the keyboard.
result = ReadKeyboard();
if (!result)
{
return false;
}
// Read the current state of the mouse.
result = ReadMouse();
if (!result)
{
return false;
}
// Process the changes in the mouse and keyboard.
ProcessInput();
return true;
}
bool InputClass::ReadKeyboard()
{
HRESULT result;
// Read the keyboard device.
result = m_keyboard->GetDeviceState(sizeof(m_keyboardState), (LPVOID)&m_keyboardState);
if (FAILED(result))
{
// If the keyboard lost focus or was not acquired then try to get control back.
if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
{
m_keyboard->Acquire();
}
else
{
return false;
}
}
return true;
}
bool InputClass::ReadMouse()
{
HRESULT result;
// Read the mouse device.
result = m_mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&m_mouseState);
if (FAILED(result))
{
// If the mouse lost focus or was not acquired then try to get control back.
if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
{
m_mouse->Acquire();
}
else
{
return false;
}
}
return true;
}
void InputClass::ProcessInput()
{
// Update the location of the mouse cursor based on the change of the mouse location during the frame.
m_mouseX += m_mouseState.lX;
m_mouseY += m_mouseState.lY;
// Ensure the mouse location doesn't exceed the screen width or height.
if (m_mouseX < 0) { m_mouseX = 0; }
if (m_mouseY < 0) { m_mouseY = 0; }
if (m_mouseX > m_screenWidth) { m_mouseX = m_screenWidth; }
if (m_mouseY > m_screenHeight) { m_mouseY = m_screenHeight; }
return;
}
void InputClass::KeyUp(unsigned int input)
bool InputClass::IsEscapePressed()
{
// If a key is released then clear that state in the key array.
m_keys[input] = false;
// Do a bitwise and on the keyboard state to check if the escape key is currently being pressed.
if (m_keyboardState[DIK_ESCAPE] & 0x80)
{
return true;
}
return false;
}
void InputClass::GetMouseLocation(int& mouseX, int& mouseY)
{
mouseX = m_mouseX;
mouseY = m_mouseY;
return;
}
bool InputClass::IsKeyDown(unsigned int key)
bool InputClass::IsMousePressed()
{
// Return what state the key is in (pressed/not pressed).
return m_keys[key];
}
// Check the left mouse button state.
if (m_mouseState.rgbButtons[0] & 0x80)
{
return true;
}
return false;
}

View File

@ -1,6 +1,21 @@
#ifndef _INPUTCLASS_H_
#define _INPUTCLASS_H_
///////////////////////////////
// PRE-PROCESSING DIRECTIVES //
///////////////////////////////
#define DIRECTINPUT_VERSION 0x0800
/////////////
// LINKING //
/////////////
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")
//////////////
// INCLUDES //
//////////////
#include <dinput.h>
////////////////////////////////////////////////////////////////////////////////
// Class name: InputClass
@ -12,15 +27,34 @@ public:
InputClass(const InputClass&);
~InputClass();
void Initialize();
bool Initialize(HINSTANCE, HWND, int, int);
void Shutdown();
bool Frame();
void KeyDown(unsigned int);
bool IsEscapePressed();
void GetMouseLocation(int&, int&);
bool IsMousePressed();
/*void KeyDown(unsigned int);
void KeyUp(unsigned int);
bool IsKeyDown(unsigned int);
bool IsKeyDown(unsigned int);*/
private:
bool m_keys[256];
bool ReadKeyboard();
bool ReadMouse();
void ProcessInput();
private:
IDirectInput8* m_directInput;
IDirectInputDevice8* m_keyboard;
IDirectInputDevice8* m_mouse;
unsigned char m_keyboardState[256];
DIMOUSESTATE m_mouseState;
int m_screenWidth, m_screenHeight, m_mouseX, m_mouseY;
};
#endif