SpriteFont - using variables in the string

Dec 2, 2013 at 11:06 AM
Hi there,

I am using spriteFont to draw strings to the screen, however I cant seem to be able to draw it with a variable as you would be able to in XNA


spriteFont->DrawString(spriteBatch.get(), L"Score: " + score, Vector2(10,10)........

Is this possible to achieve using C++ for windows 8 store applications?

Dec 2, 2013 at 9:16 PM
SpriteFont just takes a wide-character string. You can use any valid C++ method for building such a string.

A reasonably efficient method is to use traditional C printf routines (actually the wide-character, Safer CRT) which are supported in Windows Store apps
    wchar_t buff[1024];
    swprintf_s( buff, L"Score: %d\n", score );
    spriteFont->DrawString( spriteBatch.get(), buff, Vector2(10,10)........ 
You can also use C++ std::wstring operations to compose your value, and then pass the result with .c_str() to DrawString.
    std::wstringstream ss;
    ss << score;

    std::wstring str( L"Score: " );
    str += ss.str();
    spriteFont->DrawString( spriteBatch.get(), str.c_str(), Vector2(10,10)........ 
Marked as answer by walbourn on 12/23/2013 at 11:07 PM