From 76d957929f8519b1600208dedd49473bd036f7d3 Mon Sep 17 00:00:00 2001 From: bailehuni Date: Fri, 31 May 2024 23:50:59 +0100 Subject: [PATCH] Move glTF file dialog out of application.cpp and clean up win32 ifdefs --- CMakeLists.txt | 4 +-- include/util/file_dialog.h | 11 ++++++ src/application.cpp | 71 +++++--------------------------------- src/util/file_dialog.cpp | 58 +++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 65 deletions(-) create mode 100644 include/util/file_dialog.h create mode 100644 src/util/file_dialog.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cd5e6d5..67a87bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,7 +67,7 @@ set(SRC_FILES "src/vulkan/swapchain.cpp" "src/vulkan/swapchain.h" "src/window.cpp" -) + "src/util/file_dialog.cpp") set(INCLUDE_FILES "include/application.h" @@ -106,7 +106,7 @@ set(INCLUDE_FILES "include/util/gen_tangents.h" "include/util/gltf_loader.h" "include/window.h" -) + "include/util/file_dialog.h") file(GLOB_RECURSE RES_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/res/engine/*") diff --git a/include/util/file_dialog.h b/include/util/file_dialog.h new file mode 100644 index 0000000..0650066 --- /dev/null +++ b/include/util/file_dialog.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include +#include + +namespace engine::util { + +std::filesystem::path OpenFileDialog(const std::vector& extensions); + +} // namespace engine::util \ No newline at end of file diff --git a/src/application.cpp b/src/application.cpp index 1dc3d73..019360d 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -30,12 +30,7 @@ #include "scene_manager.h" #include "window.h" #include "util/gltf_loader.h" - -#ifdef _MSC_VER -#include -#include -#define WIN_MAX_PATH 260 -#endif +#include "util/file_dialog.h" #include static struct ImGuiThings { @@ -46,67 +41,17 @@ namespace engine { static std::filesystem::path getResourcesPath() { - std::filesystem::path resourcesPath{}; + std::filesystem::path resourcesPath(SDL_GetBasePath()); -#ifdef _MSC_VER - // get the path of the currently running process - CHAR exeDirBuf[MAX_PATH + 1]; - GetModuleFileNameA(NULL, exeDirBuf, WIN_MAX_PATH + 1); - std::filesystem::path cwd = std::filesystem::path(exeDirBuf).parent_path(); - (void)_chdir((const char*)std::filesystem::absolute(cwd).c_str()); -#else - std::filesystem::path cwd = std::filesystem::current_path(); -#endif - - if (std::filesystem::is_directory(cwd / "res")) { - resourcesPath = cwd / "res"; - } - else { - resourcesPath = cwd.parent_path() / "share" / "sdltest"; - } + resourcesPath /= "res"; if (std::filesystem::is_directory(resourcesPath) == false) { - resourcesPath = cwd.root_path() / "usr" / "local" / "share" / "sdltest"; - } - - if (std::filesystem::is_directory(resourcesPath) == false) { - throw std::runtime_error("Unable to determine resources location. CWD: " + cwd.string()); + throw std::runtime_error("Unable to find game resources directory"); } return resourcesPath; } -#ifdef _WIN32 -static std::string openGLTFDialog() { - OPENFILENAMEA ofn; // common dialog box structure - CHAR szFile[260] = { 0 }; // if using TCHAR macros, use TCHAR array - - // Initialize OPENFILENAME - ZeroMemory(&ofn, sizeof(ofn)); - ofn.lStructSize = sizeof(ofn); - ofn.hwndOwner = NULL; - ofn.lpstrFile = szFile; - ofn.lpstrFile[0] = '\0'; - ofn.nMaxFile = sizeof(szFile); - ofn.lpstrFilter = "GLTF Files (*.gltf;*.glb)\0*.gltf;*.glb\0All Files (*.*)\0*.*\0"; - ofn.nFilterIndex = 1; - ofn.lpstrFileTitle = NULL; - ofn.nMaxFileTitle = 0; - ofn.lpstrInitialDir = NULL; - ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR; - - // Display the Open dialog box - if (GetOpenFileNameA(&ofn) == TRUE) - { - return ofn.lpstrFile; - } - else - { - return ""; // User cancelled the dialog - } -} -#endif - static auto frametimeFromFPS(int fps) { return std::chrono::nanoseconds(1'000'000'000 / fps); } Application::Application(const char* appName, const char* appVersion, gfx::GraphicsSettings graphicsSettings, Configuration configuration) @@ -330,10 +275,10 @@ void Application::GameLoop() if (!scene) ImGui::BeginDisabled(); // load gltf file dialog if (ImGui::Button("Load glTF")) { -#ifdef _WIN32 - std::string path = std::filesystem::path(openGLTFDialog()).string(); - util::LoadGLTF(*scene, std::filesystem::path(path).string(), false); -#endif + std::filesystem::path path = util::OpenFileDialog({ "glb" }); + if (path.empty() == false) { + util::LoadGLTF(*scene, path.string(), false); + } } if (!scene) ImGui::EndDisabled(); } diff --git a/src/util/file_dialog.cpp b/src/util/file_dialog.cpp new file mode 100644 index 0000000..670e51b --- /dev/null +++ b/src/util/file_dialog.cpp @@ -0,0 +1,58 @@ +#include "util/file_dialog.h" + +#ifdef _WIN32 +#include +#define WIN_MAX_PATH 260 +#endif + +#include +#include +#include + +#include "log.h" + +namespace engine::util { + +std::filesystem::path OpenFileDialog(const std::vector& extensions) +{ +#ifdef _WIN32 + + // build the filter string + std::string wildcards{}; + for (const std::string& ext : extensions) { + wildcards += "*." + ext + ";"; + } + wildcards.pop_back(); // remove the last semicolon + const std::string filter = "(" + wildcards + ")\0" + wildcards + "\0All Files (*.*)\0*.*\0"; + + OPENFILENAMEA ofn{}; // common dialog box structure + CHAR szFile[WIN_MAX_PATH] = {0}; // if using TCHAR macros, use TCHAR array + + // Initialize OPENFILENAME + ofn.lStructSize = sizeof(ofn); + ofn.hwndOwner = NULL; + ofn.lpstrFile = szFile; + ofn.lpstrFile[0] = '\0'; + ofn.nMaxFile = sizeof(szFile); + ofn.lpstrFilter = filter.c_str(); + ofn.nFilterIndex = 1; + ofn.lpstrFileTitle = NULL; + ofn.nMaxFileTitle = 0; + ofn.lpstrInitialDir = NULL; + ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR; + + // Display the Open dialog box + if (GetOpenFileNameA(&ofn) == TRUE) { + return std::filesystem::path(std::string(ofn.lpstrFile)); + } + else { + return std::filesystem::path{}; // User cancelled the dialog + } +#else + // only Windows dialogs supported at the moment + LOG_ERROR("Open file dialog not supported on this platform"); + return ""; +#endif +} + +} // namespace engine::util \ No newline at end of file