/*
** Sprite example
** --------------
** This example shows you how to set up a 16 colour OCS sprite with a
** doubled X axis (32 pixels width).  Those with hardware experience will
** know that it takes 4 sprite banks to do this successfully in OCS, which
** leaves you with another 4 banks to do with what you will.  You can do
** this same demo in AGA with just 2 banks used.
** 
** The sprite is attached to the mouse, so try moving it around a bit.
*/

#include <stdio.h>
#include <stdlib.h>

#include <exec/types.h>
#include <exec/memory.h>
#include <games/games.h>
#include <proto/games.h>
#include <proto/exec.h>

struct GMSBase *GMSBase;
UWORD  Timer;
APTR   SprMemBase;
ULONG  ZBXY;

struct Sprite Sprite0 = {
   SPV1,                     /* Version number */
   0,                        /* Bank Number 0 */
   0,                        /* Ptr to graphic */
   100,100,                  /* Beginning X/Y positions */
   0,                        /* Current frame */
   16,21,                    /* Width, Height */
   16,                       /* Amt of colours */
   16,                       /* Colour start in palette */
   2,                        /* Amt of planes */
   LORES|XLONG,              /* Resolution attributes */
   0,                        /* Position in relation to playfields */
   0,0                       /* Private */
};

UWORD Palette[] = {
   0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0688,0x0466,0x0344,0x0CC0,0x0980,0x0870,0x0650,
   0x01C2,0x0050,0x0B0B,0x0606,0x0F20,0x0910,0x0BBB,0x0FFF,
   0x00BF,0x0068,0x0568,0x09BF,0x0FF0,0x0EE0,0x0BA0,0x0540
};

struct GameScreen GameScreen = {
   GSV1,                     /* GameScreen Version */
   0,0,0,                    /* Screen_Mem1,2,3 */
   0,                        /* ScreenLink */
   &Palette,                 /* Address of Palette */
   0,                        /* Address of RasterList */
   32,                       /* Amount of colours */
   320,256,320,256,          /* Screen & Pic Height/Width */
   1,                        /* Amt_Planes */
   0,0,                      /* Top Of Screen, X/Y */
   0,0,                      /* X/Y pic offsets */
   SPRITES|NOSPRBDR,         /* Special attributes */
   LORES,                    /* Screen mode */
   INTERLEAVED,              /* Screen type */
   0                         /* Reserved */
};

struct GameScreen *OurScreen = &GameScreen;
struct Sprite *Sparkie = &Sprite0;

/*=========================================================================*/

void main(void)
{
   struct GamesLibrary *GMSBase = (struct GamesLibrary *)
       OpenLibrary("games.library", 0);
   if (GMSBase == NULL) exit(FALSE);

   SetUserPri();
   Sparkie->Data = SmartLoad("GAMESLIB:data/Sparkie.raw",0,0,MEMF_CHIP);

   if (Add_Screen(OurScreen) == NULL) {
      Init_Sprite(OurScreen,Sparkie);
      Update_Sprite(OurScreen,Sparkie);
      Show_Screen(OurScreen);

      ZBXY = Read_Mouse(JPORT1);         /* Initialise the mouse port */

      while (!(ZBXY&MB_LMB)) {

         if (++Timer&0x1) {
            if (Sparkie->Frame == 5) Sparkie->Frame = 0;
            else Sparkie->Frame++;
         }

         ZBXY = Read_Mouse(JPORT1);
         Sparkie->XPos += (BYTE)(ZBXY>>8);
         Sparkie->YPos += (BYTE)ZBXY;
         Wait_OSVBL();
         Update_Sprite(OurScreen, Sparkie);
      }
   }

   FreeMemBlock(Sparkie->Data);
   Delete_Screen(OurScreen);
   CloseLibrary((struct Library *)GMSBase);
}

