Skip to main content
GameDev.net gamedev.net
🔒 Locked

formatted text?

Started by MB Mar 21, 2003 at 3:17 AM 3 replies 300+ views
Original Post
MB
MB
I am wondering if it is possible to extend the CD3DFont class - so that it displays formatted text. If not - what alternative is there?
http://www.superfluxus.de/phpBB2
niyaw
niyaw
yes.

alternative is preformatting text.
MB
MB
sorry for asking stupid questions:
But how would you preformat text?
http://www.superfluxus.de/phpBB2
niyaw
niyaw
CString text;
text.Format("%s is equal to %d", variable, value);
pfont->DrawText(text);

or you can use snprintf.
dorix
dorix
Add this function to the CD3DFont class (or derive your own class from CD3DFont, and add this to the new class), and make sure that the #include directives are in the source file:

  
#include <stdarg.h>

#include <tchar.h>

#include <stdio.h>

HRESULT CD3DFont::DrawTextF(FLOAT x, FLOAT y, DWORD dwColor, TCHAR* strText, DWORD dwFlags, ...)
{
TCHAR buf[1024];
va_list marker;
va_start(marker, dwFlags);
_vsntprintf(buf, 1023, strText, marker);
va_end(marker);

return DrawText(x, y, dwColor, buf, dwFlags);
)

Notes:

  • The dwFlags argument is no longer optional.
  • The stdarg.h header is required for the va_* macros. The tchar.h header is required for the _vsntprintf macro. The stdio.h header is required for _vsnprintf and _vsnwprintf, which are what _vsntprintf expands to, depending on whether you're building a Unicode application or not.
  • Change the 1024 and 1023 to change the maximum length of the completely formatted string. Note that it is *very* difficult to know beforehand how large the formatted string will be, so it's easiest to just pick a size you can reasonably expect to be large enough.
  • A formatting version of the DrawTextScaled function will be left as an exercise to the reader, but it will look very similar to the DrawTextF function above.


- dorix


[edited by - dorix on March 21, 2003 12:16:53 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.