 
   /*
      Programma : BUG.C
      Autore:  Gianni Biagini per Amiga Magazine 
      scopo :  mostrare l'utilizzo di sprites semplici 
   */


#include <exec/types.h>
#include <intuition/intuition.h>
#include <graphics/sprite.h>

struct Intuition *IntuitionBase;
struct GfxBase *GfxBase;
struct SimpleSprite imp0;

/* definiamo qui la prima forma dello sprite */

USHORT imp_data0[]=
{
0,0,
0x0248,0x0248,
0x0248,0x0248,
0x0248,0x0248,
0x03f8,0x03f8,
0x0c04,0x0c04,
0x1203,0x1203,
0x1203,0x1203,
0x0c04,0x0c04,
0x03f8,0x03f8,
0x0248,0x0248,
0x0248,0x0248,
0x0248,0x0248,
0,0
};

/* lo stesso scarafaggino ma con le zampe piegate */

USHORT imp_data1[]=
{
0,0,
0x0092,0x0092,
0x0124,0x0124,
0x0248,0x0248,
0x03f8,0x03f8,
0x0c04,0x0c04,
0x1203,0x1203,
0x1203,0x1203,
0x0c04,0x0c04,
0x03f8,0x03f8,
0x0248,0x0248,
0x0124,0x0124,
0x0092,0x0092,
0,0
};


#define INTUITION_REV  29
#define GRAPHICS_REV   29

struct TextAttr MyFont =
{
 "topaz.font",
 TOPAZ_EIGHTY,
};

/* Apriamo uno screen definito da noi in bassa risoluzione e a 2 colori */

struct NewScreen NewScreen =
{
 0,
 0,
 320,
 200,
 2,
 0, 1,
 NULL,
 CUSTOMSCREEN,
 &MyFont,
 "Amiga Magazine",
 NULL,
 NULL,
 };

main()
{
  struct Screen *Screen;
  struct NewWindow NewWindow;
  struct Window *Window;

  LONG i;
  USHORT mode;
  int gg,m,n,nn;
  SHORT s_id0,s_id1,k,xmove,ymove;

  /* Apriamo le librerie di intuition e quelle grafiche */

  IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", INTUITION_REV);
  if (IntuitionBase == NULL) exit(FALSE);

  GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", GRAPHICS_REV);
  if (GfxBase == NULL) exit(FALSE);
  
  if ((Screen = (struct Screen *)OpenScreen(&NewScreen)) == NULL)
     exit(FALSE);
  
  s_id0=2;

  /* Creiamo una nuova finestra con gadget di chiusura */
  NewWindow.LeftEdge = 0;
  NewWindow.TopEdge = 30;
  NewWindow.Width = 320;
  NewWindow.Height = 100;
  NewWindow.DetailPen = 0;
  NewWindow.BlockPen = 1;
  NewWindow.Title = "Prova Sprites";
  NewWindow.Flags = WINDOWCLOSE|SMART_REFRESH|BORDERLESS|WINDOWDRAG;
  NewWindow.IDCMPFlags = CLOSEWINDOW;
  NewWindow.Type = CUSTOMSCREEN;
  NewWindow.FirstGadget = NULL;
  NewWindow.CheckMark = NULL;
  NewWindow.Screen = Screen;
  NewWindow.BitMap = NULL;
  NewWindow.MinWidth = 2;
  NewWindow.MinHeight = 6;
  NewWindow.MaxWidth = 640;
  NewWindow.MaxHeight = 220;

  if (( Window = (struct Window *)OpenWindow(&NewWindow) ) == NULL)
      exit(FALSE);

  

  if((s_id0=GetSprite(&imp0,2))==-1)
  exit();

  imp0.x=640;
  imp0.y=60;
  imp0.height=12;

  
  switch(s_id0)
  {
  case0:
  case1: k=16;
         break;
  case2:
  case3: k=20;
         break;
  case4:
  case5: k=24;
         break;
  case6:
  case7: k=28;
         break;
  }

  SetRGB4(&Screen->ViewPort,k+1,12,3,8);
  SetRGB4(&Screen->ViewPort,k+2,13,13,13);
  SetRGB4(&Screen->ViewPort,k+3,4,4,15);

  /*Posizioniamo lo sprite alle coordinate imp0.x e imp0.y connettendolo
    alla viewport */
  ChangeSprite(&Screen->ViewPort,&imp0,&imp_data0);
  MoveSprite(0,&imp0,imp0.x,imp0.y);
  
  xmove=-1;
  ymove=0;
  
  /* facciamo ora camminare il nostro scarafaggino */

  for (n=0;n<=2;n++)
  {
     gg=0;
        imp0.x=320;
     while(gg++<320)
        {
         if((gg%2)==0)
          {
          MoveSprite(0,&imp0,imp0.x+xmove,imp0.y+ymove);
          ChangeSprite(0,&imp0,&imp_data0);
          WaitTOF();
          }
         else
          {
           MoveSprite(0,&imp0,imp0.x+xmove,imp0.y+ymove);
           ChangeSprite(0,&imp0,&imp_data1);
           WaitTOF();
          }
         }
       }

   /* liberiamo la memoria allocata */

  FreeSprite(s_id0);
  FreeSprite(s_id1);   

  /* si esce quando l'utente chiude la finestra */
  Wait(1 << Window->UserPort->mp_SigBit);
  CloseWindow(Window);
  CloseScreen(Screen);
  exit(TRUE);
}
   /* Fine programma BUG */
