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 Output
View page
Source of version: 6
(current)
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); const std::string& getText(const std::string& ID); };{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", "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(); }{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))
Search by Tags
Search Wiki by Freetags
Latest Changes
Ogre 2.1 FAQ
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
MogreSocks
Critter AI
Mogre Add-ons
MOGRE
Mogre MyGUI wrapper
...more
Search
Find
Advanced
Search Help
Online Users
78 online users