#ifdef COMP_COMPILER_SYSTEM

#include "system.h"

// include your headers

FILE* System::SystemFopen (char* filename, char* mode)
{
  // First translate the '/' is the filename to whatever
  // your OS uses as directory seperator.
  ...

  return fopen (new_filename, mode);
}

long System::SystemTime ()
{
  // Return milliseconds.
  // If your system supports ftime use this:
  timeb tp;
  ftime (&tp);
  return tp.time*1000+tp.millitm;

  // Else use some specific function:
  ...

  // You can also use (but this is not accurate):
  return time (NULL)*1000;
}

void System::SetSystemDefaults ()
{
  FRAME_WIDTH = config.get_int ("WIDTH", 320);
  FRAME_HEIGHT = config.get_int ("HEIGHT", 200);
  ...
}

void System::ParseCmdLine (int argc, char* argv[])
{
  int i;

  for (i=1 ; i < argc ; i++)
  {
    if (strcasecmp ("-mode", argv[i]) == 0)
    {
      i++;
      if (i < argc) SetMode (argv[i]);
    }
    else if (...)
    ...
    else ParseStdArg (argc, argv, i);
  }
}

void System::SystemHelp ()
{
  printf ("   -mode <w>x<y>      set resolution (default=%dx%d)\n",
	  FRAME_WIDTH, FRAME_HEIGHT);
  ...
}

// Keyboard fonctions
Graphics::Graphics(int argc, char *argv[])
{
  // Create your graphic interface
  FRAME_WIDTH=320;
  FRAME_HEIGHT=240;

// don't touch, please !
        graphicsData=Memory;
}

Graphics::~Graphics(void)
{
  // Destroy your graphic interface
}

int Graphics::Open(void)
{
  // Open your graphic interface
}

void Graphics::Close(void)
{
  // Close your graphic interface
}

void Graphics::Print(void)
{
  // Print to your graphic interface
}

void Graphics::Clear (int color)
{
  // Clear your graphic interface
  // this is the standard one
  if (Memory) memset (Memory, color, FRAME_WIDTH*FRAME_HEIGHT);
}

void Graphics::SetPixel (int x, int y, int color)
{
  // print a pixel on your graphic interface
  // this is the standard one
  if (Memory)
    {
      if (x < 0 || x >= FRAME_WIDTH) return;
      if (y < 0 || y >= FRAME_HEIGHT) return;
      y = FRAME_HEIGHT-y;
      *(Memory+y*FRAME_WIDTH+x) = color;
    }
}

void Graphics::SetLine (int x1, int y1, int x2, int y2, int color)
{
  // print a line on your graphic interface
  // this is the standard one
  if (Memory)
  {
    if (x1 < 0 || x1 > FRAME_WIDTH) { printf ("*"); return ;}
    if (x2 < 0 || x2 > FRAME_WIDTH) { printf ("*"); return ;}
    if (y1 < 0 || y1 > FRAME_HEIGHT) { printf ("*"); return ;}
    if (y2 < 0 || y2 > FRAME_HEIGHT) { printf ("*"); return ;}

    y2 = FRAME_HEIGHT-y2;
    y1 = FRAME_HEIGHT-y1;
    int i, x, y;
    for (i = 0 ; i < 200 ; i++)
    {
      x = (x2-x1)*i/200+x1;
      y = (y2-y1)*i/200+y1;
      *(Memory+y*FRAME_WIDTH+x) = color;
    }
  }
}

void Graphics::SetHorLine (int x1, int x2, int y, int color)
{
  // print a line with overflow on your graphic interface
  // this is the standard one
  if(Memory)
    {
      y = FRAME_HEIGHT-y;
      char* d = Memory+y*FRAME_WIDTH+x1;
      if (y < 0 || y >= FRAME_HEIGHT) { printf ("OVERFLOW draw_hor_line(y=%d)!\n", y); return; }
      if (x1 < 0 || x1 >= FRAME_WIDTH) { printf ("OVERFLOW draw_hor_line(x1=%d,x2=%d,y=%d)!\n", x1, x2, y); return; }
      if (x2 < 0 || x2 >= FRAME_WIDTH) { printf ("OVERFLOW draw_hor_line(x1=%d,x2=%d,y=%d)!\n", x1, x2, y); return; }
      if (x2 >= x1) memset (d, color, x2-x1+1);
      //for (x = x1 ; x < x2 ; x++) *d++ = color;
    }
}

void Graphics::SetRGB(int i, int r, int g, int b)
{
  // set a rgb color in the palette of your graphic interface

// dont't touch, please !
  graphicsPalette[i].red = r;
  graphicsPalette[i].green = g;
  graphicsPalette[i].blue = b;
  graphicsPalette_alloc[i] = TRUE;
}

// Keyboard fonctions
Keyboard::Keyboard(int argc, char *argv[])
{
  // Create your keyboard interface
}

Keyboard::~Keyboard(void)
{
  // Destroy your keyboard interface
  Close();
}

int Keyboard::Open(void)
{
  // Open your keyboard interface
  return (1);
}

void Keyboard::Close(void)
{
  // Close your keyboard interface
}

// don't touch, please !
void do_buttonpress (int x, int y, int shift, int alt, int ctrl);
void do_buttonrelease (int x, int y);
void do_mousemotion (int x, int y);
void do_stuff ();
void do_expose ();
void do_keypress (int key, int shift, int alt, int ctrl);


// System loop !
void System::Loop(void)
{
  // don't touch, please !
  Graph->Clear(0);
  Shutdown=0;

  while(!Shutdown)
    {
      do_stuff (); // don't remove, please !
 
      // here include your keyboard interface upgrade
      // use do_keypress(<value>, <shift>, <alt>, <ctrl>)
      // where :
      //    <value> is the ascii value (ex: 'a' for A key, 'b' for B key...)
      //    <shift> if boolean for SHIFT key pressed
      //    <alt> if boolean for ALT key pressed
      //    <ctrl> if boolean for CTRL key pressed
      do_keypress("q", 0, 0, 0);
    } // while (!Shutdown)
}

#endif //COMP_COMPILER_SYSTEM
