Skip to content

Commit

Permalink
initial body implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
BBBmau committed Oct 11, 2024
1 parent 21a8f9e commit d27d52a
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
45 changes: 45 additions & 0 deletions selfdrive/ui/raylib/body.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <algorithm>
#include <cmath>
#include <iostream>

#include "selfdrive/ui/raylib/util.h"
#include "selfdrive/ui/raylib/body.h"
#include "third_party/raylib/include/raylib.h"

constexpr int kProgressBarWidth = 1000;
constexpr int kProgressBarHeight = 20;
constexpr float kRotationRate = 12.0f;
constexpr int kMargin = 200;
constexpr int kTextureSize = 360;
constexpr int kFontSize = 80;

int main(int argc, char *argv[]) {
initApp("body", 30);

Texture2D awakeTexture = LoadTextureResized("../assets/body/awake.gif", kTextureSize);
Texture2D sleepTexture = LoadTextureResized("../assets/body/sleep.gif", kTextureSize);

float rotation = 0.0f;

while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(BLACK);

rotation = fmod(rotation + kRotationRate, 360.0f);
Vector2 center = {GetScreenWidth() / 2.0f, GetScreenHeight() / 2.0f};
const Vector2 spinnerOrigin{kTextureSize / 2.0f, kTextureSize / 2.0f};
const Vector2 commaPosition{center.x - kTextureSize / 2.0f, center.y - kTextureSize / 2.0f};

// Draw rotating spinner and static comma logo
DrawTexturePro(awakeTexture, {0, 0, (float)kTextureSize, (float)kTextureSize},
{center.x, center.y, (float)kTextureSize, (float)kTextureSize},
spinnerOrigin, rotation, WHITE);
DrawTextureV(sleepTexture, commaPosition, WHITE);


EndDrawing();
}

CloseWindow();
return 0;
}
Empty file added selfdrive/ui/raylib/body.h
Empty file.
56 changes: 56 additions & 0 deletions selfdrive/ui/raylib/util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "selfdrive/ui/raylib/util.h"

#include <array>

#undef GREEN
#undef RED
#undef YELLOW
#include "common/swaglog.h"
#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",
};

struct FontManager {
FontManager() {
for (int i = 0; i < fonts.size(); ++i) {
fonts[i] = LoadFontEx(FONT_FILE_PATHS[i], 120, nullptr, 250);
SetTextureFilter(fonts[i].texture, TEXTURE_FILTER_TRILINEAR);
}
}

~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);
Texture2D texture = LoadTextureFromImage(img);
SetTextureFilter(texture, TEXTURE_FILTER_TRILINEAR);
return texture;
}

void initApp(const char *title, int fps) {
Hardware::set_display_power(true);
Hardware::set_brightness(65);
SetTraceLogLevel(LOG_NONE);
InitWindow(0, 0, title);
SetTargetFPS(fps);
}
21 changes: 21 additions & 0 deletions selfdrive/ui/raylib/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <string>

#include "third_party/raylib/include/raylib.h"

enum class FontWeight {
Normal,
Bold,
ExtraBold,
ExtraLight,
Medium,
Regular,
SemiBold,
Thin,
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);

0 comments on commit d27d52a

Please sign in to comment.