/*
** Name:      Blit Worm Demo
** Author:    Paul Manias
** Copyright: DreamWorld Productions (c) 1996-1997.
** SAS/C:     1> sc BlitWorm.c link startup=LIB:gms.o data=far nostackcheck
** Dice:      1> dcc -l0 -mD gms.o BlitWorm.c -o BlitWorm
**
** Blits a Worm to screen using a bob structure.  This version does not
** include the sound from the assembler version.
**
*/

#include <proto/games.h>

extern struct GMSBase *GMSBase;
ULONG PREFSNAME = DEFAULT;

struct GameScreen  *screen;
struct Restorelist *restore;
struct Bob         *Worm;

#define TRUE 1
#define FALSE 0

void Demo(void);
void Wrap(struct Bob *);

void main(void) {
  struct Picture *bobpic;
  struct Picture *background;

  WORD WormFrames[] = {
      0,0,  32,0, 64,0,  96,0, 128,0, 160,0, 192,0, 224,0,
    256,0, 288,0, 0,48, 32,48, 64,48,
    -1,-1
  };

  if (AllocBlitter() == ERR_OK) {
   if (AllocAudio() == ERR_OK) {
    if (background = LoadPicFile("GMS:demos/data/PIC.Green",GETPALETTE|VIDEOMEM)) {

     if (screen = GetScreen()) {
        CopyStructure(background,screen);
        screen->MemPtr1 = background->Data;
        screen->Attrib  = DBLBUFFER;

      if (AddScreen(screen)) {
         CopyBuffer(screen,BUFFER1,BUFFER2);

       if (restore = InitRestore(screen,0,1)) {

          bobpic = GetPicture();
          CopyStructure(background,bobpic);
          bobpic->Options = VIDEOMEM;
          bobpic->File    = "GMS:demos/data/PIC.Rambo";

        if (LoadPic(bobpic)) {

         if (Worm = InitBobTags(screen,TAGS_BOB,NULL,
            BBA_GfxCoords,WormFrames,
            BBA_Width,32,
            BBA_Height,24,
            BBA_XCoord,150,
            BBA_YCoord,150,
            BBA_Attrib,RESTORE|GENMASKS|CLIP,
            BBA_Picture,bobpic,
            TAGEND)) {

           ShowScreen(screen); 
           Demo();
         }
        }
       }
      }
     }
    if (Worm)       { FreeBob(Worm);        Worm = NULL; }
    if (bobpic)     { FreePic(bobpic);      bobpic = NULL; }
    if (restore)    { FreeRestore(restore); restore = NULL; }
    if (screen)     { DeleteScreen(screen); screen = NULL; } 
    if (background) { FreePic(background);  background = NULL; }
    }
   FreeAudio(); 
   }
  FreeBlitter(); 
  }
}

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

void Demo(void)
{
  ULONG mouse=0; 
  UWORD anim=0,fire=FALSE;

  InitJoyPorts(); 

  do
  {
    Restore(screen->Bitmap); 
    DrawBob(Worm); 
    WaitVBL(); 
    SwapBuffers(screen);

    /* Animate the Worm's movements */

    anim++;

    if (fire == FALSE) {
      if (anim > 5) {
        anim = 0;
        Worm->Frame++;
        if (Worm->Frame > 9)
           Worm->Frame = 0;
      }
    }
    else if (anim > 1) {
      anim = 0;
      if (Worm->Frame < 10)
         Worm->Frame = 9;

      Worm->Frame++;

      if (Worm->Frame > 12) {
         if (mouse & MB_LMB)
            Worm->Frame = 11;
         else {
            Worm->Frame = 0;
            fire = FALSE;
         }
      }
    }

    /* Get the user input, wrap the bob around if  */

    mouse = ReadJoyPort(JPORT1,JT_ZBXY);
    Worm->XCoord += GetX(mouse);
    Worm->YCoord += GetY(mouse);
    Wrap(Worm);

    if (mouse & MB_LMB)
       fire = TRUE;

  } while (!(mouse & MB_RMB)); 
}

/*****************************************************************************
** Function: This function will wrap a bob to the other side of a screen if
**           it leaves the bob's screen borders.
**
** Synopsis: Wrap(Bob);
*/

void Wrap(struct Bob *bob)
{
  if (bob->XCoord < -bob->Width)
     bob->XCoord = bob->Bitmap->Width;
  if (bob->XCoord > bob->Bitmap->Width)
     bob->XCoord = -bob->Width;
  if (bob->YCoord < -bob->Height)
     bob->YCoord = bob->Bitmap->Height;
  if (bob->YCoord > bob->Bitmap->Height)
     bob->YCoord = -bob->Height;
}

