1
0

finish buggy dx11 code

This commit is contained in:
2026-01-04 23:11:58 +08:00
parent a66d3dee8b
commit 7b234ec405
12 changed files with 432 additions and 219 deletions

View File

@@ -5,6 +5,7 @@ PRIVATE
pipe_operator.cpp
engine.cpp
deliver.cpp
directx_util.cpp
)
target_sources(BasaltShared
PUBLIC
@@ -16,6 +17,7 @@ FILES
pipe_operator.hpp
engine.hpp
deliver.hpp
directx_util.hpp
)
target_include_directories(BasaltShared
PUBLIC

View File

@@ -0,0 +1,67 @@
#include "directx_util.hpp"
#include <stdexcept>
#include <string>
namespace Basalt::Shared::DirectX {
// Window procedure for the render window
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CLOSE:
PostQuitMessage(0);
return 0;
default:
break;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
// Create a render window for DirectX
HWND CreateRenderWindow(std::uint32_t width, std::uint32_t height, const std::wstring_view& title) {
static bool g_CLSREG = false;
constexpr wchar_t class_name[] = L"DirectXRenderWindowClass";
std::wstring c_title(title);
if (!g_CLSREG) {
WNDCLASSEXW wc = {0};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandleW(nullptr);
wc.hCursor = LoadCursorW(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszClassName = class_name;
if (!RegisterClassExW(&wc)) {
throw std::runtime_error("Failed to register window class");
}
g_CLSREG = true;
}
// Calculate window size including borders
RECT rect = {0, 0, static_cast<LONG>(width), static_cast<LONG>(height)};
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
HWND hwnd = CreateWindowExW(0,
class_name,
c_title.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
rect.right - rect.left,
rect.bottom - rect.top,
nullptr,
nullptr,
GetModuleHandleW(nullptr),
nullptr);
if (!hwnd) {
throw std::runtime_error("Failed to create render window");
}
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
return hwnd;
}
}

View File

@@ -0,0 +1,10 @@
#pragma once
#include <cinttypes>
#include <string_view>
#include <windows.h>
namespace Basalt::Shared::DirectX {
HWND CreateRenderWindow(std::uint32_t width, std::uint32_t height, const std::wstring_view& title);
}

View File

@@ -16,13 +16,13 @@ namespace Basalt::Shared {
// Create Windows pipe name from given name
auto fullname = std::format(BSTEXT("\\\\.\\pipe\\{}"), name);
m_Handle = CreateFileW(fullname.c_str(), // 管道名称
GENERIC_READ | GENERIC_WRITE, // 读写权限
0, // 不共享
NULL, // 默认安全属性
OPEN_EXISTING, // 打开已存在的管道
0, // 默认属性
NULL // 无模板文件
m_Handle = CreateFileW(fullname.c_str(), // 管道名称
GENERIC_READ | GENERIC_WRITE, // 读写权限
0, // 不共享
NULL, // 默认安全属性
OPEN_EXISTING, // 打开已存在的管道
0, // 默认属性
NULL // 无模板文件
);
if (m_Handle == BAD_PIPE_HANDLE) {
@@ -80,16 +80,16 @@ namespace Basalt::Shared {
void PipeOperator::Read(void *buffer, size_t size) {
if (size == 0) {
return; // 读取0字节直接返回
return; // 读取0字节直接返回
}
#if defined(BASALT_OS_WINDOWS)
DWORD bytesRead = 0;
BOOL success = ReadFile(m_Handle, // 管道句柄
buffer, // 缓冲区
static_cast<DWORD>(size), // 缓冲区大小
&bytesRead, // 实际读取的字节数
NULL // 不使用重叠I/O
BOOL success = ReadFile(m_Handle, // 管道句柄
buffer, // 缓冲区
static_cast<DWORD>(size), // 缓冲区大小
&bytesRead, // 实际读取的字节数
NULL // 不使用重叠I/O
);
if (!success) {
@@ -111,7 +111,7 @@ namespace Basalt::Shared {
}
if (bytesRead == 0) {
// 管道已关闭或到达末尾
// 管道已关闭或到达末尾
throw std::runtime_error("Named pipe closed during read.");
}
@@ -126,16 +126,16 @@ namespace Basalt::Shared {
void PipeOperator::Write(const void *buffer, size_t size) {
if (size == 0) {
return; // 写入0字节直接返回
return; // 写入0字节直接返回
}
#if defined(BASALT_OS_WINDOWS)
DWORD bytesWritten = 0;
BOOL success = WriteFile(m_Handle, // 管道句柄
buffer, // 数据缓冲区
static_cast<DWORD>(size), // 数据大小
&bytesWritten, // 实际写入的字节数
NULL // 不使用重叠I/O
BOOL success = WriteFile(m_Handle, // 管道句柄
buffer, // 数据缓冲区
static_cast<DWORD>(size), // 数据大小
&bytesWritten, // 实际写入的字节数
NULL // 不使用重叠I/O
);
if (!success) {