/*
 * PVVMUD a 3D MUD
 * Copyright (C) 1998-1999  Programvareverkstedet (pvv@pvv.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <iostream.h>
#include "pvvmud.H"
#include "glconsole.H"
#include "pvvmudclient.H"

GLubyte CGLConsole::m_cursor[13] = {
    0x00, 0x00, 0x00,
    0x00, 0x00, 0x00,
    0x00, 0x00, 0x00,
    0x00, 0x00, 0xff,
    0xff};



CGLConsole::CGLConsole(CPvvmudClient * cli) : CConsole(cli) {
  m_font = GLUT_BITMAP_8_BY_13;
}

CGLConsole::CGLConsole(CPvvmudClient * cli, int rows, int cols) : CConsole(cli, rows, cols) {
  m_font = GLUT_BITMAP_8_BY_13;  
  setCursorPos(1, 10);
}
CGLConsole::~CGLConsole() {
}

CGLConsole::drawString(char *string) {
  int len,t;
  t = 0;
  len = getRows();
  while (t < len && string[t] != '\n') {
    if (string[t] != 0) {
      glutBitmapCharacter(m_font,string[t]);
    }
    t++;
  } // while
  //cout << '\n';
}

void CGLConsole::drawText() {
  int x, y;
  //  Kode for � tegna opp tekstkonsoll
  glDisable(GL_DEPTH_TEST);
  glDisable(GL_TEXTURE_2D);
  glDisable(GL_LIGHTING);
  // Darken background
  
  glLoadIdentity();
  GLdouble dark[4] = {0.0, 0.0, 0.0, 0.5};
  //glRotatef(-90, 1.0, 0.0, 0.0);
  gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0);
  //CCliViewpoint * viewpoint = getClient()->getManager()->getViewpoint();
  //viewpoint->transform();
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glShadeModel(GL_FLAT);
  glBegin(GL_QUADS);
  glColor4dv(dark);
  glVertex3f(-300, -300, 0.5);
  glVertex3f(-300, 300, 0.5);
  glVertex3f(300, 300, 0.5);
  glVertex3f(300, -300, 0.5);
  glEnd();
  glDisable(GL_BLEND);
  glShadeModel(GL_SMOOTH);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-0.5,getClient()->getGUI()->getWidth() ,-0.5,getClient()->getGUI()->getHeight(),-1.0,1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glColor3dv(m_charColor);
  x = m_screen_x;
  y = m_screen_y;
  for (int i=0;i<getCols();i++) {
    glRasterPos2i(x, y);
    drawString(&m_console[i*getRows()]);
    y-=13;
  } // for
  //  Draw cursor
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  if (getInputMode()) {
    glRasterPos2i(m_screen_x + (8*m_input_x), m_screen_y - (13*(m_input_y+1)));
    glBitmap(8, 13, 0.0, 0.0, 0.0, 0.0, m_cursor);
  } 
}