Move glTF file dialog out of application.cpp and clean up win32 ifdefs

This commit is contained in:
bailehuni 2024-05-31 23:50:59 +01:00
parent 5105b6696e
commit 76d957929f
4 changed files with 79 additions and 65 deletions

View File

@ -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/*")

View File

@ -0,0 +1,11 @@
#pragma once
#include <filesystem>
#include <string>
#include <vector>
namespace engine::util {
std::filesystem::path OpenFileDialog(const std::vector<std::string>& extensions);
} // namespace engine::util

View File

@ -30,12 +30,7 @@
#include "scene_manager.h"
#include "window.h"
#include "util/gltf_loader.h"
#ifdef _MSC_VER
#include <windows.h>
#include <direct.h>
#define WIN_MAX_PATH 260
#endif
#include "util/file_dialog.h"
#include <systems/collisions.h>
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();
}

58
src/util/file_dialog.cpp Normal file
View File

@ -0,0 +1,58 @@
#include "util/file_dialog.h"
#ifdef _WIN32
#include <windows.h>
#define WIN_MAX_PATH 260
#endif
#include <filesystem>
#include <string>
#include <vector>
#include "log.h"
namespace engine::util {
std::filesystem::path OpenFileDialog(const std::vector<std::string>& 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