Overview
A common question in the forums is How can I display text quickly and easily?.
Here is a basic class that will display as many text items as you like anywhere on the screen. Just place the following code into a file called OgreText.h and include it into your project.
Copy to clipboard
//----------------------------------------------------------------------------- // Lonewolff // // Filename: OgreText.h // Description: Class for simple text in Ogre (Version 040507:18.30) //----------------------------------------------------------------------------- #include "OgreTextAreaOverlayElement.h" #include "OgreStringConverter.h" using namespace Ogre; #ifndef __OgreText_H__ #define __OgreText_H__ class OgreText { public: OgreText() { olm=OverlayManager::getSingletonPtr(); if(init==0) { panel=static_cast<OverlayContainer*>(olm->createOverlayElement("Panel","GUI")); panel->setMetricsMode(Ogre::GMM_PIXELS); panel->setPosition(0,0); panel->setDimensions(1.0f,1.0f); overlay=olm->create("GUI_OVERLAY"); overlay->add2D(panel); } ++(this->init); szElement="element_"+StringConverter::toString(init); overlay=olm->getByName("GUI_OVERLAY"); panel=static_cast<OverlayContainer*>(olm->getOverlayElement("GUI")); textArea=static_cast<TextAreaOverlayElement*>(olm->createOverlayElement("TextArea",szElement)); panel->addChild(textArea); overlay->show(); } ~OgreText() { szElement="element_"+StringConverter::toString(init); olm->destroyOverlayElement(szElement); --(this->init); if(init==0) { olm->destroyOverlayElement("GUI"); olm->destroy("GUI_OVERLAY"); } } void setText(char *szString) { textArea->setCaption(szString); textArea->setDimensions(1.0f,1.0f); textArea->setMetricsMode(Ogre::GMM_RELATIVE); textArea->setFontName("BlueHighway"); textArea->setCharHeight(0.03f); } void setText(String szString) // now You can use Ogre::String as text { textArea->setCaption(szString); textArea->setDimensions(1.0f,1.0f); textArea->setMetricsMode(Ogre::GMM_RELATIVE); textArea->setFontName("BlueHighway"); textArea->setCharHeight(0.03f); } void setPos(float x,float y) { textArea->setPosition(x,y); } void setCol(float R,float G,float B,float I) { textArea->setColour(Ogre::ColourValue(R,G,B,I)); } private: OverlayManager *olm; OverlayContainer *panel ; Overlay *overlay; TextAreaOverlayElement *textArea; static int init; String szElement; }; int OgreText::init=0; #endif
Usage
Using this class is as easy as doing the following;
Copy to clipboard
OgreText *textItem=new OgreText; textItem->setText("Hello World!"); // Text to be displayed // Now it is possible to use the Ogre::String as parameter too textItem->setPos(0.1f,0.1f); // Text position, using relative co-ordinates textItem->setCol(1.0f,1.0f,1.0f,0.5f); // Text colour (Red, Green, Blue, Alpha)
Don't forget to delete your text item after use to avoid the dreaded memory leak, like this:
Copy to clipboard
delete textItem;
Ogre version compatability
This has been tested on and is known to work on Ogre 1.4.0.
This has been tested on and is known to work on Ogre 1.8.1.
Final word
This is a very basic class for simple text out and fairly limited in function. But it can very easily be expanded to offer greater flexibility. Feel free to modify it to suit your needs.
Alias: Simple_text