printing some stuff on screen?

Started by
12 comments, last by ApochPiQ 17 years ago
Eek. I looked up the DXU code that you're using, and frankly it's a bit crap. I know it's a big change, but if you can, you might be better off getting rid of it and using DirectX directly. (Alternatively, depending on what you're working on, you might find other libraries like SDL more appropriate.)

That's a bit more major than switching to consistent std::string usage, so I totally understand if you'd rather not do that [smile]

Here's what I'd recommend; just change your DXUDrawText calls to this DrawText function:

void DrawText(LPDXUFONT font, const std::string& text){  size_t buffersize = text.length() + 1;  char* buffer = new char[buffersize];  std::copy(text.c_str(), text.c_str() + buffersize, buffer);  // Pray that DXU doesn't throw exceptions.  // If it does, see version 2 below.  DXUDrawText(font, buffer);  delete [] buffer;}// You should only need to use this version if you KNOW that the// DXU library throws C++ exceptions. I don't think it does, so// the above version should be OK.void DrawText(LPDXUFONT font, const std::string& text){  size_t buffersize = text.length() + 1;  char* buffer = new char[buffersize];  std::copy(text.c_str(), text.c_str() + buffersize, buffer);  try  {    DXUDrawText(font, buffer);  }  catch(...)  {    delete [] buffer;    throw;  }  delete [] buffer;}



The basic problem is that the DXU font drawing code is asking for a C-string buffer that it can modify. I don't know for sure if it actually will modify the string, but I doubt it; in any case, better safe than sorry.

The trouble is, when we have a std::string and want to get a C-string from it, we get a C-string which isn't safe to modify. This is denoted by the fact that the value returned by c_str() is a const.

So we have a string we can't modify, but we have to give it to a function that demands permission to modify it. We're stuck.

(For the record, this is part of why I say the DXU code is garbage; the functions should take const strings at the very least, which would be denoted by the shortcut LPCTSTR rather than LPSTR. The DXU code is riddled with such inconsistencies and poor practices.)

The solution is to create a temporary copy of the string, in a special buffer that we have complete control over. That way, it's OK if the DXU code changes the string. Then, once the DXU code is done doing its thing, we clean up that temporary copy, and move on.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Advertisement
i find the dxu library really easy to use compared to other direct x wrappers but yeah it isn't very well done with some things :), thanks again mate, your really helpfull :)
Quote:
// You should only need to use this version if you KNOW that the// DXU library throws C++ exceptions. I don't think it does, so// the above version should be OK.void DrawText(LPDXUFONT font, const std::string& text){  size_t buffersize = text.length() + 1;  char* buffer = new char[buffersize];  std::copy(text.c_str(), text.c_str() + buffersize, buffer);  try  {    DXUDrawText(font, buffer);  }  catch(...)  {    delete [] buffer;    throw;  }  delete [] buffer;}


Ugh. Instead:

void DrawText(LPDXUFONT font, const std::string& text){  std::vector<char> buffer(text.c_str(), text.c_str() + text.length() + 1);  DXUDrawText(font, &buffer[0]);}
Hmm, much cleaner. Now I need to figure out why I didn't think of that first [smile]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement