#ifndef CWINSTREAM_H
#define CWINSTREAM_H

////////////////////////////////////////////////////////
//                                                    //
//  CWinStream                                        //
//                                                    //
//  This class is meant to have one instance, wout,   //
//  whose behavior should be quite the same as the    //
//  cout object, but in a GLUT window application.    //
//                                                    //
////////////////////////////////////////////////////////

class CWinStream : public std::ostringstream
{
 protected:
  void * Font[7];
  int Height[7];
  int ScreenWidth, ScreenHeight;

 public:
  int TheFont;
  int x, y;
  float Color[4];
  float Spacing;


  // Constructor
  CWinStream()
    {
      x = 0; y = 0;

      Font[0] = GLUT_BITMAP_9_BY_15;
      Font[1] = GLUT_BITMAP_8_BY_13;
      Font[2] = GLUT_BITMAP_TIMES_ROMAN_10;
      Font[3] = GLUT_BITMAP_TIMES_ROMAN_24;
      Font[4] = GLUT_BITMAP_HELVETICA_10;
      Font[5] = GLUT_BITMAP_HELVETICA_12;
      Font[6] = GLUT_BITMAP_HELVETICA_18;

      Height[0] = 15;
      Height[1] = 13;
      Height[2] = 10;
      Height[3] = 24;
      Height[4] = 10;
      Height[5] = 12;
      Height[6] = 18;

      ScreenWidth = 100;
      ScreenHeight = 100;

      Color[0] = 1;
      Color[1] = 1;
      Color[2] = 1;
      Color[3] = 1;

      TheFont = 0;

      Spacing = 1.0f;
    }

  inline void SetPos(int xx, int yy)
    {
      x = xx; y = yy;
      glColor4fv(Color);
      glRasterPos2i(x, y);
    }
  inline void SetColor(float r, float g, float b, float a)
    {
      Color[0] = r;
      Color[1] = g;
      Color[2] = b;
      Color[3] = a;
      glColor4fv(Color);
    }
  inline int Length(const unsigned char * String) const
    { return glutBitmapLength(Font[TheFont], String); }



  // INITIALISATION FUNCTIONS
  inline void Begin(void)
    {
      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      glLoadIdentity();
      glMatrixMode(GL_PROJECTION);
      glPushMatrix();
      glLoadIdentity();
      glOrtho(0, ScreenWidth, 0, ScreenHeight, -2, 2);
      glColor4fv(Color);
      glRasterPos2i(x, y);
    }
  inline void End(void)
    {
      flush();
      glMatrixMode(GL_PROJECTION);
      glPopMatrix();
      glMatrixMode(GL_MODELVIEW);
      glPopMatrix();
    }
  inline void SetToWindow(void)
    {
      ScreenWidth = glutGet(GLUT_WINDOW_WIDTH);
      ScreenHeight = glutGet(GLUT_WINDOW_HEIGHT);
    }

  // flush
  void flush(void);
};


// flush
inline void CWinStream::flush(void)
{
  put(0);
  std::string String = str();

  for (unsigned int i = 0; i < String.size(); i++)
    {
      if (String[i] == '\n')
        SetPos(x, y - Spacing * Height[TheFont]);
      else
        glutBitmapCharacter(Font[TheFont], String[i]);
    }

  str("");
}


extern CWinStream wout;



#endif
