mirror of
https://github.com/lubosz/overte.git
synced 2025-08-07 18:21:16 +02:00
Share instances of TextRenderer.
This commit is contained in:
parent
2216f89b8d
commit
de8dd4cecc
7 changed files with 70 additions and 37 deletions
|
@ -181,9 +181,10 @@ const glm::vec3 randVector() {
|
||||||
}
|
}
|
||||||
|
|
||||||
static TextRenderer* textRenderer(int mono) {
|
static TextRenderer* textRenderer(int mono) {
|
||||||
static TextRenderer* monoRenderer = new TextRenderer(MONO_FONT_FAMILY);
|
static TextRenderer* monoRenderer = TextRenderer::getInstance(MONO_FONT_FAMILY);
|
||||||
static TextRenderer* proportionalRenderer = new TextRenderer(SANS_FONT_FAMILY, -1, -1, false, TextRenderer::SHADOW_EFFECT);
|
static TextRenderer* proportionalRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY,
|
||||||
static TextRenderer* inconsolataRenderer = new TextRenderer(INCONSOLATA_FONT_FAMILY, -1, QFont::Bold, false);
|
-1, -1, false, TextRenderer::SHADOW_EFFECT);
|
||||||
|
static TextRenderer* inconsolataRenderer = TextRenderer::getInstance(INCONSOLATA_FONT_FAMILY, -1, QFont::Bold, false);
|
||||||
switch (mono) {
|
switch (mono) {
|
||||||
case 1:
|
case 1:
|
||||||
return monoRenderer;
|
return monoRenderer;
|
||||||
|
|
|
@ -239,8 +239,9 @@ enum TextRendererType {
|
||||||
};
|
};
|
||||||
|
|
||||||
static TextRenderer* textRenderer(TextRendererType type) {
|
static TextRenderer* textRenderer(TextRendererType type) {
|
||||||
static TextRenderer* chatRenderer = new TextRenderer(SANS_FONT_FAMILY, 24, -1, false, TextRenderer::SHADOW_EFFECT);
|
static TextRenderer* chatRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, 24, -1,
|
||||||
static TextRenderer* displayNameRenderer = new TextRenderer(SANS_FONT_FAMILY, 12, -1, false, TextRenderer::NO_EFFECT);
|
false, TextRenderer::SHADOW_EFFECT);
|
||||||
|
static TextRenderer* displayNameRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, 12);
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case CHAT:
|
case CHAT:
|
||||||
|
|
|
@ -50,7 +50,7 @@ BandwidthMeter::ChannelInfo BandwidthMeter::_CHANNELS[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
BandwidthMeter::BandwidthMeter() :
|
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) {
|
_scaleMaxIndex(INITIAL_SCALE_MAXIMUM_INDEX) {
|
||||||
|
|
||||||
_channels = static_cast<ChannelInfo*>( malloc(sizeof(_CHANNELS)) );
|
_channels = static_cast<ChannelInfo*>( malloc(sizeof(_CHANNELS)) );
|
||||||
|
@ -140,7 +140,7 @@ void BandwidthMeter::render(int screenWidth, int screenHeight) {
|
||||||
float totalMax = glm::max(totalIn, totalOut);
|
float totalMax = glm::max(totalIn, totalOut);
|
||||||
|
|
||||||
// Get font / caption metrics
|
// Get font / caption metrics
|
||||||
QFontMetrics const& fontMetrics = _textRenderer.metrics();
|
QFontMetrics const& fontMetrics = _textRenderer->metrics();
|
||||||
int fontDescent = fontMetrics.descent();
|
int fontDescent = fontMetrics.descent();
|
||||||
int labelWidthIn = fontMetrics.width(CAPTION_IN);
|
int labelWidthIn = fontMetrics.width(CAPTION_IN);
|
||||||
int labelWidthOut = fontMetrics.width(CAPTION_OUT);
|
int labelWidthOut = fontMetrics.width(CAPTION_OUT);
|
||||||
|
@ -163,9 +163,9 @@ void BandwidthMeter::render(int screenWidth, int screenHeight) {
|
||||||
|
|
||||||
// Render captions
|
// Render captions
|
||||||
setColorRGBA(COLOR_TEXT);
|
setColorRGBA(COLOR_TEXT);
|
||||||
_textRenderer.draw(barWidth + SPACING_LEFT_CAPTION_UNIT, textYcenteredLine, CAPTION_UNIT);
|
_textRenderer->draw(barWidth + SPACING_LEFT_CAPTION_UNIT, textYcenteredLine, CAPTION_UNIT);
|
||||||
_textRenderer.draw(-labelWidthIn - SPACING_RIGHT_CAPTION_IN_OUT, textYupperLine, CAPTION_IN);
|
_textRenderer->draw(-labelWidthIn - SPACING_RIGHT_CAPTION_IN_OUT, textYupperLine, CAPTION_IN);
|
||||||
_textRenderer.draw(-labelWidthOut - SPACING_RIGHT_CAPTION_IN_OUT, textYlowerLine, CAPTION_OUT);
|
_textRenderer->draw(-labelWidthOut - SPACING_RIGHT_CAPTION_IN_OUT, textYlowerLine, CAPTION_OUT);
|
||||||
|
|
||||||
// Render vertical lines for the frame
|
// Render vertical lines for the frame
|
||||||
setColorRGBA(COLOR_FRAME);
|
setColorRGBA(COLOR_FRAME);
|
||||||
|
@ -229,11 +229,11 @@ void BandwidthMeter::render(int screenWidth, int screenHeight) {
|
||||||
char fmtBuf[8];
|
char fmtBuf[8];
|
||||||
setColorRGBA(COLOR_TEXT);
|
setColorRGBA(COLOR_TEXT);
|
||||||
sprintf(fmtBuf, "%0.1f", totalIn);
|
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),
|
PADDING_HORIZ_VALUE),
|
||||||
textYupperLine, fmtBuf);
|
textYupperLine, fmtBuf);
|
||||||
sprintf(fmtBuf, "%0.1f", totalOut);
|
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),
|
PADDING_HORIZ_VALUE),
|
||||||
textYlowerLine, fmtBuf);
|
textYlowerLine, fmtBuf);
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,7 @@ private:
|
||||||
|
|
||||||
static ChannelInfo _CHANNELS[];
|
static ChannelInfo _CHANNELS[];
|
||||||
|
|
||||||
TextRenderer _textRenderer;
|
TextRenderer* _textRenderer;
|
||||||
ChannelInfo* _channels;
|
ChannelInfo* _channels;
|
||||||
Stream _streams[N_STREAMS];
|
Stream _streams[N_STREAMS];
|
||||||
int _scaleMaxIndex;
|
int _scaleMaxIndex;
|
||||||
|
|
|
@ -24,22 +24,23 @@
|
||||||
// the width/height of the cached glyph textures
|
// the width/height of the cached glyph textures
|
||||||
const int IMAGE_SIZE = 256;
|
const int IMAGE_SIZE = 256;
|
||||||
|
|
||||||
Glyph::Glyph(int textureID, const QPoint& location, const QRect& bounds, int width) :
|
static uint qHash(const TextRenderer::Properties& key, uint seed = 0) {
|
||||||
_textureID(textureID), _location(location), _bounds(bounds), _width(width) {
|
// 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,
|
static bool operator==(const TextRenderer::Properties& p1, const TextRenderer::Properties& p2) {
|
||||||
EffectType effectType, int effectThickness, QColor color) :
|
return p1.font == p2.font && p1.effect == p2.effect && p1.effectThickness == p2.effectThickness && p1.color == p2.color;
|
||||||
_font(family, pointSize, weight, italic),
|
}
|
||||||
_metrics(_font),
|
|
||||||
_effectType(effectType),
|
TextRenderer* TextRenderer::getInstance(const char* family, int pointSize, int weight, bool italic,
|
||||||
_effectThickness(effectThickness),
|
EffectType effect, int effectThickness, const QColor& color) {
|
||||||
_x(IMAGE_SIZE),
|
Properties properties = { QFont(family, pointSize, weight, italic), effect, effectThickness, color };
|
||||||
_y(IMAGE_SIZE),
|
TextRenderer*& instance = _instances[properties];
|
||||||
_rowHeight(0),
|
if (!instance) {
|
||||||
_color(color) {
|
instance = new TextRenderer(properties);
|
||||||
|
}
|
||||||
_font.setKerning(false);
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
TextRenderer::~TextRenderer() {
|
TextRenderer::~TextRenderer() {
|
||||||
|
@ -122,6 +123,19 @@ int TextRenderer::computeWidth(const char* str)
|
||||||
return width;
|
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) {
|
const Glyph& TextRenderer::getGlyph(char c) {
|
||||||
Glyph& glyph = _glyphs[c];
|
Glyph& glyph = _glyphs[c];
|
||||||
if (glyph.isValid()) {
|
if (glyph.isValid()) {
|
||||||
|
@ -213,3 +227,10 @@ const Glyph& TextRenderer::getGlyph(char c) {
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
return glyph;
|
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) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,6 @@ const char SOLID_BLOCK_CHAR = 127;
|
||||||
// the Inconsolata font family
|
// the Inconsolata font family
|
||||||
#define INCONSOLATA_FONT_FAMILY "Inconsolata"
|
#define INCONSOLATA_FONT_FAMILY "Inconsolata"
|
||||||
|
|
||||||
|
|
||||||
class Glyph;
|
class Glyph;
|
||||||
|
|
||||||
class TextRenderer {
|
class TextRenderer {
|
||||||
|
@ -41,9 +40,17 @@ public:
|
||||||
|
|
||||||
enum EffectType { NO_EFFECT, SHADOW_EFFECT, OUTLINE_EFFECT };
|
enum EffectType { NO_EFFECT, SHADOW_EFFECT, OUTLINE_EFFECT };
|
||||||
|
|
||||||
TextRenderer(const char* family, int pointSize = -1, int weight = -1, bool italic = false,
|
class Properties {
|
||||||
EffectType effect = NO_EFFECT, int effectThickness = 1,
|
public:
|
||||||
QColor color = QColor(255, 255, 255));
|
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();
|
~TextRenderer();
|
||||||
|
|
||||||
const QFontMetrics& metrics() const { return _metrics; }
|
const QFontMetrics& metrics() const { return _metrics; }
|
||||||
|
@ -59,7 +66,9 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
const Glyph& getGlyph (char c);
|
TextRenderer(const Properties& properties);
|
||||||
|
|
||||||
|
const Glyph& getGlyph(char c);
|
||||||
|
|
||||||
// the font to render
|
// the font to render
|
||||||
QFont _font;
|
QFont _font;
|
||||||
|
@ -90,6 +99,8 @@ private:
|
||||||
|
|
||||||
// text color
|
// text color
|
||||||
QColor _color;
|
QColor _color;
|
||||||
|
|
||||||
|
static QHash<Properties, TextRenderer*> _instances;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Glyph {
|
class Glyph {
|
||||||
|
|
|
@ -45,22 +45,21 @@ void TextOverlay::render() {
|
||||||
|
|
||||||
//TextRenderer(const char* family, int pointSize = -1, int weight = -1, bool italic = false,
|
//TextRenderer(const char* family, int pointSize = -1, int weight = -1, bool italic = false,
|
||||||
// EffectType effect = NO_EFFECT, int effectThickness = 1);
|
// EffectType effect = NO_EFFECT, int effectThickness = 1);
|
||||||
TextRenderer textRenderer(SANS_FONT_FAMILY, _fontSize, 50, false, TextRenderer::NO_EFFECT, 1,
|
TextRenderer* textRenderer = TextRenderer::getInstance(SANS_FONT_FAMILY, _fontSize, 50);
|
||||||
QColor(_color.red, _color.green, _color.blue));
|
|
||||||
|
|
||||||
const int leftAdjust = -1; // required to make text render relative to left edge of bounds
|
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
|
const int topAdjust = -2; // required to make text render relative to top edge of bounds
|
||||||
int x = _bounds.left() + _leftMargin + leftAdjust;
|
int x = _bounds.left() + _leftMargin + leftAdjust;
|
||||||
int y = _bounds.top() + _topMargin + topAdjust;
|
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");
|
QStringList lines = _text.split("\n");
|
||||||
int lineOffset = 0;
|
int lineOffset = 0;
|
||||||
foreach(QString thisLine, lines) {
|
foreach(QString thisLine, lines) {
|
||||||
if (lineOffset == 0) {
|
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;
|
const int lineGap = 2;
|
||||||
lineOffset += lineGap;
|
lineOffset += lineGap;
|
||||||
|
|
Loading…
Reference in a new issue