Skip to content

Instantly share code, notes, and snippets.

@arielm
Last active December 15, 2015 23:09
Show Gist options
  • Save arielm/5338375 to your computer and use it in GitHub Desktop.
Save arielm/5338375 to your computer and use it in GitHub Desktop.
Test case for Cinder's gl::TextureFont
#include "cinder/app/AppNative.h"
#include "cinder/gl/TextureFont.h"
using namespace ci;
using namespace ci::app;
using namespace std;
const string text = "This is a long sentence that should split over a few good lines, hopefully!";
class Application : public AppNative
{
public:
Font font;
gl::TextureFontRef textureFont;
TextBox textBox;
Rectf bounds;
vector<pair<uint16_t,Vec2f>> glyphMeasures;
gl::TextureFont::DrawOptions options;
float scale;
Vec2f corner;
void setup();
void draw();
};
void Application::setup()
{
gl::TextureFont::Format format;
format.textureWidth(512).textureHeight(512).enableMipmapping();
font = Font("ArialMT", 32);
textureFont = gl::TextureFont::create(font, format, gl::TextureFont::defaultChars());
scale = 0.75f;
corner = Vec2f(30, 50);
/*
* PROBLEM 1: THIS IS CREATING A DEFAULT-FONT WHICH ENDS-UP BEHING UNUSED
*/
textBox = TextBox().font(font).text(text).size(320, TextBox::GROW);
/*
* PROBLEM 2, PART A: THIS IS INVOKING TextBox::createLines()
*/
Vec2f size = textBox.measure() * scale;
bounds = Rectf(0, 0, size.x, size.y);
/*
* PROBLEM 2, PART B: THIS IS (AGAIN) INVOKING TextBox::createLines()
*/
glyphMeasures = textBox.measureGlyphs();
options = gl::TextureFont::DrawOptions().scale(scale);
// ---
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);
}
void Application::draw()
{
gl::clear(Color(0.5f, 0.5f, 0.5f), false);
gl::setMatricesWindow(getWindowSize());
// ---
gl::translate(corner);
gl::color(Color(1.0f, 0.5f, 0.0f));
gl::drawSolidRect(bounds);
/*
* PROBLEM 3: TEXT LOOKS DIRTY WHEN SCALE IS NOT 1.0
*/
gl::color(Color::white());
textureFont->drawGlyphs( glyphMeasures, Vec2f(0, textureFont->getAscent()) * scale, options);
}
CINDER_APP_NATIVE(Application, RendererGl(0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment