mirror of
https://github.com/devZoGok/vb01.git
synced 2026-08-26 19:43:30 +00:00
using the same glyph textures for the same fonts
This commit is contained in:
@@ -3,9 +3,17 @@
|
||||
|
||||
#include "asset.h"
|
||||
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
namespace vb01{
|
||||
class Texture;
|
||||
|
||||
struct FontAsset : public Asset{
|
||||
FT_Face face;
|
||||
std::vector<std::pair<int, Texture*>> glyphTextures;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include "texture.h"
|
||||
#include "node.h"
|
||||
#include "material.h"
|
||||
#include "fontAsset.h"
|
||||
#include "assetManager.h"
|
||||
|
||||
#include "glad.h"
|
||||
@@ -23,17 +22,20 @@ namespace vb01{
|
||||
}
|
||||
|
||||
Text::~Text(){
|
||||
clearFont();
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
}
|
||||
|
||||
//TODO ensure destruction of textures when unloading the asset
|
||||
void Text::applyFont(string fontPath, u16 firstChar, u16 lastChar){
|
||||
clearFont();
|
||||
characters.clear();
|
||||
|
||||
FontAsset *font = (FontAsset*)AssetManager::getSingleton()->getAsset(fontPath);
|
||||
font = (FontAsset*)AssetManager::getSingleton()->getAsset(fontPath);
|
||||
FT_Face face = font->face;
|
||||
vector<int> keys;
|
||||
|
||||
for (pair<int, Texture*> p : font->glyphTextures)
|
||||
keys.push_back(p.first);
|
||||
|
||||
for(u16 i = firstChar; i < lastChar; i++){
|
||||
if(FT_Load_Char(face, i, FT_LOAD_RENDER)){
|
||||
@@ -43,18 +45,15 @@ namespace vb01{
|
||||
|
||||
Glyph c;
|
||||
c.ch = i;
|
||||
c.texture = new Texture(face);
|
||||
c.size = Vector2(face->glyph->bitmap.width, face->glyph->bitmap.rows);
|
||||
c.bearing = Vector2(face->glyph->bitmap_left, face->glyph->bitmap_top);
|
||||
c.advance = face->glyph->advance.x;
|
||||
characters.push_back(c);
|
||||
}
|
||||
}
|
||||
c.texId = i;
|
||||
|
||||
void Text::clearFont(){
|
||||
while(!characters.empty()){
|
||||
delete characters[characters.size() - 1].texture;
|
||||
characters.pop_back();
|
||||
if(find(keys.begin(), keys.end(), i) == keys.end())
|
||||
font->glyphTextures.push_back(make_pair(i, new Texture(face)));
|
||||
|
||||
characters.push_back(c);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,11 +71,17 @@ namespace vb01{
|
||||
for(int i = (leftToRight ? 0 : entry.length() - 1); (leftToRight ? (i < entry.length()) : (i >= 0)); (leftToRight ? i++ : i--)){
|
||||
Glyph *glyphPtr = getGlyph(entry[i]);
|
||||
|
||||
if(!glyphPtr)
|
||||
continue;
|
||||
if(!glyphPtr) continue;
|
||||
|
||||
int glyphTexId = -1;
|
||||
for(int j = 0; j < font->glyphTextures.size(); j++)
|
||||
if(font->glyphTextures[j].first == glyphPtr->texId){
|
||||
glyphTexId = j;
|
||||
break;
|
||||
}
|
||||
|
||||
Glyph glyph = *glyphPtr;
|
||||
prepareGlyphs(glyph, advanceOffset);
|
||||
prepareGlyphs(glyph, glyphTexId, advanceOffset);
|
||||
Vector2 size = glyph.size, bearing = glyph.bearing;
|
||||
|
||||
if(horizontal)
|
||||
@@ -86,7 +91,7 @@ namespace vb01{
|
||||
}
|
||||
}
|
||||
|
||||
void Text::prepareGlyphs(Glyph glyph, Vector2 advanceOffset){
|
||||
void Text::prepareGlyphs(Glyph glyph, int glyphTexId, Vector2 advanceOffset){
|
||||
Vector3 nodePos = node->getPosition();
|
||||
Vector2 origin = Vector2(nodePos.x, nodePos.y) + (advanceOffset * scale);
|
||||
Vector3 scale = node->getScale();
|
||||
@@ -104,17 +109,13 @@ namespace vb01{
|
||||
origin.x + bearing.x, origin.y - bearing.y, 0, 0
|
||||
};
|
||||
|
||||
renderGlyphs(glyph, data, sizeof(data));
|
||||
}
|
||||
|
||||
void Text::renderGlyphs(Glyph glyph, float data[], u32 dataSize){
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, dataSize, data);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(data), data);
|
||||
|
||||
int id = 2;
|
||||
material->getShader()->setInt(id, "textures[1].pastTexture");
|
||||
glyph.texture->select(id);
|
||||
font->glyphTextures[glyphTexId].second->select(id);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "fontAsset.h"
|
||||
#include "attachable.h"
|
||||
#include "vector.h"
|
||||
#include "util.h"
|
||||
|
||||
namespace vb01{
|
||||
class Node;
|
||||
class Texture;
|
||||
class Shader;
|
||||
class Material;
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace vb01{
|
||||
public:
|
||||
struct Glyph{
|
||||
u16 ch;
|
||||
Texture *texture = nullptr;
|
||||
int texId;
|
||||
u32 advance;
|
||||
Vector2 size, bearing;
|
||||
};
|
||||
@@ -41,9 +41,9 @@ namespace vb01{
|
||||
private:
|
||||
void applyFont(std::string, u16, u16 = 256);
|
||||
void clearFont();
|
||||
void prepareGlyphs(Glyph, Vector2);
|
||||
void renderGlyphs(Glyph, float[], u32);
|
||||
void prepareGlyphs(Glyph, int, Vector2);
|
||||
|
||||
FontAsset *font = nullptr;
|
||||
Material *material = nullptr;
|
||||
std::wstring entry;
|
||||
std::vector<Glyph> characters;
|
||||
|
||||
Reference in New Issue
Block a user