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: Printf with ogre
View page
Source of version: 4
(current)
!!!Printf with Ogre getli has written a nice class that lets you use printf with ogre. Its been put up by grizzley90 with some small modifications to the code so it can run anywhere. This is the original thread: [http://www.ogre3d.org/phpBB2/viewtopic.php?t=22457] . This is the header file (screentext.h): {CODE(wrap="1", colors="c++")} #ifndef SCREEN_TEXT_H #define SCREEN_TEXT_H namespace Ogre { class TextAreaOverlayElement; } class ScreenText { public: ScreenText(); void init(); // Print at <x/y> void print(float x, float y, const char*, ...); void print(float x, float y, float size, const char*, ...); // Begin printing at a certain coordinate void beginPrint(float x, float y, float size); // Continue printing (started with beginPrint). Each print will start in a new line. void print(const char*, ...); // Call this at the start of each frame void frameStarted(); // Call this at the end of each frame before it is rendered! void update(); private: struct Entry { Entry() { mX = 0; mY = 0; mSize = 1.0f; } float mX; float mY; float mSize; std::string mString; }; typedef std::vector<Entry> Strings; Strings mStrings; Overlay* mOverlay; OverlayContainer* mPanel; enum { MAX_TEXT_AREAS = 50 }; TextAreaOverlayElement* mTextAreas[MAX_TEXT_AREAS]; struct Context { Context(); float mX; float mY; float mSize; } mContext; }; extern ScreenText gScreenText; #endif // !SCREEN_TEXT_H {CODE} This is the Source File (screentext.cpp): {CODE(wrap="1", colors="c++")} #include <tchar.h> #include <assert.h> #include <stdlib.h> #include <iostream> #include <vector> #include <string> #include "Ogre.h" using namespace Ogre; #define WIN32_LEAN_AND_MEAN #define _WIN32_WINNT 0x0501 #include <windows.h> #include "ScreenText.h" #include "OgreTextAreaOverlayElement.h" static float DEFAULT_SIZE = 0.03f; ScreenText gScreenText; //------------------------------------------------------------------------------ ScreenText::ScreenText() //------------------------------------------------------------------------------ { mPanel = 0; mOverlay = 0; memset(mTextAreas, 0, MAX_TEXT_AREAS*sizeof(TextAreaOverlayElement*)); } //------------------------------------------------------------------------------ void ScreenText::init() //------------------------------------------------------------------------------ { // Create a panel mPanel = static_cast<OverlayContainer*>( OverlayManager::getSingleton().createOverlayElement("Panel", "PanelName")); mPanel->setMetricsMode(Ogre::GMM_RELATIVE); mPanel->setPosition(0, 0); mPanel->setDimensions(1.0f, 1.0f); //panel->setMaterialName("MaterialName"); // Optional background material for (int i = 0; i<MAX_TEXT_AREAS; ++i) { mTextAreas[i] = static_cast<TextAreaOverlayElement*>( OverlayManager::getSingleton().createOverlayElement("TextArea", StringConverter::toString(i) + "TextAreaName")); mTextAreas[i]->setMetricsMode(Ogre::GMM_RELATIVE); mTextAreas[i]->setFontName("BlueHighway"); mTextAreas[i]->setColourBottom(ColourValue(1,1,1)); mTextAreas[i]->setColourTop(ColourValue(1,1,1)); mTextAreas[i]->setDimensions(1.0f, 1.0f); mPanel->addChild(mTextAreas[i]); } mOverlay = OverlayManager::getSingleton().create("ScreenText"); mOverlay->add2D(mPanel); // Show the overlay mOverlay->show(); } //------------------------------------------------------------------------------ void ScreenText::frameStarted() //------------------------------------------------------------------------------ { mStrings.clear(); for (int i = 0; i<MAX_TEXT_AREAS; ++i) { mTextAreas[i]->hide(); } } //------------------------------------------------------------------------------ void ScreenText::update() //------------------------------------------------------------------------------ { int count = 0; for (Strings::iterator i = mStrings.begin(); i!=mStrings.end() && count < MAX_TEXT_AREAS; ++i) { Entry& entry = *i; mTextAreas[count]->setPosition(entry.mX, entry.mY); mTextAreas[count]->setCaption(entry.mString.c_str()); mTextAreas[count]->setCharHeight(DEFAULT_SIZE*entry.mSize); mTextAreas[count]->show(); ++count; assert(count < MAX_TEXT_AREAS); } } namespace { //------------------------------------------------------------------------------ const char* vprint(const char* format, va_list args) //------------------------------------------------------------------------------ { static const int BUFFER_SIZE = 200; static char buffer[BUFFER_SIZE]; int size = _vscprintf(format, args); assert(size < BUFFER_SIZE); vsnprintf(buffer, BUFFER_SIZE, format, args); return buffer; } } //------------------------------------------------------------------------------ void ScreenText::print(float x, float y, const char* format, ...) //------------------------------------------------------------------------------ { Entry entry; entry.mX = x; entry.mY = y; va_list args; va_start( args, format ); entry.mString = vprint(format, args); va_end( args ); mStrings.push_back(entry); } //------------------------------------------------------------------------------ void ScreenText::print(float x, float y, float size, const char* format, ...) //------------------------------------------------------------------------------ { Entry entry; entry.mX = x; entry.mY = y; entry.mSize = size; va_list args; va_start( args, format ); entry.mString = vprint(format, args); va_end( args ); mStrings.push_back(entry); } //------------------------------------------------------------------------------ ScreenText::Context::Context() //------------------------------------------------------------------------------ { mX = 0.0f; mY = 0.0f; mSize = 1.0f; } //------------------------------------------------------------------------------ void ScreenText::beginPrint(float x, float y, float size) //------------------------------------------------------------------------------ { mContext.mX = x; mContext.mY = y; mContext.mSize = size; } //------------------------------------------------------------------------------ void ScreenText::print(const char* format, ...) //------------------------------------------------------------------------------ { Entry entry; entry.mX = mContext.mX; entry.mY = mContext.mY; entry.mSize = mContext.mSize; mContext.mY += mContext.mSize*DEFAULT_SIZE; va_list args; va_start( args, format ); entry.mString = vprint(format, args); va_end( args ); mStrings.push_back(entry); } {CODE} --- Alias: (alias(Printf_with_ogre))
Search by Tags
Search Wiki by Freetags
Latest Changes
FMOD SoundManager
HDRlib
Building Ogre V2 with CMake
Ogre 2.1 FAQ
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
MogreSocks
Critter AI
...more
Search
Find
Advanced
Search Help
Online Users
32 online users