Giter Site home page Giter Site logo

glfont's Introduction

GLFont

GLFont is a text-rendering library written in C++ for OpenGL 3.3, using FreeType 2.

Dependencies

  1. Freetype - http://freetype.org/
  2. GLM - http://glm.g-truc.net/0.9.4/index.html
  3. GLEW - http://glew.sourceforge.net/build.html
  4. GLFW - http://www.glfw.org/ (This is only necessary for the included test project. You can still use GLFont with your own application without GLFW)

C++ Quick Start

Initialize library and create label

First create a font face to use for our labels.

shared_ptr<GLFont> glFont = shared_ptr<GLFont>(new GLFont(".../myFont.ttf"));

Now create a label, passing in the font face we just created.

// Some values we will use to create our labels
int startX = 100;
int startY = 100;
int windowWidth = 800;
int windowHeight = 600;

unique_ptr<FTLabel> label = unique_ptr<FTLabel>(new FTLabel(
  glfont,         // Font face handle
  "Hello world!", // Text to render
  startX,
  startY,
  windowWidth,
  windowHeight
));

Note that the starting x and y coords should be in window space. This means (0,0) is at the top-left corner.

Additionally, we could specify a max width and/or height for our text. For example, the following will print a paragraph starting at window coordinates (100, 100), with a bounding box of 200 x 100.

int maxWidth = 200;
int maxHeight = 100;
unique_ptr<FTLabel> label = unique_ptr<FTLabel>(new FTLabel(
  glFont,
  "[long paragraph here]",
  startX,
  startY,
  maxWidth,
  maxWidth,
  windowWidth,
  windowHeight
));

You can manually change the size or starting position of your label at any time. This is often done when the window is resized (unless you are using absolute label positions that do not change depending on window size).

This will set our label to start being drawn at (500, 250):

label->setPosition(500, 250);

Set max width to 100 and max height to 100:

label->setSize(100, 100);

Font Settings

Set the alignment, color, or size of the label text.

label->setPixelSize(24);
label->setIndentation(50);
label->setAlignment(FTLabel::FontFlags::LeftAligned);
label->setColor(0.89, 0.26, 0.3, 0.9);

Rotate text 90 degrees on the y axis:

label->rotate(90, 0, 1, 0);

Additionally, set font options with FTLabel::setFontFlags()

label->setFontFlags(FontFlags::WordWrap | FontFlags::Indented);

Rendering Text

In the render loop, simply call FTLabel::render()

label->render();

Additional Notes

Whenever the window is resized, you should update the window size of your label

label->setWindowSize(windowWidth, windowSize);

glfont's People

Contributors

jtberglund avatar e-sarkis avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.