Skip to content

Latest commit

 

History

History
198 lines (145 loc) · 8.02 KB

lecture10.adoc

File metadata and controls

198 lines (145 loc) · 8.02 KB

On-screen text

On-screen text

  • Text is represented by a series of glyphs (symbols)

  • Glyphs are generated from a font

  • A font is a particular size, weight and style of a typeface.

  • A typeface (aka font-family) is a set of fonts that share common design features

  • !!!

  • the words "font" and "typeface" are frequently confused

What do we need to represent (independent of rendering)

  • letters

    • A, a, Q, I

  • numbers

    • 1, 2, 0

  • symbols

    • @, $, £, /

More challenging ones characters??

  • non-ASCII letters

    • é, ω

  • non-ASCII numbers

    • ¼, ³

  • non-ASCII symbols

    • ,

Other Characters

Character encoding

  • ASCII

  • Unicode (Universal Coded Character Set)

    • UTF-8 - USE THIS

    • UTF-16

    • UTF-32

Note
Unicode is a character set. UTF-8 is an encoding of that character set.

Detecting/setting file encoding

ASCII Table

ASCII Table wide

UTF-8

  • You should encode all your strings with UTF-8

  • Your editor (probably) already supports UTF-8

Utf8webgrowth

UTF-8

Number bytes

Bits for code point

First code point

Last code point

Byte 1

Byte 2

Byte 3

Byte 4

1

7

U+0000

U+007F

0xxxxxxx

2

11

U+0080

U+07FF

110xxxxx

10xxxxxx

3

16

U+0800

U+FFFF

1110xxxx

10xxxxxx

10xxxxxx

4

21

U+10000

U+10FFFF

11110xxx

10xxxxxx

10xxxxxx

10xxxxxx

How do we represent text in code

  1. char * - only default option in C

  2. std::string - C++

How well does C++ support Unicode??

  • Pretty badly

    • for you, now, it definitely doesn’t matter

    • there are other libraries to support

Other challenges with text rendering

  • proportional spacing - not all letters are the same width

  • matching screen resolution

    • what about text closer, further

    • what about text in a 3D world?

  • typefaces are mostly copyrighted!!

Good on-screen text turns out to be pretty hard

It is very likely that at one point you will want to draw text with OpenGL. It may seem like a very basic functionality of any drawing API, but you will not find any function in OpenGL that deals with text. There are good reasons for that: text is much more complicated than you think. If you are American and use a fixed-width font, then life is simple. You don’t have more than 128 possible characters to think about, and they are all the same size. If you have a proportional-width font, things are already getting more difficult. If you are European, then 256 characters might be just enough for one language, but it is already impossible to represent all European languages with that. If you include the rest of the world, then you need more than 65536 (16-bit) characters, and text might need to be rendered from right to left or from top to bottom, and you might have to use other techniques than just drawing characters next to each other on the screen to produce something intelligible. Related to text rendering, you might also want to render mathematical equations, Feynman diagrams, chess board diagrams or sheet music. I hope you are convinced now that text rendering is a very high-level function that has no place in a low-level graphics API such as OpenGL.

Font file formats

  • Fonts are usually described in some format of vector format

    • rather than a raster format

  • TrueType is the most common format (.ttf files)

    • the content of .ttf files is protected by copyright

    • you should be careful distributing .ttf files in your game

    • there are a variety of openly licensed .ttf files/formats

On-screen text output in practice

  • In games, text is usually rendered to the screen via a texture or texture atlas

  • There are various libraries that support this

    • for you, now, it may not be worth using any of them

    • just use a preset font atlas, and write your own code

On-screen text output in practice

  1. Somehow have (create) a font atlas (texture) with your set of needed characters at various resolutions

  2. When you render text, draw a sequence of sprites (rectangles), each indexing into the font atlas

Library support

  • SDL2_ttf

    • not working with Conan.io right now. You could add it yourself

    • suggest using a Font atlas approach

  • stb_truetype

    • header only

    • lower level

SDL2_ttf

  • the SDL2_ttf library provides useful functions

    • the TTF_OpenFont() function loads a font from a .ttf file

    • the TTF_RenderText_Solid() function generates an SDL_Surface from a font, a string, and a colour

    • this SDL_Surface can be converted to an SDL_Texture with SDL_CreateTextureFromSurface()

    • an SDL_Surface can then be rendered using SDL_RenderCopy(), just like a sprite

  • manual set up instructions (there are other sources)

State of the Art

improvedAlphaTestedMagnificationForVectorTextureAndSpecialEffects
Figure 1. Green, C., 2007. Improved alpha-tested magnification for vector textures and special effects. In ACM SIGGRAPH 2007 courses (pp. 9-18). ACM.