how to display camerapos.x on the IGUIStaticText ?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
allked
Posts: 12
Joined: Mon Oct 02, 2006 3:44 am

how to display camerapos.x on the IGUIStaticText ?

Post by allked »

i use smgr->GetActiveCamera()->getPosition().x as orign data, but how to set that variable to an statictext control? tst->setText(smgr->GetActiveCamera()->getPosition().x), i got errors; thanks
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

You aren't the first person to ask this, and I'm not the first to say this in response. You should really spend some time getting to know the C/C++ language. Maybe pick up a book or two on the language and do some reading. Don't take this the wrong way, I don't want to discourage you. It is just frustrating to see questions like this. And here is the long answer...

The function ISceneNode::getPosition() returns a core::vector3df that is made up of three members [x, y, z] that are all of type f32 [float]. So, when you write smgr->getActiveCamera()->getPosition().x, the type of that expression is f32. The function IGUIElement::setText(const wchar_t*) takes a wchar_t string as a parameter.

So, when you call tst->setText(smgr->getActiveCamera()->getPosition().x), you are passing a f32 where the compiler is expecting a const wchar_t*. As you can probably imagine, a floating point number and a wide character string are completely unrelated types. That is why it doesn't compile. The compiler thinks you have no idea what you're doing.

Well, what to do? You convert the f32 to a wchar_t* and you pass that to the function. How? Well, you can use the core::string template to help you out.

Code: Select all

core::stringw s(smgr->getActiveCamera()->getPosition().x);
tst->setText(s.c_str());
That works because the core::stringw class has a constructor that takes a double. The f32 type is implicitly convertable to double, so the compiler says that is okay. So the string constructor creates a string from a f32. Then we access the raw const wchar_t* from the string by calling s.c_str(). That is all fine and dandy.

If you want to display more than one variable in a string? Well, you can append strings.

Code: Select all

core::vector3df pos = smgr->getActiveCamera()->getPosition();

core::stringw s;
s += " x=";
s += core::stringw(pos.x);
s += " y=";
s += core::stringw(pos.y);
s += " z=";
s += core::stringw(pos.z);
That would create a string that looked like x=1.154000 y=123.190012 z=77.432185.

You could also do the same thing with the fomatted output function swprintf.

Code: Select all

wchar_t s[64];
swprintf(s, L"x=%0f y=%0f z=%f", pos.x, pos.y, pos.z);
And that concludes our lesson for the day. :)

Travis
Post Reply