From 7fa68792fb330e3a195e888f35b076e700108281 Mon Sep 17 00:00:00 2001 From: Asuro Date: Fri, 22 Jul 2022 21:48:04 +0200 Subject: [PATCH] Use exe path to search for fonts --- AsuroTool/AsuroTool.cpp | 15 +++++++++++++-- AsuroTool/AsuroTool.h | 1 - AsuroTool/AsuroTool.vcxproj | 8 ++++++++ AsuroTool/Settings.cpp | 7 ++----- AsuroTool/Util.cpp | 22 ++++++++++++++++++++++ AsuroTool/Util.h | 2 ++ ImguiBase/ImguiBase.vcxproj | 8 ++++++++ ImguiNodes/ImguiNodes.vcxproj | 4 ++++ 8 files changed, 59 insertions(+), 8 deletions(-) diff --git a/AsuroTool/AsuroTool.cpp b/AsuroTool/AsuroTool.cpp index 49572e2..7e050d8 100644 --- a/AsuroTool/AsuroTool.cpp +++ b/AsuroTool/AsuroTool.cpp @@ -23,6 +23,7 @@ #include "resource.h" #include "AsuroTool.h" +const size_t MAX_FONT_PATH_LENGTH = 2048; const UINT TRAY_ID = 420; const UINT WMAPP_NOTIFYCALLBACK = WM_APP + 1; @@ -46,20 +47,30 @@ int main() void init(DrawData& drawData, ApplicationData& appData) { + std::wstring appPath; + getAppDir(appPath); + char appPathStr[MAX_FONT_PATH_LENGTH]; + HRESULT convResult = WideCharToMultiByte(CP_UTF8, NULL, &appPath[0], -1, appPathStr, MAX_FONT_PATH_LENGTH, NULL, nullptr); + isError(convResult, "Failed to convert path: "); + // sad :( gDrawData = &drawData; gAppData = &appData; // Load text font ImGuiIO& io = ImGui::GetIO(); - io.Fonts->AddFontFromFileTTF("Montserrat-Regular.ttf", 18.0f); + std::string fontPath = std::string(appPathStr); + fontPath.append("\\Montserrat-Regular.ttf"); + io.Fonts->AddFontFromFileTTF(fontPath.c_str(), 18.0f); // Load icon font static const ImWchar icons_ranges[] = { 0xEA01, 0xF2DF, 0 }; ImFontConfig icons_config; icons_config.MergeMode = true; icons_config.PixelSnapH = true; - io.Fonts->AddFontFromFileTTF("remixicon.ttf", 14.0f, &icons_config, icons_ranges); + std::string iconFontPath = std::string(appPathStr); + iconFontPath.append("\\remixicon.ttf"); + io.Fonts->AddFontFromFileTTF(iconFontPath.c_str(), 14.0f, &icons_config, icons_ranges); // Set window icon HINSTANCE instance = GetModuleHandle(NULL); diff --git a/AsuroTool/AsuroTool.h b/AsuroTool/AsuroTool.h index 3540297..0e6004e 100644 --- a/AsuroTool/AsuroTool.h +++ b/AsuroTool/AsuroTool.h @@ -13,5 +13,4 @@ ImVec2 menuBar(DrawData& drawData, ApplicationData& appData); ImVec2 audioDeviceWindow(ApplicationData& appData, std::vector& deviceList, const char* title); void drawCircle(float radius, ImU32 color); -void GenerateTrayUUID(UUID* uuid); LRESULT trayIconEventHandler(int code, WPARAM wParam, LPARAM lParam); diff --git a/AsuroTool/AsuroTool.vcxproj b/AsuroTool/AsuroTool.vcxproj index 2c82626..c60cb41 100644 --- a/AsuroTool/AsuroTool.vcxproj +++ b/AsuroTool/AsuroTool.vcxproj @@ -97,6 +97,8 @@ Console true + pathcch.lib;%(AdditionalDependencies) + msvcrt.lib @@ -114,6 +116,8 @@ true true true + pathcch.lib;%(AdditionalDependencies) + msvcrt.lib @@ -127,6 +131,8 @@ Console true + pathcch.lib;%(AdditionalDependencies) + msvcrt.lib @@ -144,6 +150,8 @@ true true true + pathcch.lib;%(AdditionalDependencies) + msvcrt.lib diff --git a/AsuroTool/Settings.cpp b/AsuroTool/Settings.cpp index 126fea8..8565bca 100644 --- a/AsuroTool/Settings.cpp +++ b/AsuroTool/Settings.cpp @@ -81,12 +81,9 @@ void setAutostart(bool newValue) if (newValue) { std::wstring appPath; - appPath.resize(MAX_PATH_LENGTH); - hr = GetModuleFileName(NULL, &appPath[0], static_cast(appPath.size())); - if (isError(hr, "Failed to get executable name: ")) return; - appPath.resize(wcslen(appPath.data())); + if (FAILED(getAppPath(appPath))) return; - hr = RegSetValueEx(runKey, KEY_APP_NAME, 0, REG_SZ, (BYTE*)appPath.c_str(), (appPath.size() + 1) * sizeof(wchar_t)); + hr = RegSetValueEx(runKey, KEY_APP_NAME, 0, REG_SZ, (BYTE*)appPath.c_str(), static_cast((appPath.size() + 1) * sizeof(wchar_t))); if (isError(hr, "Failed to write autostart key: ")) return; } else diff --git a/AsuroTool/Util.cpp b/AsuroTool/Util.cpp index a4a35b8..ade34c1 100644 --- a/AsuroTool/Util.cpp +++ b/AsuroTool/Util.cpp @@ -1,6 +1,7 @@ #include #include "Util.h" +#include "pathcch.h" bool isError(const HRESULT result, const std::stringstream message) { @@ -29,3 +30,24 @@ std::string utf8Encode(const std::wstring& wstr) WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &resultString[0], sizeNeeded, NULL, NULL); return resultString; } + +HRESULT getAppPath(std::wstring& outPath) +{ + const size_t MAX_PATH_LENGTH = 32767; + outPath.resize(MAX_PATH_LENGTH); + HRESULT hr = GetModuleFileName(NULL, &outPath[0], static_cast(outPath.size())); + if (isError(hr, "Failed to get executable name: ")) return hr; + outPath.resize(wcslen(outPath.data())); + return hr; +} + +HRESULT getAppDir(std::wstring& outPath) +{ + HRESULT hr = getAppPath(outPath); + if (FAILED(hr)) return hr; + + hr = PathCchRemoveFileSpec(&outPath[0], static_cast(outPath.size())); + if (isError(hr, "Failed to get executable dir: ")) return hr; + outPath.resize(wcslen(outPath.data())); + return hr; +} \ No newline at end of file diff --git a/AsuroTool/Util.h b/AsuroTool/Util.h index 1ec1d7d..8f055d0 100644 --- a/AsuroTool/Util.h +++ b/AsuroTool/Util.h @@ -6,3 +6,5 @@ bool isError(const HRESULT result, const std::stringstream message); bool isError(const HRESULT result, const char* message); std::string utf8Encode(const std::wstring& wstr); +HRESULT getAppPath(std::wstring& outPath); +HRESULT getAppDir(std::wstring& outPath); diff --git a/ImguiBase/ImguiBase.vcxproj b/ImguiBase/ImguiBase.vcxproj index 0b4b2e7..f792661 100644 --- a/ImguiBase/ImguiBase.vcxproj +++ b/ImguiBase/ImguiBase.vcxproj @@ -107,6 +107,8 @@ E:\Code\glfw-3.3.7.bin.WIN64\lib-vc2022;D:\Applications\VulkanSDK\1.2.182.0\Lib;%(AdditionalLibraryDirectories) vulkan-1.lib;glfw3.lib;%(AdditionalDependencies) + + @@ -130,6 +132,8 @@ E:\Code\glfw-3.3.7.bin.WIN64\lib-vc2022;D:\Applications\VulkanSDK\1.2.182.0\Lib;%(AdditionalLibraryDirectories) vulkan-1.lib;glfw3.lib;%(AdditionalDependencies) + + @@ -149,6 +153,8 @@ E:\Code\glfw-3.3.7.bin.WIN64\lib-vc2022;D:\Applications\VulkanSDK\1.2.182.0\Lib;%(AdditionalLibraryDirectories) vulkan-1.lib;glfw3.lib;%(AdditionalDependencies) + + @@ -172,6 +178,8 @@ E:\Code\glfw-3.3.7.bin.WIN64\lib-vc2022;D:\Applications\VulkanSDK\1.2.182.0\Lib;%(AdditionalLibraryDirectories) vulkan-1.lib;glfw3.lib;%(AdditionalDependencies) + + diff --git a/ImguiNodes/ImguiNodes.vcxproj b/ImguiNodes/ImguiNodes.vcxproj index 7f73fc1..92241d7 100644 --- a/ImguiNodes/ImguiNodes.vcxproj +++ b/ImguiNodes/ImguiNodes.vcxproj @@ -96,6 +96,7 @@ Console true + msvcrt.lib @@ -112,6 +113,7 @@ true true true + msvcrt.lib @@ -124,6 +126,7 @@ Console true + msvcrt.lib @@ -140,6 +143,7 @@ true true true + msvcrt.lib