From 7f3845152d6b04ae72de70d5131d942fdedceb6b Mon Sep 17 00:00:00 2001 From: gradient Date: Tue, 12 Sep 2017 21:07:13 -0500 Subject: [PATCH] bump to c++17, use std::experimental::filesystem --- .gitmodules | 4 +--- CMakeLists.txt | 10 ++++++---- deps/filesystem | 1 - src/main.cpp | 4 ++-- src/project.cpp | 20 ++++++++++++-------- src/project.h | 11 ++++++++--- src/shaders.cpp | 20 ++++++++++---------- src/shaders.h | 16 +++++++++------- 8 files changed, 48 insertions(+), 38 deletions(-) delete mode 160000 deps/filesystem diff --git a/.gitmodules b/.gitmodules index 5c36d43..bfb24c5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,6 +7,4 @@ [submodule "deps/cpptoml"] path = deps/cpptoml url = https://github.com/skystrife/cpptoml -[submodule "deps/filesystem"] - path = deps/filesystem - url = https://github.com/wjakob/filesystem + diff --git a/CMakeLists.txt b/CMakeLists.txt index dbd8a9e..4ec55c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.1) project(zinnia VERSION 0.1 LANGUAGES C CXX) -set (CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 17) find_package(PkgConfig REQUIRED) find_package(OpenGL REQUIRED) @@ -20,9 +20,8 @@ include_directories(deps/gl3w/include) add_library(gl3w deps/gl3w/src/gl3w.c) target_link_libraries(gl3w dl) -#include_directories(deps/nfd/src/include) include_directories(deps/cpptoml/include) -include_directories(deps/filesystem) +include_directories(deps/inotify) include_directories(${PROJECT_SOURCE_DIR}) add_executable(zinnia src/main.cpp src/gui.cpp src/project.cpp @@ -34,4 +33,7 @@ target_link_libraries(zinnia ${GLFW_LIBRARIES}) target_link_libraries(zinnia gl3w) target_link_libraries(zinnia ${GTK3_LIBRARIES}) target_link_libraries(zinnia inotifytools) -#target_link_libraries(zinnia ${CMAKE_SOURCE_DIR}/deps/nfd/build/lib/Release/x64/libnfd.a) + +if(CMAKE_COMPILER_IS_GNUCXX) + target_link_libraries(zinnia stdc++fs) +endif() diff --git a/deps/filesystem b/deps/filesystem deleted file mode 160000 index 0a539a6..0000000 --- a/deps/filesystem +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 0a539a6c988dc8691af317e077893e831dee2908 diff --git a/src/main.cpp b/src/main.cpp index 6dbfb94..c7e8dbc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,8 +68,8 @@ int main(int argc, char* argv[]) { auto gui = Gui(&state); if (argc == 2) { - auto proj = Project::fromFile(std::string(argv[1])); - inotify.watch(*proj); + auto proj = std::make_shared(Project::fromFile(std::string(argv[1]))); + inotify.watch(proj); state.current_project = std::move(proj); } diff --git a/src/project.cpp b/src/project.cpp index 81bb3bf..2cbe430 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -1,16 +1,20 @@ #include "project.h" #include -std::unique_ptr Project::fromFile(std::string path) { +Project Project::fromFile(std::string path) { + return Project::fromFile(fs::path(path)); +} + +Project Project::fromFile(fs::path path) { auto manifest = cpptoml::parse_file(path); - auto basepath = filesystem::path(path).parent_path().make_absolute(); + auto basepath = fs::absolute(path).parent_path(); auto project = Project::fromToml(manifest, basepath); return project; } -std::unique_ptr Project::fromToml(std::shared_ptr manifest, - filesystem::path basepath = filesystem::path(".")) { +Project Project::fromToml(std::shared_ptr manifest, + fs::path basepath = fs::path(".")) { auto project = Project(); project.basepath = basepath; project.name = manifest->get_qualified_as("project.name").value_or(""); @@ -25,9 +29,9 @@ std::unique_ptr Project::fromToml(std::shared_ptr manif auto p_vert = conf_scene->get_as("vert"); if (p_frag && p_vert) { - std::vector> shaders; - shaders.push_back(std::make_pair((basepath / *p_vert).str(), GL_VERTEX_SHADER)); - shaders.push_back(std::make_pair((basepath / *p_frag).str(), GL_FRAGMENT_SHADER)); + std::vector> shaders; + shaders.push_back(std::make_pair(basepath / *p_vert, GL_VERTEX_SHADER)); + shaders.push_back(std::make_pair(basepath / *p_frag, GL_FRAGMENT_SHADER)); scene->shader_p = project.shader_bundle->add_program(shaders); } @@ -43,7 +47,7 @@ std::unique_ptr Project::fromToml(std::shared_ptr manif project.shader_bundle->recompile().link(); } - return std::make_unique(project); + return project; } void Project::load_scene(std::shared_ptr scene) { diff --git a/src/project.h b/src/project.h index 1f577aa..ba2763c 100644 --- a/src/project.h +++ b/src/project.h @@ -3,11 +3,14 @@ #include #include #include +#include #include #include "shaders.h" #include "pipeline/post.h" +namespace fs = std::experimental::filesystem; + class Scene { public: std::string name; @@ -24,18 +27,20 @@ class Project { public: Project() = default; - static std::unique_ptr fromFile(std::string path); - static std::unique_ptr fromToml(std::shared_ptr manifest, filesystem::path basepath); + static Project fromFile(std::string path); + static Project fromFile(fs::path path); + static Project fromToml(std::shared_ptr manifest, fs::path basepath); void load_scene(std::shared_ptr scene); void reload_shaders(); + void unload_project(); std::vector> scenes; std::shared_ptr current_scene; bool scene_loaded = false; std::string name; - filesystem::path basepath; + fs::path basepath; std::shared_ptr shader_bundle; }; diff --git a/src/shaders.cpp b/src/shaders.cpp index e3e1b3f..047b115 100644 --- a/src/shaders.cpp +++ b/src/shaders.cpp @@ -10,7 +10,7 @@ #include "shaders.h" -static std::string string_from_file(std::string path) { +static std::string string_from_file(fs::path path) { std::ifstream in(path, std::ios::in); if (!in) { std::cerr << "Opening file " << path << " failed.\n"; @@ -33,11 +33,11 @@ ShaderBundle::~ShaderBundle() { } ShaderBundle::ProgramHandle* ShaderBundle::add_program( - const std::vector>& shaderPairs) { + const std::vector>& shaderPairs) { std::vector shader_ids; for (auto shaderPair : shaderPairs) { ShaderId shader_id; - std::tie(shader_id.name, shader_id.type) = shaderPair; + std::tie(shader_id.path, shader_id.type) = shaderPair; auto upserted = this->shader_pool.emplace(std::move(shader_id), Shader()).first; // if the shader doesn't have a handle from OpenGL, get one @@ -66,8 +66,8 @@ ShaderBundle::ProgramHandle* ShaderBundle::add_program( ShaderBundle& ShaderBundle::recompile() { for (std::pair shader : this->shader_pool) { - auto source_s = string_from_file(shader.first.name); - source_s = ShaderBundle::preprocess(filesystem::path(shader.first.name), source_s); + auto source_s = string_from_file(shader.first.path); + source_s = ShaderBundle::preprocess(shader.first.path, source_s); const char *source_buf = source_s.c_str(); glShaderSource(shader.second.handle, 1, &source_buf, NULL); glCompileShader(shader.second.handle); @@ -85,7 +85,7 @@ ShaderBundle& ShaderBundle::recompile() { std::cerr << "Error compiling: " << log_s << '\n'; } - std::cout << "Successfully compiled " << shader.first.name << '\n'; + std::cout << "Successfully compiled " << shader.first.path << '\n'; } return *this; @@ -137,7 +137,7 @@ ShaderBundle& ShaderBundle::link() { return *this; } -std::string ShaderBundle::preprocess(filesystem::path path, std::string shader) { +std::string ShaderBundle::preprocess(fs::path path, std::string shader) { // TODO: this is probably not idiomatic C++; perhaps use a stringstream? // TODO: proper erroring out. @@ -153,10 +153,10 @@ std::string ShaderBundle::preprocess(filesystem::path path, std::string shader) auto idx_nl = shader.find('\n', idx_end); auto file_str = shader.substr(idx_end + 1, idx_nl - idx_end - 2); - auto file_path = path.parent_path().make_absolute() / file_str; + auto file_path = fs::absolute(path.parent_path()) / file_str; - auto included_glsl = preprocess(file_path, string_from_file(file_path.str())); + auto included_glsl = preprocess(file_path, string_from_file(file_path)); shader.replace(directive_idx+1, idx_nl - directive_idx - 1, included_glsl); return preprocess(path, shader); -} \ No newline at end of file +} diff --git a/src/shaders.h b/src/shaders.h index 972ecd0..c24b6c7 100644 --- a/src/shaders.h +++ b/src/shaders.h @@ -5,7 +5,9 @@ #include #include #include -#include "filesystem/path.h" +#include + +namespace fs = std::experimental::filesystem; class ShaderBundle { public: @@ -14,13 +16,13 @@ public: using ShaderType = GLenum; struct ShaderId { - std::string name; + fs::path path; ShaderType type; bool operator<(const ShaderId& rhs) const { - return std::tie(name, type) < std::tie(rhs.name, rhs.type); + return std::tie(path, type) < std::tie(rhs.path, rhs.type); } bool operator==(const ShaderId& rhs) const { - return std::tie(name, type) == std::tie(rhs.name, rhs.type); + return std::tie(path, type) == std::tie(rhs.path, rhs.type); } }; @@ -43,10 +45,10 @@ public: std::map shader_pool; std::map, Program> programs; - ProgramHandle* add_program(const std::vector>& shaders); + ProgramHandle* add_program(const std::vector>& shaders); ShaderBundle& recompile(); ShaderBundle& link(); - static std::string preprocess(filesystem::path path, std::string shader); -}; \ No newline at end of file + static std::string preprocess(fs::path path, std::string shader); +};