celestia/src/celtxf/texturefont.h

112 lines
2.4 KiB
C++

// texturefont.h
//
// Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _TEXTUREFONT_H_
#define _TEXTUREFONT_H_
#include <vector>
#include <string>
#include <iostream>
#include <celcompat/filesystem.h>
class Renderer;
class TextureFont
{
TextureFont(const Renderer*);
public:
TextureFont() = delete;
~TextureFont();
float render(wchar_t ch, float xoffset = 0.0f, float yoffset = 0.0f) const;
float render(const std::string& s, float xoffset = 0.0f, float yoffset = 0.0f) const;
int getWidth(const std::string&) const;
int getWidth(int c) const;
int getMaxWidth() const;
int getHeight() const;
int getMaxAscent() const;
void setMaxAscent(int);
int getMaxDescent() const;
void setMaxDescent(int);
short getAdvance(wchar_t c)
{
const Glyph* glyph = getGlyph(c);
if (glyph == nullptr)
glyph = getGlyph((wchar_t) '?');
return glyph->advance;
}
int getTextureName() const;
void bind();
void unbind();
void flush() {};
bool buildTexture();
public:
struct TexCoord
{
float u, v;
};
struct Glyph
{
Glyph(unsigned short _id) : __id(_id) {};
unsigned short __id;
unsigned short width;
unsigned short height;
short x;
short xoff;
short y;
short yoff;
short advance;
TexCoord texCoords[4];
};
enum
{
TxfByte = 0,
TxfBitmap = 1,
};
private:
void addGlyph(const Glyph&);
const TextureFont::Glyph* getGlyph(wchar_t) const;
void rebuildGlyphLookupTable();
private:
const Renderer *renderer;
int maxAscent{ 0 };
int maxDescent{ 0 };
int maxWidth{ 0 };
int texWidth{ 0 };
int texHeight{ 0 };
unsigned char* fontImage{ nullptr };
unsigned int texName{ 0 };
std::vector<Glyph> glyphs;
const Glyph** glyphLookup{ nullptr };
unsigned int glyphLookupTableSize{ 0 };
public:
static TextureFont* load(const Renderer*, std::istream& in);
};
TextureFont* LoadTextureFont(const Renderer*, const fs::path&);
#endif // _TEXTUREFONT_H_