OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username:
Password:
CapsLock is on.
Remember me (for 1 year)
Log in
Home
Tutorials
Tutorials Home
Basic Tutorials
Intermediate Tutorials
Mad Marx Tutorials
In Depth Tutorials
Older Tutorials
External Tutorials
Cookbook
Cookbook Home
CodeBank
Snippets
Experiences
Ogre Articles
Libraries
Libraries Home
Alternative Languages
Assembling A Toolset
Development Tools
OGRE Libraries
List of Libraries
Tools
Tools Home
DCC Tools
DCC Tutorials
DCC Articles
DCC Resources
Assembling a production pipeline
Development
Development Home
Roadmap
Building Ogre
Installing the Ogre SDK
Setting Up An Application
Ogre Wiki Tutorial Framework
Frequently Asked Questions
Google Summer Of Code
Help Requested
Ogre Core Articles
Community
Community Home
Projects Using Ogre
Recommended Reading
Contractors
Wiki
Immediate Wiki Tasklist
Wiki Ideas
Wiki Guidelines
Article Writing Guidelines
Wiki Styles
Wiki Page Tracker
Ogre Wiki Help
Ogre Wiki Help Overview
Help - Basic Syntax
Help - Images
Help - Pages and Structures
Help - Wiki Plugins
Toolbox
Freetags
Categories
List Pages
Structures
Trackers
Statistics
Rankings
List Galleries
Ogre Lexicon
Comments
History: Simple text
View page
Source of version: 6
(current)
!!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. {CODE(wrap="1", colors="c++")}//----------------------------------------------------------------------------- // 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 {CODE} !!Usage Using this class is as easy as doing the following; {CODE(wrap="1", colors="c++")}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){CODE} Don't forget to delete your text item after use to avoid the dreaded memory leak, like this: {CODE(wrap="1", colors="c++")}delete textItem;{CODE} !!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__. {img src="img/wiki_up/140.gif" alt="140.gif"} !!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: (alias(Simple_text))
Search by Tags
Search Wiki by Freetags
Latest Changes
Random Particle Texture (HLSL)
Explosion
Using SDL Input
MovableText
-GLSL
HLSL
User:Nauk
Architecture and Design in Games
Terrain Howto
Intermediate Tutorial 4
...more
Search
Find
Advanced
Search Help
Online Users
74 online users