A game debug console - help

Started by
2 comments, last by Vulcan 21 years, 2 months ago
I have this little game debug console thing. It all works nice and fins and everything, I just want to simplyfy the syntax for it. The way I have it now, you can just spit messages out to the console, like so sConsole() << "here is some message"; sConsole().End(); The End() method adds the constructed string finally to the console, and prepares for another msg, etc. what I want to be able to do is sConsole() << "here is some message" << End(); But I cannot figure out for the life of me the syntax to allow this to work. Any help? btw - I dont just have an ''End'' function, I have other functions such as changing the current color of the text, etc... -Vulcan
Advertisement
Simple.
You need to overload the ''<<'' operator - check google for C++ operator overloading. A suggestion of mine would be the following:


  // struct that is used to set the colorstruct SetColor{    int mColor;  // actual color value    // constructor that initializes member     SetColor(const int nColor) : mColor(nColor)    {}    // convert to integer    const operator int() const    {        return mColor;    }};// just an empty typestruct End {};class Console{     // other stuff here     void SetColor(cont int nColor)     {         // change our text color for output here     }     // text output     Console& operator << (const string& strText)     {         // call your printing function here         return *this;     }     // end output     Console& operator << (const End& sEnd)     {         return (*this << "\n"; // or whatever you use to end     }          // change output color     Console& operator << (const SetColor& sColor)     {         SetTextColor(sColor);         return *this;     }};// usage exampleConsole sColor;sColor << "This is a " << SetColor(GREEN) << "green" << SetColor(GREY) << " text" << End();  


Just my 0.02€,
Pat
Ohhhhhhh I see now.... so the End() would not actually be a method but a struct/class called End but then overloaded by the Console so simply end the line?

thanks much for the help, i was thinking it would be more of a method than overloading for a constructed object

thx
-Vulcan

[edited by - Vulcan on January 29, 2003 9:17:15 PM]
Actually, I was gonna post my SDL based pluggable console here in a bit, but what I did for it was this:


  class SDL_Console{public:  // some stuff  // Stream operators  enum OpFlag   {    Flush = 0x01,    Exec = 0x02,    Term = Flush | Exec  };    SDL_Console& operator<<(OpFlag op);  // some other stuff};SDL_Console& SDL_Console::operator<<(SDL_Console::OpFlag of){  if(of & Exec)    Execute(m_stringBuffer.str());  if(of & Flush)  {    Out(m_stringBuffer.str());    m_stringBuffer.str("");  }  return *this;}  


That way, you can do things like:


  con << "echo here" << SDL_Console::Term;// output: "here"con << "echo this" << SDL_Console::Exec << " is a test" << Flush;// output: this// output: echo this is a test  


I hope that makes sense.
daerid@gmail.com

This topic is closed to new replies.

Advertisement