• Advertisement
Sign in to follow this  

Is sprintf'ing a D3DXVECTOR3 possible?

This topic is 3898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying to output/convert a D3DXVECTOR3 to something I can render in a font, and I think I'm trying to do something like: sprintf( m_cameraPos, "%d, %d, %d, Campos", m_camera->GetTranslation ); But I'm a little lost and I can't exactly figure it out. Is there a way to extract the individual components of a D3DXVECTOR3, or do I have to like, cast it to something more native to a sprintf function? In the past, I've just kept extra x, y and z floats embedded in the objects but if there's an easier or more simple way, I'd really appreciate the advice. Thanks a bunch!

Share this post


Link to post
Share on other sites
Advertisement
If you look at the definition of D3DXVECTOR3, you can see that you can just use x, y, and z fields from the vector.
So,
D3DXVECTOR3 const &t = m_camera->GetTranslation;
sprintf( m_cameraPos, "%.2f, %.2f, %.2f, Campos", t.x, t.y, t.z );
should just work.

Share this post


Link to post
Share on other sites
sprintf cannot be extended to support more types, which is one of the reasons why C++ streams are a superior solution.

In your particular case, it would be as simple as:
return boost::lexical_cast<std::string>(m_camera->GetTranslation)


Or perhaps:
std::strintstream stream;
stream << m_camera->GetTranslation;
return stream.str();


As long as you've defined operator<< for the vector:
std::ostream& operator<<(std::ostream& out, const D3DXVECTOR3 & v)
{ return out << v.x << " " << v.y << " " << v.z; }


Share this post


Link to post
Share on other sites
D3DXVECTOR3 const &t = m_camera->GetTranslation;
sprintf( m_cameraPos, "%.2f, %.2f, %.2f, Campos", t.x, t.y, t.z );

Works beautifully! Like magic. Thanks, that's been bugging me for a very long time. And thanks for the other stuff too, I'll be sure to look into streams! :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement