add simple input class for testing

This commit is contained in:
Linda Andersson 2013-11-26 11:33:16 +01:00
parent abfd3116ca
commit 5e8e106327
6 changed files with 297 additions and 11 deletions

View File

@ -69,7 +69,7 @@
<OutDir>$(SolutionDir)..\Bin\DLL\</OutDir> <OutDir>$(SolutionDir)..\Bin\DLL\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<LibraryPath>$(SolutionDir)..\Bin\DLL;$(LibraryPath)</LibraryPath> <LibraryPath>C:\Users\Linda Andersson\Desktop\spel\git\External\Lib\Input;$(SolutionDir)..\Bin\DLL;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)..\Bin\DLL\</OutDir> <OutDir>$(SolutionDir)..\Bin\DLL\</OutDir>
@ -94,12 +94,12 @@
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(SolutionDir)Input;$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>GamePhysics_$(PlatformShortName)D.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>Input_$(PlatformShortName)D.lib;GamePhysics_$(PlatformShortName)D.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -107,12 +107,12 @@
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(SolutionDir)Input;$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>GamePhysics_$(PlatformShortName)D.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>Input_$(PlatformShortName)D.lib;GamePhysics_$(PlatformShortName)D.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -122,14 +122,14 @@
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(SolutionDir)Input;$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>GamePhysics_$(PlatformShortName).lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>Input_$(PlatformShortName).lib;GamePhysics_$(PlatformShortName).lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -139,14 +139,14 @@
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(SolutionDir)Input;$(SolutionDir)OysterGraphics;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterPhysics3D;$(SolutionDir)GamePhysics;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>GAME_DLL_EXPORT;_WINDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>GamePhysics_$(PlatformShortName).lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>Input_$(PlatformShortName).lib;GamePhysics_$(PlatformShortName).lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
@ -158,6 +158,9 @@
<LinkLibraryDependencies>false</LinkLibraryDependencies> <LinkLibraryDependencies>false</LinkLibraryDependencies>
<UseLibraryDependencyInputs>false</UseLibraryDependencyInputs> <UseLibraryDependencyInputs>false</UseLibraryDependencyInputs>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\Input\Input.vcxproj">
<Project>{7e3990d2-3d94-465c-b58d-64a74b3ecf9b}</Project>
</ProjectReference>
<ProjectReference Include="..\Misc\Misc.vcxproj"> <ProjectReference Include="..\Misc\Misc.vcxproj">
<Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project> <Project>{2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee}</Project>
</ProjectReference> </ProjectReference>

View File

@ -10,7 +10,7 @@
#include "Core/Core.h" #include "Core/Core.h"
#include "Render\Preparations\Preparations.h" #include "Render\Preparations\Preparations.h"
#include "IGame.h" #include "IGame.h"
//#include "InputController.h" #include "L_inputClass.h"
@ -21,6 +21,7 @@ HINSTANCE g_hInst = NULL;
HWND g_hWnd = NULL; HWND g_hWnd = NULL;
GameLogic::IGame* game; GameLogic::IGame* game;
InputClass* inputObj;
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -181,16 +182,30 @@ HRESULT InitGame()
game = new GameLogic::IGame(); game = new GameLogic::IGame();
game->Init(); game->Init();
game->StartGame(); 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; return S_OK;
} }
HRESULT Update(float deltaTime) HRESULT Update(float deltaTime)
{ {
game->Update(); game->Update();
inputObj->Update();
return S_OK; return S_OK;
} }
HRESULT Render(float deltaTime) HRESULT Render(float deltaTime)
{ {
int isPressed = 0;
if(inputObj->IsKeyPressed(DIK_A))
{
isPressed = 1;
}
//Oyster::Graphics::Render::Rendering::Basic::NewFrame(); //Oyster::Graphics::Render::Rendering::Basic::NewFrame();
Oyster::Graphics::Render::Preparations::Basic::ClearBackBuffer(Oyster::Math::Float4(0,0,1,1)); 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::Core::deviceContext->Draw(3,0);
//Oyster::Graphics::Render::Rendering::Basic::EndFrame(); //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); Oyster::Graphics::Core::swapChain->Present(0,0);

View File

@ -18,13 +18,19 @@
<Platform>x64</Platform> <Platform>x64</Platform>
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClCompile Include="L_inputClass.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="L_inputClass.h" />
</ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{7E3990D2-3D94-465C-B58D-64A74B3ECF9B}</ProjectGuid> <ProjectGuid>{7E3990D2-3D94-465C-B58D-64A74B3ECF9B}</ProjectGuid>
<RootNamespace>Input</RootNamespace> <RootNamespace>Input</RootNamespace>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset> <PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>

View File

@ -14,4 +14,14 @@
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClCompile Include="L_inputClass.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="L_inputClass.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project> </Project>

203
Code/Input/L_inputClass.cpp Normal file
View File

@ -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<float>( m_mouseState.lX)/150);
float dy = (static_cast<float>( 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;
}

45
Code/Input/L_inputClass.h Normal file
View File

@ -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 <dinput.h>
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