Commit 5f700ed8 authored by chili's avatar chili
Browse files

keyboard i/o class

parent 9efa93f3
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#include "Keyboard.h"
bool Keyboard::KeyIsPressed( unsigned char keycode ) const noexcept
{
return keystates[keycode];
}
Keyboard::Event Keyboard::ReadKey() noexcept
{
if( keybuffer.size() > 0u )
{
Keyboard::Event e = keybuffer.front();
keybuffer.pop();
return e;
}
else
{
return Keyboard::Event();
}
}
bool Keyboard::KeyIsEmpty() const noexcept
{
return keybuffer.empty();
}
char Keyboard::ReadChar() noexcept
{
if( charbuffer.size() > 0u )
{
unsigned char charcode = charbuffer.front();
charbuffer.pop();
return charcode;
}
else
{
return 0;
}
}
bool Keyboard::CharIsEmpty() const noexcept
{
return charbuffer.empty();
}
void Keyboard::FlushKey() noexcept
{
keybuffer = std::queue<Event>();
}
void Keyboard::FlushChar() noexcept
{
charbuffer = std::queue<char>();
}
void Keyboard::Flush() noexcept
{
FlushKey();
FlushChar();
}
void Keyboard::EnableAutorepeat() noexcept
{
autorepeatEnabled = true;
}
void Keyboard::DisableAutorepeat() noexcept
{
autorepeatEnabled = false;
}
bool Keyboard::AutorepeatIsEnabled() const noexcept
{
return autorepeatEnabled;
}
void Keyboard::OnKeyPressed( unsigned char keycode ) noexcept
{
keystates[keycode] = true;
keybuffer.push( Keyboard::Event( Keyboard::Event::Type::Press,keycode ) );
TrimBuffer( keybuffer );
}
void Keyboard::OnKeyReleased( unsigned char keycode ) noexcept
{
keystates[keycode] = false;
keybuffer.push( Keyboard::Event( Keyboard::Event::Type::Release,keycode ) );
TrimBuffer( keybuffer );
}
void Keyboard::OnChar( char character ) noexcept
{
charbuffer.push( character );
TrimBuffer( charbuffer );
}
void Keyboard::ClearState() noexcept
{
keystates.reset();
}
template<typename T>
void Keyboard::TrimBuffer( std::queue<T>& buffer ) noexcept
{
while( buffer.size() > bufferSize )
{
buffer.pop();
}
}
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
#include <queue>
#include <bitset>
class Keyboard
{
friend class Window;
public:
class Event
{
public:
enum class Type
{
Press,
Release,
Invalid
};
private:
Type type;
unsigned char code;
public:
Event()
:
type( Type::Invalid ),
code( 0u )
{}
Event( Type type,unsigned char code ) noexcept
:
type( type ),
code( code )
{}
bool IsPress() const noexcept
{
return type == Type::Press;
}
bool IsRelease() const noexcept
{
return type == Type::Release;
}
bool IsValid() const noexcept
{
return type != Type::Invalid;
}
unsigned char GetCode() const noexcept
{
return code;
}
};
public:
Keyboard() = default;
Keyboard( const Keyboard& ) = delete;
Keyboard& operator=( const Keyboard& ) = delete;
// key event stuff
bool KeyIsPressed( unsigned char keycode ) const noexcept;
Event ReadKey() noexcept;
bool KeyIsEmpty() const noexcept;
void FlushKey() noexcept;
// char event stuff
char ReadChar() noexcept;
bool CharIsEmpty() const noexcept;
void FlushChar() noexcept;
void Flush() noexcept;
// autorepeat control
void EnableAutorepeat() noexcept;
void DisableAutorepeat() noexcept;
bool AutorepeatIsEnabled() const noexcept;
private:
void OnKeyPressed( unsigned char keycode ) noexcept;
void OnKeyReleased( unsigned char keycode ) noexcept;
void OnChar( char character ) noexcept;
void ClearState() noexcept;
template<typename T>
static void TrimBuffer( std::queue<T>& buffer ) noexcept;
private:
static constexpr unsigned int nKeys = 256u;
static constexpr unsigned int bufferSize = 16u;
bool autorepeatEnabled = false;
std::bitset<nKeys> keystates;
std::queue<Event> keybuffer;
std::queue<char> charbuffer;
};
\ No newline at end of file
...@@ -37,6 +37,10 @@ int CALLBACK WinMain( ...@@ -37,6 +37,10 @@ int CALLBACK WinMain(
// TranslateMessage will post auxilliary WM_CHAR messages from key msgs // TranslateMessage will post auxilliary WM_CHAR messages from key msgs
TranslateMessage( &msg ); TranslateMessage( &msg );
DispatchMessage( &msg ); DispatchMessage( &msg );
if( wnd.kbd.KeyIsPressed( VK_SPACE ) )
{
MessageBox( nullptr,"Something Happon!","Space Key Was Pressed",MB_OK | MB_ICONEXCLAMATION );
}
} }
// check if GetMessage call itself borked // check if GetMessage call itself borked
......
...@@ -137,6 +137,18 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex ...@@ -137,6 +137,18 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
case WM_CLOSE: case WM_CLOSE:
PostQuitMessage( 0 ); PostQuitMessage( 0 );
return 0; return 0;
/*********** KEYBOARD MESSAGES ***********/
case WM_KEYDOWN:
kbd.OnKeyPressed( static_cast<unsigned char>(wParam) );
break;
case WM_KEYUP:
kbd.OnKeyReleased( static_cast<unsigned char>(wParam) );
break;
case WM_CHAR:
kbd.OnChar( static_cast<unsigned char>(wParam) );
break;
/*********** END KEYBOARD MESSAGES ***********/
} }
return DefWindowProc( hWnd,msg,wParam,lParam ); return DefWindowProc( hWnd,msg,wParam,lParam );
...@@ -144,7 +156,7 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex ...@@ -144,7 +156,7 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
// Window Exception Stuff // Window Exception Stuff
Window::Exception::Exception( int line,const char * file,HRESULT hr ) noexcept Window::Exception::Exception( int line,const char* file,HRESULT hr ) noexcept
: :
ChiliException( line,file ), ChiliException( line,file ),
hr( hr ) hr( hr )
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#pragma once #pragma once
#include "ChiliWin.h" #include "ChiliWin.h"
#include "ChiliException.h" #include "ChiliException.h"
#include "Keyboard.h"
class Window class Window
...@@ -62,6 +63,8 @@ private: ...@@ -62,6 +63,8 @@ private:
static LRESULT CALLBACK HandleMsgSetup( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept; static LRESULT CALLBACK HandleMsgSetup( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept;
static LRESULT CALLBACK HandleMsgThunk( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept; static LRESULT CALLBACK HandleMsgThunk( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept;
LRESULT HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept; LRESULT HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept;
public:
Keyboard kbd;
private: private:
int width; int width;
int height; int height;
......
...@@ -146,6 +146,7 @@ ...@@ -146,6 +146,7 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="ChiliException.cpp" /> <ClCompile Include="ChiliException.cpp" />
<ClCompile Include="Keyboard.cpp" />
<ClCompile Include="Window.cpp" /> <ClCompile Include="Window.cpp" />
<ClCompile Include="WindowsMessageMap.cpp" /> <ClCompile Include="WindowsMessageMap.cpp" />
<ClCompile Include="WinMain.cpp" /> <ClCompile Include="WinMain.cpp" />
...@@ -153,6 +154,7 @@ ...@@ -153,6 +154,7 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="ChiliException.h" /> <ClInclude Include="ChiliException.h" />
<ClInclude Include="ChiliWin.h" /> <ClInclude Include="ChiliWin.h" />
<ClInclude Include="Keyboard.h" />
<ClInclude Include="resource.h" /> <ClInclude Include="resource.h" />
<ClInclude Include="Window.h" /> <ClInclude Include="Window.h" />
<ClInclude Include="WindowsMessageMap.h" /> <ClInclude Include="WindowsMessageMap.h" />
......
...@@ -27,6 +27,9 @@ ...@@ -27,6 +27,9 @@
<ClCompile Include="ChiliException.cpp"> <ClCompile Include="ChiliException.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Keyboard.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="WindowsMessageMap.h"> <ClInclude Include="WindowsMessageMap.h">
...@@ -44,6 +47,9 @@ ...@@ -44,6 +47,9 @@
<ClInclude Include="resource.h"> <ClInclude Include="resource.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Keyboard.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="hw3d.rc"> <ResourceCompile Include="hw3d.rc">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment