How do you printf?

Problems building or running the engine, queries about how to use features etc.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
getli
Gnoblar
Posts: 19
Joined: Sun Oct 03, 2004 2:59 pm

Post by getli »

I've written a small class where you can use normal printf syntax to print anywhere on screen e.g. gScreenText.print(10, 10, "blabla %d", 5); It's more flexible than the DebugText thing as you can print anywhere on screen. Also it allows you to start a print at a certain coordinate and then just continue printing like a normal printf

Here's the header file:

Code: Select all

#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
and here the cpp file

Code: Select all

#include "stdafx.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);
}
User avatar
pjcast
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2543
Joined: Fri Oct 24, 2003 2:53 am
Location: San Diego, Ca
x 2
Contact:

Post by pjcast »

That's created by default in windows project, relating to precompiled headers. Just delete that header line, and you should be ok ;)
Have a question about Input? Video? WGE? Come on over... http://www.wreckedgames.com/forum/
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: ok..

Post by jacmoe »

ryandebraal wrote:looks good, but it says it can't find
stdafx.h
Remove it. That was just a test to see if you were using the code. Which you are.:wink:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

screentext.h is the header:
#ifndef SCREEN_TEXT_H
#define SCREEN_TEXT_H
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

OMG.

I don't know what to say.

Did you follow instructions on the wiki for setting up your compiler?
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

If you want to use std::string, put #include <string> in your file.
And so on.

<edit>
Your VC directories looks fine.
</edit>
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

Add <string> and <vector> to the header file.
I haven't looked at the rest of the code, but getli can probably guide you the rest of the way, should you have problems with it.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

Add <ogre.h> to it as well. :wink:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

That's what I suspected.

Will the addition of a using namespace Ogre; do any good?
Add it to the header, just after the includes.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

When you say 'nope, nothing changed', I don't believe you.
Maybe you still have errors, but different errors.

Look at the linenumbers, find the line where it errors out: what is VC not able to find?
If it is an Ogre::Overlay, you need to tell it where Ogre::Overlay is.
And so on.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
Post Reply