Rendering text to the screen

This simple recipe will show you how to quickly put some text on the screen. This might be useful for debug output, but also for presenting the current score or hit points to the player.

Getting ready

If you haven't done it yet, set up your project according to the steps presented in the recipe Setting up the game structure before you proceed. You can find this recipe in Chapter 1,

How to do it...

Let's put some text on the screen:

  1. Open Application.py and add the following source code:
    from direct.showbase.ShowBase import ShowBase
    from direct.gui.OnscreenText import OnscreenText
    from panda3d.core import *
    class Application(ShowBase):
    def __init__(self):
    ShowBase.__init__(self)
    font = loader.loadFont("cmr12.egg")
    props = TextProperties()
    props.setTextColor(1, 1, 0, 0.5)
    tp = TextPropertiesManager.getGlobalPtr()
    tp.setProperties("yellow", props)
    OnscreenText(text = "Panda3D Rocks!!",
    frame = Vec4(1, 0, 0, 1),
    bg = Vec4(1, 1, 0, 1),
    pos = Vec2(-0.5, 0.5),
    scale = 0.2,
    font = font)
    wrapWidth = 6
    text = OnscreenText(text = "So long... 1yellow1And thanks for all the bamboo2!!",
    wordwrap = wrapWidth,
    fg = Vec4(1, 1, 1, 1),
    shadow = Vec4(0, 0, 0, 1),
    scale = 0.07,
    font = font)
    wrap = text.getScale()[0] * wrapWidth
    print "Word wrap after", wrap, "screen units"
    
  2. Press F6 and run the program:
How to do it...

How it works...

As you can see, outputting text is very easy using OnscreenText(). First, you import the proper package, which is direct.gui.OnscreenText. Then you load the font you wish to use, in this case the "cmr12" font that is included with Panda3D. If you wish to use a different font—no problem! Just use loadFont() to load any "Truetype" font you wish to use. Like other resources, such as models and textures, fonts need to be located in a directory that is configured to be a part of Panda3D's search path. See the description of the model-path configuration variable in the recipe Understanding Panda3D's runtime configuration options in Chapter 1, Setting Up Panda3D and Configuring Development Tools, for further information on adding directories to the search path.

OnscreenText() also allows you to set additional text properties, some of which are shown in the code you just added to your program, like frame for the red outline around the Panda Rocks!! text, bg for setting the background color, fg for setting the text color, scale for scaling the size of the text, shadow for defining a drop shadow below each font and wordwrap to set at which width to insert a line break. You may also use the pos parameter to position the text.

The wordwrap parameter sets the wrap width relative to the scale of the font. The last two lines of the sample code calculate the absolute wrap width in screen units and print it to the screen. In this case, this is 0.7 * 6, so a line break is inserted after about a fifth of the screen width. If the scale parameter is omitted, the engine automatically chooses a scale factor to make the font fit onto the screen.

When you are positioning and scaling text or setting the word wrap width, you are working with a normalized coordinate system. The origin of this coordinate system is at the centre of the window, (-1, 1) is the top-left corner and (1, -1) are the coordinates of the bottom-right corner. This makes it easier to position elements independent from the screen resolution.

Text properties can also be set for sections of text. For getting this to work, you need to create a new instance of the TextProperties class, set the text properties you wish and register them with the global TextPropertiesManager. The custom text properties you create this way are then activated and deactivated using 1[name]1 and 2. This works like a stack: A property name enclosed by 1 enables properties for any subsequent text. Putting 2 into a string pops and thus deactivates the text property you activated last. Watch how the text changes in the sample: The text color is set to yellow before the sentence And thanks for all the bamboo and deactivated again right before the two exclamation marks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset