feat: initial DX12 foundation framework

This commit is contained in:
SpecialX
2026-03-19 18:27:49 +08:00
commit 60f73b525d
70 changed files with 8993 additions and 0 deletions

103
Engine/Core/EngineWin32.cpp Normal file
View File

@@ -0,0 +1,103 @@
/**
* @file EngineWin32.cpp
* @brief Win32 运行模式下的引擎生命周期实现。
* @details
* 该文件负责连接平台消息循环与引擎帧循环:
* - 初始化阶段加载内容并创建主窗口;
* - 更新阶段驱动脚本系统与帧间休眠;
* - 关闭阶段销毁窗口并卸载内容资源。
*/
#if !defined(SHIPPING) && defined(_WIN64)
#include "..\Content\ContentLoader.h"
#include "..\Components\Script.h"
#include "..\Platform\PlatformTypes.h"
#include "..\Platform\Platform.h"
#include "..\Graphics\Renderer.h"
#include <thread>
using namespace XEngine;
namespace {
/**
* @brief 游戏窗口渲染表面实例。
*/
graphics::render_surface game_window{};
/**
* @brief 主窗口消息回调。
* @param hwnd 窗口句柄。
* @param msg 消息类型。
* @param wparam 消息参数。
* @param lparam 消息参数。
* @return 消息处理结果。
*/
LRESULT win_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_DESTROY:
{
if (game_window.window.is_closed())
{
PostQuitMessage(0);
return 0;
}
}
case WM_SYSCHAR:
if (wparam == VK_RETURN && (HIWORD(lparam) & KF_ALTDOWN))
{
game_window.window.set_fullscreen(!game_window.window.is_fullscreen());
return 0;
}
default:
break;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
}
/**
* @brief 初始化引擎运行环境。
* @return 初始化成功返回 true。
*/
bool
engine_initialize()
{
if (!XEngine::content::load_games()) return false;
platform::window_init_info info
{
&win_proc, nullptr, L"XGame"
};
game_window.window = platform::create_window(&info);
if (!game_window.window.is_valid()) return false;
return true;
}
/**
* @brief 执行单帧更新。
*/
void
engine_update()
{
XEngine::script::update(10.f);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
/**
* @brief 关闭引擎并释放运行时资源。
*/
void
engine_shutdown()
{
platform::remove_window(game_window.window.get_id());
XEngine::content::unload_game();
}
#endif //!defined(SHIPPING)

77
Engine/Core/MainWin32.cpp Normal file
View File

@@ -0,0 +1,77 @@
/**
* @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