diff --git a/src/gui/FontManager.cc b/src/gui/FontManager.cc index 6298df4..ac448fd 100644 --- a/src/gui/FontManager.cc +++ b/src/gui/FontManager.cc @@ -1,22 +1,39 @@ #include "FontManager.h" +#include "jes/Runtime.h" #include namespace jes { - FontManager::FontManager() + bool FontManager::load() { if (FT_Init_FreeType(&m_ft) != 0) { std::cerr << "Error initializing FreeType" << std::endl; - return; + return false; } + + /* Load failsafe font */ + m_mono_font = load_font("FreeMono", 20); + + if (m_mono_font.is_null()) + { + std::cerr << "Error loading FreeMono font" << std::endl; + return false; + } + + return true; } FontRef FontManager::load_font(const char * fname, size_t size) { - FontRef font = new Font(); - if (!font->load(m_ft, fname, size)) + PathRef font_path = Runtime::locate(Runtime::FONT, fname); + if (font_path.is_null()) return NULL; + + FontRef font = new Font(); + if (!font->load(m_ft, font_path->to_s().c_str(), size)) + return NULL; + return font; } } diff --git a/src/gui/FontManager.h b/src/gui/FontManager.h index 77f1241..75bf52a 100644 --- a/src/gui/FontManager.h +++ b/src/gui/FontManager.h @@ -11,10 +11,11 @@ namespace jes class FontManager { public: - FontManager(); + bool load(); FontRef load_font(const char * fname, size_t size); protected: FT_Library m_ft; + FontRef m_mono_font; }; typedef Ref FontManagerRef; diff --git a/src/gui/GUI.cc b/src/gui/GUI.cc index 9fbb2cb..12bb964 100644 --- a/src/gui/GUI.cc +++ b/src/gui/GUI.cc @@ -7,12 +7,12 @@ namespace jes { - GUI::GUI() + bool GUI::load() { if (SDL_Init(SDL_INIT_VIDEO)) { std::cerr << "Failed to initialize SDL!" << std::endl; - return; + return false; } atexit(SDL_Quit); @@ -27,7 +27,7 @@ namespace jes { std::cerr << "Failed to create window!" << std::endl; SDL_Quit(); - return; + return false; } (void)SDL_GL_CreateContext(m_window); @@ -36,17 +36,24 @@ namespace jes { std::cerr << "Failed to gl3wInit()" << std::endl; SDL_Quit(); - return; + return false; } if (!gl3wIsSupported(3, 0)) { std::cerr << "OpenGL 3.0 is not supported!" << std::endl; SDL_Quit(); - return; + return false; } glClearColor (0.0, 0.0, 0.0, 0.0); + + m_font_manager = new FontManager(); + + if (!m_font_manager->load()) + return false; + + return true; } int GUI::run() diff --git a/src/gui/GUI.h b/src/gui/GUI.h index bab0e5b..f4fcf42 100644 --- a/src/gui/GUI.h +++ b/src/gui/GUI.h @@ -3,16 +3,18 @@ #include "jes/Ref.h" #include +#include "FontManager.h" namespace jes { class GUI { public: - GUI(); + bool load(); int run(); protected: SDL_Window * m_window; + FontManagerRef m_font_manager; }; typedef Ref GUIRef; diff --git a/src/gui/main.cc b/src/gui/main.cc index d4e99b6..16eadb4 100644 --- a/src/gui/main.cc +++ b/src/gui/main.cc @@ -7,6 +7,9 @@ using namespace std; int main(int argc, char *argv[]) { + jes::Core::instance.init(argv[0]); jes::GUIRef gui = new jes::GUI(); + if (!gui->load()) + return 2; return gui->run(); } diff --git a/src/lib/src/Runtime.cc b/src/lib/src/Runtime.cc index 5e7ac00..c7d7755 100644 --- a/src/lib/src/Runtime.cc +++ b/src/lib/src/Runtime.cc @@ -18,21 +18,29 @@ namespace jes break; case Runtime::FONT: { + static const char * extensions[] = { + "ttf", + }; std::vector dirents = path->dir_entries(); for (auto de : dirents) { PathRef test_path = path->join(de)->join(name); if (test_path->exists()) + return test_path; + for (auto ext : extensions) { - rv = test_path; - break; + test_path = test_path->ext(ext); + if (test_path->exists()) + return test_path; } } } break; } - if (rv->exists()) + + if ((!rv.is_null()) && (rv->exists())) return rv; + return NULL; } }