Adding bitmap fonts

This commit is contained in:
2023-03-14 18:55:10 -07:00
parent 4d0439ca11
commit 7d1fc6906a
18 changed files with 328 additions and 20 deletions

View File

@ -0,0 +1,124 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "BitmapFont.hpp"
using namespace Dawn;
void BitmapFont::buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh *mesh,
struct FontMeasure *info
) {
assertNotNull(mesh);
assertNotNull(info);
assertTrue(maxWidth == -1 || maxWidth > 0);
assertTrue(this->isReady());
// Initialize primitive
mesh->createBuffers(
QUAD_VERTICE_COUNT * text.size(),
QUAD_INDICE_COUNT * text.size()
);
// Setup Scales
info->length = 0;
info->realLength = 0;
info->lines.clear();
info->lineHeight = this->getLineHeight(fontSize);
// Prepare the line counters
info->addLine(0, 0);
// Reset Dimensions
char c;
Tile tile;
info->width = info->height = 0;
float_t x = 0;
float_t y = 0;
size_t i = 0;
size_t j = 0;
glm::vec2 xy0(0, 0);
glm::vec2 tileSize = glm::vec2(tileset->getTileWidth(), tileset->getTileHeight());
// Buffer quads
while(c = text[i++]) {
if(c == FONT_SPACE) {
// Did this space cause a newline?
if(maxWidth != -1 && xy0.x > maxWidth) {
info->addLine(i, 0);
info->width = mathMax<float_t>(info->width, xy0.x);
xy0.x = 0;
xy0.y += tileSize.y;
info->height = mathMax<float_t>(info->height, xy0.y);
continue;
}
xy0.x += tileSize.x;
continue;
}
if(c == FONT_NEWLINE) {
info->addLine(i, 0);
info->width = mathMax<float_t>(info->width, xy0.x);
xy0.x = 0;
xy0.y += tileSize.y;
info->height = mathMax<float_t>(info->height, xy0.y);
continue;
}
// Check for wrapping, todo.
if(maxWidth != -1 && (xy0.x+tileSize.x) > maxWidth) {
// We've exceeded the edge, go back to the start of the word and newline.
// Go back to the previous (still current) line and remove the chars
// Next line begins with this word
}
tile = this->tileset->getTile(c);
QuadMesh::bufferQuadMesh(mesh,
xy0, tile.uv0,
xy0+tileSize, tile.uv1,
j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT
);
xy0.x += tileSize.x;//TODO: Spacing?
j++;
}
info->width = mathMax<float_t>(info->width, xy0.x);
info->height = mathMax<float_t>(info->height, xy0.y + (xy0.x > 0 ? tileSize.y : 0));
}
bool_t BitmapFont::isReady() {
if(this->texture == nullptr) return false;
if(this->tileset == nullptr) return false;
return this->texture->isReady();
}
Texture * BitmapFont::getTexture() {
return this->texture;
}
void BitmapFont::draw(Mesh *mesh, int32_t start, int32_t len) {
assertNotNull(mesh);
mesh->draw(
MESH_DRAW_MODE_TRIANGLES,
start * QUAD_INDICE_COUNT,
len == -1 ? len : len * QUAD_INDICE_COUNT
);
}
float_t BitmapFont::getLineHeight(float_t fontSize) {
return 16.0f;
}
float_t BitmapFont::getDefaultFontSize() {
return 16.0f;
}

View File

@ -0,0 +1,31 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "Font.hpp"
#include "display/Tileset.hpp"
namespace Dawn {
class BitmapFont : public Font {
protected:
public:
Texture *texture = nullptr;
TilesetGrid *tileset = nullptr;
void buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh *mesh,
struct FontMeasure *info
) override;
bool_t isReady() override;
Texture * getTexture() override;
void draw(Mesh *mesh, int32_t startCharacter, int32_t length) override;
float_t getLineHeight(float_t fontSize) override;
float_t getDefaultFontSize() override;
};
}

View File

@ -1,11 +1,12 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
TrueTypeFont.cpp
FontMeasure.cpp
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
BitmapFont.cpp
TrueTypeFont.cpp
FontMeasure.cpp
)