Commit fcfc26d9 authored by Administrator's avatar Administrator
Browse files

redo use PeekMessage instead of GetMessage

parent 33056784
#include "App.h" #include "App.h"
#include <sstream>
#include <iomanip>
App::App() App::App()
: :
...@@ -7,28 +9,24 @@ App::App() ...@@ -7,28 +9,24 @@ App::App()
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();
// ȴ10룬ֹ
Sleep(10);
} }
// 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()
{ {
const float t = timer.Peek();
std::ostringstream oss;
oss << "Time elapsed: " << std::setprecision(1) << std::fixed << t << "s";
wnd.SetTitle(oss.str());
} }
\ No newline at end of file
#pragma once #pragma once
#include "Window.h" #include "Window.h"
#include "ChiliTimer.h"
class App class App
{ {
...@@ -11,4 +12,5 @@ private: ...@@ -11,4 +12,5 @@ private:
void DoFrame(); void DoFrame();
private: private:
Window wnd; Window wnd;
ChiliTimer timer;
}; };
\ No newline at end of file
#include "ChiliTimer.h"
using namespace std::chrono;
ChiliTimer::ChiliTimer() noexcept
{
last = steady_clock::now();
}
float ChiliTimer::Mark() noexcept
{
const auto old = last;
last = steady_clock::now();
const duration<float> frameTime = last - old;
return frameTime.count();
}
float ChiliTimer::Peek() const noexcept
{
return duration<float>( steady_clock::now() - last ).count();
}
#pragma once
#include <chrono>
class ChiliTimer
{
public:
ChiliTimer() noexcept;
float Mark() noexcept;
float Peek() const noexcept;
private:
std::chrono::steady_clock::time_point last;
};
\ No newline at end of file
...@@ -139,6 +139,7 @@ ...@@ -139,6 +139,7 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="App.cpp" /> <ClCompile Include="App.cpp" />
<ClCompile Include="ChiliException.cpp" /> <ClCompile Include="ChiliException.cpp" />
<ClCompile Include="ChiliTimer.cpp" />
<ClCompile Include="Keyboard.cpp" /> <ClCompile Include="Keyboard.cpp" />
<ClCompile Include="Mouse.cpp" /> <ClCompile Include="Mouse.cpp" />
<ClCompile Include="Window.cpp" /> <ClCompile Include="Window.cpp" />
...@@ -148,6 +149,7 @@ ...@@ -148,6 +149,7 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="App.h" /> <ClInclude Include="App.h" />
<ClInclude Include="ChiliException.h" /> <ClInclude Include="ChiliException.h" />
<ClInclude Include="ChiliTimer.h" />
<ClInclude Include="ChiliWin.h" /> <ClInclude Include="ChiliWin.h" />
<ClInclude Include="Keyboard.h" /> <ClInclude Include="Keyboard.h" />
<ClInclude Include="Mouse.h" /> <ClInclude Include="Mouse.h" />
......
...@@ -36,6 +36,9 @@ ...@@ -36,6 +36,9 @@
<ClCompile Include="App.cpp"> <ClCompile Include="App.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="ChiliTimer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="ChiliException.h"> <ClInclude Include="ChiliException.h">
...@@ -62,6 +65,9 @@ ...@@ -62,6 +65,9 @@
<ClInclude Include="App.h"> <ClInclude Include="App.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="ChiliTimer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="StudyDx.rc"> <ResourceCompile Include="StudyDx.rc">
......
...@@ -129,6 +129,29 @@ void Window::SetTitle(const std::string& title) ...@@ -129,6 +129,29 @@ 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 (int)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;
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
#define REGISTER_MESSAGE(msg){msg,#msg} #define REGISTER_MESSAGE(msg){msg,#msg}
WindowsMessageMap::WindowsMessageMap() WindowsMessageMap::WindowsMessageMap() noexcept
: :
map({ map({
REGISTER_MESSAGE(WM_CREATE), REGISTER_MESSAGE(WM_CREATE),
...@@ -168,15 +168,6 @@ WindowsMessageMap::WindowsMessageMap() ...@@ -168,15 +168,6 @@ WindowsMessageMap::WindowsMessageMap()
REGISTER_MESSAGE(WM_QUERYNEWPALETTE), REGISTER_MESSAGE(WM_QUERYNEWPALETTE),
REGISTER_MESSAGE(WM_PALETTEISCHANGING), REGISTER_MESSAGE(WM_PALETTEISCHANGING),
REGISTER_MESSAGE(WM_PALETTECHANGED), REGISTER_MESSAGE(WM_PALETTECHANGED),
REGISTER_MESSAGE(WM_DDE_INITIATE),
REGISTER_MESSAGE(WM_DDE_TERMINATE),
REGISTER_MESSAGE(WM_DDE_ADVISE),
REGISTER_MESSAGE(WM_DDE_UNADVISE),
REGISTER_MESSAGE(WM_DDE_ACK),
REGISTER_MESSAGE(WM_DDE_DATA),
REGISTER_MESSAGE(WM_DDE_REQUEST),
REGISTER_MESSAGE(WM_DDE_POKE),
REGISTER_MESSAGE(WM_DDE_EXECUTE),
REGISTER_MESSAGE(WM_DROPFILES), REGISTER_MESSAGE(WM_DROPFILES),
REGISTER_MESSAGE(WM_POWER), REGISTER_MESSAGE(WM_POWER),
REGISTER_MESSAGE(WM_WINDOWPOSCHANGED), REGISTER_MESSAGE(WM_WINDOWPOSCHANGED),
...@@ -219,7 +210,7 @@ WindowsMessageMap::WindowsMessageMap() ...@@ -219,7 +210,7 @@ WindowsMessageMap::WindowsMessageMap()
}) })
{} {}
std::string WindowsMessageMap::operator()(DWORD msg, LPARAM lp, WPARAM wp) const std::string WindowsMessageMap::operator()(DWORD msg, LPARAM lp, WPARAM wp) const noexcept
{ {
constexpr int firstColWidth = 25; constexpr int firstColWidth = 25;
const auto i = map.find(msg); const auto i = map.find(msg);
......
...@@ -21,12 +21,13 @@ ...@@ -21,12 +21,13 @@
#include <unordered_map> #include <unordered_map>
#include <Windows.h> #include <Windows.h>
#include <string> #include <string>
#include "ChiliWin.h"
class WindowsMessageMap class WindowsMessageMap
{ {
public: public:
WindowsMessageMap(); WindowsMessageMap() noexcept;
std::string operator()(DWORD msg, LPARAM lp, WPARAM wp) const; std::string operator()(DWORD msg, LPARAM lp, WPARAM wp) const noexcept;
private: private:
std::unordered_map<DWORD, std::string> map; std::unordered_map<DWORD, std::string> map;
}; };
\ No newline at end of file
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