Skip to main content

History: Simple Text Output

Source of version: 4

Copy to clipboard
            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:
{CODE(wrap="1", colors="c++")}#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);
};{CODE}

TextRenderer.cpp:
{CODE(wrap="1", colors="c++")}#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", "16");
    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);
}{CODE}

For this code to work, you need a font def that defines MyFont. Something like this:
{CODE(wrap="1", colors="c++")}MyFont
{
    type         truetype
    source         arial.ttf
    size         16
    resolution     72
}{CODE}
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).
{CODE(wrap="1", colors="c++")}new TextRenderer();{CODE}

To create a text box, you would write something like:
{CODE(wrap="1", colors="c++")}TextRenderer::getSingleton().addTextBox("txtGreeting", "Hello", 10, 10, 100, 20, Ogre::ColourValue::Green);{CODE}

Then, if you want to update the text:
{CODE(wrap="1", colors="c++")}TextRenderer::getSingleton().setText("txtGreeting", "Goodbye");{CODE}

''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.
{CODE(wrap="1", colors="c++")}#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);
}{CODE}
---
Alias: (alias(Simple_Text_Output))
        

History

Information Version
Thu 07 of Aug, 2014 20:20 GMT-0000 fd9_ 6
Sun 02 of Jan, 2011 02:02 GMT-0000 van4dium 5
Wed 30 of Jun, 2010 21:13 GMT-0000 JustBoo 4
Sat 02 of Jan, 2010 21:00 GMT-0000 jacmoe 3
Fri 25 of Dec, 2009 13:53 GMT-0000 jacmoe 2
Sun 24 of May, 2009 11:28 GMT-0000 SpaXe 1