From 5e8e106327adef84b06288bb714ccc0a880b4ef5 Mon Sep 17 00:00:00 2001 From: Linda Andersson Date: Tue, 26 Nov 2013 11:33:16 +0100 Subject: [PATCH] add simple input class for testing --- Code/GameLogic/GameLogic.vcxproj | 21 ++-- Code/GameLogic/TestGLMain.cpp | 21 +++- Code/Input/Input.vcxproj | 8 +- Code/Input/Input.vcxproj.filters | 10 ++ Code/Input/L_inputClass.cpp | 203 +++++++++++++++++++++++++++++++ Code/Input/L_inputClass.h | 45 +++++++ 6 files changed, 297 insertions(+), 11 deletions(-) create mode 100644 Code/Input/L_inputClass.cpp create mode 100644 Code/Input/L_inputClass.h diff --git a/Code/GameLogic/GameLogic.vcxproj b/Code/GameLogic/GameLogic.vcxproj index 05419d2c..2d2ee4ef 100644 --- a/Code/GameLogic/GameLogic.vcxproj +++ b/Code/GameLogic/GameLogic.vcxproj @@ -69,7 +69,7 @@ $(SolutionDir)..\Bin\DLL\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D - $(SolutionDir)..\Bin\DLL;$(LibraryPath) + C:\Users\Linda Andersson\Desktop\spel\git\External\Lib\Input;$(SolutionDir)..\Bin\DLL;$(LibraryPath) $(SolutionDir)..\Bin\DLL\ @@ -94,12 +94,12 @@ Level3 Disabled true - $(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) + $(SolutionDir)Input;$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions) true - GamePhysics_$(PlatformShortName)D.lib;%(AdditionalDependencies) + Input_$(PlatformShortName)D.lib;GamePhysics_$(PlatformShortName)D.lib;%(AdditionalDependencies) @@ -107,12 +107,12 @@ Level3 Disabled true - $(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) + $(SolutionDir)Input;$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions) true - GamePhysics_$(PlatformShortName)D.lib;%(AdditionalDependencies) + Input_$(PlatformShortName)D.lib;GamePhysics_$(PlatformShortName)D.lib;%(AdditionalDependencies) @@ -122,14 +122,14 @@ true true true - $(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) + $(SolutionDir)Input;$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions) true true true - GamePhysics_$(PlatformShortName).lib;%(AdditionalDependencies) + Input_$(PlatformShortName).lib;GamePhysics_$(PlatformShortName).lib;%(AdditionalDependencies) @@ -139,14 +139,14 @@ true true true - $(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) + $(SolutionDir)Input;$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories) GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions) true true true - GamePhysics_$(PlatformShortName).lib;%(AdditionalDependencies) + Input_$(PlatformShortName).lib;GamePhysics_$(PlatformShortName).lib;%(AdditionalDependencies) @@ -158,6 +158,9 @@ false false + + {7e3990d2-3d94-465c-b58d-64a74b3ecf9b} + {2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee} diff --git a/Code/GameLogic/TestGLMain.cpp b/Code/GameLogic/TestGLMain.cpp index bf8cebbe..5ca1ac1d 100644 --- a/Code/GameLogic/TestGLMain.cpp +++ b/Code/GameLogic/TestGLMain.cpp @@ -10,7 +10,7 @@ #include "Core/Core.h" #include "Render\Preparations\Preparations.h" #include "IGame.h" -//#include "InputController.h" +#include "L_inputClass.h" @@ -21,6 +21,7 @@ HINSTANCE g_hInst = NULL; HWND g_hWnd = NULL; GameLogic::IGame* game; +InputClass* inputObj; //-------------------------------------------------------------------------------------- @@ -181,16 +182,30 @@ HRESULT InitGame() game = new GameLogic::IGame(); game->Init(); game->StartGame(); + + inputObj = new InputClass; + if(!inputObj->Initialize(g_hInst, g_hWnd, 1024, 768)) + { + MessageBox(0, L"Could not initialize the input object.", L"Error", MB_OK); + return false; + } + return S_OK; } HRESULT Update(float deltaTime) { game->Update(); + inputObj->Update(); return S_OK; } HRESULT Render(float deltaTime) { + int isPressed = 0; + if(inputObj->IsKeyPressed(DIK_A)) + { + isPressed = 1; + } //Oyster::Graphics::Render::Rendering::Basic::NewFrame(); Oyster::Graphics::Render::Preparations::Basic::ClearBackBuffer(Oyster::Math::Float4(0,0,1,1)); @@ -199,7 +214,11 @@ HRESULT Render(float deltaTime) //Oyster::Graphics::Core::deviceContext->Draw(3,0); //Oyster::Graphics::Render::Rendering::Basic::EndFrame(); + wchar_t title[255]; + //swprintf(title, L"| FPS: %d \n",(int)(isPressed)); + swprintf(title, sizeof(title), L"| Pressing A: %d | \n", (int)(isPressed)); + SetWindowText(g_hWnd, title); Oyster::Graphics::Core::swapChain->Present(0,0); diff --git a/Code/Input/Input.vcxproj b/Code/Input/Input.vcxproj index df018aab..6788f744 100644 --- a/Code/Input/Input.vcxproj +++ b/Code/Input/Input.vcxproj @@ -18,13 +18,19 @@ x64 + + + + + + {7E3990D2-3D94-465C-B58D-64A74B3ECF9B} Input - Application + StaticLibrary true v110 Unicode diff --git a/Code/Input/Input.vcxproj.filters b/Code/Input/Input.vcxproj.filters index d7ef6a1a..fc15cc38 100644 --- a/Code/Input/Input.vcxproj.filters +++ b/Code/Input/Input.vcxproj.filters @@ -14,4 +14,14 @@ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + Source Files + + + + + Header Files + + \ No newline at end of file diff --git a/Code/Input/L_inputClass.cpp b/Code/Input/L_inputClass.cpp new file mode 100644 index 00000000..e644b5dc --- /dev/null +++ b/Code/Input/L_inputClass.cpp @@ -0,0 +1,203 @@ +#include "L_inputclass.h" + +InputClass::InputClass() +{ + m_directInput = NULL; + m_keyboard = NULL; + m_mouse = NULL; +} + +InputClass::~InputClass() +{ + // Release the mouse. + if(m_mouse) + { + (m_mouse)->Unacquire(); + (m_mouse)->Release(); + (m_mouse) = NULL; + } + //SAFE_UNACQUIRE(m_mouse); + + // Release the keyboard. + if(m_keyboard) + { + (m_keyboard)->Unacquire(); + (m_keyboard)->Release(); + (m_keyboard) = NULL; + } + //SAFE_UNACQUIRE(m_keyboard); + + // Release the main interface to direct input. + if( m_directInput ) + { + (m_directInput)->Release(); + (m_directInput) = NULL; + } + //SAFE_RELEASE(m_directInput); + + return; +} + +bool InputClass::Initialize(HINSTANCE hinstance, HWND hwnd, int screenWidth, int screenHeight) +{ + HRESULT result; + + // Initialize the main direct input interface. + result = DirectInput8Create(hinstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_directInput, NULL); + if(FAILED(result)) + { + 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 cooperative level of the keyboard to not share with other programs. + result = m_keyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); + 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; + } + + // 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; +} + +bool InputClass::Update() +{ + 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; + } + + 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::MouseMove(float &Pitch, float &RotateY ) +{ + //if left mouse button is pressed + if (m_mouseState.rgbButtons[0]) + { + float dx = (static_cast( m_mouseState.lX)/150); + float dy = (static_cast( m_mouseState.lY)/150); + + // + Pitch=dy; + RotateY=dx; + + } +} +bool InputClass::IsMousePressed() +{ + if (m_mouseState.rgbButtons[0]) + { + return true; + } + return false; +} +bool InputClass::IsKeyPressed(int key) +{ + if( m_keyboardState[key] & 0x80 ) + return true; + + return false; +} diff --git a/Code/Input/L_inputClass.h b/Code/Input/L_inputClass.h new file mode 100644 index 00000000..b5a08c31 --- /dev/null +++ b/Code/Input/L_inputClass.h @@ -0,0 +1,45 @@ +#ifndef _INPUTCLASS_H_ +#define _INPUTCLASS_H_ + + +#define DIRECTINPUT_VERSION 0x0800 + +#pragma comment(lib, "dinput8.lib") +#pragma comment(lib, "dxguid.lib") + +#include + + + +class InputClass +{ +private: + IDirectInput8* m_directInput; + IDirectInputDevice8* m_keyboard; + IDirectInputDevice8* m_mouse; + + unsigned char m_keyboardState[256]; + DIMOUSESTATE m_mouseState; + + bool ReadKeyboard(); + bool ReadMouse(); + + +public: + + InputClass(); + ~InputClass(); + + // Initialize DirectInput8Create and acquire the mouse and keyboard + bool Initialize(HINSTANCE, HWND, int, int); + + //read the mouse and keyboard and send back + // delta mouse pos and if any button is pressed + bool Update(); + + bool IsKeyPressed(int key); + bool IsMousePressed(); + void MouseMove(float &Pitch, float &RoateY); +}; + +#endif \ No newline at end of file