Public Group

# Old Code?

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

## Recommended Posts

USING Cpp/OpenGL/SDL/VCpp 2008exp I've just integrated SDL into my old OpenGL code and have started looking at my font routine. This seems a bit out-of-date? Straight away I can see it needs to be updated to use strings. Can anyone suggest any new way of updating the following code?
void Font::Print(char *format, ...)
{
if(format == NULL)
return;

char text[256];

va_list args;

va_start(args, format);
vsprintf_s(text, 256, format, args);
va_end(args);

glPushAttrib(GL_LIST_BIT);
glListBase(base - 32);
glCallLists(text.length(), GL_UNSIGNED_BYTE, text);
glPopAttrib();
}



##### Share on other sites
Well if you are using a va_list and OpenGL's glCallLists then these both require the use of C strings, so the only thing extra you will be doing with std::strings is converting them using .c_str() when calling the functions.
I use pretty much the same code as you (and with std::strings) and i don't think there is anything wrong with it.

##### Share on other sites
Code that uses printf-style formatting is very tricky, subtle stuff. It can cause a lot of bugs with type safety, and we don't like that in C++.

If you really want to preserve that kind of formatting for the calling code, check out boost::format. But if you just want to be able to build up the output text easily, and you don't mind changing the calling code a little, check this out:

class Text {  std::stringstream ss;  Font* owner;  Text(const Text&);  Text& operator=(const Text&);  public:  Text(const Font& f): ss(), owner(&f) {}  template <typename T>  Text& operator()(const T& t) { ss << t; return *this; }  void Print() {    owner->Print(ss.str());  }}void Font::Print(const std::string& s) {  glPushAttrib(GL_LIST_BIT);  glListBase(base - 32);  glCallLists(text.size(), GL_UNSIGNED_BYTE, s.c_str());  glPopAttrib();}// Text(myFont)("hi mom")(42.3f)(foo).Print();

• 23
• 10
• 19
• 15
• 14