/* You "System Include" this because you want the wallpaper
routine to stay in mem even if this task is killed.

    <CTRL-t> to see the hidden text needed for sprite elements.

    <CTRL-r> to add a sprite to a document.
*/

<1>/* Graphics Not Rendered in HTML */


<2>/* Graphics Not Rendered in HTML */


<3>/* Graphics Not Rendered in HTML */


<4>/* Graphics Not Rendered in HTML */



<5>/* Graphics Not Rendered in HTML */



<6>/* Graphics Not Rendered in HTML */



<7>/* Graphics Not Rendered in HTML */


<8>/* Graphics Not Rendered in HTML */


<9>/* Graphics Not Rendered in HTML */


#define TYPES_OF_CRITTERS       3
#define FRAMES_PER_CRITTER      4

U8 *imgs[TYPES_OF_CRITTERS][FRAMES_PER_CRITTER] = {
    {<1>, <2>, <3>, <2>}, 
    {<4>, <5>, <6>, <5>}, 
    {<7>, <8>, <9>, <8>}
};

#define CRITTERS_NUM    16
class Critter
{
    I64 x, y, dx, dy, type;
    F64 t_offset;

} wall_crits[CRITTERS_NUM];

U0 (*old_wall_paper)(CTask *task);

U0 WallPaperFish(CTask *task)
{
    I64      i, j, x, y;
    CDC     *dc = DCAlias(gr.dc2, task);
    Critter *c = wall_crits;

    task->text_attr = CYAN << 4 + WHITE;
    dc->color = BROWN;
    for (i = 0; i < GR_HEIGHT; i += 20) {
        j = 16 * Tri(tS * 10, 20);
        GrLine(dc, 0, GR_HEIGHT - i - j, GR_HEIGHT - i - j, GR_HEIGHT);
    }
    for (i = 0; i < CRITTERS_NUM; i++, c++)
    {
        j = (tS * 4 + c->t_offset) % FRAMES_PER_CRITTER;
        x = c->x >> 16 % GR_WIDTH;
        if (x < 0)
            x += GR_WIDTH;
        y = c->y >> 16 % GR_HEIGHT;
        if (y < 0)
            y += GR_HEIGHT;
        if (c->dx < 0)
        {
            dc->flags |= DCF_SYMMETRY | DCF_JUST_MIRROR;
            DCSymmetrySet(dc, x, y - 1, x, y + 1);
        }
        else
            dc->flags &= ~(DCF_SYMMETRY | DCF_JUST_MIRROR);
        Sprite3(dc, x, y, 0, imgs[c->type][j]);
        c->x += c->dx;  c->y += c->dy;
    }
    DCDel(dc);

    //Uncomment the following if you wish.
    //old_wall_paper(task);
}

U0 WallInit()
{
    I64      i;
    Critter *c;

    if (Fs != sys_task)
    {
        "Must be System Included with SHIFT-F5.\n"
        "(Would crash when code mem was freed.)\n";
        return;
    }
    old_wall_paper = gr.fp_wall_paper;
    c = wall_crits;
    for (i = 0; i < CRITTERS_NUM; i++, c++)
    {
        c->x    = (RandU16 % GR_WIDTH)  << 16;
        c->y    = (RandU16 % GR_HEIGHT) << 16;
        c->type = RandU16 % TYPES_OF_CRITTERS;
        c->dx   = RandI16;
        if (c->dx < 0)
            c->dx -= 0x4000;
        else
            c->dx += 0x4000;
        c->dy       = RandI16;
        c->t_offset = Rand * FRAMES_PER_CRITTER;
    }
    gr.fp_wall_paper = &WallPaperFish;
}

WallInit;