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.

TextRenderer.h:

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

class TextRenderer : public Ogre::Singleton<TextRenderer>
{
private:

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

public:

    TextRenderer();
    ~TextRenderer();

    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);
};


TextRenderer.cpp:

#include "TextRenderer.h"

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

TextRenderer::TextRenderer()
{
    _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);

    _overlay->add2D(_panel);

    _overlay->show();
}

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->setMetricsMode(Ogre::GMM_PIXELS);
    textBox->setPosition(x, y);
    textBox->setWidth(width);
    textBox->setHeight(height);
    textBox->setParameter("font_name", "MyFont");
    textBox->setParameter("char_height", "0.3");
    textBox->setColour(color);

    textBox->setCaption(text);

    _panel->addChild(textBox);
}

void TextRenderer::removeTextBox(const std::string& ID)
{
    _panel->removeChild(ID);
    _overlayMgr->destroyOverlayElement(ID);
}

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

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:

MyFont
{
    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)
        *text=0;

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

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

Alias: Simple_Text_Output