Commit fcfc26d9 authored by Administrator's avatar Administrator
Browse files

redo use PeekMessage instead of GetMessage

parent 33056784
#include "App.h"
#include <sstream>
#include <iomanip>
App::App()
:
......@@ -7,28 +9,24 @@ App::App()
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 );
// process all messages pending, but to not block for new messages
if (const auto ecode = Window::ProcessMessages())
{
// if return optional has value, means we're quitting so return exit code
return *ecode;
}
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()
{
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
#include "Window.h"
#include "ChiliTimer.h"
class App
{
......@@ -11,4 +12,5 @@ private:
void DoFrame();
private:
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 @@
<ItemGroup>
<ClCompile Include="App.cpp" />
<ClCompile Include="ChiliException.cpp" />
<ClCompile Include="ChiliTimer.cpp" />
<ClCompile Include="Keyboard.cpp" />
<ClCompile Include="Mouse.cpp" />
<ClCompile Include="Window.cpp" />
......@@ -148,6 +149,7 @@
<ItemGroup>
<ClInclude Include="App.h" />
<ClInclude Include="ChiliException.h" />
<ClInclude Include="ChiliTimer.h" />
<ClInclude Include="ChiliWin.h" />
<ClInclude Include="Keyboard.h" />
<ClInclude Include="Mouse.h" />
......
......@@ -36,6 +36,9 @@
<ClCompile Include="App.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ChiliTimer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ChiliException.h">
......@@ -62,6 +65,9 @@
<ClInclude Include="App.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ChiliTimer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="StudyDx.rc">
......
......@@ -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
{
// use create parameter passed in from CreateWindow() to store window class pointer at WinAPI side
......
......@@ -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;
......
......@@ -33,7 +33,7 @@
#define REGISTER_MESSAGE(msg){msg,#msg}
WindowsMessageMap::WindowsMessageMap()
WindowsMessageMap::WindowsMessageMap() noexcept
:
map({
REGISTER_MESSAGE(WM_CREATE),
......@@ -168,15 +168,6 @@ WindowsMessageMap::WindowsMessageMap()
REGISTER_MESSAGE(WM_QUERYNEWPALETTE),
REGISTER_MESSAGE(WM_PALETTEISCHANGING),
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_POWER),
REGISTER_MESSAGE(WM_WINDOWPOSCHANGED),
......@@ -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;
const auto i = map.find(msg);
......
......@@ -21,12 +21,13 @@
#include <unordered_map>
#include <Windows.h>
#include <string>
#include "ChiliWin.h"
class WindowsMessageMap
{
public:
WindowsMessageMap();
std::string operator()(DWORD msg, LPARAM lp, WPARAM wp) const;
WindowsMessageMap() noexcept;
std::string operator()(DWORD msg, LPARAM lp, WPARAM wp) const noexcept;
private:
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