Browse Source

bump to c++17, use std::experimental::filesystem

master
gradient 8 years ago
parent
commit
7f3845152d
8 changed files with 48 additions and 38 deletions
  1. +1
    -3
      .gitmodules
  2. +6
    -4
      CMakeLists.txt
  3. +0
    -1
      deps/filesystem
  4. +2
    -2
      src/main.cpp
  5. +12
    -8
      src/project.cpp
  6. +8
    -3
      src/project.h
  7. +10
    -10
      src/shaders.cpp
  8. +9
    -7
      src/shaders.h

+ 1
- 3
.gitmodules View File

@ -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

+ 6
- 4
CMakeLists.txt View File

@ -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()

+ 0
- 1
deps/filesystem

@ -1 +0,0 @@
Subproject commit 0a539a6c988dc8691af317e077893e831dee2908

+ 2
- 2
src/main.cpp View File

@ -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>(Project::fromFile(std::string(argv[1])));
inotify.watch(proj);
state.current_project = std::move(proj);
}


+ 12
- 8
src/project.cpp View File

@ -1,16 +1,20 @@
#include "project.h"
#include <iostream>
std::unique_ptr<Project> 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> Project::fromToml(std::shared_ptr<cpptoml::table> manifest,
filesystem::path basepath = filesystem::path(".")) {
Project Project::fromToml(std::shared_ptr<cpptoml::table> manifest,
fs::path basepath = fs::path(".")) {
auto project = Project();
project.basepath = basepath;
project.name = manifest->get_qualified_as<std::string>("project.name").value_or("");
@ -25,9 +29,9 @@ std::unique_ptr<Project> Project::fromToml(std::shared_ptr<cpptoml::table> manif
auto p_vert = conf_scene->get_as<std::string>("vert");
if (p_frag && p_vert) {
std::vector<std::pair<std::string, ShaderBundle::ShaderType>> 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<std::pair<fs::path, ShaderBundle::ShaderType>> 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> Project::fromToml(std::shared_ptr<cpptoml::table> manif
project.shader_bundle->recompile().link();
}
return std::make_unique<Project>(project);
return project;
}
void Project::load_scene(std::shared_ptr<Scene> scene) {


+ 8
- 3
src/project.h View File

@ -3,11 +3,14 @@
#include <string>
#include <vector>
#include <memory>
#include <experimental/filesystem>
#include <cpptoml.h>
#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<Project> fromFile(std::string path);
static std::unique_ptr<Project> fromToml(std::shared_ptr<cpptoml::table> manifest, filesystem::path basepath);
static Project fromFile(std::string path);
static Project fromFile(fs::path path);
static Project fromToml(std::shared_ptr<cpptoml::table> manifest, fs::path basepath);
void load_scene(std::shared_ptr<Scene> scene);
void reload_shaders();
void unload_project();
std::vector<std::shared_ptr<Scene>> scenes;
std::shared_ptr<Scene> current_scene;
bool scene_loaded = false;
std::string name;
filesystem::path basepath;
fs::path basepath;
std::shared_ptr<ShaderBundle> shader_bundle;
};

+ 10
- 10
src/shaders.cpp View File

@ -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<std::pair<std::string, ShaderType>>& shaderPairs) {
const std::vector<std::pair<fs::path, ShaderType>>& shaderPairs) {
std::vector<ShaderId> 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<const ShaderId, Shader> 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);
}
}

+ 9
- 7
src/shaders.h View File

@ -5,7 +5,9 @@
#include <string>
#include <map>
#include <set>
#include "filesystem/path.h"
#include <experimental/filesystem>
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<ShaderId, Shader> shader_pool;
std::map<std::vector<ShaderId>, Program> programs;
ProgramHandle* add_program(const std::vector<std::pair<std::string, ShaderType>>& shaders);
ProgramHandle* add_program(const std::vector<std::pair<fs::path, ShaderType>>& shaders);
ShaderBundle& recompile();
ShaderBundle& link();
static std::string preprocess(filesystem::path path, std::string shader);
};
static std::string preprocess(fs::path path, std::string shader);
};

Loading…
Cancel
Save