Skip to main content
Printf with ogre         A simple class that lets you use printf with ogre

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):

Copy to clipboard
#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

This is the Source File (screentext.cpp):

Copy to clipboard
#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); }

Alias: Printf_with_ogre