space-game001/src/render/TextRenderer.h

41 lines
964 B
C++

#pragma once
#include <string>
#include <memory>
#include <vector>
#include <unordered_map>
#include <Eigen/Dense>
#include "render/Renderer.h"
namespace ZL {
struct GlyphInfo {
unsigned int texID = 0; // GL texture for glyph
Eigen::Vector2f size; // glyph size in pixels
Eigen::Vector2f bearing; // offset from baseline
unsigned int advance = 0; // advance.x in 1/64 pixels
};
class TextRenderer {
public:
TextRenderer() = default;
~TextRenderer();
bool init(Renderer& renderer, const std::string& ttfPath, int pixelSize);
void drawText(const std::string& text, float x, float y, float scale, bool centered);
private:
bool loadGlyphs(const std::string& ttfPath, int pixelSize);
Renderer* r = nullptr;
std::unordered_map<char, GlyphInfo> glyphs;
// OpenGL objects for a dynamic quad
unsigned int vao = 0;
unsigned int vbo = 0;
std::string shaderName = "text2d";
};
} // namespace ZL