
/*
\\    Snake - demonstriert Pointer & Sprites
*/
#include "TOOL.h"
#define VP ACTIVE_SCREEN->ViewPort
#define RP win->RPort

SHORT xpos,ypos,lastx[90],lasty[90],tail,len,points;
UWORD *CP, *CB;
UWORD Pointer[]={
    0,0,   0xFFFF,0, 0,0xFFFF, 0xFFFF,0xFFFF,   0,0
};
UWORD Ball[]={
    0,0,   0x1800, 0x2000, 0x1C00,0x6000, 0x8C00,0xF200,
    0x8000,0xFE00, 0xC200,0xFE00, 0x7C00,0x7C00,
    0x3800,0x3800, 0,0
};
struct Window *win;
struct SimpleSprite Sprite={
    NULL,7,10,10,2
};

VOID error(meldung)
    STRPTR meldung;
{
    if(meldung) printf("Kann %s nicht öffnen",meldung);
    if(CP)      FreeMem(CP,sizeof(Pointer));
    if(CB)      FreeMem(CB,sizeof(Ball));
    if(win)     CloseWindow(win);
    CloseLib();
    exit(0);
}

VOID Score()
{/* Spielstandsanzeige in Form eines Balkens */
    SHORT col=points>8?3:2,
          top=win->Height-((win->Height-10)/10)*points;

    Block(RP,1,0,10,10,win->Height);
    Block(RP,col,2,top,8,win->Height-2);
}

VOID Init()
{/* ein neues Spiel beginnt */
    SHORT i;

    tail=points=0;
    xpos=ypos=10;
    len=(win->Width+win->Height)/10;       /* Länge von Windowgröße abhängig */
    for(i=0;i<len;i++) lastx[i]=lasty[i]=0;
    SetRast(RP,0);                                       /* RastPort löschen */
    RefreshWindowFrame(win);       /* Rahmen, Titelleiste, Gadgets refreshen */
    Score();
}

VOID main()
{
    SHORT  i,x=2,y=1,SpriteNr,tip,col=2;
    USHORT code;
    ULONG  class=0,idcmp=CLOSEWINDOW|NEWSIZE|MOUSEBUTTONS,
           flags=WINDOWCLOSE|WINDOWDEPTH|WINDOWSIZING|RMBTRAP|ACTIVATE;

    if(OpenLib()) error("Lib's");
    if(!(win=GetWindow(NULL,0,0,640,80,idcmp,flags,"Snake",0))) error("Window");
    if((CP=AllocMem(sizeof(Pointer),MEMF_CHIP))==0) error("Chip-Memory");
    if((CB=AllocMem(sizeof(Ball),MEMF_CHIP))==0)    error("Chip-Memory");
    for(i=0;i<10;i++) CP[i]=Pointer[i];    /* Daten ins Chip-Memory kopieren */
    for(i=0;i<18;i++) CB[i]=Ball[i];
    Sprite.posctldata=&CB[0];           /* Adresse Sprite-Daten bekanntgeben */
    SetRGB4(&VP, 21,12,12,12);
    SetRGB4(&VP, 22,10,10,10);
    SetRGB4(&VP, 23, 8, 8, 8);
    SetPointer(win,&CP[0],3,16,0,0);                /* Mauspointer verändern */
    SpriteNr=GetSprite(&Sprite,2);
    Init();
    while(class!=CLOSEWINDOW){
        class=GetMessage(win,&code,0,0);
        if(class==NEWSIZE) Init();           /* Fenstergröße wurde verändert */
        MoveSprite(&VP,&Sprite,xpos+=x,ypos+=y);     /* neue Sprite-Position */
        lastx[tail]=xpos+8-x;                    /* 1. Position des Schweifs */
        lasty[tail]=ypos+2-y;
        SetDrMd(RP,JAM1|COMPLEMENT);
        DrawEllipse(RP,lastx[tail],lasty[tail],2,1);    /* Kopf des Schweifs */
        tail=tail>len-2?0:tail+1;
        tip =tail>len-2?0:tail+1;              /* Ende des Schweifs löschen: */
        if(lastx[tip]+lasty[tip]) DrawEllipse(RP,lastx[tip],lasty[tip],2,1);
        if(xpos>=win->Width-16) x=-2;                 /* Bewegung nach links */
        else if(xpos<=10)       x= 2;                /* Bewegung nach rechts */
        if(ypos>=win->Height-8){
            y=-1;                                      /* Bewegung nach oben */
            points++;
            if(points>9){
                SetWindowTitles(win,">> GAME-OVER <<     Klick...",-1);
                do{
                    Wait(1<<win->UserPort->mp_SigBit);
                    class=GetMessage(win,&code,0,0);
                }while((code!=SELECTUP)&&(class!=CLOSEWINDOW));
                SetWindowTitles(win,"Snake",-1);
                Init();
            }
            else Score();
        }
        else if(ypos<=10) y=1;                        /* Bewegung nach unten */
        if((win->MouseY>=ypos+9)&&(win->MouseY<=ypos+11)){       /* Schläger */
            if((y>0)&&(xpos>win->MouseX-12)&&(xpos<win->MouseX+28)) y=-1;
        }
    }
    FreeSprite(SpriteNr);
    error(0);
}


