Commit 7d303e4d authored by chili's avatar chili
Browse files

use peekmessage instead of get to prevent blocking game loop

parent f00bb134
...@@ -4,30 +4,21 @@ ...@@ -4,30 +4,21 @@
App::App() App::App()
: :
wnd( 800,600,"The Donkey Fart Box") wnd( 800,600,"The Donkey Fart Box" )
{} {}
int App::Go() int App::Go()
{ {
MSG msg; while( true )
BOOL gResult;
while( (gResult = GetMessage( &msg,nullptr,0,0 )) > 0 )
{ {
// TranslateMessage will post auxilliary WM_CHAR messages from key msgs // process all messages pending, but to not block for new messages
TranslateMessage( &msg ); if( const auto ecode = Window::ProcessMessages() )
DispatchMessage( &msg ); {
// if return optional has value, means we're quitting so return exit code
return *ecode;
}
DoFrame(); DoFrame();
} }
// check if GetMessage call itself borked
if( gResult == -1 )
{
throw CHWND_LAST_EXCEPT();
}
// wParam here is the value passed to PostQuitMessage
return msg.wParam;
} }
void App::DoFrame() void App::DoFrame()
......
...@@ -112,6 +112,28 @@ void Window::SetTitle( const std::string& title ) ...@@ -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 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 // use create parameter passed in from CreateWindow() to store window class pointer at WinAPI side
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "ChiliException.h" #include "ChiliException.h"
#include "Keyboard.h" #include "Keyboard.h"
#include "Mouse.h" #include "Mouse.h"
#include <optional>
class Window class Window
...@@ -61,6 +62,7 @@ public: ...@@ -61,6 +62,7 @@ public:
Window( const Window& ) = delete; Window( const Window& ) = delete;
Window& operator=( const Window& ) = delete; Window& operator=( const Window& ) = delete;
void SetTitle( const std::string& title ); void SetTitle( const std::string& title );
static std::optional<int> ProcessMessages();
private: 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;
......
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