mirror of
https://github.com/bailwillharr/engine.git
synced 2024-09-21 04:51:18 +00:00
Move glTF file dialog out of application.cpp and clean up win32 ifdefs
This commit is contained in:
parent
5105b6696e
commit
76d957929f
@ -67,7 +67,7 @@ set(SRC_FILES
|
|||||||
"src/vulkan/swapchain.cpp"
|
"src/vulkan/swapchain.cpp"
|
||||||
"src/vulkan/swapchain.h"
|
"src/vulkan/swapchain.h"
|
||||||
"src/window.cpp"
|
"src/window.cpp"
|
||||||
)
|
"src/util/file_dialog.cpp")
|
||||||
|
|
||||||
set(INCLUDE_FILES
|
set(INCLUDE_FILES
|
||||||
"include/application.h"
|
"include/application.h"
|
||||||
@ -106,7 +106,7 @@ set(INCLUDE_FILES
|
|||||||
"include/util/gen_tangents.h"
|
"include/util/gen_tangents.h"
|
||||||
"include/util/gltf_loader.h"
|
"include/util/gltf_loader.h"
|
||||||
"include/window.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/*")
|
file(GLOB_RECURSE RES_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/res/engine/*")
|
||||||
|
|
||||||
|
11
include/util/file_dialog.h
Normal file
11
include/util/file_dialog.h
Normal 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
|
@ -30,12 +30,7 @@
|
|||||||
#include "scene_manager.h"
|
#include "scene_manager.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
#include "util/gltf_loader.h"
|
#include "util/gltf_loader.h"
|
||||||
|
#include "util/file_dialog.h"
|
||||||
#ifdef _MSC_VER
|
|
||||||
#include <windows.h>
|
|
||||||
#include <direct.h>
|
|
||||||
#define WIN_MAX_PATH 260
|
|
||||||
#endif
|
|
||||||
#include <systems/collisions.h>
|
#include <systems/collisions.h>
|
||||||
|
|
||||||
static struct ImGuiThings {
|
static struct ImGuiThings {
|
||||||
@ -46,67 +41,17 @@ namespace engine {
|
|||||||
|
|
||||||
static std::filesystem::path getResourcesPath()
|
static std::filesystem::path getResourcesPath()
|
||||||
{
|
{
|
||||||
std::filesystem::path resourcesPath{};
|
std::filesystem::path resourcesPath(SDL_GetBasePath());
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
resourcesPath /= "res";
|
||||||
// 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";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (std::filesystem::is_directory(resourcesPath) == false) {
|
if (std::filesystem::is_directory(resourcesPath) == false) {
|
||||||
resourcesPath = cwd.root_path() / "usr" / "local" / "share" / "sdltest";
|
throw std::runtime_error("Unable to find game resources directory");
|
||||||
}
|
|
||||||
|
|
||||||
if (std::filesystem::is_directory(resourcesPath) == false) {
|
|
||||||
throw std::runtime_error("Unable to determine resources location. CWD: " + cwd.string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resourcesPath;
|
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); }
|
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)
|
Application::Application(const char* appName, const char* appVersion, gfx::GraphicsSettings graphicsSettings, Configuration configuration)
|
||||||
@ -330,10 +275,10 @@ void Application::GameLoop()
|
|||||||
if (!scene) ImGui::BeginDisabled();
|
if (!scene) ImGui::BeginDisabled();
|
||||||
// load gltf file dialog
|
// load gltf file dialog
|
||||||
if (ImGui::Button("Load glTF")) {
|
if (ImGui::Button("Load glTF")) {
|
||||||
#ifdef _WIN32
|
std::filesystem::path path = util::OpenFileDialog({ "glb" });
|
||||||
std::string path = std::filesystem::path(openGLTFDialog()).string();
|
if (path.empty() == false) {
|
||||||
util::LoadGLTF(*scene, std::filesystem::path(path).string(), false);
|
util::LoadGLTF(*scene, path.string(), false);
|
||||||
#endif
|
}
|
||||||
}
|
}
|
||||||
if (!scene) ImGui::EndDisabled();
|
if (!scene) ImGui::EndDisabled();
|
||||||
}
|
}
|
||||||
|
58
src/util/file_dialog.cpp
Normal file
58
src/util/file_dialog.cpp
Normal 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
|
Loading…
Reference in New Issue
Block a user