From 1d2b1dae192e7e33fd77a7069c4eec11239d9013 Mon Sep 17 00:00:00 2001 From: Marin Nilsson Date: Tue, 4 Aug 2015 19:41:08 +0200 Subject: [PATCH] Out-of-source builds without environment variables. Using CMake's configure_file command to generate a header file defining a C string containing the path to the source root directory; the directory where the resource folder is. Textures and models can now be found directly as long the user doesn't move stuff around. Shaders are still a problem. --- CMakeLists.txt | 2 ++ configuration/root_directory.h.in | 1 + includes/learnopengl/filesystem.h | 4 +++- 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 configuration/root_directory.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 07b08cc..09a89e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,6 +111,8 @@ set(5.advanced_lighting ) +configure_file(configuration/root_directory.h.in configuration/root_directory.h) +include_directories(${CMAKE_BINARY_DIR}/configuration) foreach(CHAPTER ${CHAPTERS}) foreach(DEMO ${${CHAPTER}}) diff --git a/configuration/root_directory.h.in b/configuration/root_directory.h.in new file mode 100644 index 0000000..daefa5f --- /dev/null +++ b/configuration/root_directory.h.in @@ -0,0 +1 @@ +const char * logl_root = "${CMAKE_SOURCE_DIR}"; \ No newline at end of file diff --git a/includes/learnopengl/filesystem.h b/includes/learnopengl/filesystem.h index 7cb469c..80f1e88 100644 --- a/includes/learnopengl/filesystem.h +++ b/includes/learnopengl/filesystem.h @@ -2,6 +2,7 @@ #define FILESYSTEM_H #include +#include "root_directory.h" // This is a configuration file generated by CMake. class FileSystem { @@ -18,7 +19,8 @@ public: private: static std::string const & getRoot() { - static char const * givenRoot {getenv("LOGL_ROOT_PATH")}; + static char const * envRoot = getenv("LOGL_ROOT_PATH"); + static char const * givenRoot {envRoot != nullptr ? envRoot : logl_root}; static std::string root {givenRoot != nullptr ? givenRoot : ""}; return root; }