
#pragma once
#include <raylib.h>
extern Font noto_regular;
extern Font noto_italic;
extern Font pong_font;
/**
* @brief use this after raylib init window function
*/
void LoadGlobalFonts();
#include "global-fonts.h"
#include <raylib.h>
#include <cstdlib>
#include <cstring>
#include <vector>
Font noto_regular;
Font noto_italic;
Font pong_font;
void LoadGlobalFonts() {
noto_regular = LoadFont("assets/fonts/noto-regular.ttf");
noto_italic = LoadFont("assets/fonts/noto-regular.ttf");
// load custom bitmap font
Image font_image = LoadImage("assets/fonts/pong-font.png");
const char *pong_charset =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890':\\"()[] "
"!,-|_+/*\\\\?.%@";
int char_count = strlen(pong_charset);
int char_height = 7;
GlyphInfo *glyphs = (GlyphInfo *)calloc(char_count, sizeof(GlyphInfo));
Rectangle *recs = (Rectangle *)calloc(char_count, sizeof(Rectangle));
// pre process (find split column)
Color *pixel_colors = LoadImageColors(font_image); // colors
Color split_color = pixel_colors[0]; // split
std::vector<int> index;
for (int i = 0; i < font_image.width; i++) {
if (ColorIsEqual(pixel_colors[i], split_color)) {
index.emplace_back(i);
}
}
for (int i = 0; i < char_count; i++) {
Rectangle rec = {.x = (float)index[i] + 1,
.y = 0,
.width = (float)(index[i + 1] - index[i] - 2),
.height = (float)char_height};
// Create GlyphInfo
glyphs[i].value = pong_charset[i];
glyphs[i].offsetX = 0;
glyphs[i].offsetY = 0;
glyphs[i].advanceX = index[i + 1] - index[i] - 2;
recs[i] = rec;
}
// Generate Font
Texture2D tex = LoadTextureFromImage(font_image);
SetTextureFilter(tex, TEXTURE_FILTER_POINT);
pong_font = {.baseSize = char_height,
.glyphCount = char_count,
.glyphPadding = 0,
.texture = tex,
.recs = recs,
.glyphs = glyphs};
}
Font manager
Singleton Utility
#include <raylib.h>
#include <cstddef>
#include "utility/singleton.h"
class FontManager {
MAKE_SINGLETON(FontManager)
public:
const Font& Regular() const { return normal_font_; }
const Font& Italic() const { return itlaic_font_; }
private:
FontManager()
: normal_font_(LoadFontEx("assets/fonts/noto-regular.ttf", 64, NULL, 0)),
itlaic_font_(LoadFontEx("assets/fonts/noto-italic.ttf", 64, NULL, 0)) {}
~FontManager() {
UnloadFont(normal_font_);
UnloadFont(itlaic_font_);
}
Font normal_font_;
Font itlaic_font_;
};