/*
 * AMIGA_Window.C - AMIGA window driver.
 *
 * Copyright (C) 1992, Lucas Ammon
 *                     University of Berne, Switzerland
 * All rights reserved.
 *
 * This software may be freely copied, modified, and redistributed
 * provided that this copyright notice is preserved on all copies.
 *
 * You may not distribute this software, in whole or in part, as part of
 * any commercial product without the express consent of the authors.
 *
 * There is no warranty or other guarantee of fitness of this software
 * for any purpose.  It is provided solely "as is".
 *
 */

#include <stdlib.h>

#include "AMIGA_Window.h"
#include "Error.h"

extern "C" printf(...);
//___________________________________________________________ AMIGA_Window


AMIGA_Window::AMIGA_Window() {}


void AMIGA_Window::open(int width, int height, const rcString& windowName)
{

  struct NewWindow nw = 
  { 
    0,10,
    400,400,
    3,
    1,
    VANILLAKEY,
    WINDOWDEPTH|WINDOWDRAG|SMART_REFRESH|GIMMEZEROZERO,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    20,
    20,
    640,
    512,
    WBENCHSCREEN             
  };

  printf("Window name: %s ...\n",windowName.chars());

  BaseWindow::open(width, height, windowName);

  WindowName=windowName;
  w_xres=width;
  w_yres=height;
 
  nw.Title=(unsigned char *)WindowName.chars();
  nw.Width=width;
  nw.Height=height;


  if(!(GfxBase = gfxbase = (struct GfxBase *)OpenLibrary("graphics.library",0))) {
    Error(ERR_PANIC,rcString("can't open graphics library"));
  }

  if(!(IntuitionBase = intuitionbase = (struct IntuitionBase *)OpenLibrary("intuition.library",0))){
     CloseLibrary(gfxbase);
     Error(ERR_PANIC,rcString("can't open intuition library"));
  }


  if (!(w = (struct Window *)OpenWindow(&nw) )) {
     CloseLibrary(gfxbase);
     CloseLibrary(intuitionbase);
     Error(ERR_PANIC,rcString("can't open window"));
  }

  rp = w->RPort;

  SetAPen(rp,1);
  SetBPen(rp,0);

}

void AMIGA_Window::close()
{

  printf("Window close ...\n");

  BaseWindow::close();

  CloseWindow(w);              
  CloseLibrary(gfxbase);
  CloseLibrary(intuitionbase);

}

void AMIGA_Window::clear()
{
  printf("Window clear ...\n");

  BaseWindow::clear();

  SetRast(rp,0);

}

char AMIGA_Window::waitForKey()
{

  ULONG MessageClass;
  USHORT code;
  LONG warte;

  printf("Window wait for key ...\n");

  for(;;) {
    if (message = (struct IntuiMessage *)GetMsg(w->UserPort)) {                
      MessageClass = message->Class; 
      code = message->Code;
      ReplyMsg(message);
      if (MessageClass == VANILLAKEY) {
        return(code);  
      }
    }           
  }

}

void AMIGA_Window::writeText(const rcString& msg, int x, int y)
{

  struct IntuiText txt =
  {
    1,0,
    JAM1,
    0,0,
    NULL,
    NULL,
    NULL
  };

  printf("Window text: %s x: %d y: %d ...\n",msg.chars(),x,y);

  txt.IText=(unsigned char *)msg.chars();
  PrintIText(rp,&txt,x,w_yres-y);

}


void AMIGA_Window::drawLine(int x1, int y1, int x2, int y2)
{ 

  Move(rp,x1,w_yres-y1);
  Draw(rp,x2,w_yres-y2);

}


void AMIGA_Window::flush()
{
 
  printf("Window flush\n");
 
  BaseWindow::flush();

}

