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
7d303e4d
Commit
7d303e4d
authored
Dec 26, 2018
by
chili
Browse files
use peekmessage instead of get to prevent blocking game loop
parent
f00bb134
Changes
3
Show whitespace changes
Inline
Side-by-side
hw3d/App.cpp
View file @
7d303e4d
...
...
@@ -4,30 +4,21 @@
App
::
App
()
:
wnd
(
800
,
600
,
"The Donkey Fart Box"
)
wnd
(
800
,
600
,
"The Donkey Fart Box"
)
{}
int
App
::
Go
()
{
MSG
msg
;
BOOL
gResult
;
while
(
(
gResult
=
GetMessage
(
&
msg
,
nullptr
,
0
,
0
))
>
0
)
while
(
true
)
{
// TranslateMessage will post auxilliary WM_CHAR messages from key msgs
TranslateMessage
(
&
msg
);
DispatchMessage
(
&
msg
);
DoFrame
();
}
// check if GetMessage call itself borked
if
(
gResult
==
-
1
)
// process all messages pending, but to not block for new messages
if
(
const
auto
ecode
=
Window
::
ProcessMessages
()
)
{
throw
CHWND_LAST_EXCEPT
();
// if return optional has value, means we're quitting so return exit code
return
*
ecode
;
}
DoFrame
();
}
// wParam here is the value passed to PostQuitMessage
return
msg
.
wParam
;
}
void
App
::
DoFrame
()
...
...
hw3d/Window.cpp
View file @
7d303e4d
...
...
@@ -112,6 +112,28 @@ void Window::SetTitle( const std::string& title )
}
}
std
::
optional
<
int
>
Window
::
ProcessMessages
()
{
MSG
msg
;
// while queue has messages, remove and dispatch them (but do not block on empty queue)
while
(
PeekMessage
(
&
msg
,
nullptr
,
0
,
0
,
PM_REMOVE
)
)
{
// check for quit because peekmessage does not signal this via return val
if
(
msg
.
message
==
WM_QUIT
)
{
// return optional wrapping int (arg to PostQuitMessage is in wparam) signals quit
return
msg
.
wParam
;
}
// TranslateMessage will post auxilliary WM_CHAR messages from key msgs
TranslateMessage
(
&
msg
);
DispatchMessage
(
&
msg
);
}
// return empty optional when not quitting app
return
{};
}
LRESULT
CALLBACK
Window
::
HandleMsgSetup
(
HWND
hWnd
,
UINT
msg
,
WPARAM
wParam
,
LPARAM
lParam
)
noexcept
{
// use create parameter passed in from CreateWindow() to store window class pointer at WinAPI side
...
...
hw3d/Window.h
View file @
7d303e4d
...
...
@@ -22,6 +22,7 @@
#include "ChiliException.h"
#include "Keyboard.h"
#include "Mouse.h"
#include <optional>
class
Window
...
...
@@ -61,6 +62,7 @@ public:
Window
(
const
Window
&
)
=
delete
;
Window
&
operator
=
(
const
Window
&
)
=
delete
;
void
SetTitle
(
const
std
::
string
&
title
);
static
std
::
optional
<
int
>
ProcessMessages
();
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
;
...
...
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