From 94c19cf117be508443a253b70fcfe5f7ca57f58a Mon Sep 17 00:00:00 2001 From: DevWiki Date: Fri, 22 Dec 2023 19:05:40 +0800 Subject: [PATCH] =?UTF-8?q?debug=20test=E6=AD=A3=E5=B8=B8=E8=BF=90?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DeviceManager/.gitignore | 2 + DeviceManager/CMakeLists.txt | 45 +++++++++++------ WinDevice/.gitignore | 2 +- WinDevice/CMakeLists.txt | 40 +++++++++------ WinDevice/CMakeSettings.json | 15 ------ WinDevice/src/Utils/StringUtil.h | 10 +--- WinDevice/src/Video/ScreenManager.cpp | 72 +++++++++++++-------------- WinDevice/src/Video/ScreenManager.h | 4 +- WinDevice/src/main.cpp | 8 +++ 9 files changed, 103 insertions(+), 95 deletions(-) create mode 100644 DeviceManager/.gitignore delete mode 100644 WinDevice/CMakeSettings.json create mode 100644 WinDevice/src/main.cpp diff --git a/DeviceManager/.gitignore b/DeviceManager/.gitignore new file mode 100644 index 0000000..c6b92b8 --- /dev/null +++ b/DeviceManager/.gitignore @@ -0,0 +1,2 @@ +/cmake-* +/output diff --git a/DeviceManager/CMakeLists.txt b/DeviceManager/CMakeLists.txt index 6efc417..035dcb0 100644 --- a/DeviceManager/CMakeLists.txt +++ b/DeviceManager/CMakeLists.txt @@ -49,26 +49,41 @@ endif() target_link_libraries(DeviceManager PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) +set(WinDevice_INCLUDE_DIR "../WinDevice/output/include/") +set(WinDevice_THIRD_INCLUDE_DIR "../WinDevice/output/third_lib/") if (CMAKE_BUILD_TYPE STREQUAL "Debug") - # Debug 模式下,添加 WinDevice 项目作为子项目 - add_subdirectory(../WinDevice WinDevice) - # 链接WinDevice项目 - target_include_directories(DeviceManager PRIVATE - ../WinDevice/src - include/third_lib - ) - target_link_libraries(DeviceManager PRIVATE WinDevice) + set(WinDevice_LIB_DIR "../WinDevice/output/debug/") else() - # Release 模式下,链接 WinDevice 生成的库文件 - target_link_libraries(DeviceManager PRIVATE WinDevice) - # 在 Release 模式下,添加 WinDevice 的头文件路径 - target_include_directories(DeviceManager PRIVATE - ../WinDevice/lib - ) + set(WinDevice_LIB_DIR "../WinDevice/output/release/") endif() +# 使用 find_library 查找动态库文件 +find_library(WinDevice_LIB + NAMES WinDevice + HINTS ${WinDevice_LIB_DIR} +) +# 添加头文件目录 +include_directories(${WinDevice_INCLUDE_DIR} ${WinDevice_THIRD_INCLUDE_DIR}) +# 将找到的库链接到你的目标 +target_link_libraries(DeviceManager PRIVATE ${WinDevice_LIB}) +# if (CMAKE_BUILD_TYPE STREQUAL "Debug") +# # Debug 模式下,添加 WinDevice 项目作为子项目 +# add_subdirectory(../WinDevice WinDevice) +# # 链接WinDevice项目 +# target_include_directories(DeviceManager PRIVATE +# ../WinDevice/src +# include/third_lib +# ) +# target_link_libraries(DeviceManager PRIVATE WinDevice) +# else() +# # Release 模式下,链接 WinDevice 生成的库文件 +# target_link_libraries(DeviceManager PRIVATE WinDevice) +# # 在 Release 模式下,添加 WinDevice 的头文件路径 +# target_include_directories(DeviceManager PRIVATE +# ../WinDevice/lib +# ) +# endif() -## target_link_libraries(DeviceManager PRIVATE dxgi) set_target_properties(DeviceManager PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com diff --git a/WinDevice/.gitignore b/WinDevice/.gitignore index 5c8f679..c6b92b8 100644 --- a/WinDevice/.gitignore +++ b/WinDevice/.gitignore @@ -1,2 +1,2 @@ /cmake-* -/output \ No newline at end of file +/output diff --git a/WinDevice/CMakeLists.txt b/WinDevice/CMakeLists.txt index 40fa835..12ee2ed 100644 --- a/WinDevice/CMakeLists.txt +++ b/WinDevice/CMakeLists.txt @@ -27,13 +27,6 @@ if (ENABLE_UTF8_SOURCE) endif() endif() -# 使用通配符自动列举源文件 -file(GLOB_RECURSE SOURCE_FILES "./src/*") -# 添加功能模块的源文件 -add_library(WinDevice SHARED - ${SOURCE_FILES} -) - # DirectX目录 set(Windows_Kits_DIR "C:\\Program Files (x86)\\Windows Kits\\10") set(Windows_Kits_Version "10.0.19041.0") @@ -48,24 +41,33 @@ else() set(CMAKE_LIBRARY_ARCHITECTURE x86) set(DirectX_SDK_LIB_DIR "${Windows_Kits_DIR}\\Lib\\${Windows_Kits_Version}\\um\\x86") endif() +# 获取所有 DirectX 相关的库文件 +file(GLOB DirectX_LIBS + "${DirectX_SDK_LIB_DIR}/d3d9.lib" + "${DirectX_SDK_LIB_DIR}/d3d10.lib" + "${DirectX_SDK_LIB_DIR}/d3d11.lib" + "${DirectX_SDK_LIB_DIR}/d3d12.lib" + "${DirectX_SDK_LIB_DIR}/dxgi.lib" + "${DirectX_SDK_LIB_DIR}/ddraw.lib" +) +# 使用通配符自动列举源文件 +file(GLOB_RECURSE SOURCE_FILES "./src/*") +set(Third_Include_DIR "include/third_lib/") +# 添加功能模块的源文件 +add_library(WinDevice SHARED + ${SOURCE_FILES} +) # 设置WinDevice库的包含目录 target_include_directories(WinDevice PUBLIC $ PRIVATE ${DirectX_SDK_INCLUDE_DIR} - include/third_lib + ${Third_Include_DIR} ) - # 链接 d3d 的 lib -target_link_libraries(WinDevice PRIVATE - "${DirectX_SDK_LIB_DIR}\\d3d9.lib" - "${DirectX_SDK_LIB_DIR}\\d3d10.lib" - "${DirectX_SDK_LIB_DIR}\\d3d11.lib" - "${DirectX_SDK_LIB_DIR}\\d3d12.lib" - "${DirectX_SDK_LIB_DIR}\\ddraw.lib" - "${DirectX_SDK_LIB_DIR}\\dxgi.lib") +target_link_libraries(WinDevice PRIVATE ${DirectX_LIBS}) # 创建 output 目录 set(OUTPUT_DIR ${CMAKE_SOURCE_DIR}/output) @@ -75,6 +77,12 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug") # Debug 模式下的配置 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG") set(LIBRARY_OUTPUT_PATH "${OUTPUT_DIR}/debug") + # 添加测试 + add_executable(WinDeviceTest "src/main.cpp" ${SOURCE_FILES}) + target_include_directories(WinDeviceTest + PUBLIC $ + PRIVATE ${Third_Include_DIR} ${DirectX_SDK_INCLUDE_DIR}) + target_link_libraries(WinDeviceTest PRIVATE ${DirectX_LIBS}) else() # Release 模式下的配置 set(LIBRARY_OUTPUT_PATH "${OUTPUT_DIR}/release") diff --git a/WinDevice/CMakeSettings.json b/WinDevice/CMakeSettings.json deleted file mode 100644 index 9204f06..0000000 --- a/WinDevice/CMakeSettings.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "configurations": [ - { - "name": "x64-Debug", - "generator": "Ninja", - "configurationType": "Debug", - "inheritEnvironments": [ "msvc_x64_x64" ], - "buildRoot": "${projectDir}\\out\\build\\${name}", - "installRoot": "${projectDir}\\out\\install\\${name}", - "cmakeCommandArgs": "", - "buildCommandArgs": "", - "ctestCommandArgs": "" - } - ] -} \ No newline at end of file diff --git a/WinDevice/src/Utils/StringUtil.h b/WinDevice/src/Utils/StringUtil.h index 1236658..5516d60 100644 --- a/WinDevice/src/Utils/StringUtil.h +++ b/WinDevice/src/Utils/StringUtil.h @@ -1,7 +1,4 @@ -#ifndef STRING_UTIL_H -#define STRING_UTIL_H - -#include +#include #include #include @@ -18,7 +15,4 @@ std::string Wstring2String(const std::wstring& input) { std::string Wchar2String(const WCHAR* input) { std::wstring_convert> converter; return converter.to_bytes(input); -} - - -#endif // STRING_CONVERSION_H \ No newline at end of file +} \ No newline at end of file diff --git a/WinDevice/src/Video/ScreenManager.cpp b/WinDevice/src/Video/ScreenManager.cpp index 7b03bf1..dd7ffda 100644 --- a/WinDevice/src/Video/ScreenManager.cpp +++ b/WinDevice/src/Video/ScreenManager.cpp @@ -1,10 +1,8 @@ #include "ScreenManager.h" -#include #include "spdlog/spdlog.h" -#include "spdlog/sinks/basic_file_sink.h" -#include "spdlog/sinks/stdout_color_sinks.h" #include "Utils/StringUtil.h" #include "Utils/Log.h" +#include ScreenManager::ScreenManager() { @@ -43,43 +41,43 @@ void ScreenManager::_UpdateDisplayDeviceList() void ScreenManager::_UpdateDisplayAdapterList() { - LOG_FUNC_START(); - HRESULT hr = S_OK; - IDXGIFactory* pFactory = nullptr; - hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&pFactory)); - if (FAILED(hr)) { - spdlog::error("CreateDXGIFactory failed"); - return; - } + LOG_FUNC_START(); + HRESULT hr = S_OK; + IDXGIFactory* pFactory = nullptr; + hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&pFactory)); + if (FAILED(hr)) { + spdlog::error("CreateDXGIFactory failed"); + return; + } - IDXGIAdapter* pAdapter = nullptr; - for (UINT adapterIndex = 0; pFactory->EnumAdapters(adapterIndex, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++adapterIndex) - { - DXGI_ADAPTER_DESC adapterDesc; - pAdapter->GetDesc(&adapterDesc); - spdlog::info("Adapter Index:{0}, Description:{1}, DeviceId:{2}, VendorId:{3}, SubSysId:{4}, Revision:{5}, AdapterLuid(H-L):{6}-{7} ", - adapterIndex, Wchar2String(adapterDesc.Description), adapterDesc.DeviceId, adapterDesc.VendorId, adapterDesc.SubSysId, - adapterDesc.Revision, adapterDesc.AdapterLuid.HighPart, adapterDesc.AdapterLuid.LowPart); + IDXGIAdapter* pAdapter = nullptr; + for (UINT adapterIndex = 0; pFactory->EnumAdapters(adapterIndex, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++adapterIndex) + { + DXGI_ADAPTER_DESC adapterDesc; + pAdapter->GetDesc(&adapterDesc); + spdlog::info("Adapter Index:{0}, Description:{1}, DeviceId:{2}, VendorId:{3}, SubSysId:{4}, Revision:{5}, AdapterLuid(H-L):{6}-{7} ", + adapterIndex, Wchar2String(adapterDesc.Description), adapterDesc.DeviceId, adapterDesc.VendorId, adapterDesc.SubSysId, + adapterDesc.Revision, adapterDesc.AdapterLuid.HighPart, adapterDesc.AdapterLuid.LowPart); - // print adapter output info - IDXGIOutput* pOutput; - int outputCount = 0; - for (UINT j = 0; pAdapter->EnumOutputs(j, &pOutput) != DXGI_ERROR_NOT_FOUND; ++j) { - DXGI_OUTPUT_DESC outputDesc; - pOutput->GetDesc(&outputDesc); + // print adapter output info + IDXGIOutput* pOutput; + int outputCount = 0; + for (UINT j = 0; pAdapter->EnumOutputs(j, &pOutput) != DXGI_ERROR_NOT_FOUND; ++j) { + DXGI_OUTPUT_DESC outputDesc; + pOutput->GetDesc(&outputDesc); - MONITORINFOEX monitorInfo; - monitorInfo.cbSize = sizeof(MONITORINFOEX); - if (GetMonitorInfo(outputDesc.Monitor, &monitorInfo)) - { - // 输出友好名称 - spdlog::info("Adapter Output Index:{0}, DeviceName:{1}, szDevice:{2}, right:{3}, bottom:{4}", - j, Wchar2String(outputDesc.DeviceName), monitorInfo.szDevice, monitorInfo.rcMonitor.right, monitorInfo.rcMonitor.bottom); - } - } - } - pFactory->Release(); - LOG_FUNC_END(); + MONITORINFOEX monitorInfo; + monitorInfo.cbSize = sizeof(MONITORINFOEX); + if (GetMonitorInfo(outputDesc.Monitor, &monitorInfo)) + { + // 输出友好名称 + spdlog::info("Adapter Output Index:{0}, DeviceName:{1}, szDevice:{2}, right:{3}, bottom:{4}", + j, Wchar2String(outputDesc.DeviceName), monitorInfo.szDevice, monitorInfo.rcMonitor.right, monitorInfo.rcMonitor.bottom); + } + } + } + pFactory->Release(); + LOG_FUNC_END(); } BOOL ScreenManager::_EnumMonitorProc(HMONITOR hMonitor) diff --git a/WinDevice/src/Video/ScreenManager.h b/WinDevice/src/Video/ScreenManager.h index 5e1280c..b65333b 100644 --- a/WinDevice/src/Video/ScreenManager.h +++ b/WinDevice/src/Video/ScreenManager.h @@ -1,9 +1,7 @@ #pragma once #include #include - -#include -#include +#include class ScreenManager { diff --git a/WinDevice/src/main.cpp b/WinDevice/src/main.cpp new file mode 100644 index 0000000..bf53b39 --- /dev/null +++ b/WinDevice/src/main.cpp @@ -0,0 +1,8 @@ +// +// Created by zyz on 2023/12/20. +// +#include "Video/ScreenManager.h" +int main() { + ScreenManager screenManager; + screenManager.UpdateDisplayInfo(); +} \ No newline at end of file