78 lines
1.9 KiB
C++
78 lines
1.9 KiB
C++
#include "Graphics.h"
|
|
#include <vector>
|
|
|
|
#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;
|
|
}
|
|
|
|
error = FT_New_Face(library, "Roboto-Regular.ttf", 0, &face);
|
|
if (error == FT_Err_Unknown_File_Format) {
|
|
std::cerr << "FreeType Unknown_File_Format: " << error << std::endl;
|
|
}
|
|
else if (error) {
|
|
std::cerr << "FreeType font loading error: " << error << std::endl;
|
|
}
|
|
|
|
error = FT_Set_Char_Size(face, 0, 12 * 64, 300, 300);
|
|
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);
|
|
}
|
|
|
|
void RenderString(const std::string& s, SDL_Surface* surface) {
|
|
FT_GlyphSlot slot = face->glyph;
|
|
int pen_x = 300, pen_y = 200;
|
|
|
|
|
|
|
|
for (const auto& c : s) {
|
|
error = FT_Load_Char(face, c, FT_LOAD_RENDER);
|
|
if (error)
|
|
continue;
|
|
|
|
std::vector<unsigned char> temp(slot->bitmap.width * slot->bitmap.rows);
|
|
for (int i = 0; i < slot->bitmap.width * slot->bitmap.rows; i++) {
|
|
temp[i] = slot->bitmap.buffer[i];
|
|
}
|
|
|
|
SDL_Surface* glyph = SDL_CreateRGBSurfaceFrom(
|
|
&slot->bitmap.buffer,
|
|
slot->bitmap.width,
|
|
slot->bitmap.rows,
|
|
8,
|
|
slot->bitmap.width * 4,
|
|
0, 0, 0, 255);
|
|
|
|
SDL_Palette* pal = SDL_AllocPalette(256);
|
|
for (int i = 0; i < 256; i++)
|
|
{
|
|
pal->colors[i].r = pal->colors[i].g = pal->colors[i].b = i;
|
|
}
|
|
SDL_SetSurfacePalette(glyph, pal);
|
|
|
|
SDL_SetSurfaceBlendMode(glyph, SDL_BlendMode::SDL_BLENDMODE_NONE);
|
|
SDL_Rect pos = { pen_x + slot->bitmap_left, pen_y - slot->bitmap_top, 0, 0 };
|
|
SDL_BlitSurface(glyph, nullptr, surface, &pos);
|
|
SDL_FreeSurface(glyph);
|
|
|
|
pen_x += slot->advance.x >> 6;
|
|
pen_y += slot->advance.y >> 6;
|
|
}
|
|
}
|
|
}
|