/****************************************************************************
 *                                                                          *
 *                      V S P R I T E   E X A M P L E                       *
 * by Eric Cotton                                       *
 *      (c) 1986 Commodore-Amiga, Inc.                                      *
 * Posted to Bix by Andy Finkel
 ****************************************************************************/

#include "vsprite.h"            /* standard includes & definitions */

#define WIDTH 320
#define HEIGHT 200
#define DEPTH 2

extern struct Custom custom;
extern struct Window *OpenWindow(); /* function to open a window */
extern struct ViewPort *ViewPortAddress(); /* function to get ViewPort add. */
extern struct IntuiMessage *GetMsg(); /* function to retrieve IntuiMessage */

extern struct VSprite *MakeVSprite();
extern struct VSprite *MakeBob();

struct GfxBase *GfxBase;        /* pointer to Graphics library */
struct IntuitionBase *IntuitionBase; /* pointer to Intuition library */

struct Window *window;          /* pointer to window */
struct RastPort *rport;         /* pointer to window's RastPort */
struct ViewPort *vport;         /* pointer to window's ViewPort */

struct IntuiMessage *message;   /* pointer to window IDCMP messages */

struct VSprite *MyVSprite, *MyBob;

WORD MySpriteColors[] = {0x0000, 0x00f0, 0x0f00};

WORD MySpriteData[22] = {
    0xfc00, 0x0000,
    0x8200, 0x7c00,
    0xfa00, 0x7c00,
    0xf400, 0x7800,
    0xfa00, 0x7c00,
    0xfd00, 0x6e00,
    0x6e80, 0x0700,
    0x0740, 0x0380,
    0x03a0, 0x01c0,
    0x01c0, 0x0080,
    0x0080, 0x0000,
};

WORD MyBobData[44] = {
    0xfff0, 0x0000,     /* plane 1 */
    0xc00c, 0x0000,
    0xffcc, 0x0000,
    0xff30, 0x0000,
    0xffcc, 0x0000,
    0xfff3, 0x0000,
    0x3cfc, 0xc000,
    0x003f, 0x3000,
    0x000f, 0xcc00,
    0x0003, 0xf000,
    0x0000, 0xc000,

    0x0000, 0x0000,     /* plane 2 */
    0x3ff0, 0x0000,
    0x3ff0, 0x0000,
    0x3fc0, 0x0000,
    0x3ff0, 0x0000,
    0x3cfc, 0x0000,
    0x003f, 0x0000,
    0x000f, 0xc000,
    0x0003, 0xf000,
    0x0000, 0xc000,
    0x0000, 0x0000
};

struct NewWindow nw = {         /* titler-window definition */
    0, 0,               /* LeftEdge, TopEdge */
    WIDTH, HEIGHT,      /* Width, Heigth */
    -1, -1,             /* DetailPen, BlockPen */
    /* IDCMPFlags */
    CLOSEWINDOW|NEWSIZE|MOUSEMOVE|WINDOWDRAG,
    /* Flags */
    ACTIVATE|REPORTMOUSE|SIZEBBOTTOM|WINDOWCLOSE|WINDOWDEPTH
        |WINDOWDRAG|WINDOWSIZING,
    NULL,               /* FirstGadget */
    NULL,               /* CheckMark */
    "VSprite Example",  /* Title */
    NULL,               /* Screen */
    NULL,               /* BitMap */
    80, 25,             /* MinWidth, MinHeight */
    640, 200,           /* MaxWidth, MaxHeight */
    WBENCHSCREEN        /* Type */
};


/****************************************************************************
 *                                M A I N                                   *
 ****************************************************************************/

main()
{       
    BOOL MouseMoved;
    UWORD i;                    /* general purpose index variable */
    UWORD x, y;

    /* open libraries, bail out if there is a problem */
    if((IntuitionBase = (struct IntuitionBase *)
        OpenLibrary("intuition.library", 0)) == NULL)KillVS();
    if((GfxBase = (struct GfxBase *)
        OpenLibrary("graphics.library", 0)) == NULL)KillVS();

    /* open window, abort if unable */
    if((window = OpenWindow(&nw)) == NULL)KillVS();
    rport = window->RPort;
    vport = ViewPortAddress(window);

    SetDrMd(rport, JAM1);

    MakeGelsInfo(rport, 0xfc);

    MyVSprite = MakeVSprite(160, 100, 11, 1, 2, MySpriteData, MySpriteColors);
    AddVSprite(MyVSprite, rport);

    MyBob = MakeBob(160, 100, 11, 2, 2, MyBobData, DEPTH, 0x03, 0x00);
    AddBob(MyBob->VSBob, rport);

    ON_SPRITE;
    SortGList(rport);
    DrawGList(rport, vport);
    RethinkDisplay();

    FOREVER
    {
        Wait(1 << window->UserPort->mp_SigBit);
        MouseMoved = FALSE;
        while(message = GetMsg(window->UserPort))
        {
            switch(message->Class)
            {
                case CLOSEWINDOW:
                    ReplyMsg(message);
                    KillVS();
                    break;
                case MOUSEMOVE:
                    MouseMoved = TRUE;
                    x = message->MouseX;
                    y = message->MouseY;
                    ReplyMsg(message);
                    break;
                default:
                    ReplyMsg(message);
            }
        }

        if(MouseMoved = TRUE)
        {
            MyVSprite->X = x + 20;
            MyVSprite->Y = y + 1;
            if( (x < (window->Width - window->BorderRight - 16))
                && (x > window->BorderLeft) &&
                (y < (window->Height - window->BorderBottom - 16))
                && (y > window->BorderTop))
            {
                MyBob->X = x;
                MyBob->Y = y + 10;
            }
            SortGList(rport);
            Forbid();
            DrawGList(rport, vport);
            Permit();
            RethinkDisplay();
        }
    } 
}


/* ======================================================================== */
/* ==== KillVS: Kill/Exit VSprite Example ================================= */
/* ==== free any allocated memory, close window if open, close any     ==== */
/* ==== open libraries, and make sure WorkBench is open                ==== */
/* ======================================================================== */

KillVS()
{
    DeleteVSprite(MyBob, DEPTH);
    DeleteVSprite(MyVSprite, DEPTH);

    Forbid();
    SortGList(rport);
    DrawGList(rport, vport);
    Permit();
    RethinkDisplay();
    DeleteGelsInfo(rport->GelsInfo);
    
    if(window != NULL)
        CloseWindow(window);

    /* Close any open libraries */
    if(GfxBase != NULL)
        CloseLibrary(GfxBase);
    if(IntuitionBase != NULL)
        CloseLibrary(IntuitionBase);

    /* Open WorkBench, exit */
    OpenWorkBench();
    exit(0);
}


/* =========================== END OF FILE ================================ */


