Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Clark Lin
hw3d
Commits
5f700ed8
Commit
5f700ed8
authored
Dec 11, 2018
by
chili
Browse files
keyboard i/o class
parent
9efa93f3
Changes
7
Hide whitespace changes
Inline
Side-by-side
hw3d/Keyboard.cpp
0 → 100644
View file @
5f700ed8
/******************************************************************************************
* 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
();
}
}
hw3d/Keyboard.h
0 → 100644
View file @
5f700ed8
/******************************************************************************************
* 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
hw3d/WinMain.cpp
View file @
5f700ed8
...
...
@@ -37,6 +37,10 @@ int CALLBACK WinMain(
// TranslateMessage will post auxilliary WM_CHAR messages from key msgs
TranslateMessage
(
&
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
...
...
hw3d/Window.cpp
View file @
5f700ed8
...
...
@@ -137,6 +137,18 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
case
WM_CLOSE
:
PostQuitMessage
(
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
);
...
...
@@ -144,7 +156,7 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
// 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
),
hr
(
hr
)
...
...
hw3d/Window.h
View file @
5f700ed8
...
...
@@ -20,6 +20,7 @@
#pragma once
#include "ChiliWin.h"
#include "ChiliException.h"
#include "Keyboard.h"
class
Window
...
...
@@ -62,6 +63,8 @@ private:
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
;
LRESULT
HandleMsg
(
HWND
hWnd
,
UINT
msg
,
WPARAM
wParam
,
LPARAM
lParam
)
noexcept
;
public:
Keyboard
kbd
;
private:
int
width
;
int
height
;
...
...
hw3d/hw3d.vcxproj
View file @
5f700ed8
...
...
@@ -146,6 +146,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile
Include=
"ChiliException.cpp"
/>
<ClCompile
Include=
"Keyboard.cpp"
/>
<ClCompile
Include=
"Window.cpp"
/>
<ClCompile
Include=
"WindowsMessageMap.cpp"
/>
<ClCompile
Include=
"WinMain.cpp"
/>
...
...
@@ -153,6 +154,7 @@
<ItemGroup>
<ClInclude
Include=
"ChiliException.h"
/>
<ClInclude
Include=
"ChiliWin.h"
/>
<ClInclude
Include=
"Keyboard.h"
/>
<ClInclude
Include=
"resource.h"
/>
<ClInclude
Include=
"Window.h"
/>
<ClInclude
Include=
"WindowsMessageMap.h"
/>
...
...
hw3d/hw3d.vcxproj.filters
View file @
5f700ed8
...
...
@@ -27,6 +27,9 @@
<ClCompile
Include=
"ChiliException.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
<ClCompile
Include=
"Keyboard.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"WindowsMessageMap.h"
>
...
...
@@ -44,6 +47,9 @@
<ClInclude
Include=
"resource.h"
>
<Filter>
Header Files
</Filter>
</ClInclude>
<ClInclude
Include=
"Keyboard.h"
>
<Filter>
Header Files
</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile
Include=
"hw3d.rc"
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment