#include <stdio.h>
#include "tscreen.h"

static char colorBuffer[768];
static TScreen* thisScreen;

static T_INT tMouseCallback( PARAMETER *parameter)
{
  static int mx=0, my=0, mb=0;
  int x, y, b;
  x = vesa_mouse_x;
  y = vesa_mouse_y;
  b = vesa_mouse_button;

  if( ( x != mx) || ( y != my ) || ( mb != b ) )
    thisScreen->mouseCallback();

  mx = x;
  my = y;
  mb = b;

  return T_OK;
}

static T_INT tKeyCallback( PARAMETER *parameter )
{
  thisScreen->keyCallback( parameter->taste );
  return T_OK;
}

TScreen::TScreen()
{
  error = 0;
  init_error(); // fehler zeug
  init_tools(); // tool initialisieren
  if( tool_error == TOOL_INIT_ERROR )
  {
    tool_exit(TOOL_INIT_ERROR);
    printf("\nKonnte Tool-Library aus irgendeinem Grund nicht initialisieren...\n");
    error = 1;
  }
  xRes = 0;
  yRes = 0;
}

TScreen::~TScreen()
{
 close();
 de_init_tools();
}

int TScreen::isError()
{
  return error;
}

int TScreen::start()
{
  thisScreen = this;

  default_tasten_funktion = tKeyCallback;
  default_area_funktion = tMouseCallback;

  PROGRAMM_INIT;
  return 0;
}

int TScreen::open()
{
  vesa_alten_modus_speichern();
  if (vesa_setze_modus(SVGA640X480)!=T_OK)
  {
    printf("\nKonnte Video mode nicht setzen...\n");
    return 2;
  }
  xRes = 640;
  yRes = 480;

  vesa_load_mehrere_dac(224, (T_PCHAR) "tool.dat\\16colors.col"); // 16 farben laden...
  init_colors(); // und diese in die GUI initialisieren
  vesa_mouse_init();
  setFgc( weiss );
  setBgc( schwarz );
  fontHeight = font_now->zeichen[' '].hoehe;
  fontWidth  = font_now->zeichen[' '].breite;
  white = weiss;
  black = schwarz;
  red   = f_red;
  grey  = dhell_grau;
  return 0;
}

/*
int TScreen::reopen()
{
 vesa_set_mehrere_dac( 0, 256, (T_PCHAR) colorBuffer );
 show_all_windows();
 back_show();
 return 0;
}
*/

int TScreen::close()
{
 vesa_get_mehrere_dac( 0, 256, (T_PCHAR) colorBuffer ); // speichern der Farben fuer reopen
 hide_all_windows();
 back_save( FORCED_SAVE );
 vesa_mouse_deinit();
 vesa_alten_modus_setzen();
 return 0;
}

int TScreen::mouseX( void )             // Maus X-Position holen
{
  return vesa_mouse_x;
}

int TScreen::mouseX( int x )  // Maus X-Position setzen und holen
{
  int h = vesa_mouse_x;
  vesa_mouse_setze_cursor( x, vesa_mouse_y );
  return h;
}

int TScreen::mouseY( void ) // Maus Y-Position holen
{
  return vesa_mouse_y;
}

int TScreen::mouseY( int y ) // Maus Y-Position setzen und holen
{
  int h = vesa_mouse_y;
  vesa_mouse_setze_cursor( vesa_mouse_x, y );
  return h;
}

int TScreen::mouseButton( void ) // Maus Button abfragen
{
  switch( vesa_mouse_button )
  {
    case 1:
         return MMOUSE_LEFTBUTTON;
    case 2:
         return MMOUSE_RIGHTBUTTON;
    case 3:
         return MMOUSE_MIDDLEBUTTON;
  }
  return MMOUSE_UNKNOWNBUTTON;
}
int TScreen::getKey( void ) // liefert die gedrueckte Taste
{
  return fKBDishit();
}
int TScreen::getChar( void ) // liefert die gedrueckte Taste
{
  return fKBDgetch();
}

int TScreen::drawPoint( int x, int y, int c )
{
 if( c == -1 )
     c = fgc;
 vesa_plot256( (T_UWORD)x,(T_UWORD) y, (T_UWORD) c ); // not anti alliased
 return 0;
}

int TScreen::drawLine( int x1, int y1, int x2, int y2, int c )
{
 if( c == -1 )
   c = fgc;
 vesa_draw_line( (T_SWORD)x1, (T_SWORD)y1, (T_SWORD)x2, (T_SWORD)y2, (T_SWORD)c); // not anti alliased
 return 0;
}

void TScreen::drawCircle( int x, int y, int radius, Color c )
{
  vesa_circle( x, y, radius, c );
}

int TScreen::clearPoint( int x, int y )
{
 vesa_plot256( (T_UWORD)x, (T_UWORD)y, (T_UWORD)bgc ); // not anti alliased
 return 0;
}

Color TScreen::getPoint( int x, int y )
{
  return vesa_get_pixel( (T_UWORD)x, (T_UWORD)y );
}

int TScreen::print( int x, int y, char* text )
{
 T_INT old_print_modus;
 T_INT old_text;
 T_INT old_back_color;
 T_INT old_text_color;
 old_print_modus=vesa_print_modus;
 old_text=vesa_text;
 old_back_color=vesa_back_color;
 old_text_color=vesa_text_color;
 vesa_print_modus=PIC_FILLED;
 vesa_text=SINGLE_COLOR;
 vesa_back_color=bgc;
 vesa_text_color=fgc;

  vesa_print_str( x, y , ( T_PCHAR ) text );

 vesa_print_modus=old_print_modus;
 vesa_text=old_text;
 vesa_back_color=old_back_color;
 vesa_text_color=old_text_color;
  return 0;
}

int TScreen::print( int x, int y, char c )
{
 T_INT old_print_modus;
 T_INT old_text;
 T_INT old_back_color;
 T_INT old_text_color;
 old_print_modus=vesa_print_modus;
 old_text=vesa_text;
 old_back_color=vesa_back_color;
 old_text_color=vesa_text_color;
 vesa_print_modus=PIC_FILLED;
 vesa_text=SINGLE_COLOR;
 vesa_back_color=bgc;
 vesa_text_color=fgc;

  vesa_print_char( x, y , c );

 vesa_print_modus=old_print_modus;
 vesa_text=old_text;
 vesa_back_color=old_back_color;
 vesa_text_color=old_text_color;
  return 0;
}

void TScreen::mouseOn( void )
{
  MOUSE_ZEIGE;
}
void TScreen::mouseOff( void )
{
  MOUSE_VERSTECKE;
}

void TScreen::clearScreen( void )
{
  clear_screen();
}

AreaBuffer* TScreen::getArea( int x, int y, int width, int height )
{
  return lade_vga_nach_mem( x, y, width, height, ALL_MEM_OK );
}

int TScreen::putArea( int x, int y, AreaBuffer* buffer )
{
  return print_mem_bild( buffer, x, y );
}

int TScreen::freeArea( AreaBuffer* buffer )
{
  return free_mem_bild( buffer );
}

int TScreen::setArea( int x, int y, int width, int height, Color c )
{
  vesa_rect_fill( x, y, width, height, (T_BYTE) c );
  return 0;
}

AreaBuffer* TScreen::cloneArea( AreaBuffer* area )
{
  return NULL;
}
