修复c++标准设置不正确,使用新的std::println来输出日志

This commit is contained in:
daiqingshuang 2025-04-10 13:21:35 +08:00
parent 84330d5069
commit 7f37e2098d
11 changed files with 128 additions and 73 deletions

View File

@ -4,25 +4,8 @@
cmake_minimum_required(VERSION 3.21)
project(mirage)
# C++
set(CMAKE_CXX_STANDARD 23)
#
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 使
set(CMAKE_CXX_EXTENSIONS OFF)
if (MSVC)
# utf-8
add_compile_options(/utf-8)
# MSVC __cplusplus
add_compile_options(/Zc:__cplusplus)
endif ()
if (WIN32)
# Windows
# add_definitions(-DWIN32_LEAN_AND_MEAN)
add_definitions(-DUNICODE -D_UNICODE)
endif ()
include(cmake/project_cpp_standard.cmake)
set_cpp_standard(23)
set(MIRAGE_USE_HDR OFF CACHE BOOL "Enable HDR format")
set(MIRAGE_HDR_FORMAT "SG_PIXELFORMAT_RGBA16F" CACHE STRING "Enable HDR format")

View File

@ -0,0 +1,52 @@
function(set_cpp_standard standard)
#
set(VALID_STANDARDS 11 14 17 20 23)
if(NOT ${standard} IN_LIST VALID_STANDARDS)
message(WARNING "非标准 C++ 版本: ${standard},支持的版本有: ${VALID_STANDARDS}")
endif()
# C++
set(CMAKE_CXX_STANDARD ${standard} PARENT_SCOPE)
#
set(CMAKE_CXX_STANDARD_REQUIRED ON PARENT_SCOPE)
# 使
set(CMAKE_CXX_EXTENSIONS OFF PARENT_SCOPE)
if(MSVC)
# utf-8
add_compile_options(/utf-8)
# MSVC __cplusplus
add_compile_options(/Zc:__cplusplus)
# MSVC
add_compile_options(/W4)
endif()
if(WIN32)
# Windows
add_definitions(-DUNICODE -D_UNICODE)
# WIN32_LEAN_AND_MEAN Windows
# add_definitions(-DWIN32_LEAN_AND_MEAN)
endif()
# GCC/Clang
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra)
# C++
if(${standard} GREATER 14) # 使 GREATER_EQUAL
add_compile_options(-Wshadow -Wnon-virtual-dtor)
endif()
endif()
# MinGW, 使C++17, libstdc++exp
if(MINGW)
message(STATUS "检测到MinGW编译器")
#
if(${standard} GREATER 14) # C++17
message(STATUS "为C++${standard}添加libstdc++exp库支持")
link_libraries(-lstdc++exp)
endif()
endif()
message(STATUS "已设置C++${standard}标准")
endfunction()

View File

@ -41,11 +41,11 @@ int main(int argc, char* argv[]) {
for (const char* test_str : test_cases) {
std::optional<linear_color> color = linear_color::from_string(test_str);
std::cout << "Parsing '" << test_str << "': ";
std::println(std::cout, "Parsing '{}': ", test_str);
if (color) {
std::cout << "Success -> r:" << color->r << " g:" << color->g << " b:" << color->b << " a:" << color->a << std::endl;
std::println(std::cout, "Success -> r:{}, g:{}, b:{}, a:{}", color->r, color->g, color->b, color->a);
} else {
std::cout << "Failed" << std::endl;
std::println(std::cerr, "Failed to parse color string: {}", test_str);
}
}
@ -70,29 +70,33 @@ int main(int argc, char* argv[]) {
const auto& text_block2 = std::make_shared<mtext_block>();
text_block2->set_text(U"Hello, World!");
const auto button = mnew(mbutton)
mslot(mbutton)
.margin({10})
.visibility(visibility_t::visible)
[
text_block
];
auto button = std::make_shared<mbutton>();
button->push_slot(
mslot(mbutton)
.margin({10})
.visibility(visibility_t::visible)
[
text_block
]
);
const auto button2 = mnew(mbutton)
mslot(mbutton)
.visibility(visibility_t::visible)
[
text_block2
];
// const auto button2 = mnew(mbutton)
// mslot(mbutton)
// .visibility(visibility_t::visible)
// [
// text_block2
// ];
const auto& window = mwindow::create({ 800, 600 }, L"Hello, World!");
window->set_content(
mnew(mborder)
mslot(mborder)
.h_alignment(horizontal_alignment_t::center)
.v_alignment(vertical_alignment_t::center)
mborder()
[
button
mslot(mborder)
.h_alignment(horizontal_alignment_t::center)
.v_alignment(vertical_alignment_t::center)
[
button
]
]
);

View File

@ -967,7 +967,7 @@ namespace color_space {
z /= zn;
// XYZ到Lab的变换
auto f = [](T t) {
auto f = [](T t) static {
const T delta = 6.0f / 29.0f;
return (t > std::pow(delta, 3)) ? std::pow(t, 1.0f / 3.0f) : (t / (3 * std::pow(delta, 2)) + 4.0f / 29.0f);
};
@ -998,7 +998,7 @@ namespace color_space {
const T zn = 1.08883f;
// Lab到XYZ的变换
auto f_inv = [](T t) {
auto f_inv = [](T t) static {
const T delta = 6.0f / 29.0f;
return (t > delta) ? std::pow(t, 3) : 3 * std::pow(delta, 2) * (t - 4.0f / 29.0f);
};

View File

@ -68,7 +68,7 @@ struct mirage_window_state {
*
*
*/
virtual void rebuild_swapchain(const Eigen::Vector2i& size) {}
virtual void rebuild_swapchain(const Eigen::Vector2i& size) { (void)size; }
};
/**

View File

@ -2,6 +2,8 @@
#include <iostream>
#include <ranges>
#include <utf8.h>
#include "misc/angle_literals.h"
#include <windows.h>
@ -59,8 +61,7 @@ bool windows_mirage_render_context::init() {
// 检查是否成功创建设备
if (FAILED(hr)) {
std::cerr << "mirage: " << "Failed to create D3D11 device with any driver type. HRESULT: 0x"
<< std::hex << hr << std::dec << std::endl;
std::println(std::cerr, "mirage: 所有的 D3D11 设备类型都创建失败. HRESULT: 0x{:#06x}", hr);
cleanup();
return false;
}
@ -69,7 +70,7 @@ bool windows_mirage_render_context::init() {
IDXGIDevice* dxgi_device = nullptr;
hr = device->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&dxgi_device));
if (FAILED(hr)) {
std::cerr << "mirage: Failed to get DXGI device. HRESULT: 0x" << std::hex << hr << std::dec << std::endl;
std::println(std::cerr, "mirage: 获取 DXGI 设备失败. HRESULT: 0x{:#06x}", hr);
cleanup();
return false;
}
@ -80,7 +81,7 @@ bool windows_mirage_render_context::init() {
dxgi_device->Release();
if (FAILED(hr)) {
std::cerr << "mirage: Failed to get DXGI adapter. HRESULT: 0x" << std::hex << hr << std::dec << std::endl;
std::println(std::cerr, "mirage: 无法获取 DXGI 适配器. HRESULT: 0x{:#06x}", hr);
cleanup();
return false;
}
@ -89,46 +90,46 @@ bool windows_mirage_render_context::init() {
DXGI_ADAPTER_DESC adapter_desc;
hr = dxgi_adapter->GetDesc(&adapter_desc);
if (FAILED(hr)) {
std::wcerr << L"mirage: Failed to get DXGI Adapter description" << std::endl;
std::println(std::cerr, "mirage: 无法获取 DXGI 适配器描述信息. HRESULT: 0x{:#06x}", hr);
}
// **尝试获取最新的DXGI工厂版本 - 从最新的Factory6开始尝试**
// Windows 10 Fall Creators Update (1709)或更高版本支持
hr = dxgi_adapter->GetParent(__uuidof(IDXGIFactory6), reinterpret_cast<void**>(&dxgi_factory));
if (SUCCEEDED(hr)) {
std::cout << "mirage: Using IDXGIFactory6" << std::endl;
std::println(std::cout, "mirage: 使用 DXGI Factory6");
} else {
// 尝试Factory5 - Windows 10 Anniversary Update (1607)或更高版本
hr = dxgi_adapter->GetParent(__uuidof(IDXGIFactory5), reinterpret_cast<void**>(&dxgi_factory));
if (SUCCEEDED(hr)) {
std::cout << "mirage: Using IDXGIFactory5" << std::endl;
std::println(std::cout, "mirage: 使用 DXGI Factory5");
} else {
// 尝试Factory4 - Windows 10初始版本
hr = dxgi_adapter->GetParent(__uuidof(IDXGIFactory4), reinterpret_cast<void**>(&dxgi_factory));
if (SUCCEEDED(hr)) {
std::cout << "mirage: Using IDXGIFactory4" << std::endl;
std::println(std::cout, "mirage: 使用 DXGI Factory4");
} else {
// 尝试Factory3 - Windows 8.1
hr = dxgi_adapter->GetParent(__uuidof(IDXGIFactory3), reinterpret_cast<void**>(&dxgi_factory));
if (SUCCEEDED(hr)) {
std::cout << "mirage: Using IDXGIFactory3" << std::endl;
std::println(std::cout, "mirage: 使用 DXGI Factory3");
} else {
// 尝试Factory2 - Windows 8支持FLIP模式交换链
hr = dxgi_adapter->GetParent(__uuidof(IDXGIFactory2), reinterpret_cast<void**>(&dxgi_factory));
if (SUCCEEDED(hr)) {
std::cout << "mirage: Using IDXGIFactory2" << std::endl;
std::println(std::cout, "mirage: 使用 DXGI Factory2");
} else {
// 回退到基本Factory1 - Windows 7
hr = dxgi_adapter->GetParent(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&dxgi_factory));
if (SUCCEEDED(hr)) {
std::cout << "mirage: Using IDXGIFactory1" << std::endl;
std::println(std::cout, "mirage: 使用 DXGI Factory1");
} else {
// 最后尝试原始Factory
hr = dxgi_adapter->GetParent(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&dxgi_factory));
if (SUCCEEDED(hr)) {
std::cout << "mirage: Using IDXGIFactory" << std::endl;
std::println(std::cout, "mirage: 使用 DXGI Factory");
} else {
std::cerr << "mirage: Failed to get any DXGI Factory" << std::endl;
std::println(std::cerr, "mirage: 无法获取 DXGI Factory. HRESULT: 0x{:#06x}", hr);
dxgi_adapter->Release();
cleanup();
return false;
@ -149,7 +150,7 @@ bool windows_mirage_render_context::init() {
DXGI_FEATURE_PRESENT_ALLOW_TEARING,
&allow_tearing,
sizeof(allow_tearing)))) {
std::cout << "mirage: Tearing support: " << (allow_tearing ? "Yes" : "No") << std::endl;
std::println(std::cout, "mirage: 防撕裂支持: {}", (bool)allow_tearing);
}
factory5->Release();
}
@ -157,9 +158,15 @@ bool windows_mirage_render_context::init() {
dxgi_adapter->Release();
// 输出初始化成功信息
std::cout << "mirage: D3D11 device created successfully" << std::endl;
std::wcout << L"mirage: Using adapter: " << adapter_desc.Description << std::endl;
std::println(std::cout, "mirage: 成功创建 D3D11 设备");
{
std::u16string adapter_desc_str((const char16_t*)adapter_desc.Description);
auto str = utf8::utf16to8(adapter_desc_str);
std::println(std::cout, "mirage: 适配器名称: {}", str);
}
std::println(std::cout, "mirage: 适配器 VRAM: {} MB", adapter_desc.DedicatedVideoMemory / 1024 / 1024);
// 输出驱动类型信息
auto driver_type_str = "Unknown";
@ -172,7 +179,7 @@ bool windows_mirage_render_context::init() {
break;
default: ;
}
std::cout << "mirage: Using driver type: " << driver_type_str << std::endl;
std::println(std::cout, "mirage: 使用驱动类型: {}", driver_type_str);
// 输出特性级别信息
auto feature_level_str = "Unknown";
@ -187,11 +194,11 @@ bool windows_mirage_render_context::init() {
break;
default: ;
}
std::cout << "mirage: Using feature level: " << feature_level_str << std::endl;
std::println(std::cout, "mirage: 使用特性级别: {}", feature_level_str);
return true;
} catch (const std::exception& e) {
std::cerr << "mirage: Exception during D3D11 initialization: " << e.what() << std::endl;
std::println(std::cerr, "mirage: D3D11初始化失败: {}", e.what());
cleanup();
return false;
}

View File

@ -202,7 +202,7 @@ void windows_render_state::rebuild_swapchain(const Eigen::Vector2i& size) {
IDXGISwapChain4* swapChain4 = nullptr;
if (SUCCEEDED(dx_swap_chain->QueryInterface(__uuidof(IDXGISwapChain4), (void**)&swapChain4))) {
#if DEBUG
std::cout << "使用IDXGISwapChain4重建交换链" << std::endl;
std::println(std::cout, "mirage: 使用IDXGISwapChain4重建交换链");
#endif
// 保留当前交换链标志
@ -225,7 +225,7 @@ void windows_render_state::rebuild_swapchain(const Eigen::Vector2i& size) {
else {
IDXGISwapChain1* swapChain1 = nullptr;
if (SUCCEEDED(dx_swap_chain->QueryInterface(__uuidof(IDXGISwapChain1), (void**)&swapChain1))) {
std::cout << "使用IDXGISwapChain1重建交换链" << std::endl;
std::println(std::cout, "mirage: 使用IDXGISwapChain1重建交换链");
// 保留当前交换链标志
DXGI_SWAP_CHAIN_DESC1 desc = {};
@ -248,7 +248,7 @@ void windows_render_state::rebuild_swapchain(const Eigen::Vector2i& size) {
}
// 回退到基础IDXGISwapChain
else {
std::cout << "使用基础IDXGISwapChain重建交换链" << std::endl;
std::println(std::cout, "mirage: 使用基础IDXGISwapChain重建交换链");
// 获取基础交换链描述
DXGI_SWAP_CHAIN_DESC desc = {};
@ -323,6 +323,6 @@ void windows_render_state::rebuild_swapchain(const Eigen::Vector2i& size) {
context->Release();
#if DEBUG
std::cout << "交换链重建成功,新尺寸: " << size.x() << "x" << size.y() << std::endl;
std::println(std::cout, "mirage: 交换链重建成功,新尺寸: {} x {}", size.x(), size.y());
#endif
}

View File

@ -29,34 +29,34 @@ void mbutton::on_paint(mirage_paint_context& in_context) {
void mbutton::on_click(const Eigen::Vector2f& in_position, mouse_button in_button) {
mborder::on_click(in_position, in_button);
std::cout << this << ": Button clicked!" << in_position.x() << ", " << in_position.y() << std::endl;
std::println(std::cout, "点击 Button: {}, {}", in_position.x(), in_position.y());
color_ = {0.2, 0.2, 0.2, 1};
invalidate(invalidate_reason::paint);
}
void mbutton::on_double_click(const Eigen::Vector2f& in_position, mouse_button in_button) {
mborder::on_double_click(in_position, in_button);
std::cout << this << ": Button double clicked!" << in_position.x() << ", " << in_position.y() << std::endl;
std::println(std::cout, "双击 Button: {}, {}", in_position.x(), in_position.y());
invalidate(invalidate_reason::paint);
}
void mbutton::on_mouse_enter() {
mborder::on_mouse_enter();
std::cout << this << ": Mouse entered!" << std::endl;
std::println(std::cout, "鼠标进入 Button");
color_ = {0.2, 0.2, 0.2, 1};
invalidate(invalidate_reason::paint);
}
void mbutton::on_mouse_leave() {
mborder::on_mouse_leave();
std::cout << this << ": Mouse left!" << std::endl;
std::println(std::cout, "鼠标离开 Button");
color_ = {0.1, 0.1, 0.1, 1};
invalidate(invalidate_reason::paint);
}
hit_test_handle mbutton::on_mouse_button_down(const Eigen::Vector2f& in_position, mouse_button in_button) {
mborder::on_mouse_button_down(in_position, in_button);
std::cout << this << ": Mouse pressed!" << in_position.x() << ", " << in_position.y() << std::endl;
std::println(std::cout, "鼠标按下 Button: {}, {}", in_position.x(), in_position.y());
color_ = {0.1, 0.1, 0.1, 1};
invalidate(invalidate_reason::paint);
return hit_test_handle::handled();
@ -64,7 +64,7 @@ hit_test_handle mbutton::on_mouse_button_down(const Eigen::Vector2f& in_position
hit_test_handle mbutton::on_mouse_button_up(const Eigen::Vector2f& in_position, mouse_button in_button) {
mborder::on_mouse_button_up(in_position, in_button);
std::cout << this << ": Mouse released!" << in_position.x() << ", " << in_position.y() << std::endl;
std::println(std::cout, "鼠标释放 Button: {}, {}", in_position.x(), in_position.y());
color_ = {0.2, 0.2, 0.2, 1};
invalidate(invalidate_reason::paint);
return hit_test_handle::handled();

View File

@ -93,6 +93,10 @@ public:
}
return slot_;
}
auto operator[](const SlotType& in_slot) {
push_slot(in_slot);
return shared_from_this();
}
//-------------- 尺寸计算 --------------

View File

@ -123,6 +123,11 @@ public:
const auto& get_slots() const {
return slots_;
}
auto operator[](auto... in_slot) {
for (const auto& slot: { in_slot... }) { add_slot(slot); }
return shared_from_this();
}
private:
std::vector<SlotType> slots_;
};

View File

@ -55,5 +55,5 @@ struct mwidget_decl {
std::shared_ptr<WidgetType> widget_;
};
#define mslot(type) +type::slot_type()
#define mslot(type) type::slot_type()
#define mnew(type, ...) mwidget_decl<type>(__VA_ARGS__) <<= mwidget_slot_guard<type>()