Share instances of TextRenderer.

This commit is contained in:
Andrzej Kapolka 2014-09-10 18:55:36 -07:00
parent 2216f89b8d
commit de8dd4cecc
7 changed files with 70 additions and 37 deletions

View file

@ -181,9 +181,10 @@ const glm::vec3 randVector() {
}
static TextRenderer* textRenderer(int mono) {
static TextRenderer* monoRenderer = new TextRenderer(MONO_FONT_FAMILY);
static TextRenderer* proportionalRenderer = new TextRenderer(SANS_FONT_FAMILY, -1, -1, false, TextRenderer::SHADOW_EFFECT);
static TextRenderer* inconsolataRenderer = new TextRenderer(INCONSOLATA_FONT_FAMILY, -1, QFont::Bold, false);
static TextRenderer* monoRenderer = TextRenderer::getInstance(MONO_FONT_FAMILY);
static TextRenderer* proportionalRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY,
-1, -1, false, TextRenderer::SHADOW_EFFECT);
static TextRenderer* inconsolataRenderer = TextRenderer::getInstance(INCONSOLATA_FONT_FAMILY, -1, QFont::Bold, false);
switch (mono) {
case 1:
return monoRenderer;

View file

@ -239,8 +239,9 @@ enum TextRendererType {
};
static TextRenderer* textRenderer(TextRendererType type) {
static TextRenderer* chatRenderer = new TextRenderer(SANS_FONT_FAMILY, 24, -1, false, TextRenderer::SHADOW_EFFECT);
static TextRenderer* displayNameRenderer = new TextRenderer(SANS_FONT_FAMILY, 12, -1, false, TextRenderer::NO_EFFECT);
static TextRenderer* chatRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, 24, -1,
false, TextRenderer::SHADOW_EFFECT);
static TextRenderer* displayNameRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, 12);
switch(type) {
case CHAT:

View file

@ -50,7 +50,7 @@ BandwidthMeter::ChannelInfo BandwidthMeter::_CHANNELS[] = {
};
BandwidthMeter::BandwidthMeter() :
_textRenderer(INCONSOLATA_FONT_FAMILY, -1, QFont::Bold, false),
_textRenderer(TextRenderer::getInstance(INCONSOLATA_FONT_FAMILY, -1, QFont::Bold, false)),
_scaleMaxIndex(INITIAL_SCALE_MAXIMUM_INDEX) {
_channels = static_cast<ChannelInfo*>( malloc(sizeof(_CHANNELS)) );
@ -140,7 +140,7 @@ void BandwidthMeter::render(int screenWidth, int screenHeight) {
float totalMax = glm::max(totalIn, totalOut);
// Get font / caption metrics
QFontMetrics const& fontMetrics = _textRenderer.metrics();
QFontMetrics const& fontMetrics = _textRenderer->metrics();
int fontDescent = fontMetrics.descent();
int labelWidthIn = fontMetrics.width(CAPTION_IN);
int labelWidthOut = fontMetrics.width(CAPTION_OUT);
@ -163,9 +163,9 @@ void BandwidthMeter::render(int screenWidth, int screenHeight) {
// Render captions
setColorRGBA(COLOR_TEXT);
_textRenderer.draw(barWidth + SPACING_LEFT_CAPTION_UNIT, textYcenteredLine, CAPTION_UNIT);
_textRenderer.draw(-labelWidthIn - SPACING_RIGHT_CAPTION_IN_OUT, textYupperLine, CAPTION_IN);
_textRenderer.draw(-labelWidthOut - SPACING_RIGHT_CAPTION_IN_OUT, textYlowerLine, CAPTION_OUT);
_textRenderer->draw(barWidth + SPACING_LEFT_CAPTION_UNIT, textYcenteredLine, CAPTION_UNIT);
_textRenderer->draw(-labelWidthIn - SPACING_RIGHT_CAPTION_IN_OUT, textYupperLine, CAPTION_IN);
_textRenderer->draw(-labelWidthOut - SPACING_RIGHT_CAPTION_IN_OUT, textYlowerLine, CAPTION_OUT);
// Render vertical lines for the frame
setColorRGBA(COLOR_FRAME);
@ -229,11 +229,11 @@ void BandwidthMeter::render(int screenWidth, int screenHeight) {
char fmtBuf[8];
setColorRGBA(COLOR_TEXT);
sprintf(fmtBuf, "%0.1f", totalIn);
_textRenderer.draw(glm::max(xIn - fontMetrics.width(fmtBuf) - PADDING_HORIZ_VALUE,
_textRenderer->draw(glm::max(xIn - fontMetrics.width(fmtBuf) - PADDING_HORIZ_VALUE,
PADDING_HORIZ_VALUE),
textYupperLine, fmtBuf);
sprintf(fmtBuf, "%0.1f", totalOut);
_textRenderer.draw(glm::max(xOut - fontMetrics.width(fmtBuf) - PADDING_HORIZ_VALUE,
_textRenderer->draw(glm::max(xOut - fontMetrics.width(fmtBuf) - PADDING_HORIZ_VALUE,
PADDING_HORIZ_VALUE),
textYlowerLine, fmtBuf);

View file

@ -78,7 +78,7 @@ private:
static ChannelInfo _CHANNELS[];
TextRenderer _textRenderer;
TextRenderer* _textRenderer;
ChannelInfo* _channels;
Stream _streams[N_STREAMS];
int _scaleMaxIndex;

View file

@ -24,22 +24,23 @@
// the width/height of the cached glyph textures
const int IMAGE_SIZE = 256;
Glyph::Glyph(int textureID, const QPoint& location, const QRect& bounds, int width) :
_textureID(textureID), _location(location), _bounds(bounds), _width(width) {
static uint qHash(const TextRenderer::Properties& key, uint seed = 0) {
// can be switched to qHash(key.font, seed) when we require Qt 5.3+
return qHash(key.font.family(), qHash(key.font.pointSize(), seed));
}
TextRenderer::TextRenderer(const char* family, int pointSize, int weight, bool italic,
EffectType effectType, int effectThickness, QColor color) :
_font(family, pointSize, weight, italic),
_metrics(_font),
_effectType(effectType),
_effectThickness(effectThickness),
_x(IMAGE_SIZE),
_y(IMAGE_SIZE),
_rowHeight(0),
_color(color) {
_font.setKerning(false);
static bool operator==(const TextRenderer::Properties& p1, const TextRenderer::Properties& p2) {
return p1.font == p2.font && p1.effect == p2.effect && p1.effectThickness == p2.effectThickness && p1.color == p2.color;
}
TextRenderer* TextRenderer::getInstance(const char* family, int pointSize, int weight, bool italic,
EffectType effect, int effectThickness, const QColor& color) {
Properties properties = { QFont(family, pointSize, weight, italic), effect, effectThickness, color };
TextRenderer*& instance = _instances[properties];
if (!instance) {
instance = new TextRenderer(properties);
}
return instance;
}
TextRenderer::~TextRenderer() {
@ -122,6 +123,19 @@ int TextRenderer::computeWidth(const char* str)
return width;
}
TextRenderer::TextRenderer(const Properties& properties) :
_font(properties.font),
_metrics(_font),
_effectType(properties.effect),
_effectThickness(properties.effectThickness),
_x(IMAGE_SIZE),
_y(IMAGE_SIZE),
_rowHeight(0),
_color(properties.color) {
_font.setKerning(false);
}
const Glyph& TextRenderer::getGlyph(char c) {
Glyph& glyph = _glyphs[c];
if (glyph.isValid()) {
@ -213,3 +227,10 @@ const Glyph& TextRenderer::getGlyph(char c) {
glBindTexture(GL_TEXTURE_2D, 0);
return glyph;
}
QHash<TextRenderer::Properties, TextRenderer*> TextRenderer::_instances;
Glyph::Glyph(int textureID, const QPoint& location, const QRect& bounds, int width) :
_textureID(textureID), _location(location), _bounds(bounds), _width(width) {
}

View file

@ -33,7 +33,6 @@ const char SOLID_BLOCK_CHAR = 127;
// the Inconsolata font family
#define INCONSOLATA_FONT_FAMILY "Inconsolata"
class Glyph;
class TextRenderer {
@ -41,9 +40,17 @@ public:
enum EffectType { NO_EFFECT, SHADOW_EFFECT, OUTLINE_EFFECT };
TextRenderer(const char* family, int pointSize = -1, int weight = -1, bool italic = false,
EffectType effect = NO_EFFECT, int effectThickness = 1,
QColor color = QColor(255, 255, 255));
class Properties {
public:
QFont font;
EffectType effect;
int effectThickness;
QColor color;
};
static TextRenderer* getInstance(const char* family, int pointSize = -1, int weight = -1, bool italic = false,
EffectType effect = NO_EFFECT, int effectThickness = 1, const QColor& color = QColor(255, 255, 255));
~TextRenderer();
const QFontMetrics& metrics() const { return _metrics; }
@ -59,7 +66,9 @@ public:
private:
const Glyph& getGlyph (char c);
TextRenderer(const Properties& properties);
const Glyph& getGlyph(char c);
// the font to render
QFont _font;
@ -90,6 +99,8 @@ private:
// text color
QColor _color;
static QHash<Properties, TextRenderer*> _instances;
};
class Glyph {

View file

@ -45,22 +45,21 @@ void TextOverlay::render() {
//TextRenderer(const char* family, int pointSize = -1, int weight = -1, bool italic = false,
// EffectType effect = NO_EFFECT, int effectThickness = 1);
TextRenderer textRenderer(SANS_FONT_FAMILY, _fontSize, 50, false, TextRenderer::NO_EFFECT, 1,
QColor(_color.red, _color.green, _color.blue));
TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, _fontSize, 50);
const int leftAdjust = -1; // required to make text render relative to left edge of bounds
const int topAdjust = -2; // required to make text render relative to top edge of bounds
int x = _bounds.left() + _leftMargin + leftAdjust;
int y = _bounds.top() + _topMargin + topAdjust;
glColor3f(1.0f, 1.0f, 1.0f);
glColor3f(_color.red / MAX_COLOR, _color.green / MAX_COLOR, _color.blue / MAX_COLOR);
QStringList lines = _text.split("\n");
int lineOffset = 0;
foreach(QString thisLine, lines) {
if (lineOffset == 0) {
lineOffset = textRenderer.calculateHeight(qPrintable(thisLine));
lineOffset = textRenderer->calculateHeight(qPrintable(thisLine));
}
lineOffset += textRenderer.draw(x, y + lineOffset, qPrintable(thisLine));
lineOffset += textRenderer->draw(x, y + lineOffset, qPrintable(thisLine));
const int lineGap = 2;
lineOffset += lineGap;