# Is sprintf'ing a D3DXVECTOR3 possible?

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

## 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 on other sites
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 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 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! :)

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 13
• 14
• 9
• ### Forum Statistics

• Total Topics
633365
• Total Posts
3011520
• ### Who's Online (See full list)

There are no registered users currently online

×