NouVeL/ADVect/Graphics.cpp

83 lines
2.2 KiB
C++
Raw Normal View History

2022-08-18 12:17:43 -04:00
#include <ft2build.h>
#include FT_FREETYPE_H
2022-05-09 01:50:09 -04:00
#include "Graphics.h"
#include <iostream>
namespace {
FT_Library library;
FT_Error error;
FT_Face face;
}
namespace ADVect::Graphics {
void Init() {
error = FT_Init_FreeType(&library);
if (error) {
std::cerr << "FreeType init error: " << error << std::endl;
}
2022-05-09 03:46:44 -04:00
error = FT_New_Face(library, "SourceHanSans-Regular.ttc", 0, &face);
2022-05-09 01:50:09 -04:00
if (error == FT_Err_Unknown_File_Format) {
std::cerr << "FreeType Unknown_File_Format: " << error << std::endl;
}
2022-08-18 12:17:43 -04:00
else if (error) {
2022-05-09 01:50:09 -04:00
std::cerr << "FreeType font loading error: " << error << std::endl;
}
2022-05-09 03:46:44 -04:00
error = FT_Set_Char_Size(face, 0, 20 * 64, 72, 72);
2022-05-09 01:50:09 -04:00
if (error == FT_Err_Unknown_File_Format) {
std::cerr << "FreeType Set_Char_Size error: " << error << std::endl;
}
}
void Destroy() {
FT_Done_Face(face);
FT_Done_FreeType(library);
}
2022-08-18 12:17:43 -04:00
void RenderString(const NVL::String& s, u32 pos_x, u32 pos_y, const SDL_Color& attr) {
2022-05-09 03:46:44 -04:00
FT_GlyphSlot slot = face->glyph;
2022-08-18 12:17:43 -04:00
SDL_Rect pos = { pos_x, pos_y, 0, 0 };
SDL_Color colors[256];
for (u16 i = 0; i < 256; i++) {
colors[i].r = f32(i) / 255.f * (f32)attr.r;
colors[i].g = f32(i) / 255.f * (f32)attr.g;
colors[i].b = f32(i) / 255.f * (f32)attr.b;
}
2022-05-09 01:50:09 -04:00
2022-08-18 12:17:43 -04:00
for (const auto& c : s) {
2022-05-09 01:50:09 -04:00
error = FT_Load_Char(face, c, FT_LOAD_RENDER);
2022-08-18 12:17:43 -04:00
if (error) continue;
//SDL_Surface* surface = SDL_CreateRGBSurfaceFrom(
// slot->bitmap.buffer,
// slot->bitmap.width,
// slot->bitmap.rows,
// 8,
// slot->bitmap.pitch,
// 0, 0, 0, 255);
//SDL_SetPaletteColors(surface->format->palette, colors, 0, 256);
2022-05-09 01:50:09 -04:00
2022-08-18 12:17:43 -04:00
//SDL_Texture* glyph = SDL_CreateTextureFromSurface(renderer, surface);
2022-05-09 01:50:09 -04:00
2022-08-18 12:17:43 -04:00
//SDL_SetTextureBlendMode(glyph, SDL_BlendMode::SDL_BLENDMODE_NONE);
// SDL_SetTextureAlphaMod(glyph, attr.a);
2022-05-09 01:50:09 -04:00
2022-08-18 12:17:43 -04:00
pos.x += slot->bitmap_left;
pos.y -= slot->bitmap_top;
//SDL_RenderCopy(renderer, glyph, nullptr, &pos);
2022-05-09 01:50:09 -04:00
2022-08-18 12:17:43 -04:00
//SDL_DestroyTexture(glyph);
//SDL_FreeSurface(surface);
pos.x += slot->advance.x >> 6;
pos.y += slot->advance.y >> 6;
2022-05-09 01:50:09 -04:00
}
}
2022-08-18 12:17:43 -04:00
void RenderStringMarkdown(const NVL::Environment::Variable& s, u32 pos_x, u32 pos_y, const SDL_Color& attr) {
NVL::String str = std::get<NVL::String>(s.value);
RenderString(str, pos_x, pos_y, attr);
}
}