diff --git a/system/ui/raylib/spinner.cc b/system/ui/raylib/spinner.cc index 891d34b2d5..7190953631 100644 --- a/system/ui/raylib/spinner.cc +++ b/system/ui/raylib/spinner.cc @@ -12,7 +12,7 @@ constexpr int kTextureSize = 360; constexpr int kFontSize = 80; int main(int argc, char *argv[]) { - initApp("spinner", 30); + App app("spinner", 30); // Turn off input buffering for std::cin std::cin.sync_with_stdio(false); @@ -55,14 +55,12 @@ int main(int argc, char *argv[]) { bar.width *= progress / 100.0f; DrawRectangleRounded(bar, 0.5f, 10, RAYLIB_RAYWHITE); } else { - Vector2 textSize = MeasureTextEx(getFont(), userInput.c_str(), kFontSize, 1.0); - DrawTextEx(getFont(), userInput.c_str(), {center.x - textSize.x / 2, yPos}, kFontSize, 1.0, RAYLIB_WHITE); + Vector2 textSize = MeasureTextEx(app.getFont(), userInput.c_str(), kFontSize, 1.0); + DrawTextEx(app.getFont(), userInput.c_str(), {center.x - textSize.x / 2, yPos}, kFontSize, 1.0, RAYLIB_WHITE); } } EndDrawing(); } - - CloseWindow(); return 0; } diff --git a/system/ui/raylib/util.cc b/system/ui/raylib/util.cc index 46f8a4fd3f..be7137220c 100644 --- a/system/ui/raylib/util.cc +++ b/system/ui/raylib/util.cc @@ -1,6 +1,7 @@ #include "system/ui/raylib/util.h" #include +#include #undef GREEN #undef RED @@ -9,35 +10,16 @@ #include "system/hardware/hw.h" constexpr std::array(FontWeight::Count)> FONT_FILE_PATHS = { - "../../assets/fonts/Inter-Black.ttf", - "../../assets/fonts/Inter-Bold.ttf", - "../../assets/fonts/Inter-ExtraBold.ttf", - "../../assets/fonts/Inter-ExtraLight.ttf", - "../../assets/fonts/Inter-Medium.ttf", - "../../assets/fonts/Inter-Regular.ttf", - "../../assets/fonts/Inter-SemiBold.ttf", - "../../assets/fonts/Inter-Thin.ttf", + "../../selfdrive/assets/fonts/Inter-Black.ttf", + "../../selfdrive/assets/fonts/Inter-Bold.ttf", + "../../selfdrive/assets/fonts/Inter-ExtraBold.ttf", + "../../selfdrive/assets/fonts/Inter-ExtraLight.ttf", + "../../selfdrive/assets/fonts/Inter-Medium.ttf", + "../../selfdrive/assets/fonts/Inter-Regular.ttf", + "../../selfdrive/assets/fonts/Inter-SemiBold.ttf", + "../../selfdrive/assets/fonts/Inter-Thin.ttf", }; -struct FontManager { - FontManager() { - for (int i = 0; i < fonts.size(); ++i) { - fonts[i] = LoadFontEx(FONT_FILE_PATHS[i], 120, nullptr, 250); - } - } - - ~FontManager() { - for (auto &f : fonts) UnloadFont(f); - } - - std::array(FontWeight::Count)> fonts; -}; - -const Font& getFont(FontWeight weight) { - static FontManager font_manager; - return font_manager.fonts[(int)weight]; -} - Texture2D LoadTextureResized(const char *fileName, int size) { Image img = LoadImage(fileName); ImageResize(&img, size, size); @@ -45,10 +27,39 @@ Texture2D LoadTextureResized(const char *fileName, int size) { return texture; } -void initApp(const char *title, int fps) { +App *pApp = nullptr; + +App::App(const char *title, int fps) { + // Ensure the current dir matches the exectuable's directory + auto self_path = util::readlink("/proc/self/exe"); + auto exe_dir = std::filesystem::path(self_path).parent_path(); + chdir(exe_dir.c_str()); + Hardware::set_display_power(true); Hardware::set_brightness(65); + // SetTraceLogLevel(LOG_NONE); - InitWindow(2160, 1080, title); + InitWindow(0, 0, title); SetTargetFPS(fps); + + // Load fonts + fonts_.reserve(FONT_FILE_PATHS.size()); + for (int i = 0; i < FONT_FILE_PATHS.size(); ++i) { + fonts_.push_back(LoadFontEx(FONT_FILE_PATHS[i], 120, nullptr, 250)); + } + + pApp = this; +} + +App::~App() { + for (auto &font : fonts_) { + UnloadFont(font); + } + + CloseWindow(); + pApp = nullptr; +} + +const Font &App::getFont(FontWeight weight) const { + return fonts_[static_cast(weight)]; } diff --git a/system/ui/raylib/util.h b/system/ui/raylib/util.h index 1e2662fb90..5bc05ddc25 100644 --- a/system/ui/raylib/util.h +++ b/system/ui/raylib/util.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "system/ui/raylib/raylib.h" @@ -16,6 +17,17 @@ enum class FontWeight { Count // To represent the total number of fonts }; -void initApp(const char *title, int fps); -const Font& getFont(FontWeight weight = FontWeight::Normal); Texture2D LoadTextureResized(const char *fileName, int size); + +class App { +public: + App(const char *title, int fps); + ~App(); + const Font &getFont(FontWeight weight = FontWeight::Normal) const; + +protected: + std::vector fonts_; +}; + +// Global pointer to the App instance +extern App *pApp;