Simple Text Output         Simple debug text output using overlays

Here's a very simple class I put together for showing debug strings in my apps. This is based on some examples I found around the forums.


#include <Ogre.h>
#include <OgreSingleton.h>

class TextRenderer : public Ogre::Singleton<TextRenderer>

    Ogre::OverlayManager*    _overlayMgr;
    Ogre::Overlay*           _overlay;
    Ogre::OverlayContainer*  _panel;



    void addTextBox(
        const std::string& ID,
        const std::string& text,
        Ogre::Real x, Ogre::Real y,
        Ogre::Real width, Ogre::Real height,
        const Ogre::ColourValue& color = Ogre::ColourValue(1.0, 1.0, 1.0, 1.0));

    void removeTextBox(const std::string& ID);

    void setText(const std::string& ID, const std::string& Text);
    const std::string& getText(const std::string& ID);


#include "TextRenderer.h"

template<> TextRenderer* Ogre::Singleton<TextRenderer>::ms_Singleton = 0;

    _overlayMgr = Ogre::OverlayManager::getSingletonPtr();

    _overlay = _overlayMgr->create("overlay1");
    _panel = static_cast<Ogre::OverlayContainer*>(_overlayMgr->createOverlayElement("Panel", "container1"));
    _panel->setDimensions(1, 1);
    _panel->setPosition(0, 0);



void TextRenderer::addTextBox(const std::string& ID,
                const std::string& text,
                Ogre::Real x, Ogre::Real y,
                Ogre::Real width, Ogre::Real height,
                const Ogre::ColourValue& color)
    Ogre::OverlayElement* textBox = _overlayMgr->createOverlayElement("TextArea", ID);
    textBox->setDimensions(width, height);
    textBox->setPosition(x, y);
    textBox->setParameter("font_name", "MyFont");
    textBox->setParameter("char_height", "0.3");



void TextRenderer::removeTextBox(const std::string& ID)

void TextRenderer::setText(const std::string& ID, const std::string& Text)
    Ogre::OverlayElement* textBox = _overlayMgr->getOverlayElement(ID);

const std::string& TextRenderer::getText(const std::string& ID)
    Ogre::OverlayElement* textBox = _overlayMgr->getOverlayElement(ID);
    return textBox->getCaption();

For this code to work, you need a font def that defines MyFont. Something like this:

    type         truetype
    source         arial.ttf
    size         16
    resolution     72

Make sure that this font is located in the directory with your font script. (Just because it is a common font, it doesn't meant it will go and find it in your system fonts directory).

Make sure the TextRenderer singleton is constructed (call only once).

new TextRenderer();

To create a text box, you would write something like:

TextRenderer::getSingleton().addTextBox("txtGreeting", "Hello", 10, 10, 100, 20, Ogre::ColourValue::Green);

Then, if you want to update the text:

TextRenderer::getSingleton().setText("txtGreeting", "Goodbye");

Edit by durmieu:
If you miss a 'printf-like' formated output function just add the following to the class. Don't know if this is the best way of doing it but it works fine and it's quite useful for debugging.

#include <stdio.h>
#include <stdarg.h>

void TextRenderer::printf(const std::string& ID,  const char *fmt, /* args*/ ...)
    char        text[256];
    va_list        ap;

    if (fmt == NULL)

    else {
    va_start(ap, fmt);
        vsprintf(text, fmt, ap);

    Ogre::OverlayElement* textBox = _overlayMgr->getOverlayElement(ID);

Alias: Simple_Text_Output