mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-07-08 15:12:10 +08:00
raylib: refactor to implement new App class (#34375)
refactor to implement new App class
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
+40
-29
@@ -1,6 +1,7 @@
|
||||
#include "system/ui/raylib/util.h"
|
||||
|
||||
#include <array>
|
||||
#include <filesystem>
|
||||
|
||||
#undef GREEN
|
||||
#undef RED
|
||||
@@ -9,35 +10,16 @@
|
||||
#include "system/hardware/hw.h"
|
||||
|
||||
constexpr std::array<const char *, static_cast<int>(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<Font, static_cast<int>(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<int>(weight)];
|
||||
}
|
||||
|
||||
+14
-2
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Font> fonts_;
|
||||
};
|
||||
|
||||
// Global pointer to the App instance
|
||||
extern App *pApp;
|
||||
|
||||
Reference in New Issue
Block a user