#include <intuition/intuition.h>
#include <libraries/arpbase.h>
#include <exec/memory.h>
#include <proto/exec.h>
#include <proto/intuition.h>

#include "messages.h"
#include "metalworx.h"

static struct Image Metalworx_Image =
{
  0, 0, metalworx_iff_width, metalworx_iff_height, metalworx_iff_num_planes,
  (USHORT *)metalworx_iff_data, 0xF, 0, NULL
};

static struct NewWindow Metalworx_window =
{
  -1, -1, metalworx_iff_width, metalworx_iff_height, 0, 0, MOUSEBUTTONS,
  SIMPLE_REFRESH | BORDERLESS | GIMMEZEROZERO, NULL, NULL, NULL, NULL, NULL,
  5, 5, -1, -1, CUSTOMSCREEN
};

extern long conwin;
extern int Nachricht(struct Window *);
extern void WrConWin(long,char*);

int Metalworx(struct Screen *screen)
{
  USHORT *MetalBuffer=NULL;
  struct Window *win;

  if(TypeOfMem(metalworx_iff_data) & MEMF_FAST)             /* Image-Daten im FastMem ? */
  {				      /* dann ins ChipMem kopieren */
    MetalBuffer=(USHORT *)AllocMem(sizeof(metalworx_iff_data),MEMF_CHIP|
      MEMF_PUBLIC);
    if(MetalBuffer==NULL)
    {
      WrConWin(conwin,NO_METAL_MEM);
      return 1;
    }
    movmem(metalworx_iff_data,MetalBuffer,sizeof(metalworx_iff_data));
    Metalworx_Image.ImageData=MetalBuffer;
  }
  Metalworx_window.Screen = screen;
  Metalworx_window.LeftEdge = (640 - metalworx_iff_width) / 2;
  Metalworx_window.TopEdge = (screen->Height - metalworx_iff_height) / 2;
  if ((win = OpenWindow(&Metalworx_window)) == NULL)
    return 1;
  DrawImage(win->RPort, &Metalworx_Image, 0, 0);
  Nachricht(win);
  CloseWindowSafely(win,TRUE);
  if(MetalBuffer)
    FreeMem((UBYTE *)MetalBuffer,sizeof(metalworx_iff_data));
  return 0;
}
