diff --git a/olcPixelGameEngine.h b/olcPixelGameEngine.h index 17b2d4a..9e88601 100644 --- a/olcPixelGameEngine.h +++ b/olcPixelGameEngine.h @@ -896,6 +896,7 @@ namespace olc virtual ~Platform() = default; virtual olc::rcode ApplicationStartUp() = 0; virtual olc::rcode ApplicationCleanUp() = 0; + virtual olc::rcode SetThreadName(const std::string&) = 0; virtual olc::rcode ThreadStartUp() = 0; virtual olc::rcode ThreadCleanUp() = 0; virtual olc::rcode CreateGraphics(bool bFullScreen, bool bEnableVSYNC, const olc::vi2d& vViewPos, const olc::vi2d& vViewSize) = 0; @@ -3405,6 +3406,8 @@ namespace olc void PixelGameEngine::EngineThread() { + platform->SetThreadName("OLC PGE Engine Thread"); + // Allow platform to do stuff here if needed, since its now in the // context of this thread if (platform->ThreadStartUp() == olc::FAIL) return; @@ -4952,6 +4955,33 @@ namespace olc public: virtual olc::rcode ApplicationStartUp() override { return olc::rcode::OK; } virtual olc::rcode ApplicationCleanUp() override { return olc::rcode::OK; } + + virtual olc::rcode SetThreadName(const std::string& s) + { + // Reference: + // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code?view=vs-2019#set-a-thread-name-by-throwing-an-exception + + #pragma pack(push, 8) + struct THREADNAME_INFO + { + DWORD dwType; + LPCSTR szName; + DWORD dwThreadID; + DWORD dwFlags; + }; + #pragma pack(pop) + + THREADNAME_INFO info{ 0x1000, s.c_str(), 0xFFFFFFFF, 0 }; + + __try + { + RaiseException(0x406D1388, 0, sizeof(info) / sizeof(ULONG_PTR), reinterpret_cast(&info)); + } + __except(EXCEPTION_CONTINUE_EXECUTION) { } + + return olc::rcode::OK; + } + virtual olc::rcode ThreadStartUp() override { return olc::rcode::OK; } virtual olc::rcode ThreadCleanUp() override @@ -5154,6 +5184,11 @@ namespace olc return olc::rcode::OK; } + virtual olc::rcode SetThreadName(const std::string&) override + { + return olc::rcode::OK; + } + virtual olc::rcode ThreadStartUp() override { return olc::rcode::OK; @@ -5404,6 +5439,11 @@ namespace olc { return olc::rcode::OK; } + virtual olc::rcode SetThreadName(const std::string&) override + { + return olc::rcode::OK; + } + virtual olc::rcode ThreadStartUp() override { return olc::rcode::OK; @@ -5721,6 +5761,9 @@ namespace olc virtual olc::rcode ApplicationCleanUp() override { ThreadCleanUp(); return olc::rcode::OK; } + virtual olc::rcode SetThreadName(const std::string&) + { return olc::rcode::OK; } + virtual olc::rcode ThreadStartUp() override { return olc::rcode::OK; }