Files
DX12/Engine/Core/MainWin32.cpp
2026-03-19 18:27:49 +08:00

78 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file MainWin32.cpp
* @brief Win32 可执行程序入口与消息泵实现。
* @details
* 负责设置工作目录、初始化调试内存检测,并驱动主循环:
* - 轮询并分发 Windows 消息;
* - 在消息空闲阶段调用 engine_update
* - 退出时由 engine_shutdown 完成资源收尾。
*/
#ifdef _WIN64
#include "CommonHeader.h"
#include <filesystem>
#ifndef WIN32_MEAN_AND_LEAN
#define WIN32_MEAN_AND_LEAN
#endif
#include <Windows.h>
#include <crtdbg.h>
namespace {
/**
* @brief 将当前工作目录切换到可执行文件所在目录。
* @return 切换后的当前目录;失败时返回空路径。
*/
std::filesystem::path
set_current_directory_to_executable_path()
{
wchar_t path[MAX_PATH]{};
const uint32_t length{ GetModuleFileName(0, &path[0], MAX_PATH) };
if (!length || GetLastError() == ERROR_INSUFFICIENT_BUFFER) return {};
std::filesystem::path p{ path };
std::filesystem::current_path(p.parent_path());
return std::filesystem::current_path();
}
}
#ifndef USE_WITH_EDITOR
extern bool engine_initialize();
extern void engine_update();
extern void engine_shutdown();
/**
* @brief Win32 程序入口函数。
* @return 进程退出码。
*/
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
#if _DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
set_current_directory_to_executable_path();
if (engine_initialize())
{
MSG msg{};
bool is_running{ true };
while (is_running)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
is_running &= (msg.message != WM_QUIT);
}
engine_update();
}
}
}
#endif // USE_WITH_EDITOR
#endif // _WIN64