diff --git a/src/core/Runtime.cc b/src/core/Runtime.cc new file mode 100644 index 0000000..9f1b917 --- /dev/null +++ b/src/core/Runtime.cc @@ -0,0 +1,76 @@ +#include "Runtime.h" +#include "Path.h" + +static const std::initializer_list Font_Extensions = {"", ".ttf", ".otf"}; +static const std::initializer_list Shader_Extensions = {"", ".glsl"}; + +static std::string System_Runtime_Dir; + +void Runtime::init(const char * argv_0, const char * appname) +{ + System_Runtime_Dir = Path::join(Path::dirname(Path::dirname(argv_0)), "share", appname); +} + +std::string Runtime::find(Type t, const std::string & name) +{ + if (Path::is_file(name)) + { + return name; + } + + switch (t) + { + case FONT: + return find_first_under("fonts", name, Font_Extensions, true); + + case SHADER: + return find_first_under("shaders", name, Shader_Extensions); + } + + return ""; +} + +std::string Runtime::find_first_under(const std::string & runtime_subpath, + const std::string & name, + const std::initializer_list & extensions, + bool look_in_subdirs) +{ + std::string system_runtime_path = Path::join(System_Runtime_Dir, runtime_subpath); + std::string first = find_first_in(system_runtime_path, name, extensions); + if (first.empty() && look_in_subdirs) + { + auto dir_ents = Path::listdir(system_runtime_path); + for (auto dir_ent : *dir_ents) + { + if ((*dir_ent)[0] != '.') + { + std::string subpath = Path::join(system_runtime_path, *dir_ent); + if (Path::is_dir(subpath)) + { + std::string p = find_first_in(subpath, name, extensions); + if (!p.empty()) + { + return p; + } + } + } + } + } + return first; +} + +std::string Runtime::find_first_in(const std::string & path, + const std::string & name, + const std::initializer_list & extensions) +{ + for (auto ext : extensions) + { + std::string attempt_path = Path::join(path, name + ext); + if (Path::is_file(attempt_path)) + { + return attempt_path; + } + } + + return ""; +} diff --git a/src/core/Runtime.h b/src/core/Runtime.h new file mode 100644 index 0000000..e37983d --- /dev/null +++ b/src/core/Runtime.h @@ -0,0 +1,29 @@ +#ifndef RUNTIME_H +#define RUNTIME_H + +#include +#include + +class Runtime +{ +public: + enum Type + { + FONT, + SHADER, + }; + + static std::string find(Type t, const std::string & name); + static void init(const char * argv_0, const char * appname); + +protected: + static std::string find_first_under(const std::string & runtime_subpath, + const std::string & name, + const std::initializer_list & extensions, + bool look_in_subdirs = false); + static std::string find_first_in(const std::string & path, + const std::string & name, + const std::initializer_list & extensions); +}; + +#endif diff --git a/src/jes.cc b/src/jes.cc index b5ebb10..2604865 100644 --- a/src/jes.cc +++ b/src/jes.cc @@ -1,8 +1,11 @@ #include #include "Window.h" +#include "Runtime.h" int main(int argc, char * argv[]) { + Runtime::init(argv[0], "jes"); + Window w; if (!w.create()) {