Skip to content

Commit

Permalink
Add Font::print(const Mat3&, const std::string&, Rgba) overload
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Nov 12, 2024
1 parent 19f78a3 commit 3d3b722
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/AchievementLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ void AchievementLayer::draw() const {
drawRect(mv, BOX, Color(50, 50, 50));
mv.translate(PADDING);

setFontColor(0xffffff_rgb, 1.f);
setFontSize(37);
pWindow->getFontImpl()->print(mv, achievement->name);
pWindow->getFontImpl()->print(mv, achievement->name, 0xffffffff_rgba);

mv.translate({0, 50});
setFontSize(28);
Expand All @@ -86,7 +85,7 @@ void AchievementLayer::draw() const {
} else {
tmp << maxValue;
}
pWindow->getFontImpl()->print(mv, tmp.str());
pWindow->getFontImpl()->print(mv, tmp.str(), 0xffffffff_rgba);

Vec2 bar(BOX.x - PADDING.x * 2, 10);
mv.translate({0, 40}); // below text
Expand Down
6 changes: 3 additions & 3 deletions src/freetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ void FontImpl::setLineHeight(Pixels h) {
lineHeight = static_cast<int>(h);
}

void FontImpl::print(Mat3 modelview, const std::string& text) {
void FontImpl::print(Mat3 modelview, const std::string& text, Rgba color) {
auto context = Texture::textureShaderProgram->use();
glUniform4f(Texture::shaderSpriteColorUniform, gFontColor.getRed(), gFontColor.getGreen(),
gFontColor.getBlue(), gFontColor.getAlpha());
glUniform4f(Texture::shaderSpriteColorUniform, color.getRed(), color.getGreen(),
color.getBlue(), color.getAlpha());
std::vector<std::string> lines(splitlines(text));

auto lineEnd = lines.end();
Expand Down
2 changes: 1 addition & 1 deletion src/freetype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class FontImpl {
FontImpl(FontImpl&&) = delete;
FontImpl& operator=(FontImpl&&) = delete;
~FontImpl();
void print(Mat3 modelview, const std::string& text);
void print(Mat3 modelview, const std::string& text, Rgba color);
void print(ScaleablePixels x, ScaleablePixels y, const std::string& text);
Pixels getTextWidth(const std::string& text);
Pixels getLineHeight() const;
Expand Down
2 changes: 1 addition & 1 deletion src/jngl/TextLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void TextLine::draw(Mat3 modelview) const {
const double lineSpacing = static_cast<double>(ScaleablePixels{ fontImpl->getLineHeight() }) *
(1 - 1 / LINE_HEIGHT_FACOTR);

fontImpl->print(modelview.translate(position + Vec2(0, lineSpacing / 2.)), text);
fontImpl->print(modelview.translate(position + Vec2(0, lineSpacing / 2.)), text, gFontColor);
}

void TextLine::setText(std::string text) {
Expand Down
6 changes: 5 additions & 1 deletion src/jngl/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Font::Font(const std::string& filename, unsigned int size, float strokePercentag
: impl(new FontImpl(filename, size, strokePercentage)) {
}

void Font::print(const Mat3& modelview, const std::string& text, Rgba color) const {
impl->print(modelview, text, color);
}

void Font::print(const std::string& text, int x, int y) {
impl->print(ScaleablePixels(x), ScaleablePixels(y), text);
}
Expand All @@ -50,7 +54,7 @@ void Font::print(const std::string& text, const Vec2 position) const {
}

void Font::print(const Mat3& modelview, const std::string& text) const {
impl->print(modelview, text);
impl->print(modelview, text, gFontColor);
}

double Font::getTextWidth(std::string_view text) {
Expand Down
7 changes: 5 additions & 2 deletions src/jngl/font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ class Font {
/// you would pass 10.f for \a strokePercentage.
Font(const std::string& filename, unsigned int size, float strokePercentage = 0);

/// Draw \a text using \a modelview in \a color
void print(const Mat3& modelview, const std::string& text, Rgba color) const;

/// Uses the font to print something at \a x \a y. The color can be specified using setFontColor.
void print(const std::string&, int x, int y);

/// Draw \a text at \a position
/// Draw \a text at \a position. The color can be specified using setFontColor.
void print(const std::string& text, Vec2 position) const;

/// Draw \a text using \a modelview
/// Draw \a text using \a modelview. The color can be specified using setFontColor.
void print(const Mat3& modelview, const std::string& text) const;

/// Calculates the width of \a text in scale-independent pixels if it would be drawn with this
Expand Down
2 changes: 1 addition & 1 deletion src/jngl/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Text::Line {
this->font = std::move(font);
}
void draw(Mat3 modelview) const {
font->print(modelview.translate(position), text);
font->print(modelview.translate(position), text, gFontColor);
}
double getWidth() const {
return static_cast<double>(static_cast<ScaleablePixels>(width));
Expand Down
2 changes: 1 addition & 1 deletion src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void Window::print(const std::string& text, const int xposition, const int yposi
}

void Window::print(const Mat3& modelview, const std::string& text) {
getFontImpl()->print(modelview, text);
getFontImpl()->print(modelview, text, gFontColor);
}

void Window::setFont(const std::string& filename) {
Expand Down

0 comments on commit 3d3b722

Please sign in to comment.